-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
executable file
·115 lines (94 loc) · 2.89 KB
/
index.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
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
/* global AFRAME */
if (typeof AFRAME === 'undefined') {
throw new Error('Component attempted to register before AFRAME was available.');
}
/**
* Example component for A-Frame.
*/
AFRAME.registerComponent('touch-rotation-controls', {
/**
* Touch Rotation controls.
*
* Based on: https://github.com/aframevr/aframe/pull/1056
*/
schema: {
enabled: { default: true },
sensitivity: { default: 1 / 25 }
},
init: function () {
this.touchDown = false;
this.lookVector = new THREE.Vector2();
this.bindMethods();
},
play: function () {
this.addEventListeners();
},
pause: function () {
this.removeEventListeners();
this.lookVector.set(0, 0);
},
remove: function () {
this.pause();
},
bindMethods: function () {
this.onTouchStart = this.onTouchStart.bind(this);
this.onTouchMove = this.onTouchMove.bind(this);
this.onTouchEnd = this.onTouchEnd.bind(this);
this.onTouchCancel = this.onTouchCancel.bind(this);
},
addEventListeners: function () {
var sceneEl = this.el.sceneEl;
var canvasEl = sceneEl.canvas;
if (!canvasEl) {
sceneEl.addEventListener('render-target-loaded', this.addEventListeners.bind(this));
return;
}
canvasEl.addEventListener('touchstart', this.onTouchStart, false);
canvasEl.addEventListener('touchmove', this.onTouchMove, false);
canvasEl.addEventListener('touchend', this.onTouchEnd, false);
canvasEl.addEventListener('touchcancel', this.onTouchCancel, false);
},
removeEventListeners: function () {
var canvasEl = this.el.sceneEl && this.el.sceneEl.canvas;
if (canvasEl) {
canvasEl.removeEventListener('touchstart', this.onTouchStart, false);
canvasEl.removeEventListener('touchmove', this.onTouchMove, false);
canvasEl.removeEventListener('touchend', this.onTouchEnd, false);
canvasEl.removeEventListener('touchcancel', this.onTouchCancel, false);
}
},
isRotationActive: function () {
return this.data.enabled && this.touchDown;
},
/**
* Returns the sum the touch movement since last call.
*/
getRotationDelta: function () {
var dRotation = this.lookVector.clone().multiplyScalar(this.data.sensitivity);
this.lookVector.set(0, 0);
return dRotation;
},
onTouchMove: function (event) {
if (!this.data.enabled || !this.touchDown) {
return;
}
var touch = event.touches[0];
var movementX = touch.screenX - this.previousTouchX;
var movementY = touch.screenY - this.previousTouchY;
this.lookVector.x += movementX;
this.lookVector.y += movementY;
this.previousTouchX = touch.screenX;
this.previousTouchY = touch.screenY;
},
onTouchStart: function (event) {
this.touchDown = true;
this.previousTouchX = event.touches[0].x;
this.previousTouchY = event.touches[0].y;
},
onTouchEnd: function () {
this.touchDown = false;
},
onTouchCancel: function () {
this.touchDown = false;
}
});