Created
November 19, 2015 19:33
-
-
Save ayamflow/038026febd0b3db64fae to your computer and use it in GitHub Desktop.
FPS control with RAF
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
// 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