Skip to content

Instantly share code, notes, and snippets.

@maccman
Created September 19, 2011 13:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maccman/1226477 to your computer and use it in GitHub Desktop.
Save maccman/1226477 to your computer and use it in GitHub Desktop.
$ = jQuery
$ ->
threshold = 20
x1 = y1 = z1 = x2 = y2 = z2 = 0
checkShake = (e) ->
current = e.accelerationIncludingGravity
x1 = current.x
y1 = current.y
z1 = current.z
setInterval ->
change = Math.abs(x1-x2+y1-y2+z1-z2)
if change > threshold
$('body, window').trigger('shake')
x2 = x1
y2 = y1
z2 = z1
, 200
window.addEventListener 'devicemotion', checkShake, false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment