gamvas.Camera

Description

The camera class.

Note

Every gamvas.State has a default camera, which you can access with this.camera

Constructor

new gamvas.Camera([x], [y], [rot], [zoom]);

Parameters

x/yThe position of the cameras center (optional)
rotThe rotation of the camera in radians (optional)
zoomThe zoom factor of the camera (optional)
Summary
gamvas.CameraThe camera class.
Variables
positionA gamvas.Vector2D object that holds the position of the camera.
rotationThe rotation of the camera in radians.
zoomFactorThe zoom factor of the camera.
Functions
setPositionSet the position of the camera to absolute values.
moveMove the camera, relative to its current position.
setRotationSet the rotation to a specific degree in radians
rotateRotate the camera for r radians
setZoomSet a specific zoom level to the camera.
zoomZoom the camera
startStart the camera.
stopStop the camera, everything drawn from now on will not be under the influence of the camera.
toWorldConvert screen coordinates to world coordinates.
toScreenConvert world coordinates to screen coordinates.

Variables

position

this.position

Description

A gamvas.Vector2D object that holds the position of the camera.  It is recommended to use it only for reading.

rotation

this.rotation

Description

The rotation of the camera in radians.  It is recommended to use it only for reading.

zoomFactor

this.zoomFactor

Description

The zoom factor of the camera.  It is recommended to use it only for reading.

Functions

setPosition

gamvas.Camera.prototype.setPosition = function(x,
y)

Description

Set the position of the camera to absolute values.

Parameters

xThe new x position of the cameras center
yThe new y position of the cameras center

move

gamvas.Camera.prototype.move = function(x,
y)

Description

Move the camera, relative to its current position.

Parameters

xThe amount of pixels to move along the horizontal axis
yThe amount of pixels to move along the vertical axis

setRotation

gamvas.Camera.prototype.setRotation = function(r)

Description

Set the rotation to a specific degree in radians

Parameters

rrotation in radians

rotate

gamvas.Camera.prototype.rotate = function(r)

Description

Rotate the camera for r radians

Parameters

rrotation in radians

setZoom

gamvas.Camera.prototype.setZoom = function(z)

Description

Set a specific zoom level to the camera.

Parameters

zthe zoom level, as 1 = original size, < 1 = zoom out, > 1 = zoom in

zoom

gamvas.Camera.prototype.zoom = function(z)

Description

Zoom the camera

Parameters

zthe change of zoom, relative to its current zoom

start

gamvas.Camera.prototype.start = function()

Description

Start the camera.  Needs to be called before every drawing operation that should be in the influence of the camera (e.g. your game actors)

See

gamvas.Camera.stop

Example

this.camera.start();
// draw everything under camera influence
this.drawGameObjects();
this.camera.stop();
// draw everything not under camera influence (e.g. Score, Health, ...)
this.drawHUD();

stop

gamvas.Camera.prototype.stop = function()

Description

Stop the camera, everything drawn from now on will not be under the influence of the camera.  You usually do this before drawing player score and health.

See

gamvas.Camera.start

toWorld

gamvas.Camera.prototype.toWorld = function(x,
y)

Description

Convert screen coordinates to world coordinates.  For example if you have a rotated camera and the user clicks on the screen, use this function to check which position the click on the rotated camera is in the unrotated world.

Returns

gamvas.Vector2D

See

gamvas.Camera.toScreen

toScreen

gamvas.Camera.prototype.toScreen = function(x,
y)

Description

Convert world coordinates to screen coordinates.  If you have a object in your world and your camera has moved 3 screen aside, this function gets you the actual screen position of the object.

Returns

gamvas.Vector2D

See

gamvas.Camera.toWorld

this.position
A gamvas.Vector2D object that holds the position of the camera.
A 2D vector class
this.rotation
The rotation of the camera in radians.
this.zoomFactor
The zoom factor of the camera.
gamvas.Camera.prototype.setPosition = function(x,
y)
Set the position of the camera to absolute values.
gamvas.Camera.prototype.move = function(x,
y)
Move the camera, relative to its current position.
gamvas.Camera.prototype.setRotation = function(r)
Set the rotation to a specific degree in radians
gamvas.Camera.prototype.rotate = function(r)
Rotate the camera for r radians
gamvas.Camera.prototype.setZoom = function(z)
Set a specific zoom level to the camera.
gamvas.Camera.prototype.zoom = function(z)
Zoom the camera
gamvas.Camera.prototype.start = function()
Start the camera.
gamvas.Camera.prototype.stop = function()
Stop the camera, everything drawn from now on will not be under the influence of the camera.
gamvas.Camera.prototype.toWorld = function(x,
y)
Convert screen coordinates to world coordinates.
gamvas.Camera.prototype.toScreen = function(x,
y)
Convert world coordinates to screen coordinates.
Game state class, overwrite for own states
Close