-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevicemotion.html
180 lines (139 loc) · 4.85 KB
/
devicemotion.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<!DOCTYPE HTML>
<!--
this file is from https://github.com/nickshin/CheatSheets/
this file contains some HTML5 snippets on:
- devicemotion
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
reference sources:
+ http://www.html5rocks.com/en/tutorials/device/orientation/
DEMO!!!
+ http://www.html5rocks.com/en/tutorials/device/orientation/devicemotionsample.html
notes:
- this is basically a copy of the DEMO above...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
best viewed in editor with tab stops set to 4
NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
-->
<!-- ================================================== -->
<!-- START OF HTML -->
<html>
<head>
<title>DeviceMotion : HTML5 test code</title>
<!-- ================================================== -->
<!-- Cascade Style Sheets {{{ -->
<!-- ================================================== -->
<style>
body {
font-family: sans-serif;
}
.main {
border: 1px solid black;
box-shadow: 10px 10px 5px #888;
border-radius: 12px;
padding: 20px;
background-color: #ddd;
margin: 25px;
width: 450px;
margin-left:auto;
margin-right:auto;
}
.logo {
width:275px;
margin-left: auto;
margin-right: auto;
display: block;
padding: 15px;
}
.container {
-webkit-perspective: 300;
perspective: 300;
}
</style>
<!-- ================================================== -->
<!-- Cascade Style Sheets }}} -->
<!-- Javascript {{{ -->
<!-- ================================================== -->
<!-- .................................................. -->
<!-- DeviceMotionEvent {{{2 -->
<!-- .................................................. -->
<script>
// function init() {
// if (window.DeviceMotionEvent) {
// console.log("DeviceMotionEvent supported");
// } else if ('listenForDeviceMovement' in window) {
// console.log("DeviceMotionEvent supported [listenForDeviceMovement]");
// }
// }
function init2() {
if ((window.DeviceMotionEvent) || ('listenForDeviceMovement' in window)) {
window.addEventListener('devicemotion', deviceMotionHandler3, false);
} else {
document.getElementById("dmEvent").innerHTML = "Not supported on your device or browser. Sorry."
}
}
function deviceMotionHandler3(eventData) {
// Grab the acceleration including gravity from the results
var acceleration = eventData.accelerationIncludingGravity;
// Display the raw acceleration data
var rawAcceleration = "[" + Math.round(acceleration.x) + ", " + Math.round(acceleration.y) + ", " + Math.round(acceleration.z) + "]";
// Z is the acceleration in the Z axis, and tells us if the device is facing up, or down
var facingUp = -1;
if (acceleration.z > 0) {
facingUp = +1;
}
// Convert the value from acceleration to degress
// acceleration.x|y is the acceleration according to gravity, we'll assume we're on Earth and divide
// by 9.81 (earth gravity) to get a percentage value, and then multiply that by 90 to convert to degrees.
var tiltLR = Math.round(((acceleration.x) / 9.81) * -90);
var tiltFB = Math.round(((acceleration.y + 9.81) / 9.81) * 90 * facingUp);
// Display the acceleration and calculated values
document.getElementById("moAccel").innerHTML = rawAcceleration;
document.getElementById("moCalcTiltLR").innerHTML = tiltLR;
document.getElementById("moCalcTiltFB").innerHTML = tiltFB;
// Apply the 2D rotation and 3D rotation to the image
var rotation = "rotate("+ tiltLR +"deg) rotate3d(1,0,0, "+ (tiltFB)+"deg)";
document.getElementById("imgLogo").style.webkitTransform = rotation
}
</script>
<!-- .................................................. -->
<!-- DeviceMotionEvent }}}2 -->
<!-- this document {{{2 -->
<!-- .................................................. -->
<script>
onload = onload_handler;
function onload_handler() {
init2();
}
</script>
<!-- .................................................. -->
<!-- this document }}}2 -->
<!-- Javascript }}} -->
<!-- ================================================== -->
</head>
<!-- ================================================== -->
<!-- START OF BODY -->
<body>
<!-- .................................................. -->
<div class="main">
<h2>Device Motion</h2>
<table>
<tr>
<td>Event Supported</td><td id="dmEvent"></td>
</tr>
<tr>
<td>accelerationIncludingGravity</td><td id="moAccel"></td>
</tr>
<tr>
<td>Calculated Left-Right Tilt</td><td id="moCalcTiltLR"></td>
</tr>
<tr>
<td>Calculated Front-Back Tilt</td><td id="moCalcTiltFB"></td>
</tr>
</table>
</div>
<div class="container" style="-webkit-perspective: 300; perspective: 300;">
<img src="images/html5_logo.png" id="imgLogo" class="logo">
</div>
<!-- ================================================== -->
</body>
</html>