PhotoView.tsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. touchedTime: 0,
  54. lastTouchLength: 0,
  55. animation: defaultAnimationConfig,
  56. };
  57. private photoRef;
  58. constructor(props) {
  59. super(props);
  60. this.handleMove = throttle(this.handleMove, 8);
  61. }
  62. componentDidMount() {
  63. if (isMobile) {
  64. window.addEventListener('touchmove', this.handleTouchMove);
  65. window.addEventListener('touchend', this.handleTouchEnd);
  66. } else {
  67. window.addEventListener('mousemove', this.handleMouseMove);
  68. window.addEventListener('mouseup', this.handleMouseUp);
  69. }
  70. }
  71. componentWillUnmount() {
  72. if (isMobile) {
  73. window.removeEventListener('touchmove', this.handleTouchMove);
  74. window.removeEventListener('touchend', this.handleTouchEnd);
  75. } else {
  76. window.removeEventListener('mousemove', this.handleMouseMove);
  77. window.removeEventListener('mouseup', this.handleMouseUp);
  78. }
  79. }
  80. handleStart = (pageX: number, pageY: number, touchLength: number = 0) => {
  81. this.setState(prevState => ({
  82. touched: true,
  83. pageX,
  84. pageY,
  85. lastX: prevState.x,
  86. lastY: prevState.y,
  87. lastTouchLength: touchLength,
  88. touchedTime: Date.now(),
  89. }));
  90. }
  91. handleMove = (newPageX: number, newPageY: number, touchLength: number = 0) => {
  92. if (this.state.touched) {
  93. this.setState(({
  94. pageX,
  95. pageY,
  96. lastX,
  97. lastY,
  98. scale,
  99. lastTouchLength,
  100. }) => {
  101. const toScale = scale + (touchLength - lastTouchLength) / 100 / 2 * scale;
  102. return {
  103. lastTouchLength: touchLength,
  104. ...getPositionOnMoveOrScale({
  105. x: newPageX - pageX + lastX,
  106. y: newPageY - pageY + lastY,
  107. pageX,
  108. pageY,
  109. fromScale: scale,
  110. toScale,
  111. }),
  112. };
  113. });
  114. }
  115. }
  116. handleDoubleClick = (e) => {
  117. const { pageX, pageY } = e;
  118. this.setState(({ x, y, scale }) => {
  119. return {
  120. pageX,
  121. pageY,
  122. ...getPositionOnMoveOrScale({
  123. x,
  124. y,
  125. pageX,
  126. pageY,
  127. fromScale: scale,
  128. toScale: scale !== 1 ? 1 : 2,
  129. }),
  130. animation: defaultAnimationConfig,
  131. };
  132. });
  133. }
  134. handleWheel = (e) => {
  135. const { pageX, pageY, deltaY } = e;
  136. this.setState(({ x, y, scale }) => {
  137. return {
  138. pageX,
  139. pageY,
  140. ...getPositionOnMoveOrScale({
  141. x,
  142. y,
  143. pageX,
  144. pageY,
  145. fromScale: scale,
  146. toScale: scale - deltaY / 100 / 2,
  147. }),
  148. animation: defaultAnimationConfig,
  149. };
  150. });
  151. }
  152. handleTouchStart = e => {
  153. if (e.touches.length >= 2) {
  154. const { pageX, pageY, touchLength } = getMultipleTouchPosition(e);
  155. this.handleStart(pageX, pageY, touchLength);
  156. } else {
  157. const { pageX, pageY } = e.touches[0];
  158. this.handleStart(pageX, pageY);
  159. }
  160. }
  161. handleMouseDown = e => {
  162. this.handleStart(e.pageX, e.pageY);
  163. }
  164. handleTouchMove = e => {
  165. e.preventDefault();
  166. if (e.touches.length >= 2) {
  167. const { pageX, pageY, touchLength } = getMultipleTouchPosition(e);
  168. this.handleMove(pageX, pageY, touchLength);
  169. } else {
  170. const { pageX, pageY } = e.touches[0];
  171. this.handleMove(pageX, pageY);
  172. }
  173. }
  174. handleMouseMove = e => {
  175. e.preventDefault();
  176. this.handleMove(e.pageX, e.pageY);
  177. }
  178. handleUp = (newPageX: number, newPageY: number) => {
  179. const { width, height } = this.photoRef.state;
  180. this.setState(({
  181. x,
  182. y,
  183. lastX,
  184. lastY,
  185. scale,
  186. touchedTime,
  187. pageX,
  188. pageY,
  189. }) => {
  190. const hasMove = pageX !== newPageX || pageY !== newPageY;
  191. return {
  192. touched: false,
  193. ...slideToSuitableOffset({
  194. x,
  195. y,
  196. lastX,
  197. lastY,
  198. width,
  199. height,
  200. scale,
  201. touchedTime,
  202. hasMove,
  203. }),
  204. };
  205. });
  206. }
  207. handleTouchEnd = (e) => {
  208. const { pageX, pageY } = e.changedTouches[0];
  209. this.handleUp(pageX, pageY);
  210. }
  211. handleMouseUp = (e) => {
  212. const { pageX, pageY } = e;
  213. this.handleUp(pageX, pageY);
  214. }
  215. handlePhotoRef = (ref) => {
  216. this.photoRef = ref;
  217. }
  218. render() {
  219. const { src } = this.props;
  220. const { x, y, scale, touched, animation } = this.state;
  221. const style = {
  222. currX: touched ? x : spring(x, animation),
  223. currY: touched ? y : spring(y, animation),
  224. currScale: touched ? scale : spring(scale, animation),
  225. };
  226. return (
  227. <PhotoContainer>
  228. <Backdrop />
  229. <Motion style={style}>
  230. {({ currX, currY, currScale }) => {
  231. const transform = `translate3d(${currX}px, ${currY}px, 0) scale(${currScale})`;
  232. return (
  233. <Photo
  234. src={src}
  235. ref={this.handlePhotoRef}
  236. onDoubleClick={this.handleDoubleClick}
  237. onMouseDown={isMobile ? undefined : this.handleMouseDown}
  238. onTouchStart={isMobile ? this.handleTouchStart : undefined}
  239. onWheel={this.handleWheel}
  240. style={{
  241. WebkitTransform: transform,
  242. transform,
  243. }}
  244. />
  245. );
  246. }}
  247. </Motion>
  248. </PhotoContainer>
  249. );
  250. }
  251. }