Skip to content

Camera2D

A 2D camera that produces a transform matrix for SpriteBatch.begin(). Follows the XNA convention.

import { Camera2D } from 'webgpu-spritebatch'
const cam = new Camera2D()
PropertyTypeDescription
position[x, y]World-space point the camera is centered on
origin[x, y]Screen-space anchor (typically screen center)
zoomnumberZoom factor (1 = no zoom)
rotationnumberRotation in radians

Returns a Float32Array(16) — a 4x4 column-major matrix to pass to BeginOptions.transformMatrix.

batch.begin({ transformMatrix: cam.getTransformMatrix() })

Shorthand for setting position.

cam.lookAt(playerX, playerY)

Convert screen coordinates to world coordinates. Returns [x, y].

const [wx, wy] = cam.screenToWorld(mouseX, mouseY)

Convert world coordinates to screen coordinates. Returns [x, y].

const [sx, sy] = cam.worldToScreen(entityX, entityY)