forked from joshmtucker/OrientationEvents
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OrientationEvents.js
88 lines (69 loc) · 2.98 KB
/
OrientationEvents.js
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Generated by CoffeeScript 1.9.1
/*
Based with values documented in:
deviceorientation -> https://developer.mozilla.org/en-US/docs/Web/Events/deviceorientation
devicemotion - > https://developer.mozilla.org/en-US/docs/Web/Events/devicemotion
*/
var _motion, _orientation, filteredAlpha, filteredBeta, filteredGamma, filteredGravX, filteredGravY, filteredGravZ, filteredX, filteredY, filteredZ;
exports.orientation = null;
exports.motion = null;
exports.smoothOrientation = 1;
exports.smoothMotion = 1;
filteredAlpha = 0;
filteredBeta = 0;
filteredGamma = 0;
filteredX = 0;
filteredY = 0;
filteredZ = 0;
filteredGravX = 0;
filteredGravY = 0;
filteredGravZ = 0;
exports.OrientationEvents = function() {
var events;
return events = (function() {
switch (false) {
case !(window.DeviceOrientationEvent && !window.DeviceMotionEvent):
window.addEventListener("deviceorientation", _orientation);
return print("Device motion events are not support on this device.");
case !(window.DeviceMotionEvent && !window.DeviceOrientationEvent):
window.addEventListener("devicemotion", _motion);
return print("Device orientation events are not suported on this device");
case !(window.DeviceOrientationEvent && window.DeviceMotionEvent):
window.addEventListener("deviceorientation", _orientation);
return window.addEventListener("devicemotion", _motion);
default:
return print("Device orientation and motion events are not support on this device.");
}
})();
};
_motion = function(event) {
filteredX = (event.acceleration.x * exports.smoothMotion) + (filteredX * (1 - exports.smoothMotion));
filteredY = (event.acceleration.y * exports.smoothMotion) + (filteredY * (1 - exports.smoothMotion));
filteredZ = (event.acceleration.z * exports.smoothMotion) + (filteredZ * (1 - exports.smoothMotion));
filteredGravX = (event.accelerationIncludingGravity.x * exports.smoothMotion) + (filteredGravX * (1 - exports.smoothMotion));
filteredGravY = (event.accelerationIncludingGravity.y * exports.smoothMotion) + (filteredGravY * (1 - exports.smoothMotion));
filteredGravZ = (event.accelerationIncludingGravity.z * exports.smoothMotion) + (filteredGravZ * (1 - exports.smoothMotion));
exports.motion = {
x: filteredX,
y: filteredY,
z: filteredZ,
gravX: filteredGravX,
gravY: filteredGravY,
gravZ: filteredGravZ,
rotationRate: event.rotationRate,
interval: event.interval
};
return motion;
};
_orientation = function(event) {
filteredAlpha = (event.alpha * exports.smoothOrientation) + (filteredAlpha * (1 - exports.smoothOrientation));
filteredBeta = (event.beta * exports.smoothOrientation) + (filteredBeta * (1 - exports.smoothOrientation));
filteredGamma = (event.gamma * exports.smoothOrientation) + (filteredGamma * (1 - exports.smoothOrientation));
exports.orientation = {
alpha: filteredAlpha,
beta: filteredBeta,
gamma: filteredGamma,
absolute: event.absolute
};
return orientation;
};