-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzoomAndPan.js
214 lines (187 loc) · 6.04 KB
/
zoomAndPan.js
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
export function zoomAndPan(
PARAMS,
selectorContainer,
selectorMovable,
options = {},
) {
const container = document.querySelector(selectorContainer)
const movable = container.querySelector(selectorMovable)
movable.style.transformOrigin = '0 0'
movable.style.position = 'absolute'
let constantSizeElements = options.constantSizeElements || []
let scale = options.scale || 1
let panning = false
let startPoint = {x: 0, y: 0}
let endPoint = {x: 0, y: 0}
let lastDistance = 0
let isSpacePressed = false
let lastTouchDistance = 0
// Параметры ограничений
const MIN_SCALE = options.minScale || 0.02
const MAX_SCALE = options.maxScale || 10
// Zoom and pan functionality
container.addEventListener('wheel', handleWheel, {passive: false})
// Pan functionality
container.addEventListener('mousedown', handleMouseDown)
container.addEventListener('mousemove', handleMouseMove)
container.addEventListener('mouseup', handleMouseUp)
container.addEventListener('mouseleave', handleMouseUp)
// Touch events for mobile devices
container.addEventListener('touchstart', handleTouchStart, {passive: false})
container.addEventListener('touchmove', handleTouchMove, {passive: false})
container.addEventListener('touchend', handleTouchEnd)
// Keyboard events
window.addEventListener('keydown', handleKeyDown)
window.addEventListener('keyup', handleKeyUp)
function handleWheel(e) {
e.preventDefault()
if (e.ctrlKey || e.metaKey) {
// Zoom
const delta = e.deltaY > 0 ? 1 - PARAMS.zoomSpeed : 1 + PARAMS.zoomSpeed
zoomAround(e.clientX, e.clientY, delta)
} else {
// Pan
const newEndPoint = {
x: endPoint.x - e.deltaX,
y: endPoint.y - e.deltaY,
}
let [isXValid, isYValid] = isPositionValid(newEndPoint)
if (isXValid) {
endPoint.x = newEndPoint.x
}
if (isYValid) {
endPoint.y = newEndPoint.y
}
updateTransform()
}
}
function handleMouseDown(e) {
if (e.button !== 0) return // Only left mouse button
e.preventDefault()
startPanning(e.clientX, e.clientY)
}
function handleMouseMove(e) {
if (panning) {
e.preventDefault()
movePanning(e.clientX, e.clientY)
}
}
function handleMouseUp() {
panning = false
}
function handleTouchStart(e) {
e.preventDefault()
if (e.touches.length === 2) {
lastTouchDistance = getDistance(e.touches[0], e.touches[1])
} else if (e.touches.length === 1) {
startPanning(e.touches[0].clientX, e.touches[0].clientY)
}
}
function handleTouchMove(e) {
e.preventDefault()
if (e.touches.length === 2) {
const newDistance = getDistance(e.touches[0], e.touches[1])
const delta = newDistance / lastTouchDistance
zoomAround(
(e.touches[0].clientX + e.touches[1].clientX) / 2,
(e.touches[0].clientY + e.touches[1].clientY) / 2,
delta,
)
lastTouchDistance = newDistance
} else if (e.touches.length === 1) {
movePanning(e.touches[0].clientX, e.touches[0].clientY)
}
}
function handleTouchEnd() {
panning = false
lastTouchDistance = 0
}
function handleKeyDown(e) {
if (e.code === 'Space') {
isSpacePressed = true
container.style.cursor = 'grab'
}
}
function handleKeyUp(e) {
if (e.code === 'Space') {
isSpacePressed = false
container.style.cursor = 'default'
}
}
function startPanning(clientX, clientY) {
panning = true
startPoint = {x: clientX - endPoint.x, y: clientY - endPoint.y}
}
function movePanning(clientX, clientY) {
const newEndPoint = {
x: clientX - startPoint.x,
y: clientY - startPoint.y,
}
let [isXValid, isYValid] = isPositionValid(newEndPoint)
if (isXValid) {
endPoint.x = newEndPoint.x
}
if (isYValid) {
endPoint.y = newEndPoint.y
}
updateTransform()
}
function zoomAround(clientX, clientY, delta) {
const newScale = clamp(scale * delta, MIN_SCALE, MAX_SCALE)
if (newScale !== scale) {
const oldScale = scale
scale = newScale
const rect = movable.getBoundingClientRect()
const mouseX = clientX - rect.left
const mouseY = clientY - rect.top
const newEndPoint = {
x: endPoint.x + mouseX * (1 - delta),
y: endPoint.y + mouseY * (1 - delta),
}
let [isXValid, isYValid] = isPositionValid(newEndPoint)
if (isXValid) {
endPoint.x = newEndPoint.x
}
if (isYValid) {
endPoint.y = newEndPoint.y
}
updateTransform()
}
}
function getDistance(touch1, touch2) {
return Math.hypot(
touch1.clientX - touch2.clientX,
touch1.clientY - touch2.clientY,
)
}
function clamp(value, min, max) {
return Math.min(Math.max(value, min), max)
}
function isPositionValid(newEndPoint) {
const containerRect = container.getBoundingClientRect()
const movableRect = movable.getBoundingClientRect()
// Получаем размеры после трансформации
const scaledWidth =
movableRect.width *
(scale / window.getComputedStyle(movable).transform.split(',')[0].slice(7))
const scaledHeight =
movableRect.height *
(scale / window.getComputedStyle(movable).transform.split(',')[0].slice(7))
// Проверяем, что хотя бы часть контента всегда видна
const minVisible = 50 // минимальное количество пикселей, которое должно быть видно
const isXValid =
newEndPoint.x + scaledWidth >= minVisible &&
newEndPoint.x <= containerRect.width - minVisible
const isYValid =
newEndPoint.y + scaledHeight >= minVisible &&
newEndPoint.y <= containerRect.height - minVisible
return [isXValid, isYValid]
}
function updateTransform() {
movable.style.transform = `translate(${endPoint.x}px, ${endPoint.y}px) scale(${scale})`
constantSizeElements.forEach(el => {
el.style.transform = `scale(${0.5 / scale})`
})
}
updateTransform()
}