Last active
February 11, 2024 22:25
-
-
Save frostney/5427786 to your computer and use it in GitHub Desktop.
RequestAnimationFrame and CancelAnimationFrame as AMD modules
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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