-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathOrientationEvents.coffee
More file actions
executable file
·88 lines (66 loc) · 2.76 KB
/
OrientationEvents.coffee
File metadata and controls
executable file
·88 lines (66 loc) · 2.76 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
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
###
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
###
# –––– VARIABLES
# Orientation values
exports.orientation = null
# Motion Values
exports.motion = null
# Used for smoothing out data values
exports.smoothOrientation = 1
exports.smoothMotion = 1
# Filtered variables for orientation
filteredAlpha = 0
filteredBeta = 0
filteredGamma = 0
# Filtered variables for motion
filteredX = 0
filteredY = 0
filteredZ = 0
filteredGravX = 0
filteredGravY = 0
filteredGravZ = 0
# –––– PUBLIC
exports.OrientationEvents = ->
events = switch
when window.DeviceOrientationEvent && !(window.DeviceMotionEvent)
window.addEventListener "deviceorientation", _orientation
print "Device motion events are not support on this device."
when window.DeviceMotionEvent && !(window.DeviceOrientationEvent)
window.addEventListener "devicemotion", _motion
print "Device orientation events are not suported on this device"
when window.DeviceOrientationEvent && window.DeviceMotionEvent
window.addEventListener "deviceorientation", _orientation
window.addEventListener "devicemotion", _motion
else
console.warn('Device orientation and motion events are not support on this device.')
# –––– PRIVATE
_motion = (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 if motion?
_orientation = (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 if orientation?