Skip to content

Instantly share code, notes, and snippets.

@hinell
Created February 2, 2019 19:35
Show Gist options
  • Save hinell/75f38fd3db216957c4c6ba81c3ef6456 to your computer and use it in GitHub Desktop.
Save hinell/75f38fd3db216957c4c6ba81c3ef6456 to your computer and use it in GitHub Desktop.
MouseEventX
// 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