在 javascript 中获取
window.requestAnimationFrame
回调之间的时间差的最佳方法是什么?
我已经尝试过:
// create the best .requestAnimationFrame callback for each browser
window.FPS = (function() {
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame ||
function(callback) {window.setTimeout(callback, 1000 / 60);};
})();
// start animation loop
var dt, stamp = (new Date()).getTime();
function loop() {
window.FPS(loop);
var now = (new Date()).getTime();
var dt = now - stamp;
stamp = now;
}
// has "dt" the best accuracy?
大多数现代浏览器会自动将高精度时间戳作为参数发送到每个 requestAnimation 回调循环中:http://caniuse.com/#search=performance
因此,您只需从当前时间戳中减去最后一个时间戳即可获得自上次运行循环以来经过的时间。
这里是示例代码和演示:
var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");
var cw=canvas.width;
var ch=canvas.height;
var startingTime;
var lastTime;
var totalElapsedTime;
var elapsedSinceLastLoop;
var $total=$('#total');
var $loop=$('#loop');
requestAnimationFrame(loop);
function loop(currentTime){
if(!startingTime){startingTime=currentTime;}
if(!lastTime){lastTime=currentTime;}
totalElapsedTime=(currentTime-startingTime);
elapsedSinceLastLoop=(currentTime-lastTime);
lastTime=currentTime;
$total.text('Since start: '+totalElapsedTime+' ms');
$loop.text('Since last loop: '+elapsedSinceLastLoop+' ms');
requestAnimationFrame(loop);
}
body{ background-color: ivory; }
#canvas{border:1px solid red;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<p id=total>T1</p>
<p id=loop>T2</p>
<canvas id="canvas" width=300 height=300></canvas>
对于不支持
Performance
的几个浏览器,您必须在循环内使用 Date.now()
而不是 currentTime
,因为这些浏览器不会自动将时间戳发送到循环中。
“dt”的准确度最高吗?
不。根据文档,
回调方法传递一个参数,即 DOMHighResTimeStamp,它指示按
排队的回调开始触发的当前时间requestAnimationFrame
所以你应该使用它来获得高精度。
function loop(now) {
var last = now || Date.now(); // fallback if no precise time is given
window.FPS(function(now) {
now = now || Date.now();
var dt = now - last;
if (dt != 0) // something might be wrong with our frames
console.log(dt);
loop(now);
});
}
window.FPS(loop);
我会写一个明确的结论,给所有想使用这个模式的人
// CREATING AN FPS ENGINE
window.FPS = (function() {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(callback) {window.setTimeout(callback, 1000 / 60);};
})();
var FPS = {
loop: function(canvas_object) { // OPTIONAL canvas_object, I think it increases performance | canvas_object = document.getElementById("canvas_id")
var ticks = window.FPS(function(now){
var dt = now - FPS.stamp || 0;
FPS.stamp = now;
FPS.update(dt, FPS.stamp, ticks);
FPS.loop(canvas_object);
}, canvas_object);
},
update: undefined,
stamp: undefined
};
// USING THE FPS ENGINE
FPS.loop(the_canvas_object); // starts the engine
FPS.update = function(dt, stamp, ticks) {
// The game/video loop, using accurate dt. Stamp is the time since engine started. Ticks is the number of the loop cycles
console.log("dt: " + dt + ", Stamp: " + stamp + ", Ticks: " + ticks); // check output
// HAPPY GAME CREATING
var fps= (1 / (dt / 1000)).toFixed(1);
};
your_module.prototype.log_frame_speed=function(){
this.cframe++;
if(this.cframe>10000) {sys.exit();}
this.date_now=Date.now();
this.date_delta=this.date_now-this.date_previous;
this.date_previous=this.date_now;
//console.log(this.date_delta);
};
your_module.prototype.implement_next_frame=function(){
// code update.
// render update.
this.log_frame_speed();
requestAnimationFrame(this.implement_next_frame.bind(this));
};