Convert from world to screen space coordinates * @param {Vector2} worldPos * @return {Vector2} * @memberof Draw
(worldPos)
| 1001 | * @return {Vector2} |
| 1002 | * @memberof Draw */ |
| 1003 | function 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 |
no test coverage detected