html
<!DOCTYPE html>
<html>
<head>
<title>Image Viewer with Selections</title>
<style>
body, html {
margin: 0;
padding: 0;
overflow: hidden;
user-select: none;
height: 100%;
width: 100%;
}
#canvas {
display: block;
background-color: #333;
}
#toolbar {
position: fixed;
top: 10px;
left: 10px;
z-index: 10;
background: rgba(255,255,255,0.8);
padding: 10px;
border-radius: 5px;
}
#toolbar input, #toolbar button {
margin: 5px;
}
</style>
</head>
<body>
<div id="toolbar">
<input type="file" id="fileInput" accept="image/*">
<button id="saveSelections">Save Selections</button>
</div>
<canvas id="canvas"></canvas>
<script>
// JavaScript code
let canvas = document.getElementById('canvas');
let ctx = canvas.getContext('2d');
// Variables for image and transformations
let loadedImage = null;
let scale = 1, translateX = 0, translateY = 0;
let isPanning = false;
let startPanX = 0, startPanY = 0;
let isSelecting = false;
let selectionStartX = 0, selectionStartY = 0;
let currentSelection = null;
let selections = [];
let isCtrlPressed = false;
let isResizingSelection = false;
let currentHandle = null;
let isSpacePressed = false;
// Set canvas size to window size
function resizeCanvas() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
draw();
}
window.addEventListener('resize', resizeCanvas);
resizeCanvas();
// Load image when selected
document.getElementById('fileInput').addEventListener('change', function(e) {
if (e.target.files && e.target.files[0]) {
let imgFile = e.target.files[0];
let img = new Image();
img.onload = function() {
loadedImage = img;
initView();
draw();
}
img.src = URL.createObjectURL(imgFile);
}
});
function initView() {
scale = 1;
translateX = (canvas.width - loadedImage.width) / 2;
translateY = (canvas.height - loadedImage.height) / 2;
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
if (loadedImage) {
ctx.save();
ctx.translate(translateX, translateY);
ctx.scale(scale, scale);
ctx.drawImage(loadedImage, 0, 0);
drawSelections();
ctx.restore();
} else {
// Placeholder
ctx.fillStyle = 'gray';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = 'white';
ctx.font = '20px Arial';
ctx.textAlign = 'center';
ctx.fillText('Please select an image', canvas.width/2, canvas.height/2);
}
}
function drawSelections() {
ctx.lineWidth = 2 / scale; // Keep line width consistent
ctx.strokeStyle = 'red';
ctx.fillStyle = 'rgba(255, 0, 0, 0.2)';
for (let sel of selections) {
ctx.save();
ctx.beginPath();
ctx.rect(sel.x, sel.y, sel.width, sel.height);
ctx.fill();
ctx.stroke();
// Draw handles
drawSelectionHandles(sel);
ctx.restore();
}
}
function drawSelectionHandles(sel) {
let handleSize = 8 / scale;
let handlePositions = getHandlePositions(sel);
ctx.fillStyle = 'blue';
ctx.strokeStyle = 'white';
for (let pos of handlePositions) {
ctx.beginPath();
ctx.rect(pos.x - handleSize / 2, pos.y - handleSize / 2, handleSize, handleSize);
ctx.fill();
ctx.stroke();
}
}
function getHandlePositions(sel) {
return [
{ x: sel.x, y: sel.y }, // Top-left
{ x: sel.x + sel.width, y: sel.y }, // Top-right
{ x: sel.x, y: sel.y + sel.height }, // Bottom-left
{ x: sel.x + sel.width, y: sel.y + sel.height } // Bottom-right
];
}
// Handle mouse wheel for zooming
canvas.addEventListener('wheel', function(e) {
e.preventDefault();
if (!loadedImage) return;
let delta = e.deltaY > 0 ? -1 : 1;
let zoomFactor = 1.1;
let mouseX = e.offsetX;
let mouseY = e.offsetY;
// Convert mouse coordinates to image coordinates
let x = (mouseX - translateX) / scale;
let y = (mouseY - translateY) / scale;
if (delta > 0) {
// Zoom in
scale *= zoomFactor;
} else {
// Zoom out
scale /= zoomFactor;
}
// Adjust translateX and translateY to zoom around the cursor position
translateX = mouseX - x * scale;
translateY = mouseY - y * scale;
draw();
}, { passive: false });
// Handle mouse events
canvas.addEventListener('mousedown', function(e) {
e.preventDefault();
if (e.button === 1 || isSpacePressed) { // Middle mouse button or spacebar + drag for panning
isPanning = true;
startPanX = e.clientX;
startPanY = e.clientY;
} else if (isCtrlPressed && e.button === 0){ // Ctrl + Left mouse button
startSelection(e);
} else {
checkHandleGrabbing(e);
}
});
canvas.addEventListener('mousemove', function(e) {
if (isPanning) {
let dx = e.clientX - startPanX;
let dy = e.clientY - startPanY;
translateX += dx;
translateY += dy;
startPanX = e.clientX;
startPanY = e.clientY;
draw();
} else if (isSelecting) {
updateSelection(e);
} else if (isResizingSelection) {
resizeSelection(e);
} else {
updateCursor(e);
}
});
canvas.addEventListener('mouseup', function(e) {
if (e.button === 1 || isSpacePressed) { // Middle mouse button or spacebar + drag for panning
isPanning = false;
} else if (isSelecting) {
finishSelection(e);
} else if (isResizingSelection) {
finishResizingSelection(e);
}
});
canvas.addEventListener('mouseleave', function(e) {
isPanning = false;
isSelecting = false;
isResizingSelection = false;
});
function startSelection(e) {
if (!loadedImage) return;
isSelecting = true;
// Get mouse position in image coordinates
let x = (e.offsetX - translateX) / scale;
let y = (e.offsetY - translateY) / scale;
selectionStartX = x;
selectionStartY = y;
currentSelection = { x: x, y: y, width: 0, height: 0 };
selections.push(currentSelection);
}
function updateSelection(e) {
let x = (e.offsetX - translateX) / scale;
let y = (e.offsetY - translateY) / scale;
currentSelection.width = x - selectionStartX;
currentSelection.height = y - selectionStartY;
draw();
}
function finishSelection(e) {
isSelecting = false;
// Remove selection if too small
if (Math.abs(currentSelection.width) < 5 || Math.abs(currentSelection.height) < 5) {
selections.pop();
}
currentSelection = null;
draw();
}
function checkHandleGrabbing(e) {
if (!loadedImage) return;
let x = (e.offsetX - translateX) / scale;
let y = (e.offsetY - translateY) / scale;
for (let sel of selections) {
let handles = getHandlePositions(sel);
for (let i = 0; i < handles.length; i++) {
let pos = handles[i];
let handleSize = 8 / scale;
if (x >= pos.x - handleSize / 2 && x <= pos.x + handleSize / 2 &&
y >= pos.y - handleSize / 2 && y <= pos.y + handleSize / 2) {
// Start resizing
isResizingSelection = true;
currentSelection = sel;
currentHandle = i;
return;
}
}
}
}
function resizeSelection(e) {
let x = (e.offsetX - translateX) / scale;
let y = (e.offsetY - translateY) / scale;
let sel = currentSelection;
switch (currentHandle) {
case 0: // Top-left corner
sel.width += sel.x - x;
sel.height += sel.y - y;
sel.x = x;
sel.y = y;
break;
case 1: // Top-right corner
sel.width = x - sel.x;
sel.height += sel.y - y;
sel.y = y;
break;
case 2: // Bottom-left corner
sel.width += sel.x - x;
sel.x = x;
sel.height = y - sel.y;
break;
case 3: // Bottom-right corner
sel.width = x - sel.x;
sel.height = y - sel.y;
break;
}
draw();
}
function finishResizingSelection(e) {
isResizingSelection = false;
currentSelection = null;
currentHandle = null;
}
function updateCursor(e) {
if (!loadedImage) return;
let x = (e.offsetX - translateX) / scale;
let y = (e.offsetY - translateY) / scale;
let overHandle = false;
for (let sel of selections) {
let handles = getHandlePositions(sel);
for (let i = 0; i < handles.length; i++) {
let pos = handles[i];
let handleSize = 8 / scale;
if (x >= pos.x - handleSize / 2 && x <= pos.x + handleSize / 2 &&
y >= pos.y - handleSize / 2 && y <= pos.y + handleSize / 2) {
// Change cursor based on handle position
let cursors = ['nwse-resize', 'nesw-resize', 'nesw-resize', 'nwse-resize'];
canvas.style.cursor = cursors[i];
overHandle = true;
return;
}
}
}
if (!overHandle) {
canvas.style.cursor = 'default';
}
}
// Save selections as images
document.getElementById('saveSelections').addEventListener('click', function() {
saveSelections();
});
function saveSelections() {
for (let i = 0; i < selections.length; i++) {
let sel = selections[i];
// Create an off-screen canvas to extract the selected area
let tempCanvas = document.createElement('canvas');
tempCanvas.width = Math.abs(sel.width);
tempCanvas.height = Math.abs(sel.height);
let tempCtx = tempCanvas.getContext('2d');
// Handle negative widths/heights
let sx = sel.width >= 0 ? sel.x : sel.x + sel.width;
let sy = sel.height >= 0 ? sel.y : sel.y + sel.height;
let sw = Math.abs(sel.width);
let sh = Math.abs(sel.height);
tempCtx.drawImage(loadedImage, sx, sy, sw, sh, 0, 0, sw, sh);
// Convert to image and download
tempCanvas.toBlob(function(blob) {
let link = document.createElement('a');
link.download = 'selection_' + (i + 1) + '.png';
link.href = URL.createObjectURL(blob);
link.click();
}, 'image/png');
}
}
// Handle key events
window.addEventListener('keydown', function(e) {
if (e.key === 'Control') isCtrlPressed = true;
if (e.key === ' ') isSpacePressed = true;
});
window.addEventListener('keyup', function(e) {
if (e.key === 'Control') isCtrlPressed = false;
if (e.key === ' ') isSpacePressed = false;
});
// Prevent default middle mouse button scroll
window.addEventListener('mousedown', function(e) {
if (e.button === 1) {
e.preventDefault();
}
});
// Prevent context menu on canvas
canvas.oncontextmenu = function(e) { e.preventDefault(); }
</script>
</body>
</html>