Last active
May 12, 2018 03:38
-
-
Save julienetie/09dc63f1e4d590ddb866 to your computer and use it in GitHub Desktop.
Sets the frame rate of requestAnimationFrame in FPS without setTimeout/ interval, performance or Date functions.
This file contains hidden or 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
| // Just add water | |
| const = document.getElementById('water'); // Demo | |
| /** | |
| * setFPS sets the frame rate of the rAF function | |
| * with minimum overhead. | |
| * @Copyright Julien Etienne 2015 | |
| * @License MIT | |
| */ | |
| const setFPS = (callback, rAF, fps) => { | |
| let fPSTimeStamp = 0; | |
| const loopCallback = time => { | |
| if (time > fPSTimeStamp) { | |
| const timeStamp = fPSTimeStamp += 1000 / fps; | |
| callback(timeStamp); | |
| } | |
| rAF(loopCallback); | |
| } | |
| rAF(loopCallback); | |
| } | |
| // Demo | |
| function showFPSTime(timeStamp) { | |
| div.innerHTML = timeStamp | |
| } | |
| // Demo | |
| setFPS(showFPSTime, requestAnimationFrame, 2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment