-
Notifications
You must be signed in to change notification settings - Fork 97
/
gesture-handler.js
67 lines (56 loc) · 1.99 KB
/
gesture-handler.js
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
/* global AFRAME, THREE */
AFRAME.registerComponent("gesture-handler", {
schema: {
enabled: { default: true },
rotationFactor: { default: 5 },
minScale: { default: 0.3 },
maxScale: { default: 8 },
},
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;
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);
this.el.sceneEl.removeEventListener("twofingermove", this.handleScale);
},
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;
}
},
});