Skip to content

Instantly share code, notes, and snippets.

@dexit
Forked from k1sul1/mousemove.ts
Created June 27, 2023 20:30
Show Gist options
  • Select an option

  • Save dexit/bbc41a3c2f7247dce6a0fa61d0b9ba4b to your computer and use it in GitHub Desktop.

Select an option

Save dexit/bbc41a3c2f7247dce6a0fa61d0b9ba4b to your computer and use it in GitHub Desktop.
I spent way too much time digging the correct properties. For future reference.
function onMouseMove(event, initialX: number, initialY: number) {
const { clientX, clientY, movementX, movementY } = event
const moving = {
left: movementX < 0
right: movementX > 0
up: movementY < 0
down: movementY > 0
}
// Use these to determine how much the cursor has moved from initialX & Y
const xOffset = clientX - initialX // Negative value means left, positive means right
const yOffset = clientY - initialY // Positive value means down, negative means up
return {
xOffset, yOffset,
moving
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment