Here's a copy / pasta of a small part of my drawing application. Notice that I use canvas offsets in my calculations. I also have a zoom function that scales the canvas, so given that I added it to the calculation of the mouse cursor.
$('canvas').mousemove(function(e) { // x and y are globals, x inits to null, mousedown sets x, mouseup returns x to null if (x==null) return; x = (100/$('#zoom').val())*(e.pageX - $(this).offset().left); y = (100/$('#zoom').val())*(e.pageY - $(this).offset().top); $('#debug').html(x+', '+y); // constantly update (x,y) position in a fixed div during debugging });
MaKR Dec 24 '13 at 15:41 2013-12-24 15:41
source share