-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
96 lines (83 loc) · 4.38 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Motion Sensor App</title>
<link rel="stylesheet" href="global.css">
<script src="/socket.io/socket.io.js"></script>
</head>
<body>
<button id="accelPermsButton" onclick="getIMU()">Get Sensor Permissions</button>
<fieldset>
<p>Select performer:
<select id="performerSelect" name="performerSelect">
<option value="/player1">Player 1</option>
<option value="/player2">Player 2</option>
<option value="/player3">Player 3</option>
<option value="/player4">Player 4</option>
</select>
</p>
</fieldset>
<h1>Device Orientation:</h1>
<p><span id="orientationAlpha"></span></p><br/>
<p><span id="orientationBeta"></span></p><br/>
<p><span id="orientationGamma"></span></p><br/>
<h1>Device Motion Rate:</h1>
<p><span id="motionAlpha"></span></p><br/>
<p><span id="motionBeta"></span></p><br/>
<p><span id="motionGamma"></span></p><br/>
<script>
let updateRate = 1/60;
const socket = io();
let selectedPlayer = io("/player1");
function wrap(inVal, mod) {
inVal %= mod;
return inVal < 0 ? inVal + mod : inVal;
}
function orientationHandler(event) {
document.getElementById('orientationAlpha').textContent = "Alpha: " + wrap(event.alpha + 180, 360).toFixed(2).toString();
document.getElementById('orientationBeta').textContent = "Beta: " + event.beta.toFixed(2).toString();
document.getElementById('orientationGamma').textContent = "Gamma: " + event.gamma.toFixed(2).toString();
selectedPlayer.emit('orient', {alpha: event.alpha, beta: event.beta, gamma: event.gamma});
}
function motionHandler(event) {
let motionAlpha = document.getElementById('motionAlpha');
let motionBeta = document.getElementById('motionBeta');
let motionGamma = document.getElementById('motionGamma');
// if event.rotationRate, use that; else use event.accelerationIncludingGravity
if (event.rotationRate) {
motionAlpha.textContent = "Alpha: " + Math.round(event.rotationRate.alpha).toString();
motionBeta.textContent = "Beta: " + Math.round(event.rotationRate.beta).toString();
motionGamma.textContent = "Gamma: " + Math.round(event.rotationRate.gamma).toString();
selectedPlayer.emit('motion', event.rotationRate);
} else {
motionAlpha.textContent = "X: " + Math.round(event.accelerationIncludingGravity.x).toString();
motionBeta.textContent = "Y: " + Math.round(event.accelerationIncludingGravity.y).toString();
motionGamma.textContent = "Z: " + Math.round(event.accelerationIncludingGravity.z).toString();
selectedPlayer.emit('accel', event.accelerationIncludingGravity);
}
}
function eventListenerUpdate() {
// units in degrees
window.removeEventListener('deviceorientation', event => orientationHandler(event));
window.addEventListener('deviceorientation', event => orientationHandler(event));
// units in m/s^2
window.removeEventListener('devicemotion', event => motionHandler(event));
window.addEventListener('devicemotion', event => motionHandler(event));
}
function getIMU(){
DeviceMotionEvent.requestPermission().then(response => {
if (response == 'granted') {
eventListenerUpdate();
let selector = document.getElementById("performerSelect");
selector.addEventListener('change', event => {
selectedPlayer = io(selector.value);
eventListenerUpdate();
})
}
});
}
</script>
</body>
</html>