From 4a0b56372d3ab3db7b25c1bf191c0effd06181a4 Mon Sep 17 00:00:00 2001 From: elioat Date: Sun, 29 Dec 2024 17:29:12 -0500 Subject: * --- html/isometric-bounce/js/game.js | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) (limited to 'html/isometric-bounce/js/game.js') diff --git a/html/isometric-bounce/js/game.js b/html/isometric-bounce/js/game.js index fb6530d..e2b0021 100644 --- a/html/isometric-bounce/js/game.js +++ b/html/isometric-bounce/js/game.js @@ -8,6 +8,7 @@ function createGame() { offsetX: 0, offsetY: 0, particles: [], + lastFrameTime: 0, player: { x: 0, y: 0, @@ -251,14 +252,22 @@ function createGame() { state.ctx.stroke(); } - function gameLoop() { - state.ctx.clearRect(0, 0, state.canvas.width, state.canvas.height); + function gameLoop(timestamp) { + // Set target frame duration for 60 FPS (approximately 16.67ms) + const frameInterval = 1000 / 60; - drawGrid(); - updateParticles(); - drawParticles(); - updatePlayer(); - drawPlayer(); + // If we haven't stored the last frame time, or enough time has passed + if (!state.lastFrameTime || timestamp - state.lastFrameTime >= frameInterval) { + state.ctx.clearRect(0, 0, state.canvas.width, state.canvas.height); + + drawGrid(); + updateParticles(); + drawParticles(); + updatePlayer(); + drawPlayer(); + + state.lastFrameTime = timestamp; + } requestAnimationFrame(gameLoop); } @@ -291,6 +300,7 @@ function createGame() { resizeCanvas(); window.addEventListener('resize', resizeCanvas); state.canvas.addEventListener('click', handleClick); + state.lastFrameTime = 0; gameLoop(); } -- cgit 1.4.1-2-gfad0