Selaa lähdekoodia

Viewport.convertToPixels(): add parameter space

customisations
alemart 9 kuukautta sitten
vanhempi
commit
b161a74982
2 muutettua tiedostoa jossa 22 lisäystä ja 9 poistoa
  1. 4
    3
      docs/api/viewport.md
  2. 18
    6
      src/core/viewport.ts

+ 4
- 3
docs/api/viewport.md Näytä tiedosto

@@ -156,15 +156,16 @@ A promise that is resolved once the fullscreen mode is no longer active, or reje
156 156
 
157 157
 ### convertToPixels
158 158
 
159
-`viewport.convertToPixels(position: Vector2): Vector2`
159
+`viewport.convertToPixels(position: Vector2, space: string): Vector2`
160 160
 
161
-Convert a `position` given in normalized units to a corresponding pixel position in [canvas](#canvas) space. Normalized units range from -1 to +1. The center of the canvas is at (0,0). The top right corner is at (1,1). The bottom left corner is at (-1,-1).
161
+Convert a `position` given in space units to a corresponding pixel position in [canvas](#canvas) space.
162 162
 
163 163
 *Since:* 0.4.0
164 164
 
165 165
 **Arguments**
166 166
 
167
-`position: Vector2`. A position in normalized units.
167
+- `position: Vector2`. A position in space units.
168
+- `space: string, optional`. The space to convert from. Possible values: `"normalized"` (default) or `"adjusted"`. See also: [PointerTracker.space](pointer-tracker.md#space).
168 169
 
169 170
 **Returns**
170 171
 

+ 18
- 6
src/core/viewport.ts Näytä tiedosto

@@ -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
     }

Loading…
Peruuta
Tallenna