-
Notifications
You must be signed in to change notification settings - Fork 25
/
svg.js
161 lines (153 loc) · 5.86 KB
/
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
/**
* Created by Fry on 11/7/16.
*/
function svg_svg({id="", html_class="", style="", width=500, height=500, viewBox="", child_elements=[]}){
let result = '<svg' +
' id="' + id +
'" class="' + html_class +
'" style="' + style +
'" width="' + width +
'" height="' + height +
((viewBox.length == 0) ? "" : '" viewBox="' + viewBox) +
'">\n ' + child_elements.join("\n ") +
'\n</svg>'
return result
}
function svg_circle({id="", html_class="", style="", cx=0, cy=0, r=10, color="black", border_width=1, border_color="black"}){
if (style.length > 0) { //ensure proper ending
if (last(style) == ";") style += " "
else style += "; "
}
style += 'fill:' + color + '; stroke:' + border_color + '; stroke-width:' + border_width + ";"
let result = '<circle' +
' id="' + id +
'" class="' + html_class +
'" style="' + style +
'" cx="' + cx +
'" cy="' + cy +
'" r="' + r +
//'" namespaceURI="http://www.w3.org/2000/svg' +
'"/>'
return result
}
function svg_ellipse({id="", html_class="", style="", cx=0, cy=0, rx=20, ry=10, color="black", border_width=1, border_color="black"}){
if (style.length > 0) { //ensure proper ending
if (last(style) == ";") style += " "
else style += "; "
}
style += 'fill:' + color + '; stroke:' + border_color + '; stroke-width:' + border_width + ";"
let result = '<ellipse' +
' id="' + id +
'" class="' + html_class +
'" style="' + style +
'" cx="' + cx +
'" cy="' + cy +
'" rx="' + rx +
'" ry="' + ry +
'"/>'
return result
}
function svg_line({id="", html_class="", style="", x1=0, y1=0, x2=100, y2=100, color="black", width=1}){
if (style.length > 0) { //ensure proper ending
if (last(style) == ";") style += " "
else style += "; "
}
style += 'stroke:' + color + '; stroke-width:' + width + ";"
return '<line' +
' id="' + id +
'" class="' + html_class +
'" style="' + style +
'" x1="' + x1 +
'" y1="' + y1 +
'" x2="' + x2 +
'" y2="' + y2 +
'"/>'
}
//svg_line()
function svg_rect({id="", html_class="", style="", x=0, y=0, width=100, height=100, rx=0, ry=0,
color="black", border_width=1, border_color="black"}){
if (style.length > 0) { //ensure proper ending
if (last(style) == ";") style += " "
else style += "; "
}
style += 'fill:' + color + '; stroke-width:' + border_width + '; stroke:' + border_color + ";"
return '<rect' +
' id="' + id +
'" class="' + html_class +
'" style="' + style + '"' +
((x === null) ? '' : ' x="' + x + '"') + //for svg "inheritance
((y === null) ? '' : ' y="' + y + '"') + //for svg "inheritance
' width="' + width +
'" height="' + height +
'" rx="' + rx +
'" ry="' + ry +
'"/>'
}
// svg_rect()
//'<text style="font-size:30px;" x="60" y="400" fill="#AA66AA" transform="rotate(0, 0, 0)">Flexibility/Power/Complexity</text> <!-- 2nd arg = x 3rd arg = y rotate point -->
//points is of reasonable format [[2,3], [4,5]...] but svg requires "2,3 4,5"
function svg_polygon({id="", html_class="", style="", points=[], color="black", border_width=1, border_color="black"}){
if (style.length > 0) { //ensure proper ending
if (last(style) == ";") style += " "
else style += "; "
}
let points_string = ""
let separator = ""
for(let p of points){
points_string += separator + p[0] + "," + p[1]
separator = " "
}
style += 'fill:' + color + '; stroke:' + border_color + '; stroke-width:' + border_width + ';'
return '<polygon' +
' id="' + id +
'" class="' + html_class +
'" style="' + style +
'" points="' + points_string +
'"/>'
}
function svg_polyline({id="", html_class="", style="", points=[], color="black", width=1}){
if (style.length > 0) { //ensure proper ending
if (last(style) == ";") style += " "
else style += "; "
}
let points_string = ""
let separator = ""
for(let p of points){
points_string += separator + p[0] + "," + p[1]
separator = " "
}
style += 'stroke:' + color + '; stroke-width:' + width + ';'
return '<polyline' +
' id="' + id +
'" class="' + html_class +
'" style="' + style +
'" points="' + points_string +
'" fill="none' +
'"/>'
}
function svg_text({id="", html_class="", style="", text="hi", x=20, y=20, size=16, color="black", border_width=0, border_color="black", transform="rotate(0, 0, 0)"}){
if (style.length > 0) { //ensure proper ending
if (last(style) == ";") style += " "
else style += "; "
}
style += 'fill:' + color + '; font-size:' + size + "px" + '; stroke:' + border_color + '; stroke-width:' + border_width + ';'
return '<text' +
' id="' + id +
'" class="' + html_class +
'" style="' + style +
'" x="' + x +
'" y="' + y +
'" transform="' + transform +
'">' + text +
'</text>'
}
// svg_text()
function svg_html({html="<h1>hi</h1>", x=0, y=0, width=100, height=100}){
return '<foreignObject x="' + x +
'" y="' + y +
'" width="' + width +
'" height="' + height +
//'" requiredExtensions="http://www.w3.org/1999/xhtml">' +
'">' + html +
'</foreignObject>'
}