PhotoView.tsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. import React from 'react';
  2. import { Motion, spring } from 'react-motion';
  3. import throttle from 'lodash.throttle';
  4. import Photo from './Photo';
  5. import { PhotoContainer, Backdrop } from './StyledElements';
  6. import {
  7. isMobile,
  8. getMultipleTouchPosition,
  9. getPositionOnMoveOrScale,
  10. slideToSuitableOffset,
  11. } from './utils';
  12. import { defaultAnimationConfig } from './variables';
  13. import { animationType } from './types';
  14. export interface IPhotoViewProps {
  15. src: string;
  16. }
  17. type PhotoViewState = {
  18. // 图片 X 偏移量
  19. x: number;
  20. // 图片 y 偏移量
  21. y: number;
  22. // 图片缩放程度
  23. scale: number;
  24. // 图片处于触摸的状态
  25. touched: boolean;
  26. // 触摸开始时 x 原始坐标
  27. pageX: number;
  28. // 触摸开始时 y 原始坐标
  29. pageY: number;
  30. // 触摸开始时图片 x 偏移量
  31. lastX: number;
  32. // 触摸开始时图片 y 偏移量
  33. lastY: number;
  34. // 多指触控间距
  35. lastTouchLength: number;
  36. // 触摸开始时时间
  37. touchedTime: number;
  38. } & animationType;
  39. export default class PhotoView extends React.Component<
  40. IPhotoViewProps,
  41. PhotoViewState
  42. > {
  43. static displayName = 'PhotoView';
  44. readonly state = {
  45. x: 0,
  46. y: 0,
  47. scale: 1,
  48. touched: false,
  49. pageX: 0,
  50. pageY: 0,
  51. lastX: 0,
  52. lastY: 0,
  53. originX: 0,
  54. originY: 0,
  55. originTranslateX: 0,
  56. originTranslateY: 0,
  57. touchedTime: 0,
  58. lastTouchLength: 0,
  59. animation: defaultAnimationConfig,
  60. };
  61. private photoRef;
  62. constructor(props) {
  63. super(props);
  64. this.handleMove = throttle(this.handleMove, 8);
  65. }
  66. componentDidMount() {
  67. if (isMobile) {
  68. window.addEventListener('touchmove', this.handleTouchMove);
  69. window.addEventListener('touchend', this.handleTouchEnd);
  70. } else {
  71. window.addEventListener('mousemove', this.handleMouseMove);
  72. window.addEventListener('mouseup', this.handleMouseUp);
  73. }
  74. }
  75. componentWillUnmount() {
  76. if (isMobile) {
  77. window.removeEventListener('touchmove', this.handleTouchMove);
  78. window.removeEventListener('touchend', this.handleTouchEnd);
  79. } else {
  80. window.removeEventListener('mousemove', this.handleMouseMove);
  81. window.removeEventListener('mouseup', this.handleMouseUp);
  82. }
  83. }
  84. handleStart = (pageX: number, pageY: number, touchLength: number = 0) => {
  85. this.setState(prevState => ({
  86. touched: true,
  87. pageX,
  88. pageY,
  89. lastX: prevState.x,
  90. lastY: prevState.y,
  91. lastTouchLength: touchLength,
  92. touchedTime: Date.now(),
  93. }));
  94. }
  95. handleMove = (newPageX: number, newPageY: number, touchLength: number = 0) => {
  96. if (this.state.touched) {
  97. this.setState(({
  98. pageX,
  99. pageY,
  100. lastX,
  101. lastY,
  102. scale,
  103. lastTouchLength
  104. }) => {
  105. return {
  106. lastTouchLength: touchLength,
  107. ...getPositionOnMoveOrScale({
  108. x: newPageX - pageX + lastX,
  109. y: newPageY - pageY + lastY,
  110. pageX,
  111. pageY,
  112. fromScale: scale,
  113. toScale: scale + (touchLength - lastTouchLength) / (window.innerHeight / 2) * scale,
  114. }),
  115. };
  116. });
  117. }
  118. }
  119. handleDoubleClick = (e) => {
  120. const { pageX, pageY } = e;
  121. this.setState(({ x, y, scale }) => {
  122. return {
  123. pageX,
  124. pageY,
  125. ...getPositionOnMoveOrScale({
  126. x,
  127. y,
  128. pageX,
  129. pageY,
  130. fromScale: scale,
  131. toScale: scale !== 1 ? 1 : 2,
  132. }),
  133. animation: defaultAnimationConfig,
  134. };
  135. });
  136. }
  137. handleWheel = (e) => {
  138. const { pageX, pageY, deltaY } = e;
  139. this.setState(({ x, y, scale }) => {
  140. return {
  141. pageX,
  142. pageY,
  143. ...getPositionOnMoveOrScale({
  144. x,
  145. y,
  146. pageX,
  147. pageY,
  148. fromScale: scale,
  149. toScale: scale - deltaY / 100 / 2,
  150. }),
  151. animation: defaultAnimationConfig,
  152. };
  153. });
  154. }
  155. handleTouchStart = e => {
  156. if (e.touches.length >= 2) {
  157. const { pageX, pageY, touchLength } = getMultipleTouchPosition(e);
  158. this.handleStart(pageX, pageY, touchLength);
  159. } else {
  160. const { pageX, pageY } = e.touches[0];
  161. this.handleStart(pageX, pageY);
  162. }
  163. }
  164. handleMouseDown = e => {
  165. this.handleStart(e.pageX, e.pageY);
  166. }
  167. handleTouchMove = e => {
  168. e.preventDefault();
  169. if (e.touches.length >= 2) {
  170. const { pageX, pageY, touchLength } = getMultipleTouchPosition(e);
  171. this.handleMove(pageX, pageY, touchLength);
  172. } else {
  173. const { pageX, pageY } = e.touches[0];
  174. this.handleMove(pageX, pageY);
  175. }
  176. }
  177. handleMouseMove = e => {
  178. e.preventDefault();
  179. this.handleMove(e.pageX, e.pageY);
  180. }
  181. handleUp = (newPageX: number, newPageY: number) => {
  182. const { width, height } = this.photoRef.state;
  183. this.setState(({
  184. x,
  185. y,
  186. lastX,
  187. lastY,
  188. scale,
  189. touchedTime,
  190. pageX,
  191. pageY,
  192. }) => {
  193. const hasMove = pageX !== newPageX || pageY !== newPageY;
  194. return {
  195. touched: false,
  196. ...slideToSuitableOffset({
  197. x,
  198. y,
  199. lastX,
  200. lastY,
  201. width,
  202. height,
  203. scale,
  204. touchedTime,
  205. hasMove,
  206. }),
  207. };
  208. });
  209. }
  210. handleTouchEnd = (e) => {
  211. const { pageX, pageY } = e.changedTouches[0];
  212. this.handleUp(pageX, pageY);
  213. }
  214. handleMouseUp = (e) => {
  215. const { pageX, pageY } = e;
  216. this.handleUp(pageX, pageY);
  217. }
  218. handlePhotoRef = (ref) => {
  219. this.photoRef = ref;
  220. }
  221. render() {
  222. const { src } = this.props;
  223. const { x, y, scale, touched, animation } = this.state;
  224. const style = {
  225. currX: touched ? x : spring(x, animation),
  226. currY: touched ? y : spring(y, animation),
  227. currScale: touched ? scale : spring(scale, animation),
  228. };
  229. return (
  230. <PhotoContainer>
  231. <Backdrop />
  232. <Motion style={style}>
  233. {({ currX, currY, currScale }) => {
  234. const transform = `translate3d(${currX}px, ${currY}px, 0) scale(${currScale})`;
  235. return (
  236. <Photo
  237. src={src}
  238. ref={this.handlePhotoRef}
  239. onDoubleClick={this.handleDoubleClick}
  240. onMouseDown={isMobile ? undefined : this.handleMouseDown}
  241. onTouchStart={isMobile ? this.handleTouchStart : undefined}
  242. onWheel={this.handleWheel}
  243. style={{
  244. WebkitTransform: transform,
  245. transform,
  246. }}
  247. />
  248. );
  249. }}
  250. </Motion>
  251. </PhotoContainer>
  252. );
  253. }
  254. }