Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 36 ยท Tilemaps and Cameras

Camera Effects and Multiple Cameras

Add fade, flash, shake, pan and zoom camera effects in Phaser, then use extra cameras for a minimap, a fixed UI layer and split-screen views.

A camera does more than scroll. It can fade the screen to black between levels, flash white when the player is hit, shake when something explodes and glide to a new spot to reveal a door. These effects are built in, take a single line each, and are one of the cheapest ways to make a game feel polished.

The second half of this lesson uses more than one camera. A scene can own as many cameras as you like, each drawing a rectangle of the canvas. That gives you a minimap, a split-screen mode for two players, or a separate camera for the user interface so that shaking the world does not shake the score.

Built-in camera effects

Every camera has these methods. All durations are in milliseconds, and colors are given as red, green and blue values from 0 to 255:

Method Effect
cam.fadeOut(duration, r, g, b) Fades the view to a color (black by default)
cam.fadeIn(duration, r, g, b) Fades from a color back to the scene
cam.flash(duration, r, g, b) A bright flash that fades away (white by default)
cam.shake(duration, intensity) Random jitter; intensity 0.01 is a small shake
cam.pan(x, y, duration, ease) Moves the center to a world point
cam.zoomTo(zoom, duration, ease) Animates the zoom
cam.rotateTo(radians, shortestPath, duration, ease) Rotates the view (cam.setAngle sets it instantly)

An effect that is still running is not restarted by default. Pass true as the force argument (for fade it is the fifth parameter, for flash the fifth, for shake the third) when you want a new call to replace the running one.

The effects send events when they finish, which is how you chain a scene change after a fade:

JavaScript
cam.fadeOut(500, 0, 0, 0);
cam.once(Phaser.Cameras.Scene2D.Events.FADE_OUT_COMPLETE, () => {
  this.scene.start('Level2');
});

Always wait for the event instead of using a timer. A timer can start the next scene too early or too late if the effect was interrupted.

Trying the effects

The buttons below live on their own camera, so you can see the point of the second half of this lesson already: when the world shakes, the buttons stay still.

Camera effects with a separate UI camera

Press Shake: the circles jitter while the buttons stay still. Press Pan several times and the main camera glides across the world; the UI camera never moves, because nothing tells it to.

Several cameras in one scene

this.cameras.add(x, y, width, height) creates a camera whose viewport is that rectangle of the canvas. Each camera renders every game object by default, so a second camera shows the same world and the same UI as the first. To control what each camera sees, you use ignore:

JavaScript
uiCamera.ignore(worldObjects);      // the UI camera will not draw these
mainCamera.ignore(uiObjects);       // the main camera will not draw these

ignore accepts a single game object, an array, a group or a layer. Layers (created with this.add.layer()) are handy for this: put everything that belongs together into one layer and ignore the layer. Ignoring is per camera, and it is a visibility rule, not a removal; the objects still update and collide normally.

flowchart TB
  subgraph Scene
    WL["worldLayer<br/>(sprites, map)"]
    UL["uiLayer<br/>(score, buttons)"]
  end
  MC["Main camera"] -- sees --> WL
  MC -. ignores .-> UL
  UC["UI camera"] -- sees --> UL
  UC -. ignores .-> WL

Each camera draws only the layers it does not ignore.

Cameras are drawn in the order they were added, so the UI camera (added second) paints on top. Give a camera a transparent background (the default) when it is an overlay. Set cam.setBackgroundColor on cameras that should paint their own rectangle, such as a minimap.

A minimap

A minimap is a small second camera that shows the whole world at a tiny zoom. You place its viewport in a corner, set its zoom so that the world fits, and center it. Because it renders the world objects directly, it needs no extra sprites.

Minimap with a second camera

The minimap camera zooms out to 140 / 1600, which is 0.0875, so the 1600 pixel wide world fits into its 140 pixel viewport. Because the minimap draws the world objects themselves, the player circle is visible there too; to make it easier to spot, many games draw a larger marker that only the minimap sees, by ignoring it on the main camera.

Zoom is applied around the center of a camera's viewport. When you set a zoom, call centerOn (or follow something) afterward. Otherwise the camera keeps its old scroll values and shows the wrong part of the world.

Split screen

For two players on one screen, create two cameras that each take half of the canvas and follow a different player:

JavaScript
const left = this.cameras.main.setViewport(0, 0, 400, 600);
left.startFollow(player1);
const right = this.cameras.add(400, 0, 400, 600);
right.startFollow(player2);

Both cameras draw the same world, so everything works without duplicating objects. Add setBounds to each, and put the shared HUD on a third overlay camera if you want it to span both halves.

Practical advice

  • Keep effect durations short (200 to 500 ms). Long effects make the game feel slow.
  • Use a flash or shake only for events that matter, such as taking damage. If everything shakes, nothing does.
  • Fade out before scene.start, and fade in at the start of the next scene's create. This hides loading hitches.
  • Remember that shake can move the camera off the bounds for a moment; that is normal.
  • Many cameras multiply the drawing work, because each one renders the scene again. Two or three are fine; a camera per enemy is not.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How do you run code exactly when a fade out has finished?

    Cameras emit events when effects complete. Waiting for the event is reliable even if the effect was interrupted or the frame rate drops.
  2. 2. A scene has a main camera and a second camera. Without calling ignore, what does the second camera draw?

    Every camera renders the whole display list unless you tell it to ignore objects or layers.
  3. 3. Why use a separate UI camera instead of setScrollFactor(0) on every HUD element?

    Scroll factor 0 only stops scrolling. The shake and zoom of the main camera still apply to those objects, while a UI camera is not affected.
  4. 4. A world is 1600 pixels wide and the minimap camera viewport is 160 pixels wide. What zoom shows the whole width?

    Zoom is the ratio of viewport size to the world size you want to show: 160 / 1600 = 0.1.

Summary

  • Camera effects (fadeOut, fadeIn, flash, shake, pan, zoomTo) are one-line calls and emit completion events.
  • A scene can have many cameras; each draws a viewport rectangle and renders everything unless told to ignore objects or layers.
  • Layers plus ignore give clean separation between a world camera and a UI camera.
  • A minimap is a small extra camera with a tiny zoom, centered on the world.
  • Split-screen uses one camera per player, each following its own target.

Next: Audio Basics