--- /dev/null
+<!DOCTYPE html>
+<html>
+<head>
+ <meta charset="utf-8" />
+ <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
+ <meta name="description" content="Device Orientation - Devicemotion Event"/>
+
+ <title>Device Orientation - Device Motion Capture</title>
+
+ <link rel="stylesheet" type="text/css" href="css/style.css"/>
+ <script src="js/main.js"></script>
+</head>
+
+<body>
+ <h1>DEVICE MOTION CAPTURE</h1>
+
+ <div id="circle">Circle</div>
+ <div>
+ <p id="firElem"></p>
+ <p id="secElem"></p>
+ <p id="thirElem"></p>
+ </div>
+
+ <audio id="audio_re" controls preload src="audio/gun1.wav"></audio>
+</body>
+</html>
--- /dev/null
+function deviceMotionEvents() {
+ var firElem = document.getElementById("firElem");
+ var secElem =document.getElementById("secElem");
+ var thirElem = document.getElementById("thirElem");
+ var circle = document.getElementById('circle');
+ var audioElem = document.getElementById("audio_re");
+ var x = 0, y = 0, vx = 0, vy = 0, ax = 0, ay = 0;
+
+ window.addEventListener("devicemotion", function(e) {
+ deviceMotionHandler(e);
+ playSoundHandler(e);
+ logDeviceMotionHandler(e);
+ }, true);
+
+ function deviceMotionHandler(e) {
+ //Calculate the acceleration due to gravity
+ ax = e.accelerationIncludingGravity.x * 5;
+ ay = e.accelerationIncludingGravity.y * 5;
+
+ vx = vx + ax;
+ vy = vy - ay;
+
+ vx = vx * 0.98;
+ vy = vy * 0.98;
+
+ x = parseInt(x + (vx / 20));
+ y = parseInt(y + (vy / 20));
+
+ // Bound Check
+ if (x < 0) {
+ x = 0;
+ vx = -vx;
+ }
+ if (y < 0) {
+ y = 0;
+ vy = -vy;
+ }
+ if (x > document.documentElement.clientWidth - 80) {
+ x = document.documentElement.clientWidth - 80;
+ vx = -vx;
+ }
+ if (y > document.documentElement.clientHeight - 80) {
+ y = document.documentElement.clientHeight - 80;
+ vy = -vy;
+ }
+
+ //Style insert
+ circle.style.bottom = y + "px";
+ circle.style.right = x + "px";
+}
+
+ function playSoundHandler(e) {
+ if(e.acceleration.x > 6){
+ audioElem.currentTime = 0;
+ audioElem.volume=1;
+ audioElem.play();
+ }
+ }
+
+ function logDeviceMotionHandler(e) {
+ firElem.innerHTML = 'acceleration value<br><br> '
+ + '[ x value : '+ Math.round(e.acceleration.x) + " ]<br>"
+ + '[ y value : '+ Math.round(e.acceleration.y) + " ]<br>"
+ + '[ z value : '+ Math.round(e.acceleration.z) + ']';
+
+ secElem.innerHTML = 'accelerationIncludingGravity value<br><br> '
+ + '[ x value : ' + Math.round(e.accelerationIncludingGravity.x) + " ]<br>"
+ + '[ y value : ' + Math.round(e.accelerationIncludingGravity.y) + " ]<br>"
+ + '[ z value : ' + Math.round(e.accelerationIncludingGravity.z) + ']';
+
+ thirElem.innerHTML = 'rotationRate value<br><br> '
+ + '[ alpha value : ' + Math.round(e.rotationRate.alpha) + " degree ]<br>"
+ + '[ beta value : ' + Math.round(e.rotationRate.beta) + " degree ]<br>"
+ + '[ gamma value : ' + Math.round(e.rotationRate.gamma) + ' degree ]';
+ }
+}
+
+window.onload = function() {deviceMotionEvents();};