-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvg.js
269 lines (220 loc) · 7.13 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
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
/**
* @copyright 2022-2023 Chris Zuber <[email protected]>
*/
import { SVG, XLINK } from '@shgysk8zer0/consts/namespaces.js';
import { isObject } from './utility.js';
import { css, data, attr } from './attrs.js';
import { SVG as TYPE } from '@shgysk8zer0/consts/mimes.js';
export { rotate, scale, translate } from './animate.js';
export const DEFAULT_ROLE = 'presentation';
export function createSVGFile(svg, { name = 'image.svg' } = {}) {
if (svg instanceof SVGElement) {
return new File([svg.outerHTML], name, { type: TYPE });
} else {
throw new TypeError('Expected an <svg>');
}
}
export function createSVGElement (tag, {
fill, stroke, width, height, pathLength, children = [], id, classList = [],
styles, dataset, role, ariaLabel,
events: {
capture,
passive,
once,
signal,
...events
} = {},
animation: {
keyframes,
duration = 0,
delay = 0,
endDelay = 0,
easing = 'linear',
direction = 'normal',
fill: svgFill = 'none',
iterations = 1,
iterationStart = 0,
composite = 'replace',
iterationComposite = 'replace',
pseudoElement,
} = {},
...rest } = {}) {
const el = document.createElementNS(SVG, tag);
if (typeof id === 'string') {
el.id = id;
}
if (typeof role === 'string') {
el.setAttribute('role', role);
}
if (typeof ariaLabel === 'string') {
el.setAttribute('aria-label', ariaLabel);
}
if (Array.isArray(classList) && classList.length !== 0) {
el.classList.add(...classList);
}
if (typeof fill === 'string') {
el.setAttribute('fill', fill);
}
if (typeof stroke === 'string') {
el.setAttribute('stroke', stroke);
}
if (typeof height === 'number' && ! Number.isNaN(height)) {
el.setAttribute('height', height.toString());
}
if (typeof width === 'number' && ! Number.isNaN(width)) {
el.setAttribute('width', width.toString());
}
if (typeof pathLength === 'number' && ! Number.isNaN(pathLength)) {
el.setAttribute('pathLength', pathLength.toString());
}
if (typeof rest.title === 'string') {
const title = createSVGElement('title');
title.textContent = rest.title;
el.prepend(title);
}
if (Array.isArray(keyframes) || isObject(keyframes) && el.animate instanceof Function) {
el.animate(keyframes, {
duration, delay, endDelay, easing, direction, fill: svgFill, iterations,
iterationStart, composite, iterationComposite, pseudoElement,
});
}
if (Array.isArray(children) && children.length !== 0) {
el.append(...children.filter(node => (typeof node === 'string') || (node instanceof Element)));
}
if (isObject(styles)) {
css(el, styles);
}
if (isObject(events)) {
Object.entries(events).forEach(
([event, callback]) => el.addEventListener(event, callback, { capture, passive, once, signal })
);
}
if (isObject(dataset)) {
data(el, dataset);
}
attr(el, rest);
return el;
}
export function createSVG({
id = null,
fill = null,
viewBox = null,
height = null,
width = null,
slot = null,
role = DEFAULT_ROLE,
ariaLabel,
hidden = false,
classList = [],
children = [],
animation,
part = [],
...rest
} = {}) {
const svg = createSVGElement('svg', { width, height, fill, animation, children, id, classList, ...rest });
svg.setAttribute('role', role);
svg.setAttribute('xmlns', SVG);
if (typeof viewBox === 'string') {
svg.setAttributeNS(null, 'viewBox', viewBox);
} else if (Array.isArray(viewBox)) {
svg.setAttributeNS(null, 'viewBox', viewBox.map(n => n.toString()).join(' '));
}
if (typeof ariaLabel === 'string') {
svg.setAttribute('aria-label', ariaLabel);
}
if (hidden === true) {
svg.setAttribute('aria-hidden', 'true');
}
if (typeof slot === 'string') {
svg.slot = slot;
}
if (Array.isArray(classList) && classList.length !== 0) {
svg.classList.add(...classList);
}
if (Array.isArray(part) && part.length !== 0) {
if('part' in svg) {
svg.part.add(...part);
} else {
svg.setAttribute('part', part.join(' '));
}
}
return svg;
}
export function useSVG(sprite, {
id = null,
src = '/img/icons.svg',
fill = null,
height = null,
width = null,
slot = null,
part = [],
classList = [],
label = null,
animation,
...rest
} = {}) {
const svg = createSVG({ id, fill, height, width, classList, label, slot, part, animation, hidden: true, ...rest });
const use = createSVGElement('use');
if (typeof src === 'string') {
const url = new URL(src, document.baseURI);
url.hash = `#${sprite}`;
use.setAttributeNS(XLINK, 'xlink:href', url.href);
} else {
use.setAttributeNS(XLINK, 'xlink:href', `#${sprite}`);
}
svg.append(use);
return svg;
}
export function createPath(d, { id, fill, stroke, pathLength, animation, ...rest } = {}) {
const path = createSVGElement('path', { id, fill, stroke, pathLength, animation, ...rest });
if (Array.isArray(d) && d.length !== 0) {
path.setAttribute('d', d.join(' '));
} else if (typeof d === 'string') {
path.setAttribute('d', d);
} else {
throw new TypeError('Invalid `d` attribute in <path>.');
}
return path;
}
export function createGroup({ id, fill, stroke, children, animation, ...attrs }) {
return createSVGElement('g', { id, fill, stroke, children, animation, ...attrs });
}
export function createRect({ id, width, height, x, y, rx, ry, fill, stroke, pathLength, animation, ...rest }) {
return createSVGElement('rect', { id, width, height, x, y, rx, ry, fill, stroke, pathLength, animation, ...rest });
}
export function createCircle({ id, width, height, cx, cy, r, fill, stroke, pathLength, animation, ...rest }) {
return createSVGElement('circle', { id, width, height, cx, cy, r, fill, stroke, pathLength, animation, ...rest });
}
export function createEllipse({ id, width, height, cx, cy, rx, ry, fill, stroke, pathLength, animation, ...rest }) {
return createSVGElement('ellipse', { id, width, height, cx, cy, rx, ry, fill, stroke, pathLength, animation, ...rest });
}
export function createPolygon(points, { id, fill, stroke, pathLength, animation, ...rest } = {}) {
const polygon = createSVGElement('polygon', { id, fill, stroke, pathLength, animation, ...rest });
if (Array.isArray(points) && points.length !== 0) {
polygon.setAttribute('points', points.map(([x, y]) => `${x},${y}`).join(' '));
} else if (typeof points === 'string') {
polygon.setAttribute('points', points);
} else {
throw new TypeError('Invalid `points` attribute in <polygon>.');
}
return polygon;
}
export function createPolyline(points, { id, fill, stroke, pathLength, animation, ...rest } = {}) {
const polyline = createSVGElement('polyline', { id, fill, stroke, pathLength, animation, ...rest });
if (Array.isArray(points) && points.length !== 0) {
polyline.setAttribute('points', points.map(([x, y]) => `${x},${y}`).join(' '));
} else if (typeof points === 'string') {
polyline.setAttribute('points', points);
} else {
throw new TypeError('Invalid `points` attribute in <polygon>.');
}
return polyline;
}
export function createLine([[x1, y1], [x2, y2]], { id, fill, stroke, pathLength, animation, ...rest } = {}) {
if (! [x1, x2, y1, y2].every(n => typeof n === 'number' && ! Number.isNaN(n))) {
throw new TypeError('Invalid coordinates in <line>.');
} else {
return createSVGElement('line', { id, fill, stroke, x1, y1, x2, y2, pathLength, animation, ...rest });
}
}
export { TYPE };