Skip to content

Instantly share code, notes, and snippets.

@yushulx
Created October 13, 2020 03:18
Show Gist options
  • Save yushulx/a18b40ec81752877cb2c6d2c5e053cfa to your computer and use it in GitHub Desktop.
Save yushulx/a18b40ec81752877cb2c6d2c5e053cfa to your computer and use it in GitHub Desktop.
function clearOverlay() {
overlayCtx.clearRect(0, 0, overlay.width, overlay.height);
overlayCtx.strokeStyle = '#ff0000';
overlayCtx.lineWidth = 5;
}
overlay.addEventListener('mousedown', e => {
startX = e.offsetX;
startY = e.offsetY;
isDrawing = true;
clearOverlay();
overlay.style.cursor = "crosshair";
});
overlay.addEventListener('mousemove', e => {
if (isDrawing) {
clearOverlay();
overlayCtx.beginPath();
overlayCtx.rect(startX, startY, e.offsetX - startX, e.offsetY - startY);
overlayCtx.stroke();
}
mousePosition.innerHTML = "Cursor: (" + e.offsetX + ", " + e.offsetY + ")";
});
overlay.addEventListener('mouseup', e => {
if (isDrawing) {
isDrawing = false;
mousePosition.innerHTML = "Cursor: (" + e.offsetX + ", " + e.offsetY + ")";
region.innerHTML = "Decode a region: (" + startX + ", " + startY + ", " + e.offsetX + ", " + e.offsetY + "). ";
overlay.style.cursor = "default";
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment