-
Notifications
You must be signed in to change notification settings - Fork 1
/
svg-pan-zoom.js
1900 lines (1638 loc) · 60.9 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
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// svg-pan-zoom v3.6.0
// https://github.com/ariutta/svg-pan-zoom
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
var svgPanZoom = require('./svg-pan-zoom.js');
// UMD module definition
(function(window, document){
// AMD
if (typeof define === 'function' && define.amd) {
define('svg-pan-zoom', function () {
return svgPanZoom;
});
// CMD
} else if (typeof module !== 'undefined' && module.exports) {
module.exports = svgPanZoom;
// Browser
// Keep exporting globally as module.exports is available because of browserify
window.svgPanZoom = svgPanZoom;
}
})(window, document)
},{"./svg-pan-zoom.js":4}],2:[function(require,module,exports){
var SvgUtils = require('./svg-utilities');
module.exports = {
enable: function(instance) {
// Select (and create if necessary) defs
var defs = instance.svg.querySelector('defs')
if (!defs) {
defs = document.createElementNS(SvgUtils.svgNS, 'defs')
instance.svg.appendChild(defs)
}
// Check for style element, and create it if it doesn't exist
var styleEl = defs.querySelector('style#svg-pan-zoom-controls-styles');
if (!styleEl) {
var style = document.createElementNS(SvgUtils.svgNS, 'style')
style.setAttribute('id', 'svg-pan-zoom-controls-styles')
style.setAttribute('type', 'text/css')
style.textContent = '.svg-pan-zoom-control { cursor: pointer; fill: black; fill-opacity: 0.333; } .svg-pan-zoom-control:hover { fill-opacity: 0.8; } .svg-pan-zoom-control-background { fill: white; fill-opacity: 0.5; } .svg-pan-zoom-control-background { fill-opacity: 0.8; }'
defs.appendChild(style)
}
// Zoom Group
var zoomGroup = document.createElementNS(SvgUtils.svgNS, 'g');
zoomGroup.setAttribute('id', 'svg-pan-zoom-controls');
zoomGroup.setAttribute('transform', 'translate(' + ( instance.width - 70 ) + ' ' + ( instance.height - 76 ) + ') scale(0.75)');
zoomGroup.setAttribute('class', 'svg-pan-zoom-control');
// Control elements
zoomGroup.appendChild(this._createZoomIn(instance))
zoomGroup.appendChild(this._createZoomReset(instance))
zoomGroup.appendChild(this._createZoomOut(instance))
// Finally append created element
instance.svg.appendChild(zoomGroup)
// Cache control instance
instance.controlIcons = zoomGroup
}
, _createZoomIn: function(instance) {
var zoomIn = document.createElementNS(SvgUtils.svgNS, 'g');
zoomIn.setAttribute('id', 'svg-pan-zoom-zoom-in');
zoomIn.setAttribute('transform', 'translate(30.5 5) scale(0.015)');
zoomIn.setAttribute('class', 'svg-pan-zoom-control');
zoomIn.addEventListener('click', function() {instance.getPublicInstance().zoomIn()}, false)
zoomIn.addEventListener('touchstart', function() {instance.getPublicInstance().zoomIn()}, false)
var zoomInBackground = document.createElementNS(SvgUtils.svgNS, 'rect'); // TODO change these background space fillers to rounded rectangles so they look prettier
zoomInBackground.setAttribute('x', '0');
zoomInBackground.setAttribute('y', '0');
zoomInBackground.setAttribute('width', '1500'); // larger than expected because the whole group is transformed to scale down
zoomInBackground.setAttribute('height', '1400');
zoomInBackground.setAttribute('class', 'svg-pan-zoom-control-background');
zoomIn.appendChild(zoomInBackground);
var zoomInShape = document.createElementNS(SvgUtils.svgNS, 'path');
zoomInShape.setAttribute('d', 'M1280 576v128q0 26 -19 45t-45 19h-320v320q0 26 -19 45t-45 19h-128q-26 0 -45 -19t-19 -45v-320h-320q-26 0 -45 -19t-19 -45v-128q0 -26 19 -45t45 -19h320v-320q0 -26 19 -45t45 -19h128q26 0 45 19t19 45v320h320q26 0 45 19t19 45zM1536 1120v-960 q0 -119 -84.5 -203.5t-203.5 -84.5h-960q-119 0 -203.5 84.5t-84.5 203.5v960q0 119 84.5 203.5t203.5 84.5h960q119 0 203.5 -84.5t84.5 -203.5z');
zoomInShape.setAttribute('class', 'svg-pan-zoom-control-element');
zoomIn.appendChild(zoomInShape);
return zoomIn
}
, _createZoomReset: function(instance){
// reset
var resetPanZoomControl = document.createElementNS(SvgUtils.svgNS, 'g');
resetPanZoomControl.setAttribute('id', 'svg-pan-zoom-reset-pan-zoom');
resetPanZoomControl.setAttribute('transform', 'translate(5 35) scale(0.4)');
resetPanZoomControl.setAttribute('class', 'svg-pan-zoom-control');
resetPanZoomControl.addEventListener('click', function() {instance.getPublicInstance().reset()}, false);
resetPanZoomControl.addEventListener('touchstart', function() {instance.getPublicInstance().reset()}, false);
var resetPanZoomControlBackground = document.createElementNS(SvgUtils.svgNS, 'rect'); // TODO change these background space fillers to rounded rectangles so they look prettier
resetPanZoomControlBackground.setAttribute('x', '2');
resetPanZoomControlBackground.setAttribute('y', '2');
resetPanZoomControlBackground.setAttribute('width', '182'); // larger than expected because the whole group is transformed to scale down
resetPanZoomControlBackground.setAttribute('height', '58');
resetPanZoomControlBackground.setAttribute('class', 'svg-pan-zoom-control-background');
resetPanZoomControl.appendChild(resetPanZoomControlBackground);
var resetPanZoomControlShape1 = document.createElementNS(SvgUtils.svgNS, 'path');
resetPanZoomControlShape1.setAttribute('d', 'M33.051,20.632c-0.742-0.406-1.854-0.609-3.338-0.609h-7.969v9.281h7.769c1.543,0,2.701-0.188,3.473-0.562c1.365-0.656,2.048-1.953,2.048-3.891C35.032,22.757,34.372,21.351,33.051,20.632z');
resetPanZoomControlShape1.setAttribute('class', 'svg-pan-zoom-control-element');
resetPanZoomControl.appendChild(resetPanZoomControlShape1);
var resetPanZoomControlShape2 = document.createElementNS(SvgUtils.svgNS, 'path');
resetPanZoomControlShape2.setAttribute('d', 'M170.231,0.5H15.847C7.102,0.5,0.5,5.708,0.5,11.84v38.861C0.5,56.833,7.102,61.5,15.847,61.5h154.384c8.745,0,15.269-4.667,15.269-10.798V11.84C185.5,5.708,178.976,0.5,170.231,0.5z M42.837,48.569h-7.969c-0.219-0.766-0.375-1.383-0.469-1.852c-0.188-0.969-0.289-1.961-0.305-2.977l-0.047-3.211c-0.03-2.203-0.41-3.672-1.142-4.406c-0.732-0.734-2.103-1.102-4.113-1.102h-7.05v13.547h-7.055V14.022h16.524c2.361,0.047,4.178,0.344,5.45,0.891c1.272,0.547,2.351,1.352,3.234,2.414c0.731,0.875,1.31,1.844,1.737,2.906s0.64,2.273,0.64,3.633c0,1.641-0.414,3.254-1.242,4.84s-2.195,2.707-4.102,3.363c1.594,0.641,2.723,1.551,3.387,2.73s0.996,2.98,0.996,5.402v2.32c0,1.578,0.063,2.648,0.19,3.211c0.19,0.891,0.635,1.547,1.333,1.969V48.569z M75.579,48.569h-26.18V14.022h25.336v6.117H56.454v7.336h16.781v6H56.454v8.883h19.125V48.569z M104.497,46.331c-2.44,2.086-5.887,3.129-10.34,3.129c-4.548,0-8.125-1.027-10.731-3.082s-3.909-4.879-3.909-8.473h6.891c0.224,1.578,0.662,2.758,1.316,3.539c1.196,1.422,3.246,2.133,6.15,2.133c1.739,0,3.151-0.188,4.236-0.562c2.058-0.719,3.087-2.055,3.087-4.008c0-1.141-0.504-2.023-1.512-2.648c-1.008-0.609-2.607-1.148-4.796-1.617l-3.74-0.82c-3.676-0.812-6.201-1.695-7.576-2.648c-2.328-1.594-3.492-4.086-3.492-7.477c0-3.094,1.139-5.664,3.417-7.711s5.623-3.07,10.036-3.07c3.685,0,6.829,0.965,9.431,2.895c2.602,1.93,3.966,4.73,4.093,8.402h-6.938c-0.128-2.078-1.057-3.555-2.787-4.43c-1.154-0.578-2.587-0.867-4.301-0.867c-1.907,0-3.428,0.375-4.565,1.125c-1.138,0.75-1.706,1.797-1.706,3.141c0,1.234,0.561,2.156,1.682,2.766c0.721,0.406,2.25,0.883,4.589,1.43l6.063,1.43c2.657,0.625,4.648,1.461,5.975,2.508c2.059,1.625,3.089,3.977,3.089,7.055C108.157,41.624,106.937,44.245,104.497,46.331z M139.61,48.569h-26.18V14.022h25.336v6.117h-18.281v7.336h16.781v6h-16.781v8.883h19.125V48.569z M170.337,20.14h-10.336v28.43h-7.266V20.14h-10.383v-6.117h27.984V20.14z');
resetPanZoomControlShape2.setAttribute('class', 'svg-pan-zoom-control-element');
resetPanZoomControl.appendChild(resetPanZoomControlShape2);
return resetPanZoomControl
}
, _createZoomOut: function(instance){
// zoom out
var zoomOut = document.createElementNS(SvgUtils.svgNS, 'g');
zoomOut.setAttribute('id', 'svg-pan-zoom-zoom-out');
zoomOut.setAttribute('transform', 'translate(30.5 70) scale(0.015)');
zoomOut.setAttribute('class', 'svg-pan-zoom-control');
zoomOut.addEventListener('click', function() {instance.getPublicInstance().zoomOut()}, false);
zoomOut.addEventListener('touchstart', function() {instance.getPublicInstance().zoomOut()}, false);
var zoomOutBackground = document.createElementNS(SvgUtils.svgNS, 'rect'); // TODO change these background space fillers to rounded rectangles so they look prettier
zoomOutBackground.setAttribute('x', '0');
zoomOutBackground.setAttribute('y', '0');
zoomOutBackground.setAttribute('width', '1500'); // larger than expected because the whole group is transformed to scale down
zoomOutBackground.setAttribute('height', '1400');
zoomOutBackground.setAttribute('class', 'svg-pan-zoom-control-background');
zoomOut.appendChild(zoomOutBackground);
var zoomOutShape = document.createElementNS(SvgUtils.svgNS, 'path');
zoomOutShape.setAttribute('d', 'M1280 576v128q0 26 -19 45t-45 19h-896q-26 0 -45 -19t-19 -45v-128q0 -26 19 -45t45 -19h896q26 0 45 19t19 45zM1536 1120v-960q0 -119 -84.5 -203.5t-203.5 -84.5h-960q-119 0 -203.5 84.5t-84.5 203.5v960q0 119 84.5 203.5t203.5 84.5h960q119 0 203.5 -84.5 t84.5 -203.5z');
zoomOutShape.setAttribute('class', 'svg-pan-zoom-control-element');
zoomOut.appendChild(zoomOutShape);
return zoomOut
}
, disable: function(instance) {
if (instance.controlIcons) {
instance.controlIcons.parentNode.removeChild(instance.controlIcons)
instance.controlIcons = null
}
}
}
},{"./svg-utilities":5}],3:[function(require,module,exports){
var SvgUtils = require('./svg-utilities')
, Utils = require('./utilities')
;
var ShadowViewport = function(viewport, options){
this.init(viewport, options)
}
/**
* Initialization
*
* @param {SVGElement} viewport
* @param {Object} options
*/
ShadowViewport.prototype.init = function(viewport, options) {
// DOM Elements
this.viewport = viewport
this.options = options
// State cache
this.originalState = {zoom: 1, x: 0, y: 0}
this.activeState = {zoom: 1, x: 0, y: 0}
this.updateCTMCached = Utils.proxy(this.updateCTM, this)
// Create a custom requestAnimationFrame taking in account refreshRate
this.requestAnimationFrame = Utils.createRequestAnimationFrame(this.options.refreshRate)
// ViewBox
this.viewBox = {x: 0, y: 0, width: 0, height: 0}
this.cacheViewBox()
// Process CTM
var newCTM = this.processCTM()
// Update viewport CTM and cache zoom and pan
this.setCTM(newCTM)
// Update CTM in this frame
this.updateCTM()
}
/**
* Cache initial viewBox value
* If no viewBox is defined, then use viewport size/position instead for viewBox values
*/
ShadowViewport.prototype.cacheViewBox = function() {
var svgViewBox = this.options.svg.getAttribute('viewBox')
if (svgViewBox) {
var viewBoxValues = svgViewBox.split(/[\s\,]/).filter(function(v){return v}).map(parseFloat)
// Cache viewbox x and y offset
this.viewBox.x = viewBoxValues[0]
this.viewBox.y = viewBoxValues[1]
this.viewBox.width = viewBoxValues[2]
this.viewBox.height = viewBoxValues[3]
var zoom = Math.min(this.options.width / this.viewBox.width, this.options.height / this.viewBox.height)
// Update active state
this.activeState.zoom = zoom
this.activeState.x = (this.options.width - this.viewBox.width * zoom) / 2
this.activeState.y = (this.options.height - this.viewBox.height * zoom) / 2
// Force updating CTM
this.updateCTMOnNextFrame()
this.options.svg.removeAttribute('viewBox')
} else {
this.simpleViewBoxCache()
}
}
/**
* Recalculate viewport sizes and update viewBox cache
*/
ShadowViewport.prototype.simpleViewBoxCache = function() {
var bBox = this.viewport.getBBox()
this.viewBox.x = bBox.x
this.viewBox.y = bBox.y
this.viewBox.width = bBox.width
this.viewBox.height = bBox.height
}
/**
* Returns a viewbox object. Safe to alter
*
* @return {Object} viewbox object
*/
ShadowViewport.prototype.getViewBox = function() {
return Utils.extend({}, this.viewBox)
}
/**
* Get initial zoom and pan values. Save them into originalState
* Parses viewBox attribute to alter initial sizes
*
* @return {CTM} CTM object based on options
*/
ShadowViewport.prototype.processCTM = function() {
var newCTM = this.getCTM()
if (this.options.fit || this.options.contain) {
var newScale;
if (this.options.fit) {
newScale = Math.min(this.options.width/this.viewBox.width, this.options.height/this.viewBox.height);
} else {
newScale = Math.max(this.options.width/this.viewBox.width, this.options.height/this.viewBox.height);
}
newCTM.a = newScale; //x-scale
newCTM.d = newScale; //y-scale
newCTM.e = -this.viewBox.x * newScale; //x-transform
newCTM.f = -this.viewBox.y * newScale; //y-transform
}
if (this.options.center) {
var offsetX = (this.options.width - (this.viewBox.width + this.viewBox.x * 2) * newCTM.a) * 0.5
, offsetY = (this.options.height - (this.viewBox.height + this.viewBox.y * 2) * newCTM.a) * 0.5
newCTM.e = offsetX
newCTM.f = offsetY
}
// Cache initial values. Based on activeState and fix+center opitons
this.originalState.zoom = newCTM.a
this.originalState.x = newCTM.e
this.originalState.y = newCTM.f
return newCTM
}
/**
* Return originalState object. Safe to alter
*
* @return {Object}
*/
ShadowViewport.prototype.getOriginalState = function() {
return Utils.extend({}, this.originalState)
}
/**
* Return actualState object. Safe to alter
*
* @return {Object}
*/
ShadowViewport.prototype.getState = function() {
return Utils.extend({}, this.activeState)
}
/**
* Get zoom scale
*
* @return {Float} zoom scale
*/
ShadowViewport.prototype.getZoom = function() {
return this.activeState.zoom
}
/**
* Get zoom scale for pubilc usage
*
* @return {Float} zoom scale
*/
ShadowViewport.prototype.getRelativeZoom = function() {
return this.activeState.zoom / this.originalState.zoom
}
/**
* Compute zoom scale for pubilc usage
*
* @return {Float} zoom scale
*/
ShadowViewport.prototype.computeRelativeZoom = function(scale) {
return scale / this.originalState.zoom
}
/**
* Get pan
*
* @return {Object}
*/
ShadowViewport.prototype.getPan = function() {
return {x: this.activeState.x, y: this.activeState.y}
}
/**
* Return cached viewport CTM value that can be safely modified
*
* @return {SVGMatrix}
*/
ShadowViewport.prototype.getCTM = function() {
var safeCTM = this.options.svg.createSVGMatrix()
// Copy values manually as in FF they are not itterable
safeCTM.a = this.activeState.zoom
safeCTM.b = 0
safeCTM.c = 0
safeCTM.d = this.activeState.zoom
safeCTM.e = this.activeState.x
safeCTM.f = this.activeState.y
return safeCTM
}
/**
* Set a new CTM
*
* @param {SVGMatrix} newCTM
*/
ShadowViewport.prototype.setCTM = function(newCTM) {
var willZoom = this.isZoomDifferent(newCTM)
, willPan = this.isPanDifferent(newCTM)
if (willZoom || willPan) {
// Before zoom
if (willZoom) {
// If returns false then cancel zooming
if (this.options.beforeZoom(this.getRelativeZoom(), this.computeRelativeZoom(newCTM.a)) === false) {
newCTM.a = newCTM.d = this.activeState.zoom
willZoom = false
} else {
this.updateCache(newCTM);
this.options.onZoom(this.getRelativeZoom())
}
}
// Before pan
if (willPan) {
var preventPan = this.options.beforePan(this.getPan(), {x: newCTM.e, y: newCTM.f})
// If prevent pan is an object
, preventPanX = false
, preventPanY = false
// If prevent pan is Boolean false
if (preventPan === false) {
// Set x and y same as before
newCTM.e = this.getPan().x
newCTM.f = this.getPan().y
preventPanX = preventPanY = true
} else if (Utils.isObject(preventPan)) {
// Check for X axes attribute
if (preventPan.x === false) {
// Prevent panning on x axes
newCTM.e = this.getPan().x
preventPanX = true
} else if (Utils.isNumber(preventPan.x)) {
// Set a custom pan value
newCTM.e = preventPan.x
}
// Check for Y axes attribute
if (preventPan.y === false) {
// Prevent panning on x axes
newCTM.f = this.getPan().y
preventPanY = true
} else if (Utils.isNumber(preventPan.y)) {
// Set a custom pan value
newCTM.f = preventPan.y
}
}
// Update willPan flag
// Check if newCTM is still different
if ((preventPanX && preventPanY) || !this.isPanDifferent(newCTM)) {
willPan = false
} else {
this.updateCache(newCTM);
this.options.onPan(this.getPan());
}
}
// Check again if should zoom or pan
if (willZoom || willPan) {
this.updateCTMOnNextFrame()
}
}
}
ShadowViewport.prototype.isZoomDifferent = function(newCTM) {
return this.activeState.zoom !== newCTM.a
}
ShadowViewport.prototype.isPanDifferent = function(newCTM) {
return this.activeState.x !== newCTM.e || this.activeState.y !== newCTM.f
}
/**
* Update cached CTM and active state
*
* @param {SVGMatrix} newCTM
*/
ShadowViewport.prototype.updateCache = function(newCTM) {
this.activeState.zoom = newCTM.a
this.activeState.x = newCTM.e
this.activeState.y = newCTM.f
}
ShadowViewport.prototype.pendingUpdate = false
/**
* Place a request to update CTM on next Frame
*/
ShadowViewport.prototype.updateCTMOnNextFrame = function() {
if (!this.pendingUpdate) {
// Lock
this.pendingUpdate = true
// Throttle next update
this.requestAnimationFrame.call(window, this.updateCTMCached)
}
}
/**
* Update viewport CTM with cached CTM
*/
ShadowViewport.prototype.updateCTM = function() {
var ctm = this.getCTM()
// Updates SVG element
SvgUtils.setCTM(this.viewport, ctm, this.defs)
// Free the lock
this.pendingUpdate = false
// Notify about the update
if(this.options.onUpdatedCTM) {
this.options.onUpdatedCTM(ctm)
}
}
module.exports = function(viewport, options){
return new ShadowViewport(viewport, options)
}
},{"./svg-utilities":5,"./utilities":7}],4:[function(require,module,exports){
var Wheel = require('./uniwheel')
, ControlIcons = require('./control-icons')
, Utils = require('./utilities')
, SvgUtils = require('./svg-utilities')
, ShadowViewport = require('./shadow-viewport')
var SvgPanZoom = function(svg, options) {
this.init(svg, options)
}
var 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
}
var passiveListenerOption = {passive: true};
SvgPanZoom.prototype.init = function(svg, options) {
var 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
var 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
var 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() {
var that = this
, prevEvt = null // use for touchstart event to detect double tap
;
this.eventListeners = {
// Mouse down group
mousedown: function(evt) {
var result = that.handleMouseDown(evt, prevEvt);
prevEvt = evt
return result;
}
, touchstart: function(evt) {
var 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
if (this.options.customEventsHandler != null) { // jshint ignore:line
this.options.customEventsHandler.init({
svgElement: this.svg
, eventsListenerElement: this.options.eventsListenerElement
, instance: this.getPublicInstance()
})
// Custom event handler may halt builtin listeners
var haltEventListeners = this.options.customEventsHandler.haltEventListeners
if (haltEventListeners && haltEventListeners.length) {
for (var i = haltEventListeners.length - 1; i >= 0; i--) {
if (this.eventListeners.hasOwnProperty(haltEventListeners[i])) {
delete this.eventListeners[haltEventListeners[i]]
}
}
}
}
// Bind eventListeners
for (var 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) {
var that = this
// Mouse wheel listener
this.wheelListener = function(evt) {
return that.handleMouseWheel(evt);
}
// Bind wheelListener
var 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) {
var 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;
}
}
// Default delta in case that deltaY is not available
var 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
var 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) {
var 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()
}
var 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() {
var 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) {
var targetClass = evt.target.getAttribute('class') || ''
if (targetClass.indexOf('svg-pan-zoom-control') > -1) {
return false
}
}
var zoomFactor
if (evt.shiftKey) {
zoomFactor = 1/((1 + this.options.zoomScaleSensitivity) * 2) // zoom out when shift key pressed
} else {
zoomFactor = (1 + this.options.zoomScaleSensitivity) * 2
}
var 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
var 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() {
var 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() {