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
|
/* svg.js v0.1-49-g7d3e4c4 - svg container element event group arrange defs clip mask gradient doc shape rect ellipse poly path image text sugar - svgjs.com/license */
(function() {
this.SVG = {
// define default namespaces
ns: 'http://www.w3.org/2000/svg',
xlink: 'http://www.w3.org/1999/xlink',
// initialize defs id sequence
did: 0,
// method for element creation
create: function(e) {
return document.createElementNS(this.ns, e);
},
// method for extending objects
extend: function(o, m) {
for (var k in m)
o.prototype[k] = m[k];
}
};
SVG.Container = {
// add given element at goven position
add: function(e, i) {
if (!this.has(e)) {
i = i == null ? this.children().length : i;
this.children().splice(i, 0, e);
this.node.insertBefore(e.node, this.node.childNodes[i] || null);
e.parent = this;
}
return this;
},
// basically does the same as add() but returns the added element rather than 'this'
put: function(e, i) {
this.add(e, i);
return e;
},
// chacks if the given element is a child
has: function(e) {
return this.children().indexOf(e) >= 0;
},
// returns all child elements and initializes store array if non existant
children: function() {
return this._children || (this._children = []);
},
// remove a given child element
remove: function(e) {
return this.removeAt(this.children().indexOf(e));
},
// remove child element at a given position
removeAt: function(i) {
if (0 <= i && i < this.children().length) {
var e = this.children()[i];
this.children().splice(i, 1);
this.node.removeChild(e.node);
e.parent = null;
}
return this;
},
// returns defs element and initializes store array if non existant
defs: function() {
return this._defs || (this._defs = this.put(new SVG.Defs(), 0));
},
// re-level defs to first positon in element stack
level: function() {
return this.remove(d).put(this.defs(), 0);
},
// create a group element
group: function() {
return this.put(new SVG.G());
},
// create a rect element
rect: function(w, h) {
return this.put(new SVG.Rect().size(w, h));
},
// create circle element, based on ellipse
circle: function(d) {
return this.ellipse(d);
},
// create and ellipse
ellipse: function(w, h) {
return this.put(new SVG.Ellipse().size(w, h));
},
// create a polyline element
polyline: function(p) {
return this.put(new SVG.Polyline().plot(p));
},
// create a polygon element
polygon: function(p) {
return this.put(new SVG.Polygon().plot(p));
},
// create a path element
path: function(d) {
return this.put(new SVG.Path().plot(d));
},
// create image element, load image and set its size
image: function(s, w, h) {
w = w != null ? w : 100;
return this.put(new SVG.Image().load(s).size(w, h != null ? h : w));
},
// create text element
text: function(t) {
return this.put(new SVG.Text().text(t));
},
// create element in defs
gradient: function(t, b) {
return this.defs().gradient(t, b);
},
// hack for safari preventing text to be rendered in one line,
// basically it sets the position of the svg node to absolute
// when the dom is loaded, and resets it to relative a few ms later.
stage: function() {
var r, e = this;
r = function() {
if (document.readyState === 'complete') {
e.attr('style', 'position:absolute;');
setTimeout(function() { e.attr('style', 'position:relative;'); }, 5);
} else {
setTimeout(r, 10);
}
};
r();
return this;
}
};
SVG.Element = function Element(n) {
// keep reference to the element node
this.node = n;
// initialize attribute store
this.attrs = {};
// initialize transformations store
this.trans = {
x: 0,
y: 0,
scaleX: 1,
scaleY: 1,
rotation: 0,
skewX: 0,
skewY: 0
};
};
// Add element-specific functions
SVG.extend(SVG.Element, {
// move element to given x and y values
move: function(x, y) {
return this.attr({ x: x, y: y });
},
// set element size to given width and height
size: function(w, h) {
return this.attr({ width: w, height: h });
},
// position element by its center
center: function(x, y) {
var b = this.bbox();
return this.move(x - b.width / 2, y - b.height / 2);
},
// remove element
remove: function() {
return this.parent != null ? this.parent.remove(this) : void 0;
},
// get parent document
parentDoc: function() {
return this._parent(SVG.Doc);
},
// set svg element attribute
attr: function(a, v, n) {
if (arguments.length < 2) {
// apply every attribute individually if an object is passed
if (typeof a == 'object')
for (v in a) this.attr(v, a[v]);
// act as a getter for style attributes
else if (this._isStyle(a))
return a == 'text' ?
this.content :
a == 'leading' ?
this[a] :
this.style[a];
// act as a getter if the first and only argument is not an object
else
return this.attrs[a];
} else {
// store value
this.attrs[a] = v;
// treat x differently on text elements
if (a == 'x' && this._isText())
for (var i = this.lines.length - 1; i >= 0; i--)
this.lines[i].attr(a, v);
// set the actual attribute
else
n != null ?
this.node.setAttributeNS(n, a, v) :
this.node.setAttribute(a, v);
// if the passed argument belongs to the style as well, add it there
if (this._isStyle(a)) {
a == 'text' ?
this.text(v) :
a == 'leading' ?
this[a] = v :
this.style[a] = v;
this.text(this.content);
}
}
return this;
},
transform: function(o) {
// act as a getter if the first argument is a string
if (typeof o === 'string')
return this.trans[o];
// ... otherwise continue as a setter
var k,
t = [],
b = this.bbox(),
s = this.attr('transform') || '',
l = s.match(/[a-z]+\([^\)]+\)/g) || [];
// merge values
for (k in o)
if (o[k] != null)
this.trans[k] = o[k];
// alias current transformations
o = this.trans;
// add rotate
if (o.rotation != 0)
t.push('rotate(' + o.rotation + ',' + (o.cx != null ? o.cx : b.cx) + ',' + (o.cy != null ? o.cy : b.cy) + ')');
// add scale
t.push('scale(' + o.scaleX + ',' + o.scaleY + ')');
// add skew on x axis
if (o.skewX != 0)
t.push('skewX(' + o.skewX + ')');
// add skew on y axis
if (o.skewY != 0)
t.push('skewY(' + o.skewY + ')')
// add translate
t.push('translate(' + o.x + ',' + o.y + ')');
// add only te required transformations
return this.attr('transform', t.join(' '));
},
// get bounding box
bbox: function() {
// actual bounding box
var b = this.node.getBBox();
return {
// include translations on x an y
x: b.x + this.trans.x,
y: b.y + this.trans.y,
// add the center
cx: b.x + this.trans.x + b.width / 2,
cy: b.y + this.trans.y + b.height / 2,
// plain width and height
width: b.width,
height: b.height
};
},
// show element
show: function() {
this.node.style.display = '';
return this;
},
// hide element
hide: function() {
this.node.style.display = 'none';
return this;
},
// private: find svg parent
_parent: function(pt) {
var e = this;
// find ancestor with given type
while (e != null && !(e instanceof pt))
e = e.parent;
return e;
},
// private: tester method for style detection
_isStyle: function(a) {
return typeof a == 'string' && this._isText() ? (/^font|text|leading/).test(a) : false;
},
// private: element type tester
_isText: function() {
return this instanceof SVG.Text;
}
});
[ 'click',
'dblclick',
'mousedown',
'mouseup',
'mouseover',
'mouseout',
'mousemove',
'touchstart',
'touchend',
'touchmove',
'touchcancel' ].forEach(function(e) {
// add event to SVG.Elment
SVG.Element.prototype[e] = function(f) {
var s = this;
// bind event to element rather than element node
this.node['on' + e] = function() {
return f.apply(s, arguments);
};
// return self
return this;
};
});
SVG.G = function G() {
this.constructor.call(this, SVG.create('g'));
};
// inherit from SVG.Element
SVG.G.prototype = new SVG.Element();
// include the container object
SVG.extend(SVG.G, SVG.Container);
SVG.extend(SVG.Element, {
// get all siblings, including myself
siblings: function() {
return this.parent.children();
},
// send given element one step forwards
forward: function() {
var i = this.siblings().indexOf(this);
return this.parent.remove(this).put(this, i + 1);
},
// send given element one step backwards
backward: function() {
var i, p = this.parent.level();
i = this.siblings().indexOf(this);
if (i > 1)
p.remove(this).add(this, i - 1);
return this;
},
// send given element all the way to the front
front: function() {
return this.parent.remove(this).put(this);
},
// send given element all the way to the back
back: function() {
var i, p = this.parent.level();
i = this.siblings().indexOf(this);
if (i > 1)
p.remove(this).add(this, 0);
return this;
}
});
SVG.Defs = function Defs() {
this.constructor.call(this, SVG.create('defs'));
};
// inherit from SVG.Element
SVG.Defs.prototype = new SVG.Element();
// include the container object
SVG.extend(SVG.Defs, SVG.Container);
SVG.Clip = function Clip() {
this.constructor.call(this, SVG.create('clipPath'));
// set unique id
this.id = 'svgjs_' + (SVG.did++);
this.attr('id', this.id);
};
// inherit from SVG.Element
SVG.Clip.prototype = new SVG.Element();
// include the container object
SVG.extend(SVG.Clip, SVG.Container);
// add clipping functionality to element
SVG.extend(SVG.Element, {
// clip element using another element
clip: function(b) {
var p = this.parent.defs().clip();
b(p);
return this.clipTo(p);
},
// distribute clipping path to svg element
clipTo: function(p) {
return this.attr('clip-path', 'url(#' + p.id + ')');
}
});
// add def-specific functions
SVG.extend(SVG.Defs, {
// create clippath
clip: function() {
return this.put(new SVG.Clip());
}
});
SVG.Mask = function Mask() {
this.constructor.call(this, SVG.create('mask'));
// set unique id
this.id = 'svgjs_' + (SVG.did++);
this.attr('id', this.id);
};
// inherit from SVG.Element
SVG.Mask.prototype = new SVG.Element();
// include the container object
SVG.extend(SVG.Mask, SVG.Container);
// add clipping functionality to element
SVG.extend(SVG.Element, {
// mask element using another element
mask: function(b) {
var m = this.parent.defs().mask();
b(m);
return this.maskTo(m);
},
// distribute mask to svg element
maskTo: function(m) {
return this.attr('mask', 'url(#' + m.id + ')');
}
});
// add def-specific functions
SVG.extend(SVG.Defs, {
// create clippath
mask: function() {
return this.put(new SVG.Mask());
}
});
SVG.Gradient = function Gradient(t) {
this.constructor.call(this, SVG.create(t + 'Gradient'));
// set unique id
this.id = 'svgjs_' + (SVG.did++);
this.attr('id', this.id);
// store type
this.type = t;
};
// inherit from SVG.Element
SVG.Gradient.prototype = new SVG.Element();
// include the container object
SVG.extend(SVG.Gradient, SVG.Container);
// add gradient-specific functions
SVG.extend(SVG.Gradient, {
// from position
from: function(x, y) {
return this.type == 'radial' ?
this.attr({ fx: x + '%', fy: y + '%' }) :
this.attr({ x1: x + '%', y1: y + '%' });
},
// to position
to: function(x, y) {
return this.type == 'radial' ?
this.attr({ cx: x + '%', cy: y + '%' }) :
this.attr({ x2: x + '%', y2: y + '%' });
},
// radius for radial gradient
radius: function(r) {
return this.type == 'radial' ?
this.attr({ r: r + '%' }) :
this;
},
// add a color stop
at: function(o) {
return this.put(new SVG.Stop(o));
},
// update gradient
update: function(b) {
// remove all stops
while (this.node.hasChildNodes())
this.node.removeChild(this.node.lastChild);
// invoke passed block
b(this);
return this;
},
// return the fill id
fill: function() {
return 'url(#' + this.id + ')';
}
});
// add def-specific functions
SVG.extend(SVG.Defs, {
// define clippath
gradient: function(t, b) {
var e = this.put(new SVG.Gradient(t));
// invoke passed block
b(e);
return e;
}
});
SVG.Stop = function Stop(o) {
this.constructor.call(this, SVG.create('stop'));
// immediatelly build stop
this.update(o);
};
// inherit from SVG.Element
SVG.Stop.prototype = new SVG.Element();
// add mark-specific functions
SVG.extend(SVG.Stop, {
// add color stops
update: function(o) {
var i,
s = '',
a = ['opacity', 'color'];
// build style attribute
for (i = a.length - 1; i >= 0; i--)
if (o[a[i]] != null)
s += 'stop-' + a[i] + ':' + o[a[i]] + ';';
// set attributes
return this.attr({
offset: (o.offset != null ? o.offset : this.attrs.offset || 0) + '%',
style: s
});
}
});
SVG.Doc = function Doc(e) {
this.constructor.call(this, SVG.create('svg'));
// create extra wrapper
var w = document.createElement('div');
w.style.cssText = 'position:relative;width:100%;height:100%;';
// ensure the presence of a html element
if (typeof e == 'string')
e = document.getElementById(e);
// set svg element attributes
this.
attr({ xmlns: SVG.ns, version: '1.1', width: '100%', height: '100%' }).
attr('xlink', SVG.xlink, SVG.ns).
defs();
// add elements
e.appendChild(w);
w.appendChild(this.node);
// ensure correct rendering for safari
this.stage();
};
// inherit from SVG.Element
SVG.Doc.prototype = new SVG.Element();
// include the container object
SVG.extend(SVG.Doc, SVG.Container);
SVG.Shape = function Shape(element) {
this.constructor.call(this, element);
};
// inherit from SVG.Element
SVG.Shape.prototype = new SVG.Element();
SVG.Rect = function Rect() {
this.constructor.call(this, SVG.create('rect'));
};
// inherit from SVG.Shape
SVG.Rect.prototype = new SVG.Shape();
SVG.Ellipse = function Ellipse() {
this.constructor.call(this, SVG.create('ellipse'));
};
// inherit from SVG.Shape
SVG.Ellipse.prototype = new SVG.Shape();
// Add ellipse-specific functions
SVG.extend(SVG.Ellipse, {
// custom move function
move: function(x, y) {
this.attrs.x = x;
this.attrs.y = y;
return this.center();
},
// custom size function
size: function(w, h) {
return this.
attr({ rx: w / 2, ry: (h != null ? h : w) / 2 }).
center();
},
// position element by its center
center: function(x, y) {
return this.attr({
cx: x || (this.attrs.x || 0) + (this.attrs.rx || 0),
cy: y || (this.attrs.y || 0) + (this.attrs.ry || 0)
});
}
});
SVG.Poly = {
// set polygon data with default zero point if no data is passed
plot: function(p) {
return this.attr('points', p || '0,0');
},
// move path using translate
move: function(x, y) {
return this.transform({ x: x, y: y });
}
};
SVG.Polyline = function Polyline() {
this.constructor.call(this, SVG.create('polyline'));
};
// inherit from SVG.Shape
SVG.Polyline.prototype = new SVG.Shape();
// Add polygon-specific functions
SVG.extend(SVG.Polyline, SVG.Poly);
SVG.Polygon = function Polygon() {
this.constructor.call(this, SVG.create('polygon'));
};
// inherit from SVG.Shape
SVG.Polygon.prototype = new SVG.Shape();
// Add polygon-specific functions
SVG.extend(SVG.Polygon, SVG.Poly);
SVG.Path = function Path() {
this.constructor.call(this, SVG.create('path'));
};
// inherit from SVG.Shape
SVG.Path.prototype = new SVG.Shape();
// Add path-specific functions
SVG.extend(SVG.Path, {
// set path data
plot: function(d) {
return this.attr('d', d || 'M0,0');
},
// move path using translate, path's don't take x and y
move: function(x, y) {
return this.transform({ x: x, y: y });
}
});
SVG.Image = function Image() {
this.constructor.call(this, SVG.create('image'));
};
// inherit from SVG.Element
SVG.Image.prototype = new SVG.Shape();
// add image-specific functions
SVG.extend(SVG.Image, {
// (re)load image
load: function(u) {
return this.attr('xlink:href', u, SVG.xlink);
}
});
var _styleAttr = ['size', 'family', 'weight', 'stretch', 'variant', 'style'];
SVG.Text = function Text() {
this.constructor.call(this, SVG.create('text'));
// define default style
this.style = { 'font-size': 16, 'font-family': 'Helvetica', 'text-anchor': 'start' };
this.leading = 1.2;
};
// inherit from SVG.Element
SVG.Text.prototype = new SVG.Shape();
// Add image-specific functions
SVG.extend(SVG.Text, {
text: function(t) {
// update the content
this.content = t = t || 'text';
this.lines = [];
var i, n,
s = this._style(),
p = this.parentDoc(),
a = t.split("\n"),
f = this.style['font-size'];
// remove existing child nodes
while (this.node.hasChildNodes())
this.node.removeChild(this.node.lastChild);
// build new lines
for (i = 0, l = a.length; i < l; i++) {
// create new tspan and set attributes
n = new TSpan().
text(a[i]).
attr({
dy: f * this.leading - (i == 0 ? f * 0.3 : 0),
x: (this.attrs.x || 0),
style: s
});
// add new tspan
this.node.appendChild(n.node);
this.lines.push(n);
};
// set style
return this.attr('style', s);
},
// build style based on _styleAttr
_style: function() {
var i, o = '';
for (i = _styleAttr.length - 1; i >= 0; i--)
if (this.style['font-' + _styleAttr[i]] != null)
o += 'font-' + _styleAttr[i] + ':' + this.style['font-' + _styleAttr[i]] + ';';
o += 'text-anchor:' + this.style['text-anchor'] + ';';
return o;
}
});
function TSpan() {
this.constructor.call(this, SVG.create('tspan'));
};
// inherit from SVG.Element
TSpan.prototype = new SVG.Shape();
// include the container object
SVG.extend(TSpan, {
text: function(t) {
this.node.appendChild(document.createTextNode(t));
return this;
}
});
var _strokeAttr = ['width', 'opacity', 'linecap', 'linejoin', 'miterlimit', 'dasharray', 'dashoffset'],
_fillAttr = ['opacity', 'rule'];
// Add shape-specific functions
SVG.extend(SVG.Shape, {
// set fill color and opacity
fill: function(f) {
var i;
// set fill color if not null
if (f.color != null)
this.attr('fill', f.color);
// set all attributes from _fillAttr list with prependes 'fill-' if not null
for (i = _fillAttr.length - 1; i >= 0; i--)
if (f[_fillAttr[i]] != null)
this.attr('fill-' + _fillAttr[i], f[_fillAttr[i]]);
return this;
},
// set stroke color and opacity
stroke: function(s) {
var i;
// set stroke color if not null
if (s.color)
this.attr('stroke', s.color);
// set all attributes from _strokeAttr list with prependes 'stroke-' if not null
for (i = _strokeAttr.length - 1; i >= 0; i--)
if (s[_strokeAttr[i]] != null)
this.attr('stroke-' + _strokeAttr[i], s[_strokeAttr[i]]);
return this;
}
});
// Add element-specific functions
SVG.extend(SVG.Element, {
// rotation
rotate: function(d, x, y) {
var b = this.bbox();
return this.transform({
rotation: d || 0,
cx: x == null ? b.cx : x,
cy: y == null ? b.cx : y
});
},
// skew
skew: function(x, y) {
return this.transform({
skewX: x || 0,
skewY: y || 0
});
}
});
// Add group-specific functions
SVG.extend(SVG.G, {
// move using translate
move: function(x, y) {
return this.transform({ x: x, y: y });
}
});
// Add text-specific functions
SVG.extend(SVG.Text, {
// set font
font: function(o) {
var k, a = {};
for (k in o)
k == 'leading' ?
a[k] = o[k] :
k == 'anchor' ?
a['text-anchor'] = o[k] :
_styleAttr.indexOf(k) > -1 ?
a['font-'+ k] = o[k] :
void 0;
return this.attr(a).text(this.content);
}
});
}).call(this);
function svg(e) { return new SVG.Doc(e); };
|