-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsrc
305 lines (240 loc) · 12.2 KB
/
src
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
//This libary is an ultra-light libray of static SVG functions (non animated with SVG) to be used in JavaScript.
//It is is a very small subset of SVG.js, but to be also used more compactly than SVG.js and states errors explicitly.
// Usage: Units are necessary! Tests are at the bottom of the script.
class SVG_InputValidator {
//The functions that are inside the Svg Input Validator class are to validate input.
static isValidRGBA(str) {
const regex = /^rgba\((\d{1,3}),(\d{1,3}),(\d{1,3}),(\d?(?:\.\d{1,2})?)\)$/;
const colorCode = str.toString();
if (regex.test(colorCode)) {
const values = colorCode.match(regex).slice(1).map(Number);
if (values.slice(0, 3).every(num => num >= 0 && num <= 255) && values[3] >= 0 && values[3] <= 1) {
return true;
} else {
console.error("Invalid color code values. Ensure values are within valid range.");
}
} else {
console.error("Invalid color code format. The color must be of the format: rgba().");
}
}
static isValidCSSDimension_and_Unit(str) {
const units = ["%", "ch", "cm", "em", "ex", "in", "lh", "mm", "pc", "pt", "px", "rem", "vh", "vmax", "vmin", "vw"];
let numPart = "";
let unitPart = "";
str = str.toString();
if(str.length <= 18) {
let i = 0;
while (i < str.length && ((str[i] >= '0' && str[i] <= '9') || str[i] === '.' || str[i] === '-')) {
numPart += str[i];
i++;
}
while (i < str.length) {
unitPart += str[i];
i++;
}
if (isNaN(parseFloat(numPart))) {
console.error("Invalid input. The numeric part is not a valid number.");
};
if (!units.includes(unitPart)) {
console.error("Invalid input. The unit part must be a valid CSS unit.");
};
//This returns the CSS dimension and unit for manipulation in JavaScript
if (!isNaN(parseFloat(numPart)) && units.includes(unitPart)) {
return true;
};
} else {
console.error("The dimension value has too many characters.");
console.error("The number of charactars must be less than or equal to 18.");
}
}
//We do not allow for SVG paths with curves.
static isValidSVGPath(pathString) {
const regex = /^[MmLlHhVvZz0-9\-,.\s]+$/;
pathString = pathString.toString();
if(regex.test(pathString) == true) {
return true;
} else {
console.error("The path is not a valid path string.");
}
}
static isValidSVGPolyline(polylineString) {
const regex = /^(\s*\d+(?:\.\d+)?\s*,\s*\d+(?:\.\d+)?\s*)+$/;
polylineString = polylineString.toString();
if(regex.test(polylineString.trim())) {
return true;
} else{
console.error("The polyline is not a valid polyline string.");
}
}
static isValidFontFamily(str) {
const regex = /^[a-zA-Z0-9 \-,'"]+$/;
if(regex.test(str) == true) {
return true;
} else {
console.error("The string is not a valid font family string.");
}
}
}
class SvgElementCreator {
constructor(svgNamespace) {
this.svgNamespace = svgNamespace || "http://www.w3.org/2000/svg";
}
//Basic Shapes: <circle>, <ellipse>, <line>, <path>, <polygon>, <polyline>, <rect>
//This function dynamically creates an SVG circle. A circle is a special case of an ellipse.
Circle(svg, cx, cy, r, fill, strokeColor, strokeWidth){
if(
SVG_InputValidator.isValidCSSDimension_and_Unit(cx) == true &&
SVG_InputValidator.isValidCSSDimension_and_Unit(cy) == true &&
SVG_InputValidator.isValidCSSDimension_and_Unit(r) == true &&
SVG_InputValidator.isValidRGBA(fill) == true &&
SVG_InputValidator.isValidRGBA(strokeColor) == true &&
SVG_InputValidator.isValidCSSDimension_and_Unit(strokeWidth) == true) {
let circle = document.createElementNS(this.svgNamespace, "circle");
circle.setAttribute("cx",cx.toString());
circle.setAttribute("cy",cy.toString());
circle.setAttribute("r",r.toString());
circle.setAttribute("fill",fill.toString());
circle.setAttribute("stroke",strokeColor.toString());
circle.setAttribute("stroke-width",strokeWidth.toString());
svg.appendChild(circle);
};
}
//This function dynamically creates an SVG ellipse.
Ellipse(svg, cx, cy, rx, ry, fill, strokeColor, strokeWidth){
if(
SVG_InputValidator.isValidCSSDimension_and_Unit(cx) == true &&
SVG_InputValidator.isValidCSSDimension_and_Unit(cy) == true &&
SVG_InputValidator.isValidCSSDimension_and_Unit(rx) == true &&
SVG_InputValidator.isValidCSSDimension_and_Unit(ry) == true &&
SVG_InputValidator.isValidRGBA(fill) == true &&
SVG_InputValidator.isValidRGBA(strokeColor) == true &&
SVG_InputValidator.isValidCSSDimension_and_Unit(strokeWidth) == true) {
let ellipse = document.createElementNS(this.svgNamespace, "ellipse");
ellipse.setAttribute("cx",cx);
ellipse.setAttribute("cy",cy);
ellipse.setAttribute("rx",rx);
ellipse.setAttribute("ry",ry);
ellipse.setAttribute("fill",fill);
ellipse.setAttribute("stroke",strokeColor);
ellipse.setAttribute("stroke-width",strokeWidth);
svg.appendChild(ellipse);
};
}
//This function dynamically creates an SVG line.
Line(svg, x1, y1, x2, y2, strokeColor, strokeWidth){
if(
SVG_InputValidator.isValidCSSDimension_and_Unit(x1) == true &&
SVG_InputValidator.isValidCSSDimension_and_Unit(y1) == true &&
SVG_InputValidator.isValidCSSDimension_and_Unit(x2) == true &&
SVG_InputValidator.isValidCSSDimension_and_Unit(y2) == true &&
SVG_InputValidator.isValidRGBA(strokeColor) == true &&
SVG_InputValidator.isValidCSSDimension_and_Unit(strokeWidth) == true) {
let line = document.createElementNS(this.svgNamespace, "line");
line.setAttribute("x1",x1);
line.setAttribute("y1",y1);
line.setAttribute("x2",x2);
line.setAttribute("y2",y2);
line.setAttribute("stroke",strokeColor);
line.setAttribute("stroke-width",strokeWidth);
svg.appendChild(line);
};
}
//This function dynamically creates an SVG path. This library only allows for straight lines, no curves.
Path(svg, d, fill, strokeColor, strokeWidth){
if(
SVG_InputValidator.isValidSVGPath(d) == true &&
SVG_InputValidator.isValidRGBA(fill) == true &&
SVG_InputValidator.isValidRGBA(strokeColor) == true &&
SVG_InputValidator.isValidCSSDimension_and_Unit(strokeWidth) == true) {
let path = document.createElementNS(this.svgNamespace, "path");
path.setAttribute("d",d);
path.setAttribute("fill",fill);
path.setAttribute("stroke",strokeColor);
path.setAttribute("stroke-width",strokeWidth);
svg.appendChild(path);
};
}
//This function dynamically creates an SVG polygon.
//It does not test if it is closed or not. It only accepts points.
Polygon(svg, points, fill, strokeColor, strokeWidth){
if(
SVG_InputValidator.isValidSVGPolyline(points) == true &&
SVG_InputValidator.isValidRGBA(fill) == true &&
SVG_InputValidator.isValidRGBA(strokeColor) == true &&
SVG_InputValidator.isValidCSSDimension_and_Unit(strokeWidth) == true) {
let polygon = document.createElementNS(this.svgNamespace, "polygon");
polygon.setAttribute("points",points);
polygon.setAttribute("fill",fill);
polygon.setAttribute("stroke",strokeColor);
polygon.setAttribute("stroke-width",strokeWidth);
svg.appendChild(polygon);
};
}
//This function dynamically creates an SVG polyline. It should be used for only lines. It is essentially a polygonal chain.
Polyline(svg, points, fill, strokeColor, strokeWidth){
if(
SVG_InputValidator.isValidSVGPolyline(points) == true &&
SVG_InputValidator.isValidRGBA(fill) == true &&
SVG_InputValidator.isValidRGBA(strokeColor) == true &&
SVG_InputValidator.isValidCSSDimension_and_Unit(strokeWidth) == true) {
let polyline = document.createElementNS(this.svgNamespace, "polyline");
polyline.setAttribute("points",points);
polyline.setAttribute("fill",fill);
polyline.setAttribute("stroke",strokeColor);
polyline.setAttribute("stroke-width",strokeWidth);
svg.appendChild(polyline);
};
}
//This function dynamically creates an SVG rectangle. A rectangle is a special case of a polygon.
Rectangle(svg, x, y, width, height, fill, strokeColor, strokeWidth){
if(
SVG_InputValidator.isValidCSSDimension_and_Unit(x) == true &&
SVG_InputValidator.isValidCSSDimension_and_Unit(y) == true &&
SVG_InputValidator.isValidCSSDimension_and_Unit(width) == true &&
SVG_InputValidator.isValidCSSDimension_and_Unit(height) == true &&
SVG_InputValidator.isValidRGBA(fill) == true &&
SVG_InputValidator.isValidRGBA(strokeColor) == true &&
SVG_InputValidator.isValidCSSDimension_and_Unit(strokeWidth) == true) {
let rect = document.createElementNS(this.svgNamespace, "rect");
rect.setAttribute("x",x);
rect.setAttribute("y",y);
rect.setAttribute("width",width);
rect.setAttribute("height",height);
rect.setAttribute("fill",fill);
rect.setAttribute("stroke",strokeColor);
rect.setAttribute("stroke-width",strokeWidth);
svg.appendChild(rect);
};
}
//Text Elements: <text>
Text(svg, x, y, textContent, fontFamily, fontSize, Color){
if(
SVG_InputValidator.isValidCSSDimension_and_Unit(x) == true &&
SVG_InputValidator.isValidCSSDimension_and_Unit(y) == true &&
SVG_InputValidator.isValidFontFamily(fontFamily) == true &&
SVG_InputValidator.isValidCSSDimension_and_Unit(fontSize) == true &&
SVG_InputValidator.isValidRGBA(Color) == true) {
let text = document.createElementNS(this.svgNamespace, "text");
text.setAttribute("x", x);
text.setAttribute("y", y);
text.setAttribute('font-family',fontFamily);
text.setAttribute('font-size',fontSize);
text.setAttribute('color',Color);
text.textContent = textContent.toString();
svg.appendChild(text);
};
}
}
// Usage: Units are necessary!
/*
let creator = new SvgElementCreator();
let svg = document.getElementById('mySvg');
let circle = creator.Circle(svg, '50px', '50px', '20px', 'rgba(255,0,0,1)', 'rgba(0,0,0,1)', '2px');
let ellipse = creator.Ellipse(svg, '50px', '50px', '30px', '20px', 'rgba(255,0,0,1)', 'rgba(0,0,0,1)', '2px');
let line = creator.Line(svg, '10px', '10px', '40px', '40px', 'rgba(255,0,0,1)', '2px');
let rectangle = creator.Rectangle(svg, '50px', '50px', '80px', '40px', 'rgba(255,0,0,1)', 'rgba(0,0,0,1)', '2px');
let path = creator.Path(svg, "M10 20 L30 40", 'rgba(255,0,0,1)', 'rgba(0,0,0,1)', '2px');
let polygon = creator.Polygon(svg, "50,50 70,70 60,90", 'rgba(255,0,0,1)', 'rgba(0,0,0,1)', '2px');
let polyline = creator.Polyline(svg, "60,110 70,120 80,130", 'rgba(255,0,0,1)', 'rgba(0,0,0,1)', '2px');
let text = creator.Text(svg, '50px', '50px', "Hello SVG",'Arial', '20px', 'rgba(255,0,0,1)');
*/