calculate frame time independent of fps

This commit is contained in:
Luca Conte 2025-04-28 00:21:23 +02:00
parent c4599824c6
commit 6f50150a6b
1 changed files with 9 additions and 1 deletions

View File

@ -40,6 +40,8 @@ let smoothCameraDistance = 4;
let mouseDragging = false;
let frameTimes = [];
function resizeCanvas() {
const dpr = window.devicePixelRatio;
cv.width = cv.parentElement.clientWidth * dpr;
@ -439,8 +441,11 @@ function updateStats(deltaTime) {
fpsDisplay.innerText = Math.round(frameCount / timeSinceLastUpdate)
let msDisplay = document.getElementById("ms");
msDisplay.innerText = deltaTime * 1000 + "ms";
if (frameTimes.length > 0) {
msDisplay.innerText = (Math.round(frameTimes.reduce((prev, curr) => prev + curr) / frameTimes.length * 100) / 100) + "ms";
}
frameTimes = [];
frameCount = 0;
}
@ -449,6 +454,7 @@ let hue = 0;
async function draw() {
let now = Date.now();
let deltaTime = (now - lastFrame) / 1000;
let frameStart = performance.now();
hue += deltaTime / 5;
if (hue > 1) hue = 0;
@ -640,6 +646,8 @@ async function draw() {
setAttribPointers();
gl.drawArrays(gl.TRIANGLES, 0, floorVertices.length / 6);
frameTimes.push(performance.now() - frameStart);
lastFrame = now;
requestAnimationFrame(draw);
}