Mouse position

Hi guys there is any way to get mouse position when mouse is idle

No. You have to wait for a mouse move event and keep track of the last position it was in.

You can also subscribe and listen to the mouse move event and cache the last known position of the mouse.

When you require the idle mouse position, it would be last cached position.

@Leonidas sir you have any example or how can I do this please explain me

You’ll get the coordinates only when the mouse moves.
If the mouse doesn’t move, is because is still in the last known position.
If you save the position everytime you get a new one, you know where the mouse is, even if it doesn’t move:

const lastKnownPosition = [null, null];
canvas.addEventListener('mousemove', function(event) {
    lastKnownPosition[0] = event.screenX;
    lastKnownPosition[1] = event.screenY;
});

As a detail, I created a single Array to save the last known position only once, and change its content later. This is to avoid creating a new Array everytime there is a new event.

3 Likes