Last active
December 17, 2015 22:59
-
-
Save shamangeorge/06f61adb3700246d6886 to your computer and use it in GitHub Desktop.
Device Motion events Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#deviceMotionVariables { | |
text-align: justify; | |
width: 200px; | |
border: 5px solid black; | |
position: absolute; | |
right: 10%; | |
top: 5%; | |
display: none; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<section id="deviceMotionVariables"> | |
<div id="acceleration"> | |
<p id="accelerationX"> | |
a<sub>x</sub> | |
</p> | |
<p id="accelerationY"> | |
a<sub>y</sub> | |
</p> | |
<p id="accelerationZ"> | |
a<sub>z</sub> | |
</p> | |
</div> | |
<div id="rotation"> | |
<p id="rotationAlpha"> | |
r<sub>α</sub> | |
</p> | |
<p id="rotationBeta"> | |
r<sub>β</sub> | |
</p> | |
<p id="rotationGamma"> | |
r<sub>γ</sub> | |
</p> | |
</div> | |
</section> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.ondevicemotion = function(event) | |
{ | |
document.getElementById("deviceMotionVariables").style.display = "block"; | |
document.getElementById("accelerationX").innerHTML = "a<sub>x</sub>: " + event.accelerationIncludingGravity.x.toFixed(3); | |
document.getElementById("accelerationY").innerHTML = "a<sub>y</sub>: " + event.accelerationIncludingGravity.y.toFixed(3); | |
document.getElementById("accelerationZ").innerHTML = "a<sub>z</sub>: " + event.accelerationIncludingGravity.z.toFixed(3); | |
if (event.rotationRate) | |
{ | |
document.getElementById("rotationAlpha").innerHTML = "r<sub>α</sub>: " + event.rotationRate.alpha.toFixed(3); | |
document.getElementById("rotationBeta").innerHTML = "r<sub>β</sub>: " + event.rotationRate.beta.toFixed(3); | |
document.getElementById("rotationGamma").innerHTML = "r<sub>γ</sub>: " + event.rotationRate.gamma.toFixed(3); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment