Options
All
  • Public
  • Public/Protected
  • All
Menu

Class BaseCamera

Cameras

BaseCamera is the base class for all Excalibur cameras. Cameras are used to move around your game and set focus. They are used to determine what is "off screen" and can be used to scale the game.

Excalibur comes with a LockedCamera and a SideCamera, depending on your game needs.

Cameras are attached to Scenes and can be changed by setting Scene.camera. By default, a Scene is initialized with a BaseCamera that doesn't move and is centered on the screen.

Focus

Cameras have a position (x, y) which means they center around a specific point. This can also be an Actor (BaseCamera.setActorToFollow) which the camera will follow as the actor moves, which can be useful for cutscene scenarios (using invisible actors).

If a camera is following an Actor, it will ensure the Actor is always at the center of the screen. You can use x and y instead if you wish to offset the focal point.

Camera Shake

To add some fun effects to your game, the shake method will do a random shake. This is great for explosions, damage, and other in-game effects.

Camera Lerp

"Lerp" is short for Linear Interpolation and it enables the camera focus to move smoothly between two points using timing functions. Use move to ease to a specific point using a provided EasingFunction.

Camera Zooming

To adjust the zoom for your game, use zoom which will scale the game accordingly. You can pass a duration to transition between zoom levels.

Known Issues

Actors following a path will wobble when camera is moving
Issue #276

Hierarchy

Index

Properties

Private _cameraMoving

_cameraMoving: boolean = false

Private _currentLerpTime

_currentLerpTime: number = 0

Private _currentZoomScale

_currentZoomScale: number = 1

Private _easing

_easing: EasingFunction = EasingFunctions.EaseInOutCubic

Private _elapsedShakeTime

_elapsedShakeTime: number = 0

Private _elapsedZoomTime

_elapsedZoomTime: number = 0

Protected _follow

_follow: Actor

Protected _isShaking

_isShaking: boolean = false

Protected _isZooming

_isZooming: boolean = false

Private _lerpDuration

_lerpDuration: number = 1000

Private _lerpEnd

_lerpEnd: Vector = null

Private _lerpPromise

_lerpPromise: IPromise<Vector>

Private _lerpStart

_lerpStart: Vector = null

Private _maxZoomScale

_maxZoomScale: number = 1

Private _shakeDuration

_shakeDuration: number = 0

Private _shakeMagnitudeX

_shakeMagnitudeX: number = 0

Private _shakeMagnitudeY

_shakeMagnitudeY: number = 0

Private _totalLerpTime

_totalLerpTime: number = 0

Private _x

_x: number = 0

Private _xShake

_xShake: number = 0

Private _y

_y: number = 0

Private _yShake

_yShake: number = 0

Private _zoomDuration

_zoomDuration: number = 0

Private _zoomIncrement

_zoomIncrement: number = 0.01

ax

ax: number = 0

ay

ay: number = 0

az

az: number = 0

dx

dx: number = 0

dy

dy: number = 0

dz

dz: number = 0

rotation

rotation: number = 0

rx

rx: number = 0

z

z: number = 1

Accessors

x

  • get x(): number
  • set x(value: number): void
  • Get the camera's x position

    Returns number

  • Set the camera's x position (cannot be set when following an Actor or when moving)

    Parameters

    • value: number

    Returns void

y

  • get y(): number
  • set y(value: number): void
  • Get the camera's y position

    Returns number

  • Set the camera's y position (cannot be set when following an Actor or when moving)

    Parameters

    • value: number

    Returns void

Methods

Private _isDoneShaking

  • _isDoneShaking(): boolean
  • Returns boolean

Private _isDoneZooming

  • _isDoneZooming(): boolean
  • Returns boolean

Private _setCurrentZoomScale

  • _setCurrentZoomScale(zoomScale: number): void
  • Parameters

    • zoomScale: number

    Returns void

debugDraw

  • debugDraw(ctx: CanvasRenderingContext2D): void
  • Parameters

    • ctx: CanvasRenderingContext2D

    Returns void

draw

  • draw(ctx: CanvasRenderingContext2D, delta: number): void
  • Applies the relevant transformations to the game canvas to "move" or apply effects to the Camera

    Parameters

    • ctx: CanvasRenderingContext2D

      Canvas context to apply transformations

    • delta: number

      The number of milliseconds since the last update

    Returns void

getFocus

  • Returns the focal point of the camera, a new point giving the x and y position of the camera

    Returns Vector

getZoom

  • getZoom(): number
  • Gets the current zoom scale

    Returns number

move

  • This moves the camera focal point to the specified position using specified easing function. Cannot move when following an Actor.

    Parameters

    • pos: Vector

      The target position to move to

    • duration: number

      The duration in milliseconds the move should last

    • Default value easingFn: EasingFunction = EasingFunctions.EaseInOutCubic

    Returns IPromise<Vector>

    A Promise that resolves when movement is finished, including if it's interrupted. The Promise value is the Vector of the target position. It will be rejected if a move cannot be made.

setActorToFollow

  • setActorToFollow(actor: Actor): void
  • Sets the Actor to follow with the camera

    Parameters

    • actor: Actor

      The actor to follow

    Returns void

shake

  • shake(magnitudeX: number, magnitudeY: number, duration: number): void
  • Sets the camera to shake at the specified magnitudes for the specified duration

    Parameters

    • magnitudeX: number

      The x magnitude of the shake

    • magnitudeY: number

      The y magnitude of the shake

    • duration: number

      The duration of the shake in milliseconds

    Returns void

update

  • update(engine: Engine, delta: number): void
  • Parameters

    • engine: Engine
    • delta: number

    Returns void

zoom

  • zoom(scale: number, duration?: number): void
  • Zooms the camera in or out by the specified scale over the specified duration. If no duration is specified, it take effect immediately.

    Parameters

    • scale: number

      The scale of the zoom

    • Default value duration: number = 0

      The duration of the zoom in milliseconds

    Returns void