aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2014-06-20 11:18:34 -0700
committerAlon Zakai <alonzakai@gmail.com>2014-06-20 11:18:34 -0700
commitc6e416fcd10d2b9a42c3d77988b822682961b94f (patch)
tree43e0e6aafd92aca220f9a55d9b004821f13105b4
parentcd7a868630ab58fa0edf96d982d80b96f5e58b1e (diff)
fake requestAnimationFrame properly
-rw-r--r--src/library_browser.js20
1 files changed, 18 insertions, 2 deletions
diff --git a/src/library_browser.js b/src/library_browser.js
index b3e0a048..fff9387e 100644
--- a/src/library_browser.js
+++ b/src/library_browser.js
@@ -466,9 +466,25 @@ mergeInto(LibraryManager.library, {
canvasContainer.requestFullScreen();
},
+ nextRAF: 0,
+
+ fakeRequestAnimationFrame: function(func) {
+ // try to keep 60fps between calls to here
+ var now = Date.now();
+ if (Browser.nextRAF === 0) {
+ Browser.nextRAF = now + 1000/60;
+ } else {
+ while (now + 2 >= Browser.nextRAF) { // fudge a little, to avoid timer jitter causing us to do lots of delay:0
+ Browser.nextRAF += 1000/60;
+ }
+ }
+ var delay = Math.max(Browser.nextRAF - now, 0);
+ setTimeout(func, delay);
+ },
+
requestAnimationFrame: function requestAnimationFrame(func) {
if (typeof window === 'undefined') { // Provide fallback to setTimeout if window is undefined (e.g. in Node.js)
- setTimeout(func, 1000/60);
+ Browser.fakeRequestAnimationFrame(func);
} else {
if (!window.requestAnimationFrame) {
window.requestAnimationFrame = window['requestAnimationFrame'] ||
@@ -476,7 +492,7 @@ mergeInto(LibraryManager.library, {
window['webkitRequestAnimationFrame'] ||
window['msRequestAnimationFrame'] ||
window['oRequestAnimationFrame'] ||
- function(func) { setTimeout(func, 1000/60) };
+ Browser.fakeRequestAnimationFrame;
}
window.requestAnimationFrame(func);
}