A simple script with a few niceties that allows for multiple
requestAnimationFrame calls, and FPS pinning.
How it works
The script polyfills rAF if required, then overloads
cancelAnimationFrame with a process that allows multiple frames to be queued up for rAF to run.
This is useful if there are multiple animations running on the page, you want all the callbacks to happen at once, and not on multiple rAF calls. This script is meant as a drop-in solution to that problem.
By default it will overload the original
requestAnimationFrame methods, but if you remove the two arguments at the end of the script (or change them) it will introduce
window.raf (or what you've named the request & cancel functions).
requestAnimationFrame(fn): can be called multiple times at once, and the queue will only be cleared when the real rAF callback fires
requestAnimationFrame.cancel: helper to
trueif a handler was due to fire.
requestAnimationFrame.fps(fn, fps): helper to pin you function to run every N frames per second.
fpsis not milliseconds. Note that this is more akin to
setIntervalthan rAF, as it will reschedule your function to run every N frames.
requestAnimationFrame.running: boolean flag to pause all rAF calls - set to
falseand animations will stop, set to
trueand they'll resume.