Skip to content

Instantly share code, notes, and snippets.

@julienetie
Last active May 12, 2018 03:38
Show Gist options
  • Select an option

  • Save julienetie/09dc63f1e4d590ddb866 to your computer and use it in GitHub Desktop.

Select an option

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.
// 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