JavaScript | Coordinates of mouse
The top left corner of the screen is (0, 0) i,e, X and Y coordinate is (0, 0). This means that vertical zero is topmost point and horizontal zero is the leftmost point.
So, the task is to find the coordinates of the mouse over the screen.
It can be find using the clientX and clientY property:
- ClientX: It gives the horizontal coordinate of the event.
- ClientY: It gives the vertical coordiante of the mouse.
Syntax:
For X coordinate: event.ClientX For Y coordiante: event.ClientY
Below is the required implementation:
<html> <head> <script> // coordinate function that calculate the X // coordinate and Y coordinates function coordinate(event) { // clientX gives horizontal coordinate var x = event.clientX; // clientY gives vertical coordinates var y = event.clientY; document.getElementById("X").value = x; document.getElementById("Y").value = y; } </script> </head> <!-- onmousemove event is called when the mouse pointer is moving over the screen --> <!-- calling of coordinate function --> <body onmousemove="coordinate(event)"> <!-- X coordinate is stored in X-coordinate variable --> X-coordinate <input type="text" id="X"> <br> <br> <!-- Y coordinate is stored in Y-coordinate variable --> Y-coordinate <input type="text" id="Y"> </body> </html> |
chevron_right
filter_none
Output:

Recommended Posts:
- JavaScript for Capturing mouse positions after every given interval
- p5.js | Mouse | pmouseY
- p5.js | Mouse | mouseReleased()
- p5.js | Mouse | mouseButton
- p5.js | Mouse | pmouseX
- p5.js | Mouse | mouseY
- p5.js | Mouse | winmouseY
- p5.js | Mouse | mouseIsPressed
- p5.js | Mouse | winmouseX
- p5.js | Mouse | mouseX
- p5.js | Mouse | mouseClicked() Function
- p5.js | Mouse | mouseDragged() Function
- p5.js | Mouse | mouseMoved() Function
- p5.js | Mouse | mousePressed() Function
- How to position a div at specific coordinates ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




