Skip to content

Instantly share code, notes, and snippets.

@tkd55
Created February 18, 2015 05:16
Show Gist options
  • Save tkd55/8e818df697b4492d625f to your computer and use it in GitHub Desktop.
Save tkd55/8e818df697b4492d625f to your computer and use it in GitHub Desktop.
deviceorientation and devicemotion for JavaScript
<!DOCTYPE html>
<html lang="jp">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    
    <div>
        <p>deviceorientation(傾き)</p>
        <div id="ori-a"></div>
        <div id="ori-b"></div>
        <div id="ori-g"></div>
    </div>

    <div>
        <p>devicemotion(加速度)</p>
        <div id="acc-a"></div>
        <div id="acc-b"></div>
        <div id="acc-g"></div>
    </div>

    <script type="text/javascript">
    (function(){
        window.addEventListener('deviceorientation', function(evt){
            document.getElementById('ori-a').innerText = evt.alpha;
            document.getElementById('ori-b').innerText = evt.beta;
            document.getElementById('ori-g').innerText = evt.gamma;
        }, true);

        window.addEventListener('devicemotion', function(){
            var acc = event.acceleration;
            document.getElementById('acc-a').innerText = acc.x;
            document.getElementById('acc-b').innerText = acc.y;
            document.getElementById('acc-g').innerText = acc.z;

        }, true);
    })();
    </script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment