Back to Phaser

Cameras — Reference

skills/cameras/references/REFERENCE.md

4.1.07.1 KB
Original Source

Cameras — Reference

Detailed configuration, API tables, and source file maps for the cameras skill.

API Quick Reference

BaseCamera Properties (inherited by Camera)

PropertyTypeDefaultDescription
scrollX / scrollYnumber0World position the camera is looking at
zoomX / zoomYnumber1Zoom level per axis
zoomnumber1Shortcut: sets/gets both zoomX and zoomY
rotationnumber0Rotation in radians
originX / originYnumber0.5Rotation origin within the viewport (0..1)
x / ynumber0Viewport position on the canvas
width / heightnumbergame sizeViewport dimensions
worldViewRectangle--Read-only rect of visible world area
midPointVector2--Read-only center of camera in world coords
alphanumber1Opacity of everything rendered by this camera
visiblebooleantrueWhether the camera renders at all
backgroundColorColortransparentBackground fill color
transparentbooleantrueFalse when backgroundColor has alpha > 0
roundPixelsbooleanfalseRound positions to integers during render
namestring''Optional name for lookup
idnumberbitmaskAssigned by manager for ignore-list filtering
useBoundsbooleanfalseWhether scroll bounds are active
inputEnabledbooleantrueWhether objects through this camera receive input
disableCullbooleanfalseSkip culling before input hit tests
maskGeometryMasknullCanvas-only mask. In WebGL, use filters instead.
forceCompositebooleanfalseForce framebuffer rendering (WebGL only, v4)

BaseCamera Methods

MethodSignature
setScroll(x, y?)
setZoom(x?, y?)
setRotation(radians?)
setAngle(degrees?)
setPosition(x, y?) -- viewport position
setSize(width, height?) -- viewport size
setViewport(x, y, width, height?) -- position + size
setOrigin(x?, y?) -- rotation origin
setBounds(x, y, width, height, centerOn?)
removeBounds()
getBounds(out?) -- returns Rectangle copy
centerOn(x, y)
centerOnX(x)
centerOnY(y)
getScroll(x, y, out?) -- returns Vector2
getWorldPoint(x, y, output?) -- screen to world coords
ignore(entries) -- GameObject, array, Group, or Layer
setBackgroundColor(color?)
setAlpha(value?)
setVisible(value)
setName(value?)
setRoundPixels(value)
setMask(mask, fixedPosition?) -- Canvas only
clearMask(destroyMask?)
setForceComposite(value) -- WebGL only, v4

Camera Methods (extends BaseCamera)

MethodSignature
startFollow(target, roundPixels?, lerpX?, lerpY?, offsetX?, offsetY?)
stopFollow()
setLerp(x?, y?)
setFollowOffset(x?, y?)
setDeadzone(width?, height?) -- omit args to clear
fadeIn(duration?, r?, g?, b?, callback?, context?)
fadeOut(duration?, r?, g?, b?, callback?, context?)
fade(duration?, r?, g?, b?, force?, callback?, context?)
fadeFrom(duration?, r?, g?, b?, force?, callback?, context?)
flash(duration?, r?, g?, b?, force?, callback?, context?)
shake(duration?, intensity?, force?, callback?, context?)
pan(x, y, duration?, ease?, force?, callback?, context?)
zoomTo(zoom, duration?, ease?, force?, callback?, context?)
rotateTo(angle, shortestPath?, duration?, ease?, force?, callback?, context?)
resetFX()

Effect Handler Properties

Each effect is accessible as a property on Camera: fadeEffect, flashEffect, shakeEffect, panEffect, zoomEffect, rotateToEffect. All have isRunning (boolean) and progress (0..1) properties you can check.

Gotchas

  1. this.cameras is the CameraManager, not an array. The array of cameras is this.cameras.cameras. The main camera is this.cameras.main.

  2. Viewport vs Scroll confusion. setPosition / setViewport move the camera's rendering rectangle on screen. setScroll / scrollX / scrollY move what the camera is looking at in the world. These are independent.

  3. Zoom of 0 will break rendering. setZoom(0) is clamped to 0.001 internally, but avoid passing 0.

  4. Effects do not restart by default. Calling cam.fade() while a fade is running does nothing unless you pass force: true. The same applies to flash, shake, pan, zoomTo, and rotateTo.

  5. startFollow snaps immediately on first call. The camera jumps to the target position, then lerps from there. To avoid a visible snap, set scroll to the target position before calling startFollow.

  6. Ignore list limit of 32 cameras. Only the first 32 cameras created get unique bitmask IDs for ignore(). Camera 33+ gets ID 0 and cannot use Game Object exclusion.

  7. roundPixels and non-integer zoom. Setting roundPixels: true only works correctly when zoom is an integer. Non-integer zoom with roundPixels causes jitter.

  8. Camera rotation does not rotate the viewport rectangle. The viewport is always axis-aligned. Rotation is applied during rendering only.

  9. Masks vs Filters in v4. setMask only works with Canvas renderer (GeometryMask). For WebGL, use cam.filters.external (FilterList) instead.

  10. Keyboard controls require manual update call. Both FixedKeyControl and SmoothedKeyControl must have their update(delta) called in your Scene's update method -- they do not auto-update.

  11. pan() overrides follow. While a pan effect is running, the follow logic is paused. It resumes after the pan completes.

  12. Camera setViewport caveats. The viewport is limited to an axis-aligned rectangle, cannot be rotated, and filters/masks may render incorrectly with non-default viewports. For mini-cam effects, consider using RenderTexture or DynamicTexture instead.

Source File Map

FileDescription
src/cameras/2d/CameraManager.jsScene plugin managing all cameras. Access via this.cameras.
src/cameras/2d/BaseCamera.jsBase class with scroll, zoom, bounds, viewport, ignore, mask, background.
src/cameras/2d/Camera.jsExtends BaseCamera. Adds effects (fade, flash, shake, pan, zoomTo, rotateTo), follow, deadzone, filters.
src/cameras/2d/effects/Fade.jsFade effect implementation.
src/cameras/2d/effects/Flash.jsFlash effect implementation.
src/cameras/2d/effects/Shake.jsShake effect implementation.
src/cameras/2d/effects/Pan.jsPan effect implementation.
src/cameras/2d/effects/Zoom.jsZoom effect implementation.
src/cameras/2d/effects/RotateTo.jsRotateTo effect implementation.
src/cameras/2d/events/Event constant modules (FADE_IN_START, FLASH_COMPLETE, etc.).
src/cameras/controls/FixedKeyControl.jsFixed-speed keyboard camera control.
src/cameras/controls/SmoothedKeyControl.jsSmoothed (acceleration/drag) keyboard camera control.