-
Notifications
You must be signed in to change notification settings - Fork 0
/
mySvg.js
106 lines (82 loc) · 2.42 KB
/
mySvg.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
var mySvg = (function () {
"use strict";
var nsSvg = "http://www.w3.org/2000/svg";
function svgInit(s, options) {
var o = options || {},
h = o.height || 100,
w = o.width || 100;
s.setAttributeNS(null, 'width', w);
s.setAttributeNS(null, 'height', h);
}
function svgElement(options) {
var s = document.createElementNS(nsSvg, "svg");
svgInit(s, options);
return s;
}
function svgCircle(options) {
var c = document.createElementNS(nsSvg, "circle"),
o = options || {},
cx = o.cx || 50,
cy = o.cy || 50,
r = o.r || 40;
c.setAttributeNS(null, 'cx', cx);
c.setAttributeNS(null, 'cy', cy);
c.setAttributeNS(null, 'r', r);
return c;
}
function svgDashedCircle(options) {
var c = svgCircle(options),
o = options || {},
stroke = o.stroke || '#fff';
c.setAttributeNS(null, 'stroke', stroke);
c.setAttributeNS(null, 'fill', 'transparent');
c.setAttributeNS(null, 'stroke-dasharray', '5,10');
c.setAttributeNS(null, 'stroke-width', 1);
c.setAttributeNS(null, 'class', 'persist');
return c;
}
function svgCircles(coords, options) {
var g = document.createElementNS(nsSvg, "g"),
o = options || {},
stroke = o.stroke || '#fff',
fill = o.fill || 'transparent';
g.setAttributeNS(null, 'stroke', stroke);
g.setAttributeNS(null, 'fill', fill);
coords.forEach(function (c) {
g.appendChild(svgCircle({cx: c.x, cy: c.y, r: o.r}));
});
return g;
}
function svgCircles2(params, options) {
var g = document.createElementNS(nsSvg, "g"),
o = options || {},
stroke = o.stroke || '#fff',
fill = o.fill || 'transparent';
g.setAttributeNS(null, 'fill', fill);
g.setAttributeNS(null, 'stroke', stroke);
params.forEach(function (p) {
g.appendChild(svgCircle({cx: p.x, cy: p.y, r: p.r}));
});
return g;
}
function removeGroups(parent) {
var gNodes = parent.getElementsByTagName('g'),
arrNodes = [],
i;
for (i = 0; i < gNodes.length; i++) {
arrNodes.push(gNodes[i]);
}
arrNodes.forEach(function (node) {
if (node.parentNode) { node.parentNode.removeChild(node); }
});
}
return {
svgInit: svgInit,
svgElement: svgElement,
svgCircle: svgCircle,
svgDashedCircle: svgDashedCircle,
svgCircles: svgCircles,
svgCircles2: svgCircles2,
removeGroups: removeGroups
};
}());