Retrieving requestAnimationFrame with JavaScript
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!
![CSS Gradients]()
With CSS border-radius, I showed you how CSS can bridge the gap between design and development by adding rounded corners to elements. CSS gradients are another step in that direction. Now that CSS gradients are supported in Internet Explorer 8+, Firefox, Safari, and Chrome...
![9 Mind-Blowing Canvas Demos]()
The <canvas> element has been a revelation for the visual experts among our ranks. Canvas provides the means for incredible and efficient animations with the added bonus of no Flash; these developers can flash their awesome JavaScript skills instead. Here are nine unbelievable canvas demos that...
![Multiple Background CSS Animations]()
CSS background animation has been a hot topic for a long time, mostly because they look pretty sweet and don't require additional elements. I was recently asked if it was possible to have multiple background animations on a given element and the answer is yes...with...
![9 Mind-Blowing WebGL Demos]()
As much as developers now loathe Flash, we're still playing a bit of catch up to natively duplicate the animation capabilities that Adobe's old technology provided us. Of course we have canvas, an awesome technology, one which I highlighted 9 mind-blowing demos. Another technology available...
According to caniuse, Microsoft’s browsers never had a vendor prefixed version of
requestAnimationFrame, so we can just keepmozandwebkit.That’s a very common way to normalize the function, but in most recent implementations
requestAnimationFramepasses an argument to the callback function, which is the amount of milliseconds sinceperformance.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
requestAnimationFrameis 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.