Skip to content

Instantly share code, notes, and snippets.

@joshpuckett
Created February 25, 2014 19:42
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshpuckett/9216195 to your computer and use it in GitHub Desktop.
Save joshpuckett/9216195 to your computer and use it in GitHub Desktop.
Swipe Angle
view.on("touchstart", function(e) {
var touch = event.touches[0]
//Grab the initial touch coordinates
xInit = touch.pageX
yInit = touch.pageY
})
view.on("touchmove", function(e) {
var touch = event.touches[0]
xCurrent = touch.pageX
yCurrent = touch.pageY
//Determine the distance between initial and current touch coordinates
deltaX = xCurrent - xInit
deltaY = yCurrent - yInit
//Trig is awesome!
theta = Math.atan2(deltaY, deltaX)
theta *= 180/Math.PI
//Set a threshold on when to determine the angle
if (Math.abs(deltaX) > 20 || Math.abs(deltaY) > 20) {
switch (true)
{
//Swiping up while moving
case (-135 < theta && theta < -45):
break
//Swiping right while moving
case (-45 < theta && theta < 45):
break
//Swiping down while moving
case (45 < theta && theta < 135):
break
//Swiping left while moving
case (-135 > theta || theta > 135):
break
}
}
})
view.on("touchend", function(){
switch (true)
{
//Swiped up
case (-135 < theta && theta < -45):
break
//Swiped right
case (-45 < theta && theta < 45):
break
//Swiped down
case (45 < theta && theta < 135):
break
//Swiped left
case (-135 > theta || theta > 135):
break
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment