Game Feel & Graphics
Adding "Juice" with Canvas Particles
A game without particles is like a sandwich without sauce—dry and functional, but missing the magic. In this interactive lesson, we'll build a lightweight, high-performance particle system from scratch.
What is "Game Juice"?
In game design, "juice" refers to the visual and audio feedback a game provides when a player performs an action. If a player shoots an enemy and the enemy simply disappears, the interaction feels flat. If the enemy explodes into a shower of sparks, the screen shakes, and a chunky sound effect plays, the interaction feels juicy.
Particles are the cheapest and most effective way to add juice. They can simulate dust kicks when a character jumps, blood splatters, sparks from a sword clash, or magical auras.
Particle Physics 101
At its core, a particle is just an object with a few basic properties:
- Position (x, y): Where it is on the screen.
- Velocity (vx, vy): How fast it is moving along each axis.
- Life (or Alpha): How long it survives before fading out and being removed.
To make particles look realistic, we apply two simple mathematical forces every frame: Gravity (pulls them down) and Friction (slows them down).
// Applying Physics to a Particle
particle.vy += gravity; // Gravity pulls down
particle.vx *= friction; // Friction slows horizontal movement
particle.vy *= friction; // Friction slows vertical movement
particle.x += particle.vx;
particle.y += particle.vy;
Interactive Explosion Sandbox
Below is a fully working particle explosion system. Click anywhere on the black canvas to spawn an explosion!
Try modifying the code on the left. Change the gravity, friction, or the colors in the colors array. Click ▶ Run Code to see your changes instantly.
Memory & Object Pooling (A Word of Warning)
In the sandbox example above, we use particles.push(new Particle()) and particles.splice(i, 1). This is incredibly easy to read and understand, but it has a hidden cost: Garbage Collection (GC).
If you have a game where a machine gun is firing 20 bullets a second, and each bullet creates 10 particles when it hits a wall, you are creating and destroying hundreds of objects every second. Eventually, the browser will pause the game for a few milliseconds to clean up the memory, resulting in a noticeable stutter.
Pro Tip: For production games, pre-allocate an array of 500 particles at the start of the game (Object Pooling). Instead of destroying them, mark them as "inactive". When an explosion happens, find an inactive particle and reset its position and alpha!
Ready for more? Check out our Astrominer game to see these exact particle tricks used for mining space rocks!