浏览代码

暂时移除不必要的依赖

MinJieLiu 7 年之前
父节点
当前提交
e5917a789c
共有 10 个文件被更改,包括 30 次插入103 次删除
  1. 0 5
      examples/simple.css
  2. 0 15
      examples/simple.html
  3. 1 2
      examples/simple.tsx
  4. 2 3
      package.json
  5. 2 2
      src/Photo.tsx
  6. 20 35
      src/PhotoView.tsx
  7. 0 11
      src/types.ts
  8. 2 4
      src/utils/slideToPosition.ts
  9. 2 7
      src/utils/slideToSuitableOffset.ts
  10. 1 19
      src/variables.ts

+ 0 - 5
examples/simple.css

@@ -1,5 +0,0 @@
-* {
-  margin: 0;
-  padding: 0;
-  box-sizing: border-box;
-}

+ 0 - 15
examples/simple.html

@@ -1,15 +0,0 @@
-<!DOCTYPE html>
-<html lang="zh-CN">
-<head>
-  <meta charset="UTF-8">
-  <title>React 图片预览组件 - Example</title>
-  <meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1" />
-  <meta name="renderer" content="webkit" />
-  <meta name="viewport" content="width=device-width,user-scalable=no,initial-scale=1,maximum-scale=1,minimum-scale=1">
-  <link href="./simple.css" type="text/css" rel="stylesheet" />
-</head>
-<body>
-<div id="root"></div>
-<script src="simple.js"></script>
-</body>
-</html>

+ 1 - 2
examples/simple.tsx

@@ -10,7 +10,6 @@ const Container = styled.div`
 const Header = styled.header`
   padding: 40px;
   font-size: 32px;
-  height: 1000px;
   border-bottom: 1px solid #ccc;
 `;
 
@@ -54,4 +53,4 @@ class Example extends React.Component {
   }
 }
 
-ReactDOM.render(<Example />, document.getElementById('root'));
+ReactDOM.render(<Example />, document.getElementById('__react-content'));

+ 2 - 3
package.json

@@ -1,7 +1,7 @@
 {
   "name": "react-photo-view",
-  "version": "0.1.3",
-  "description": "React photo preview",
+  "version": "0.1.4",
+  "description": "React photo preview.",
   "main": "./lib",
   "module": "./es",
   "keywords": [
@@ -47,7 +47,6 @@
   "dependencies": {
     "lodash.throttle": "^4.1.1",
     "lodash.uniqueid": "^4.0.1",
-    "react-motion": "^0.5.2",
     "styled-components": "^3.0.1"
   }
 }

+ 2 - 2
src/Photo.tsx

@@ -22,10 +22,10 @@ type PhotoState = {
 
 const PhotoImage = styled.img`
   will-change: transform;
