gameify. Camera

A camera (controls the viewport of the game)

Constructor

new Camera(context)

Parameters:
NameTypeDescription
contextCanvasRenderingContext2D

The canvas context

Example
import { gameify } from "./gameify/gameify.js"
let myScreen = new gameify.Screen(canvas, 600, 400);
myScreen.camera.translationSpeed = 0.7;
myScreen.camera.translate(50, 70);

Members

maxDistance :Number

The maximum distance the camera can be from its target position, in pixels. If the camera goes too far away, it will speed up to stay close to the target. (Note, this distance is measured as a square, not a circle)

Type:
  • Number
Default Value
  • canvas height * 0.5
Example
// Set speed to zero to disable interpolation
camera.translationSpeed = 0; // or camera.setSpeed(0)
// Create a 100 px "dead zone" in the center of the screen before
// the camera starts following
camera.maxDistance = 100;

minDistance :Number

The minimum distance the camera must be from its target position, in pixels, before the camera starts to move. (Note, this distance is measured as a square, not a circle)

Type:
  • Number
Default Value
  • 1

screenToWorld

Convert screen coordinates to world coordinates

translationSpeed :Number

The translation speed of the camera. 1 is instant, 0 is stationary.

Type:
  • Number
Default Value
  • 1

Methods

focus(x, y, offsetx, offsety)

Position the center of the screen (using absolute position). Useful for following a player/sprite

Parameters:
NameTypeDescription
xNumber

The x position to translate to

yNumber

The y position to translate to

offsetxNumber

Y offset (from given position)

offsetyNumber

X offset (from given position)

focus(position, offsetopt)

Position the center of the screen (using absolute position). Useful for following a player/sprite

Parameters:
NameTypeAttributesDefaultDescription
positiongameify.Vector2d

The position to translate to

offsetgameify.Vector2d<optional>
new gameify.Vector2d(0, 0)

Offset by an amount

getPosition() → {gameify.Vector2d}

Get the current position of the camera

Returns:
Type: 
gameify.Vector2d

resetTransform()

Reset the camera's (canvas's) transformation

screenToWorld(x, y) → {gameify.Vector2d}

Convert screen coordinates to world coordinates

Parameters:
NameTypeDescription
xNumber

The x screen coordinate

yNumber

The y screen coordinate

Returns:
Type: 
gameify.Vector2d

setDrawMode(mode)

Sets the camera draw mode. Set to 'ui' mode before drawing UI elements, set to 'world' for drawing everything else. (draw mode is reset to 'world' each frame). Equivalent to calling camera.resetTransform() or camera.update(0)

Parameters:
NameTypeDescription
modeString

'world' or 'ui'

setSpeed(speed)

Set the translation speed of the camera. 1 is instant, 0 is stationary.

Parameters:
NameTypeDescription
speedNumber

The speed of camera translations

translate(x, y)

Translate the camera (relative to the current transform)

Parameters:
NameTypeDescription
xNumber

The x amount, in pixels, to translate

yNumber

The y amount, in pixels, to translate

translate(amount)

Translate the camera (relative to the current transform)

Parameters:
NameTypeDescription
amountgameify.Vector2d

The amount, in pixels, to translate

translateAbsolute(x, y)

Translate the camera (using absolute positioning)

Parameters:
NameTypeDescription
xNumber

The x position to translate to

yNumber

The y position to translate to

translateAbsolute(position)

Translate the camera (using absolute positioning)

Parameters:
NameTypeDescription
positiongameify.Vector2d

The position to translate to

update(delta)

Update the camera. If you're using the a gameify.Screen's camera, this will be called automatically. This function rounds translation vectors to whole numbers for better rendering

Parameters:
NameTypeDescription
deltaNumber

The time, in milliseconds, since the last frame