1 function deviceMotionEvents() {
2 var firElem = document.getElementById("firElem");
3 var secElem =document.getElementById("secElem");
4 var thirElem = document.getElementById("thirElem");
5 var circle = document.getElementById('circle');
6 var audioElem = document.getElementById("audio_re");
7 var x = 0, y = 0, vx = 0, vy = 0, ax = 0, ay = 0;
9 window.addEventListener("devicemotion", function(e) {
10 deviceMotionHandler(e);
12 logDeviceMotionHandler(e);
15 function deviceMotionHandler(e) {
16 //Calculate the acceleration due to gravity
17 ax = e.accelerationIncludingGravity.x * 5;
18 ay = e.accelerationIncludingGravity.y * 5;
26 x = parseInt(Math.round(x + (vx / 20)));
27 y = parseInt(Math.round(y + (vy / 20)));
38 if (x >= document.documentElement.clientWidth - 80) {
39 x = document.documentElement.clientWidth - 80;
42 if (y >= document.documentElement.clientHeight - 80) {
43 y = document.documentElement.clientHeight - 80;
48 circle.style.bottom = y + "px";
49 circle.style.right = x + "px";
52 function playSoundHandler(e) {
53 if(e.acceleration.x > 6){
54 audioElem.currentTime = 0;
60 function logDeviceMotionHandler(e) {
61 firElem.innerHTML = 'acceleration value<br><br> '
62 + '[ x value : '+ Math.round(e.acceleration.x) + " ]<br>"
63 + '[ y value : '+ Math.round(e.acceleration.y) + " ]<br>"
64 + '[ z value : '+ Math.round(e.acceleration.z) + ']';
66 secElem.innerHTML = 'accelerationIncludingGravity value<br><br> '
67 + '[ x value : ' + Math.round(e.accelerationIncludingGravity.x) + " ]<br>"
68 + '[ y value : ' + Math.round(e.accelerationIncludingGravity.y) + " ]<br>"
69 + '[ z value : ' + Math.round(e.accelerationIncludingGravity.z) + ']';
71 thirElem.innerHTML = 'rotationRate value<br><br> '
72 + '[ alpha value : ' + Math.round(e.rotationRate.alpha) + " degree ]<br>"
73 + '[ beta value : ' + Math.round(e.rotationRate.beta) + " degree ]<br>"
74 + '[ gamma value : ' + Math.round(e.rotationRate.gamma) + ' degree ]';
78 window.onload = function() {deviceMotionEvents();};