|
@@ -0,0 +1,173 @@
|
|
1
|
+/**
|
|
2
|
+ * encantar.js pointer demo
|
|
3
|
+ * @license LGPL-3.0-or-later
|
|
4
|
+ * @author Alexandre Martins <alemartf(at)gmail.com> (https://github.com/alemart/encantar-js)
|
|
5
|
+ */
|
|
6
|
+
|
|
7
|
+(function() {
|
|
8
|
+
|
|
9
|
+let displayMessage = true;
|
|
10
|
+
|
|
11
|
+const POINTER_RADIUS = 50;
|
|
12
|
+const POINTER_COLOR = 'red';
|
|
13
|
+const PRIMARY_POINTER_COLOR = 'lime';
|
|
14
|
+const BACKGROUND_COLOR = 'antiquewhite';
|
|
15
|
+const TEXT_COLOR = 'black';
|
|
16
|
+const TEXT_FONT = '36px sans-serif';
|
|
17
|
+const TEXT_LARGE_FONT = '96px sans-serif';
|
|
18
|
+const TEXT_LINE_HEIGHT = 40;
|
|
19
|
+const TWO_PI = 2.0 * Math.PI;
|
|
20
|
+
|
|
21
|
+/**
|
|
22
|
+ * Render the virtual scene
|
|
23
|
+ * @param {TrackablePointer[]} pointers
|
|
24
|
+ * @param {Viewport} viewport
|
|
25
|
+ * @returns {void}
|
|
26
|
+ */
|
|
27
|
+function render(pointers, viewport)
|
|
28
|
+{
|
|
29
|
+ /*
|
|
30
|
+
|
|
31
|
+ Check out the API reference to see the various interesting properties of a
|
|
32
|
+ TrackablePointer! They are useful for creating all sorts of interactive
|
|
33
|
+ experiences!
|
|
34
|
+
|
|
35
|
+ */
|
|
36
|
+
|
|
37
|
+ // get the canvas context
|
|
38
|
+ const canvas = viewport.canvas;
|
|
39
|
+ const ctx = canvas.getContext('2d');
|
|
40
|
+
|
|
41
|
+ if(!ctx)
|
|
42
|
+ return;
|
|
43
|
+
|
|
44
|
+ ctx.textAlign = 'center';
|
|
45
|
+
|
|
46
|
+ // draw the background
|
|
47
|
+ ctx.fillStyle = BACKGROUND_COLOR;
|
|
48
|
+ ctx.fillRect(0, 0, canvas.width, canvas.height);
|
|
49
|
+
|
|
50
|
+ // display a message
|
|
51
|
+ if(displayMessage) {
|
|
52
|
+ ctx.fillStyle = TEXT_COLOR;
|
|
53
|
+ ctx.font = TEXT_LARGE_FONT;
|
|
54
|
+ ctx.fillText('Tap to begin', canvas.width / 2, canvas.height / 2);
|
|
55
|
+
|
|
56
|
+ if(pointers.length > 0)
|
|
57
|
+ displayMessage = false;
|
|
58
|
+ }
|
|
59
|
+
|
|
60
|
+ // render the pointers
|
|
61
|
+ for(const pointer of pointers) {
|
|
62
|
+ // pointer.position is given in normalized units [-1,1]x[-1,1]
|
|
63
|
+ // we convert it to pixel coordinates (canvas space)
|
|
64
|
+ const position = viewport.convertToPixels(pointer.position);
|
|
65
|
+
|
|
66
|
+ ctx.fillStyle = pointer.isPrimary ? PRIMARY_POINTER_COLOR : POINTER_COLOR;
|
|
67
|
+ ctx.beginPath();
|
|
68
|
+ ctx.arc(position.x, position.y, POINTER_RADIUS, 0, TWO_PI);
|
|
69
|
+ ctx.fill();
|
|
70
|
+ }
|
|
71
|
+
|
|
72
|
+ // render the texts above the pointers
|
|
73
|
+ ctx.fillStyle = TEXT_COLOR;
|
|
74
|
+ ctx.font = TEXT_FONT;
|
|
75
|
+ for(const pointer of pointers) {
|
|
76
|
+ const position = viewport.convertToPixels(pointer.position);
|
|
77
|
+ position.y -= POINTER_RADIUS + 6 * TEXT_LINE_HEIGHT;
|
|
78
|
+
|
|
79
|
+ ctx.fillText('id: ' + pointer.id, position.x, position.y + 0 * TEXT_LINE_HEIGHT);
|
|
80
|
+ ctx.fillText(pointer.phase, position.x, position.y + 1 * TEXT_LINE_HEIGHT);
|
|
81
|
+ ctx.fillText('x: ' + pointer.position.x.toFixed(5), position.x, position.y + 2 * TEXT_LINE_HEIGHT);
|
|
82
|
+ ctx.fillText('y: ' + pointer.position.y.toFixed(5), position.x, position.y + 3 * TEXT_LINE_HEIGHT);
|
|
83
|
+ ctx.fillText('speed: ' + pointer.velocity.length().toFixed(5), position.x, position.y + 4 * TEXT_LINE_HEIGHT);
|
|
84
|
+ ctx.fillText('time: ' + pointer.elapsedTime.toFixed(5), position.x, position.y + 5 * TEXT_LINE_HEIGHT);
|
|
85
|
+ }
|
|
86
|
+}
|
|
87
|
+
|
|
88
|
+/**
|
|
89
|
+ * Start the AR session
|
|
90
|
+ * @returns {Promise<Session>}
|
|
91
|
+ */
|
|
92
|
+async function startARSession()
|
|
93
|
+{
|
|
94
|
+ if(!AR.isSupported()) {
|
|
95
|
+ throw new Error(
|
|
96
|
+ 'This device is not compatible with this AR experience.\n\n' +
|
|
97
|
+ 'User agent: ' + navigator.userAgent
|
|
98
|
+ );
|
|
99
|
+ }
|
|
100
|
+
|
|
101
|
+ const viewport = AR.Viewport({
|
|
102
|
+ resolution: '720p',
|
|
103
|
+ container: document.getElementById('ar-viewport'),
|
|
104
|
+ hudContainer: document.getElementById('ar-hud')
|
|
105
|
+ });
|
|
106
|
+
|
|
107
|
+ const tracker = AR.Tracker.Pointer();
|
|
108
|
+ const source = AR.Source.Pointer();
|
|
109
|
+
|
|
110
|
+ const session = await AR.startSession({
|
|
111
|
+ mode: 'immersive',
|
|
112
|
+ viewport: viewport,
|
|
113
|
+ trackers: [ tracker ],
|
|
114
|
+ sources: [ source ],
|
|
115
|
+ stats: true,
|
|
116
|
+ });
|
|
117
|
+
|
|
118
|
+ return session;
|
|
119
|
+}
|
|
120
|
+
|
|
121
|
+/**
|
|
122
|
+ * Animation loop
|
|
123
|
+ * @param {number} time
|
|
124
|
+ * @param {Frame} frame
|
|
125
|
+ * @returns {void}
|
|
126
|
+ */
|
|
127
|
+function animate(time, frame)
|
|
128
|
+{
|
|
129
|
+ const session = frame.session;
|
|
130
|
+ const pointers = read(frame);
|
|
131
|
+
|
|
132
|
+ render(pointers, session.viewport);
|
|
133
|
+
|
|
134
|
+ session.requestAnimationFrame(animate);
|
|
135
|
+}
|
|
136
|
+
|
|
137
|
+/**
|
|
138
|
+ * Read the results of the tracker
|
|
139
|
+ * @param {Frame} frame
|
|
140
|
+ * @returns {TrackablePointer[]}
|
|
141
|
+ */
|
|
142
|
+function read(frame)
|
|
143
|
+{
|
|
144
|
+ for(const result of frame.results) {
|
|
145
|
+ if(result.tracker.type == 'pointer-tracker') {
|
|
146
|
+ const pointers = result.trackables;
|
|
147
|
+ return pointers;
|
|
148
|
+ }
|
|
149
|
+ }
|
|
150
|
+
|
|
151
|
+ return [];
|
|
152
|
+}
|
|
153
|
+
|
|
154
|
+/**
|
|
155
|
+ * Start the demo
|
|
156
|
+ * @returns {void}
|
|
157
|
+ */
|
|
158
|
+function main()
|
|
159
|
+{
|
|
160
|
+ startARSession().then(session => {
|
|
161
|
+
|
|
162
|
+ session.requestAnimationFrame(animate); // start the animation loop
|
|
163
|
+
|
|
164
|
+ }).catch(error => {
|
|
165
|
+
|
|
166
|
+ alert(error.message);
|
|
167
|
+
|
|
168
|
+ });
|
|
169
|
+}
|
|
170
|
+
|
|
171
|
+document.addEventListener('DOMContentLoaded', main);
|
|
172
|
+
|
|
173
|
+})();
|