forked from stemkoski/AR.js-examples
/
index.html
340 lines (256 loc) · 10.4 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
<!doctype HTML>
<html>
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
<script src="https://cdn.jsdelivr.net/gh/aframevr/aframe@1.3.0/dist/aframe-master.min.js"></script>
<script src="https://raw.githack.com/AR-js-org/AR.js/master/aframe/build/aframe-ar.js"></script>
<body style="margin: 0px; overflow: hidden;">
<script>
// Global state to track touch events
window.isTouching = false;
// access underlying 3D object this component is attached to
AFRAME.registerComponent('spinner', {
init: function()
{
this.el.object3D.scale.set(1, 1, 1);
this.line = document.querySelector('#line-to-zurich');
this.text = document.querySelector('#irchel-text');
},
tick: function (time, timeDelta)
{
// Only rotate if no touch event is active
if (!window.isTouching) {
this.el.object3D.rotation.y += 0.01
}
const latitude = 47.3769; // Zurich's latitude
const longitude = 8.5417; // Zurich's longitude
let globeRotationY = this.el.object3D.rotation.y;
let scale = this.el.object3D.scale.x;
let endPoint = this.calculateEndPoint(latitude, longitude, globeRotationY);
// Ensure we're updating the line component correctly
this.line.setAttribute('line', `start: 0 ${ scale > 1.5 ? scale/1.5*2.7 : 2.7 } 0; end: ${endPoint.x} ${endPoint.y} ${endPoint.z}; color: red`);
// Ensure we're updating the text component correctly
this.text.setAttribute('position', `0 ${ scale > 1.5 ? scale*2 : 3 } 0`)
},
calculateEndPoint: function(latitude, longitude, globeRotationY) {
const globeScale = this.el.object3D.scale.x;
const radius = globeScale; // Globe's radius adjusted for scale
const globeCenterY = 0.5; // Globe's center Y position
// Convert latitude and longitude to radians
const latRad = Math.PI / 2 - THREE.Math.degToRad(latitude);
const lonRad = THREE.Math.degToRad(longitude) - globeRotationY - 0.302;
// Spherical to Cartesian conversion
const x = radius * Math.sin(latRad) * Math.cos(lonRad);
const y = globeCenterY + radius * Math.cos(latRad);
const z = radius * Math.sin(latRad) * Math.sin(lonRad);
return { x: x, y: y, z: z };
}
});
// Component that detects and emits events for touch gestures
AFRAME.registerComponent("gesture-detector", {
schema: {
element: { default: "" }
},
init: function() {
this.targetElement =
this.data.element && document.querySelector(this.data.element);
if (!this.targetElement) {
this.targetElement = this.el;
}
this.internalState = {
previousState: null
};
this.emitGestureEvent = this.emitGestureEvent.bind(this);
this.targetElement.addEventListener("touchstart", this.emitGestureEvent);
this.targetElement.addEventListener("touchend", this.emitGestureEvent);
this.targetElement.addEventListener("touchmove", this.emitGestureEvent);
},
remove: function() {
this.targetElement.removeEventListener("touchstart", this.emitGestureEvent);
this.targetElement.removeEventListener("touchend", this.emitGestureEvent);
this.targetElement.removeEventListener("touchmove", this.emitGestureEvent);
},
emitGestureEvent(event) {
const currentState = this.getTouchState(event);
const previousState = this.internalState.previousState;
// Custom logic for setting global state
if (event.type === "touchstart") {
window.isTouching = true;
} else if (event.type === "touchend" && event.touches.length === 0) {
window.isTouching = false;
}
const gestureContinues =
previousState &&
currentState &&
currentState.touchCount == previousState.touchCount;
const gestureEnded = previousState && !gestureContinues;
const gestureStarted = currentState && !gestureContinues;
if (gestureEnded) {
const eventName =
this.getEventPrefix(previousState.touchCount) + "fingerend";
this.el.emit(eventName, previousState);
this.internalState.previousState = null;
}
if (gestureStarted) {
currentState.startTime = performance.now();
currentState.startPosition = currentState.position;
currentState.startSpread = currentState.spread;
const eventName = this.getEventPrefix(currentState.touchCount) + "fingerstart";
this.el.emit(eventName, currentState);
this.internalState.previousState = currentState;
}
if (gestureContinues) {
const eventDetail = {
positionChange: {
x: currentState.position.x - previousState.position.x,
y: currentState.position.y - previousState.position.y
}
};
if (currentState.spread) {
eventDetail.spreadChange = currentState.spread - previousState.spread;
}
// Update state with new data
Object.assign(previousState, currentState);
// Add state data to event detail
Object.assign(eventDetail, previousState);
const eventName =
this.getEventPrefix(currentState.touchCount) + "fingermove";
this.el.emit(eventName, eventDetail);
}
},
getTouchState: function(event) {
if (event.touches.length === 0) {
return null;
}
// Convert event.touches to an array so we can use reduce
const touchList = [];
for (let i = 0; i < event.touches.length; i++) {
touchList.push(event.touches[i]);
}
const touchState = {
touchCount: touchList.length
};
// Calculate center of all current touches
const centerPositionRawX =
touchList.reduce((sum, touch) => sum + touch.clientX, 0) /
touchList.length;
const centerPositionRawY =
touchList.reduce((sum, touch) => sum + touch.clientY, 0) /
touchList.length;
touchState.positionRaw = { x: centerPositionRawX, y: centerPositionRawY };
// Scale touch position and spread by average of window dimensions
const screenScale = 2 / (window.innerWidth + window.innerHeight);
touchState.position = {
x: centerPositionRawX * screenScale,
y: centerPositionRawY * screenScale
};
// Calculate average spread of touches from the center point
if (touchList.length >= 2) {
const spread =
touchList.reduce((sum, touch) => {
return (
sum +
Math.sqrt(
Math.pow(centerPositionRawX - touch.clientX, 2) +
Math.pow(centerPositionRawY - touch.clientY, 2)
)
);
}, 0) / touchList.length;
touchState.spread = spread * screenScale;
}
return touchState;
},
getEventPrefix(touchCount) {
const numberNames = ["one", "two", "three", "many"];
return numberNames[Math.min(touchCount, 4) - 1];
}
});
AFRAME.registerComponent("gesture-handler", {
schema: {
enabled: { default: true },
rotationFactor: { default: 5 },
minScale: { default: 0.3 },
maxScale: { default: 3 },
},
init: function () {
this.handleScale = this.handleScale.bind(this);
this.handleRotation = this.handleRotation.bind(this);
this.isVisible = false;
this.initialScale = this.el.object3D.scale.clone();
this.scaleFactor = 1.5;
this.el.sceneEl.addEventListener("markerFound", (e) => {
this.isVisible = true;
});
this.el.sceneEl.addEventListener("markerLost", (e) => {
this.isVisible = false;
});
},
update: function () {
if (this.data.enabled) {
this.el.sceneEl.addEventListener("onefingermove", this.handleRotation);
this.el.sceneEl.addEventListener("twofingermove", this.handleScale);
} else {
this.el.sceneEl.removeEventListener("onefingermove", this.handleRotation);
this.el.sceneEl.removeEventListener("twofingermove", this.handleScale);
}
},
remove: function () {
this.el.sceneEl.removeEventListener("onefingermove", this.handleRotation);
},
handleRotation: function (event) {
if (this.isVisible) {
this.el.object3D.rotation.y +=
event.detail.positionChange.x * this.data.rotationFactor;
// this.el.object3D.rotation.x +=
// event.detail.positionChange.y * this.data.rotationFactor;
}
},
handleScale: function (event) {
if (this.isVisible) {
this.scaleFactor *= 1 + event.detail.spreadChange / event.detail.startSpread;
this.scaleFactor = Math.min(
Math.max(this.scaleFactor, this.data.minScale),
this.data.maxScale
);
this.el.object3D.scale.x = this.scaleFactor * this.initialScale.x;
this.el.object3D.scale.y = this.scaleFactor * this.initialScale.y;
this.el.object3D.scale.z = this.scaleFactor * this.initialScale.z;
}
},
});
</script>
<a-scene embedded vr-mode-ui="enabled: false;" renderer="logarithmicDepthBuffer: true"arjs='sourceType: webcam; debugUIEnabled: false; detectionMode: mono_and_matrix; matrixCodeType: 3x3;' gesture-detector>
<a-assets>
<img id="earth-sphere" src="images/earth-sphere.jpg" />
</a-assets>
<a-marker
type="barcode"
value="6"
raycaster="objects: .clickable"
emitevents="true"
cursor="fuse: false; rayOrigin: mouse;"
>
<!-- Adding Irchel Campus text in red -->
<a-text value="Campus Irchel"
id="irchel-text"
color="red"
position="0 3 0"
align="center"
width="6"
zOffset="-5">
</a-text>
<a-sphere
id="earth"
position="0 0.5 0"
rotation="0 100 0"
material="src: #earth-sphere; transparent: true; opacity: 0.95;"
class="clickable"
gesture-handler
spinner>
</a-sphere>
<!-- Drawing a red line pointing to Zurich -->
<a-entity id="line-to-zurich" line="start: 0 2.7 0; end: 1.004, 1.604, -0.151; color: red"></a-entity>
</a-marker>
<a-entity camera></a-entity>
</a-scene>
</body>
</html>