Update index.html

Fix problems arising from the new webkit implementation of window.performace.now (high resolution timer draft).
This commit is contained in:
arcadeJHS 2013-02-11 19:24:24 +01:00
parent 36f27c2da3
commit f595895a32
1 changed files with 34 additions and 22 deletions

View File

@ -318,23 +318,32 @@
return el.querySelector(selector);
};
// crossbrowser implementation for requestAnimationFrame
/*var requestAnimation = (function() {
return window.requestAnimationFrame
|| window.webkitRequestAnimationFrame
|| window.mozRequestAnimationFrame
|| window.oRequestAnimationFrame
|| window.msRequestAnimationFrame
|| function(callback, element) {
window.setTimeout(callback, 1000/60);
};
})();*/
var requestAnimation = (function() {
return function(callback, element) {
window.setTimeout(callback, 1000/60);
};
})();
(function() {
var lastTime = 0;
var vendors = ['ms', 'moz', 'webkit', 'o'];
for(var x = 0; x < vendors.length && !window.requestAnimationFrameFrame; ++x) {
window.requestAnimationFrameFrame = window[vendors[x]+'requestAnimationFrameFrame'];
window.cancelAnimationFrame =
window[vendors[x]+'CancelAnimationFrame'] || window[vendors[x]+'CancelrequestAnimationFrameFrame'];
}
if (!window.requestAnimationFrameFrame)
window.requestAnimationFrameFrame = 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);
};
}());
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// game objects
@ -1314,8 +1323,9 @@
// animation loop
var gameWrapper = $("#game");
var startTime = window.mozAnimationStartTime || +new Date;
requestAnimation(loop, gameWrapper);
var startTime = window.performance.now() || window.mozAnimationStartTime || +new Date;
requestAnimationFrame(loop, gameWrapper);
function loop(timestamp) {
// time since last draw
var drawStart = (timestamp || +new Date);
@ -1331,7 +1341,7 @@
startTime = drawStart;
}
requestAnimation(loop, gameWrapper);
requestAnimationFrame(loop, gameWrapper);
}
}
@ -1453,9 +1463,11 @@
// animation loop
var gameWrapper = $("#game");
var startTime = window.mozAnimationStartTime || +new Date;
requestAnimation(loop, gameWrapper);
debugger;
var startTime = window.performance.now() || window.mozAnimationStartTime || +new Date;
requestAnimationFrame(loop, gameWrapper);
function loop(timestamp) {
// time since last draw
var drawStart = (timestamp || +new Date);
var diff = drawStart - startTime;
@ -1473,7 +1485,7 @@
startTime = drawStart;
}
requestAnimation(loop, gameWrapper);
requestAnimationFrame(loop, gameWrapper);
}
};