Last active
August 29, 2015 14:10
-
-
Save Convicted202/97df7103db6c6384a0c6 to your computer and use it in GitHub Desktop.
Common usage of animation with callback called constantly
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
function animLoop( render, element ) { | |
var running, lastFrame = +new Date; | |
function loop( now ) { | |
// stop the loop if render returned false | |
if ( running !== false ) { | |
requestAnimationFrame( loop, element ); | |
var deltaT = now - lastFrame; | |
// do not render frame when deltaT is too high | |
if ( deltaT < 160 ) { | |
running = render( deltaT ); | |
} | |
lastFrame = now; | |
} | |
} | |
loop( lastFrame ); | |
} | |
// Usage | |
animLoop(function( deltaT ) { | |
elem.style.left = ( left += 10 * deltaT / 16 ) + "px"; | |
if ( left > 400 ) { | |
return false; | |
} | |
// optional 2nd arg: elem containing the animation | |
}, animWrapper ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment