inertial.js 1.83 KB
arcs_module(function (ARCS) {
    var Inertial = ARCS.Component.create( function() {
        var self = this;
        var screenOrientation = false;

        // here, we should correct orientation
        var handleOrientation = function (event) {
            if (screenOrientation) {
                var orientation = screen.msOrientation 
                    || screen.mozOrientation || screen.orientation;
                event.alpha -= (orientation)?orientation.angle:0;
            }
            self.emit("sendOrientation",event);
        };
        
        var handleMotion = function(event) {
            self.emit("sendAcceleration",event.acceleration);
            self.emit("sendAccelerationIncludingGravity", event.accelerationIncludingGravity);
            self.emit("sendRotationRate",event.rotationRate); 
        };
        
        
        this.start = function() {
            if (window.DeviceOrientationEvent) {
                console.log("Device orientation capability detected");
                window.addEventListener("deviceorientation", handleOrientation, false);
            } else {
                console.log("[Inertial]","no device orientation API");
            }
        
            if (window.DeviceMotionEvent) {
                console.log("Device motion capability detected");
                window.addEventListener("devicemotion", handleMotion, true);
            } else {
                console.log("[Inertial]","no device motion API");
            }
            
            
        };

        this.setScreenOrientation = function(flag) {
            screenOrientation = flag;
        };
        
        },
        ["start","setScreenOrientation"],
        ["sendOrientation","sendAcceleration","sendAccelerationIncludingGravity", "sendRotationRate"]
    );
    
    
    
 
    
    return { Inertial : Inertial }; 
});