use-touch.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. var import_utils = require("../utils");
  25. function getDirection(x, y) {
  26. if (x > y) {
  27. return "horizontal";
  28. }
  29. if (y > x) {
  30. return "vertical";
  31. }
  32. return "";
  33. }
  34. function useTouch() {
  35. const startX = (0, import_vue.ref)(0);
  36. const startY = (0, import_vue.ref)(0);
  37. const deltaX = (0, import_vue.ref)(0);
  38. const deltaY = (0, import_vue.ref)(0);
  39. const offsetX = (0, import_vue.ref)(0);
  40. const offsetY = (0, import_vue.ref)(0);
  41. const direction = (0, import_vue.ref)("");
  42. const isTap = (0, import_vue.ref)(true);
  43. const isVertical = () => direction.value === "vertical";
  44. const isHorizontal = () => direction.value === "horizontal";
  45. const reset = () => {
  46. deltaX.value = 0;
  47. deltaY.value = 0;
  48. offsetX.value = 0;
  49. offsetY.value = 0;
  50. direction.value = "";
  51. isTap.value = true;
  52. };
  53. const start = (event) => {
  54. reset();
  55. startX.value = event.touches[0].clientX;
  56. startY.value = event.touches[0].clientY;
  57. };
  58. const move = (event) => {
  59. const touch = event.touches[0];
  60. deltaX.value = (touch.clientX < 0 ? 0 : touch.clientX) - startX.value;
  61. deltaY.value = touch.clientY - startY.value;
  62. offsetX.value = Math.abs(deltaX.value);
  63. offsetY.value = Math.abs(deltaY.value);
  64. const LOCK_DIRECTION_DISTANCE = 10;
  65. if (!direction.value || offsetX.value < LOCK_DIRECTION_DISTANCE && offsetY.value < LOCK_DIRECTION_DISTANCE) {
  66. direction.value = getDirection(offsetX.value, offsetY.value);
  67. }
  68. if (isTap.value && (offsetX.value > import_utils.TAP_OFFSET || offsetY.value > import_utils.TAP_OFFSET)) {
  69. isTap.value = false;
  70. }
  71. };
  72. return {
  73. move,
  74. start,
  75. reset,
  76. startX,
  77. startY,
  78. deltaX,
  79. deltaY,
  80. offsetX,
  81. offsetY,
  82. direction,
  83. isVertical,
  84. isHorizontal,
  85. isTap
  86. };
  87. }