From 038a287e965e068305d398e05849602b9d5b483b Mon Sep 17 00:00:00 2001 From: Evert Date: Mon, 21 Aug 2017 17:21:09 +0300 Subject: [PATCH] \(hopefully\) improved fps counter --- index.js | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 3e35618..e3b580e 100644 --- a/index.js +++ b/index.js @@ -690,17 +690,20 @@ window.onload = function () { } let lastTime = Date.now() + let now + let fpsRes = 50 + function gameLoop () { - requestAnimationFrame(gameLoop) - - let delta = (Date.now() - lastTime) / 1000 - - fps = 1 / (delta === 0 ? 1 : delta) - - update(delta) + update() render() - lastTime = Date.now() + let cfps = 1000 / ((now = new Date) - lastTime) + if (now != lastTime) { + fps += (cfps - fps) / fpsRes + lastTime = now + } + + requestAnimationFrame(gameLoop) } function initialize () {