Skip to content

Instantly share code, notes, and snippets.

@bbuecherl
Created May 15, 2014 20:17
Show Gist options
  • Save bbuecherl/2eb62d43eb8770fd021e to your computer and use it in GitHub Desktop.
Save bbuecherl/2eb62d43eb8770fd021e to your computer and use it in GitHub Desktop.
normalize pointer/mouse/touch events
var normalizeEvent = ( function( ) {
var getPixels = function( style ) {
switch( style ) {
case "thick":
return 6;
case "medium":
return 4;
case "thin":
return 2;
default:
return parseInt( style, 10 );
}
};
return function( e ) {
e = e || window.event;
var target = e.target || e.srcElement,
style = target.currentStyle || window.getComputedStyle(target, null),
rect = target.getBoundingClientRect();
e.sceneX = e.clientX - rect.left;
e.sceneY = e.clientY - rect.top;
if( style.borderStyle !== "" && style.borderStyle !== "none" ) {
e.sceneX -= getPixels( style.borderLeftWidth );
e.sceneY -= getPixels( style.borderTopWidth );
}
try {
e.target = target;
} catch( ex ) {}
};
}( ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment