-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathshakeEvent.coffee
41 lines (34 loc) · 1.06 KB
/
shakeEvent.coffee
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
# https://github.com/RayPS/Framer-Module-ShakeEvent
throttleInterval = exports.throttleInterval ?= 1
handler = Utils.throttle throttleInterval, ->
exports.onShake()
# http://stackoverflow.com/questions/711536
if typeof window.DeviceMotionEvent != 'undefined'
# Position variables
x1 = 0
y1 = 0
z1 = 0
x2 = 0
y2 = 0
z2 = 0
# Listen to motion events and update the position
window.addEventListener 'devicemotion', ((e) ->
x1 = e.accelerationIncludingGravity.x
y1 = e.accelerationIncludingGravity.y
z1 = e.accelerationIncludingGravity.z
return
), false
# Periodically check the position and fire
# if the change is greater than the sensitivity
setInterval (->
# Shake sensitivity (a lower number is more)
sensitivity = exports.sensitivity ?= 20
change = Math.abs(x1 - x2 + y1 - y2 + z1 - z2)
if change > sensitivity
handler()
# Update new position
x2 = x1
y2 = y1
z2 = z1
return
), 150