-
Notifications
You must be signed in to change notification settings - Fork 46
/
snap.svg.zpd.js
940 lines (731 loc) · 35.3 KB
/
snap.svg.zpd.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
/* globals Snap, document, navigator */
/**
* snapsvg-zpd.js: A zoom/pan/drag plugin for Snap.svg
* ==================================================
*
* Usage
* =======
* var paper = Snap();
* var bigCircle = paper.circle(150, 150, 100);
* paper.zpd();
*
* // or settings and callback
* paper.zpd({ zoom: false }), function (err, paper) { });
*
* // or callback
* paper.zpd(function (err, paper) { });
*
* // destroy
* paper.zpd('destroy');
*
* // save
* paper.zpd('save');
*
* // load
* // paper.zpd({ load: SVGMatrix {} });
*
* // origin
* paper.zpd('origin');
*
* // zoomTo
* paper.zoomTo(1);
*
* // panTo
* paper.panTo(0, 0); // original location
* paper.panTo('+10', 0); // move right
*
* // rotate
* paper.rotate(15); // rotate 15 deg
*
* // change pan directions
* paper.zpd({'panDirections':'horizontal'});
*
* Notice
* ========
* This usually use on present view only. Not for Storing, modifying the paper.
*
* Reason:
* Usually <pan> <zoom> => <svg transform="matrix(a,b,c,d,e,f)"></svg>
*
* But if you need to store the <drag> location, (for storing)
* we have to use <circle cx="x" cy="y"></circle> not <circle tranform="matrix(a,b,c,d,e,f)"></circle>
*
* License
* =========
* This code is licensed under the following BSD license:
*
* Copyright 2014-2020 Huei Tan (Snap.svg integration). All rights reserved.
* Copyright 2009-2010 Andrea Leofreddi <[email protected]> (original author). All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY Andrea Leofreddi ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Andrea Leofreddi OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of Andrea Leofreddi.
*/
SVGElement.prototype.getTransformToElement = SVGElement.prototype.getTransformToElement || function(elem) {
return elem.getScreenCTM().inverse().multiply(this.getScreenCTM());
};
(function (Snap) {
Snap.plugin(function (Snap, Element, Paper, glob, Fragment) {
/**
* Global variable for snap.svg.zpd plugin
*/
var snapsvgzpd = {
uniqueIdPrefix: 'snapsvg-zpd-', // prefix for the unique ids created for zpd
dataStore: {}, // "global" storage for all our zpd elements
enable: true // By default, snapsvgzpd should enable, zpd('toggle') to toggle enable or disable
};
/**
* Global variable to store root of the svg element
*/
var rootSvgObject;
/**
* remove node parent but keep children
*/
var _removeNodeKeepChildren = function removeNodeKeepChildren(node) {
if (!node.parentNode) {
return;
}
while (node.firstChild) {
node.parentNode.insertBefore(node.firstChild, node);
}
node.parentNode.removeChild(node);
};
/**
* Detect is +1 -1 or 1
* increase decrease or just number
*/
var _increaseDecreaseOrNumber = function increaseDecreaseOrNumber(defaultValue, input) {
if (input === undefined) {
return parseInt(defaultValue);
} else if (input[0] == '+') {
return defaultValue + parseInt(input.split('+')[1]);
} else if (input[0] == '-') {
return defaultValue - parseInt(input.split('-')[1]);
} else {
return parseInt(input);
}
};
/**
* Sets the current transform matrix of an element.
*/
var _setCTM = function setCTM(element, matrix) {
var s = "matrix(" + matrix.a + "," + matrix.b + "," + matrix.c + "," + matrix.d + "," + matrix.e + "," + matrix.f + ")";
element.setAttribute("transform", s);
};
/**
* Dumps a matrix to a string (useful for debug).
*/
var _dumpMatrix = function dumpMatrix(matrix) {
var s = "[ " + matrix.a + ", " + matrix.c + ", " + matrix.e + "\n " + matrix.b + ", " + matrix.d + ", " + matrix.f + "\n 0, 0, 1 ]";
return s;
};
/**
* Instance an SVGPoint object with given event coordinates.
*/
var _findPos = function findPos(obj) {
var curleft = curtop = 0;
var boundingClientRect = obj.getBoundingClientRect();
if (boundingClientRect) {
curleft = boundingClientRect.left;
curtop = boundingClientRect.top;
}
return [curleft, curtop];
};
var _getEventPoint = function getEventPoint(event, svgNode) {
var p = svgNode.node.createSVGPoint(),
svgPos = _findPos(svgNode.node);
if (typeof event.touches != 'undefined') {
p.x = event.touches[0].clientX - svgPos[0];
p.y = event.touches[0].clientY - svgPos[1];
} else {
p.x = event.clientX - svgPos[0];
p.y = event.clientY - svgPos[1];
}
return p;
};
/**
* Detect multi-touch (i.e. pinch)
*/
var _isTwoTouch = function isTwoTouch(event) {
var b = false;
if (typeof event.touches != 'undefined' && event.touches.length == 2) {
b = true;
}
return b;
};
/**
* Calculate the distance between the 1st and 2nd touches
*/
var _getMultiTouchDistance = function getMultiTouchDistance(event) {
return Math.sqrt(
(event.touches[0].clientX - event.touches[1].clientX) * (event.touches[0].clientX - event.touches[1].clientX) +
(event.touches[0].clientY - event.touches[1].clientY) * (event.touches[0].clientY - event.touches[1].clientY));
};
/**
* Common function to handle the zooming for both touch and mouse wheel.
*/
var _handleZoomingEvent = function handleZoomingEvent(event, zpdElement, delta) {
var z = Math.pow(1 + zpdElement.options.zoomScale, delta);
var g = zpdElement.element.node;
var p = _getEventPoint(event, zpdElement.data.svg);
p = p.matrixTransform(g.getCTM().inverse());
// Compute new scale matrix in current mouse position
var k = zpdElement.data.root.createSVGMatrix().translate(p.x, p.y).scale(z).translate(-p.x, -p.y);
var matrix = g.getCTM().multiply(k);
// detecting if zoom threshold was exceeded
var recalculateMatrix = function recalculateMatrix(scale) {
z = scale / g.getCTM().a;
k = zpdElement.data.root.createSVGMatrix().translate(p.x, p.y).scale(z).translate(-p.x, -p.y);
matrix = g.getCTM().multiply(k);
matrix.a = matrix.a.toFixed(4);
matrix.d = matrix.d.toFixed(4);
}
var threshold = zpdElement.options.zoomThreshold;
if (threshold && typeof threshold === 'object') { // array [0.5,2]
var oldMatrix = Snap(g).transform().globalMatrix;
if ( (matrix.a < oldMatrix.a && matrix.a < threshold[0])
|| (matrix.d < oldMatrix.d && matrix.d < threshold[0])) {
recalculateMatrix(threshold[0]);
} else if ( (matrix.a > oldMatrix.a && matrix.a > threshold[1])
|| (matrix.d > oldMatrix.d && matrix.d > threshold[1])) {
recalculateMatrix(threshold[1]);
}
}
_setCTM(g, matrix);
if (typeof(stateTf) == 'undefined') {
zpdElement.data.stateTf = g.getCTM().inverse();
}
zpdElement.data.stateTf = zpdElement.data.stateTf.multiply(k.inverse());
};
/**
* Get an svg transformation matrix as string representation
*/
var _getSvgMatrixAsString = function _getMatrixAsString (matrix) {
return 'matrix(' + matrix.a + ',' + matrix.b + ',' + matrix.c + ',' + matrix.d + ',' + matrix.e + ',' + matrix.f + ')';
};
/**
* add a new <g> element to the paper
* add paper nodes into <g> element (Snapsvg Element)
* and give the nodes an unique id like 'snapsvg-zpd-12345'
* and let this <g> Element to global snapsvgzpd.dataStore['snapsvg-zpd-12345']
* and
* <svg>
* <def>something</def>
* <circle cx="10" cy="10" r="100"></circle>
* </svg>
*
* transform to =>
*
* <svg>
* <g id="snapsvg-zpd-12345">
* <def>something</def>
* <circle cx="10" cy="10" r="100"></circle>
* </g>
* </svg>
*/
var _initZpdElement = function initAndGetZpdElement (svgObject, options) {
// get root of svg object
rootSvgObject = svgObject.node;
// get all child nodes in our svg element
var rootChildNodes = svgObject.node.childNodes;
// create a new graphics element in our svg element
var gElement = svgObject.g();
var gNode = gElement.node;
// add our unique id to the element
gNode.id = snapsvgzpd.uniqueIdPrefix + svgObject.id;
// check if a matrix has been supplied to initialize the drawing
if (options.load && typeof options.load === 'object') {
var matrix = options.load;
// create a matrix string from our supplied matrix
var matrixString = "matrix(" + matrix.a + "," + matrix.b + "," + matrix.c + "," + matrix.d + "," + matrix.e + "," + matrix.f + ")";
// load <g> transform matrix
gElement.transform(matrixString);
} else {
// initial set <g transform="matrix(1,0,0,1,0,0)">
gElement.transform('matrix');
}
// initialize our index counter for child nodes
var index = 0;
// get the number of child nodes in our root node
// substract -1 to exclude our <g> element
var noOfChildNodes = rootChildNodes.length - 1;
// go through all child elements
// (except the last one, which is our <g> element)
while (index < noOfChildNodes) {
gNode.appendChild(rootChildNodes[0]);
index += 1;
}
// define some data to be used in the function internally
var data = {
svg: svgObject,
root: svgObject.node, // get paper svg
state: 'none',
stateTarget: null,
stateOrigin: null,
stateTf: null,
touchZoom: false,
prevZoomDistance: null,
};
// create an element with all required properties
var item = {
"element": gElement,
"data": data,
"options": options,
};
// create some mouse event handlers for our item
// store them globally for optional removal later on
item.handlerFunctions = _getHandlerFunctions(item);
// return our element
return item;
};
/**
* create some handler functions for our mouse actions
* we will take advantace of closures to preserve some data
*/
var _getHandlerFunctions = function getHandlerFunctions(zpdElement) {
var handleMouseOrTouchUp = function handleMouseOrTouchUp (event) {
if (event.preventDefault && zpdElement.options.preventDefaultEvent.handleMouseOrTouchUp) {
event.preventDefault();
}
if (!snapsvgzpd.enable) return;
event.returnValue = false;
// On touchend reset the touchZoom variable to false
zpdElement.data.touchZoom = false;
// On touchend reset the prevZoomDistance variable to default value
zpdElement.data.prevZoomDistance = null
if (zpdElement.data.state == 'pan' || zpdElement.data.state == 'drag') {
// quit pan mode
zpdElement.data.state = '';
}
};
var handleMouseOrTouchDown = function handleMouseOrTouchDown (event) {
if (event.preventDefault && zpdElement.options.preventDefaultEvent.handleMouseOrTouchDown) {
event.preventDefault();
}
if (!snapsvgzpd.enable) return;
event.returnValue = false;
// Detect if multi-touch and set touchZoom variable, this will be used in determining when to pan or zoom
if (zpdElement.options.touch) zpdElement.data.touchZoom = _isTwoTouch(event);
var g = zpdElement.element.node;
if (
event.target.tagName == "svg" || !zpdElement.options.drag // Pan anyway when drag is disabled and the user clicked on an element
) {
// Pan mode
zpdElement.data.state = 'pan';
zpdElement.data.stateTf = g.getCTM().inverse();
zpdElement.data.stateOrigin = _getEventPoint(event, zpdElement.data.svg).matrixTransform(zpdElement.data.stateTf);
} else {
// Drag mode
zpdElement.data.state = 'drag';
zpdElement.data.stateTarget = event.target;
zpdElement.data.stateTf = g.getCTM().inverse();
zpdElement.data.stateOrigin = _getEventPoint(event, zpdElement.data.svg).matrixTransform(zpdElement.data.stateTf);
}
};
var handleMouseMove = function handleMouseMove (event) {
if (event.preventDefault && zpdElement.options.preventDefaultEvent.handleMouseMove) {
event.preventDefault();
}
if (!snapsvgzpd.enable) return;
event.returnValue = false;
var g = zpdElement.element.node;
if (zpdElement.data.state == 'pan' && zpdElement.options.pan) {
// Pan mode
var p = _getEventPoint(event, zpdElement.data.svg).matrixTransform(zpdElement.data.stateTf);
var trans_x=0;
var trans_y=0;
if ((zpdElement.options.panDirections == 'both') || (zpdElement.options.panDirections == 'horizontal')) {
var trans_x=p.x - zpdElement.data.stateOrigin.x;
}
if ((zpdElement.options.panDirections == 'both') || (zpdElement.options.panDirections == 'vertical')) {
var trans_y=p.y - zpdElement.data.stateOrigin.y;
}
_setCTM(g, zpdElement.data.stateTf.inverse().translate(trans_x,trans_y));
} else if (zpdElement.data.state == 'drag' && zpdElement.options.drag) {
// Drag mode
var dragPoint = _getEventPoint(event, zpdElement.data.svg).matrixTransform(g.getCTM().inverse());
var trans_x=0;
var trans_y=0;
if ((zpdElement.options.panDirections == 'both') || (zpdElement.options.panDirections == 'horizontal')) {
var trans_x=dragPoint.x - zpdElement.data.stateOrigin.x;
}
if ((zpdElement.options.panDirections == 'both') || (zpdElement.options.panDirections == 'vertical')) {
var trans_y=dragPoint.y - zpdElement.data.stateOrigin.y;
}
_setCTM(zpdElement.data.stateTarget,
zpdElement.data.root.createSVGMatrix()
.translate(trans_x, trans_y)
.multiply(g.getCTM().inverse())
.multiply(zpdElement.data.stateTarget.getCTM()));
zpdElement.data.stateOrigin = dragPoint;
}
};
var handleMouseWheel = function handleMouseWheel (event) {
if (!zpdElement.options.zoom) {
return;
}
if (event.preventDefault && zpdElement.options.preventDefaultEvent.handleMouseWheel) {
event.preventDefault();
}
if (!snapsvgzpd.enable) return;
event.returnValue = false;
var delta = 0;
if (event.wheelDelta) {
delta = event.wheelDelta / 360; // Chrome/Safari
}
else {
delta = event.detail / -9; // Mozilla
}
_handleZoomingEvent(event, zpdElement, delta);
};
var handleTouchMove = function handleTouchMove (event) {
if (!zpdElement.options.zoom || !zpdElement.options.touch) {
return;
}
if (event.preventDefault && zpdElement.options.preventDefaultEvent.handleTouchMove) {
event.preventDefault();
}
if (!snapsvgzpd.enable) return;
event.returnValue = false;
// If multi-touch is true, then we are zooming instead of panning or dragging.
if (zpdElement.data.touchZoom) {
var distance = _getMultiTouchDistance(event);
if (zpdElement.data.prevZoomDistance != null) {
// The delta value is set to 0.15 as it best matches the zoom sensitivity in browsers. Use zoomScale option to change the zoom sensitivity.
var delta = 0.15; // Case for the pinch being opened.
// Case for pinch being closed, make the delta negative
if (zpdElement.data.prevZoomDistance > distance) delta = delta * -1;
// Stop zooming event when distance equal to prevZoomDistance
if (zpdElement.data.prevZoomDistance != distance) _handleZoomingEvent(event, zpdElement, delta);
}
// Store the distance between touch positions so we can compare the changes to see if it's getting larger or smaller.
zpdElement.data.prevZoomDistance = distance;
} else {
handleMouseMove (event);
}
};
return {
"mouseOrTouchUp": handleMouseOrTouchUp,
"mouseOrTouchDown": handleMouseOrTouchDown,
"mouseMove": handleMouseMove,
"mouseWheel": handleMouseWheel,
"touchMove": handleTouchMove
};
};
/**
* Register handlers
* desktop and mobile
*/
var _setupHandlers = function setupHandlers(svgElement, handlerFunctions) {
// mobile
if ('ontouchend' in document.documentElement) {
svgElement.addEventListener('touchend', handlerFunctions.mouseOrTouchUp, false);
svgElement.addEventListener('touchcancel', handlerFunctions.mouseOrTouchUp, false);
svgElement.addEventListener('touchstart', handlerFunctions.mouseOrTouchDown, false);
// This event handles both panning and zooming
svgElement.addEventListener('touchmove', handlerFunctions.touchMove, false);
// desktop
} else if ('onmouseup' in document.documentElement) {
// IE < 9 would need to use the event onmouseup, but they do not support svg anyway..
svgElement.addEventListener('mouseup', handlerFunctions.mouseOrTouchUp, false);
svgElement.addEventListener('mousedown', handlerFunctions.mouseOrTouchDown, false);
svgElement.addEventListener('mousemove', handlerFunctions.mouseMove, false);
if (navigator.userAgent.toLowerCase().indexOf('webkit') >= 0 ||
navigator.userAgent.toLowerCase().indexOf('trident') >= 0) {
svgElement.addEventListener('mousewheel', handlerFunctions.mouseWheel, false); // Chrome/Safari
}
else {
svgElement.addEventListener('DOMMouseScroll', handlerFunctions.mouseWheel, false); // Others
}
}
};
/**
* remove event handlers
*/
var _tearDownHandlers = function tearDownHandlers(svgElement, handlerFunctions) {
// mobile
if ('ontouchend' in document.documentElement) {
svgElement.removeEventListener('touchend', handlerFunctions.mouseOrTouchUp, false);
svgElement.removeEventListener('touchcancel', handlerFunctions.mouseOrTouchUp, false);
svgElement.removeEventListener('touchstart', handlerFunctions.mouseOrTouchDown, false);
// This event handles both panning and zooming
svgElement.removeEventListener('touchmove', handlerFunctions.touchMove, false);
// desktop
} else if ('onmouseup' in document.documentElement) {
svgElement.removeEventListener('mouseup', handlerFunctions.mouseOrTouchUp, false);
svgElement.removeEventListener('mousedown', handlerFunctions.mouseOrTouchDown, false);
svgElement.removeEventListener('mousemove', handlerFunctions.mouseMove, false);
if (navigator.userAgent.toLowerCase().indexOf('webkit') >= 0 ||
navigator.userAgent.toLowerCase().indexOf('trident') >= 0) {
svgElement.removeEventListener('mousewheel', handlerFunctions.mouseWheel, false);
}
else {
svgElement.removeEventListener('DOMMouseScroll', handlerFunctions.mouseWheel, false);
}
}
};
/* our global zpd function */
var zpd = function (options, callbackFunc) {
// get a reference to the current element
var self = this;
// define some custom options
var zpdOptions = {
pan: true, // enable or disable panning (default enabled)
panDirections: 'both', // "both" | "horizontal" | "vertical"
zoom: true, // enable or disable zooming (default enabled)
drag: false, // enable or disable dragging (default disabled)
zoomScale: 0.2, // define zoom sensitivity
zoomThreshold: null, // define zoom threshold
touch: true, // enable or disable touch (default enabled)
preventDefaultEvent: { // enable or disable preventDefault call in events (default enabled) WARNING may have unwanted effect
handleMouseOrTouchUp: true,
handleMouseOrTouchDown: true,
handleMouseMove: true,
handleMouseWheel: true,
handleTouchMove: true
}
};
// the situation event of zpd, may be init, reinit, destroy, save, origin, toggle
var situation,
situationState = {
init: 'init',
reinit: 'reinit',
destroy: 'destroy',
save: 'save',
origin: 'origin',
callback: 'callback',
toggle: 'toggle'
};
var zpdElement = null;
// it is also possible to only specify a callback function without any options
if (typeof options === 'function') {
callbackFunc = options;
situation = situationState.callback;
}
// check if element was already initialized
if (snapsvgzpd.dataStore.hasOwnProperty(self.id)) {
// return existing element
zpdElement = snapsvgzpd.dataStore[self.id];
// adapt the stored options, with the options passed in
if (typeof options === 'object') {
if (options.preventDefaultEvent !== undefined && typeof options.preventDefaultEvent !== 'object') {
options.preventDefaultEvent = {
handleMouseOrTouchUp: !!options.preventDefaultEvent,
handleMouseOrTouchDown: !!options.preventDefaultEvent,
handleMouseMove: !!options.preventDefaultEvent,
handleMouseWheel: !!options.preventDefaultEvent,
handleTouchMove: !!options.preventDefaultEvent
}
}
for (var prop in options) {
if (typeof options[prop] === 'object') {
if (typeof zpdElement.options[prop] !== 'object') {
zpdElement.options[prop] = options[prop];
}
for (var subprop in options[prop]) {
zpdElement.options[prop][subprop] = options[prop][subprop];
}
} else {
zpdElement.options[prop] = options[prop];
}
}
situation = situationState.reinit;
} else if (typeof options === 'string') {
situation = options;
}
}
else {
// adapt the default options
if (typeof options === 'object') {
if (options.preventDefaultEvent !== undefined && typeof options.preventDefaultEvent !== 'object') {
options.preventDefaultEvent = {
handleMouseOrTouchUp: !!options.preventDefaultEvent,
handleMouseOrTouchDown: !!options.preventDefaultEvent,
handleMouseMove: !!options.preventDefaultEvent,
handleMouseWheel: !!options.preventDefaultEvent,
handleTouchMove: !!options.preventDefaultEvent
}
}
for (var prop2 in options) {
if (typeof options[prop2] === 'object') {
if (typeof zpdOptions[prop2] !== 'object' || zpdOptions[prop2] === null) {
zpdOptions[prop2] = options[prop2];
}
for (var subprop2 in options[prop2]) {
zpdOptions[prop2][subprop2] = options[prop2][subprop2];
}
} else {
zpdOptions[prop2] = options[prop2];
}
}
situation = situationState.init;
} else if (typeof options === 'string') {
situation = options;
}
// initialize a new element and save it to our global storage
zpdElement = _initZpdElement(self, zpdOptions);
// setup the handlers for our svg-canvas
_setupHandlers(self.node, zpdElement.handlerFunctions);
snapsvgzpd.dataStore[self.id] = zpdElement;
}
switch (situation) {
case situationState.init:
case situationState.reinit:
case situationState.callback:
// callback
if (callbackFunc) {
callbackFunc(null, zpdElement);
}
return;
case situationState.destroy:
// remove event handlers
_tearDownHandlers(self.node, zpdElement.handlerFunctions);
// remove our custom <g> element
_removeNodeKeepChildren(self.node.firstChild);
// remove the object from our internal storage
delete snapsvgzpd.dataStore[self.id];
// callback
if (callbackFunc) {
callbackFunc(null, zpdElement);
}
return; // exit all
case situationState.save:
var g = document.getElementById(snapsvgzpd.uniqueIdPrefix + self.id);
var returnValue = g.getCTM();
// callback
if (callbackFunc) {
callbackFunc(null, returnValue);
}
return returnValue;
case situationState.origin:
// back to origin location
self.zoomTo(1, 1000);
// callback
if (callbackFunc) {
callbackFunc(null, zpdElement);
}
return;
case situationState.toggle:
// toggle enabled
snapsvgzpd.enable = !snapsvgzpd.enable;
// callback
if (callbackFunc) {
callbackFunc(null, snapsvgzpd.enable);
}
return;
}
};
/**
* zoom element to a certain zoom factor
*/
var zoomTo = function (zoom, interval, ease, callbackFunction) {
if (zoom < 0 || typeof zoom !== 'number') {
console.error('zoomTo(arg) should be a number and greater than 0');
return;
}
if (typeof interval !== 'number') {
interval = 3000;
}
var self = this;
// check if we have this element in our zpd data storage
if (snapsvgzpd.dataStore.hasOwnProperty(self.id)) {
// get a reference to the element
var zpdElement = snapsvgzpd.dataStore[self.id].element;
var currentTransformMatrix = zpdElement.node.getTransformToElement(rootSvgObject);
var currentZoom = currentTransformMatrix.a;
var originX = currentTransformMatrix.e;
var originY = currentTransformMatrix.f;
var boundingBox = zpdElement.getBBox();
var deltaX = parseFloat(boundingBox.width) / 2.0;
var deltaY = parseFloat(boundingBox.height) / 2.0;
Snap.animate(currentZoom, zoom, function (value) {
// calculate difference of zooming value to initial zoom
var deltaZoom = value / currentZoom;
if (value !== currentZoom) {
// calculate new translation
currentTransformMatrix.e = originX - ((deltaX * deltaZoom - deltaX));
currentTransformMatrix.f = originY - ((deltaY * deltaZoom - deltaY));
// add new scaling
currentTransformMatrix.a = value;
currentTransformMatrix.d = value;
// apply transformation to our element
zpdElement.node.setAttribute('transform', _getSvgMatrixAsString(currentTransformMatrix));
}
}, interval, ease, callbackFunction);
}
};
/**
* move the element to a certain position
*/
var panTo = function (x, y, interval, ease, cb) {
// get a reference to the current element
var self = this;
// check if we have this element in our zpd data storage
if (snapsvgzpd.dataStore.hasOwnProperty(self.id)) {
var zpdElement = snapsvgzpd.dataStore[self.id].element;
var gMatrix = zpdElement.node.getCTM(),
matrixX = _increaseDecreaseOrNumber(gMatrix.e, x),
matrixY = _increaseDecreaseOrNumber(gMatrix.f, y),
matrixString = "matrix(" + gMatrix.a + "," + gMatrix.b + "," + gMatrix.c + "," + gMatrix.d + "," + matrixX + "," + matrixY + ")";
// dataStore[me.id].transform(matrixString); // load <g> transform matrix
zpdElement.animate({ transform: matrixString }, interval || 10, ease || null, function () {
if (cb) {
cb(null, zpdElement);
}
});
}
};
/**
* rotate the element to a certain rotation
*/
var rotate = function (a, x, y, interval, ease, cb) {
// get a reference to the current element
var self = this;
// check if we have this element in our zpd data storage
if (snapsvgzpd.dataStore.hasOwnProperty(self.id)) {
var zpdElement = snapsvgzpd.dataStore[self.id].element;
var gMatrix = zpdElement.node.getCTM(),
matrixString = "matrix(" + gMatrix.a + "," + gMatrix.b + "," + gMatrix.c + "," + gMatrix.d + "," + gMatrix.e + "," + gMatrix.f + ")";
if (!x || typeof x !== 'number') {
x = self.node.offsetWidth / 2;
}
if (!y || typeof y !== 'number') {
y = self.node.offsetHeight / 2;
}
// dataStore[me.id].transform(matrixString); // load <g> transform matrix
zpdElement.animate({ transform: new Snap.Matrix(gMatrix).rotate(a, x, y) }, interval || 10, ease || null, function () {
if (cb) {
cb(null, zpdElement);
}
});
}
};
Paper.prototype.zpd = zpd;
Paper.prototype.zoomTo = zoomTo;
Paper.prototype.panTo = panTo;
Paper.prototype.rotate = rotate;
/** More Features to add (click event) help me if you can **/
// Element.prototype.panToCenter = panToCenter; // arg (ease, interval, cb)
/** UI for zpdr **/
});
})(Snap);