Ever wondered how browser games move objects smoothly across the screen, like a character jumping or a ball bouncing?
The secret is requestAnimationFrame
, a built-in browser method designed for smooth, efficient animations.
In this article, we’ll explore what requestAnimationFrame
does, why it’s awesome for games, and how to use it in a simple game loop.
What Is requestAnimationFrame
?
requestAnimationFrame
is a JavaScript method that tells the browser:
“Run this function before the next repaint.”
This makes it perfect for smooth animations because:
- It's synced with the screen’s refresh rate (usually 60 FPS).
- It pauses automatically when the tab isn't visible (saving power).
- It’s more efficient than using
setInterval()
orsetTimeout()
for game loops.
🎮 Why Use It in Game Development?
In games, we often update the position of things (players, enemies, obstacles) every frame.
Instead of using setInterval(() => update(), 16)
, which can be jittery or misaligned with screen refresh, use requestAnimationFrame(update)
— it makes animations smoother and less CPU-intensive.
Simple Game Example: A Bouncing Ball
Let’s create a small demo to see requestAnimationFrame
in action — a ball bouncing around the screen!
HTML
<canvas id="gameCanvas" width="500" height="300"></canvas>
CSS
canvas {
border: 2px solid #444;
display: block;
margin: 20px auto;
background-color: #f0f0f0;
}
JavaScript
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
let x = 50;
let y = 50;
let dx = 2;
let dy = 3;
const radius = 20;
function drawBall() {
ctx.beginPath();
ctx.arc(x, y, radius, 0, Math.PI * 2);
ctx.fillStyle = '#3498db';
ctx.fill();
ctx.closePath();
}
function update() {
// Clear the canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawBall();
// Update position
x += dx;
y += dy;
// Bounce off walls
if (x + radius > canvas.width || x - radius < 0) dx = -dx;
if (y + radius > canvas.height || y - radius < 0) dy = -dy;
// Call this function again on the next frame
requestAnimationFrame(update);
}
// Start the game loop
update();
What's Happening?
- We clear the canvas each frame.
- We draw the ball at its new position.
- We check for wall collisions and reverse direction if needed.
-
requestAnimationFrame(update)
schedules the next frame.
This loop runs as fast as the browser allows — typically 60 frames per second — resulting in smooth, fluid motion.
Pro Tips for Game Dev with requestAnimationFrame
- Always clear the canvas each frame to prevent "ghosting."
- Use
delta time
(time difference between frames) for consistent movement across devices. - Combine with keyboard or mouse input to make interactive games!
What’s Next?
Now that you’ve seen requestAnimationFrame
in action, try adding more features:
- Paddle & ball collision (like Pong 🏓)
- Gravity for jumping characters
- Enemies, scores, or game over logic
✅ Final Thoughts
requestAnimationFrame
is the go-to tool for making smooth animations in the browser. Whether you're building a bouncing ball, a side-scroller, or a full 2D game — it’s essential for any modern web-based game engine.
Want to Learn More?
Let me know if you want a follow-up article on:
- Handling input for movement
- Creating a basic platformer
- Building a tiny game engine from scratch
Ready to make your browser games buttery smooth? Game on! 🕹️
Top comments (0)