Skip to content

Instantly share code, notes, and snippets.

@mfields
Created May 18, 2013 11:50
Show Gist options
  • Save mfields/5604160 to your computer and use it in GitHub Desktop.
Save mfields/5604160 to your computer and use it in GitHub Desktop.
/**
* Simple animation loop with start and stop controls.
*/
var animationFrameId;
/**
* Animation Loop.
*
* @uses animationFrameId
* @uses requestAnimationFrame()
*/
function animate() {
animationFrameId = window.requestAnimationFrame( animate );
render();
}
/**
* Start the animation.
*
* @uses animationFrameId
* @uses animate()
*/
function start() {
if ( ! animationFrameId )
animate();
}
/**
* Stop the animation.
*
* @uses animationFrameId Sets the value of id to undefined.
* @uses cancelAnimationFrame() to stop the animation.
*/
function stop() {
if ( animationFrameId ) {
animationFrameId = window.cancelAnimationFrame( animationFrameId );
animationFrameId = undefined;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment