Skip to content

Commit 65ffbb9

Browse files
committed
feat(image-viewer): 优化preview
1 parent ee7483f commit 65ffbb9

File tree

2 files changed

+33
-33
lines changed

2 files changed

+33
-33
lines changed

src/image-viewer/image-viewer.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ const ImageViewer: React.FC<ImageViewerProps> = (props) => {
5050
MAX_SCALE,
5151
undefined,
5252
// onTransform,
53-
currentIndex,
53+
// currentIndex,
5454
);
5555
const { isTouching, onTouchStart, onTouchMove, onTouchEnd } = useTouchEvent(
5656
imgRefs,

src/image-viewer/transform.ts

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useRef, useState } from 'react';
22
import raf from './raf';
3-
import { getClientSize } from './util';
3+
// import { getClientSize } from './util';
44

55
function isEqual(objA, objB) {
66
const keys = Object.keys(objA);
@@ -63,7 +63,7 @@ export function useImageTransform(
6363
minScale: number,
6464
maxScale: number,
6565
onTransform?: (info: { transform: TransformType; action: TransformAction }) => void,
66-
currentIndex?: number,
66+
// currentIndex?: number,
6767
) {
6868
const frame = useRef(null);
6969
const queue = useRef<TransformType[]>([]);
@@ -101,57 +101,57 @@ export function useImageTransform(
101101

102102
/** Scale according to the position of centerX and centerY */
103103
const dispatchZoomChange: DispatchZoomChangeFunc = (ratio, action, centerX?, centerY?, isTouch?) => {
104-
const { width, height, offsetWidth, offsetHeight, offsetLeft, offsetTop } = imgRef.current[currentIndex];
104+
// const { width, height, offsetWidth, offsetHeight, offsetLeft, offsetTop } = imgRef.current[currentIndex];
105105

106-
let newRatio = ratio;
106+
// let newRatio = ratio;
107107
let newScale = transform.scale * ratio;
108108
if (newScale > maxScale) {
109109
newScale = maxScale;
110-
newRatio = maxScale / transform.scale;
110+
// newRatio = maxScale / transform.scale;
111111
} else if (newScale < minScale) {
112112
// For mobile interactions, allow scaling down to the minimum scale.
113113
newScale = isTouch ? newScale : minScale;
114-
newRatio = newScale / transform.scale;
114+
// newRatio = newScale / transform.scale;
115115
}
116116

117117
/** Default center point scaling */
118-
const mergedCenterX = centerX ?? innerWidth / 2;
119-
const mergedCenterY = centerY ?? innerHeight / 2;
120-
121-
const diffRatio = newRatio - 1;
122-
/** Deviation calculated from image size */
123-
const diffImgX = diffRatio * width * 0.5;
124-
const diffImgY = diffRatio * height * 0.5;
125-
/** The difference between the click position and the edge of the document */
126-
const diffOffsetLeft = diffRatio * (mergedCenterX - transform.x - offsetLeft);
127-
const diffOffsetTop = diffRatio * (mergedCenterY - transform.y - offsetTop);
128-
/** Final positioning */
129-
let newX = transform.x - (diffOffsetLeft - diffImgX);
130-
let newY = transform.y - (diffOffsetTop - diffImgY);
118+
// const mergedCenterX = centerX ?? innerWidth / 2;
119+
// const mergedCenterY = centerY ?? innerHeight / 2;
120+
121+
// const diffRatio = newRatio - 1;
122+
// /** Deviation calculated from image size */
123+
// const diffImgX = diffRatio * width * 0.5;
124+
// const diffImgY = diffRatio * height * 0.5;
125+
// /** The difference between the click position and the edge of the document */
126+
// const diffOffsetLeft = diffRatio * (mergedCenterX - transform.x - offsetLeft);
127+
// const diffOffsetTop = diffRatio * (mergedCenterY - transform.y - offsetTop);
128+
// /** Final positioning */
129+
// let newX = transform.x - (diffOffsetLeft - diffImgX);
130+
// let newY = transform.y - (diffOffsetTop - diffImgY);
131131

132132
/**
133133
* When zooming the image
134134
* When the image size is smaller than the width and height of the window, the position is initialized
135135
*/
136-
if (ratio < 1 && newScale === 1) {
137-
const mergedWidth = offsetWidth * newScale;
138-
const mergedHeight = offsetHeight * newScale;
139-
const { width: clientWidth, height: clientHeight } = getClientSize();
140-
if (mergedWidth <= clientWidth && mergedHeight <= clientHeight) {
141-
newX = 0;
142-
newY = 0;
143-
}
144-
}
136+
// if (ratio < 1 && newScale === 1) {
137+
// const mergedWidth = offsetWidth * newScale;
138+
// const mergedHeight = offsetHeight * newScale;
139+
// const { width: clientWidth, height: clientHeight } = getClientSize();
140+
// if (mergedWidth <= clientWidth && mergedHeight <= clientHeight) {
141+
// newX = 0;
142+
// newY = 0;
143+
// }
144+
// }
145145

146146
if (action === 'doubleClick') {
147-
newX = 0;
148-
newY = 0;
147+
// newX = 0;
148+
// newY = 0;
149149
}
150150

151151
updateTransform(
152152
{
153-
x: newX,
154-
y: newY,
153+
// x: newX,
154+
// y: newY,
155155
scale: newScale,
156156
},
157157
action,

0 commit comments

Comments
 (0)