-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmotion.html
More file actions
79 lines (75 loc) · 3.43 KB
/
motion.html
File metadata and controls
79 lines (75 loc) · 3.43 KB
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
<html>
<head>
<style>
body { white-space: pre; }
</style>
<script>
window.addEventListener('DOMContentLoaded', () => {
const output = document.querySelector('#output');
document.querySelector('#request').addEventListener('click', async () => {
if (typeof DeviceMotionEvent?.requestPermission === 'function') {
try {
output.textContent += 'Requesting Motion Permission\n';
const res = await DeviceMotionEvent.requestPermission();
output.textContent += `Response: ${res}\n`;
if (res === 'granted') {
window.addEventListener('devicemotion', (event) => {
const {acceleration, accelerationIncludingGravity, rotationRate, interval } = event;
document.querySelector('#motion_acceleration').textContent = `${JSON.stringify(acceleration)}`;
document.querySelector('#motion_accelerationIncludingGravity').textContent = `${JSON.stringify(accelerationIncludingGravity)}`;
document.querySelector('#motion_rotationRate').textContent = `${JSON.stringify(rotationRate)}`;
document.querySelector('#motion_interval').textContent = interval;
});
}
} catch (error) {
output.textContent += `Error: ${error}\n`;
}
} else {
output.textContent += `Error: DeviceMotionEvent.requestPermission() is not a function!\n`;
}
if (typeof DeviceOrientationEvent?.requestPermission === 'function') {
try {
output.textContent += 'Requesting Orientation Permission\n';
const res = await DeviceOrientationEvent.requestPermission();
output.textContent += `Response: ${res}\n`;
if (res === 'granted') {
window.addEventListener('deviceorientation', (event) => {
const { absolute, alpha, beta, gamma } = event;
document.querySelector('#orient_absolute').textContent = absolute ? 'Yes' : 'No';
document.querySelector('#orient_alpha').textContent = alpha;
document.querySelector('#orient_beta').textContent = beta;
document.querySelector('#orient_gamma').textContent = gamma;
});
}
} catch (error) {
output.textContent += `Error: ${error}\n`;
}
} else {
output.textContent += `Error: DeviceOrientationEvent.requestPermission() is not a function!\n`;
}
});
output.textContent += 'Loaded!\n';
});
</script>
</head>
<body>
<div>
<button type="button" id="request">Request Permission</button>
</div>
<div>
<h3>Motion Event Data</h3>
<b>acceleration:</b> <span id="motion_acceleration"></span><br />
<b>accelerationIncludingGravity</b> <span id="motion_accelerationIncludingGravity"></span><br />
<b>rotationRate</b> <span id="motion_rotationRate"></span><br />
<b>interval</b> <span id="motion_interval"></span>
</div>
<div>
<h3>Orientation Event Data</h3>
<b>absolute:</b> <span id="orient_absolute"></span><br />
<b>alpha</b> <span id="orient_alpha"></span><br />
<b>beta</b> <span id="orient_beta"></span><br />
<b>gamma</b> <span id="orient_gamma"></span>
</div>
<div id="output"></div>
</body>
</html>