Build smooth 60 FPS games with requestAnimationFrame, delta time, and professional timing techniques
Section 1 of 9
// Basic game loop structure
function gameLoop() {
// 1. Process Input
handleInput();
// 2. Update Game State
updatePlayer();
updateEnemies();
checkCollisions();
updatePhysics();
// 3. Render
clearScreen();
drawBackground();
drawPlayer();
drawEnemies();
drawUI();
// Continue the loop
requestAnimationFrame(gameLoop);
}
// Start the game
requestAnimationFrame(gameLoop);
// Simple example with a moving box
let x = 0;
function simpleLoop() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Update
x += 2;
if (x > canvas.width) x = 0;
// Draw
ctx.fillStyle = 'blue';
ctx.fillRect(x, 100, 50, 50);
requestAnimationFrame(simpleLoop);
}Build a smooth platformer demo showcasing game loop mastery:
Master AABB, circle, and advanced collision algorithms