Created
May 15, 2014 20:17
-
-
Save bbuecherl/2eb62d43eb8770fd021e to your computer and use it in GitHub Desktop.
normalize pointer/mouse/touch events
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
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