Skip to content

Instantly share code, notes, and snippets.

@tkojitu
Created April 10, 2014 11:39
Show Gist options
  • Save tkojitu/10372011 to your computer and use it in GitHub Desktop.
Save tkojitu/10372011 to your computer and use it in GitHub Desktop.
<!-- http://www.html5rocks.com/en/tutorials/device/orientation/deviceorientationsample.html -->
<!doctype html>
<html>
<head>
<style>
</style>
</head>
<body>
<h2>Device Orientation</h2>
<table>
<tr>
<td>Event Supported</td>
<td id="doEvent"></td>
</tr>
<tr>
<td>Direction [alpha]</td>
<td id="doDirection"></td>
</tr>
<tr>
<td>Tilt Front/Back [beta]</td>
<td id="doTiltFB"></td>
</tr>
<tr>
<td>Tilt Left/Right [gamma]</td>
<td id="doTiltLR"></td>
</tr>
</table>
<img src="html5_logo.png" id="imgLogo" class="logo">
<script src="devoridemo.js"></script>
</body>
</html>
init();
function init() {
document.getElementById("doEvent").innerHTML = "DeviceOrientation";
window.addEventListener('deviceorientation', function(eventData) {
var tiltLR = eventData.gamma;
var tiltFB = eventData.beta;
var dir = eventData.alpha;
showOrientationText(tiltLR, tiltFB, dir);
transformImage(tiltLR, tiltFB, dir);
}, false);
}
function showOrientationText(tiltLR, tiltFB, dir) {
document.getElementById("doTiltLR").innerHTML = Math.round(tiltLR);
document.getElementById("doTiltFB").innerHTML = Math.round(tiltFB);
document.getElementById("doDirection").innerHTML = Math.round(dir);
}
function transformImage(tiltLR, tiltFB, dir) {
var logo = document.getElementById("imgLogo");
logo.style.webkitTransform = "rotate(" + tiltLR + "deg) rotate3d(1,0,0, " +
(tiltFB * -1) + "deg)";
logo.style.MozTransform = "rotate(" + tiltLR + "deg)";
logo.style.transform = "rotate(" + tiltLR + "deg) rotate3d(1,0,0, " +
(tiltFB * -1) + "deg)";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment