|
@@ -986,18 +986,30 @@ export class Viewport extends ViewportEventTarget
|
986
|
986
|
}
|
987
|
987
|
|
988
|
988
|
/**
|
989
|
|
- * Convert a position given in normalized units to a corresponding pixel
|
990
|
|
- * position in canvas space. Normalized units range from -1 to +1. The
|
991
|
|
- * center of the canvas is at (0,0). The top right corner is at (1,1).
|
|
989
|
+ * Convert a position given in space units to a corresponding pixel
|
|
990
|
+ * position in canvas space. Units in normalized space range from -1 to +1.
|
|
991
|
+ * The center of the canvas is at (0,0). The top right corner is at (1,1).
|
992
|
992
|
* The bottom left corner is at (-1,-1).
|
993
|
993
|
* @param position in normalized units
|
|
994
|
+ * @param space either "normalized" (default) or "adjusted", @see PointerSpace
|
994
|
995
|
* @returns an equivalent pixel position in canvas space
|
995
|
996
|
*/
|
996
|
|
- convertToPixels(position: Vector2): Vector2
|
|
997
|
+ convertToPixels(position: Vector2, space: "normalized" | "adjusted" = 'normalized'): Vector2
|
997
|
998
|
{
|
998
|
999
|
const canvas = this.canvas;
|
999
|
|
- const x = 0.5 * (1 + position.x) * canvas.width;
|
1000
|
|
- const y = 0.5 * (1 - position.y) * canvas.height;
|
|
1000
|
+ let x = 0.5 * (1 + position.x) * canvas.width;
|
|
1001
|
+ let y = 0.5 * (1 - position.y) * canvas.height;
|
|
1002
|
+
|
|
1003
|
+ if(space == 'adjusted') {
|
|
1004
|
+ const a = canvas.width / canvas.height;
|
|
1005
|
+
|
|
1006
|
+ if(a >= 1)
|
|
1007
|
+ y *= a;
|
|
1008
|
+ else
|
|
1009
|
+ x /= a;
|
|
1010
|
+ }
|
|
1011
|
+ else if(space != 'normalized')
|
|
1012
|
+ throw new IllegalArgumentError(`Invalid space: "${space}"`);
|
1001
|
1013
|
|
1002
|
1014
|
return new Vector2(x, y);
|
1003
|
1015
|
}
|