<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>JavaScript Sensor Access Demo</title>
</head>
<style>
body {
background-image: url('airplane.png');
background-repeat: no-repeat;
}
</style>
<body>
<h1 align="left">Orientation</h1>
<p>Sensor data is displayed by javascript running on the local browser. </p>
<p>Run on a mobile phone or a tablet - a device that has the appropriate sensors.</p>
<p><b>deviceorientation provides:</b><ul><li>alpha - yaw </li><li>beta - pitch</li><li>gamma - roll</li></ul></p>
<a id="start_demo" href="#" role="button" >Start the demo</a>
<p>Num. of datapoints: <span id="num-observed-events">0</span></p>
<h4>Device Orientation:</h4>
<ul>
<li style="font-size: 2em">Yaw (α): <span id="Orientation_a">0</span><span>°</span></li>
<li style="font-size: 2em">Pitch (β): <span id="Orientation_b">0</span><span>°</span></li>
<li style="font-size: 2em">Roll (γ): <span id="Orientation_g">0</span><span>°</span></li>
</ul>
<script>
function updateFieldIfNotNull(fieldName, value, precision=1){
if (value != null)
document.getElementById(fieldName).innerHTML = value.toFixed(precision);
}
function incrementEventCount(){
let counterElement = document.getElementById("num-observed-events")
let eventCount = parseInt(counterElement.innerHTML)
counterElement.innerHTML = eventCount + 1;
}
function handleOrientation(event) {
updateFieldIfNotNull('Orientation_a', event.alpha);
updateFieldIfNotNull('Orientation_b', event.beta);
updateFieldIfNotNull('Orientation_g', event.gamma);
incrementEventCount();
}
let is_running = false;
let demo_button = document.getElementById("start_demo");
demo_button.onclick = function(e) {
e.preventDefault();
if ( DeviceMotionEvent && typeof DeviceMotionEvent.requestPermission === "function" ) {
DeviceMotionEvent.requestPermission();
}
if (is_running){
window.removeEventListener("deviceorientation", handleOrientation);
demo_button.innerHTML = "Start demo";
is_running = false;
}else{
window.addEventListener("deviceorientation", handleOrientation);
document.getElementById("start_demo").innerHTML = "Stop demo";
is_running = true;
document.getElementById("num-observed-events").innerHTML = 0;
}
};
</script>
</body>
</html>