Skip to content

Commit ad97d5e

Browse files
committed
add custom fps info
1 parent 55eddad commit ad97d5e

File tree

1 file changed

+41
-10
lines changed

1 file changed

+41
-10
lines changed

src/main.ts

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -227,14 +227,10 @@ async function init() {
227227

228228
// === Update ===
229229
const clock = new THREE.Clock();
230-
const stats = new Stats();
231-
stats.dom.style.position = 'fixed';
232-
stats.dom.style.top = 'calc(env(safe-area-inset-top,0px) + 10px)';
233-
stats.dom.style.left = 'calc(env(safe-area-inset-left,0px) + 10px)';
234-
stats.dom.style.zIndex = '9999';
235-
stats.dom.style.transformOrigin = 'top left';
236-
stats.dom.style.transform = isMobile() ? 'scale(1.6)' : 'scale(1)';
237-
document.body.appendChild(stats.dom);
230+
const fpsDiv = ensureFpsDiv();
231+
let currentFps: number = 0;
232+
let frames: number = 0;
233+
let prevTime: number = 0;
238234

239235
function Update() {
240236
const delta = clock.getDelta();
@@ -259,17 +255,52 @@ async function init() {
259255
else {
260256
composer?.render();
261257
}
262-
stats.update();
258+
frames++;
259+
const time = performance.now();
260+
261+
if (time >= prevTime + 1000) {
262+
263+
currentFps = Math.round((frames * 1000) / (time - prevTime));
264+
265+
frames = 0;
266+
prevTime = time;
267+
268+
}
269+
fpsDiv.textContent = `${currentFps.toFixed(0)} fps`;
270+
263271
requestAnimationFrame(Update);
264272
}
273+
265274
Update();
266275

267276
window.addEventListener("resize", () => {
268-
stats.dom.style.transform = isMobile() ? 'scale(1.6)' : 'scale(1)';
269277
camera.aspect = window.innerWidth / window.innerHeight;
270278
camera.updateProjectionMatrix();
271279
renderer.setSize(window.innerWidth, window.innerHeight);
272280
});
281+
282+
function ensureFpsDiv() {
283+
let el = document.getElementById("fps") as HTMLDivElement | null;
284+
if (!el) {
285+
el = document.createElement("div");
286+
el.id = "fps";
287+
Object.assign(el.style, {
288+
position: "fixed",
289+
top: "8px",
290+
left: "8px",
291+
padding: "4px 8px",
292+
fontFamily: "monospace",
293+
fontSize: isMobile() ? "3rem" : "14px",
294+
background: "rgba(0,0,0,0.5)",
295+
color: "#fff",
296+
borderRadius: "6px",
297+
zIndex: "9999",
298+
userSelect: "none",
299+
});
300+
document.body.appendChild(el);
301+
}
302+
return el;
303+
}
273304
}
274305

275306
init().catch(console.error);

0 commit comments

Comments
 (0)