Skip to content

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 ​

ParameterType
cameraEntityEntity
screenXnumber
screenYnumber
canvasWidthnumber
canvasHeightnumber

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 ​

ParameterType
cameraEntityEntity
screenXnumber
screenYnumber
canvasWidthnumber
canvasHeightnumber

Returns ​

Vec2


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 ​

ParameterType
cameraEntityEntity
screenXnumber
screenYnumber
canvasWidthnumber
canvasHeightnumber

Returns ​

Vec2 | null


ViewportToScreen() ​

ts
static ViewportToScreen(
   cameraEntity, 
   vx, 
   vy, 
   canvasWidth, 
   canvasHeight
): Vec2;

Normalized viewport coords (0..1) → screen pixel.

Parameters ​

ParameterType
cameraEntityEntity
vxnumber
vynumber
canvasWidthnumber
canvasHeightnumber

Returns ​

Vec2


WorldToScreen() ​

ts
static WorldToScreen(
   cameraEntity, 
   worldX, 
   worldY, 
   canvasWidth, 
   canvasHeight
): Vec2 | null;

World coords → screen pixel (orthographic). Returns null if camera is perspective.

Parameters ​

ParameterType
cameraEntityEntity
worldXnumber
worldYnumber
canvasWidthnumber
canvasHeightnumber

Returns ​

Vec2 | null

© 2026 PlayAdGentic