gestures.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /**
  2. * 获取触摸点位置信息
  3. */
  4. export const getTouchPoints = (e, index = 0) => {
  5. const { pageX: x, pageY: y } = e.touches[index] || e.changedTouches[index]
  6. return {
  7. x,
  8. y,
  9. }
  10. }
  11. /**
  12. * 获取触摸点个数
  13. */
  14. export const getPointsNumber = (e) => e.touches && e.touches.length || e.changedTouches && e.changedTouches.length
  15. /**
  16. * 判断是否为同一点
  17. */
  18. export const isEqualPoints = (p1, p2) => p1.x === p2.x && p1.y === p2.y
  19. /**
  20. * 判断是否为相近的两点
  21. */
  22. export const isNearbyPoints = (p1, p2, DOUBLE_TAP_RADIUS = 25) => {
  23. const xMove = Math.abs(p1.x - p2.x)
  24. const yMove = Math.abs(p1.y - p2.y)
  25. return xMove < DOUBLE_TAP_RADIUS & yMove < DOUBLE_TAP_RADIUS
  26. }
  27. /**
  28. * 获取两点之间的距离
  29. */
  30. export const getPointsDistance = (p1, p2) => {
  31. const xMove = Math.abs(p1.x - p2.x)
  32. const yMove = Math.abs(p1.y - p2.y)
  33. return Math.sqrt(xMove * xMove + yMove * yMove)
  34. }
  35. /**
  36. * 获取触摸移动方向
  37. */
  38. export const getSwipeDirection = (x1, x2, y1, y2) => {
  39. return Math.abs(x1 - x2) >= Math.abs(y1 - y2) ? (x1 - x2 > 0 ? 'Left' : 'Right') : (y1 - y2 > 0 ? 'Up' : 'Down')
  40. }