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
|
// Usage:
//
// var control = new MX.RotationControl()
// control.init( object{MX.Object3D} [, listener{HTMLElement}] )
//
// In animation loop:
//
// control.update()
//
// The above code will register handler functions on `listener`
// and will be updating `object`s rotationX and rotationY
// If no `listener` is provided, will default to `object`s el.
MX.GyroControl = function () {
var object,
locked = false
var down = false,
active = false,
lastX,
lastY
var pub = {
sensitivity : .5,
ease : 10,
drag : true,
inverseX : false,
inverseY : false,
disableX : false,
disableY : false,
rotationX : 0,
rotationY : 0,
offsetX : 0,
offsetY : 0,
upperBoundX : undefined,
lowerBoundX : undefined,
upperBoundY : undefined,
lowerBoundY : undefined,
usePreset: function (name) {
var ops = presets[name]
if (ops) {
if (currentPreset && presets[currentPreset].teardown) {
presets[currentPreset].teardown()
}
for (var op in ops) {
if (op !== 'setup' && op !== 'teardown') {
pub[op] = ops[op]
}
}
if (op.setup) ops.setup()
}
}
}
var currentPreset
var presets = {
firstPerson: {
drag: false,
ease: 2,
sensitivity: .18,
inverseX: true,
inverseY: true,
upperBoundX: MX.rotationUnit === 'deg' ? 90 : Math.PI / 2,
lowerBoundX: MX.rotationUnit === 'deg' ? -90 : -Math.PI / 2
},
skybox: {
sensitivity: .18,
inverseX: true,
inverseY: true,
upperBoundX: MX.rotationUnit === 'deg' ? 90 : Math.PI / 2,
lowerBoundX: MX.rotationUnit === 'deg' ? -90 : -Math.PI / 2
}
}
function init (obj, lis) {
if (active) return
object = obj
pub.rotationX = object.rotationX
pub.rotationY = object.rotationY
if (lis instanceof HTMLElement) {
listener = lis
} else if (lis instanceof MX.Object3D) {
listener = lis.el
} else {
listener = window.document
}
window.addEventListener('deviceorientation', updateTarget, false);
updateTarget({ 'alpha': 0, 'beta': 0, 'gamma': 0 });
active = true
}
function changeObject (obj) {
object = obj
pub.rotationX = object.rotationX
pub.rotationY = object.rotationY
}
function changeListener (lis) {
remove()
active = false
init(object, lis)
}
function remove () {
if (!active) return
window.removeEventListener('deviceorientation', deviceorientation, false);
active = false
}
function updateTarget (e) {
var dy = 360 * ((-e.beta + 90) / 180);
var dx = 360 * (-e.gamma /180);
if (MX.rotationUnit !== 'deg') {
dx = MX.toRad(dx)
dy = MX.toRad(dy)
}
if (!pub.disableX) {
pub.rotationX = dy * pub.sensitivity
if (pub.upperBoundX) pub.rotationX = Math.min(pub.rotationX, pub.upperBoundX)
if (pub.lowerBoundX) pub.rotationX = Math.max(pub.rotationX, pub.lowerBoundX)
}
if (!pub.disableY) {
pub.rotationY = dx * pub.sensitivity
if (pub.upperBoundY) pub.rotationY = Math.min(pub.rotationY, pub.upperBoundY)
if (pub.lowerBoundY) pub.rotationY = Math.max(pub.rotationY, pub.lowerBoundY)
}
pub.rotationX += pub.offsetX
pub.rotationY += pub.offsetY
}
function update () {
if (!object || locked) return
var dx = pub.rotationX - object.rotationX,
dy = pub.rotationY - object.rotationY
if (Math.abs(dx) < 0.0001) {
object.rotationX = pub.rotationX
} else {
object.rotationX += dx / pub.ease
}
if (Math.abs(dy) < 0.0001) {
object.rotationY = pub.rotationY
} else {
object.rotationY += dy / pub.ease
}
}
function lock () {
locked = true
}
function unlock () {
pub.rotationX = object.rotationX
pub.rotationY = object.rotationY
locked = false
}
pub.init = init
pub.remove = remove
pub.update = update
pub.lock = lock
pub.unlock = unlock
pub.changeObject = changeObject
pub.changeListener = changeListener
return pub
}
|