-  cursor: -webkit-grab;
+  cursor: grab;
 
   &:active {
-    cursor: -webkit-grabbing;
+    cursor: grabbing;
   }
 `;
 

+ 20 - 35
src/PhotoView.tsx

@@ -1,5 +1,4 @@
 import React from 'react';
-import { Motion, spring } from 'react-motion';
 import throttle from 'lodash.throttle';
 import Photo from './Photo';
 import PhotoWrap from './components/PhotoWrap';
@@ -10,7 +9,6 @@ import getPositionOnMoveOrScale from './utils/getPositionOnMoveOrScale';
 import slideToSuitableOffset from './utils/slideToSuitableOffset';
 import { getClosedHorizontal, getClosedVertical } from './utils/getCloseEdge';
 import {
-  defaultAnimationConfig,
   minReachOffset,
   minScale,
   maxScale,
@@ -75,8 +73,6 @@ const initialState = {
   touchedTime: 0,
   // 多指触控间距
   lastTouchLength: 0,
-  // 动画类型
-  animation: defaultAnimationConfig,
 
   // 当前边缘触发状态,0: 未触发,1: x 轴,2: y 轴
   reachState: 0,
@@ -210,7 +206,6 @@ export default class PhotoView extends React.Component<
           // 若图片足够大,则放大适应的倍数
           toScale: scale !== 1 ? 1 : Math.max(2, naturalWidth / width),
         }),
-        animation: defaultAnimationConfig,
       };
     });
   }
@@ -240,7 +235,6 @@ export default class PhotoView extends React.Component<
           fromScale: scale,
           toScale,
         }),
-        animation: defaultAnimationConfig,
       };
     });
   }
@@ -341,7 +335,6 @@ export default class PhotoView extends React.Component<
             }) : {
               x,
               y,
-              animation: defaultAnimationConfig,
             },
         };
       });
@@ -439,7 +432,9 @@ export default class PhotoView extends React.Component<
       loadingElement,
       brokenElement,
     } = this.props;
-    const { x, y, scale, touched, animation } = this.state;
+    const { x, y, scale, touched } = this.state;
+
+    const transform = `translate3d(${x}px, ${y}px, 0) scale(${scale * photoScale})`;
 
     return (
       <PhotoWrap className={wrapClassName} style={style}>
@@ -447,35 +442,25 @@ export default class PhotoView extends React.Component<
           onMouseDown={isMobile ? undefined : this.handleMaskMouseDown}
           onTouchStart={isMobile ? this.handleMaskTouchStart : undefined}
         />
-        <Motion
+        <Photo
+          className={className}
+          src={src}
+          ref={this.handlePhotoRef}
+          onDoubleClick={this.handleDoubleClick}
+          onMouseDown={isMobile ? undefined : this.handleMouseDown}
+          onTouchStart={isMobile ? this.handleTouchStart : undefined}
+          onWheel={this.handleWheel}
+          onPhotoResize={this.handleResize}
           style={{
-            currX: touched ? x : spring(x, animation),
-            currY: touched ? y : spring(y, animation),
-            currScale: touched ? scale : spring(scale, animation),
-          }}
-        >
-          {({ currX, currY, currScale }) => {
-            const transform = `translate3d(${currX}px, ${currY}px, 0) scale(${currScale * photoScale})`;
-            return (
-              <Photo
-                className={className}
-                src={src}
-                ref={this.handlePhotoRef}
-                onDoubleClick={this.handleDoubleClick}
-                onMouseDown={isMobile ? undefined : this.handleMouseDown}
-                onTouchStart={isMobile ? this.handleTouchStart : undefined}
-                onWheel={this.handleWheel}
-                onPhotoResize={this.handleResize}
-                style={{
-                  WebkitTransform: transform,
-                  transform,
-                }}
-                loadingElement={loadingElement}
-                brokenElement={brokenElement}
-              />
-            );
+            WebkitTransform: transform,
+            transform,
+            transition: touched
+              ? undefined
+              : 'transform 0.5s cubic-bezier(0.25, 0.8, 0.25, 1)',
           }}
-        </Motion>
+          loadingElement={loadingElement}
+          brokenElement={brokenElement}
+        />
       </PhotoWrap>
     );
   }

+ 0 - 11
src/types.ts

@@ -1,14 +1,3 @@
-export type springType = {
-  // 刚性
-  stiffness: number;
-  // 减震
-  damping: number;
-};
-
-export type animationType = {
-  animation: springType;
-};
-
 /**
  * 图片 item 类型
  */

+ 2 - 4
src/utils/slideToPosition.ts

@@ -1,5 +1,4 @@
-import { animationType } from '../types';
-import { maxTouchTime, slideAnimationConfig } from '../variables';
+import { maxTouchTime } from '../variables';
 
 /**
  * 根据速度滑动至目标位置
@@ -19,7 +18,7 @@ const slideToPosition = ({
 }): {
   endX: number;
   endY: number;
-} & animationType => {
+} => {
   const moveTime = Date.now() - touchedTime;
   const speedX = (x - lastX) / moveTime;
   const speedY = (y - lastY) / moveTime;
@@ -28,7 +27,6 @@ const slideToPosition = ({
   return {
     endX: Math.floor(x + speedX * slideTime),
     endY: Math.floor(y + speedY * slideTime),
-    animation: slideAnimationConfig,
   };
 };
 

+ 2 - 7
src/utils/slideToSuitableOffset.ts

@@ -1,5 +1,3 @@
-import { animationType } from '../types';
-import { defaultAnimationConfig } from '../variables';
 import slideToPosition from './slideToPosition';
 import { getClosedHorizontal, getClosedVertical } from './getCloseEdge';
 
@@ -27,9 +25,9 @@ const slideToSuitableOffset = ({
 }): {
   x: number;
   y: number;
-} & animationType => {
+} => {
   // 滑动到结果的位置
-  const { endX, endY, animation } = slideToPosition({
+  const { endX, endY } = slideToPosition({
     x,
     y,
     lastX,
@@ -65,12 +63,9 @@ const slideToSuitableOffset = ({
     currentY = -outOffsetY;
   }
 
-  const isClosedEdge = horizontalType !== 0 || verticalType !== 0;
-
   return {
     x: currentX,
     y: currentY,
-    animation: isClosedEdge ? defaultAnimationConfig : animation,
   };
 };
 

+ 1 - 19
src/variables.ts

@@ -1,5 +1,3 @@
-import { springType } from './types';
-
 /**
  * 最大触摸时间
  */
@@ -31,7 +29,7 @@ export const defaultOpacity: number = 0.8;
 export const minScale: number = 1;
 
 /**
- * 最大缩放度(若图片足够大,则会被忽略
+ * 最大缩放度(若图片足够大,则会超出
  */
 export const maxScale: number = 6;
 
@@ -39,19 +37,3 @@ export const maxScale: number = 6;
  * 缩放弹性缓冲
  */
 export const scaleBuffer = 0.2;
-
-/**
- * 默认动画参数
- */
-export const defaultAnimationConfig: springType = {
-  stiffness: 240,
-  damping: 30,
-};
-
-/**
- * 滑动时动画参数
- */
-export const slideAnimationConfig: springType = {
-  stiffness: 170,
-  damping: 32,
-};