| name | cameras |
| description | Use this skill when working with cameras in Phaser 4. Covers camera effects (shake, fade, flash, pan, zoom), following sprites, scroll, bounds, viewports, multiple cameras, and minimap. Triggers on: camera, viewport, scroll, zoom, follow, shake, fade. |
Cameras
Camera system in Phaser 4 -- CameraManager, main camera, viewport vs scroll, zoom, bounds, following sprites, camera effects (fade, flash, shake, pan, zoomTo, rotateTo), ignore lists, filters, and keyboard controls.
Key source paths: src/cameras/2d/CameraManager.js, src/cameras/2d/BaseCamera.js, src/cameras/2d/Camera.js, src/cameras/2d/effects/, src/cameras/controls/
Related skills: ../game-setup-and-config/SKILL.md, ../sprites-and-images/SKILL.md, ../filters-and-postfx/SKILL.md
Quick Start
const cam = this.cameras.main;
cam.setScroll(200, 100);
cam.centerOn(400, 300);
cam.setZoom(2);
cam.startFollow(player, false, 0.1, 0.1);
cam.setBounds(0, 0, 2048, 2048);
cam.fadeIn(1000);
cam.filters.external.addBlur(1, 2);
Core Concepts
CameraManager
Every Scene has a CameraManager accessible via this.cameras. It manages all cameras for that Scene and is registered as a plugin under the key 'CameraManager'.
this.cameras
this.cameras.cameras
this.cameras.main
this.cameras.default
Key methods on CameraManager:
| Method | Signature | Description |
|---|
add | (x?, y?, width?, height?, makeMain?, name?) | Create a new Camera. Defaults to full game size at 0,0. Returns Camera. |
addExisting | (camera, makeMain?) | Add a pre-built Camera instance. Returns the Camera or null if it already exists. |
remove | (camera, runDestroy?) | Remove and optionally destroy a Camera or array of Cameras. If main is removed, resets to cameras[0]. |
getCamera | (name) | Find a Camera by its name string. Returns Camera or null. |
getTotal | (isVisible?) | Count cameras. Pass true to count only visible ones. |
fromJSON | (config) | Create cameras from a config object or array. Used for scene-level camera config. |
resetAll | () | Destroy all cameras and create one fresh default camera. |
resize | (width, height) | Resize all cameras to given dimensions. |
Camera limit: The manager supports up to 32 cameras that can use ignore() for Game Object exclusion (IDs are bitmasks). Cameras beyond 32 get ID 0 and cannot exclude objects.
Main Camera
The main property is a convenience reference to a Camera, typically cameras[0]. It is set automatically when:
- The scene boots (first camera created becomes main)
- You pass
makeMain: true to add() or addExisting()
- The current main camera is removed (falls back to
cameras[0])
Viewport vs World (Scroll)
A Camera has two independent coordinate concepts:
-
Viewport -- The physical rectangle on the canvas where the Camera renders. Controlled by setPosition(x, y), setSize(w, h), or setViewport(x, y, w, h). By default, fills the entire game canvas.
-
Scroll -- Where the Camera is "looking" in the game world. Controlled by scrollX / scrollY properties or setScroll(x, y). Scrolling does not affect the viewport rectangle.
const miniCam = this.cameras.add(480, 0, 320, 200);
miniCam.setScroll(1000, 500);
miniCam.setZoom(0.25);
worldView is a read-only Rectangle updated each frame that reflects what area of the world the camera can currently see, accounting for scroll, zoom, and bounds. Use it for culling or intersection checks.
const view = cam.worldView;
Common Patterns
Scrolling the Camera
cam.scrollX = 100;
cam.scrollY = 200;
cam.setScroll(100, 200);
cam.centerOn(500, 400);
const point = cam.getScroll(500, 400);
Following a Sprite
cam.startFollow(player);
cam.startFollow(player, false, 0.1, 0.1);
cam.startFollow(player, true, 0.05, 0.05, 0, -50);
cam.setLerp(0.08, 0.08);
cam.setFollowOffset(0, -50);
cam.setDeadzone(200, 150);
cam.stopFollow();
startFollow accepts any object with x and y properties -- it does not have to be a Game Object. Lerp of 1 snaps instantly; 0.1 gives smooth tracking. A lerp of 0 on an axis disables tracking on that axis.
When a deadzone is set, the camera does not scroll while the target remains inside the deadzone rectangle. The deadzone is re-centered on the camera midpoint each frame.
Zoom
cam.setZoom(2);
cam.setZoom(0.5);
cam.setZoom(2, 1);
cam.zoom;
cam.zoomX;
cam.zoomY;
Never set zoom to 0. The minimum is clamped to 0.001.
Bounds
cam.setBounds(0, 0, worldWidth, worldHeight);
cam.setBounds(0, 0, 2048, 2048, true);
cam.useBounds = false;
cam.removeBounds();
const rect = cam.getBounds();
Bounds only restrict scrolling. They do not prevent Game Objects from being placed outside the bounds, and they do not affect the viewport position.
Multiple Cameras
const main = this.cameras.main;
const minimap = this.cameras.add(600, 0, 200, 150).setZoom(0.2).setName('minimap');
minimap.setScroll(0, 0);
minimap.setBackgroundColor('rgba(0,0,0,0.5)');
const hudCam = this.cameras.add(0, 0, 800, 600).setName('hud');
hudCam.setScroll(0, 0);
main.ignore(hudGroup);
hudCam.ignore(worldGroup);
const found = this.cameras.getCamera('minimap');
this.cameras.remove(minimap);
Camera Effects
All effects are on the Camera class (not BaseCamera). Each returns this for chaining. Effects that are already running will not restart unless you pass force: true.
Fade
cam.fadeOut(1000);
cam.fadeOut(1000, 255, 0, 0);
cam.fadeIn(500);
cam.fade(1000, 0, 0, 0, true);
cam.fadeFrom(1000, 0, 0, 0, true);
cam.fadeOut(1000, 0, 0, 0, (cam, progress) => {
});
Fade direction: fadeOut / fade goes transparent-to-color. fadeIn / fadeFrom goes color-to-transparent.
Flash
cam.flash(250);
cam.flash(500, 255, 0, 0);
cam.flash(300, 255, 255, 255, true, (cam, progress) => {});
Shake
cam.shake(100);
cam.shake(500, 0.02);
cam.shake(300, new Phaser.Math.Vector2(0.1, 0.02));
The intensity value is a small float. The default 0.05 means the camera shifts up to 5% of the viewport size.
Pan
cam.pan(400, 300, 2000);
cam.pan(400, 300, 2000, 'Power2');
cam.pan(800, 600, 1500, 'Sine.easeInOut', false, (cam, progress, x, y) => {});
Pan scrolls the camera so its viewport center finishes at the given world coordinate.
ZoomTo
cam.zoomTo(2, 1000);
cam.zoomTo(0.5, 2000, 'Cubic.easeOut');
RotateTo
cam.rotateTo(Phaser.Math.DegToRad(45), false, 1000);
cam.rotateTo(Math.PI, true, 1500, 'Quad.easeInOut');
The angle is in radians. Set shortestPath to true to take the shortest rotation direction.
Reset All Effects
cam.resetFX();
Keyboard Controls
Phaser provides two built-in camera control classes. Both require you to call update(delta) in your scene's update method.
FixedKeyControl
Moves at a fixed speed per frame. No smoothing.
const cursors = this.input.keyboard.createCursorKeys();
this.camControl = new Phaser.Cameras.Controls.FixedKeyControl({
camera: this.cameras.main,
left: cursors.left,
right: cursors.right,
up: cursors.up,
down: cursors.down,
speed: 0.5
});
this.camControl.update(delta);
SmoothedKeyControl
Applies acceleration, drag, and max speed for smooth camera movement.
this.camControl = new Phaser.Cameras.Controls.SmoothedKeyControl({
camera: this.cameras.main,
left: cursors.left,
right: cursors.right,
up: cursors.up,
down: cursors.down,
zoomIn: this.input.keyboard.addKey('Q'),
zoomOut: this.input.keyboard.addKey('E'),
zoomSpeed: 0.02,
acceleration: 0.06,
drag: 0.0005,
maxSpeed: 1.0
});
this.camControl.update(delta);
Both controls support zoomIn and zoomOut keys and a zoomSpeed config value.
Ignore Lists
The ignore method updates a Game Object's cameraFilter bitmask so the camera skips rendering it.
cam.ignore(uiText);
cam.ignore([scoreText, livesIcon, pauseButton]);
cam.ignore(uiGroup);
cam.ignore(uiLayer);
This is the primary mechanism for HUD-style setups: one camera ignores world objects, another ignores HUD objects.
Camera Deadzone
The deadzone defines a rectangular area around the follow target where the camera does not scroll. The camera only moves when the target leaves this rectangle.
cam.setDeadzone(200, 150);
console.log(cam.deadzone);
cam.setDeadzone();
World Point Conversion
Convert screen (pointer) coordinates to world coordinates, accounting for scroll, zoom, and rotation:
const worldPoint = cam.getWorldPoint(pointer.x, pointer.y);
const output = new Phaser.Math.Vector2();
cam.getWorldPoint(pointer.x, pointer.y, output);
Essential when working with scrolled/zoomed cameras -- raw pointer coordinates are screen space, not world space.
Camera Render List
Each frame, renderList is populated with Game Objects visible to this camera. Rebuilt every frame.
const visible = cam.renderList;
Force Composite (Offscreen Framebuffer)
Force the camera to render to an offscreen framebuffer. Required for CaptureFrame and similar features.
cam.setForceComposite(true);
cam.setForceComposite(false);
Filters enable framebuffer rendering automatically. Use setForceComposite(true) when you need it without filters.
Filters on Cameras (v4)
In Phaser 4, Camera has a filters property with two FilterList instances:
cam.filters.internal
cam.filters.external
Add post-processing effects to a camera the same way you add them to Game Objects:
cam.filters.external.addBlur(1, 2);
cam.filters.external.addGlow(0xff0000, 4, 0, false, 0.1, 10);
const fx = cam.filters.external.addColorMatrix();
fx.grayscale();
cam.filters.external.clear();
Filters require WebGL. The camera must render via a framebuffer when filters are active. See setForceComposite(true) to explicitly enable this even without filters.
Events
Camera events are emitted on the Camera instance itself (it extends EventEmitter). Listen with cam.on(event, handler).
| Event constant | Dispatched when |
|---|
Phaser.Cameras.Scene2D.Events.FADE_IN_START | fadeIn / fadeFrom begins |
Phaser.Cameras.Scene2D.Events.FADE_IN_COMPLETE | Fade-in finishes |
Phaser.Cameras.Scene2D.Events.FADE_OUT_START | fadeOut / fade begins |
Phaser.Cameras.Scene2D.Events.FADE_OUT_COMPLETE | Fade-out finishes |
Phaser.Cameras.Scene2D.Events.FLASH_START | Flash begins |
Phaser.Cameras.Scene2D.Events.FLASH_COMPLETE | Flash finishes |
Phaser.Cameras.Scene2D.Events.SHAKE_START | Shake begins |
Phaser.Cameras.Scene2D.Events.SHAKE_COMPLETE | Shake finishes |
Phaser.Cameras.Scene2D.Events.PAN_START | Pan begins |
Phaser.Cameras.Scene2D.Events.PAN_COMPLETE | Pan finishes |
Phaser.Cameras.Scene2D.Events.ZOOM_START | Zoom effect begins |
Phaser.Cameras.Scene2D.Events.ZOOM_COMPLETE | Zoom effect finishes |
Phaser.Cameras.Scene2D.Events.ROTATE_START | RotateTo begins |
Phaser.Cameras.Scene2D.Events.ROTATE_COMPLETE | RotateTo finishes |
Phaser.Cameras.Scene2D.Events.FOLLOW_UPDATE | Camera updates its follow position (each frame while following). Args: (camera, target) |
Phaser.Cameras.Scene2D.Events.PRE_RENDER | Before the camera renders |
Phaser.Cameras.Scene2D.Events.POST_RENDER | After the camera renders |
Phaser.Cameras.Scene2D.Events.DESTROY | Camera is destroyed |
cam.on(Phaser.Cameras.Scene2D.Events.FADE_OUT_COMPLETE, () => {
this.scene.start('GameOver');
});
For detailed configuration options, API reference tables, and source file maps, see the reference guide.