Skip to content

Camera2D ​

The 2D camera.

ts
import { Camera2D, CameraFollow } from "willow";

Camera2D ​

ts
class Camera2D

The engine's single live 2D view into the scene: position, zoom, rotation and viewport. Reached via engine.camera. Points and rectangles are the core Vec2 and Rect structs, and animation curves are core Ease ids.

Camera2D: properties ​

MemberDescription
position: Vec2World-space centre of the view. x and y are the same state.
x: floatWorld-space X the camera centers on. Same state as position.x.
y: floatWorld-space Y the camera centers on. Same state as position.y.
zoom: floatScale factor: 1.0 is no zoom, greater than 1 zooms in, less than 1 zooms out.
rotation: floatCamera rotation in radians, clockwise.
cullEnabled: booleanWhen true, a node whose world bounds miss the camera's visible area is skipped for rendering.
readonly viewport: RectThe screen-space rectangle this camera renders into, in pixels.
bounds: Rect | nullThe world-space rectangle the camera keeps its view inside, or null for none. Setting it clamps the camera at once and on every frame after. Raises a RangeError when width or height is not positive.

Camera2D: methods ​

MemberDescription
follow(node: Node, options?: CameraFollow): voidTracks node every frame, easing toward it. See CameraFollow. Interrupts any in-flight scrollTo/scrollToTile, rejecting its promise.
unfollow(): voidStops following the current target node. The camera holds its last position.
scrollTo(target: Vec2, seconds: float, ease?: int): Promise<void>Animates the camera centre to target over seconds (0 = instant), returning a promise that resolves when the motion completes (or at once, for an instant move). ease is a core Ease constant (default Ease.LINEAR); an unknown id rejects the promise with a RangeError. Interrupts any in-flight follow/scroll, rejecting the superseded promise. Raises a RangeError if seconds is negative.
scrollToTile(col: int, row: int, seconds: float, ease?: int, tileSize?: Size): Promise<void>Same as scrollTo, targeting the centre of tile (col, row). tileSize defaults to 32x32 (willow's tilemap default).
shake(trauma: float, decay?: float): voidApplies a one-shot trauma-based shake: trauma (0..1) adds to the camera's accumulated trauma, and shake magnitude scales with trauma squared, so repeated hits accumulate naturally. decay, when given, sets the per-second trauma decay rate (default 1.0) for this and future shakes.
resetShake(): voidImmediately cancels any active shake/trauma and resets shake state.
screenToWorld(p: Vec2): Vec2Converts a screen-space point to world space, accounting for the camera's position, zoom and rotation.
worldToScreen(p: Vec2): Vec2Converts a world-space point to screen space, the inverse of screenToWorld.
visibleBounds(): RectThe world-space rectangle the camera can currently see.

CameraFollow ​

ts
class CameraFollow

Options for Camera2D.follow. offset shifts the tracked point in world space (default Vec2{}); lerp is the per-frame interpolation factor, where an omitted lerp snaps to the target and a lower value follows smoothly. Pass an object literal: { offset: Vec2{ x: 0.0, y: -40.0 }, lerp: 0.1 }.

CameraFollow: properties ​

MemberDescription
offset?: Vec2World-space shift of the tracked point (default Vec2{}).
lerp?: floatPer-frame interpolation factor; omit to snap to the target.