Skip to content

Instantly share code, notes, and snippets.

@Convicted202
Last active August 29, 2015 14:10
Show Gist options
  • Save Convicted202/97df7103db6c6384a0c6 to your computer and use it in GitHub Desktop.
Save Convicted202/97df7103db6c6384a0c6 to your computer and use it in GitHub Desktop.
Common usage of animation with callback called constantly
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