Skip to main content

Class: Camera

camera/camera.Camera

Represents the camera in 2D world space.

The camera's position is the centre of the viewport.

Hierarchy

Constructors

constructor

โ€ข new Camera(centre, vw, vh)

Creates a Camera instance and instantiates its Viewport with a width and height at the camera's centre.

Parameters

NameTypeDescription
centrevec2The centre of the camera's viewport
vwnumberThe camera's viewport width
vhnumberThe camera's viewport height

Overrides

Object2D.constructor

Defined in

src/camera/camera.ts:25

โ€ข new Camera(centre, viewport)

Creates a Camera instance and sets its viewport.

Parameters

NameTypeDescription
centrevec2The centre of the camera's viewport
viewportViewportThe viewport to use with the camera

Overrides

Object2D.constructor

Defined in

src/camera/camera.ts:33

Properties

lastPos

โ€ข lastPos: vec2

Defined in

src/camera/camera.ts:16


listeners

โ€ข Protected listeners: Object = {}

Index signature

โ–ช [index: string]: Listener[]

Inherited from

Object2D.listeners

Defined in

src/object2d.ts:32


maxZoom

โ€ข maxZoom: number = 10

Defined in

src/camera/camera.ts:14


minZoom

โ€ข minZoom: number = 0.1

Defined in

src/camera/camera.ts:13


viewport

โ€ข viewport: Viewport

Defined in

src/camera/camera.ts:11


zoomLevel

โ€ข Private zoomLevel: number = 1

Defined in

src/camera/camera.ts:12

Methods

addEventListener

โ–ธ addEventListener(event, listener): boolean

Executes a function when an event is fired.

Parameters

NameTypeDescription
eventstringThe event to listen for
listenerListenerThe function to execute when the event fired

Returns

boolean

Wether or not the listener was added

Inherited from

Object2D.addEventListener

Defined in

src/object2d.ts:209


fireEvent

โ–ธ fireEvent(event, ...e): void

Executes all listeners attached to a given event.

Parameters

NameTypeDescription
eventstringThe event to fire
...eanyThe data to pass to each event listener

Returns

void

Inherited from

Object2D.fireEvent

Defined in

src/object2d.ts:185


getEventListeners

โ–ธ getEventListeners(event): Listener[]

Gets the listeners attached to an event.

Parameters

NameType
eventstring

Returns

Listener[]

An array of listeners attached to the given event

Inherited from

Object2D.getEventListeners

Defined in

src/object2d.ts:198


getEvents

โ–ธ getEvents(): string[]

Gets the events that can be listened to on the object.

Returns

string[]

The events the object supports

Inherited from

Object2D.getEvents

Defined in

src/object2d.ts:175


getPosition

โ–ธ getPosition(): vec2

Gets the object's position.

Returns

vec2

The object's position as a vec2

Inherited from

Object2D.getPosition

Defined in

src/object2d.ts:82


getRotation

โ–ธ getRotation(): number

Gets the object's rotation as a number in radians relative to y+.

Returns

number

The object's rotation

Inherited from

Object2D.getRotation

Defined in

src/object2d.ts:139


getZoom

โ–ธ getZoom(): number

Gets the zoom level of the camera.

Returns

number

The zoom level of the camera

Defined in

src/camera/camera.ts:92


moveRight

โ–ธ moveRight(dist): void

Moves the object's position right (x+) relative to it's rotation.

Parameters

NameTypeDescription
distnumberThe distance to move right, can be + or -

Returns

void

Inherited from

Object2D.moveRight

Defined in

src/object2d.ts:100


moveUp

โ–ธ moveUp(dist): void

Moves the object's position up (y+) relative to it's rotation.

Parameters

NameTypeDescription
distnumberThe distance to move up, can be + or -

Returns

void

Inherited from

Object2D.moveUp

Defined in

src/object2d.ts:114


removeEventListener

โ–ธ removeEventListener(event, listener): boolean

Removes a listener from an event.

Parameters

NameTypeDescription
eventstringThe event the listener is attached to
listenerListenerThe listener to remove

Returns

boolean

Wether or not the listener was removed

Inherited from

Object2D.removeEventListener

Defined in

src/object2d.ts:223


rotate

โ–ธ rotate(angle): void

Increments the object's rotation by an angle in radians.

Parameters

NameTypeDescription
anglenumberThe angle to rotate by in radians

Returns

void

Inherited from

Object2D.rotate

Defined in

src/object2d.ts:148


setPosition

โ–ธ setPosition(pos): void

Sets the object's position.

Parameters

NameTypeDescription
posvec2The object's new position

Returns

void

Inherited from

Object2D.setPosition

Defined in

src/object2d.ts:43


setPositionX

โ–ธ setPositionX(x): void

Sets the x component of the object's position.

Parameters

NameTypeDescription
xnumberThe object's new x coordinate

Returns

void

Inherited from

Object2D.setPositionX

Defined in

src/object2d.ts:56


setPositionY

โ–ธ setPositionY(y): void

Sets the y component of the object's position.

Parameters

NameTypeDescription
ynumberThe object's new y coordinate

Returns

void

Inherited from

Object2D.setPositionY

Defined in

src/object2d.ts:69


setRotation

โ–ธ setRotation(angle): void

Sets the object's rotation.

Parameters

NameTypeDescription
anglenumberThe object's new rotation

Returns

void

Inherited from

Object2D.setRotation

Defined in

src/object2d.ts:128


setZoom

โ–ธ setZoom(zoom): void

Sets the zoom level of the camera.

This will change the size of this.viewport.

Parameters

NameTypeDescription
zoomnumberThe zoom level

Returns

void

Defined in

src/camera/camera.ts:76


setupEvents

โ–ธ Protected setupEvents(): void

Sets all the events that can be used in listeners.

Listeners can be added for an event using:

   this.listeners.eventName = [];

example Setup listeners for "position" and "rotate" events. this.listeners.position = []; this.listeners.rotate = [];

Returns

void

Inherited from

Object2D.setupEvents

Defined in

src/object2d.ts:165


translate

โ–ธ translate(v): void

Translates the object by the provided vector.

Parameters

NameTypeDescription
vvec2The vector to translate by

Returns

void

Inherited from

Object2D.translate

Defined in

src/object2d.ts:91


update

โ–ธ update(): void

Updates the camera's viewport if the camera has changed position since the last call to update.

Returns

void

Defined in

src/camera/camera.ts:52


zoom

โ–ธ zoom(zoom): void

Zooms the camera by the given amount.

Parameters

NameTypeDescription
zoomnumberThe amount to zoom by

Returns

void

Defined in

src/camera/camera.ts:64