|
@@ -990,31 +990,68 @@ export class Viewport extends ViewportEventTarget
|
990
|
990
|
* position in canvas space. Units in normalized space range from -1 to +1.
|
991
|
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
|
|
- * @param position in normalized units
|
994
|
|
- * @param space either "normalized" (default) or "adjusted", @see PointerSpace
|
|
993
|
+ * @param position in space units
|
|
994
|
+ * @param space either "normalized" (default) or "adjusted"; @see PointerSpace
|
995
|
995
|
* @returns an equivalent pixel position in canvas space
|
996
|
996
|
*/
|
997
|
997
|
convertToPixels(position: Vector2, space: "normalized" | "adjusted" = 'normalized'): Vector2
|
998
|
998
|
{
|
999
|
999
|
const canvas = this.canvas;
|
1000
|
|
- let x = 0.5 * (1 + position.x) * canvas.width;
|
1001
|
|
- let y = 0.5 * (1 - position.y) * canvas.height;
|
|
1000
|
+ let mx = 1, my = 1;
|
1002
|
1001
|
|
1003
|
1002
|
if(space == 'adjusted') {
|
|
1003
|
+ // convert from adjusted to normalized space
|
1004
|
1004
|
const a = canvas.width / canvas.height;
|
1005
|
1005
|
|
1006
|
1006
|
if(a >= 1)
|
1007
|
|
- y *= a;
|
|
1007
|
+ my *= a;
|
1008
|
1008
|
else
|
1009
|
|
- x /= a;
|
|
1009
|
+ mx /= a;
|
1010
|
1010
|
}
|
1011
|
1011
|
else if(space != 'normalized')
|
1012
|
1012
|
throw new IllegalArgumentError(`Invalid space: "${space}"`);
|
1013
|
1013
|
|
|
1014
|
+ // convert from normalized to canvas space
|
|
1015
|
+ const x = 0.5 * (1 + position.x * mx) * canvas.width;
|
|
1016
|
+ const y = 0.5 * (1 - position.y * my) * canvas.height;
|
|
1017
|
+
|
|
1018
|
+ // done!
|
1014
|
1019
|
return new Vector2(x, y);
|
1015
|
1020
|
}
|
1016
|
1021
|
|
1017
|
1022
|
/**
|
|
1023
|
+ * Convert a pixel position given in canvas space to a corresponding
|
|
1024
|
+ * position in space units. This is the inverse of convertToPixels().
|
|
1025
|
+ * @param position in canvas space
|
|
1026
|
+ * @space either "normalized" (default) or "adjusted"; see @PointerSpace
|
|
1027
|
+ * @returns an equivalent position in space units
|
|
1028
|
+ */
|
|
1029
|
+ convertFromPixels(position: Vector2, space: "normalized" | "adjusted" = 'normalized'): Vector2
|
|
1030
|
+ {
|
|
1031
|
+ const canvas = this.canvas;
|
|
1032
|
+ let mx = 1, my = 1;
|
|
1033
|
+
|
|
1034
|
+ // convert from canvas to normalized space
|
|
1035
|
+ const x = 2 * position.x / canvas.width - 1;
|
|
1036
|
+ const y = -2 * position.y / canvas.height + 1;
|
|
1037
|
+
|
|
1038
|
+ if(space == 'adjusted') {
|
|
1039
|
+ // convert from normalized to adjusted space
|
|
1040
|
+ const a = canvas.width / canvas.height;
|
|
1041
|
+
|
|
1042
|
+ if(a >= 1)
|
|
1043
|
+ my /= a;
|
|
1044
|
+ else
|
|
1045
|
+ mx *= a;
|
|
1046
|
+ }
|
|
1047
|
+ else if(space != 'normalized')
|
|
1048
|
+ throw new IllegalArgumentError(`Invalid space: "${space}"`);
|
|
1049
|
+
|
|
1050
|
+ // done!
|
|
1051
|
+ return new Vector2(x * mx, y * my);
|
|
1052
|
+ }
|
|
1053
|
+
|
|
1054
|
+ /**
|
1018
|
1055
|
* Initialize the viewport (when the session starts)
|
1019
|
1056
|
* @param getMediaSize
|
1020
|
1057
|
* @param sessionMode
|