소스 검색

Introduce Viewport.convertToPixels()

customisations
alemart 10 달 전
부모
커밋
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,7 +4,7 @@ A [trackable](trackable.md) that represents a pointer tracked by a [PointerTrack
4 4
 
5 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 9
 *Since:* 0.4.0
10 10
 
@@ -32,7 +32,7 @@ The phase of the pointer. It's one of the following strings:
32 32
 
33 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 37
 ### initialPosition
38 38
 

+ 19
- 3
docs/api/viewport.md 파일 보기

@@ -63,7 +63,7 @@ The size in pixels that matches the [resolution](#resolution) of the virtual sce
63 63
 
64 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 68
 ### style
69 69
 
@@ -146,6 +146,22 @@ Exit fullscreen mode.
146 146
 
147 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 165
 ## Events
150 166
 
151 167
 A viewport is an [AREventTarget](ar-event-target.md). You can listen to the following events:
@@ -164,7 +180,7 @@ viewport.addEventListener('resize', () => {
164 180
 
165 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 185
 *Since:* 0.3.0
170 186
 
@@ -173,7 +189,7 @@ The viewport has been switched into or out of fullscreen mode.
173 189
 ```js
174 190
 viewport.addEventListener('fullscreenchange', () => {
175 191
     if(viewport.fullscreen)
176
-        console.log('Switched into fullscreen mode');
192
+        console.log('Switched to fullscreen mode');
177 193
     else
178 194
         console.log('Switched out of fullscreen mode');
179 195
 });

+ 21
- 3
src/core/viewport.ts 파일 보기

@@ -24,12 +24,13 @@ import AR from '../main';
24 24
 import Speedy from 'speedy-vision';
25 25
 import { SpeedySize } from 'speedy-vision/types/core/speedy-size';
26 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 27
 import { SessionMode } from './session';
31 28
 import { HUD, HUDContainer } from './hud';
32 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 34
 import { AREvent, AREventTarget, AREventListener } from '../utils/ar-events';
34 35
 import { IllegalArgumentError, IllegalOperationError, NotSupportedError, AccessDeniedError } from '../utils/errors';
35 36
 
@@ -979,6 +980,23 @@ export class Viewport extends ViewportEventTarget
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 1000
      * Initialize the viewport (when the session starts)
983 1001
      * @param getMediaSize
984 1002
      * @param sessionMode

Loading…
취소
저장