Home Reference Source Repository
import Camera from 'webgame-lib/src/camera.js'
public class | source

Camera

An orthographic viewport to a scene that can be panned and zoomed.

Constructor Summary

Public Constructor
public

constructor(w: number, h: number, options: Object)

Initializes the camera.

Member Summary

Public Members
public

Bounds for center

public get

center: *

Gets the center.

public set

center(point: Vec): *

Center focus.

public

Height of viewport.

public

Maximum zoom.

public

Minimum zoom.

public

Speed of camera pan.

public

Rectangle representing the viewport.

public

Width of viewport.

public

Current zoom.

public

Factor to scale by when zoomed.

Method Summary

Public Methods
public

Apply transformations to the supplied context.

public

black()

Black out the whole screen.

public

clear()

Clear screen.

public

draw(drawable: Drawable, p: Vec)

Draw object on screen.

public

drawLayer(img: Image)

Draw fixed layer image with transformations applied.

public

end()

Reset transformations applied to context.

public

pan(px: number, py: number)

Pans the view.

public

Translate a point from screen space to world space.

public

zoom(out: boolean)

Zooms the view.

Public Constructors

public constructor(w: number, h: number, options: Object) source

Initializes the camera.

Params:

NameTypeAttributeDescription
w number

Width of viewport

h number

Height of viewport

options Object

Options

Public Members

public bounds: Rect source

Bounds for center

public get center: * source

Gets the center.

public set center(point: Vec): * source

Center focus.

public h: number source

Height of viewport.

public maxZoom: number source

Maximum zoom.

public minZoom: number source

Minimum zoom.

public panSpeed: number source

Speed of camera pan.

public view: Rect source

Rectangle representing the viewport.

public w: number source

Width of viewport.

public z: number source

Current zoom.

public zoomFactor: number source

Factor to scale by when zoomed.

Public Methods

public begin(ctx: CanvasRenderingContext2D) source

Apply transformations to the supplied context. Usually called at the beginning of every render cycle.

Params:

NameTypeAttributeDescription
ctx CanvasRenderingContext2D

Render context

public black() source

Black out the whole screen.

public clear() source

Clear screen.

public draw(drawable: Drawable, p: Vec) source

Draw object on screen. Culls if the object is out of view.

Params:

NameTypeAttributeDescription
drawable Drawable

Object to draw

p Vec

Center position to draw at

public drawLayer(img: Image) source

Draw fixed layer image with transformations applied.

Params:

NameTypeAttributeDescription
img Image

Fixed layer image

public end() source

Reset transformations applied to context. Called after the begin() method.

public pan(px: number, py: number) source

Pans the view.

Params:

NameTypeAttributeDescription
px number

Distance along X direction

py number

Distance along Y direction

public screenToWorld(v: Vec): Vec source

Translate a point from screen space to world space.

Params:

NameTypeAttributeDescription
v Vec

Point on screen

Return:

Vec

Point in world space coordinates

public zoom(out: boolean) source

Zooms the view.

Params:

NameTypeAttributeDescription
out boolean

Whether zooming out or in