Camera2D
The 2D camera.
ts
import { Camera2D, CameraFollow } from "willow";Camera2D
ts
class Camera2DThe 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
| Member | Description |
|---|---|
position: Vec2 | World-space centre of the view. x and y are the same state. |
x: float | World-space X the camera centers on. Same state as position.x. |
y: float | World-space Y the camera centers on. Same state as position.y. |
zoom: float | Scale factor: 1.0 is no zoom, greater than 1 zooms in, less than 1 zooms out. |
rotation: float | Camera rotation in radians, clockwise. |
cullEnabled: boolean | When true, a node whose world bounds miss the camera's visible area is skipped for rendering. |
readonly viewport: Rect | The screen-space rectangle this camera renders into, in pixels. |
bounds: Rect | null | The 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
| Member | Description |
|---|---|
follow(node: Node, options?: CameraFollow): void | Tracks node every frame, easing toward it. See CameraFollow. Interrupts any in-flight scrollTo/scrollToTile, rejecting its promise. |
unfollow(): void | Stops 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): void | Applies 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(): void | Immediately cancels any active shake/trauma and resets shake state. |
screenToWorld(p: Vec2): Vec2 | Converts a screen-space point to world space, accounting for the camera's position, zoom and rotation. |
worldToScreen(p: Vec2): Vec2 | Converts a world-space point to screen space, the inverse of screenToWorld. |
visibleBounds(): Rect | The world-space rectangle the camera can currently see. |
CameraFollow
ts
class CameraFollowOptions 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
| Member | Description |
|---|---|
offset?: Vec2 | World-space shift of the tracked point (default Vec2{}). |
lerp?: float | Per-frame interpolation factor; omit to snap to the target. |