I'm having trouble in getting the mouse position w.r.t canvas.
There are two cases:
1) If there is no div element surrounding the canvas div then I was able to get the mouse position.
2) When the canvas is wrapped in a div
then offsetLeft
and offsetTop
do not work as expected
What accounts for this difference?
The dimension of the canvas is found using the getBoundingClientRect() function. This method returns the size of an element and its position relative to the viewport. The position of x-coordinate of the mouse click is found by subtracting the event's x position with the bounding rectangle's x position.
Simple answer #1 use offsetX and offsetY offsetX; const y = e. offsetY; }); This answer works in Chrome, Firefox, and Safari.
- GeeksforGeeks How to get the coordinates of a mouse click on a canvas element ? The coordinates of the mouse whenever a click takes place can be found by detecting the click event with an event listener and finding the event’s x and y position. A function is created which takes in the canvas element and event as parameters.
The e.clientX and e.clientY will get the mouse positions relative to the top of the document, to change this to be based on the top of the canvas we subtract the left and right positions of the canvas from the client X and Y.
To detect the click, the canvas element is first selected using the querySelector () method. The addEventListener () method is used on this element to listen the ‘mousedown’ event. This event is triggered whenever a mouse button is pressed down.
The position of x-coordinate of the mouse click is found by subtracting the event’s x position with the bounding rectangle’s x position. The x position of the event is found using the ‘clientX’ property. The x position of the canvas element, i.e. the left side of the rectangle can be found using the ‘left’ property.
You need a function to get the position of the canvas element:
function findPos(obj) {
var curleft = 0, curtop = 0;
if (obj.offsetParent) {
do {
curleft += obj.offsetLeft;
curtop += obj.offsetTop;
} while (obj = obj.offsetParent);
return { x: curleft, y: curtop };
}
return undefined;
}
And calculate the current position of the cursor relative to that:
$('#canvas').mousemove(function(e) {
var pos = findPos(this);
var x = e.pageX - pos.x;
var y = e.pageY - pos.y;
var coordinateDisplay = "x=" + x + ", y=" + y;
writeCoordinateDisplay(coordinateDisplay);
});
The values of offsetLeft
and offsetTop
are relative to offsetParent
, which is your div
node. When you remove the div
they're relative to the body
, so there is no offset to subtract.
Similary, e.pageX
and e.pageY
give the position of the cursor relative to the document. That's why we subtract the canvas's offset from those values to arrive at the true position.
An alternative for positioned elements is to directly use the values of e.layerX
and e.layerY
. This is less reliable than the method above for two reasons:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With