inertial.js
1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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 };
});