Retrieving requestAnimationFrame with JavaScript

By  on  

The requestAnimationFrame function has been a major boost to developers creating and managing animations with JavaScript.  Paul Irish has an excellent introduction on requestAnimationFrame -- I highly recommend you read it.  This HTML5Hub post is also very good.  Most browsers now support the animation function but in the case a browser doesn't, you can shim a rough equivalent with setInterval:

var requestAnimationFrame = window.requestAnimationFrame
    || window.webkitRequestAnimationFrame
    || window.mozRequestAnimationFrame
    || window.msRequestAnimationFrame
    || function(callback) { return setTimeout(callback, 1000 / 60); };

requestAnimationFrame was implemented with browser prefixes so we'll check for those if the unprefixed window method isn't there.  If no native implementation exists, a setInterval shim will have to do!

Recent Features

  • By
    From Webcam to Animated GIF: the Secret Behind chat.meatspac.es!

    My team mate Edna Piranha is not only an awesome hacker; she's also a fantastic philosopher! Communication and online interactions is a subject that has kept her mind busy for a long time, and it has also resulted in a bunch of interesting experimental projects...

  • By
    Create Namespaced Classes with MooTools

    MooTools has always gotten a bit of grief for not inherently using and standardizing namespaced-based JavaScript classes like the Dojo Toolkit does.  Many developers create their classes as globals which is generally frowned up.  I mostly disagree with that stance, but each to their own.  In any event...

Incredible Demos

  • By
    Six Degrees of Kevin Bacon Using MooTools 1.2

    As you can probably tell, I try to mix some fun in with my MooTools madness but I also try to make my examples as practical as possible. Well...this may not be one of those times. I love movies and useless movie trivia so naturally I'm...

  • By
    MooTools Wall Plugin

    One of the more impressive MooTools plugins to hit the Forge recently was The Wall by Marco Dell'Anna.  The Wall creates an endless grid of elements which can be grabbed and dragged, fading in elements as they are encountered.  Let me show...

Discussion

  1. MaxArt

    According to caniuse, Microsoft’s browsers never had a vendor prefixed version of requestAnimationFrame, so we can just keep moz and webkit.

    That’s a very common way to normalize the function, but in most recent implementations requestAnimationFrame passes an argument to the callback function, which is the amount of milliseconds since performance.timing.navigationStart, with micro precision too. This can be very handy for the callback.

    It’s not really possible to perfectly emulate this, but you can get something close if you take note of the epoch time as soon as the script is executed. So this is how I used to polyfill requestAnimationFrame:

    (function(start) {
        window.requestAnimationFrame = function(callback) {
            return setInterval(function() {
                callback(new Date().getTime() - start);
            }, 1000 / 60);
        };
    })(new Date().getTime());
    

    (Well, not exactly… since most of the times requestAnimationFrame is called again in the callback function, but the function itself takes some milliseconds at least to be executed – because it probably involves some kind of repaint – and you should adjust the time interval accordingly, or you may never hope to even get close to 60 fps.)

    Also, don’t forget to normalize cancelAnimationFrame, which has a nasty variant in some (and maybe forgotten?) WebKit browsers: webkitCancelRequestAnimationFrame.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!