-
Notifications
You must be signed in to change notification settings - Fork 65
/
jquery.svg.js
1352 lines (1265 loc) · 55.3 KB
/
jquery.svg.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
/* http://keith-wood.name/svg.html
SVG for jQuery v1.5.0.
Written by Keith Wood (kbwood{at}iinet.com.au) August 2007.
Available under the MIT (http://keith-wood.name/licence.html) license.
Please attribute the author if you use it. */
(function($) { // Hide scope, no $ conflict
/** The SVG manager.
<p>Use the singleton instance of this class, $.svg,
to interact with the SVG functionality.</p>
<p>Expects HTML like:</p>
<pre><div></div></pre>
@module SVGManager */
function SVGManager() {
this._settings = []; // Settings to be remembered per SVG object
this._extensions = []; // List of SVG extensions added to SVGWrapper
// for each entry [0] is extension name, [1] is extension class (function)
// the function takes one parameter - the SVGWrapper instance
this.regional = []; // Localisations, indexed by language, '' for default (English)
this.regional[''] = {errorLoadingText: 'Error loading'};
this.local = this.regional['']; // Current localisation
this._uuid = new Date().getTime();
this._ie = !!window.ActiveXObject;
}
$.extend(SVGManager.prototype, {
/** Class name added to elements to indicate already configured with SVG. */
markerClassName: 'hasSVG',
/** Name of the data property for instance settings. */
propertyName: 'svgwrapper',
/** SVG namespace. */
svgNS: 'http://www.w3.org/2000/svg',
/** XLink namespace. */
xlinkNS: 'http://www.w3.org/1999/xlink',
/** SVG wrapper class. */
_wrapperClass: SVGWrapper,
/* Camel-case versions of attribute names containing dashes or are reserved words. */
_attrNames: {class_: 'class', in_: 'in',
alignmentBaseline: 'alignment-baseline', baselineShift: 'baseline-shift',
clipPath: 'clip-path', clipRule: 'clip-rule',
colorInterpolation: 'color-interpolation',
colorInterpolationFilters: 'color-interpolation-filters',
colorRendering: 'color-rendering', dominantBaseline: 'dominant-baseline',
enableBackground: 'enable-background', fillOpacity: 'fill-opacity',
fillRule: 'fill-rule', floodColor: 'flood-color',
floodOpacity: 'flood-opacity', fontFamily: 'font-family',
fontSize: 'font-size', fontSizeAdjust: 'font-size-adjust',
fontStretch: 'font-stretch', fontStyle: 'font-style',
fontVariant: 'font-variant', fontWeight: 'font-weight',
glyphOrientationHorizontal: 'glyph-orientation-horizontal',
glyphOrientationVertical: 'glyph-orientation-vertical',
horizAdvX: 'horiz-adv-x', horizOriginX: 'horiz-origin-x',
imageRendering: 'image-rendering', letterSpacing: 'letter-spacing',
lightingColor: 'lighting-color', markerEnd: 'marker-end',
markerMid: 'marker-mid', markerStart: 'marker-start',
stopColor: 'stop-color', stopOpacity: 'stop-opacity',
strikethroughPosition: 'strikethrough-position',
strikethroughThickness: 'strikethrough-thickness',
strokeDashArray: 'stroke-dasharray', strokeDashOffset: 'stroke-dashoffset',
strokeLineCap: 'stroke-linecap', strokeLineJoin: 'stroke-linejoin',
strokeMiterLimit: 'stroke-miterlimit', strokeOpacity: 'stroke-opacity',
strokeWidth: 'stroke-width', textAnchor: 'text-anchor',
textDecoration: 'text-decoration', textRendering: 'text-rendering',
underlinePosition: 'underline-position', underlineThickness: 'underline-thickness',
vertAdvY: 'vert-adv-y', vertOriginY: 'vert-origin-y',
wordSpacing: 'word-spacing', writingMode: 'writing-mode'},
/* Add the SVG object to its container. */
_attachSVG: function(container, settings) {
var svg = (container.namespaceURI === this.svgNS ? container : null);
var container = (svg ? null : container);
if ($(container || svg).hasClass(this.markerClassName)) {
return;
}
if (typeof settings === 'string') {
settings = {loadURL: settings};
}
else if (typeof settings === 'function') {
settings = {onLoad: settings};
}
$(container || svg).addClass(this.markerClassName);
try {
if (!svg) {
svg = document.createElementNS(this.svgNS, 'svg');
svg.setAttribute('version', '1.1');
if (container.clientWidth > 0) {
svg.setAttribute('width', container.clientWidth);
}
if (container.clientHeight > 0) {
svg.setAttribute('height', container.clientHeight);
}
container.appendChild(svg);
}
this._afterLoad(container, svg, settings || {});
}
catch (e) {
$(container).html('<p>SVG is not supported natively on this browser</p>');
}
},
/* Post-processing once loaded. */
_afterLoad: function(container, svg, settings) {
var settings = settings || this._settings[container.id];
this._settings[container ? container.id : ''] = null;
var wrapper = new this._wrapperClass(svg, container);
$.data(container || svg, $.svg.propertyName, wrapper);
try {
if (settings.loadURL) { // Load URL
wrapper.load(settings.loadURL, settings);
}
if (settings.settings) { // Additional settings
wrapper.configure(settings.settings);
}
if (settings.onLoad && !settings.loadURL) { // Onload callback
settings.onLoad.apply(container || svg, [wrapper]);
}
}
catch (e) {
alert(e);
}
},
/** Return the SVG wrapper created for a given container.
@param container {string|Element|jQuery} Selector for the container or
the container for the SVG object or jQuery collection where first entry is the container.
@return {SVGWrapper} The corresponding SVG wrapper element, or <code>null</code> if not attached. */
_getSVG: function(container) {
return $(container).data(this.propertyName);
},
/** Remove the SVG functionality from a div.
@param container {Element} The container for the SVG object. */
_destroySVG: function(container) {
container = $(container);
if (!container.hasClass(this.markerClassName)) {
return;
}
container.removeClass(this.markerClassName).removeData(this.propertyName);
if (container[0].namespaceURI !== this.svgNS) {
container.empty();
}
},
/** Extend the SVGWrapper object with an embedded class.
<p>The constructor function must take a single parameter that is
a reference to the owning SVG root object. This allows the
extension to access the basic SVG functionality.</p>
@param name {string} The name of the <code>SVGWrapper</code> attribute to access the new class.
@param extClass {function} The extension class constructor. */
addExtension: function(name, extClass) {
this._extensions.push([name, extClass]);
},
/** Does this node belong to SVG?
@param node {Element} The node to be tested.
@return {boolean} <code>true</code> if an SVG node, <code>false</code> if not. */
isSVGElem: function(node) {
return (node.nodeType === 1 && node.namespaceURI === $.svg.svgNS);
}
});
/** The main SVG interface, which encapsulates the SVG element.
<p>Obtain a reference from $().svg('get')</p>
@module SVGWrapper */
function SVGWrapper(svg, container) {
this._svg = svg; // The SVG root node
this._container = container; // The containing div
for (var i = 0; i < $.svg._extensions.length; i++) {
var extension = $.svg._extensions[i];
this[extension[0]] = new extension[1](this);
}
}
$.extend(SVGWrapper.prototype, {
/** Retrieve the width of the SVG object.
@return {number} The width of the SVG canvas. */
width: function() {
return (this._container ? this._container.clientWidth : this._svg.width);
},
/** Retrieve the height of the SVG object.
@return {number} The height of the SVG canvas. */
height: function() {
return (this._container ? this._container.clientHeight : this._svg.height);
},
/** Retrieve the root SVG element.
@return {SVGElement} The top-level SVG element. */
root: function() {
return this._svg;
},
/** Configure a SVG node.
@param [node] {SVGElement} The node to configure, or the SVG root if not specified.
@param settings {object} Additional settings for the root.
@param [clear=false] {boolean} <code>true</code> to remove existing attributes first,
<code>false</code> to add to what is already there.
@return {SVGWrapper} This wrapper. */
configure: function(node, settings, clear) {
if (!node.nodeName) {
clear = settings;
settings = node;
node = this._svg;
}
if (clear) {
for (var i = node.attributes.length - 1; i >= 0; i--) {
var attr = node.attributes.item(i);
if (!(attr.nodeName === 'onload' || attr.nodeName === 'version' ||
attr.nodeName.substring(0, 5) === 'xmlns')) {
node.attributes.removeNamedItem(attr.nodeName);
}
}
}
for (var attrName in settings) {
node.setAttribute($.svg._attrNames[attrName] || attrName, settings[attrName]);
}
return this;
},
/** Locate a specific element in the SVG document.
@param id {string} The element's identifier.
@return {SVGElement} The element reference, or <code>null</code> if not found. */
getElementById: function(id) {
return this._svg.ownerDocument.getElementById(id);
},
/** Change the attributes for a SVG node.
@param element {SVGElement} The node to change.
@param settings {object} The new settings.
@return {SVGWrapper} This wrapper. */
change: function(element, settings) {
if (element) {
for (var name in settings) {
if (settings[name] == null) {
element.removeAttribute($.svg._attrNames[name] || name);
}
else {
element.setAttribute($.svg._attrNames[name] || name, settings[name]);
}
}
}
return this;
},
/** Check for parent being absent and adjust arguments accordingly.
@private
@param values {string[]} The given parameters.
@param names {string[]} The names of the parameters in order.
@param optSettings {string[]} The names of optional parameters.
@return {object} An object representing the named parameters. */
_args: function(values, names, optSettings) {
names.splice(0, 0, 'parent');
names.splice(names.length, 0, 'settings');
var args = {};
var offset = 0;
if (values[0] != null && values[0].jquery) {
values[0] = values[0][0];
}
if (values[0] != null && !(typeof values[0] === 'object' && values[0].nodeName)) {
args['parent'] = null;
offset = 1;
}
for (var i = 0; i < values.length; i++) {
args[names[i + offset]] = values[i];
}
if (optSettings) {
$.each(optSettings, function(i, value) {
if (typeof args[value] === 'object') {
args.settings = args[value];
args[value] = null;
}
});
}
return args;
},
/** Add a title.
@param [parent] {SVGElement|jQuery} The parent node for the new node, or SVG root if not specified.
@param text {string} The text of the title.
@param [settings] {object} Additional settings for this node.
@return {SVGElement} The new title node. */
title: function(parent, text, settings) {
var args = this._args(arguments, ['text']);
var node = this._makeNode(args.parent, 'title', args.settings || {});
node.appendChild(this._svg.ownerDocument.createTextNode(args.text));
return node;
},
/** Add a description.
@param [parent] {SVGElement|jQuery} The parent node for the new node, or SVG root if not specified.
@param text {string} The text of the description.
@param [settings] {object} Additional settings for this node.
@return {SVGElement} The new description node. */
describe: function(parent, text, settings) {
var args = this._args(arguments, ['text']);
var node = this._makeNode(args.parent, 'desc', args.settings || {});
node.appendChild(this._svg.ownerDocument.createTextNode(args.text));
return node;
},
/** Add a definitions node.
@param [parent] {SVGElement|jQuery} The parent node for the new node, or SVG root if not specified.
@param [id] {string} The ID of this definitions (optional).
@param [settings] {object} Additional settings for this node.
@return {SVGElement} The new definitions node. */
defs: function(parent, id, settings) {
var args = this._args(arguments, ['id'], ['id']);
return this._makeNode(args.parent, 'defs', $.extend((args.id ? {id: args.id} : {}), args.settings || {}));
},
/** Add a symbol definition.
@param [parent] {SVGElement|jQuery} The parent node for the new node, or SVG root if not specified.
@param id {string} The ID of this symbol.
@param x1 {number} The left coordinate for this symbol.
@param y1 {number} The top coordinate for this symbol.
@param width {number} The width of this symbol.
@param height {number} The height of this symbol.
@param [settings] {object} Additional settings for this node.
@return {SVGElement} The new symbol node. */
symbol: function(parent, id, x1, y1, width, height, settings) {
var args = this._args(arguments, ['id', 'x1', 'y1', 'width', 'height']);
return this._makeNode(args.parent, 'symbol', $.extend({id: args.id,
viewBox: args.x1 + ' ' + args.y1 + ' ' + args.width + ' ' + args.height}, args.settings || {}));
},
/** Add a marker definition.
@param [parent] {SVGElement|jQuery} The parent node for the new node, or SVG root if not specified.
@param id {string} The ID of this marker.
@param refX {number} The x-coordinate for the reference point.
@param refY {number} The y-coordinate for the reference point.
@param mWidth {number} The marker viewport width.
@param mHeight {number} The marker viewport height.
@param [orient] {string|number} 'auto' or angle (degrees).
@param [settings] {object} Additional settings for this node.
@return {SVGElement} The new marker node. */
marker: function(parent, id, refX, refY, mWidth, mHeight, orient, settings) {
var args = this._args(arguments, ['id', 'refX', 'refY', 'mWidth', 'mHeight', 'orient'], ['orient']);
return this._makeNode(args.parent, 'marker', $.extend(
{id: args.id, refX: args.refX, refY: args.refY, markerWidth: args.mWidth,
markerHeight: args.mHeight, orient: args.orient || 'auto'}, args.settings || {}));
},
/** Add a style node.
@param [parent] {SVGElement|jQuery} The parent node for the new node, or SVG root if not specified.
@param styles {string} The CSS styles.
@param [settings] {object} Additional settings for this node.
@return {SVGElement} The new style node. */
style: function(parent, styles, settings) {
var args = this._args(arguments, ['styles']);
var node = this._makeNode(args.parent, 'style', $.extend({type: 'text/css'}, args.settings || {}));
node.appendChild(this._svg.ownerDocument.createTextNode(args.styles));
return node;
},
/** Add a script node.
@param [parent] {SVGElement|jQuery} The parent node for the new node, or SVG root if not specified.
@param script {string} The JavaScript code.
@param [type='text/javascript'] {string} The MIME type for the code.
@param [settings] {object} Additional settings for this node.
@return {SVGElement} The new script node. */
script: function(parent, script, type, settings) {
var args = this._args(arguments, ['script', 'type'], ['type']);
var node = this._makeNode(args.parent, 'script', $.extend(
{type: args.type || 'text/javascript'}, args.settings || {}));
node.appendChild(this._svg.ownerDocument.createTextNode(args.script));
if ($.svg._ie) {
$.globalEval(args.script);
}
return node;
},
/** Add a linear gradient definition.
<p>Specify all of <code>x1</code>, <code>y1</code>, <code>x2</code>, <code>y2</code> or none of them.</p>
@param [parent] {SVGElement|jQuery} The parent node for the new node, or SVG root if not specified.
@param id {string} The ID for this gradient.
@param stops {string[][]} The gradient stops, each entry is [0] is offset (0.0-1.0 or 0%-100%),
[1] is colour, [2] is opacity (optional).
@param [x1] {number} The x-coordinate of the gradient start.
@param [y1] {number} The y-coordinate of the gradient start.
@param [x2] {number} The x-coordinate of the gradient end.
@param [y2] {number} The y-coordinate of the gradient end.
@param [settings] {object} Additional settings for this node.
@return {SVGElement} The new linear gradient node. */
linearGradient: function(parent, id, stops, x1, y1, x2, y2, settings) {
var args = this._args(arguments, ['id', 'stops', 'x1', 'y1', 'x2', 'y2'], ['x1']);
var sets = $.extend({id: args.id},
(args.x1 != null ? {x1: args.x1, y1: args.y1, x2: args.x2, y2: args.y2} : {}));
return this._gradient(args.parent, 'linearGradient', $.extend(sets, args.settings || {}), args.stops);
},
/** Add a radial gradient definition.
<p>Specify all of <code>cx</code>, <code>cy</code>, <code>r</code>,
<code>fx</code>, <code>fy</code> or none of them.</p>
@param [parent] {SVGElement|jQuery} The parent node for the new node, or SVG root if not specified.
@param id {string} The ID for this gradient.
@param stops {string[][]} The gradient stops, each entry [0] is offset (0.0-1.0 or 0%-100%),
[1] is colour, [2] is opacity (optional).
@param [cx] {number} The x-coordinate of the largest circle centre.
@param [cy] {number} The y-coordinate of the largest circle centre.
@param [r] {number} The radius of the largest circle.
@param [fx] {number} The x-coordinate of the gradient focus.
@param [fy] {number} The y-coordinate of the gradient focus.
@param [settings] {object} Additional settings for this node.
@return {SVGElement} The new radial gradient node. */
radialGradient: function(parent, id, stops, cx, cy, r, fx, fy, settings) {
var args = this._args(arguments, ['id', 'stops', 'cx', 'cy', 'r', 'fx', 'fy'], ['cx']);
var sets = $.extend({id: args.id},
(args.cx != null ? {cx: args.cx, cy: args.cy, r: args.r, fx: args.fx, fy: args.fy} : {}));
return this._gradient(args.parent, 'radialGradient', $.extend(sets, args.settings || {}), args.stops);
},
/** Add a gradient node.
@private
@param parent {SVGElement|jQuery} The parent node for the new node.
@param name {string} The type of gradient node to create.
@param settings {object} The settings for this node.
@param stops {string[][]} The gradient stops.
@return {SVGElement} The new gradient node. */
_gradient: function(parent, name, settings, stops) {
var node = this._makeNode(parent, name, settings);
for (var i = 0; i < stops.length; i++) {
var stop = stops[i];
this._makeNode(node, 'stop', $.extend({offset: stop[0], stopColor: stop[1]},
(stop[2] != null ? {stopOpacity: stop[2]} : {})));
}
return node;
},
/** Add a pattern definition.
<p>Specify all of <code>vx</code>, <code>vy</code>, <code>xwidth</code>,
<code>vheight</code> or none of them.</p>
@param [parent] {SVGElement|jQuery} The parent node for the new node, or SVG root if not specified.
@param id {string} The ID for this pattern.
@param x {number} The x-coordinate for the left edge of the pattern.
@param y {number} The y-coordinate for the top edge of the pattern.
@param width {number} The width of the pattern.
@param height {number} The height of the pattern.
@param [vx] {number} The minimum x-coordinate for view box.
@param [vy] {number} The minimum y-coordinate for the view box.
@param [vwidth] {number} The width of the view box.
@param [vheight] {number} The height of the view box.
@param [settings] {object} Additional settings for this node.
@return {SVGElement} The new pattern definition node. */
pattern: function(parent, id, x, y, width, height, vx, vy, vwidth, vheight, settings) {
var args = this._args(arguments, ['id', 'x', 'y', 'width', 'height', 'vx', 'vy', 'vwidth', 'vheight'], ['vx']);
var sets = $.extend({id: args.id, x: args.x, y: args.y, width: args.width, height: args.height},
(args.vx != null ? {viewBox: args.vx + ' ' + args.vy + ' ' + args.vwidth + ' ' + args.vheight} : {}));
return this._makeNode(args.parent, 'pattern', $.extend(sets, args.settings || {}));
},
/** Add a clip path definition.
@param [parent] {SVGElement|jQuery} The parent node for the new node, or SVG root if not specified.
@param id {string} The ID for this path.
@param [units='userSpaceOnUse'] {string} Either 'userSpaceOnUse' or 'objectBoundingBox'.
@param [settings] {object} Additional settings for this node.
@return {SVGElement} The new clip path definition node. */
clipPath: function(parent, id, units, settings) {
var args = this._args(arguments, ['id', 'units']);
args.units = args.units || 'userSpaceOnUse';
return this._makeNode(args.parent, 'clipPath', $.extend(
{id: args.id, clipPathUnits: args.units}, args.settings || {}));
},
/** Add a mask definition.
@param [parent] {SVGElement|jQuery} The parent node for the new node, or SVG root if not specified.
@param id {string} The ID for this mask.
@param x {number} The x-coordinate for the left edge of the mask.
@param y {number} The y-coordinate for the top edge of the mask.
@param width {number} The width of the mask.
@param height {number} The height of the mask.
@param [settings] {object} Additional settings for this node.
@return {SVGElement} The new mask definition node. */
mask: function(parent, id, x, y, width, height, settings) {
var args = this._args(arguments, ['id', 'x', 'y', 'width', 'height']);
return this._makeNode(args.parent, 'mask', $.extend(
{id: args.id, x: args.x, y: args.y, width: args.width, height: args.height}, args.settings || {}));
},
/** Create a new path object.
@return {SVGPath} A new path object. */
createPath: function() {
return new SVGPath();
},
/** Create a new text object.
@return {SVGText} A new text object. */
createText: function() {
return new SVGText();
},
/** Add an embedded SVG element.
<p>Specify all of <code>vx</code>, <code>vy</code>,
<code>vwidth</code>, <code>vheight</code> or none of them.</p>
@param [parent] {SVGElement|jQuery} The parent node for the new node, or SVG root if not specified.
@param x {number} The x-coordinate for the left edge of the node.
@param y {number} The y-coordinate for the top edge of the node.
@param width {number} The width of the node.
@param height {number} The height of the node.
@param [vx] {number} The minimum x-coordinate for view box.
@param [vy] {number} The minimum y-coordinate for the view box.
@param [vwidth] {number} The width of the view box.
@param [vheight] {number} The height of the view box.
@param [settings] {object} Additional settings for this node.
@return {SVGElement} The new svg node. */
svg: function(parent, x, y, width, height, vx, vy, vwidth, vheight, settings) {
var args = this._args(arguments, ['x', 'y', 'width', 'height', 'vx', 'vy', 'vwidth', 'vheight'], ['vx']);
var sets = $.extend({x: args.x, y: args.y, width: args.width, height: args.height},
(args.vx != null ? {viewBox: args.vx + ' ' + args.vy + ' ' + args.vwidth + ' ' + args.vheight} : {}));
return this._makeNode(args.parent, 'svg', $.extend(sets, args.settings || {}));
},
/** Create a group.
@param [parent] {SVGElement|jQuery} The parent node for the new node, or SVG root if not specified.
@param [id] {string} The ID of this group.
@param [settings] {object} Additional settings for this node.
@return {SVGElement} The new group node. */
group: function(parent, id, settings) {
var args = this._args(arguments, ['id'], ['id']);
return this._makeNode(args.parent, 'g', $.extend({id: args.id}, args.settings || {}));
},
/** Add a usage reference.
<p>Specify all of <code>x</code>, <code>y</code>, <code>width</code>, <code>height</code> or none of them.</p>
@param [parent] {SVGElement|jQuery} The parent node for the new node, or SVG root if not specified.
@param [x] {number} The x-coordinate for the left edge of the node.
@param [y] {number} The y-coordinate for the top edge of the node.
@param [width] {number} The width of the node.
@param [height] {number} The height of the node.
@param ref {string} The ID of the definition node.
@param [settings] {object} Additional settings for this node.
@return {SVGElement} The new usage reference node. */
use: function(parent, x, y, width, height, ref, settings) {
var args = this._args(arguments, ['x', 'y', 'width', 'height', 'ref']);
if (typeof args.x === 'string') {
args.ref = args.x;
args.settings = args.y;
args.x = args.y = args.width = args.height = null;
}
var node = this._makeNode(args.parent, 'use', $.extend(
{x: args.x, y: args.y, width: args.width, height: args.height}, args.settings || {}));
node.setAttributeNS($.svg.xlinkNS, 'href', args.ref);
return node;
},
/** Add a link, which applies to all child elements.
@param [parent] {SVGElement|jQuery} The parent node for the new node, or SVG root if not specified.
@param ref {string} The target URL.
@param [settings] {object} Additional settings for this node.
@return {SVGElement} The new link node. */
link: function(parent, ref, settings) {
var args = this._args(arguments, ['ref']);
var node = this._makeNode(args.parent, 'a', args.settings);
node.setAttributeNS($.svg.xlinkNS, 'href', args.ref);
return node;
},
/** Add an image.
@param [parent] {SVGElement|jQuery} The parent node for the new node, or SVG root if not specified.
@param x {number} The x-coordinate for the left edge of the image.
@param y {number} The y-coordinate for the top edge of the image.
@param width {number} The width of the image.
@param height {number} The height of the image.
@param ref {string} The path to the image.
@param [settings] {object} Additional settings for this node.
@return {SVGElement} The new image node. */
image: function(parent, x, y, width, height, ref, settings) {
var args = this._args(arguments, ['x', 'y', 'width', 'height', 'ref']);
var node = this._makeNode(args.parent, 'image', $.extend(
{x: args.x, y: args.y, width: args.width, height: args.height}, args.settings || {}));
node.setAttributeNS($.svg.xlinkNS, 'href', args.ref);
return node;
},
/** Draw a path.
@param [parent] {SVGElement|jQuery} The parent node for the new node, or SVG root if not specified.
@param path {string|SVGPath} The path to draw.
@param [settings] {object} Additional settings for this node.
@return {SVGElement} The new path node. */
path: function(parent, path, settings) {
var args = this._args(arguments, ['path']);
return this._makeNode(args.parent, 'path', $.extend(
{d: (args.path.path ? args.path.path() : args.path)}, args.settings || {}));
},
/** Draw a rectangle.
<p>Specify both of <code>rx</code> and <code>ry</code> or neither.</p>
@param [parent] {SVGElement|jQuery} The parent node for the new node, or SVG root if not specified.
@param x {number} The x-coordinate for the left edge of the rectangle.
@param y {number} The y-coordinate for the top edge of the rectangle.
@param width {number} The width of the rectangle.
@param height {number} The height of the rectangle.
@param [rx] {number} The x-radius of the ellipse for the rounded corners.
@param [ry] {number} The y-radius of the ellipse for the rounded corners.
@param [settings] {object} Additional settings for this node.
@return {SVGElement} The new rectangle node. */
rect: function(parent, x, y, width, height, rx, ry, settings) {
var args = this._args(arguments, ['x', 'y', 'width', 'height', 'rx', 'ry'], ['rx']);
return this._makeNode(args.parent, 'rect', $.extend(
{x: args.x, y: args.y, width: args.width, height: args.height},
(args.rx ? {rx: args.rx, ry: args.ry} : {}), args.settings || {}));
},
/** Draw a circle.
@param [parent] {SVGElement|jQuery} The parent node for the new node, or SVG root if not specified.
@param cx {number} The x-coordinate for the centre of the circle.
@param cy {number} The y-coordinate for the centre of the circle.
@param r {number} The radius of the circle.
@param [settings] {object} Additional settings for this node.
@return {SVGElement} The new circle node. */
circle: function(parent, cx, cy, r, settings) {
var args = this._args(arguments, ['cx', 'cy', 'r']);
return this._makeNode(args.parent, 'circle', $.extend(
{cx: args.cx, cy: args.cy, r: args.r}, args.settings || {}));
},
/** Draw an ellipse.
@param [parent] {SVGElement|jQuery} The parent node for the new node, or SVG root if not specified.
@param cx {number} The x-coordinate for the centre of the ellipse.
@param cy {number} The y-coordinate for the centre of the ellipse.
@param rx {number} The x-radius of the ellipse.
@param ry {number} The y-radius of the ellipse.
@param [settings] {object} Additional settings for this node.
@return {SVGElement} The new ellipse node. */
ellipse: function(parent, cx, cy, rx, ry, settings) {
var args = this._args(arguments, ['cx', 'cy', 'rx', 'ry']);
return this._makeNode(args.parent, 'ellipse', $.extend(
{cx: args.cx, cy: args.cy, rx: args.rx, ry: args.ry}, args.settings || {}));
},
/** Draw a line.
@param [parent] {SVGElement|jQuery} The parent node for the new node, or SVG root if not specified.
@param x1 {number} The x-coordinate for the start of the line.
@param y1 {number} The y-coordinate for the start of the line.
@param x2 {number} The x-coordinate for the end of the line.
@param y2 {number} The y-coordinate for the end of the line.
@param [settings] {object} Additional settings for this node.
@return {SVGElement} The new line node. */
line: function(parent, x1, y1, x2, y2, settings) {
var args = this._args(arguments, ['x1', 'y1', 'x2', 'y2']);
return this._makeNode(args.parent, 'line', $.extend(
{x1: args.x1, y1: args.y1, x2: args.x2, y2: args.y2}, args.settings || {}));
},
/** Draw a polygonal line.
@param [parent] {SVGElement|jQuery} The parent node for the new node, or SVG root if not specified.
@param points {number[][]} The x-/y-coordinates for the points on the line.
@param [settings] {object} Additional settings for this node.
@return {SVGElement} The new polygonal line node. */
polyline: function(parent, points, settings) {
var args = this._args(arguments, ['points']);
return this._poly(args.parent, 'polyline', args.points, args.settings);
},
/** Draw a polygonal shape.
@param [parent] {SVGElement|jQuery} The parent node for the new node, or SVG root if not specified.
@param points {number[][]} The x-/y-coordinates for the points on the shape.
@param [settings] {object} Additional settings for this node.
@return {SVGElement} The new polygonal shape node. */
polygon: function(parent, points, settings) {
var args = this._args(arguments, ['points']);
return this._poly(args.parent, 'polygon', args.points, args.settings);
},
/** Draw a polygonal line or shape.
@private
@param parent {SVGElement|jQuery} The parent node for the new node.
@param name {string} The type of polygon to create.
@param points {number[][]} The x-/y-coordinates for the points on the shape.
@param [settings] {object} Additional settings for this node.
@return {SVGElement} The new polygon node. */
_poly: function(parent, name, points, settings) {
var ps = '';
for (var i = 0; i < points.length; i++) {
ps += points[i].join() + ' ';
}
return this._makeNode(parent, name, $.extend({points: $.trim(ps)}, settings || {}));
},
/** Draw text.
<p>Specify both of <code>x</code> and <code>y</code> or neither of them.</p>
@param [parent] {SVGElement|jQuery} The parent node for the new node, or SVG root if not specified.
@param [x] {number|number[]} The x-coordinate(s) for the text.
@param [y] {number|number[]} The y-coordinate(s) for the text.
@param value {string|SVGText} The text content or text with spans and references.
@param [settings] {object} Additional settings for this node.
@return {SVGElement} The new text node. */
text: function(parent, x, y, value, settings) {
var args = this._args(arguments, ['x', 'y', 'value']);
if (typeof args.x === 'string' && arguments.length < 4) {
args.value = args.x;
args.settings = args.y;
args.x = args.y = null;
}
return this._text(args.parent, 'text', args.value, $.extend(
{x: (args.x && $.isArray(args.x) ? args.x.join(' ') : args.x),
y: (args.y && $.isArray(args.y) ? args.y.join(' ') : args.y)}, args.settings || {}));
},
/** Draw text along a path.
@param [parent] {SVGElement|jQuery} The parent node for the new node, or SVG root if not specified.
@param path {string} The ID of the path.
@param value {string|SVGText} The text content or text with spans and references.
@param [settings] {object} Additional settings for this node.
@return {SVGElement} The new textpath node. */
textpath: function(parent, path, value, settings) {
var args = this._args(arguments, ['path', 'value']);
var node = this._text(args.parent, 'textPath', args.value, args.settings || {});
node.setAttributeNS($.svg.xlinkNS, 'href', args.path);
return node;
},
/** Draw text.
@private
@param parent {SVGElement|jQuery} The parent node for the new node.
@param name {string} The type of text to create.
@param value {string|SVGText} The text content or text with spans and references.
@param [settings] {object} Additional settings for this node.
@return {SVGElement} The new text node. */
_text: function(parent, name, value, settings) {
var node = this._makeNode(parent, name, settings);
if (typeof value === 'string') {
node.appendChild(node.ownerDocument.createTextNode(value));
}
else {
for (var i = 0; i < value._parts.length; i++) {
var part = value._parts[i];
if (part[0] === 'tspan') {
var child = this._makeNode(node, part[0], part[2]);
child.appendChild(node.ownerDocument.createTextNode(part[1]));
node.appendChild(child);
}
else if (part[0] === 'tref') {
var child = this._makeNode(node, part[0], part[2]);
child.setAttributeNS($.svg.xlinkNS, 'href', part[1]);
node.appendChild(child);
}
else if (part[0] === 'textpath') {
var set = $.extend({}, part[2]);
set.href = null;
var child = this._makeNode(node, part[0], set);
child.setAttributeNS($.svg.xlinkNS, 'href', part[2].href);
child.appendChild(node.ownerDocument.createTextNode(part[1]));
node.appendChild(child);
}
else { // straight text
node.appendChild(node.ownerDocument.createTextNode(part[1]));
}
}
}
return node;
},
/** Add a custom SVG element.
@param [parent] {SVGElement|jQuery} The parent node for the new node, or SVG root if not specified.
@param name {string} The name of the element.
@param [settings] {object} Additional settings for this node.
@return {SVGElement} The new custom node. */
other: function(parent, name, settings) {
var args = this._args(arguments, ['name']);
return this._makeNode(args.parent, args.name, args.settings || {});
},
/** Create a SVG node with the given settings.
@private
@param parent {SVGElement|jQuery} The parent node for the new node, or SVG root if <code>null</code>.
@param name {string} The name of the element.
@param [settings] {object} Additional settings for this node.
@return {SVGElement} The new node. */
_makeNode: function(parent, name, settings) {
parent = parent || this._svg;
var node = this._svg.ownerDocument.createElementNS($.svg.svgNS, name);
for (var name in settings) {
var value = settings[name];
if (value != null && (typeof value !== 'string' || value !== '')) {
node.setAttribute($.svg._attrNames[name] || name, value);
}
}
parent.appendChild(node);
return node;
},
/** Add an existing SVG node to the document.
@param [parent] {SVGElement|jQuery} The parent node for the new node, or SVG root if not specified.
@param node {SVGElement|string|jQuery} The new node to add or
the jQuery selector for the node or the set of nodes to add.
@return {SVGWrapper} This wrapper. */
add: function(parent, node) {
var args = this._args((arguments.length === 1 ? [null, parent] : arguments), ['node']);
var svg = this;
args.parent = args.parent || this._svg;
args.node = (args.node.jquery ? args.node : $(args.node));
try {
args.parent.appendChild(args.node.cloneNode(true));
}
catch (e) {
args.node.each(function() {
var child = svg._cloneAsSVG(this);
if (child) {
args.parent.appendChild(child);
}
});
}
return this;
},
/** Clone an existing SVG node and add it to the document.
@param [parent] {SVGElement|jQuery} The parent node for the new node, or SVG root if not specified.
@param node {SVGEelement|string|jQuery} The new node to add or
the jQuery selector for the node or the set of nodes to clone.
@return {SVGElement[]} The collection of new nodes. */
clone: function(parent, node) {
var svg = this;
var args = this._args((arguments.length === 1 ? [null, parent] : arguments), ['node']);
args.parent = args.parent || this._svg;
args.node = (args.node.jquery ? args.node : $(args.node));
var newNodes = [];
args.node.each(function() {
var child = svg._cloneAsSVG(this);
if (child) {
child.id = '';
args.parent.appendChild(child);
newNodes.push(child);
}
});
return newNodes;
},
/** SVG nodes must belong to the SVG namespace, so clone and ensure this is so.
@private
@param node {SVGElement} The SVG node to clone.
@return {SVGElement} The cloned node. */
_cloneAsSVG: function(node) {
var newNode = null;
if (node.nodeType === 1) { // element
newNode = this._svg.ownerDocument.createElementNS($.svg.svgNS, this._checkName(node.nodeName));
for (var i = 0; i < node.attributes.length; i++) {
var attr = node.attributes.item(i);
if (attr.nodeName !== 'xmlns' && attr.nodeValue) {
if (attr.prefix === 'xlink') {
newNode.setAttributeNS($.svg.xlinkNS, attr.localName || attr.baseName, attr.nodeValue);
}
else {
newNode.setAttribute(this._checkName(attr.nodeName), attr.nodeValue);
}
}
}
for (var i = 0; i < node.childNodes.length; i++) {
var child = this._cloneAsSVG(node.childNodes[i]);
if (child) {
newNode.appendChild(child);
}
}
}
else if (node.nodeType === 3) { // text
if ($.trim(node.nodeValue)) {
newNode = this._svg.ownerDocument.createTextNode(node.nodeValue);
}
}
else if (node.nodeType === 4) { // CDATA
if ($.trim(node.nodeValue)) {
try {
newNode = this._svg.ownerDocument.createCDATASection(node.nodeValue);
}
catch (e) {
newNode = this._svg.ownerDocument.createTextNode(
node.nodeValue.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>'));
}
}
}
return newNode;
},
/** Node names must be lower case and without SVG namespace prefix.
@private
@param name {string} The name to check.
@return {string} The corrected name. */
_checkName: function(name) {
name = (name.substring(0, 1) >= 'A' && name.substring(0, 1) <= 'Z' ? name.toLowerCase() : name);
return (name.substring(0, 4) === 'svg:' ? name.substring(4) : name);
},
/** Load an external SVG document.
@param url {string} The location of the SVG document or
the actual SVG content (starting with '<code><svg</code>'.
@param settings {boolean|function|object} Either <code>addTo</code> below or <code>onLoad</code> below or
additional settings for the load with attributes below:
<code>addTo</code> {boolean} <code>true</code> to add to what's already there,
or <code>false</code> to clear the canvas first,
<code>changeSize</code> {boolean} <code>true</code> to allow the canvas size to change,
or <code>false</code> to retain the original,
<code>onLoad</code> {function} callback after the document has loaded,
'<code>this</code>' is the container, receives SVG object and optional error message as a parameter,
<code>parent</code> {string|SVGElement|jQuery} the parent to load into,
defaults to top-level svg element.
@return {SVGWrapper} This wrapper. */
load: function(url, settings) {
settings = (typeof settings === 'boolean' ? {addTo: settings} :
(typeof settings === 'function' ? {onLoad: settings} :
(typeof settings === 'string' ? {parent: settings} :
(typeof settings === 'object' && settings.nodeName ? {parent: settings} :
(typeof settings === 'object' && settings.jquery ? {parent: settings} : settings || {})))));
if (!settings.parent && !settings.addTo) {
this.clear(false);
}
var size = [this._svg.getAttribute('width'), this._svg.getAttribute('height')];
var wrapper = this;
// Report a problem with the load
var reportError = function(message) {
message = $.svg.local.errorLoadingText + ': ' + message;
if (settings.onLoad) {
settings.onLoad.apply(wrapper._container || wrapper._svg, [wrapper, message]);
}
else {
wrapper.text(null, 10, 20, message);
}
};
// Create a DOM from SVG content
var loadXML4IE = function(data) {
var xml = new ActiveXObject('Microsoft.XMLDOM');
xml.validateOnParse = false;
xml.resolveExternals = false;
xml.async = false;
xml.loadXML(data);
if (xml.parseError.errorCode !== 0) {
reportError(xml.parseError.reason);
return null;
}
return xml;
};
// Load the SVG DOM
var loadSVG = function(data) {
if (!data) {
return;
}
if (data.documentElement.nodeName !== 'svg') {
var errors = data.getElementsByTagName('parsererror');
var messages = (errors.length ? errors[0].getElementsByTagName('div') : []); // Safari
reportError(!errors.length ? '???' : (messages.length ? messages[0] : errors[0]).firstChild.nodeValue);
return;
}
var parent = (settings.parent ? $(settings.parent)[0] : wrapper._svg);
var attrs = {};
for (var i = 0; i < data.documentElement.attributes.length; i++) {
var attr = data.documentElement.attributes.item(i);
if (!(attr.nodeName === 'version' || attr.nodeName.substring(0, 5) === 'xmlns')) {
attrs[attr.nodeName] = attr.nodeValue;
}
}
wrapper.configure(parent, attrs, !settings.parent);
var nodes = data.documentElement.childNodes;
for (var i = 0; i < nodes.length; i++) {
try {
parent.appendChild(wrapper._svg.ownerDocument.importNode(nodes[i], true));
if (nodes[i].nodeName === 'script') {
$.globalEval(nodes[i].textContent);
}
}
catch (e) {
wrapper.add(parent, nodes[i]);
}
}
if (!settings.keepRelativeLinks && url.match('/')) {
var base = url.replace(/\/[^\/]*$/, '/');
$('*', parent).each(function() {
var href = $(this).attr('xlink:href');
if (href && !href.match(/(^[a-z][-a-z0-9+.]*:.*$)|(^\/.*$)|(^#.*$)/i)) {
$(this).attr('xlink:href', base + href);
}
});
}
if (!settings.changeSize) {
wrapper.configure(parent, {width: size[0], height: size[1]});
}
if (settings.onLoad) {
settings.onLoad.apply(wrapper._container || wrapper._svg, [wrapper]);
}
};
if (url.match('<svg')) { // Inline SVG
try {
loadSVG(new DOMParser().parseFromString(url, 'text/xml'));
} catch (e) {
reportError(e);
}
}
else { // Remote SVG
$.ajax({url: url, dataType: 'xml',
success: function(xml) {
loadSVG(xml);
}, error: function(http, message, exc) {
reportError(message + (exc ? ' ' + exc.message : ''));
}});
}
return this;
},