Javascript track mouse pointer 1

Javascript track mouse pointer

var pointerX = -1;
var pointerY = -1;
document.onmousemove = function(event) {
	pointerX = event.pageX;
	pointerY = event.pageY;
}
setInterval(pointerCheck, 1000);
function pointerCheck() {
	console.log('Cursor at: '+pointerX+', '+pointerY);
}

Here is what the above code is Doing:
1. We’re creating a variable called pointerX and setting it to -1.
2. We’re creating a variable called pointerY and setting it to -1.
3. We’re creating a function called pointerCheck that will run every second.
4. We’re creating a function called document.onmousemove that will run every time the mouse moves.
5. We’re setting the pointerX variable to the current X position of the mouse.
6. We’re setting the pointerY variable to the current Y position of the mouse.
7. We’re logging the current position of the mouse to the console.

Now, let’s add some code to the pointerCheck function to make it do something useful.

Similar Posts