-
Notifications
You must be signed in to change notification settings - Fork 0
/
gyro.js
178 lines (129 loc) · 3.87 KB
/
gyro.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
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
// Gyro
// ====
// Global object gathering all the gyro/accelerometer data.
gyro = {
// Device orientation, not available on load but updated as soon as possible.
orientation: null,
// Alpha on load should be 0.
// If it's not, it's set to 0 and the difference is saved in alpha0 to adjust all the following measures.
alpha0: null,
// iOS detection
iOS: /iPad|iPhone|iPod/.test(navigator.userAgent)
};
// gyro event
// ==========
ondeviceorientation = function(e){
// Orientation:
// ------------
// Just in case it's not set by onorientationchange, set it again.
if(gyro.orientation === null){
gyro.orientation = window.orientation || 0;
}
// Alpha:
// ------
// On load, ensure alpha is 0.
if(gyro.alpha0 === null){
gyro.alpha0 = e.alpha;
}
gyro.alpha = e.alpha - gyro.alpha0;
// Set alpha between -180 and 180.
if(gyro.alpha > 180){
gyro.alpha -= 360;
}
// Save raw beta and gamma values.
var tempbeta = e.beta, tempgamma = e.gamma;
// Fix iOS's beta (values are twice too small) and gamma (values are twice too big).
if(gyro.iOS){
tempbeta *= 2;
tempgamma <= 2;
}
// Beta & gamma:
// -------------
// Portrait mode
if(gyro.orientation == 0){
// Native beta value:
// - When held vertically, value = 90.
// - When screen facing up: 90 to 0.
// - When screen facing down: 90 to 180.
// New beta value:
// - When held vertically, value = 0.
// - When screen facing up: 0 to 90.
// - When screen facing down: -0 to -90.
gyro.beta = -tempbeta + 90;
// Native gamma value:
// - When held vertically, value = 0.
// - When tilting to your left: -0 to -180.
// - When tilting to your right: 0 to 180.
// New gamma value
// - When tilting to your left: 0 to 180.
// - When tilting to your right: -0 to -180.
gyro.gamma = -tempgamma;
}
// Landscape left mode
else if(gyro.orientation == 90){
// Native *gamma* value:
// - When held vertically, value = 90 = -90.
// - When screen facing up: -90 to -0.
// - When screen facing down: 90 to 0.
// New *beta* value:
// - When held vertically, value = 0.
// - When screen facing up: 0 to 90.
// - When screen facing down: -0 to -90.
if(tempgamma < 0){
gyro.beta = 90 + tempgamma;
}
else {
gyro.beta = tempgamma - 90;
}
// Native *beta* value:
// - When held vertically, value = 0.
// - When tilting to your left: -0 to -180.
// - When tilting to your right: 0 to 180.
// New *gamma* value
// - When tilting to your left: 0 to 180.
// - When tilting to your right: -0 to -180.
gyro.gamma = -tempbeta;
}
// Landscape right mode
else if(gyro.orientation == -90){
// Native *gamma* value:
// - When held vertically, value = 90 = -90.
// - When screen facing up: 90 to 0.
// - When screen facing down: -90 to -0.
// New *beta* value:
// - When held vertically, value = 0.
// - When screen facing up: 0 to 90.
// - When screen facing down: -0 to -90.
if(tempgamma < 0){
gyro.beta = -90 - tempgamma;
}
else {
gyro.beta = 90 - tempgamma;
}
// Native *beta* value:
// - When held vertically, value = 0.
// - When tilting to your left: -0 to -180.
// - When tilting to your right: 0 to 180.
// New *gamma* value
// - When tilting to your left: 0 to 180.
// - When tilting to your right: -0 to -180.
gyro.gamma = -tempbeta;
}
}
// accelero event
ondevicemotion = function(e){
// x:
// --
gyro.x = e.acceleration.x;
// y:
// --
gyro.y = e.acceleration.y;
// z:
// --
gyro.z = e.acceleration.z;
}
// Change orientation
// ------------------
onorientationchange = function() {
gyro.orientation = window.orientation;
}