|
1 | | -async function requestSensorPermission() { |
2 | | - if (typeof DeviceMotionEvent?.requestPermission === 'function') { |
3 | | - try { |
4 | | - const response = await DeviceMotionEvent.requestPermission(); |
5 | | - if (response !== 'granted') { |
6 | | - alert('Permission denied for accelerometer'); |
7 | | - return false; |
8 | | - } |
9 | | - } catch (e) { |
10 | | - alert('Error requesting motion permission'); |
11 | | - return false; |
12 | | - } |
13 | | - } |
14 | | - |
15 | | - if (typeof DeviceOrientationEvent?.requestPermission === 'function') { |
16 | | - try { |
17 | | - const response = await DeviceOrientationEvent.requestPermission(); |
18 | | - if (response !== 'granted') { |
19 | | - alert('Permission denied for orientation'); |
20 | | - return false; |
21 | | - } |
22 | | - } catch (e) { |
23 | | - alert('Error requesting orientation permission'); |
24 | | - return false; |
25 | | - } |
| 1 | +function handleMotion(event) { |
| 2 | + const acc = event.accelerationIncludingGravity; |
| 3 | + if (acc) { |
| 4 | + document.getElementById('acc-x').textContent = acc.x?.toFixed(2) ?? '0'; |
| 5 | + document.getElementById('acc-y').textContent = acc.y?.toFixed(2) ?? '0'; |
| 6 | + document.getElementById('acc-z').textContent = acc.z?.toFixed(2) ?? '0'; |
26 | 7 | } |
27 | | - |
28 | | - return true; |
29 | 8 | } |
30 | 9 |
|
31 | | -async function initSensors() { |
32 | | - const granted = await requestSensorPermission(); |
33 | | - if (!granted) return; |
34 | | - |
35 | | - window.addEventListener('devicemotion', (event) => { |
36 | | - document.getElementById('acc-x').textContent = event.accelerationIncludingGravity.x?.toFixed(2) ?? '0'; |
37 | | - document.getElementById('acc-y').textContent = event.accelerationIncludingGravity.y?.toFixed(2) ?? '0'; |
38 | | - document.getElementById('acc-z').textContent = event.accelerationIncludingGravity.z?.toFixed(2) ?? '0'; |
39 | | - }); |
| 10 | +function handleOrientation(event) { |
| 11 | + document.getElementById('ori-alpha').textContent = event.alpha?.toFixed(2) ?? '0'; |
| 12 | + document.getElementById('ori-beta').textContent = event.beta?.toFixed(2) ?? '0'; |
| 13 | + document.getElementById('ori-gamma').textContent = event.gamma?.toFixed(2) ?? '0'; |
| 14 | +} |
40 | 15 |
|
41 | | - window.addEventListener('deviceorientation', (event) => { |
42 | | - document.getElementById('ori-alpha').textContent = event.alpha?.toFixed(2) ?? '0'; |
43 | | - document.getElementById('ori-beta').textContent = event.beta?.toFixed(2) ?? '0'; |
44 | | - document.getElementById('ori-gamma').textContent = event.gamma?.toFixed(2) ?? '0'; |
45 | | - }); |
| 16 | +function initSensorListeners() { |
| 17 | + // Some browsers (iOS 13+, maybe future Android) need permission |
| 18 | + if (typeof DeviceMotionEvent?.requestPermission === 'function') { |
| 19 | + DeviceMotionEvent.requestPermission() |
| 20 | + .then(permissionState => { |
| 21 | + if (permissionState === 'granted') { |
| 22 | + window.addEventListener('devicemotion', handleMotion); |
| 23 | + window.addEventListener('deviceorientation', handleOrientation); |
| 24 | + } else { |
| 25 | + alert("Sensor permission denied."); |
| 26 | + } |
| 27 | + }) |
| 28 | + .catch(console.error); |
| 29 | + } else { |
| 30 | + // No explicit permission needed (e.g., Android Chrome) |
| 31 | + window.addEventListener('devicemotion', handleMotion); |
| 32 | + window.addEventListener('deviceorientation', handleOrientation); |
| 33 | + } |
46 | 34 | } |
47 | 35 |
|
48 | | -initSensors(); |
| 36 | +// Run on load |
| 37 | +window.addEventListener('load', initSensorListeners); |
0 commit comments