在请求动画填充内的回调参数
Parameter in callback inside request animation polyfill
我阅读了以下请求动画填充。然而,我不明白为什么作者把currTime + timeToCall
参数放在setInterval
中的callback
中:
var id = window.setTimeout(function() {
callback(currTime + timeToCall);
},
下面是完整的多边形代码片段:
// Set up requestAnimationFrame and cancelAnimationFrame for use in the game code
(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);
};
}());
填料中的此参数用于覆盖requestAnimationFrame
规格。如果你打算在你的动画中使用时间控制,你的回调应该提供一个时间戳参数。polyfill中的(currTime + timeToCall)
为动画提供了比简单使用setTimeout
更准确的时间戳。
var now, timeBetweenFrames, lastTime;
function animation (timestamp) {
now = timestamp; // the value of timestamp is changing
timeSinceLastFrame = now - lastTime; // time between frames in miliseconds
lastTime = now;
// ... performe your animations
// you can use the timeSinceLastFrame value, for example,
// to interpolate the position of your objects,
// making the animation reacts equally, independent of performance scenarios
// here is an example:
// ...
// if you have an object named player, with moves depending on his velocity
// update the player position
player.x += player.velocity.x * (timeSinceLastFrame/1000);
player.y += player.velocity.y * (timeSinceLastFrame/1000);
// note that timeSinceLastFrame/1000 is the time in seconds instead of miliseconds
// draw the player on the screen
player.draw();
// ...
requestAnimationFrame(animation);
}
function start() {
lastTime = Date.now() // or performance.now() like described below this code snippet
requestAnimationFrame(animation); // let this party begin
}
start();
请注意,如果浏览器具有本机requestAnimationFrame
支持,则发送给回调的timestamp
参数的时间格式与performance.now()
相同,而不是Date.now()
。这对于某些新版本的浏览器(如Chrome)是有效的。有关HTML5 Rocks的详细解释,请参阅这篇文章:http://updates.html5rocks.com/2012/05/requestAnimationFrame-API-now-with-sub-millisecond-precision
相关文章:
- 要求未定义JS回调参数
- Axios spread()具有未知数量的回调参数
- 节点回调参数无法断言instanceof Error
- each()-将额外的参数传递给回调参数
- AngularJS tokenWrapper-错误的回调参数
- 拉斐尔未运行回调参数
- 使用 jQuery 回调参数通过 AJAX 调用 PHP 函数
- 将函数的执行同步为 node.js 中的回调参数
- 当第一个函数完成没有回调参数时执行第二个函数
- 使用回调参数从函数中获取值
- 如何使用可选的回调参数制作 javascript 函数
- 为什么设置超时's回调参数可以接受参数
- 对 Array.prototype.some 使用额外的回调参数
- 使用Google闭包编译器记录回调参数
- JavaScript Object Literal“;这个“;关键字jQuery回调参数传递
- 在Meteor中运行的回调中未定义回调参数
- 回调参数名称
- 将jQueryajax回调参数定义为现有对象类型
- Reactjs路由器匹配回调参数总是未定义的
- Javascript自定义事件更改回调参数