使用requestAnimationFrame在Canvas中计算FPS [英] calculate FPS in Canvas using requestAnimationFrame

查看:1121
本文介绍了使用requestAnimationFrame在Canvas中计算FPS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何计算画布游戏应用程式的FPS?我看到一些例子,但没有一个使用requestAnimationFrame,而不知道如何应用他们的解决方案。这是我的代码:

How could I calculate the FPS of a canvas game application? I've seen some examples, but none of them use requestAnimationFrame, and im not sure how to apply their solutions there. This is my code:

http://jsfiddle.net/WCKhH/

顺便问一下,有没有任何图书馆可以添加到surpervise性能?

By the way, is there any library I could add to surpervise performance?

推荐答案

您可以跟踪最后一次调用requestAnimFrame的时间。

You could keep track of the last time requestAnimFrame was called.

var lastCalledTime;
var fps;

function requestAnimFrame() {

  if(!lastCalledTime) {
     lastCalledTime = Date.now();
     fps = 0;
     return;
  }
  delta = (Date.now() - lastCalledTime)/1000;
  lastCalledTime = Date.now();
  fps = 1/delta;
} 

http://jsfiddle.net/vZP3u/

这篇关于使用requestAnimationFrame在Canvas中计算FPS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