forked from c-frame/aframe-extras
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trackpad-controls.js
84 lines (64 loc) · 1.77 KB
/
trackpad-controls.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* 3dof (Gear VR, Daydream) controls for mobile.
*/
module.exports = AFRAME.registerComponent('trackpad-controls', {
schema: {
enabled: { default: true }
},
init: function () {
this.dVelocity = new THREE.Vector3();
this.zVel = 0;
this.bindMethods();
},
play: function () {
this.addEventListeners();
},
pause: function () {
this.removeEventListeners();
this.dVelocity.set(0, 0, 0);
},
remove: function () {
this.pause();
},
addEventListeners: function () {
const sceneEl = this.el.sceneEl;
sceneEl.addEventListener('axismove', this.onAxisMove);
sceneEl.addEventListener('trackpadtouchstart', this.onTouchStart);
sceneEl.addEventListener('trackpadtouchend', this.onTouchEnd);
},
removeEventListeners: function () {
const sceneEl = this.el.sceneEl;
sceneEl.removeEventListener('axismove', this.onAxisMove);
sceneEl.removeEventListener('trackpadtouchstart', this.onTouchStart);
sceneEl.removeEventListener('trackpadtouchend', this.onTouchEnd);
},
isVelocityActive: function () {
return this.data.enabled && this.isMoving;
},
getVelocityDelta: function () {
this.dVelocity.z = this.isMoving ? -this.zVel : 1;
return this.dVelocity.clone();
},
bindMethods: function () {
this.onTouchStart = this.onTouchStart.bind(this);
this.onTouchEnd = this.onTouchEnd.bind(this);
this.onAxisMove = this.onAxisMove.bind(this);
},
onTouchStart: function (e) {
this.isMoving = true;
e.preventDefault();
},
onTouchEnd: function (e) {
this.isMoving = false;
e.preventDefault();
},
onAxisMove: function(e){
var axis_data = e.detail.axis;
if(axis_data[1] < 0){
this.zVel = 1;
}
if(axis_data[1] > 0){
this.zVel = -1;
}
}
});