Created
February 2, 2019 19:35
-
-
Save hinell/75f38fd3db216957c4c6ba81c3ef6456 to your computer and use it in GitHub Desktop.
MouseEventX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Interface for relative cursor position calculation | |
const MouseEventX = class extends MouseEvent { | |
constructor(name, init, target){ | |
super(name, init) | |
if(target instanceof HTMLElement){ | |
let tRect = target.getBoundingClientRect(); | |
tRect.widthHalf = tRect.width / 2; | |
tRect.heigtHalf = tRect.height/ 2; | |
// Local cursor coordinates | |
let x = init.clientX - tRect.x; | |
let y = init.clientY - tRect.y; | |
// Local cursor coordinates relatively to center | |
this.local = { | |
x, y, | |
center: { | |
x: (x - tRect.widthHalf) / tRect.widthHalf | |
, y: (y - tRect.heigtHalf) / tRect.heigtHalf | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment