useTouchEvent.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = useTouchEvent;
  7. var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
  8. var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
  9. var _addEventListener = _interopRequireDefault(require("rc-util/lib/Dom/addEventListener"));
  10. var _react = require("react");
  11. var _getFixScaleEleTransPosition = _interopRequireDefault(require("../getFixScaleEleTransPosition"));
  12. function getDistance(a, b) {
  13. var x = a.x - b.x;
  14. var y = a.y - b.y;
  15. return Math.hypot(x, y);
  16. }
  17. function getCenter(oldPoint1, oldPoint2, newPoint1, newPoint2) {
  18. // Calculate the distance each point has moved
  19. var distance1 = getDistance(oldPoint1, newPoint1);
  20. var distance2 = getDistance(oldPoint2, newPoint2);
  21. // If both distances are 0, return the original points
  22. if (distance1 === 0 && distance2 === 0) {
  23. return [oldPoint1.x, oldPoint1.y];
  24. }
  25. // Calculate the ratio of the distances
  26. var ratio = distance1 / (distance1 + distance2);
  27. // Calculate the new center point based on the ratio
  28. var x = oldPoint1.x + ratio * (oldPoint2.x - oldPoint1.x);
  29. var y = oldPoint1.y + ratio * (oldPoint2.y - oldPoint1.y);
  30. return [x, y];
  31. }
  32. function useTouchEvent(imgRef, movable, visible, minScale, transform, updateTransform, dispatchZoomChange) {
  33. var rotate = transform.rotate,
  34. scale = transform.scale,
  35. x = transform.x,
  36. y = transform.y;
  37. var _useState = (0, _react.useState)(false),
  38. _useState2 = (0, _slicedToArray2.default)(_useState, 2),
  39. isTouching = _useState2[0],
  40. setIsTouching = _useState2[1];
  41. var touchPointInfo = (0, _react.useRef)({
  42. point1: {
  43. x: 0,
  44. y: 0
  45. },
  46. point2: {
  47. x: 0,
  48. y: 0
  49. },
  50. eventType: 'none'
  51. });
  52. var updateTouchPointInfo = function updateTouchPointInfo(values) {
  53. touchPointInfo.current = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, touchPointInfo.current), values);
  54. };
  55. var onTouchStart = function onTouchStart(event) {
  56. if (!movable) return;
  57. event.stopPropagation();
  58. setIsTouching(true);
  59. var _event$touches = event.touches,
  60. touches = _event$touches === void 0 ? [] : _event$touches;
  61. if (touches.length > 1) {
  62. // touch zoom
  63. updateTouchPointInfo({
  64. point1: {
  65. x: touches[0].clientX,
  66. y: touches[0].clientY
  67. },
  68. point2: {
  69. x: touches[1].clientX,
  70. y: touches[1].clientY
  71. },
  72. eventType: 'touchZoom'
  73. });
  74. } else {
  75. // touch move
  76. updateTouchPointInfo({
  77. point1: {
  78. x: touches[0].clientX - x,
  79. y: touches[0].clientY - y
  80. },
  81. eventType: 'move'
  82. });
  83. }
  84. };
  85. var onTouchMove = function onTouchMove(event) {
  86. var _event$touches2 = event.touches,
  87. touches = _event$touches2 === void 0 ? [] : _event$touches2;
  88. var _touchPointInfo$curre = touchPointInfo.current,
  89. point1 = _touchPointInfo$curre.point1,
  90. point2 = _touchPointInfo$curre.point2,
  91. eventType = _touchPointInfo$curre.eventType;
  92. if (touches.length > 1 && eventType === 'touchZoom') {
  93. // touch zoom
  94. var newPoint1 = {
  95. x: touches[0].clientX,
  96. y: touches[0].clientY
  97. };
  98. var newPoint2 = {
  99. x: touches[1].clientX,
  100. y: touches[1].clientY
  101. };
  102. var _getCenter = getCenter(point1, point2, newPoint1, newPoint2),
  103. _getCenter2 = (0, _slicedToArray2.default)(_getCenter, 2),
  104. centerX = _getCenter2[0],
  105. centerY = _getCenter2[1];
  106. var ratio = getDistance(newPoint1, newPoint2) / getDistance(point1, point2);
  107. dispatchZoomChange(ratio, 'touchZoom', centerX, centerY, true);
  108. updateTouchPointInfo({
  109. point1: newPoint1,
  110. point2: newPoint2,
  111. eventType: 'touchZoom'
  112. });
  113. } else if (eventType === 'move') {
  114. // touch move
  115. updateTransform({
  116. x: touches[0].clientX - point1.x,
  117. y: touches[0].clientY - point1.y
  118. }, 'move');
  119. updateTouchPointInfo({
  120. eventType: 'move'
  121. });
  122. }
  123. };
  124. var onTouchEnd = function onTouchEnd() {
  125. if (!visible) return;
  126. if (isTouching) {
  127. setIsTouching(false);
  128. }
  129. updateTouchPointInfo({
  130. eventType: 'none'
  131. });
  132. if (minScale > scale) {
  133. /** When the scaling ratio is less than the minimum scaling ratio, reset the scaling ratio */
  134. return updateTransform({
  135. x: 0,
  136. y: 0,
  137. scale: minScale
  138. }, 'touchZoom');
  139. }
  140. var width = imgRef.current.offsetWidth * scale;
  141. var height = imgRef.current.offsetHeight * scale;
  142. // eslint-disable-next-line @typescript-eslint/no-shadow
  143. var _imgRef$current$getBo = imgRef.current.getBoundingClientRect(),
  144. left = _imgRef$current$getBo.left,
  145. top = _imgRef$current$getBo.top;
  146. var isRotate = rotate % 180 !== 0;
  147. var fixState = (0, _getFixScaleEleTransPosition.default)(isRotate ? height : width, isRotate ? width : height, left, top);
  148. if (fixState) {
  149. updateTransform((0, _objectSpread2.default)({}, fixState), 'dragRebound');
  150. }
  151. };
  152. (0, _react.useEffect)(function () {
  153. var onTouchMoveListener;
  154. if (visible && movable) {
  155. onTouchMoveListener = (0, _addEventListener.default)(window, 'touchmove', function (e) {
  156. return e.preventDefault();
  157. }, {
  158. passive: false
  159. });
  160. }
  161. return function () {
  162. var _onTouchMoveListener;
  163. (_onTouchMoveListener = onTouchMoveListener) === null || _onTouchMoveListener === void 0 || _onTouchMoveListener.remove();
  164. };
  165. }, [visible, movable]);
  166. return {
  167. isTouching: isTouching,
  168. onTouchStart: onTouchStart,
  169. onTouchMove: onTouchMove,
  170. onTouchEnd: onTouchEnd
  171. };
  172. }