Skip to content

Commit 00d800b

Browse files
committed
Improve fps adjustment
1 parent 31aac32 commit 00d800b

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

example/player/js/main.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
let x;
2-
let a = 0;
2+
let count = 0;
3+
let second = null;
4+
let fps = 0;
35

46
function setup() {
57
x = 330;
@@ -11,9 +13,16 @@ function loop() {
1113
ajiContext.font = "20pt sans-serif";
1214
ajiContext.fillStyle = "#fff";
1315
ajiDrawText("Hello, world!", x, 120);
14-
ajiDrawText(a, 10, 30);
16+
ajiDrawText(count, 10, 30);
17+
ajiDrawText("FPS: " + fps, 10, 60);
1518
x -= 3;
16-
a++;
19+
count++;
20+
const date = new Date();
21+
if (second != date.getSeconds()) {
22+
fps = count;
23+
count = 0;
24+
second = date.getSeconds();
25+
}
1726
if (x < -300) {
1827
x = 330;
1928
}

player.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ Licensed under the MIT License
66

77
const AJIPACK_WIDTH = 320;
88
const AJIPACK_HEIGHT = 240;
9-
const AJIPACK_FPS = 34.0;
9+
const AJIPACK_FPS = 33.0;
10+
let ajiDrawTime = Date.now();
1011
let ajiCanvas, ajiContext;
1112

1213
window.onload = function () {
@@ -31,12 +32,14 @@ function ajiInit() {
3132
}
3233

3334
function ajiMain() {
34-
setTimeout(function () {
35-
requestAnimationFrame(ajiMain);
35+
const ajiNowTime = Date.now();
36+
if (ajiNowTime - ajiDrawTime >= 1000 / AJIPACK_FPS) {
37+
ajiDrawTime = ajiNowTime;
3638
if (typeof loop == "function") {
3739
loop();
3840
}
39-
}, 1000 / AJIPACK_FPS);
41+
}
42+
requestAnimationFrame(ajiMain);
4043
}
4144

4245
function ajiDrawText(text, x, y) {

0 commit comments

Comments
 (0)