Class

Camera

Camera(context, frames)

The Camera class is designed to manage the rendering and viewport of the game world. It focuses on controlling the visible area based on the position of the player and efficiently rendering only the visible portion of the game world.
Constructor

# new Camera(context, frames)

Create new camera.
Parameters:
Name Type Default Description
context CanvasRenderingContext2D the canvas rendering context
frames number 0 counter to control the frequency of updates

View Source browser/lib/Scene/Camera.js, line 11

Classes

Camera

Members

CanvasRenderingContext2D

# context

context - the canvas rendering context.

View Source browser/lib/Scene/Camera.js, line 49

number

# frames

frames - counter to control camera updates.

View Source browser/lib/Scene/Camera.js, line 55

number

# offsetX

offsetX - the canvas x-axis offset.

View Source browser/lib/Scene/Camera.js, line 37

number

# offsetY

offsetY - the canvas y-axis offset.

View Source browser/lib/Scene/Camera.js, line 43

Object

# screen

screen - object to represent the viewport.

View Source browser/lib/Scene/Camera.js, line 61

number

# x

x - the camera tracking x coordinate.

View Source browser/lib/Scene/Camera.js, line 25

number

# y

y - the camera tracking y coordinate.

View Source browser/lib/Scene/Camera.js, line 31

Methods

# newScene() → {void}

Clear the rendering context to prepare for a new scene.

View Source browser/lib/Scene/Camera.js, line 122

void

# postRender() → {void}

Restore the rendering context state after rendering.

View Source browser/lib/Scene/Camera.js, line 158

void

# preRender(entity) → {void}

Adjust the camera's position based on the passed entity's position. This method calculates the offset needed to center the entity on the screen and applies a translation to the rendering context to simulate camera movement.
Parameters:
Name Type Description
entity Object the game entity (usually the player)

View Source browser/lib/Scene/Camera.js, line 136

void

# resize() → {void}

Resize placeholder.

View Source browser/lib/Scene/Camera.js, line 115

void

# update(player, entities) → {void}

Update the camera and rendering state for entities. This method increments the frame counter and, when reaching a certain threshold, calculates the new screen object based on the player's position and the canvas dimensions. It checks each entity's bounding box or bounding circle against the screen to determine if they are within the visible area. Entities outside the visible area are flagged as sleep and not updated.
Parameters:
Name Type Description
player Player the player entity
entities array array of game entities

View Source browser/lib/Scene/Camera.js, line 78

void