utils.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. /**
  2. * adb 公共工具:滑动坐标计算等
  3. */
  4. function calculateSwipeCoordinates(direction, width, height) {
  5. const margin = 0.15
  6. const swipeDistance = 0.7
  7. let x1, y1, x2, y2
  8. switch (direction) {
  9. case 'up-down':
  10. x1 = x2 = Math.round(width / 2)
  11. y1 = Math.round(height * margin)
  12. y2 = Math.round(height * (margin + swipeDistance))
  13. break
  14. case 'down-up':
  15. x1 = x2 = Math.round(width / 2)
  16. y1 = Math.round(height * (margin + swipeDistance))
  17. y2 = Math.round(height * margin)
  18. break
  19. case 'left-right':
  20. y1 = y2 = Math.round(height / 2)
  21. x1 = Math.round(width * margin)
  22. x2 = Math.round(width * (margin + swipeDistance))
  23. break
  24. case 'right-left':
  25. y1 = y2 = Math.round(height / 2)
  26. x1 = Math.round(width * (margin + swipeDistance))
  27. x2 = Math.round(width * margin)
  28. break
  29. default:
  30. throw new Error(`未知的滑动方向: ${direction}`)
  31. }
  32. return { x1, y1, x2, y2 }
  33. }
  34. module.exports = { calculateSwipeCoordinates }