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
|
import { LineString, Point, Polygon } from "geom";
/**
* @typedef {Object} SvgOptions
* @property {Record<string, string|number>} [attributes] - SVG attributes
* @property {Record<string, string>} [styles] - CSS styles
* @property {string} [id] - Element ID
* @property {string[]} [classes] - CSS classes
* @property {SVGElement[]} [children] - Child elements
*/
export class Svg {
/**
* Create an SVG element
* @param {SvgOptions} [options={}]
* @returns {SVGSVGElement}
*/
static svg(options = {}) {
const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
const defaultAttributes = {
preserveAspectRatio: "xMidYMid slice",
...options.attributes,
};
Svg.#applyOptions(svg, { ...options, attributes: defaultAttributes });
return svg;
}
/**
* Create an SVG title element for tooltips
* @param {string} text - The tooltip text
* @returns {SVGTitleElement}
*/
static title(text) {
const title = document.createElementNS("http://www.w3.org/2000/svg", "title");
title.textContent = text;
return title;
}
/**
* Create a group element
* @param {SvgOptions} [options={}]
* @returns {SVGGElement}
*/
static g(options = {}) {
const g = document.createElementNS("http://www.w3.org/2000/svg", "g");
Svg.#applyOptions(g, options);
return g;
}
/**
* Create a polygon from Polygon geometry
* @param {Polygon} polygon
* @param {SvgOptions} [options={}]
* @returns {SVGPolygonElement}
*/
static polygon(polygon, options = {}) {
const element = document.createElementNS("http://www.w3.org/2000/svg", "polygon");
// Convert polygon rings to SVG points string
const exteriorRing = polygon.getExterior();
const points = exteriorRing.map(([x, y]) => `${x},${y}`).join(" ");
const defaultAttributes = {
fill: "rgba(100,150,255,0.2)",
points,
stroke: "#555",
"stroke-width": 0.001,
...options.attributes,
};
Svg.#applyOptions(element, { ...options, attributes: defaultAttributes });
return element;
}
/**
* Create a path from LineString geometry
* @param {LineString} lineString
* @param {SvgOptions} [options={}]
* @returns {SVGPathElement}
*/
static path(lineString, options = {}) {
const element = document.createElementNS("http://www.w3.org/2000/svg", "path");
// Convert LineString coordinates to SVG path data
const coords = lineString.coordinates;
let pathData = "";
if (coords.length > 0) {
const [startLng, startLat] = coords[0];
pathData = `M ${startLng},${startLat}`;
for (let i = 1; i < coords.length; i++) {
const [lng, lat] = coords[i];
pathData += ` L ${lng},${lat}`;
}
}
const defaultAttributes = {
d: pathData,
fill: "none",
stroke: "#000",
"stroke-linecap": "round",
"stroke-linejoin": "round",
"stroke-width": 0.001,
...options.attributes,
};
Svg.#applyOptions(element, { ...options, attributes: defaultAttributes });
return element;
}
/**
* Create a circle from Point geometry
* @param {Point} point
* @param {SvgOptions} [options={}]
* @returns {SVGCircleElement}
*/
static circle(point, options = {}) {
const element = document.createElementNS("http://www.w3.org/2000/svg", "circle");
const defaultAttributes = {
cx: point.lng,
cy: point.lat,
fill: "#4caf50",
r: 0.002,
stroke: "#333",
"stroke-width": 0.001,
...options.attributes,
};
Svg.#applyOptions(element, { ...options, attributes: defaultAttributes });
return element;
}
/**
* Create a rectangle
* @param {Point} point
* @param {number} width - Rectangle width
* @param {number} height - Rectangle height
* @param {SvgOptions} [options={}]
* @returns {SVGRectElement}
*/
static rect(point, width, height, options = {}) {
const element = document.createElementNS("http://www.w3.org/2000/svg", "rect");
const defaultAttributes = {
height,
width,
x: point.lng,
y: point.lat,
...options.attributes,
};
Svg.#applyOptions(element, { ...options, attributes: defaultAttributes });
return element;
}
/**
* Create a text element
* @param {Point} point - point
* @param {string} text - Text content
* @param {SvgOptions} [options={}]
* @returns {SVGTextElement}
*/
static text(point, text, options = {}) {
const element = document.createElementNS("http://www.w3.org/2000/svg", "text");
const defaultAttributes = {
x: point.lng,
y: point.lat,
...options.attributes,
};
const defaultStyles = {
dominantBaseline: "middle",
fill: "#333",
fontSize: "0.005",
pointerEvents: "none",
textAnchor: "middle",
...options.styles,
};
element.textContent = text;
Svg.#applyOptions(element, {
...options,
attributes: defaultAttributes,
styles: defaultStyles,
});
return element;
}
/**
* Create a circle element with explicit coordinates
* @param {Point} point - Center X coordinate
* @param {number} r - Radius
* @param {SvgOptions} [options={}]
* @returns {SVGCircleElement}
*/
static circleXY(point, r, options = {}) {
const element = document.createElementNS("http://www.w3.org/2000/svg", "circle");
const defaultAttributes = {
cx: point.lng,
cy: point.lat,
fill: "#4caf50",
r,
stroke: "#333",
"stroke-width": 0.001,
...options.attributes,
};
Svg.#applyOptions(element, { ...options, attributes: defaultAttributes });
return element;
}
/**
* Create a path from raw path data
* @param {string} pathData - SVG path data string
* @param {SvgOptions} [options={}]
* @returns {SVGPathElement}
*/
static pathData(pathData, options = {}) {
const element = document.createElementNS("http://www.w3.org/2000/svg", "path");
const defaultAttributes = {
d: pathData,
fill: "none",
stroke: "#000",
"stroke-linecap": "round",
"stroke-linejoin": "round",
"stroke-width": 0.001,
...options.attributes,
};
Svg.#applyOptions(element, { ...options, attributes: defaultAttributes });
return element;
}
/**
* Create a polygon from points array
* @param {Array<[number, number]>} points - Array of [x,y] points
* @param {SvgOptions} [options={}]
* @returns {SVGPolygonElement}
*/
static polygonPoints(points, options = {}) {
const element = document.createElementNS("http://www.w3.org/2000/svg", "polygon");
const pointsString = points.map(([x, y]) => `${x},${y}`).join(" ");
const defaultAttributes = {
fill: "rgba(100,150,255,0.2)",
points: pointsString,
stroke: "#555",
"stroke-width": 0.001,
...options.attributes,
};
Svg.#applyOptions(element, { ...options, attributes: defaultAttributes });
return element;
}
/**
* Apply options to SVG element
* @param {SVGElement} element
* @param {SvgOptions} options
*/
static #applyOptions(element, options = {}) {
const { attributes = {}, styles = {}, id = "", classes = [], children = [] } = options;
// Set attributes
for (const [key, value] of Object.entries(attributes)) {
if (value != null) {
element.setAttribute(key, value.toString());
}
}
// Set styles
for (const [property, value] of Object.entries(styles)) {
if (value != null) {
element.style[property] = value;
}
}
// Set ID and classes
if (id) element.id = id;
if (classes.length > 0) {
element.classList.add(...classes.filter(Boolean));
}
for (const child of children) {
if (child) {
element.appendChild(child);
}
}
}
/**
* Clear all children from an element
* @param {SVGElement} element
*/
static clear(element) {
while (element.firstChild) {
element.removeChild(element.firstChild);
}
}
}
|