计算FPS Javascript(我做错了吗?)

时间:2017-07-24 20:12:18

标签: javascript frame-rate

这是我的calculateFPS函数:

function calculateFPS() {
  const now = performance.now();
  if(fps.lastCalledTime !== null) {
    fps.counter++;

    const secondsPerFrame = (now - fps.lastCalledTime) / 1000;
    fps.totalFPS += 1 / secondsPerFrame;
    if(fps.counter === 20) {
      fps.fps = Math.round(fps.totalFPS / fps.counter);
      fps.totalFPS = 0;
      fps.counter = 0;
    }
  }
  fps.lastCalledTime = now;
}

这是我的fps全局对象:

let fps = {
  lastCalledTime: null,
  fps: 60,
  counter: 0,
  totalFPS: 0,
}

然而,当我看到我的游戏速度减慢时(游戏是Mowing(如果我正确翻译))基本上FPS应该下降...而是它上升到400 - 500 < / p>

我做错了吗?

提前谢谢你......

1 个答案:

答案 0 :(得分:0)

您可以计算两个函数调用之间的时差

let then = performance.now();
let now;
let frame = 1000/60;

function animate() {

    now = performance.now();
    console.log(((now - then)/frame) * 60);
    then = now;

}

setInterval(animate, frame);
相关问题