Reset this lesson?

Your edits to this lesson are deleted and its starting files load again.

Angry-Birds-3 makes the world bigger than the screen. It’s WORLD_WIDTH, 3000 pixels wide, and the canvas is only 1280. A camera follows Red through the air, zooms out to show his flight, and pans back to the sling for the next shot. Now that the camera can keep up, LAUNCH_SPEED in src/objects/Slingshot.js goes from 9 to 19.

The Camera

Camera in src/services/Camera.js is a window onto the world. x is the world position of the view’s left edge, and zoom is how much of the world it shows. A zoom of 1 is full size, and 0.6 shows more of the world, smaller.

It eases toward one of two goals:

  • follow(body) keeps a body in the middle of the view.
  • panTo(x) heads for a fixed left edge.

Either way, limit() keeps the view inside the world. It clamps the left edge, so it’s never below 0 and never so far right that the view would end past WORLD_WIDTH. A camera following a bird off either end stops at the edge.

src/services/Camera.js
90
limit(x) {
91
return Math.max(0, Math.min(WORLD_WIDTH - CANVAS_WIDTH / this.zoom, x));
92
}

Easing

Each update, the camera closes a share of the gap to its goal:

src/services/Camera.js
115
this.x +=
116
(this.limit(goal) - this.x) * (1 - Math.exp(-CAMERA_DAMPING * dt));

It’s the same damping as Mario’s camera in Mario-3.

Level updates the camera once per physics step, inside stepPhysics(), so the view only moves when the bodies do.

Zooming

While it follows a bird, getZoomGoal() in Camera.js zooms the camera out to show the flight, then back in as the bird gets near arrivalX. For now arrivalX is the far edge of the world, so the camera stays zoomed out for most of a flight. Once there are blocks, the level will set it to where they start.

The demo shows two shots from above, one leaving the world and one landing in it:

Drawing Through the Camera

applyTransform() shifts the canvas into world space, through applyView(). Everything drawn between it and resetTransform() is drawn at its world position, and the canvas transform works out where that is on screen:

src/services/Camera.js
216
context.save();
217
context.translate(
218
-left,
219
CANVAS_HEIGHT * (1 - this.zoom) + this.shakeOffset.y,
220
);
221
context.scale(this.zoom, this.zoom);

Moving left by left, the camera’s x times its zoom, brings that part of the world onto the canvas. Moving down keeps the ground on the bottom edge as the view zooms out.

Screen Shake

shakeOffset in that code is the screen shake, a quick judder of the whole view for a big explosion. Camera.shake() starts one, and updateShake() shrinks it to nothing over SHAKE_DURATION. Effects in src/objects/Effects.js takes the camera too, so its own shake() can call camera.shake(). Nothing shakes the screen yet, but Bomb and Matilda’s egg, which you can add in the assignment, do.

Parallax

The sky and trees are drawn in screen space, outside the camera’s transform. Background.render() in src/objects/Background.js takes the camera’s x and zoom, and moves each layer by its own share of how far the ground moves:

src/objects/Background.js
15
const layers = [
16
{ frame: sceneryFrames.sky, scale: 2.9, scroll: 0.3, isZoomed: false },
17
{ frame: sceneryFrames.trees, scale: 1.5, scroll: 0.5, isZoomed: true },
18
];
  • scale is the size. Each layer is a sprite frame, an image cut from backgrounds.png, drawn at scale times its size and tiled across the screen.
  • scroll is the speed. It’s how far the layer moves for every pixel the ground moves. When the camera moves 1000 pixels, the ground moves 1000, the trees move 1000 × 0.5 = 500, and the sky moves 1000 × 0.3 = 300.
  • isZoomed is whether the layer shrinks as the camera zooms out. The trees do. The sky doesn’t, since a smaller sky would leave a gap above it.

This is the line in render() that moves each layer:

src/objects/Background.js
65
// The ground moves cameraX * zoom pixels on screen, and the layer its share of that.
66
const offset = Math.round(cameraX * zoom * scroll) % width;

Far things seem to move less as you pass them, like hills from a train window, so layers slower than the ground look further away. This is parallax, and it gives a flat background some depth.

In the demo, one pin is in the ground and one in the picked layer. As the camera pans, the layer’s pin moves its scroll share of the ground’s. Change the layer’s numbers to see what each does:

The Mouse

Matter’s mouse measures positions in screen space, on the canvas, but the bird is in the world. alignMouse() in src/services/Camera.js gives Matter’s mouse the camera’s offset and scale every frame, so it reports world positions, and dragging the bird still works.

The Trail

Trail in src/objects/Trail.js drops a dot every 26 pixels along each flight and keeps them until the next release, so the player can aim relative to the last shot.

Follow that bird

Fire Red and he flies straight off the screen while the view stays put. The camera works, but nothing tells it what to do. updateBird() in src/objects/Level.js still only loads the next bird.

Rewrite updateBird() so it handles the whole shot:

  1. If there’s no flyingBird, return. There’s nothing to watch.
  2. Otherwise, call this.camera.follow() with the flying bird’s body, so the view keeps him in sight.
  3. If the bird isFinished(), do what the method already does, setting this.slingshot.flyingBird to null and calling this.loadBird(). Before loading, also call this.camera.panTo(0), so the view goes back to the sling for the next shot.

After step 1, drop the ?. from flyingBird?.isFinished(). Past the early return, flyingBird will always be there.

The Challenge tab gets a ✓ once you’ve seen the camera follow a bird across the world.

Keep const { flyingBird } = this.slingshot; at the top, and put the early return straight after it.

Call follow() before the isFinished() check, so it runs on every frame the bird flies.

The whole of updateBird():

updateBird() {
const { flyingBird } = this.slingshot;
if (!flyingBird) {
return;
}
this.camera.follow(flyingBird.body);
if (flyingBird.isFinished()) {
this.slingshot.flyingBird = null;
this.camera.panTo(0);
this.loadBird();
}
}

📚 References

Powered by WebContainers
Files
Preparing Environment