1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import React, { Component } from 'react';

class ViewerMain extends Component {
constructor(props) {
super(props);
this.imageRef = React.createRef();
this.state = {
dxImg: '',
// 图片移动
isDragging: false,
position: {
x: 0,
y: 0
},
startPos: {
x: 0,
y: 0
},
// 图片缩放
scale: 1,
touchStartDistance: 0,
touchStartScale: 1,
touchStartPosition: { x: 0, y: 0 }
};
this.handleMouseDown = this.handleMouseDown.bind(this);
this.handleMouseMove = this.handleMouseMove.bind(this);
this.handleMouseUp = this.handleMouseUp.bind(this);
this.handleTouchStart = this.handleTouchStart.bind(this);
this.handleTouchMove = this.handleTouchMove.bind(this);
this.handleTouchEnd = this.handleTouchEnd.bind(this);
this.calculateDistance = this.calculateDistance.bind(this);
this.calculateCenter = this.calculateCenter.bind(this);
}
}

// 处理鼠标按下
handleMouseDown (e) {

this.setState({
isDragging: true,
startPos: {
x: e.clientX - this.state.position.x,
y: e.clientY - this.state.position.y
}
});
e.preventDefault();
};

handleMouseUp(e) {
this.setState({ isDragging: false });
}

// 处理鼠标移动
handleMouseMove(e) {
if (!this.state.isDragging) return;

const newX = e.clientX - this.state.startPos.x;
const newY = e.clientY - this.state.startPos.y;

this.setState({
position: { x: newX, y: newY }
}, this.updateImageStyle);
}
}

// 计算两点距离
calculateDistance(touch1, touch2) {
const dx = touch2.clientX - touch1.clientX;
const dy = touch2.clientY - touch1.clientY;
return Math.sqrt(dx * dx + dy * dy);
}

// 计算两点的中心点
calculateCenter(touch1, touch2) {
return {
x: (touch1.clientX + touch2.clientX) / 2,
y: (touch1.clientY + touch2.clientY) / 2
};
}

handleTouchStart(e) {
e.preventDefault();

if (e.touches.length === 1) {
// 单指触摸 - 开始拖拽
this.setState({
isDragging: true,
startPos: {
x: e.touches[0].clientX - this.state.position.x,
y: e.touches[0].clientY - this.state.position.y
}
});
} else if (e.touches.length === 2) {
// 双指触摸 - 开始缩放
const touch1 = e.touches[0];
const touch2 = e.touches[1];

this.setState({
touchStartDistance: this.calculateDistance(touch1, touch2),
touchStartScale: this.state.scale,
touchStartPosition: { ...this.state.position },
startCenter: this.calculateCenter(touch1, touch2)
});
}
}

handleTouchMove(e) {
e.preventDefault();

if (e.touches.length === 1 && this.state.isDragging) {
// 单指移动 - 拖拽
const newX = e.touches[0].clientX - this.state.startPos.x;
const newY = e.touches[0].clientY - this.state.startPos.y;

this.setState({
position: { x: newX, y: newY }
}, this.updateImageStyle);
} else if (e.touches.length === 2) {
// 双指移动 - 缩放
const touch1 = e.touches[0];
const touch2 = e.touches[1];
const currentDistance = this.calculateDistance(touch1, touch2);
const currentMidpoint = this.calculateCenter(touch1, touch2);
if (this.state.touchStartDistance > 0) {
// 计算缩放比例
const distanceRatio = currentDistance / this.state.touchStartDistance;
const newScale = Math.min(Math.max(this.state.touchStartScale * distanceRatio, 0.5), 2);


const image = this.imageRef.current;
const rect = image.getBoundingClientRect();
const offsetX = this.state.startCenter.x - (rect.left + rect.width / 2);
const offsetY = this.state.startCenter.y - (rect.top + rect.height / 2);

// 缩放补偿,保持触点为视觉缩放中心
const newX = this.state.position.x + offsetX * (1 - distanceRatio);;
const newY = this.state.position.y + offsetY * (1 - distanceRatio);

this.setState({
scale: newScale,
position: { x: newX, y: newY }
});
}
}
}

handleTouchEnd(e) {
if (e.touches.length === 0) {
this.setState({
isDragging: false,
touchStartDistance: 0
});
} else if (e.touches.length === 1) {
// 当双指变为单指时,更新拖拽起始点
this.setState({
isDragging: true,
startPos: {
x: e.touches[0].clientX - this.state.position.x,
y: e.touches[0].clientY - this.state.position.y
}
});
}
}
render() {
const { position, scale, } = this.state;
return (
<div style={{
display: 'flex',
justifyContent: 'center',
width: '100%',
height: '100%',
}}>
<img
height="100%"
ref={this.imageRef}
src={dxImg}
style={{
cursor: this.state.isDragging ? 'grabbing' : 'grab',
transform: `translate(${position.x}px, ${position.y}px) scale(${scale})`,
transformOrigin: 'center center'
}}
onMouseDown={this.handleMouseDown}
onMouseMove={this.handleMouseMove}
onTouchStart={this.handleTouchStart}
onTouchMove={this.handleTouchMove}
onTouchEnd={this.handleTouchEnd}
draggable="false" // 防止默认拖拽行为
/>
</div>
)
}
}