From d2315af2ec3332d319c8ba7839707d5d7ad1691b Mon Sep 17 00:00:00 2001 From: Gabriele Cirulli Date: Tue, 11 Mar 2014 14:48:42 +0100 Subject: [PATCH] add requestanimationframe polyfill --- index.html | 3 ++- js/animframe_polyfill.js | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 js/animframe_polyfill.js diff --git a/index.html b/index.html index 117d718..edabb6c 100644 --- a/index.html +++ b/index.html @@ -67,7 +67,8 @@ Created by Gabriele Cirulli. Based on 1024 by Veewo Studio and conceptually similar to Threes by Asher Vollmer.

- + + diff --git a/js/animframe_polyfill.js b/js/animframe_polyfill.js new file mode 100644 index 0000000..c45a13e --- /dev/null +++ b/js/animframe_polyfill.js @@ -0,0 +1,26 @@ +(function() { + var lastTime = 0; + var vendors = ['webkit', 'moz']; + for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) { + window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame']; + window.cancelAnimationFrame = + window[vendors[x]+'CancelAnimationFrame'] || window[vendors[x]+'CancelRequestAnimationFrame']; + } + + if (!window.requestAnimationFrame) { + window.requestAnimationFrame = function(callback, element) { + var currTime = new Date().getTime(); + var timeToCall = Math.max(0, 16 - (currTime - lastTime)); + var id = window.setTimeout(function() { callback(currTime + timeToCall); }, + timeToCall); + lastTime = currTime + timeToCall; + return id; + }; + } + + if (!window.cancelAnimationFrame) { + window.cancelAnimationFrame = function(id) { + clearTimeout(id); + }; + } +}()); -- GitLab