Debian Codegraph @

panzoom/src/svg.panzoom.js

raw
1 import { Svg, on, off, extend, Matrix, Box } from '@svgdotjs/svg.js'
2
3 const normalizeEvent = ev =>
4 ev.touches || [{ clientX: ev.clientX, clientY: ev.clientY }]
5
6 extend(Svg, {
7 panZoom (options) {
8 this.off('.panZoom')
9
10 // when called with false, disable panZoom
11 if (options === false) return this
12
13 options = options ?? {}
14 const zoomFactor = options.zoomFactor ?? 2
15 const zoomMin = options.zoomMin ?? Number.MIN_VALUE
16 const zoomMax = options.zoomMax ?? Number.MAX_VALUE
17 const doWheelZoom = options.wheelZoom ?? true
18 const doPinchZoom = options.pinchZoom ?? true
19 const doPanning = options.panning ?? true
20 const panButton = options.panButton ?? 0
21 const oneFingerPan = options.oneFingerPan ?? false
22 const margins = options.margins ?? false
23 const wheelZoomDeltaModeLinePixels = options.wheelZoomDeltaModeLinePixels ?? 17
24 const wheelZoomDeltaModeScreenPixels = options.wheelZoomDeltaModeScreenPixels ?? 53
25
26 let lastP
27 let lastTouches
28 let zoomInProgress = false
29
30 const viewbox = this.viewbox()
31
32 const restrictToMargins = box => {
33 if (!margins) return box
34 const { top, left, bottom, right } = margins
35
36 const { width, height } = this.attr(['width', 'height'])
37 const preserveAspectRatio = this.node.preserveAspectRatio.baseVal
38
39 // The current viewport (exactly what is shown on the screen, what we ultimately want to restrict)
40 // is not always exactly the same as current viewbox. They are different when the viewbox aspectRatio and the svg aspectRatio
41 // are different and preserveAspectRatio is not "none". These offsets represent the difference in user coordinates
42 // between the side of the viewbox and the side of the viewport.
43 let viewportLeftOffset = 0
44 let viewportRightOffset = 0
45 let viewportTopOffset = 0
46 let viewportBottomOffset = 0
47
48 // preserveAspectRatio none has no offsets
49 if (preserveAspectRatio.align !== preserveAspectRatio.SVG_PRESERVEASPECTRATIO_NONE) {
50 const svgAspectRatio = width / height
51 const viewboxAspectRatio = viewbox.width / viewbox.height
52 // when aspectRatios are the same, there are no offsets
53 if (viewboxAspectRatio !== svgAspectRatio) {
54 // aspectRatio unknown is like meet because that's the default
55 const isMeet = preserveAspectRatio.meetOrSlice !== preserveAspectRatio.SVG_MEETORSLICE_SLICE
56 const changedAxis = svgAspectRatio > viewboxAspectRatio ? 'width' : 'height'
57 const isWidth = changedAxis === 'width'
58 const changeHorizontal = (isMeet && isWidth) || (!isMeet && !isWidth)
59 const ratio = changeHorizontal
60 ? svgAspectRatio / viewboxAspectRatio
61 : viewboxAspectRatio / svgAspectRatio
62
63 const offset = box[changedAxis] - box[changedAxis] * ratio
64 if (changeHorizontal) {
65 if (
66 preserveAspectRatio.align === preserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMIDYMIN ||
67 preserveAspectRatio.align === preserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMIDYMID ||
68 preserveAspectRatio.align === preserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMIDYMAX) {
69 viewportLeftOffset = offset / 2
70 viewportRightOffset = -offset / 2
71 } else if (
72 preserveAspectRatio.align === preserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMINYMIN ||
73 preserveAspectRatio.align === preserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMINYMID ||
74 preserveAspectRatio.align === preserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMINYMAX) {
75 viewportRightOffset = -offset
76 } else if (
77 preserveAspectRatio.align === preserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMAXYMIN ||
78 preserveAspectRatio.align === preserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMAXYMID ||
79 preserveAspectRatio.align === preserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMAXYMAX) {
80 viewportLeftOffset = offset
81 }
82 } else {
83 if (
84 preserveAspectRatio.align === preserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMINYMID ||
85 preserveAspectRatio.align === preserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMIDYMID ||
86 preserveAspectRatio.align === preserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMAXYMID) {
87 viewportTopOffset = offset / 2
88 viewportBottomOffset = -offset / 2
89 } else if (
90 preserveAspectRatio.align === preserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMINYMIN ||
91 preserveAspectRatio.align === preserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMIDYMIN ||
92 preserveAspectRatio.align === preserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMAXYMIN) {
93 viewportBottomOffset = -offset
94 } else if (
95 preserveAspectRatio.align === preserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMINYMAX ||
96 preserveAspectRatio.align === preserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMIDYMAX ||
97 preserveAspectRatio.align === preserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMAXYMAX) {
98 viewportTopOffset = offset
99 }
100 }
101
102 }
103 }
104
105 // when box.x == leftLimit, the image is panned to the left,
106 // i.e the current box is to the right of the initial viewbox,
107 // and only the right part of the initial image is visible, i.e.
108 // the right side of the initial viewbox minus left margin (viewbox.x+viewbox.width-left)
109 // is aligned with the left side of the viewport (box.x + viewportLeftOffset):
110 // viewbox.width + viewbox.x - left = box.x + viewportLeftOffset
111 // viewbox.width + viewbox.x - left - viewportLeftOffset = box.x (= leftLimit)
112 const leftLimit = viewbox.width + viewbox.x - left - viewportLeftOffset
113 // when box.x == rightLimit, the image is panned to the right,
114 // i.e the current box is to the left of the initial viewbox
115 // and only the left part of the initial image is visible, i.e
116 // the left side of the initial viewbox plus right margin (viewbox.x + right)
117 // is aligned with the right side of the viewport (box.x + box.width + viewportRightOffset)
118 // viewbox.x + right = box.x + box.width + viewportRightOffset
119 // viewbox.x + right - box.width - viewportRightOffset = box.x (= rightLimit)
120 const rightLimit = viewbox.x + right - box.width - viewportRightOffset
121 // same with top and bottom
122 const topLimit = viewbox.height + viewbox.y - top - viewportTopOffset
123 const bottomLimit = viewbox.y + bottom - box.height - viewportBottomOffset
124
125 box.x = Math.min(leftLimit, Math.max(rightLimit, box.x)) // enforce rightLimit <= box.x <= leftLimit
126 box.y = Math.min(topLimit, Math.max(bottomLimit, box.y)) // enforce bottomLimit <= box.y <= topLimit
127 return box
128 }
129
130 const wheelZoom = function (ev) {
131 ev.preventDefault()
132
133 // When wheeling on a mouse,
134 // - chrome by default uses deltaY = 53, deltaMode = 0 (pixel)
135 // - firefox by default uses deltaY = 3, deltaMode = 1 (line)
136 // - chrome and firefox on windows after configuring "One screen at a time"
137 // use deltaY = 1, deltaMode = 2 (screen)
138 //
139 // Note that when when wheeling on a touchpad, deltaY depends on how fast
140 // you swipe, but the deltaMode is still different between the browsers.
141 //
142 // Normalize everything so that zooming speed is approximately the same in all cases
143 let normalizedPixelDeltaY
144 switch (ev.deltaMode) {
145 case 1:
146 normalizedPixelDeltaY = ev.deltaY * wheelZoomDeltaModeLinePixels
147 break
148 case 2:
149 normalizedPixelDeltaY = ev.deltaY * wheelZoomDeltaModeScreenPixels
150 break
151 default:
152 // 0 (already pixels) or new mode (avoid crashing)
153 normalizedPixelDeltaY = ev.deltaY
154 break
155 }
156
157 let lvl = Math.pow(1 + zoomFactor, (-1 * normalizedPixelDeltaY) / 100) * this.zoom()
158 const p = this.point(ev.clientX, ev.clientY)
159
160 if (lvl > zoomMax) {
161 lvl = zoomMax
162 }
163
164 if (lvl < zoomMin) {
165 lvl = zoomMin
166 }
167
168 if (this.dispatch('zoom', { level: lvl, focus: p }).defaultPrevented) {
169 return this
170 }
171
172 this.zoom(lvl, p)
173
174 if (margins) {
175 const box = restrictToMargins(this.viewbox())
176 this.viewbox(box)
177 }
178 }
179
180 const pinchZoomStart = function (ev) {
181 lastTouches = normalizeEvent(ev)
182
183 // Start panning in case only one touch is found
184 if (lastTouches.length < 2) {
185 if (doPanning && oneFingerPan) {
186 panStart.call(this, ev)
187 }
188 return
189 }
190
191 // Stop panning for more than one touch
192 if (doPanning && oneFingerPan) {
193 panStop.call(this, ev)
194 }
195
196 // We call it so late, so the user is still able to scroll / reload the page via gesture
197 // In case oneFingerPan is not active
198 ev.preventDefault()
199
200 if (this.dispatch('pinchZoomStart', { event: ev }).defaultPrevented) {
201 return
202 }
203
204 this.off('touchstart.panZoom', pinchZoomStart)
205
206 zoomInProgress = true
207 on(document, 'touchmove.panZoom', pinchZoom, this, { passive: false })
208 on(document, 'touchend.panZoom', pinchZoomStop, this, { passive: false })
209 }
210
211 const pinchZoomStop = function (ev) {
212 ev.preventDefault()
213
214 const currentTouches = normalizeEvent(ev)
215 if (currentTouches.length > 1) {
216 return
217 }
218
219 zoomInProgress = false
220
221 this.dispatch('pinchZoomEnd', { event: ev })
222
223 off(document, 'touchmove.panZoom', pinchZoom)
224 off(document, 'touchend.panZoom', pinchZoomStop)
225 this.on('touchstart.panZoom', pinchZoomStart)
226
227 if (currentTouches.length && doPanning && oneFingerPan) {
228 panStart.call(this, ev)
229 }
230 }
231
232 const pinchZoom = function (ev) {
233 ev.preventDefault()
234
235 const currentTouches = normalizeEvent(ev)
236 const zoom = this.zoom()
237
238 // Distance Formula
239 const lastDelta = Math.sqrt(
240 Math.pow(lastTouches[0].clientX - lastTouches[1].clientX, 2) +
241 Math.pow(lastTouches[0].clientY - lastTouches[1].clientY, 2)
242 )
243
244 const currentDelta = Math.sqrt(
245 Math.pow(currentTouches[0].clientX - currentTouches[1].clientX, 2) +
246 Math.pow(currentTouches[0].clientY - currentTouches[1].clientY, 2)
247 )
248
249 let zoomAmount = lastDelta / currentDelta
250
251 if (
252 (zoom < zoomMin && zoomAmount > 1) ||
253 (zoom > zoomMax && zoomAmount < 1)
254 ) {
255 zoomAmount = 1
256 }
257
258 const currentFocus = {
259 x:
260 currentTouches[0].clientX +
261 0.5 * (currentTouches[1].clientX - currentTouches[0].clientX),
262 y:
263 currentTouches[0].clientY +
264 0.5 * (currentTouches[1].clientY - currentTouches[0].clientY)
265 }
266
267 const lastFocus = {
268 x:
269 lastTouches[0].clientX +
270 0.5 * (lastTouches[1].clientX - lastTouches[0].clientX),
271 y:
272 lastTouches[0].clientY +
273 0.5 * (lastTouches[1].clientY - lastTouches[0].clientY)
274 }
275
276 const p = this.point(currentFocus.x, currentFocus.y)
277 const focusP = this.point(
278 2 * currentFocus.x - lastFocus.x,
279 2 * currentFocus.y - lastFocus.y
280 )
281 const box = new Box(this.viewbox()).transform(
282 new Matrix()
283 .translate(-focusP.x, -focusP.y)
284 .scale(zoomAmount, 0, 0)
285 .translate(p.x, p.y)
286 )
287
288 restrictToMargins(box)
289 this.viewbox(box)
290
291 lastTouches = currentTouches
292
293 this.dispatch('zoom', { box: box, focus: focusP })
294 }
295
296 const panStart = function (ev) {
297 const isMouse = ev.type.indexOf('mouse') > -1
298
299 // In case panStart is called with touch, ev.button is undefined
300 if (isMouse && ev.button !== panButton && ev.which !== panButton + 1) {
301 return
302 }
303
304 ev.preventDefault()
305
306 this.off('mousedown.panZoom', panStart)
307
308 lastTouches = normalizeEvent(ev)
309
310 if (zoomInProgress) return
311
312 this.dispatch('panStart', { event: ev })
313
314 lastP = { x: lastTouches[0].clientX, y: lastTouches[0].clientY }
315
316 on(document, 'touchmove.panZoom mousemove.panZoom', panning, this, {
317 passive: false
318 })
319
320 on(document, 'touchend.panZoom mouseup.panZoom', panStop, this, {
321 passive: false
322 })
323 }
324
325 const panStop = function (ev) {
326 ev.preventDefault()
327
328 off(document, 'touchmove.panZoom mousemove.panZoom', panning)
329 off(document, 'touchend.panZoom mouseup.panZoom', panStop)
330 this.on('mousedown.panZoom', panStart)
331
332 this.dispatch('panEnd', { event: ev })
333 }
334
335 const panning = function (ev) {
336 ev.preventDefault()
337
338 const currentTouches = normalizeEvent(ev)
339
340 const currentP = {
341 x: currentTouches[0].clientX,
342 y: currentTouches[0].clientY
343 }
344
345 const p1 = this.point(currentP.x, currentP.y)
346
347 const p2 = this.point(lastP.x, lastP.y)
348
349 const deltaP = [p2.x - p1.x, p2.y - p1.y]
350
351 if (!deltaP[0] && !deltaP[1]) {
352 return
353 }
354
355 const box = new Box(this.viewbox()).transform(
356 new Matrix().translate(deltaP[0], deltaP[1])
357 )
358
359 lastP = currentP
360
361 restrictToMargins(box)
362
363 if (this.dispatch('panning', { box, event: ev }).defaultPrevented) {
364 return
365 }
366
367 this.viewbox(box)
368 }
369
370 if (doWheelZoom) {
371 this.on('wheel.panZoom', wheelZoom, this, { passive: false })
372 }
373
374 if (doPinchZoom) {
375 this.on('touchstart.panZoom', pinchZoomStart, this, { passive: false })
376 }
377
378 if (doPanning) {
379 this.on('mousedown.panZoom', panStart, this, { passive: false })
380 }
381
382 return this
383 }
384 })