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

Introduce Viewport.convertToPixels()

customisations
alemart 11 месяцев назад
Родитель
Сommit
bbd653866a
3 измененных файлов: 42 добавлений и 8 удалений
  1. 2
    2
      docs/api/trackable-pointer.md
  2. 19
    3
      docs/api/viewport.md
  3. 21
    3
      src/core/viewport.ts

+ 2
- 2
docs/api/trackable-pointer.md Просмотреть файл

4
 
4
 
5
 A pointer is an abstraction that represents an instance of user input that targets one or more coordinates on a screen. For example, each point of contact between fingers and a multitouch screen generate a pointer. Devices such as a mouse and a pen/stylus also generate pointers.
5
 A pointer is an abstraction that represents an instance of user input that targets one or more coordinates on a screen. For example, each point of contact between fingers and a multitouch screen generate a pointer. Devices such as a mouse and a pen/stylus also generate pointers.
6
 
6
 
7
-Pointers are positioned in the [viewport](viewport.md). Their positions are given in normalized units, which range from -1 to +1. The center of the viewport is at (0,0). The top-right corner is at (1,1). The bottom-left corner is at (-1,-1).
7
+Pointers are positioned in the [viewport](viewport.md). Their positions are given in normalized units, which range from -1 to +1. The center of the viewport is at (0,0). The top right corner is at (1,1). The bottom left corner is at (-1,-1).
8
 
8
 
9
 *Since:* 0.4.0
9
 *Since:* 0.4.0
10
 
10
 
32
 
32
 
33
 `pointer.position: Vector2, read-only`
33
 `pointer.position: Vector2, read-only`
34
 
34
 
35
-The current position of the pointer, given in normalized units.
35
+The current position of the pointer, given in normalized units. See also: [Viewport.convertToPixels](viewport.md#converttopixels).
36
 
36
 
37
 ### initialPosition
37
 ### initialPosition
38
 
38
 

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

63
 
63
 
64
 `viewport.canvas: HTMLCanvasElement, read-only`
64
 `viewport.canvas: HTMLCanvasElement, read-only`
65
 
65
 
66
-A `<canvas>` on which the virtual scene is drawn.
66
+The `<canvas>` on which the virtual scene is drawn.
67
 
67
 
68
 ### style
68
 ### style
69
 
69
 
146
 
146
 
147
 A promise that is resolved once the fullscreen mode is no longer active, or rejected on error. The promise will be rejected if the method is called when not in fullscreen mode.
147
 A promise that is resolved once the fullscreen mode is no longer active, or rejected on error. The promise will be rejected if the method is called when not in fullscreen mode.
148
 
148
 
149
+### convertToPixels
150
+
151
+`viewport.convertToPixels(position: Vector2): Vector2`
152
+
153
+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).
154
+
155
+*Since:* 0.4.0
156
+
157
+**Arguments**
158
+
159
+`position: Vector2`. A position in normalized units.
160
+
161
+**Returns**
162
+
163
+An equivalent pixel position in canvas space.
164
+
149
 ## Events
165
 ## Events
150
 
166
 
151
 A viewport is an [AREventTarget](ar-event-target.md). You can listen to the following events:
167
 A viewport is an [AREventTarget](ar-event-target.md). You can listen to the following events:
164
 
180
 
165
 ### fullscreenchange
181
 ### fullscreenchange
166
 
182
 
167
-The viewport has been switched into or out of fullscreen mode.
183
+The viewport has been switched to or out of fullscreen mode.
168
 
184
 
169
 *Since:* 0.3.0
185
 *Since:* 0.3.0
170
 
186
 
173
 ```js
189
 ```js
174
 viewport.addEventListener('fullscreenchange', () => {
190
 viewport.addEventListener('fullscreenchange', () => {
175
     if(viewport.fullscreen)
191
     if(viewport.fullscreen)
176
-        console.log('Switched into fullscreen mode');
192
+        console.log('Switched to fullscreen mode');
177
     else
193
     else
178
         console.log('Switched out of fullscreen mode');
194
         console.log('Switched out of fullscreen mode');
179
 });
195
 });

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

24
 import Speedy from 'speedy-vision';
24
 import Speedy from 'speedy-vision';
25
 import { SpeedySize } from 'speedy-vision/types/core/speedy-size';
25
 import { SpeedySize } from 'speedy-vision/types/core/speedy-size';
26
 import { SpeedyPromise } from 'speedy-vision/types/core/speedy-promise';
26
 import { SpeedyPromise } from 'speedy-vision/types/core/speedy-promise';
27
-import { Nullable } from '../utils/utils';
28
-import { Resolution } from '../utils/resolution';
29
-import { Utils } from '../utils/utils';
30
 import { SessionMode } from './session';
27
 import { SessionMode } from './session';
31
 import { HUD, HUDContainer } from './hud';
28
 import { HUD, HUDContainer } from './hud';
32
 import { FullscreenButton } from '../ui/fullscreen-button';
29
 import { FullscreenButton } from '../ui/fullscreen-button';
30
+import { Vector2 } from '../geometry/vector2';
31
+import { Resolution } from '../utils/resolution';
32
+import { Nullable } from '../utils/utils';
33
+import { Utils } from '../utils/utils';
33
 import { AREvent, AREventTarget, AREventListener } from '../utils/ar-events';
34
 import { AREvent, AREventTarget, AREventListener } from '../utils/ar-events';
34
 import { IllegalArgumentError, IllegalOperationError, NotSupportedError, AccessDeniedError } from '../utils/errors';
35
 import { IllegalArgumentError, IllegalOperationError, NotSupportedError, AccessDeniedError } from '../utils/errors';
35
 
36
 
979
     }
980
     }
980
 
981
 
981
     /**
982
     /**
983
+     * Convert a position given in normalized units to a corresponding pixel
984
+     * position in canvas space. Normalized units range from -1 to +1. The
985
+     * center of the canvas is at (0,0). The top right corner is at (1,1).
986
+     * The bottom left corner is at (-1,-1).
987
+     * @param position in normalized units
988
+     * @returns an equivalent pixel position in canvas space
989
+     */
990
+    convertToPixels(position: Vector2): Vector2
991
+    {
992
+        const canvas = this.canvas;
993
+        const x = 0.5 * (1 + position.x) * canvas.width;
994
+        const y = -0.5 * (1 + position.y) * canvas.height;
995
+
996
+        return new Vector2(x, y);
997
+    }
998
+
999
+    /**
982
      * Initialize the viewport (when the session starts)
1000
      * Initialize the viewport (when the session starts)
983
      * @param getMediaSize
1001
      * @param getMediaSize
984
      * @param sessionMode
1002
      * @param sessionMode

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