get x and y position of mouse in javascript code example
Example 1: how to get mouse x and y in javascript
document.addEventListener("mousemove", () => {
let mousex = event.clientX;
let mousey = event.clientY;
console.log([mousex, mousey]);
});
Example 2: get x y z position of mouse javascript
var vec = new THREE.Vector3();
var pos = new THREE.Vector3();
vec.set(
( event.clientX / window.innerWidth ) * 2 - 1,
- ( event.clientY / window.innerHeight ) * 2 + 1,
0.5 );
vec.unproject( camera );
vec.sub( camera.position ).normalize();
var distance = - camera.position.z / vec.z;
pos.copy( camera.position ).add( vec.multiplyScalar( distance ) );