Просмотр исходного кода

Introduce Viewport.convertFromPixels()

customisations
alemart 9 месяцев назад
Родитель
Сommit
85f2e7e4de
2 измененных файлов: 61 добавлений и 7 удалений
  1. 18
    1
      docs/api/viewport.md
  2. 43
    6
      src/core/viewport.ts

+ 18
- 1
docs/api/viewport.md Просмотреть файл

@@ -158,7 +158,7 @@ A promise that is resolved once the fullscreen mode is no longer active, or reje
158 158
 
159 159
 `viewport.convertToPixels(position: Vector2, space: string): Vector2`
160 160
 
161
-Convert a `position` given in space units to a corresponding pixel position in [canvas](#canvas) space.
161
+Convert a `position` given in space units to a corresponding pixel position in [canvas](#canvas) space. See also: [convertFromPixels()](#convertfrompixels).
162 162
 
163 163
 *Since:* 0.4.0
164 164
 
@@ -171,6 +171,23 @@ Convert a `position` given in space units to a corresponding pixel position in [
171 171
 
172 172
 An equivalent pixel position in canvas space.
173 173
 
174
+### convertFromPixels
175
+
176
+`viewport.convertFromPixels(position: Vector2, space: string): Vector2`
177
+
178
+Convert a pixel `position` given in [canvas](#canvas) space to a corresponding position in space units. See also: [convertToPixels()](#converttopixels).
179
+
180
+*Since:* 0.4.0
181
+
182
+**Arguments**
183
+
184
+- `position: Vector2`. A pixel position in canvas space.
185
+- `space: string, optional`. The space to convert to. Possible values: `"normalized"` (default) or `"adjusted"`. See also: [PointerTracker.space](pointer-tracker.md#space).
186
+
187
+**Returns**
188
+
189
+An equivalent position in space units.
190
+
174 191
 ## Events
175 192
 
176 193
 A viewport is an [AREventTarget](ar-event-target.md). You can listen to the following events:

+ 43
- 6
src/core/viewport.ts Просмотреть файл

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

Загрузка…
Отмена
Сохранить