Currently use the following quote to detect mouse movement direction:
window.addEventListener("mousemove", function (e) {
if (time > Date.now() - 100) return
time = Date.now();
var movementX = e.movementX;
var movementY = e.movementY;
if (movementX < 0) {
// left
} else if (movementX > 0) {
// right
} else if (movementY < 0) {
// up
} else if (movementY > 0) {
// down
}
});
But it stops working when one side of the screen is reached, in this case movementX or movementY are equal to zero and my conditions stop working. For ex., the cursor has reached the left side of the window. User tries to move cursor left, but I can not detect it – movementX is equal to 0.
I tried to cancel the default movement (e.preventDefault()
) and to always keep the cursor in the center by sending another mouse movement event. But it doesn’t work.
What else should I consider?