-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensors.html
126 lines (112 loc) · 2.71 KB
/
sensors.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
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.garden {
position: relative;
width : 400px;
height: 400px;
border: 5px solid #CCC;
border-radius: 10px;
}
.ball {
position: absolute;
top : 90px;
left : 90px;
width : 20px;
height: 20px;
background: green;
border-radius: 100%;
}
</style>
</head>
<body>
<div class="garden">
<div class="ball"></div>
</div>
<textarea id="motion" cols="50" rows="30"></textarea>
<textarea id="orientation" cols="50" rows="30"></textarea>
<script>
function fmt(num, prec = 2) {
if (num) return num.toFixed(prec)
return num
}
function handleMotion(ev) {
// let { accelerationIncludingGravity, acceleration, interval, rotationRate } = ev
let {
accelerationIncludingGravity: {
x: acclGrvX,
y: acclGrvY,
z: acclGrvZ,
},
acceleration: {
x: acclX,
y: acclY,
z: acclZ,
},
interval,
rotationRate: {
alpha,
beta,
gamma,
},
} = ev
let val = {
acclerationIncGrav: {
x: fmt(acclGrvX),
y: fmt(acclGrvY),
z: fmt(acclGrvZ),
},
accleration: {
x: fmt(acclX),
y: fmt(acclY),
z: fmt(acclZ),
},
rotationRate: {
alpha: fmt(alpha),
beta: fmt(beta),
gamma: fmt(gamma),
},
interval,
}
val = JSON.stringify(val, 0, 2)
document.getElementById('motion').value = val
}
function handleOrientation(ev) {
let { alpha, beta, gamma } = ev
let val = {
alpha: fmt(alpha),
beta: fmt(beta),
gamma: fmt(gamma),
}
val = JSON.stringify(val, 0, 2)
document.getElementById('orientation').value = val
}
window.addEventListener('devicemotion', handleMotion);
window.addEventListener('deviceorientation', handleOrientation);
////////////////////////////////
const ball = document.querySelector('.ball');
const garden = document.querySelector('.garden');
const maxX = garden.clientWidth - ball.clientWidth;
const maxY = garden.clientHeight - ball.clientHeight;
window.addEventListener('deviceorientation', (ev) => {
var x = event.beta; // In degree in the range [-180,180]
var y = event.gamma; // In degree in the range [-90,90]
let pitch = ev.beta
let roll = ev.gamma
// Because we don't want to have the device upside down
// We constrain the x value to the range [-90,90]
if (pitch > 90) { pitch = 90};
if (pitch < -90) { pitch = -90};
// To make computation easier we shift the range of
// x and y to [0,180]
pitch += 90;
roll += 90;
// 10 is half the size of the ball
// It center the positioning point to the center of the ball
ball.style.top = (maxY * pitch/180 - 10) + "px";
ball.style.left = (maxX * roll/180 - 10) + "px";
});
</script>
</body>
</html>