![Mr bean movie holiday](https://loka.nahovitsyn.com/63.jpg)
This is correct as long as no wild CSS rules are involved. The very simplest approach was already posted here.
![get mouse coordinates javascript get mouse coordinates javascript](https://vbook.pub/img/crop/300x300/plon0r13j8w3.jpg)
(from MDN) The amount of scrolling that has been done of the viewport area (orĪny other scrollable element) is taken into account when computing the This includes scrolling, so stuff like scrollTop is not needed: The HTMLElement.getBoundingClientRect method is designed to to handle actual screen position of any element. You can also go with clientX and clientY properties to get mouse coordinates. For the sake of simplicity, we are going to get the position of the mouse pointer whenever we click or move anywhere in the document. I'm not sure what's the point of all these answers that loop through parent elements and do all kinds of weird stuff. The pageX property will give us horizontal coordinates and the pageY property will give us vertical coordinates.
![get mouse coordinates javascript get mouse coordinates javascript](https://i.stack.imgur.com/aQs6H.jpg)
function relMouseCoords(event)%)` Ĭanvas deliberately has differnt CSS size vs drawingbuffer size
![get mouse coordinates javascript get mouse coordinates javascript](https://codingclubuc3m.rbind.io/post/2020-02-11_files/buscocolegios_xml/developer_tools.png)
#Get mouse coordinates javascript code
This code works perfectly for me, tested in Firefox and Safari but should work for all. To account for this you need to loop through the chain of offsetParents, beginning with the canvas element itself. So as a public service, I offer this page which has JavaScript examples for finding the coordinates of the mouse for different reference points.
classmouse-click>MOUSE CLICK AREA