Skip to content

Instantly share code, notes, and snippets.

@ayamflow
Created November 19, 2015 19:33
Show Gist options
  • Save ayamflow/038026febd0b3db64fae to your computer and use it in GitHub Desktop.
Save ayamflow/038026febd0b3db64fae to your computer and use it in GitHub Desktop.
FPS control with RAF
// http://stackoverflow.com/questions/19764018/controlling-fps-with-requestanimationframe
fpsInterval = 1000 / fps; // 24 for instance
then = Date.now();
update();
function update() {
requestAnimationFrame(update);
now = Date.now();
elapsed = now - then;
if (elapsed > fpsInterval) {
then = now - (elapsed % fpsInterval);
} else {
return;
}
// Drawing code...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment