twojs-browserify
Version:
A browserified two-dimensional drawing api meant for modern browsers.
42 lines (34 loc) • 1.19 kB
JavaScript
/**
* http://paulirish.com/2011/requestanimationframe-for-smart-animating/
* And modified to work with node.js
*/
(function() {
var root = this;
var lastTime = 0;
var vendors = ['ms', 'moz', 'webkit', 'o'];
if (typeof exports !== 'undefined') {
if (typeof module !== 'undefined' && module.exports) {
exports = module.exports = raf;
}
exports.requestAnimationFrame = raf;
return;
}
for(var x = 0; x < vendors.length && !root.requestAnimationFrame; ++x) {
root.requestAnimationFrame = root[vendors[x]+'RequestAnimationFrame'];
root.cancelAnimationFrame =
root[vendors[x]+'CancelAnimationFrame'] || root[vendors[x]+'CancelRequestAnimationFrame'];
}
if (!root.requestAnimationFrame)
root.requestAnimationFrame = raf;
if (!root.cancelAnimationFrame)
root.cancelAnimationFrame = function(id) {
clearTimeout(id);
};
function raf(callback, element) {
var currTime = new Date().getTime();
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
var id = root.setTimeout(function() { callback(currTime + timeToCall); }, timeToCall);
lastTime = currTime + timeToCall;
return id;
}
}());