统计 FPS

本文最后更新于:2023年3月19日 晚上

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
let count = 0;
let prevTimestamp;

function showFPS(fps) {
// 这里设置如何将 fps 数值输出
// 比如你可以将其更新到某个 DOM 元素上
console.log(fps);
}

function loop(timestamp) {
if (prevTimestamp) {
count++;
// 间隔超过 1s,将之前计算的 count 输出
if (timestamp - prevTimestamp >= 1000) {
showFPS(Math.round((count * 1000) / (timestamp - prevTimestamp)));
prevTimestamp = timestamp;
count = 0;
}
} else {
prevTimestamp = timestamp;
}
window.requestAnimationFrame(loop);
}

window.requestAnimationFrame(loop);

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!