Skip to content

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 ​

ts
new Camera(world, screen): Camera;

Defined in: src/core/Camera.ts:60

Parameters ​

ParameterType
worldContainer
screenContainer

Returns ​

Camera

Properties ​

defaultZoom ​

ts
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 ​

ts
height: number = 0;

Defined in: src/core/Camera.ts:44


pixelGrid ​

ts
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 ​

ts
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 ​

ts
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 ​

ts
scrollX: number = 0;

Defined in: src/core/Camera.ts:41


scrollY ​

ts
scrollY: number = 0;

Defined in: src/core/Camera.ts:42


width ​

ts
width: number = 0;

Defined in: src/core/Camera.ts:43


zoom ​

ts
zoom: number = 1;

Defined in: src/core/Camera.ts:32

Accessors ​

boundsRect ​

Get Signature ​

ts
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 ​

ts
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 ​

ts
get worldView(): ViewRect;

Defined in: src/core/Camera.ts:65

Returns ​

ViewRect

Methods ​

apply() ​

ts
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 ​

ParameterType
nownumber

Returns ​

void


centerOn() ​

ts
centerOn(x, y): this;

Defined in: src/core/Camera.ts:132

Parameters ​

ParameterType
xnumber
ynumber

Returns ​

this


clearBounds() ​

ts
clearBounds(): this;

Defined in: src/core/Camera.ts:97

Lets the view go anywhere again.

Returns ​

this


flash() ​

ts
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 ​

ParameterTypeDefault value
msnumberundefined
rnumberundefined
gnumberundefined
bnumberundefined
_forcebooleanfalse
peaknumber1

Returns ​

void


follow() ​

ts
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 ​

ParameterType
layerContainer

Returns ​

void


reset() ​

ts
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() ​

ts
setBounds(
   x, 
   y, 
   width, 
   height
): this;

Defined in: src/core/Camera.ts:91

Parameters ​

ParameterType
xnumber
ynumber
widthnumber
heightnumber

Returns ​

this


setScroll() ​

ts
setScroll(x, y): this;

Defined in: src/core/Camera.ts:139

Parameters ​

ParameterType
xnumber
ynumber

Returns ​

this


setZoom() ​

ts
setZoom(z): this;

Defined in: src/core/Camera.ts:71

Parameters ​

ParameterType
znumber

Returns ​

this


shake() ​

ts
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 ​

ParameterType
msnumber
intensitynumber
angle?number

Returns ​

void


snap() ​

ts
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 ​

ParameterType
vnumber

Returns ​

number


snapRoot() ​

ts
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 ​

ParameterType
vnumber

Returns ​

number


track() ​

ts
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 ​

ParameterType
target{ x: number; y: number; }
target.xnumber
target.ynumber
optionsTrackOptions

Returns ​

this


unfollow() ​

ts
unfollow(layer): void;

Defined in: src/core/Camera.ts:168

Parameters ​

ParameterType
layerContainer

Returns ​

void


untrack() ​

ts
untrack(): this;

Defined in: src/core/Camera.ts:195

Returns ​

this

Released under the MIT License.