Constructor
new Camera(context)
| Name | Type | Description |
|---|---|---|
context | CanvasRenderingContext2D | The canvas context |
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)
- Number
- Default Value
- canvas height * 0.5
// 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)
- 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.
- 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
| Name | Type | Description |
|---|---|---|
x | Number | The x position to translate to |
y | Number | The y position to translate to |
offsetx | Number | Y offset (from given position) |
offsety | Number | X offset (from given position) |
focus(position, offsetopt)
Position the center of the screen (using absolute position). Useful for following a player/sprite
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
position | gameify. | The position to translate to | ||
offset | gameify. | <optional> | new gameify.Vector2d(0, 0) | Offset by an amount |
getPosition() → {gameify.Vector2d}
Get the current position of the camera
- Type:
- gameify.
Vector2d
resetTransform()
Reset the camera's (canvas's) transformation
screenToWorld(x, y) → {gameify.Vector2d}
Convert screen coordinates to world coordinates
| Name | Type | Description |
|---|---|---|
x | Number | The x screen coordinate |
y | Number | The y screen coordinate |
- 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)
| Name | Type | Description |
|---|---|---|
mode | String | 'world' or 'ui' |
- Tutorials
setSpeed(speed)
Set the translation speed of the camera. 1 is instant, 0 is stationary.
| Name | Type | Description |
|---|---|---|
speed | Number | The speed of camera translations |
translate(x, y)
Translate the camera (relative to the current transform)
| Name | Type | Description |
|---|---|---|
x | Number | The x amount, in pixels, to translate |
y | Number | The y amount, in pixels, to translate |
translate(amount)
Translate the camera (relative to the current transform)
| Name | Type | Description |
|---|---|---|
amount | gameify. | The amount, in pixels, to translate |
translateAbsolute(x, y)
Translate the camera (using absolute positioning)
| Name | Type | Description |
|---|---|---|
x | Number | The x position to translate to |
y | Number | The y position to translate to |
translateAbsolute(position)
Translate the camera (using absolute positioning)
| Name | Type | Description |
|---|---|---|
position | gameify. | 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
| Name | Type | Description |
|---|---|---|
delta | Number | The time, in milliseconds, since the last frame |