Class: CameraMath
Screen↔world coordinate conversion for a camera. Mirrors Unity's Camera.ScreenToWorldPoint / WorldToScreenPoint / WorldToViewportPoint / ViewportToWorldPoint and Cocos Creator's equivalents.
Conventions:
- Screen coordinates are in canvas pixels with the origin at the top-left, +y down. (This matches DOM/Canvas convention; Unity in contrast uses bottom-left origin. If your game prefers bottom-left, flip y at the boundary.)
- Viewport coordinates are normalized 0..1 within the camera's viewport rect, +y down.
- World coordinates are in framework world units, +y up, rotations counter-clockwise (see the coordinate contract on IRenderer).
All functions require the canvas pixel dimensions; the camera's viewport rect (normalized 0..1) is applied internally.
Constructors
Constructor
ts
new CameraMath(): CameraMath;Returns
CameraMath
Methods
ScreenPointInCamera()
ts
static ScreenPointInCamera(
cameraEntity,
screenX,
screenY,
canvasWidth,
canvasHeight
): boolean;True if the given screen pixel falls inside this camera's viewport rect.
Parameters
| Parameter | Type |
|---|---|
cameraEntity | Entity |
screenX | number |
screenY | number |
canvasWidth | number |
canvasHeight | number |
Returns
boolean
ScreenToViewport()
ts
static ScreenToViewport(
cameraEntity,
screenX,
screenY,
canvasWidth,
canvasHeight
): Vec2;Screen pixel → normalized viewport coords (0..1) within this camera's viewport.
Parameters
| Parameter | Type |
|---|---|
cameraEntity | Entity |
screenX | number |
screenY | number |
canvasWidth | number |
canvasHeight | number |
Returns
ScreenToWorld()
ts
static ScreenToWorld(
cameraEntity,
screenX,
screenY,
canvasWidth,
canvasHeight
): Vec2 | null;Screen pixel → world coords (orthographic). Returns null if camera is perspective (use the 3D variant).
Parameters
| Parameter | Type |
|---|---|
cameraEntity | Entity |
screenX | number |
screenY | number |
canvasWidth | number |
canvasHeight | number |
Returns
Vec2 | null
ViewportToScreen()
ts
static ViewportToScreen(
cameraEntity,
vx,
vy,
canvasWidth,
canvasHeight
): Vec2;Normalized viewport coords (0..1) → screen pixel.
Parameters
| Parameter | Type |
|---|---|
cameraEntity | Entity |
vx | number |
vy | number |
canvasWidth | number |
canvasHeight | number |
Returns
WorldToScreen()
ts
static WorldToScreen(
cameraEntity,
worldX,
worldY,
canvasWidth,
canvasHeight
): Vec2 | null;World coords → screen pixel (orthographic). Returns null if camera is perspective.
Parameters
| Parameter | Type |
|---|---|
cameraEntity | Entity |
worldX | number |
worldY | number |
canvasWidth | number |
canvasHeight | number |
Returns
Vec2 | null