浏览代码

小调整

MinJieLiu 7 年之前
父节点
当前提交
2d7f571d93
共有 2 个文件被更改,包括 26 次插入24 次删除
  1. 10 2
      src/PhotoView.tsx
  2. 16 22
      src/utils.ts

+ 10 - 2
src/PhotoView.tsx

@@ -28,6 +28,8 @@ type PhotoViewState = {
   lastX: number;
   lastX: number;
   // 触摸开始时图片 y 偏移量
   // 触摸开始时图片 y 偏移量
   lastY: number;
   lastY: number;
+  originX: number;
+  originY: number;
   // 触摸开始时时间
   // 触摸开始时时间
   touchedTime: number;
   touchedTime: number;
 } & animationType;
 } & animationType;
@@ -50,6 +52,8 @@ export default class PhotoView extends React.Component<
     lastX: 0,
     lastX: 0,
     lastY: 0,
     lastY: 0,
 
 
+    originX: 0,
+    originY: 0,
     touchedTime: 0,
     touchedTime: 0,
     animation: defaultAnimationConfig,
     animation: defaultAnimationConfig,
   };
   };
@@ -100,7 +104,7 @@ export default class PhotoView extends React.Component<
 
 
   handleDoubleClick = (e) => {
   handleDoubleClick = (e) => {
     const { pageX, pageY } = e;
     const { pageX, pageY } = e;
-    this.setState(({ x, y, scale }) => {
+    this.setState(({ x, y, scale, originX, originY }) => {
       return {
       return {
         pageX,
         pageX,
         pageY,
         pageY,
@@ -109,6 +113,8 @@ export default class PhotoView extends React.Component<
           y,
           y,
           pageX,
           pageX,
           pageY,
           pageY,
+          originX,
+          originY,
           fromScale: scale,
           fromScale: scale,
           toScale: scale > 1 ? 1 : 2,
           toScale: scale > 1 ? 1 : 2,
         }),
         }),
@@ -118,7 +124,7 @@ export default class PhotoView extends React.Component<
 
 
   handleWheel = (e) => {
   handleWheel = (e) => {
     const { pageX, pageY, deltaY } = e;
     const { pageX, pageY, deltaY } = e;
-    this.setState(({ x, y, scale }) => {
+    this.setState(({ x, y, scale, originX, originY }) => {
       return {
       return {
         pageX,
         pageX,
         pageY,
         pageY,
@@ -127,6 +133,8 @@ export default class PhotoView extends React.Component<
           y,
           y,
           pageX,
           pageX,
           pageY,
           pageY,
+          originX,
+          originY,
           fromScale: scale,
           fromScale: scale,
           toScale: scale - deltaY / 100 / 2,
           toScale: scale - deltaY / 100 / 2,
         }),
         }),

+ 16 - 22
src/utils.ts

@@ -48,6 +48,8 @@ export const getPositionOnScale = ({
   y,
   y,
   pageX,
   pageX,
   pageY,
   pageY,
+  originX,
+  originY,
   fromScale,
   fromScale,
   toScale,
   toScale,
 }: {
 }: {
@@ -55,39 +57,31 @@ export const getPositionOnScale = ({
   y: number;
   y: number;
   pageX: number;
   pageX: number;
   pageY: number;
   pageY: number;
+  originX: number;
+  originY: number;
   fromScale: number;
   fromScale: number;
   toScale: number;
   toScale: number;
 }): {
 }): {
   x: number;
   x: number;
   y: number;
   y: number;
+  originX: number;
+  originY: number;
   scale: number;
   scale: number;
 } => {
 } => {
   const { innerWidth, innerHeight } = window;
   const { innerWidth, innerHeight } = window;
-  let endScale = toScale;
-  let distanceX = x;
-  let distanceY = y;
-  // 缩放限制
-  if (toScale < 0.5) {
-    endScale = 0.5;
-  } else if (toScale > 5) {
-    endScale = 5;
-  } else {
-    // 缩放距离计算
-    const centerPageX = pageX - innerWidth / 2;
-    const centerPageY = pageY - innerHeight / 2;
-
-    const touchedImageX = (centerPageX - x) / fromScale;
-    const touchedImageY = (centerPageY - y) / fromScale;
+  // 缩放距离计算
+  const imageCenterX = innerWidth / 2 + x;
+  const imageCenterY = innerHeight / 2 + y;
 
 
-    const scale = endScale - fromScale;
+  const offsetX = pageX - imageCenterX;
+  const offsetY = pageY - imageCenterY;
 
 
-    distanceX = x - centerPageX * scale;
-    distanceY = y - centerPageY * scale;
-  }
   return {
   return {
-    x: distanceX,
-    y: distanceY,
-    scale: endScale,
+    x: x + (offsetX - originX * fromScale),
+    y: y + (offsetY - originY * fromScale),
+    scale: Math.max(Math.min(toScale, 5), 1),
+    originX: offsetX / fromScale,
+    originY: offsetY / fromScale,
   };
   };
 };
 };