forked from hueitan/snap.svg.zpd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
snap.svg.zpd.js
833 lines (653 loc) · 29.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
/* 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
*
* 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 Huei Tan <[email protected]> (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.
*/
(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
};
/**
* Global variable to store root of the svg element
*/
var rootSvgObject;
/**
* remove node parent but keep children
*/
var _removeNodeKeepChildren = function removeNodeKeepChildren(node) {
if (!node.parentElement) {
return;
}
while (node.firstChild) {
node.parentElement.insertBefore(node.firstChild, node);
}
node.parentElement.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, threshold) {
if (threshold && typeof threshold === 'object') { // array [0.5,2]
if (matrix.a <= threshold[0]) {
return;
}
if (matrix.d >= threshold[1]) {
return;
}
}
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 _getEventPoint = function getEventPoint(event, svgNode) {
var p = svgNode.node.createSVGPoint();
p.x = event.clientX;
p.y = event.clientY;
return p;
};
/**
* 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
};
// 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);
// trigger initial onPan (e.g. to provide overlaps)
_constrainPan(gNode.getCTM(), item);
// return our element
return item;
};
/**
* Default implementation for determining pan constraints.
*/
var _defaultDeterminePanConstraints = function(contentWidth, contentHeight, viewportWidth, viewportHeight) {
var dxMin = 0;
var dyMin = 0;
var dxMax = 0;
var dyMax = 0;
if (contentWidth < viewportWidth) {
// don't let content leave the viewport
dxMax = viewportWidth - contentWidth;
}
else {
dxMin = viewportWidth - contentWidth;
}
if (contentHeight < viewportHeight) {
// don't let content leave the viewport
dyMax = viewportHeight - contentHeight;
}
else {
dyMin = viewportHeight - contentHeight;
}
return {
dxMin: dxMin,
dxMax: dxMax,
dyMin: dyMin,
dyMax: dyMax
};
};
/**
* Constrain panning if applicable, trigger an onPan callback if it was provided.
*
* FIXME not sure what implications zooming might have
*/
var _constrainPan = function(matrix, zpdElement) {
var gBB = zpdElement.element.node.getBoundingClientRect();
var svgBB = zpdElement.data.root.getBoundingClientRect();
// determine original offset of group from SVG element
var currentCtm = zpdElement.element.node.getCTM();
var offsetX = gBB.left - currentCtm.e - svgBB.left;
var offsetY = gBB.top - currentCtm.f - svgBB.top;
// apply offsets for content size (TODO configurable?)
var contentWidth = gBB.width + 2 * offsetX;
var contentHeight = gBB.height + 2 * offsetY;
// apply constraints if applicable
var setting = zpdElement.options.constrainPan;
if (setting) {
var determineConstraints = _defaultDeterminePanConstraints;
var constraints = null;
if (typeof(setting) == 'function') {
// custom function to determine constraints
determineConstraints = setting;
}
else if (typeof(setting) == 'object') {
// fixed constraints
constraints = setting;
}
if (!constraints) {
// determine constraints via function
constraints = determineConstraints(contentWidth, contentHeight, svgBB.width, svgBB.height);
}
// adjust matrix translation
matrix.e = Math.min(Math.max(matrix.e, constraints.dxMin), constraints.dxMax);
matrix.f = Math.min(Math.max(matrix.f, constraints.dyMin), constraints.dyMax);
}
_setCTM(zpdElement.element.node, matrix);
return {'svgBB': svgBB, 'width': contentWidth, 'height': contentHeight};
};
var _triggerPan = function(matrix, zpdElement) {
// trigger onPan
constrainPan = _constrainPan(matrix, zpdElement);
if (zpdElement.options.onPan) {
var overlaps = {
'left': matrix.e < 0,
'top': matrix.f < 0,
'right': constrainPan.width + matrix.e > constrainPan.svgBB.width,
'bottom': constrainPan.height + matrix.f > constrainPan.svgBB.height
};
zpdElement.options.onPan(matrix.e, matrix.f, overlaps);
}
};
/**
* create some handler functions for our mouse actions
* we will take advantace of closures to preserve some data
*/
var _getHandlerFunctions = function getHandlerFunctions(zpdElement) {
var handleMouseUp = function handleMouseUp (event) {
if (event.preventDefault) {
event.preventDefault();
}
event.returnValue = false;
if (zpdElement.data.state == 'pan' || zpdElement.data.state == 'drag') {
// quit pan mode
zpdElement.data.state = '';
}
};
var handleMouseDown = function handleMouseDown (event) {
if (event.preventDefault) {
event.preventDefault();
}
event.returnValue = false;
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) {
event.preventDefault();
}
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 matrix = zpdElement.data.stateTf.inverse().translate(p.x - zpdElement.data.stateOrigin.x, p.y - zpdElement.data.stateOrigin.y);
_triggerPan(matrix, zpdElement);
} else if (zpdElement.data.state == 'drag' && zpdElement.options.drag) {
// Drag mode
var dragPoint = _getEventPoint(event, zpdElement.data.svg).matrixTransform(g.getCTM().inverse());
_setCTM(zpdElement.data.stateTarget,
zpdElement.data.root.createSVGMatrix()
.translate(dragPoint.x - zpdElement.data.stateOrigin.x, dragPoint.y - zpdElement.data.stateOrigin.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) {
event.preventDefault();
}
event.returnValue = false;
var delta = 0;
if (event.wheelDelta) {
delta = event.wheelDelta / 360; // Chrome/Safari
}
else {
delta = event.detail / -9; // Mozilla
}
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);
_setCTM(g, g.getCTM().multiply(k), zpdElement.options.zoomThreshold);
if (typeof(stateTf) == 'undefined') {
zpdElement.data.stateTf = g.getCTM().inverse();
}
zpdElement.data.stateTf = zpdElement.data.stateTf.multiply(k.inverse());
};
return {
"mouseUp": handleMouseUp,
"mouseDown": handleMouseDown,
"mouseMove": handleMouseMove,
"mouseWheel": handleMouseWheel
};
};
/**
* Register handlers
* desktop and mobile (?)
*/
var _setupHandlers = function setupHandlers(svgElement, handlerFunctions) {
// mobile
// (?)
// desktop
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.mouseUp, false);
svgElement.addEventListener('mousedown', handlerFunctions.mouseDown, 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) {
svgElement.removeEventListener('mouseup', handlerFunctions.mouseUp, false);
svgElement.removeEventListener('mousedown', handlerFunctions.mouseDown, 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)
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
};
// the situation event of zpd, may be init, reinit, destroy, save, origin
var situation,
situationState = {
init: 'init',
reinit: 'reinit',
destroy: 'destroy',
save: 'save',
origin: 'origin',
callback: 'callback',
sizes: 'sizes'
};
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') {
for (var prop in options) {
zpdElement.options[prop] = options[prop];
}
situation = situationState.reinit;
} else if (typeof options === 'string') {
situation = options;
}
}
else {
// adapt the default options
if (typeof options === 'object') {
for (var prop2 in options) {
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.sizes:
_constrainPan(zpdElement.element.node.getCTM(), zpdElement);
return _allIn(zpdElement);
}
};
/**
* 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);
}
});
}
};
/**
* Get canvas and viewport sizes
*/
var _allIn = function(zpdElement){
// Canvas width and height
var nodes = zpdElement.element.node,
zpd = nodes.getBoundingClientRect(),
currentCtm = nodes.getCTM(),
viewport = zpdElement.data.root.getBoundingClientRect(),
offset = nodes.getCTM();
return {
// 'left': viewport.width < (currentCtm.e), // future
'width': viewport.width < (zpd.width + currentCtm.e),
'height': viewport.height < (zpd.height + currentCtm.f)
};
};
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);