2

I need to detect in html5/javascript when a mobile device, displayed horizontally, has its left hand side tilted backwards and when the right hand side is being tilted back. Below is my code so far but I'm not convinced its the best approach. Im using this code http://www.html5rocks.com/en/tutorials/device/orientation/deviceorientationsample.html

  var firstBeta = null;
        var leftAngle;
        var rightAngle;

        function init() {
            if (window.DeviceOrientationEvent) {

                // Listen for the deviceorientation event and handle the raw data
                window.addEventListener('deviceorientation', function(eventData) {

                    var tiltFB = Math.round(eventData.beta);
                    if (firstBeta == null) {
                        firstBeta = tiltFB;
                        leftAngle = tiltFB + 20;
                        rightAngle = tiltFB - 20;

                    }

                    if (tiltFB > leftAngle) {
                        document.getElementById("raw").innerHTML = "left";
                    }
                    else if (tiltFB < rightAngle) {
                        document.getElementById("raw").innerHTML = "right";
                    }
                    else
                    {
                        document.getElementById("raw").innerHTML = "center";
                    }


                    //document.getElementById("raw").innerHTML = "actual beta: " + tiltFB + " compared to first: " + ((firstBeta + 20) > tiltFB ? "left" : (firstBeta - 20) < tiltFB ? "right" : "center");


                }, false);
            }
        }
user987723
  • 875
  • 3
  • 12
  • 28

0 Answers0