我剛剛開始HTML 5 javascript gamedevelopment,並且我學會了使用 requestAnimationFrame填充更好。深入解釋requestAnimationFrame填充
我明白這個功能,但我不明白它背後的數學。
準確地說:
(function() {
var lastTime = 0;
var vendors = ['ms', 'moz', 'webkit', 'o'];
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);
};
}());
與我不懂數學的路線是:
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
將是巨大的,如果有人能夠給我一個簡短的說明那怎麼行的代碼有效。
梅爾文Tehubijuluw
偉大,使一切清楚! –