1Scenery that keeps its scars
Until now, the landscape was a nice fixed background. Now, every ball that lands tears off a piece of terrain. The holes stay: by the end of the round, the battlefield is all pockmarked.
2The magic: the canvas “eraser”
The canvas knows how to draw… but also how to erase in a shape! We switch to destination-out mode: everything we “draw” then becomes an eraser. Draw a circle → a perfect round hole in the terrain.
ctx.globalCompositeOperation = 'destination-out'; // eraser mode ctx.arc(x, y, radius, 0, 6.28); // a circle… ctx.fill(); // …that erases!
Dig the terrain!
3Solid or not? The game's rule
How does the game know whether a ball has hit the ground? No need to store a huge image in memory. We keep: the ground height at each spot, and the list of craters. A point is solid if:
This little rule is enough to create caves and overhangs: a crater dug under a bump leaves the matter above it “floating”.
dx² + dy² ≤ radius²).4The details that make it feel real
- 🟫 Scorched rim. We darken the edge of the hole — but only where there is terrain (
source-atopmode), otherwise we'd see a circle floating in the sky. - 🌫️ Dust behind the terrain. The impact kicks up dust drawn behind the terrain: the relief hides it, as if it came out of the hole.
- 🌱 Scenery carried off. Grass and pebbles are “baked” into the terrain image: the eraser removes them along with the earth.
★Well done, you've been all the way round!
You now roughly know how a web game comes to life:
a loop that redraws 60×/s, a bit of maths for the physics and the scenery, a networked referee to play together, and drawing tricks to smash it all up. And above all: we move forward step by step, each one tested before the next.