Camera2D
A 2D camera that produces a transform matrix for SpriteBatch.begin(). Follows the XNA convention.
Constructor
Section titled “Constructor”import { Camera2D } from 'webgpu-spritebatch'
const cam = new Camera2D()Properties
Section titled “Properties”| Property | Type | Description |
|---|---|---|
position | [x, y] | World-space point the camera is centered on |
origin | [x, y] | Screen-space anchor (typically screen center) |
zoom | number | Zoom factor (1 = no zoom) |
rotation | number | Rotation in radians |
Methods
Section titled “Methods”getTransformMatrix()
Section titled “getTransformMatrix()”Returns a Float32Array(16) — a 4x4 column-major matrix to pass to BeginOptions.transformMatrix.
batch.begin({ transformMatrix: cam.getTransformMatrix() })lookAt(x, y)
Section titled “lookAt(x, y)”Shorthand for setting position.
cam.lookAt(playerX, playerY)screenToWorld(sx, sy)
Section titled “screenToWorld(sx, sy)”Convert screen coordinates to world coordinates. Returns [x, y].
const [wx, wy] = cam.screenToWorld(mouseX, mouseY)worldToScreen(wx, wy)
Section titled “worldToScreen(wx, wy)”Convert world coordinates to screen coordinates. Returns [x, y].
const [sx, sy] = cam.worldToScreen(entityX, entityY)