A more robust browser polyfill for requestAnimationFrame()
The requestAnimationFrame()
method of a browser's window
object is used for setting up an update to an animation, asking the browser to call the passed-in callback function to draw the next frame when it is time to repaint the screen. It is useful for effective JavaScript-based animation. See reference documentation here (MDN) and here (W3C) for more info.
Why any polyfill? Because the function didn't always exist, and old browsers therefore don't support it. Is it supported by a browser my users need to use?
Because most (all?) of the other ones have a hidden flaw: they can't handle multiple, simultaneous animations. See a fuller explanation on this blog post.
requestAnimationFrame.js (full version)
requestAnimationFrame.min.js (minified version)
npm install @trajanmcgill/requestanimationframe
- Prerequisites:
- Setup:
- First, clone from GitHub:
git clone https://github.com/trajanmcgill/requestAnimationFrame.js.git
- Next, get the linting and build tools and run them. Move inside the newly created project directory and run:
The
npm install
npm install
will automatically run the build process. Source files can be found in thesrc
directory; distributable files will be deposited indist
.
- First, clone from GitHub:
- Rebuilding: In the project directory:
This will kick off a Grunt script that will:
npm run prepare
Simply include the polyfill code in your HTML, either as a separate script file or by pasting it into one of your own, and then just call window.requestAnimationFrame()
exactly as you would on the native browser version. (Note: This shim code will leave the browser-native version alone if one is present; it only adds the function if it is not already defined.)
- Trajan McGill
This project is licensed under the Unlicense, which means it is more or less public domain and free to use in any form anywhere. See the LICENSE file for details.
Thanks to Paul Irish, Erik Möller, Joel Fillmore, and Tino Zijdel for their earlier polyfill suggestions which inspired this version.