Compass

Sample Project that uses MotionStack's Compass API

See the full list of sample projects.

This sample displays a compass that points north that is powered by MotionStack's Compass API:

<!DOCTYPE html>
<html>
<head>
  <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no">
  <style>
    body {
      background-color: #000000;
    }
    .dial {
      position: absolute;
      width: 200px;
      height: 200px;
      top: 50%;
      left: 50%;
      margin-left: -100px;
      margin-top: -100px;
    }
    .dial img {
      width: 100%;
      height: 100%;
    }
  </style>
</head>
<body>

  <div class="dial"><img src="./dial.png"></div>

  <script src="../lib/motionstack-vlatest.js"></script>
  <script>
    // motion controls
    var heading;
    function tracking(e) {
      heading = e.heading;
    }

    var compass = new MotionStack.Compass();
    compass.start(tracking);


    // animation loop
    var dial = document.querySelector(".dial img");

    function rotateDial() {
      dial.style.webkitTransform = "rotateZ("+ heading +"deg) translateZ(0)";
      dial.style.transform = dial.style.webkitTransform;
    }

    var rotateThisDial = rotateDial.bind(this);
    window.requestAnimationFrame(function loop() {
      if(heading !== undefined) {
        rotateThisDial();
      }

      window.requestAnimationFrame(loop);
    });
  </script>

</body>
</html>