Last active
December 14, 2015 18:09
-
-
Save molant/5127035 to your computer and use it in GitHub Desktop.
How to process UI events with RAF
This file contains hidden or 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 deltaX = 0, | |
queued = false; | |
function myAction(){ | |
//your awesome code here uses deltaX | |
deltaX = 0; //we reset the deltaX so it can be incremented next time onEvent is executed | |
queued = false; | |
} | |
function onEvent(evt){ | |
if(!queued){ | |
queued = true; | |
deltaX = evt.translationX; //in the case of a pointer, if you are using a mouse you will have to do some magic with pageX or similar :) | |
requestAnimationFrame(myAction); | |
}else{ | |
deltaX += evt.translationX; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment