javascript - 用javascript计算html5画布游戏的fps
问题描述
我正在创建一个 html5 画布游戏,并想检查一秒钟内通过了多少帧。我尝试过使用 new 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
解决方案
在循环函数中检查执行之间经过的时间。
let lastTime = new Date();
function loop() {
const currentTime = new Date();
// currentTime - lastTime is the number of milliseconds passed from last loop
const fps = 1000 / (currentTime - lastTime);
lastTime = currentTime;
}
推荐阅读
- python - 如何使用 Python 和 ElementTree 从 XML 文件的所有元素中提取所有内容?
- javascript - 在模板列表中创建段落
- xml - 如何在 XML 视图中应用全局模型?
- android - 在 iframe webview Android 中使用 adjustResize 没有行为
- javascript - 在我的开发者控制台中与 FullCalendar 交互
- sql - 如何在 SQL 表中返回具有列条件的前行值?
- python - 使用 tkinter 绘制一个表格,其中单元格需要在单击时着色并通过代码检索选项
- python - 芹菜版本兼容性问题
- python - 如何使用用户名密码连接 coreNLP 服务器?
- polymer-3.x - 为什么没有 @apply 导入的样式 mixin