-
Notifications
You must be signed in to change notification settings - Fork 0
/
device_orientation.html
265 lines (218 loc) · 7 KB
/
device_orientation.html
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<html>
<head>
<title>Testing device orientation</title>
<style>
.orientation-value {
border: 1px solid black;
width: 250px;
height: 20px;
margin-bottom: 10px;
}
.orientation-output {
border: 1px solid black;
width: 1024px;
height: 256px;
margin-bottom: 10px;
}
.acceleration-value {
border: 1px solid black;
width: 150px;
height: 20px;
margin-bottom: 10px;
}
</style>
</head>
<body>
<h1>Orientation</h1>
<div>
<div>supported:</div>
<div id="orientation_supported" class="orientation-value">...</div>
</div>
<div>
<div>alpha:</div>
<div id="orientation_alpha" class="orientation-value"> </div>
</div>
<div>
<div>beta:</div>
<div id="orientation_beta" class="orientation-value"> </div>
</div>
<div>
<div>gamma:</div>
<div id="orientation_gamma" class="orientation-value"> </div>
</div>
<div>
<div>delta:</div>
<div id="orientation_delta" class="orientation-value"> </div>
</div>
<div>
<div>output</div>
<div id="orientation_output" class="orientation-output"> </div>
</div>
<h1>Acceleration</h1>
<div>
<div>supported:</div>
<div id="motion_supported" class="acceleration-value">...</div>
</div>
<div>
<div>accel:</div>
<div id="acceleration_x" class="acceleration-value"> </div>
<div id="acceleration_y" class="acceleration-value"> </div>
<div id="acceleration_z" class="acceleration-value"> </div>
<div id="acceleration_delta" class="acceleration-value"> </div>
</div>
<div>
<div>rotation:</div>
<div id="rotation_alpha" class="acceleration-value"> </div>
<div id="rotation_beta" class="acceleration-value"> </div>
<div id="rotation_gamma" class="acceleration-value"> </div>
<div id="rotation_delta" class="acceleration-value"> </div>
</div>
<div>
<div>interval:</div>
<div id="interval" class="acceleration-value"> </div>
</div>
<script type="text/javascript">
var field_orientation_alpha = document.getElementById("orientation_alpha");
var field_orientation_beta = document.getElementById("orientation_beta");
var field_orientation_gamma = document.getElementById("orientation_gamma");
var field_orientation_delta = document.getElementById("orientation_delta");
var field_orientation_output = document.getElementById("orientation_output");
var field_accel_x = document.getElementById("acceleration_x");
var field_accel_y = document.getElementById("acceleration_y");
var field_accel_z = document.getElementById("acceleration_z");
var field_accel_delta = document.getElementById("acceleration_delta");
var field_rot_alpha = document.getElementById("rotation_alpha");
var field_rot_beta = document.getElementById("rotation_beta");
var field_rot_gamma = document.getElementById("rotation_gamma");
var field_rot_delta = document.getElementById("rotation_delta");
var field_interval = document.getElementById("interval");
var startTime = new Date().getTime();
// To represent 359.99 possible degrees, we need 16 bits.
var rotationBitmask = 0xFF;
function eventToBytes(orientation, time) {
var alpha = orientation.a * 100 | 0;
var beta = orientation.b * 100 | 0;
var gamma = orientation.g * 100 | 0;
var timeByte0 = time % 256;
var timeByte1 = (time / 256) % 256;
var timeByte2 = (time / 65536) % 256;
var timeByte3 = (time / 16777216) % 256;
return btoa(String.fromCharCode(
alpha & rotationBitmask,
alpha >> 8 & rotationBitmask,
beta & rotationBitmask,
beta >> 8 & rotationBitmask,
gamma & rotationBitmask,
gamma >> 8 & rotationBitmask,
timeByte0,
timeByte1,
timeByte2));
// timeByte2,
// timeByte3));
}
if (window.DeviceOrientationEvent) {
var lastOrientation = {
'a':0,
'b':0,
'g':0,
't':0
};
var orientation_delta = 0;
var gotOrientation = false;
document.getElementById("orientation_supported").innerText = "true";
window.addEventListener("deviceorientation", function(orientation) {
var time = new Date().getTime() - startTime;
//fastest way to truncate to two decimal places. see http://jsperf.com/testing-user-metrics-gathering/2
var alpha = orientation.alpha * 100 | 0;
var beta = orientation.beta * 100 | 0;
var gamma = orientation.gamma * 100 | 0;
if (gotOrientation) {
orientation_delta = Math.max(
Math.abs(alpha - lastOrientation.a),
Math.abs(beta - lastOrientation.b),
Math.abs(gamma - lastOrientation.g),
orientation_delta);
}
//keep track of the last orientation for delta calculation.
lastOrientation = {
'a': alpha,
'b': beta,
'g': gamma,
't': time
};
gotOrientation = true;
field_orientation_output.innerText += eventToBytes(orientation, time);
field_orientation_alpha.innerText = orientation.alpha;
field_orientation_beta.innerText = orientation.beta;
field_orientation_gamma.innerText = orientation.gamma;
field_orientation_delta.innerText = orientation_delta;
});
} else {
document.getElementById("orientation_supported").innerText = "false";
}
if (window.DeviceMotionEvent) {
var last_accel = {
'x': 0,
'y': 0,
'z': 0
};
var accel_delta = 0;
var last_rotation = {
'a': 0,
'b': 0,
'g': 0
};
var rot_delta = 0;
var hasMoved = false;
document.getElementById("motion_supported").innerText = "true";
window.addEventListener("devicemotion", function(eventData) {
var accel = eventData.accelerationIncludingGravity;
var rot = eventData.rotationRate;
var interval = eventData.interval;
var x = accel.x * 100 | 0;
var y = accel.y * 100 | 0;
var z = accel.z * 100 | 0;
var alpha = rot.alpha * 100 | 0;
var beta = rot.beta * 100 | 0;
var gamma = rot.gamma * 100 | 0;
if (hasMoved) {
accel_delta = Math.max(
Math.abs(x - last_accel.x),
Math.abs(y - last_accel.y),
Math.abs(z - last_accel.z),
accel_delta);
}
if (hasMoved) {
rot_delta = Math.max(
Math.abs(alpha - last_rotation.a),
Math.abs(beta - last_rotation.b),
Math.abs(gamma - last_rotation.g),
rot_delta);
}
last_accel = {
'x': x,
'y': y,
'z': z
};
last_rotation = {
'a': alpha,
'b': beta,
'g': gamma
};
hasMoved = true;
field_accel_x.innerText = accel.x;
field_accel_y.innerText = accel.y;
field_accel_z.innerText = accel.z;
field_accel_delta.innerText = accel_delta;
field_rot_alpha.innerText = rot.alpha;
field_rot_beta.innerText = rot.beta;
field_rot_gamma.innerText = rot.gamma;
field_rot_delta.innerText = rot_delta;
field_interval.innerText = interval;
});
} else {
document.getElementById("motion_supported").innerText = "false";
}
</script>
</body>
</html>