
We can get the mouse coordinates of the location we clicked on on the canvas with a few properties from the mousedown event object. This all works great.except.1 answer 1 vote: This is all kudos to MarkPond who provided the answer in the comments above.Have you looked into MouseEvent.pageX and MouseEvent.pageY instead. To get the mouse coordinates without doing the calculations. this.hoverPositionTop event.clientY this.hoverPositionLeft event.clientX. The clientX property returns the horizontal coordinate (according to the client area) of the mouse pointer when a mouse event was triggered. Then we can write the following JavaScript code: const canvas = document.querySelector('canvas') const ctx = canvas.getContext("2d") ctx.moveTo(0, 0) ctx.lineTo(200, 100) ctx.stroke() const getCursorPosition = (canvas, event) => ) On this page, press and hold the Alt key, then press the Z key to toggle the X/Y coordinates layer on. We can get the coordinates of a mouse click from the event object we get from the mousedown event handler.įor instance, we can write the following HTML: Cursor X & Y Coordinates plus Browser Access Keys. In this article, we’ll look at how to get the coordinates of a mouse click on a canvas element. The variable pos is the position of the point in 3D space, "under the mouse", and in the plane z=0.ĮDIT: If you need the point "under the mouse" and in the plane z = targetZ, replace the distance computation with: var distance = ( targetZ - ) / vec.Sometimes, we may want to get the coordinates of a mouse on an HTML canvas element.

First we set up an event listener for our event: document.addEventListener ('mousemove. If you like simplicity but still want cross-browser.


For a more detailed list of mouse events you could have a read of this. Top 5 Answer for javascript - How do I get the coordinates of a mouse click on a canvas element. In this case we will use ‘mousemove’ to log the current X and Y coordinates of the mouse to the console. This means that if you add a mousemove event listener to the. To get the current mouse position we are going to trigger a mouse event. Pos.copy( camera.position ).add( vec.multiplyScalar( distance ) ) So we have a new origin for the canvas, but while the canvas knows this, the browser does not. ( event.clientY / window.innerHeight ) * 2 + 1, ( event.clientX / window.innerWidth ) * 2 - 1, Var pos = new THREE.Vector3() // create once and reuse You can do that like so: var vec = new THREE.Vector3() // create once and reuse Possible to get the current mouse coords with Javascript javascript. 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.

You just need to extend that ray, from the camera position, until the z-coordinate of the tip of the ray is zero. The pageX property will give us horizontal coordinates and the pageY property will give us vertical coordinates. So let’s set a handler on mousemove to track coordinates and remember them. The only way to get coordinates is to listen for mouse events, like mousemove, and take coordinates from the event object. There’s no function like getCurrentMouseCoordinates(). Using vector.unproject( camera ) you can get a ray pointing in the direction you want. A DOM element whose position we want to transform the mouse coordinates to. Unfortunately, there’s no way to get current mouse coordinates in JavaScript. You do not need to have any objects in your scene to do this.
