Skip to content

Instantly share code, notes, and snippets.

@frostney
Last active February 11, 2024 22:25
Show Gist options
  • Save frostney/5427786 to your computer and use it in GitHub Desktop.
Save frostney/5427786 to your computer and use it in GitHub Desktop.
RequestAnimationFrame and CancelAnimationFrame as AMD modules
define('vendors', function() {
return ['ms', 'moz', 'webkit', 'o'];
});
define('requestAnimationFrame', ['root', 'vendors'], function(root, vendors) {
// frameRate is only used if requestAnimationFrame is not available
var frameRate = 60;
var requestAnimationFrame = root.requestAnimationFrame;
for (var x = 0; x < vendors.length && !root.requestAnimationFrame; ++x) {
requestAnimationFrame = root[vendors[x] + 'RequestAnimationFrame'];
if (requestAnimationFrame) {
break;
}
}
if (!requestAnimationFrame) {
requestAnimationFrame = function(callback) {
root.setTimeout(callback, ~~(1000 / root.frameRate));
};
}
return requestAnimationFrame;
});
define('cancelAnimationFrame', ['root', 'vendors'], function(root, vendors) {
var cancelAnimationFrame = root.cancelAnimationFrame;
for (var x = 0; x < vendors.length && !root.cancelAnimationFrame; ++x) {
cancelAnimationFrame = root[vendors[x] + 'CancelRequestAnimationFrame'];
if (cancelAnimationFrame) {
break;
}
}
if (!cancelAnimationFrame) {
cancelAnimationFrame = function(id) {
root.clearTimeout(id);
};
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment