got canvas resizing working with the watcharea coordinates

This commit is contained in:
BroodjeAap 2020-07-30 18:34:14 +00:00
parent 87b50bdeb2
commit 70ff965170

View file

@ -21,10 +21,10 @@ class WatchArea {
ctx.lineWidth = "3"; ctx.lineWidth = "3";
ctx.strokeStyle = "rgb(" + this.R + ", " + this.G + ", " + this.B + ")"; ctx.strokeStyle = "rgb(" + this.R + ", " + this.G + ", " + this.B + ")";
ctx.rect( ctx.rect(
this.x0, this.x0 * (canvas.width / img.width),
this.y0, this.y0 * (canvas.height / img.height),
this.x1 - this.x0, (this.x1 - this.x0) * (canvas.width / img.width),
this.y1 - this.y0 (this.y1 - this.y0) * (canvas.height / img.height)
); );
ctx.stroke(); ctx.stroke();
} }
@ -51,15 +51,16 @@ document.addEventListener("DOMContentLoaded", function(){
}; };
canvas.addEventListener("mousedown", e => { canvas.addEventListener("mousedown", e => {
watchArea.x0 = e.offsetX; watchArea.x0 = Math.round(e.offsetX * (img.width / canvas.width));
watchArea.y0 = e.offsetY; watchArea.y0 = Math.round(e.offsetY * (img.height / canvas.height));
isDrawing = true; isDrawing = true;
}); });
canvas.addEventListener("mousemove", e => { canvas.addEventListener("mousemove", e => {
if(isDrawing === true) { if(isDrawing === true) {
watchArea.x1 = e.offsetX; watchArea.x1 = Math.round(e.offsetX * (img.width / canvas.width));
watchArea.y1 = e.offsetY; watchArea.y1 = Math.round(e.offsetY * (img.height / canvas.height));
console.log(watchArea.x1, watchArea.y1)
draw(); draw();
update(); update();
} }
@ -80,6 +81,7 @@ function draw(){
var rect = contentDiv.getBoundingClientRect(); var rect = contentDiv.getBoundingClientRect();
canvas.width = Math.floor(rect.width * 0.9); canvas.width = Math.floor(rect.width * 0.9);
canvas.height = (img.height / img.width) * canvas.width; canvas.height = (img.height / img.width) * canvas.width;
ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.clearRect(0, 0, canvas.width, canvas.height);
drawImage(); drawImage();
watchAreas.forEach(watchArea => watchArea.draw(ctx)); watchAreas.forEach(watchArea => watchArea.draw(ctx));