1681-engine / index / Camera
Class: Camera ​
Defined in: src/core/Camera.ts:31
Moves and zooms the world layer. scrollX/scrollY is the world point at the top-left of the screen, so round(scroll * zoom) / zoom snaps the view to whole screen pixels.
Constructors ​
Constructor ​
new Camera(world, screen): Camera;Defined in: src/core/Camera.ts:60
Parameters ​
| Parameter | Type |
|---|---|
world | Container |
screen | Container |
Returns ​
Camera
Properties ​
defaultZoom ​
defaultZoom: number = 1;Defined in: src/core/Camera.ts:34
The zoom reset goes back to: the engine sets it from its zoom option (default 1).
height ​
height: number = 0;Defined in: src/core/Camera.ts:44
pixelGrid ​
pixelGrid: boolean = false;Defined in: src/core/Camera.ts:40
The world is drawn on its own pixel grid (Stage.pixelPerfect): snapRoot lands on whole world pixels. Set by the stage.
resolution ​
resolution: number = 1;Defined in: src/core/Camera.ts:36
Backing pixels per screen pixel (the renderer's resolution, zoomResolution(zoom, devicePixelRatio)): what snap rounds to.
roundPixels ​
roundPixels: boolean = false;Defined in: src/core/Camera.ts:38
Lands the view on whole backing pixels (pixel art); off, the world moves smoothly between them.
scrollX ​
scrollX: number = 0;Defined in: src/core/Camera.ts:41
scrollY ​
scrollY: number = 0;Defined in: src/core/Camera.ts:42
width ​
width: number = 0;Defined in: src/core/Camera.ts:43
zoom ​
zoom: number = 1;Defined in: src/core/Camera.ts:32
Accessors ​
boundsRect ​
Get Signature ​
get boundsRect():
| {
height: number;
width: number;
x: number;
y: number;
}
| null;Defined in: src/core/Camera.ts:128
The bounds the view is kept inside, if any.
Returns ​
| { height: number; width: number; x: number; y: number; } | null
tracking ​
Get Signature ​
get tracking():
| {
x: number;
y: number;
}
| null;Defined in: src/core/Camera.ts:123
The tracked target, if any.
Returns ​
| { x: number; y: number; } | null
worldView ​
Get Signature ​
get worldView(): ViewRect;Defined in: src/core/Camera.ts:65
Returns ​
Methods ​
apply() ​
apply(now): void;Defined in: src/core/Camera.ts:223
Writes the camera into the world layer; called once per frame after the scene updated.
Parameters ​
| Parameter | Type |
|---|---|
now | number |
Returns ​
void
centerOn() ​
centerOn(x, y): this;Defined in: src/core/Camera.ts:132
Parameters ​
| Parameter | Type |
|---|---|
x | number |
y | number |
Returns ​
this
clearBounds() ​
clearBounds(): this;Defined in: src/core/Camera.ts:97
Lets the view go anywhere again.
Returns ​
this
flash() ​
flash(
ms,
r,
g,
b,
_force?,
peak?
): void;Defined in: src/core/Camera.ts:173
Full-screen colour flash starting at peak alpha and fading out over ms.
Parameters ​
| Parameter | Type | Default value |
|---|---|---|
ms | number | undefined |
r | number | undefined |
g | number | undefined |
b | number | undefined |
_force | boolean | false |
peak | number | 1 |
Returns ​
void
follow() ​
follow(layer): void;Defined in: src/core/Camera.ts:164
Keeps a container on the screen layer placed exactly over the world layer — zoom, scroll and this frame's shake — for things drawn in world units above the screen-layer night (fireflies). Written in apply, after the scene updated, so it never lags the world by a frame or misses a shake the world takes.
Parameters ​
| Parameter | Type |
|---|---|
layer | Container |
Returns ​
void
reset() ​
reset(): this;Defined in: src/core/Camera.ts:110
Back to how the engine made it: no target, no bounds, defaultZoom, scrolled to 0, no shake or flash. The camera is the engine's, shared by every scene, so a level's target and bounds would otherwise hold over the next scene (a title screen kept following a hero that no longer existed). SceneManager.replace calls it between the old scenes leaving and the new one entering; push and pop leave the camera alone, since the scene under a pause menu still wants its view when it comes back. Layers added with follow are kept: they belong to tools that outlive scenes (a debug overlay), not to a scene.
Returns ​
this
setBounds() ​
setBounds(
x,
y,
width,
height
): this;Defined in: src/core/Camera.ts:91
Parameters ​
| Parameter | Type |
|---|---|
x | number |
y | number |
width | number |
height | number |
Returns ​
this
setScroll() ​
setScroll(x, y): this;Defined in: src/core/Camera.ts:139
Parameters ​
| Parameter | Type |
|---|---|
x | number |
y | number |
Returns ​
this
setZoom() ​
setZoom(z): this;Defined in: src/core/Camera.ts:71
Parameters ​
| Parameter | Type |
|---|---|
z | number |
Returns ​
this
shake() ​
shake(
ms,
intensity,
angle?
): void;Defined in: src/core/Camera.ts:150
Screen shake: intensity is a fraction of the screen size, like before. With an angle (the way a blow travelled) the shake runs back and forth along it instead of at random, so a hit is felt coming from somewhere.
Parameters ​
| Parameter | Type |
|---|---|
ms | number |
intensity | number |
angle? | number |
Returns ​
void
snap() ​
snap(v): number;Defined in: src/core/Camera.ts:77
A world coordinate snapped to the nearest backing pixel: one screen pixel, or a half at ×1.5.
Parameters ​
| Parameter | Type |
|---|---|
v | number |
Returns ​
number
snapRoot() ​
snapRoot(v): number;Defined in: src/core/Camera.ts:87
Where an object's root lands (a body, and whatever is laid over one): a whole world pixel while the world is drawn on its own grid (Stage.pixelPerfect), else snap. One rule for every root, so a body, what it holds and what points at it fall on the same pixel; children keep whole-pixel offsets from it.
Parameters ​
| Parameter | Type |
|---|---|
v | number |
Returns ​
number
track() ​
track(target, options?): this;Defined in: src/core/Camera.ts:188
Keeps target (anything with x and y, read every frame: an entity's position, a sprite) in view, inside the bounds if there are any. Replaces any target before.
Parameters ​
| Parameter | Type |
|---|---|
target | { x: number; y: number; } |
target.x | number |
target.y | number |
options | TrackOptions |
Returns ​
this
unfollow() ​
unfollow(layer): void;Defined in: src/core/Camera.ts:168
Parameters ​
| Parameter | Type |
|---|---|
layer | Container |
Returns ​
void
untrack() ​
untrack(): this;Defined in: src/core/Camera.ts:195
Returns ​
this