-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestRotation.html
More file actions
95 lines (86 loc) · 3.81 KB
/
testRotation.html
File metadata and controls
95 lines (86 loc) · 3.81 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Device Orientation Test</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
margin-top: 50px;
}
.value {
font-size: 2em;
margin: 10px;
}
#permission-button {
display: none;
padding: 10px 20px;
font-size: 1.2em;
margin-top: 20px;
}
</style>
</head>
<body>
<h1>Device Orientation</h1>
<p>Rotation around each axis (in degrees):</p>
<div class="value">Alpha (Z axis): <span id="alpha">0</span>°</div>
<div class="value">Beta (X axis): <span id="beta">0</span>°</div>
<div class="value">Gamma (Y axis): <span id="gamma">0</span>°</div>
<button id="permission-button">Enable Device Orientation</button>
<script>
let alpha = 0, beta = 0, gamma = 0;
let alphaDisplay = document.getElementById('alpha');
let betaDisplay = document.getElementById('beta');
let gammaDisplay = document.getElementById('gamma');
let smoothingFactor = 0.1; // How fast the values return to zero when not moving
// Function to handle device orientation and update the UI
function handleOrientation(event) {
alpha += (event.alpha - alpha) * smoothingFactor;
beta += (event.beta - beta) * smoothingFactor;
gamma += (event.gamma - gamma) * smoothingFactor;
alphaDisplay.textContent = alpha.toFixed(1);
betaDisplay.textContent = beta.toFixed(1);
gammaDisplay.textContent = gamma.toFixed(1);
}
// Request permission for iOS devices
function requestDeviceOrientationPermission() {
if (typeof DeviceOrientationEvent !== 'undefined' && typeof DeviceOrientationEvent.requestPermission === 'function') {
DeviceOrientationEvent.requestPermission()
.then(function (response) {
if (response === 'granted') {
window.addEventListener('deviceorientation', handleOrientation);
document.getElementById('permission-button').style.display = 'none';
} else {
alert('Permission not granted');
}
})
.catch(console.error);
} else {
// If not iOS, just add the listener directly
window.addEventListener('deviceorientation', handleOrientation);
}
}
// Check if permission is needed for iOS and display the button if necessary
if (typeof DeviceOrientationEvent !== 'undefined' && typeof DeviceOrientationEvent.requestPermission === 'function') {
document.getElementById('permission-button').style.display = 'block';
document.getElementById('permission-button').addEventListener('click', requestDeviceOrientationPermission);
} else {
// No need for permission, start the listener right away
window.addEventListener('deviceorientation', handleOrientation);
}
// Function to slowly bring the angles back to zero when the device is not moving
function resetToZero() {
alpha += (0 - alpha) * smoothingFactor;
beta += (0 - beta) * smoothingFactor;
gamma += (0 - gamma) * smoothingFactor;
alphaDisplay.textContent = alpha.toFixed(1);
betaDisplay.textContent = beta.toFixed(1);
gammaDisplay.textContent = gamma.toFixed(1);
}
// Call the resetToZero function periodically
// setInterval(resetToZero, 50); // Adjust the interval as needed
</script>
</body>
</html>