Skip to content

Instantly share code, notes, and snippets.

@sakapon
Last active January 6, 2019 09:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sakapon/168ebc510f375af192cd3fc6d44d6a02 to your computer and use it in GitHub Desktop.
Save sakapon/168ebc510f375af192cd3fc6d44d6a02 to your computer and use it in GitHub Desktop.
JS-Test/DeviceOrientation
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>Device Orientation</title>
<style type="text/css">
.container {
width: 2px;
height: 2px;
margin: 100px auto;
perspective: 300px;
}
#cube {
transform-style: preserve-3d;
}
.face {
width: 100px;
height: 100px;
position: absolute;
left: -50px;
top: -50px;
background: rgba(255, 102, 0, 0.5);
border: 2px solid gray;
text-align: center;
font-size: 60px;
line-height: 100px;
}
.front {
transform: translateZ(50px);
}
.back {
transform: rotateY(180deg) translateZ(50px);
}
.right {
transform: rotateY(90deg) translateZ(50px);
}
.left {
transform: rotateY(-90deg) translateZ(50px);
}
.top {
transform: rotateX(90deg) translateZ(50px);
}
.bottom {
transform: rotateX(-90deg) translateZ(50px);
}
</style>
</head>
<body>
<div id="log"></div>
<div class="container">
<div id="cube">
<div class="face front">1</div>
<div class="face back">6</div>
<div class="face right">2</div>
<div class="face left">5</div>
<div class="face top">3</div>
<div class="face bottom">4</div>
</div>
</div>
<script type="text/javascript">
var logEl = document.querySelector("#log");
var cubeEl = document.querySelector("#cube");
if (window.DeviceOrientationEvent) {
window.addEventListener("deviceorientation", function (e) {
logEl.innerHTML = `alpha: ${e.alpha}<br />beta: ${e.beta}<br />gamma: ${e.gamma}`;
cubeEl.style.transform = `rotateZ(${-e.alpha}deg) rotateX(${-e.beta}deg) rotateY(${e.gamma}deg)`;
});
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment