A simple script with a few niceties that allows for multiple requestAnimationFrame
calls, and FPS pinning.
The script polyfills rAF if required, then overloads requestAnimationFrame
and 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 firesrequestAnimationFrame.cancel
: helper tocancelAnimationFrame
- returnstrue
if a handler was due to fire.requestAnimationFrame.fps(fn, fps)
: helper to pin you function to run every N frames per second.fps
is not milliseconds. Note that this is more akin tosetInterval
than rAF, as it will reschedule your function to run every N frames.requestAnimationFrame.running
: boolean flag to pause all rAF calls - set tofalse
and animations will stop, set totrue
and they'll resume.
Wow! Great.