使用javascript计算html5画布游戏的fps

问题描述 投票:0回答:1

我正在创建一个html5画布游戏,想检查在一秒钟内通过多少帧。我尝试使用新的Date(),但无法正常工作。我将如何做到这一点?javascript模板/示例:

var fps;
function loop(){
    //some code

    window.requestAnimationFrame(loop);
}
function checkFrameRate(){
    fps = //how long it takes for loop to run
    window.requestAnimationFrame(checkFrameRate);
}
loop();
checkFrameRate
javascript runtime frame-rate requestanimationframe
1个回答
0
投票

在循环功能中,检查两次执行之间经过的时间。

let lastTime = new Date();
function loop() { 
    let currentTime = new Date();
    // currentTime - lastTime is the number of milliseconds passed from last loop 
    var fps = 1000 / (currentTime - lastTime);
    lastTime = currentTime;
}
© www.soinside.com 2019 - 2024. All rights reserved.