use-touch.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __export = (target, all) => {
  6. for (var name in all)
  7. __defProp(target, name, { get: all[name], enumerable: true });
  8. };
  9. var __copyProps = (to, from, except, desc) => {
  10. if (from && typeof from === "object" || typeof from === "function") {
  11. for (let key of __getOwnPropNames(from))
  12. if (!__hasOwnProp.call(to, key) && key !== except)
  13. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  14. }
  15. return to;
  16. };
  17. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  18. var stdin_exports = {};
  19. __export(stdin_exports, {
  20. useTouch: () => useTouch
  21. });
  22. module.exports = __toCommonJS(stdin_exports);
  23. var import_vue = require("vue");
  24. function getDirection(x, y) {
  25. if (x > y) {
  26. return "horizontal";
  27. }
  28. if (y > x) {
  29. return "vertical";
  30. }
  31. return "";
  32. }
  33. function useTouch() {
  34. const startX = (0, import_vue.ref)(0);
  35. const startY = (0, import_vue.ref)(0);
  36. const deltaX = (0, import_vue.ref)(0);
  37. const deltaY = (0, import_vue.ref)(0);
  38. const offsetX = (0, import_vue.ref)(0);
  39. const offsetY = (0, import_vue.ref)(0);
  40. const direction = (0, import_vue.ref)("");
  41. const isVertical = () => direction.value === "vertical";
  42. const isHorizontal = () => direction.value === "horizontal";
  43. const reset = () => {
  44. deltaX.value = 0;
  45. deltaY.value = 0;
  46. offsetX.value = 0;
  47. offsetY.value = 0;
  48. direction.value = "";
  49. };
  50. const start = (event) => {
  51. reset();
  52. startX.value = event.touches[0].clientX;
  53. startY.value = event.touches[0].clientY;
  54. };
  55. const move = (event) => {
  56. const touch = event.touches[0];
  57. deltaX.value = (touch.clientX < 0 ? 0 : touch.clientX) - startX.value;
  58. deltaY.value = touch.clientY - startY.value;
  59. offsetX.value = Math.abs(deltaX.value);
  60. offsetY.value = Math.abs(deltaY.value);
  61. const LOCK_DIRECTION_DISTANCE = 10;
  62. if (!direction.value || offsetX.value < LOCK_DIRECTION_DISTANCE && offsetY.value < LOCK_DIRECTION_DISTANCE) {
  63. direction.value = getDirection(offsetX.value, offsetY.value);
  64. }
  65. };
  66. return {
  67. move,
  68. start,
  69. reset,
  70. startX,
  71. startY,
  72. deltaX,
  73. deltaY,
  74. offsetX,
  75. offsetY,
  76. direction,
  77. isVertical,
  78. isHorizontal
  79. };
  80. }