ImagePreviewItem.mjs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. import { createVNode as _createVNode } from "vue";
  2. import { ref, watch, computed, reactive, defineComponent } from "vue";
  3. import { clamp, numericProp, preventDefault, createNamespace, makeRequiredProp } from "../utils/index.mjs";
  4. import { useTouch } from "../composables/use-touch.mjs";
  5. import { useEventListener } from "@vant/use";
  6. import { Image } from "../image/index.mjs";
  7. import { Loading } from "../loading/index.mjs";
  8. import { SwipeItem } from "../swipe-item/index.mjs";
  9. const getDistance = (touches) => Math.sqrt((touches[0].clientX - touches[1].clientX) ** 2 + (touches[0].clientY - touches[1].clientY) ** 2);
  10. const bem = createNamespace("image-preview")[1];
  11. var stdin_default = defineComponent({
  12. props: {
  13. src: String,
  14. show: Boolean,
  15. active: Number,
  16. minZoom: makeRequiredProp(numericProp),
  17. maxZoom: makeRequiredProp(numericProp),
  18. rootWidth: makeRequiredProp(Number),
  19. rootHeight: makeRequiredProp(Number)
  20. },
  21. emits: ["scale", "close"],
  22. setup(props, {
  23. emit
  24. }) {
  25. const state = reactive({
  26. scale: 1,
  27. moveX: 0,
  28. moveY: 0,
  29. moving: false,
  30. zooming: false,
  31. imageRatio: 0,
  32. displayWidth: 0,
  33. displayHeight: 0
  34. });
  35. const touch = useTouch();
  36. const swipeItem = ref();
  37. const vertical = computed(() => {
  38. const {
  39. rootWidth,
  40. rootHeight
  41. } = props;
  42. const rootRatio = rootHeight / rootWidth;
  43. return state.imageRatio > rootRatio;
  44. });
  45. const imageStyle = computed(() => {
  46. const {
  47. scale,
  48. moveX,
  49. moveY,
  50. moving,
  51. zooming
  52. } = state;
  53. const style = {
  54. transitionDuration: zooming || moving ? "0s" : ".3s"
  55. };
  56. if (scale !== 1) {
  57. const offsetX = moveX / scale;
  58. const offsetY = moveY / scale;
  59. style.transform = `scale(${scale}, ${scale}) translate(${offsetX}px, ${offsetY}px)`;
  60. }
  61. return style;
  62. });
  63. const maxMoveX = computed(() => {
  64. if (state.imageRatio) {
  65. const {
  66. rootWidth,
  67. rootHeight
  68. } = props;
  69. const displayWidth = vertical.value ? rootHeight / state.imageRatio : rootWidth;
  70. return Math.max(0, (state.scale * displayWidth - rootWidth) / 2);
  71. }
  72. return 0;
  73. });
  74. const maxMoveY = computed(() => {
  75. if (state.imageRatio) {
  76. const {
  77. rootWidth,
  78. rootHeight
  79. } = props;
  80. const displayHeight = vertical.value ? rootHeight : rootWidth * state.imageRatio;
  81. return Math.max(0, (state.scale * displayHeight - rootHeight) / 2);
  82. }
  83. return 0;
  84. });
  85. const setScale = (scale) => {
  86. scale = clamp(scale, +props.minZoom, +props.maxZoom + 1);
  87. if (scale !== state.scale) {
  88. state.scale = scale;
  89. emit("scale", {
  90. scale,
  91. index: props.active
  92. });
  93. }
  94. };
  95. const resetScale = () => {
  96. setScale(1);
  97. state.moveX = 0;
  98. state.moveY = 0;
  99. };
  100. const toggleScale = () => {
  101. const scale = state.scale > 1 ? 1 : 2;
  102. setScale(scale);
  103. state.moveX = 0;
  104. state.moveY = 0;
  105. };
  106. let fingerNum;
  107. let startMoveX;
  108. let startMoveY;
  109. let startScale;
  110. let startDistance;
  111. let doubleTapTimer;
  112. let touchStartTime;
  113. const onTouchStart = (event) => {
  114. const {
  115. touches
  116. } = event;
  117. const {
  118. offsetX
  119. } = touch;
  120. touch.start(event);
  121. fingerNum = touches.length;
  122. startMoveX = state.moveX;
  123. startMoveY = state.moveY;
  124. touchStartTime = Date.now();
  125. state.moving = fingerNum === 1 && state.scale !== 1;
  126. state.zooming = fingerNum === 2 && !offsetX.value;
  127. if (state.zooming) {
  128. startScale = state.scale;
  129. startDistance = getDistance(event.touches);
  130. }
  131. };
  132. const onTouchMove = (event) => {
  133. const {
  134. touches
  135. } = event;
  136. touch.move(event);
  137. if (state.moving || state.zooming) {
  138. preventDefault(event, true);
  139. }
  140. if (state.moving) {
  141. const {
  142. deltaX,
  143. deltaY
  144. } = touch;
  145. const moveX = deltaX.value + startMoveX;
  146. const moveY = deltaY.value + startMoveY;
  147. state.moveX = clamp(moveX, -maxMoveX.value, maxMoveX.value);
  148. state.moveY = clamp(moveY, -maxMoveY.value, maxMoveY.value);
  149. }
  150. if (state.zooming && touches.length === 2) {
  151. const distance = getDistance(touches);
  152. const scale = startScale * distance / startDistance;
  153. setScale(scale);
  154. }
  155. };
  156. const checkTap = () => {
  157. if (fingerNum > 1) {
  158. return;
  159. }
  160. const {
  161. offsetX,
  162. offsetY
  163. } = touch;
  164. const deltaTime = Date.now() - touchStartTime;
  165. const TAP_TIME = 250;
  166. const TAP_OFFSET = 5;
  167. if (offsetX.value < TAP_OFFSET && offsetY.value < TAP_OFFSET && deltaTime < TAP_TIME) {
  168. if (doubleTapTimer) {
  169. clearTimeout(doubleTapTimer);
  170. doubleTapTimer = null;
  171. toggleScale();
  172. } else {
  173. doubleTapTimer = setTimeout(() => {
  174. emit("close");
  175. doubleTapTimer = null;
  176. }, TAP_TIME);
  177. }
  178. }
  179. };
  180. const onTouchEnd = (event) => {
  181. let stopPropagation = false;
  182. if (state.moving || state.zooming) {
  183. stopPropagation = true;
  184. if (state.moving && startMoveX === state.moveX && startMoveY === state.moveY) {
  185. stopPropagation = false;
  186. }
  187. if (!event.touches.length) {
  188. if (state.zooming) {
  189. state.moveX = clamp(state.moveX, -maxMoveX.value, maxMoveX.value);
  190. state.moveY = clamp(state.moveY, -maxMoveY.value, maxMoveY.value);
  191. state.zooming = false;
  192. }
  193. state.moving = false;
  194. startMoveX = 0;
  195. startMoveY = 0;
  196. startScale = 1;
  197. if (state.scale < 1) {
  198. resetScale();
  199. }
  200. if (state.scale > props.maxZoom) {
  201. state.scale = +props.maxZoom;
  202. }
  203. }
  204. }
  205. preventDefault(event, stopPropagation);
  206. checkTap();
  207. touch.reset();
  208. };
  209. const onLoad = (event) => {
  210. const {
  211. naturalWidth,
  212. naturalHeight
  213. } = event.target;
  214. state.imageRatio = naturalHeight / naturalWidth;
  215. };
  216. watch(() => props.active, resetScale);
  217. watch(() => props.show, (value) => {
  218. if (!value) {
  219. resetScale();
  220. }
  221. });
  222. useEventListener("touchmove", onTouchMove, {
  223. target: computed(() => {
  224. var _a;
  225. return (_a = swipeItem.value) == null ? void 0 : _a.$el;
  226. })
  227. });
  228. return () => {
  229. const imageSlots = {
  230. loading: () => _createVNode(Loading, {
  231. "type": "spinner"
  232. }, null)
  233. };
  234. return _createVNode(SwipeItem, {
  235. "ref": swipeItem,
  236. "class": bem("swipe-item"),
  237. "onTouchstartPassive": onTouchStart,
  238. "onTouchend": onTouchEnd,
  239. "onTouchcancel": onTouchEnd
  240. }, {
  241. default: () => [_createVNode(Image, {
  242. "src": props.src,
  243. "fit": "contain",
  244. "class": bem("image", {
  245. vertical: vertical.value
  246. }),
  247. "style": imageStyle.value,
  248. "onLoad": onLoad
  249. }, imageSlots)]
  250. });
  251. };
  252. }
  253. });
  254. export {
  255. stdin_default as default
  256. };