Rafman.js helps you manage requestAnimationFrame work.
Just include rafman.js
in your page. There are no dependencies.
Rafman.js registers a Rafman
object in the global namespace.
Start requesting frames using requestAnimationFrame until all the work is done.
Stop requesting frames.
Register a callback to be executed only on the next frame. This is particularly useful to throttle events.
Rafman.once(function() {
// do something
});
An id can be specified which can later be used to cancel the callback.
Rafman.once('someId', function() {
// do something
});
Rafman.cancel('someId');
In case of an event handler you could do something like the following. Note that it's possible to pass the arguments you want the callback to be called with.
var scrollCallback = function(event, anotherArg) {
// do something
}
window.onscroll = function(event) {
Rafman.once(scrollCallback, event, anotherArg);
}
Register a callback to be called on every frame. Useful if you are constantly animating like in a game.
Cancel the execution of a callback. It's possible to pass the callback itself, or the id it was registered with. This function cancels callbacks registered with both once
and always
.
Clear all registered callbacks.
In short, the latest versions of all browsers support requestAnimation frame. IE9 and lower don't support it.
See requestAnimationFrame support on caniuse.com
You might want to include a polyfill for requestAnimationFrame.
- Add tests because
test.html
is a mess. requestAnimationFrame is kinda hard to test. Any tips?
Licensed under the MIT license.
Copyright (c) 2013 Roald Hacquebord