-
Notifications
You must be signed in to change notification settings - Fork 0
/
svg-pan-zoom.js
executable file
·909 lines (811 loc) · 25.3 KB
/
svg-pan-zoom.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
import ControlIcons from "@/utils/svg/control-icons.js";
import Utils from "./utilities.js";
import SvgUtils from "./svg-utilities.js";
import ShadowViewport from "@/utils/svg/shadow-viewport.js";
import Wheel from "uniwheel";
let SvgPanZoom = function (svg, options) {
this.init(svg, options);
};
let optionsDefaults = {
viewportSelector: ".svg-pan-zoom_viewport", // Viewport selector. Can be querySelector string or SVGElement
panEnabled: true, // enable or disable panning (default enabled)
controlIconsEnabled: false, // insert icons to give user an option in addition to mouse events to control pan/zoom (default disabled)
zoomEnabled: true, // enable or disable zooming (default enabled)
dblClickZoomEnabled: true, // enable or disable zooming by double clicking (default enabled)
mouseWheelZoomEnabled: true, // enable or disable zooming by mouse wheel (default enabled)
preventMouseEventsDefault: true, // enable or disable preventDefault for mouse events
zoomScaleSensitivity: 0.1, // Zoom sensitivity
minZoom: 0.5, // Minimum Zoom level
maxZoom: 10, // Maximum Zoom level
fit: true, // enable or disable viewport fit in SVG (default true)
contain: false, // enable or disable viewport contain the svg (default false)
center: true, // enable or disable viewport centering in SVG (default true)
refreshRate: "auto", // Maximum number of frames per second (altering SVG's viewport)
beforeZoom: null,
onZoom: null,
beforePan: null,
onPan: null,
customEventsHandler: null,
eventsListenerElement: null,
onUpdatedCTM: null
};
let passiveListenerOption = { passive: true };
SvgPanZoom.prototype.init = function (svg, options) {
let that = this;
this.svg = svg;
this.defs = svg.querySelector("defs");
// Add default attributes to SVG
SvgUtils.setupSvgAttributes(this.svg);
// Set options
this.options = Utils.extend(Utils.extend({}, optionsDefaults), options);
// Set default state
this.state = "none";
// Get dimensions
let boundingClientRectNormalized = SvgUtils.getBoundingClientRectNormalized(svg);
this.width = boundingClientRectNormalized.width;
this.height = boundingClientRectNormalized.height;
// Init shadow viewport
this.viewport = ShadowViewport(SvgUtils.getOrCreateViewport(this.svg, this.options.viewportSelector), {
svg: this.svg,
width: this.width,
height: this.height,
fit: this.options.fit,
contain: this.options.contain,
center: this.options.center,
refreshRate: this.options.refreshRate,
// Put callbacks into functions as they can change through time
beforeZoom: function (oldScale, newScale) {
if (that.viewport && that.options.beforeZoom) {
return that.options.beforeZoom(oldScale, newScale);
}
},
onZoom: function (scale) {
if (that.viewport && that.options.onZoom) {
return that.options.onZoom(scale);
}
},
beforePan: function (oldPoint, newPoint) {
if (that.viewport && that.options.beforePan) {
return that.options.beforePan(oldPoint, newPoint);
}
},
onPan: function (point) {
if (that.viewport && that.options.onPan) {
return that.options.onPan(point);
}
},
onUpdatedCTM: function (ctm) {
if (that.viewport && that.options.onUpdatedCTM) {
return that.options.onUpdatedCTM(ctm);
}
}
});
// Wrap callbacks into public API context
let publicInstance = this.getPublicInstance();
publicInstance.setBeforeZoom(this.options.beforeZoom);
publicInstance.setOnZoom(this.options.onZoom);
publicInstance.setBeforePan(this.options.beforePan);
publicInstance.setOnPan(this.options.onPan);
publicInstance.setOnUpdatedCTM(this.options.onUpdatedCTM);
if (this.options.controlIconsEnabled) {
ControlIcons.enable(this);
}
// Init events handlers
this.lastMouseWheelEventTime = Date.now();
this.setupHandlers();
};
/**
* Register event handlers
*/
SvgPanZoom.prototype.setupHandlers = function () {
let that = this,
prevEvt = null; // use for touchstart event to detect double tap
this.eventListeners = {
// Mouse down group
mousedown: function (evt) {
let result = that.handleMouseDown(evt, prevEvt);
prevEvt = evt;
return result;
},
touchstart: function (evt) {
let result = that.handleMouseDown(evt, prevEvt);
prevEvt = evt;
return result;
},
// Mouse up group
mouseup: function (evt) {
return that.handleMouseUp(evt);
},
touchend: function (evt) {
return that.handleMouseUp(evt);
},
// Mouse move group
mousemove: function (evt) {
return that.handleMouseMove(evt);
},
touchmove: function (evt) {
return that.handleMouseMove(evt);
},
// Mouse leave group
mouseleave: function (evt) {
return that.handleMouseUp(evt);
},
touchleave: function (evt) {
return that.handleMouseUp(evt);
},
touchcancel: function (evt) {
return that.handleMouseUp(evt);
}
};
// Init custom events handler if available
// eslint-disable-next-line eqeqeq
if (this.options.customEventsHandler != null) {
this.options.customEventsHandler.init({
svgElement: this.svg,
eventsListenerElement: this.options.eventsListenerElement,
instance: this.getPublicInstance()
});
// Custom event handler may halt builtin listeners
let haltEventListeners = this.options.customEventsHandler.haltEventListeners;
if (haltEventListeners && haltEventListeners.length) {
for (let i = haltEventListeners.length - 1; i >= 0; i--) {
if (this.eventListeners.hasOwnProperty(haltEventListeners[i])) {
delete this.eventListeners[haltEventListeners[i]];
}
}
}
}
// Bind eventListeners
for (let event in this.eventListeners) {
// Attach event to eventsListenerElement or SVG if not available
(this.options.eventsListenerElement || this.svg).addEventListener(
event,
this.eventListeners[event],
!this.options.preventMouseEventsDefault ? passiveListenerOption : false
);
}
// Zoom using mouse wheel
if (this.options.mouseWheelZoomEnabled) {
this.options.mouseWheelZoomEnabled = false; // set to false as enable will set it back to true
this.enableMouseWheelZoom();
}
};
/**
* Enable ability to zoom using mouse wheel
*/
SvgPanZoom.prototype.enableMouseWheelZoom = function () {
if (!this.options.mouseWheelZoomEnabled) {
let that = this;
// Mouse wheel listener
this.wheelListener = function (evt) {
return that.handleMouseWheel(evt);
};
// Bind wheelListener
let isPassiveListener = !this.options.preventMouseEventsDefault;
Wheel.on(this.options.eventsListenerElement || this.svg, this.wheelListener, isPassiveListener);
this.options.mouseWheelZoomEnabled = true;
}
};
/**
* Disable ability to zoom using mouse wheel
*/
SvgPanZoom.prototype.disableMouseWheelZoom = function () {
if (this.options.mouseWheelZoomEnabled) {
let isPassiveListener = !this.options.preventMouseEventsDefault;
Wheel.off(this.options.eventsListenerElement || this.svg, this.wheelListener, isPassiveListener);
this.options.mouseWheelZoomEnabled = false;
}
};
/**
* Handle mouse wheel event
*
* @param {Event} evt
*/
SvgPanZoom.prototype.handleMouseWheel = function (evt) {
if (!this.options.zoomEnabled || this.state !== "none") {
return;
}
if (this.options.preventMouseEventsDefault) {
if (evt.preventDefault) {
evt.preventDefault();
} else {
evt.returnValue = false;
}
}
/*
* 适配项目里面的ctrl加滚轮放大画面
* */
if (evt.ctrlKey === true || event.metaKey) {
// Default delta in case that deltaY is not available
let delta = evt.deltaY || 1,
timeDelta = Date.now() - this.lastMouseWheelEventTime,
divider = 3 + Math.max(0, 30 - timeDelta);
// Update cache
this.lastMouseWheelEventTime = Date.now();
// Make empirical adjustments for browsers that give deltaY in pixels (deltaMode=0)
if ("deltaMode" in evt && evt.deltaMode === 0 && evt.wheelDelta) {
delta = evt.deltaY === 0 ? 0 : Math.abs(evt.wheelDelta) / evt.deltaY;
}
delta = -0.3 < delta && delta < 0.3 ? delta : ((delta > 0 ? 1 : -1) * Math.log(Math.abs(delta) + 10)) / divider;
let inversedScreenCTM = this.svg.getScreenCTM().inverse(),
relativeMousePoint = SvgUtils.getEventPoint(evt, this.svg).matrixTransform(inversedScreenCTM),
zoom = Math.pow(1 + this.options.zoomScaleSensitivity, -1 * delta); // multiplying by neg. 1 so as to make zoom in/out behavior match Google maps behavior
this.zoomAtPoint(zoom, relativeMousePoint);
}
};
/**
* Zoom in at a SVG point
*
* @param {SVGPoint} point
* @param {Float} zoomScale Number representing how much to zoom
* @param {Boolean} zoomAbsolute Default false. If true, zoomScale is treated as an absolute value.
* Otherwise, zoomScale is treated as a multiplied (e.g. 1.10 would zoom in 10%)
*/
SvgPanZoom.prototype.zoomAtPoint = function (zoomScale, point, zoomAbsolute) {
let originalState = this.viewport.getOriginalState();
if (!zoomAbsolute) {
// Fit zoomScale in set bounds
if (this.getZoom() * zoomScale < this.options.minZoom * originalState.zoom) {
zoomScale = (this.options.minZoom * originalState.zoom) / this.getZoom();
} else if (this.getZoom() * zoomScale > this.options.maxZoom * originalState.zoom) {
zoomScale = (this.options.maxZoom * originalState.zoom) / this.getZoom();
}
} else {
// Fit zoomScale in set bounds
zoomScale = Math.max(this.options.minZoom * originalState.zoom, Math.min(this.options.maxZoom * originalState.zoom, zoomScale));
// Find relative scale to achieve desired scale
zoomScale = zoomScale / this.getZoom();
}
let oldCTM = this.viewport.getCTM(),
relativePoint = point.matrixTransform(oldCTM.inverse()),
modifier = this.svg.createSVGMatrix().translate(relativePoint.x, relativePoint.y).scale(zoomScale).translate(-relativePoint.x, -relativePoint.y),
newCTM = oldCTM.multiply(modifier);
if (newCTM.a !== oldCTM.a) {
this.viewport.setCTM(newCTM);
}
};
/**
* Zoom at center point
*
* @param {Float} scale
* @param {Boolean} absolute Marks zoom scale as relative or absolute
*/
SvgPanZoom.prototype.zoom = function (scale, absolute) {
this.zoomAtPoint(scale, SvgUtils.getSvgCenterPoint(this.svg, this.width, this.height), absolute);
};
/**
* Zoom used by public instance
*
* @param {Float} scale
* @param {Boolean} absolute Marks zoom scale as relative or absolute
*/
SvgPanZoom.prototype.publicZoom = function (scale, absolute) {
if (absolute) {
scale = this.computeFromRelativeZoom(scale);
}
this.zoom(scale, absolute);
};
/**
* Zoom at point used by public instance
*
* @param {Float} scale
* @param {SVGPoint|Object} point An object that has x and y attributes
* @param {Boolean} absolute Marks zoom scale as relative or absolute
*/
SvgPanZoom.prototype.publicZoomAtPoint = function (scale, point, absolute) {
if (absolute) {
// Transform zoom into a relative value
scale = this.computeFromRelativeZoom(scale);
}
// If not a SVGPoint but has x and y then create a SVGPoint
if (Utils.getType(point) !== "SVGPoint") {
if ("x" in point && "y" in point) {
point = SvgUtils.createSVGPoint(this.svg, point.x, point.y);
} else {
throw new Error("Given point is invalid");
}
}
this.zoomAtPoint(scale, point, absolute);
};
/**
* Get zoom scale
*
* @return {Float} zoom scale
*/
SvgPanZoom.prototype.getZoom = function () {
return this.viewport.getZoom();
};
/**
* Get zoom scale for public usage
*
* @return {Float} zoom scale
*/
SvgPanZoom.prototype.getRelativeZoom = function () {
return this.viewport.getRelativeZoom();
};
/**
* Compute actual zoom from public zoom
*
* @param {Float} zoom
* @return {Float} zoom scale
*/
SvgPanZoom.prototype.computeFromRelativeZoom = function (zoom) {
return zoom * this.viewport.getOriginalState().zoom;
};
/**
* Set zoom to initial state
*/
SvgPanZoom.prototype.resetZoom = function () {
let originalState = this.viewport.getOriginalState();
this.zoom(originalState.zoom, true);
};
/**
* Set pan to initial state
*/
SvgPanZoom.prototype.resetPan = function () {
this.pan(this.viewport.getOriginalState());
};
/**
* Set pan and zoom to initial state
*/
SvgPanZoom.prototype.reset = function () {
this.resetZoom();
this.resetPan();
};
/**
* Handle double click event
* See handleMouseDown() for alternate detection method
*
* @param {Event} evt
*/
SvgPanZoom.prototype.handleDblClick = function (evt) {
if (this.options.preventMouseEventsDefault) {
if (evt.preventDefault) {
evt.preventDefault();
} else {
evt.returnValue = false;
}
}
// Check if target was a control button
if (this.options.controlIconsEnabled) {
let targetClass = evt.target.getAttribute("class") || "";
if (targetClass.indexOf("svg-pan-zoom-control") > -1) {
return false;
}
}
let zoomFactor;
if (evt.shiftKey) {
zoomFactor = 1 / ((1 + this.options.zoomScaleSensitivity) * 2); // zoom out when shift key pressed
} else {
zoomFactor = (1 + this.options.zoomScaleSensitivity) * 2;
}
let point = SvgUtils.getEventPoint(evt, this.svg).matrixTransform(this.svg.getScreenCTM().inverse());
this.zoomAtPoint(zoomFactor, point);
};
/**
* Handle click event
*
* @param {Event} evt
*/
SvgPanZoom.prototype.handleMouseDown = function (evt, prevEvt) {
if (this.options.preventMouseEventsDefault) {
if (evt.preventDefault) {
evt.preventDefault();
} else {
evt.returnValue = false;
}
}
Utils.mouseAndTouchNormalize(evt, this.svg);
// Double click detection; more consistent than ondblclick
if (this.options.dblClickZoomEnabled && Utils.isDblClick(evt, prevEvt)) {
this.handleDblClick(evt);
} else {
// Pan mode
this.state = "pan";
this.firstEventCTM = this.viewport.getCTM();
this.stateOrigin = SvgUtils.getEventPoint(evt, this.svg).matrixTransform(this.firstEventCTM.inverse());
}
};
/**
* Handle mouse move event
*
* @param {Event} evt
*/
SvgPanZoom.prototype.handleMouseMove = function (evt) {
if (this.options.preventMouseEventsDefault) {
if (evt.preventDefault) {
evt.preventDefault();
} else {
evt.returnValue = false;
}
}
if (this.state === "pan" && this.options.panEnabled) {
// Pan mode
let point = SvgUtils.getEventPoint(evt, this.svg).matrixTransform(this.firstEventCTM.inverse()),
viewportCTM = this.firstEventCTM.translate(point.x - this.stateOrigin.x, point.y - this.stateOrigin.y);
this.viewport.setCTM(viewportCTM);
}
};
/**
* Handle mouse button release event
*
* @param {Event} evt
*/
SvgPanZoom.prototype.handleMouseUp = function (evt) {
if (this.options.preventMouseEventsDefault) {
if (evt.preventDefault) {
evt.preventDefault();
} else {
evt.returnValue = false;
}
}
if (this.state === "pan") {
// Quit pan mode
this.state = "none";
}
};
/**
* Adjust viewport size (only) so it will fit in SVG
* Does not center image
*/
SvgPanZoom.prototype.fit = function () {
let viewBox = this.viewport.getViewBox(),
newScale = Math.min(this.width / viewBox.width, this.height / viewBox.height);
this.zoom(newScale, true);
};
/**
* Adjust viewport size (only) so it will contain the SVG
* Does not center image
*/
SvgPanZoom.prototype.contain = function () {
let viewBox = this.viewport.getViewBox(),
newScale = Math.max(this.width / viewBox.width, this.height / viewBox.height);
this.zoom(newScale, true);
};
/**
* Adjust viewport pan (only) so it will be centered in SVG
* Does not zoom/fit/contain image
*/
SvgPanZoom.prototype.center = function () {
let viewBox = this.viewport.getViewBox(),
offsetX = (this.width - (viewBox.width + viewBox.x * 2) * this.getZoom()) * 0.5,
offsetY = (this.height - (viewBox.height + viewBox.y * 2) * this.getZoom()) * 0.5;
this.getPublicInstance().pan({ x: offsetX, y: offsetY });
};
/**
* Update content cached BorderBox
* Use when viewport contents change
*/
SvgPanZoom.prototype.updateBBox = function () {
this.viewport.simpleViewBoxCache();
};
/**
* Pan to a rendered position
*
* @param {Object} point {x: 0, y: 0}
*/
SvgPanZoom.prototype.pan = function (point) {
let viewportCTM = this.viewport.getCTM();
viewportCTM.e = point.x;
viewportCTM.f = point.y;
this.viewport.setCTM(viewportCTM);
};
/**
* Relatively pan the graph by a specified rendered position vector
*
* @param {Object} point {x: 0, y: 0}
*/
SvgPanZoom.prototype.panBy = function (point) {
let viewportCTM = this.viewport.getCTM();
viewportCTM.e += point.x;
viewportCTM.f += point.y;
this.viewport.setCTM(viewportCTM);
};
/**
* Get pan vector
*
* @return {Object} {x: 0, y: 0}
*/
SvgPanZoom.prototype.getPan = function () {
let state = this.viewport.getState();
return { x: state.x, y: state.y };
};
/**
* Recalculates cached svg dimensions and controls position
*/
SvgPanZoom.prototype.resize = function () {
// Get dimensions
let boundingClientRectNormalized = SvgUtils.getBoundingClientRectNormalized(this.svg);
this.width = boundingClientRectNormalized.width;
this.height = boundingClientRectNormalized.height;
// Recalculate original state
let viewport = this.viewport;
viewport.options.width = this.width;
viewport.options.height = this.height;
viewport.processCTM();
// Reposition control icons by re-enabling them
if (this.options.controlIconsEnabled) {
this.getPublicInstance().disableControlIcons();
this.getPublicInstance().enableControlIcons();
}
};
/**
* Unbind mouse events, free callbacks and destroy public instance
*/
SvgPanZoom.prototype.destroy = function () {
let that = this;
// Free callbacks
this.beforeZoom = null;
this.onZoom = null;
this.beforePan = null;
this.onPan = null;
this.onUpdatedCTM = null;
// Destroy custom event handlers
// eslint-disable-next-line eqeqeq
if (this.options.customEventsHandler != null) {
this.options.customEventsHandler.destroy({
svgElement: this.svg,
eventsListenerElement: this.options.eventsListenerElement,
instance: this.getPublicInstance()
});
}
// Unbind eventListeners
for (let event in this.eventListeners) {
(this.options.eventsListenerElement || this.svg).removeEventListener(
event,
this.eventListeners[event],
!this.options.preventMouseEventsDefault ? passiveListenerOption : false
);
}
// Unbind wheelListener
this.disableMouseWheelZoom();
// Remove control icons
this.getPublicInstance().disableControlIcons();
// Reset zoom and pan
this.reset();
// Remove instance from instancesStore
instancesStore = instancesStore.filter(function (instance) {
return instance.svg !== that.svg;
});
// Delete options and its contents
delete this.options;
// Delete viewport to make public shadow viewport functions uncallable
delete this.viewport;
// Destroy public instance and rewrite getPublicInstance
delete this.publicInstance;
delete this.pi;
this.getPublicInstance = function () {
return null;
};
};
/**
* Returns a public instance object
*
* @return {Object} Public instance object
*/
SvgPanZoom.prototype.getPublicInstance = function () {
let that = this;
// Create cache
if (!this.publicInstance) {
this.publicInstance = this.pi = {
// Pan
enablePan: function () {
that.options.panEnabled = true;
return that.pi;
},
disablePan: function () {
that.options.panEnabled = false;
return that.pi;
},
isPanEnabled: function () {
return !!that.options.panEnabled;
},
pan: function (point) {
that.pan(point);
return that.pi;
},
panBy: function (point) {
that.panBy(point);
return that.pi;
},
getPan: function () {
return that.getPan();
},
// Pan event
setBeforePan: function (fn) {
that.options.beforePan = fn === null ? null : Utils.proxy(fn, that.publicInstance);
return that.pi;
},
setOnPan: function (fn) {
that.options.onPan = fn === null ? null : Utils.proxy(fn, that.publicInstance);
return that.pi;
},
// Zoom and Control Icons
enableZoom: function () {
that.options.zoomEnabled = true;
return that.pi;
},
disableZoom: function () {
that.options.zoomEnabled = false;
return that.pi;
},
isZoomEnabled: function () {
return !!that.options.zoomEnabled;
},
enableControlIcons: function () {
if (!that.options.controlIconsEnabled) {
that.options.controlIconsEnabled = true;
ControlIcons.enable(that);
}
return that.pi;
},
disableControlIcons: function () {
if (that.options.controlIconsEnabled) {
that.options.controlIconsEnabled = false;
ControlIcons.disable(that);
}
return that.pi;
},
isControlIconsEnabled: function () {
return !!that.options.controlIconsEnabled;
},
// Double click zoom
enableDblClickZoom: function () {
that.options.dblClickZoomEnabled = true;
return that.pi;
},
disableDblClickZoom: function () {
that.options.dblClickZoomEnabled = false;
return that.pi;
},
isDblClickZoomEnabled: function () {
return !!that.options.dblClickZoomEnabled;
},
// Mouse wheel zoom
enableMouseWheelZoom: function () {
that.enableMouseWheelZoom();
return that.pi;
},
disableMouseWheelZoom: function () {
that.disableMouseWheelZoom();
return that.pi;
},
isMouseWheelZoomEnabled: function () {
return !!that.options.mouseWheelZoomEnabled;
},
// Zoom scale and bounds
setZoomScaleSensitivity: function (scale) {
that.options.zoomScaleSensitivity = scale;
return that.pi;
},
setMinZoom: function (zoom) {
that.options.minZoom = zoom;
return that.pi;
},
setMaxZoom: function (zoom) {
that.options.maxZoom = zoom;
return that.pi;
},
// Zoom event
setBeforeZoom: function (fn) {
that.options.beforeZoom = fn === null ? null : Utils.proxy(fn, that.publicInstance);
return that.pi;
},
setOnZoom: function (fn) {
that.options.onZoom = fn === null ? null : Utils.proxy(fn, that.publicInstance);
return that.pi;
},
// Zooming
zoom: function (scale) {
that.publicZoom(scale, true);
return that.pi;
},
zoomBy: function (scale) {
that.publicZoom(scale, false);
return that.pi;
},
zoomAtPoint: function (scale, point) {
that.publicZoomAtPoint(scale, point, true);
return that.pi;
},
zoomAtPointBy: function (scale, point) {
that.publicZoomAtPoint(scale, point, false);
return that.pi;
},
zoomIn: function () {
this.zoomBy(1 + that.options.zoomScaleSensitivity);
return that.pi;
},
zoomOut: function () {
this.zoomBy(1 / (1 + that.options.zoomScaleSensitivity));
return that.pi;
},
getZoom: function () {
return that.getRelativeZoom();
},
// CTM update
setOnUpdatedCTM: function (fn) {
that.options.onUpdatedCTM = fn === null ? null : Utils.proxy(fn, that.publicInstance);
return that.pi;
},
// Reset
resetZoom: function () {
that.resetZoom();
return that.pi;
},
resetPan: function () {
that.resetPan();
return that.pi;
},
reset: function () {
that.reset();
return that.pi;
},
// Fit, Contain and Center
fit: function () {
that.fit();
return that.pi;
},
contain: function () {
that.contain();
return that.pi;
},
center: function () {
that.center();
return that.pi;
},
// Size and Resize
updateBBox: function () {
that.updateBBox();
return that.pi;
},
resize: function () {
that.resize();
return that.pi;
},
getSizes: function () {
return {
width: that.width,
height: that.height,
realZoom: that.getZoom(),
viewBox: that.viewport.getViewBox()
};
},
// Destroy
destroy: function () {
that.destroy();
return that.pi;
}
};
}
return this.publicInstance;
};
/**
* Stores pairs of instances of SvgPanZoom and SVG
* Each pair is represented by an object {svg: SVGSVGElement, instance: SvgPanZoom}
*
* @type {Array}
*/
let instancesStore = [];
let svgPanZoom = function (elementOrSelector, options) {
let svg = Utils.getSvg(elementOrSelector);
if (svg === null) {
return null;
} else {
// Look for existent instance
for (let i = instancesStore.length - 1; i >= 0; i--) {
if (instancesStore[i].svg === svg) {
return instancesStore[i].instance.getPublicInstance();
}
}
// If instance not found - create one
instancesStore.push({
svg: svg,
instance: new SvgPanZoom(svg, options)
});
// Return just pushed instance
return instancesStore[instancesStore.length - 1].instance.getPublicInstance();
}
};
// module.exports = svgPanZoom;
export default svgPanZoom;