1

I need to get the mouse position in canvas, I am using this:

canvas.onmousedown = function(e){mouseIsDown = true; mouse.x = event.offsetX; mouse.y = event.offsetY};
canvas.onmouseup = function(e){mouseIsDown = false};
canvas.onmousemove = function(event){if(mouseIsDown) {mouse.x = event.offsetX; mouse.y = event.offsetY} };

where mouse is a vec defined by:

function vec(x,y) {
this.x = x;
this.y = y;
this.length = function() { Math.sqrt(x*x + y*y); }
this.normalize = function() {
    this.x /= this.length();
    this.y /= this.length();
}

}

and I am drawing a dot in mouse position with:

gfx.fillStyle = "#FFFFFF";
gfx.fillRect(mouse.x,mouse.y,1,1);

and it works if I don't touch canvas' dimensions, but if I modify them (in a separated css file) the dot starts doing strange things, it seems like the code is reading the mouse position in a canvas with the dimension“s unmodified.

Mocoma
  • 79
  • 1
  • 3
  • 6

0 Answers0