udpated requestAnimationFrame polyfill
This commit is contained in:
parent
d5b6dafc93
commit
1b63c630b2
41
game.js
41
game.js
@ -3,30 +3,29 @@
|
|||||||
* 小型游戏引擎
|
* 小型游戏引擎
|
||||||
*/
|
*/
|
||||||
|
|
||||||
//动画处理
|
// requestAnimationFrame polyfill
|
||||||
|
if (!Date.now)
|
||||||
|
Date.now = function() { return new Date().getTime(); };
|
||||||
(function() {
|
(function() {
|
||||||
var lastTime = 0;
|
'use strict';
|
||||||
var vendors = ['webkit', 'moz'];
|
var vendors = ['webkit', 'moz'];
|
||||||
for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
|
for (var i = 0; i < vendors.length && !window.requestAnimationFrame; ++i) {
|
||||||
window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
|
var vp = vendors[i];
|
||||||
window.cancelAnimationFrame =
|
window.requestAnimationFrame = window[vp+'RequestAnimationFrame'];
|
||||||
window[vendors[x]+'CancelAnimationFrame'] || window[vendors[x]+'CancelRequestAnimationFrame'];
|
window.cancelAnimationFrame = (window[vp+'CancelAnimationFrame']
|
||||||
|
|| window[vp+'CancelRequestAnimationFrame']);
|
||||||
|
}
|
||||||
|
if (/iP(ad|hone|od).*OS 6/.test(window.navigator.userAgent) // iOS6 is buggy
|
||||||
|
|| !window.requestAnimationFrame || !window.cancelAnimationFrame) {
|
||||||
|
var lastTime = 0;
|
||||||
|
window.requestAnimationFrame = function(callback) {
|
||||||
|
var now = Date.now();
|
||||||
|
var nextTime = Math.max(lastTime + 16, now);
|
||||||
|
return setTimeout(function() { callback(lastTime = nextTime); },
|
||||||
|
nextTime - now);
|
||||||
|
};
|
||||||
|
window.cancelAnimationFrame = clearTimeout;
|
||||||
}
|
}
|
||||||
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);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}());
|
}());
|
||||||
|
|
||||||
function Game(id,params){
|
function Game(id,params){
|
||||||
|
Loading…
x
Reference in New Issue
Block a user