Created
February 25, 2014 19:42
-
-
Save joshpuckett/9216195 to your computer and use it in GitHub Desktop.
Swipe Angle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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