MCPcopy
hub / github.com/KilledByAPixel/LittleJS / worldToScreen

Function worldToScreen

src/engineDraw.js:1003–1021  ·  view source on GitHub ↗

Convert from world to screen space coordinates * @param {Vector2} worldPos * @return {Vector2} * @memberof Draw

(worldPos)

Source from the content-addressed store, hash-verified

1001 * @return {Vector2}
1002 * @memberof Draw */
1003function worldToScreen(worldPos)
1004{
1005 ASSERT(isVector2(worldPos), 'worldPos must be a vec2');
1006
1007 let x = worldPos.x - cameraPos.x;
1008 let y = worldPos.y - cameraPos.y;
1009 if (cameraAngle)
1010 {
1011 // apply inverse camera rotation
1012 const c = cos(cameraAngle), s = sin(cameraAngle);
1013 const xr = x * c - y * s, yr = x * s + y * c;
1014 x = xr; y = yr;
1015 }
1016 return new Vector2
1017 (
1018 x * cameraScale + mainCanvasSize.x/2 - .5,
1019 y * -cameraScale + mainCanvasSize.y/2 - .5
1020 );
1021}
1022
1023/** Convert from screen to world space coordinates for a directional vector (no translation)
1024 * @param {Vector2} screenDelta

Callers 6

debugRenderFunction · 0.85
drawTextureWrappedFunction · 0.85
drawCanvas2DFunction · 0.85
drawTextFunction · 0.85
drawTextMethod · 0.85
playMethod · 0.85

Calls 2

isVector2Function · 0.85
ASSERTFunction · 0.70

Tested by

no test coverage detected