generated from rkstudio585/rkstudio585
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabscomsvg.js
328 lines (308 loc) · 10.3 KB
/
abscomsvg.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
(function() {
const AbscomSVG = {
/**
* Renders SVG content into the specified SVG element.
* @param {string|SVGElement} svgArg - The ID of the SVG element or the SVG element itself.
* @param {Object|Object[]} def - A single definition object or an array of definitions.
*/
render: function(svgArg, def) {
let svg;
if (typeof svgArg === 'string') {
svg = document.getElementById(svgArg);
} else if (svgArg instanceof SVGElement) {
svg = svgArg;
} else {
console.error('Invalid SVG argument:', svgArg);
return;
}
if (!svg) {
console.error('SVG element not found');
return;
}
const defs = Array.isArray(def) ? def : [def];
// Track existing elements with IDs
const existingEls = {};
Array.from(svg.children).forEach(child => {
const id = child.getAttribute('id');
if (id) existingEls[id] = child;
});
// Process each top-level definition
defs.forEach(d => {
if (d.id && existingEls[d.id]) {
updateElement(existingEls[d.id], d);
delete existingEls[d.id];
} else {
const el = createElement(d);
if (el) svg.appendChild(el);
}
});
// Remove elements no longer in definitions
for (const id in existingEls) {
svg.removeChild(existingEls[id]);
}
},
/** Element Creation Helpers */
/**
* Creates a circle definition.
* @param {number} cx - Center x-coordinate.
* @param {number} cy - Center y-coordinate.
* @param {number} r - Radius.
* @param {string} fill - Fill color.
* @returns {Object} Circle definition.
*/
circle: function(cx, cy, r, fill) {
return { type: 'circle', attrs: { cx, cy, r, fill } };
},
/**
* Creates a rectangle definition.
* @param {number} x - X-coordinate.
* @param {number} y - Y-coordinate.
* @param {number} width - Width.
* @param {number} height - Height.
* @param {string} fill - Fill color.
* @returns {Object} Rectangle definition.
*/
rect: function(x, y, width, height, fill) {
return { type: 'rect', attrs: { x, y, width, height, fill } };
},
/**
* Creates a text definition.
* @param {number} x - X-coordinate.
* @param {number} y - Y-coordinate.
* @param {string} text - Text content.
* @param {Object} [attrs] - Additional attributes.
* @returns {Object} Text definition.
*/
text: function(x, y, text, attrs = {}) {
return { type: 'text', attrs: { x, y, ...attrs }, text };
},
/**
* Creates an animation definition.
* @param {string} attributeName - Attribute to animate.
* @param {string} from - Starting value.
* @param {string} to - Ending value.
* @param {string} dur - Duration (e.g., '2s').
* @param {string} repeatCount - Repeat count (e.g., 'indefinite').
* @returns {Object} Animation definition.
*/
animate: function(attributeName, from, to, dur, repeatCount) {
return { type: 'animate', attrs: { attributeName, from, to, dur, repeatCount } };
},
/**
* Creates a line definition.
* @param {number} x1 - Starting x-coordinate.
* @param {number} y1 - Starting y-coordinate.
* @param {number} x2 - Ending x-coordinate.
* @param {number} y2 - Ending y-coordinate.
* @param {string} stroke - Stroke color.
* @returns {Object} Line definition.
*/
line: function(x1, y1, x2, y2, stroke) {
return { type: 'line', attrs: { x1, y1, x2, y2, stroke } };
},
/**
* Creates a polygon definition.
* @param {string} points - Points of the polygon.
* @param {string} fill - Fill color.
* @returns {Object} Polygon definition.
*/
polygon: function(points, fill) {
return { type: 'polygon', attrs: { points, fill } };
},
/**
* Creates a path definition.
* @param {string} d - Path data.
* @param {string} fill - Fill color.
* @returns {Object} Path definition.
*/
path: function(d, fill) {
return { type: 'path', attrs: { d, fill } };
},
/**
* Creates an image definition.
* @param {string} href - Image source.
* @param {number} x - X-coordinate.
* @param {number} y - Y-coordinate.
* @param {number} width - Width.
* @param {number} height - Height.
* @returns {Object} Image definition.
*/
image: function(href, x, y, width, height) {
return { type: 'image', attrs: { 'xlink:href': href, x, y, width, height } };
},
/**
* Creates an ellipse definition.
* @param {number} cx - Center x-coordinate.
* @param {number} cy - Center y-coordinate.
* @param {number} rx - X-radius.
* @param {number} ry - Y-radius.
* @param {string} fill - Fill color.
* @returns {Object} Ellipse definition.
*/
ellipse: function(cx, cy, rx, ry, fill) {
return { type: 'ellipse', attrs: { cx, cy, rx, ry, fill } };
},
/** Attribute and Transformation Helpers */
/**
* Adds stroke attributes to a definition.
* @param {Object} def - Element definition.
* @param {string} color - Stroke color.
* @param {number} width - Stroke width.
* @returns {Object} Updated definition.
*/
withStroke: function(def, color, width) {
def.attrs.stroke = color;
def.attrs['stroke-width'] = width;
return def;
},
/**
* Creates a transformation string.
* @param {string} type - Transformation type (e.g., 'translate', 'rotate').
* @param {...number} values - Transformation values.
* @returns {string} Transformation string.
*/
transform: function(type, ...values) {
return `${type}(${values.join(',')})`;
}
};
/**
* Creates an SVG element from a definition object.
* @param {Object} def - Element definition.
* @returns {SVGElement|null} Created element or null if invalid.
*/
function createElement(def) {
if (!validateDef(def)) return null;
const el = document.createElementNS('http://www.w3.org/2000/svg', def.type);
if (def.id) el.setAttribute('id', def.id);
for (const attr in def.attrs) {
el.setAttribute(attr, def.attrs[attr]);
}
if (def.text) el.textContent = def.text;
if (def.events) {
for (const event in def.events) {
const handlers = Array.isArray(def.events[event]) ? def.events[event] : [def.events[event]];
handlers.forEach(handler => {
if (typeof handler === 'function') {
el.addEventListener(event, handler);
} else {
el.addEventListener(event, handler.callback, handler.options);
}
});
}
}
if (def.children) {
def.children.forEach(childDef => {
const childEl = createElement(childDef);
if (childEl) el.appendChild(childEl);
});
}
return el;
}
/**
* Updates an existing SVG element with a new definition.
* @param {SVGElement} el - Element to update.
* @param {Object} def - New definition.
*/
function updateElement(el, def) {
if (def.id && el.getAttribute('id') !== def.id) {
el.setAttribute('id', def.id);
}
for (const attr in def.attrs) {
el.setAttribute(attr, def.attrs[attr]);
}
el.textContent = def.text || '';
if (def.events) {
for (const event in def.events) {
const handlers = Array.isArray(def.events[event]) ? def.events[event] : [def.events[event]];
handlers.forEach(handler => {
if (typeof handler === 'function') {
el.addEventListener(event, handler);
} else {
el.addEventListener(event, handler.callback, handler.options);
}
});
}
}
const childEls = Array.from(el.children);
const childDefs = def.children || [];
const existing = {};
childEls.forEach(child => {
const id = child.getAttribute('id');
if (id) existing[id] = child;
});
childDefs.forEach(childDef => {
if (childDef.id && existing[childDef.id]) {
updateElement(existing[childDef.id], childDef);
delete existing[childDef.id];
} else {
el.appendChild(createElement(childDef));
}
});
for (const id in existing) el.removeChild(existing[id]);
}
/**
* Validates a definition object.
* @param {Object} def - Definition to validate.
* @returns {boolean} True if valid, false otherwise.
*/
function validateDef(def) {
if (!def || !def.type) {
console.error('Definition missing type');
return false;
}
if (def.type === 'circle') {
const required = ['cx', 'cy', 'r'];
for (const attr of required) {
if (!(attr in def.attrs)) {
console.error(`Circle missing attribute: ${attr}`);
return false;
}
}
} else if (def.type === 'rect') {
const required = ['x', 'y', 'width', 'height'];
for (const attr of required) {
if (!(attr in def.attrs)) {
console.error(`Rect missing attribute: ${attr}`);
return false;
}
}
} else if (def.type === 'line') {
const required = ['x1', 'y1', 'x2', 'y2'];
for (const attr of required) {
if (!(attr in def.attrs)) {
console.error(`Line missing attribute: ${attr}`);
return false;
}
}
} else if (def.type === 'polygon') {
if (!('points' in def.attrs)) {
console.error('Polygon missing points attribute');
return false;
}
} else if (def.type === 'path') {
if (!('d' in def.attrs)) {
console.error('Path missing d attribute');
return false;
}
} else if (def.type === 'image') {
if (!('xlink:href' in def.attrs)) {
console.error('Image missing xlink:href attribute');
return false;
}
} else if (def.type === 'ellipse') {
const required = ['cx', 'cy', 'rx', 'ry'];
for (const attr of required) {
if (!(attr in def.attrs)) {
console.error(`Ellipse missing attribute: ${attr}`);
return false;
}
}
} else if (def.type === 'text' && !def.text) {
console.error('Text element missing text content');
return false;
}
return true;
}
// Expose AbscomSVG globally
window.AbscomSVG = AbscomSVG;
})();