@andamento/svg 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +21 -2
- package/dist/index.cjs +531 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +285 -0
- package/dist/index.d.ts +285 -0
- package/dist/index.js +483 -0
- package/dist/index.js.map +1 -0
- package/package.json +62 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Matt Putnam
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,22 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @andamento/svg
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Draws [`@andamento/core`](https://www.npmjs.com/package/@andamento/core) tiling layouts as
|
|
4
|
+
an SVG string or element, on a canvas, or as a PNG. Images, fills, rounded corners, raised
|
|
5
|
+
tiles and one picture across all the tiles are all supported.
|
|
6
|
+
|
|
7
|
+
**[Manual](https://mattputnam.github.io/andamento/) · [SVG, canvas and PNG](https://mattputnam.github.io/andamento/frameworks/svg-canvas) · [Examples](https://mattputnam.github.io/andamento/examples/) · [API](https://mattputnam.github.io/andamento/reference/)**
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @andamento/core @andamento/svg
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { createLayout, herringbone } from "@andamento/core";
|
|
15
|
+
import { renderToString, renderToPng } from "@andamento/svg";
|
|
16
|
+
|
|
17
|
+
const layout = createLayout({ region: { x: 0, y: 0, width: 240, height: 150 }, tiling: herringbone({ length: 60, width: 20 }), gap: 2 });
|
|
18
|
+
const svg = renderToString(layout, { fill: ["#e9c46a", "#f4a261", "#e76f51"], background: "#264653" });
|
|
19
|
+
const png = await renderToPng(layout, { images: photoUrls, width: 1200, relief: true });
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
MIT licensed.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,531 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
cellA11y: () => cellA11y,
|
|
24
|
+
cellPath: () => cellPath,
|
|
25
|
+
defaultBevel: () => defaultBevel,
|
|
26
|
+
defaultPalette: () => defaultPalette,
|
|
27
|
+
drawLayout: () => drawLayout,
|
|
28
|
+
imagesUsed: () => imagesUsed,
|
|
29
|
+
loadImages: () => loadImages,
|
|
30
|
+
neutralPalette: () => neutralPalette,
|
|
31
|
+
polygonToPath: () => polygonToPath,
|
|
32
|
+
rasterSize: () => rasterSize,
|
|
33
|
+
regionPath: () => regionPath,
|
|
34
|
+
reliefLayers: () => reliefLayers,
|
|
35
|
+
reliefPaths: () => reliefPaths,
|
|
36
|
+
renderToCanvas: () => renderToCanvas,
|
|
37
|
+
renderToElement: () => renderToElement,
|
|
38
|
+
renderToPng: () => renderToPng,
|
|
39
|
+
renderToString: () => renderToString,
|
|
40
|
+
resolveCellStyles: () => resolveCellStyles,
|
|
41
|
+
resolveFills: () => resolveFills,
|
|
42
|
+
serialize: () => serialize,
|
|
43
|
+
shadowOffset: () => shadowOffset,
|
|
44
|
+
toSvgTree: () => toSvgTree,
|
|
45
|
+
tracePolygon: () => tracePolygon,
|
|
46
|
+
viewPath: () => viewPath
|
|
47
|
+
});
|
|
48
|
+
module.exports = __toCommonJS(index_exports);
|
|
49
|
+
var import_core3 = require("@andamento/core");
|
|
50
|
+
|
|
51
|
+
// src/relief.ts
|
|
52
|
+
var import_core = require("@andamento/core");
|
|
53
|
+
function defaultBevel(layout) {
|
|
54
|
+
const n = layout.cells.length;
|
|
55
|
+
if (n === 0) return 0;
|
|
56
|
+
const mean = layout.cells.reduce((s, c) => s + c.area, 0) / n;
|
|
57
|
+
return 0.08 * Math.sqrt(mean);
|
|
58
|
+
}
|
|
59
|
+
var LEVELS = 8;
|
|
60
|
+
function reliefLayers(layout, cell, relief, bevel) {
|
|
61
|
+
const strength = relief.strength ?? 0.4;
|
|
62
|
+
const groups = /* @__PURE__ */ new Map();
|
|
63
|
+
for (const facet of (0, import_core.bevelFacets)(cell, layout, { width: bevel, light: relief.light })) {
|
|
64
|
+
const level = Math.round(facet.shade * LEVELS);
|
|
65
|
+
if (level === 0) continue;
|
|
66
|
+
const list = groups.get(level);
|
|
67
|
+
if (list) list.push(facet.polygon);
|
|
68
|
+
else groups.set(level, [facet.polygon]);
|
|
69
|
+
}
|
|
70
|
+
return [...groups.entries()].sort((a, b) => a[0] - b[0]).map(([level, polygons]) => ({
|
|
71
|
+
polygons,
|
|
72
|
+
fill: level > 0 ? "#fff" : "#000",
|
|
73
|
+
opacity: Number((Math.abs(level) / LEVELS * strength).toFixed(3))
|
|
74
|
+
}));
|
|
75
|
+
}
|
|
76
|
+
function reliefPaths(layout, cell, relief, bevel, precision = 3) {
|
|
77
|
+
const f = (n) => Number(n.toFixed(precision));
|
|
78
|
+
return reliefLayers(layout, cell, relief, bevel).map(({ polygons, fill, opacity }) => ({
|
|
79
|
+
d: polygons.map((poly) => "M" + poly.map((p) => `${f(p.x)} ${f(p.y)}`).join("L") + "Z").join(""),
|
|
80
|
+
fill,
|
|
81
|
+
opacity
|
|
82
|
+
}));
|
|
83
|
+
}
|
|
84
|
+
function shadowOffset(relief) {
|
|
85
|
+
const s = relief.shadow ?? 0;
|
|
86
|
+
if (!(s > 0)) return void 0;
|
|
87
|
+
const l = (0, import_core.lightVector)(relief.light);
|
|
88
|
+
return { dx: -l.x * s, dy: -l.y * s, blur: relief.shadowBlur ?? s, opacity: relief.shadowOpacity ?? 0.35 };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// src/canvas.ts
|
|
92
|
+
var import_core2 = require("@andamento/core");
|
|
93
|
+
function drawLayout(ctx, layout, options = {}) {
|
|
94
|
+
const canvas = ctx.canvas;
|
|
95
|
+
const width = options.width ?? canvas?.width ?? layout.bounds.width;
|
|
96
|
+
const height = options.height ?? canvas?.height ?? layout.bounds.height;
|
|
97
|
+
const b = layout.bounds;
|
|
98
|
+
const scale = Math.min(width / (b.width || 1), height / (b.height || 1)) || 1;
|
|
99
|
+
const ox = (width - b.width * scale) / 2 - b.x * scale;
|
|
100
|
+
const oy = (height - b.height * scale) / 2 - b.y * scale;
|
|
101
|
+
const toLayout = () => ctx.setTransform(scale, 0, 0, scale, ox, oy);
|
|
102
|
+
const styles = resolveCellStyles(layout, options);
|
|
103
|
+
const loaded = options.loaded ?? /* @__PURE__ */ new Map();
|
|
104
|
+
const radius = layout.cornerRadius;
|
|
105
|
+
const cellLoops = (c) => {
|
|
106
|
+
const s = (0, import_core2.cellShape)(c);
|
|
107
|
+
return [s.outline, ...s.holes];
|
|
108
|
+
};
|
|
109
|
+
ctx.save();
|
|
110
|
+
toLayout();
|
|
111
|
+
ctx.lineJoin = "round";
|
|
112
|
+
if (options.background) {
|
|
113
|
+
ctx.beginPath();
|
|
114
|
+
for (const p of [layout.region, ...layout.holes]) tracePolygon(ctx, p, 0);
|
|
115
|
+
ctx.fillStyle = options.background;
|
|
116
|
+
ctx.fill("evenodd");
|
|
117
|
+
}
|
|
118
|
+
if (layout.view) {
|
|
119
|
+
ctx.beginPath();
|
|
120
|
+
for (const v of layout.view) for (const p of [v.outer, ...v.holes]) tracePolygon(ctx, p, 0);
|
|
121
|
+
ctx.clip("evenodd");
|
|
122
|
+
}
|
|
123
|
+
const relief = options.relief === true ? {} : options.relief || void 0;
|
|
124
|
+
const cast = relief && shadowOffset(relief);
|
|
125
|
+
if (cast) {
|
|
126
|
+
ctx.save();
|
|
127
|
+
ctx.beginPath();
|
|
128
|
+
for (const p of [layout.region, ...layout.holes]) tracePolygon(ctx, p, 0);
|
|
129
|
+
ctx.clip("evenodd");
|
|
130
|
+
const away = (b.width + b.height) * 4;
|
|
131
|
+
ctx.setTransform(scale, 0, 0, scale, ox - away * scale, oy);
|
|
132
|
+
ctx.shadowColor = `rgba(0,0,0,${cast.opacity})`;
|
|
133
|
+
ctx.shadowBlur = cast.blur * scale * 2;
|
|
134
|
+
ctx.shadowOffsetX = (away + cast.dx) * scale;
|
|
135
|
+
ctx.shadowOffsetY = cast.dy * scale;
|
|
136
|
+
ctx.beginPath();
|
|
137
|
+
for (const c of layout.cells) for (const p of cellLoops(c)) tracePolygon(ctx, p, radius);
|
|
138
|
+
ctx.fillStyle = "#000";
|
|
139
|
+
ctx.fill("evenodd");
|
|
140
|
+
ctx.restore();
|
|
141
|
+
}
|
|
142
|
+
const backdrop = options.backdrop ? loaded.get(options.backdrop) : void 0;
|
|
143
|
+
if (backdrop) {
|
|
144
|
+
ctx.save();
|
|
145
|
+
ctx.beginPath();
|
|
146
|
+
for (const c of layout.cells) for (const p of cellLoops(c)) tracePolygon(ctx, p, radius);
|
|
147
|
+
ctx.clip("evenodd");
|
|
148
|
+
drawCover(ctx, backdrop, b);
|
|
149
|
+
ctx.restore();
|
|
150
|
+
}
|
|
151
|
+
const bevel = relief ? relief.bevel ?? defaultBevel(layout) : 0;
|
|
152
|
+
layout.cells.forEach((cell, i) => {
|
|
153
|
+
const style = styles[i];
|
|
154
|
+
ctx.beginPath();
|
|
155
|
+
for (const p of cellLoops(cell)) tracePolygon(ctx, p, radius);
|
|
156
|
+
if (!backdrop) {
|
|
157
|
+
ctx.fillStyle = style.fill;
|
|
158
|
+
ctx.fill("evenodd");
|
|
159
|
+
}
|
|
160
|
+
const img = style.image ? loaded.get(style.image) : void 0;
|
|
161
|
+
if (img) {
|
|
162
|
+
ctx.save();
|
|
163
|
+
ctx.clip("evenodd");
|
|
164
|
+
if (options.imageFit === "contain") {
|
|
165
|
+
const aspect = options.imageAspects && style.slot >= 0 ? options.imageAspects[style.slot % options.imageAspects.length] : void 0;
|
|
166
|
+
drawContain(ctx, img, (0, import_core2.safeArea)(cell, layout, { aspect, within: options.containWithin }));
|
|
167
|
+
} else {
|
|
168
|
+
const focus = style.slot >= 0 ? Array.isArray(options.images) ? options.imageFocus?.[style.slot % options.images.length] : void 0 : void 0;
|
|
169
|
+
if (focus && img.width && img.height) {
|
|
170
|
+
const r = (0, import_core2.coverRect)(cell, layout, { aspect: img.width / img.height, focus });
|
|
171
|
+
ctx.drawImage(img, 0, 0, img.width, img.height, r.x, r.y, r.width, r.height);
|
|
172
|
+
} else drawCover(ctx, img, (0, import_core2.cellShape)(cell).bbox);
|
|
173
|
+
}
|
|
174
|
+
ctx.restore();
|
|
175
|
+
}
|
|
176
|
+
if (relief && bevel > 0) {
|
|
177
|
+
for (const layer of reliefLayers(layout, cell, relief, bevel)) {
|
|
178
|
+
ctx.beginPath();
|
|
179
|
+
for (const poly of layer.polygons) tracePolygon(ctx, poly, 0);
|
|
180
|
+
ctx.globalAlpha = layer.opacity;
|
|
181
|
+
ctx.fillStyle = layer.fill;
|
|
182
|
+
ctx.fill();
|
|
183
|
+
ctx.globalAlpha = 1;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
if (options.stroke) {
|
|
187
|
+
ctx.beginPath();
|
|
188
|
+
for (const p of cellLoops(cell)) tracePolygon(ctx, p, radius);
|
|
189
|
+
ctx.strokeStyle = options.stroke;
|
|
190
|
+
ctx.lineWidth = options.strokeWidth ?? 1;
|
|
191
|
+
ctx.stroke();
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
ctx.restore();
|
|
195
|
+
}
|
|
196
|
+
function tracePolygon(ctx, poly, radius) {
|
|
197
|
+
if (poly.length < 3) return;
|
|
198
|
+
if (radius <= 0) {
|
|
199
|
+
ctx.moveTo(poly[0].x, poly[0].y);
|
|
200
|
+
for (let i = 1; i < poly.length; i++) ctx.lineTo(poly[i].x, poly[i].y);
|
|
201
|
+
ctx.closePath();
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
(0, import_core2.roundCorners)(poly, radius).forEach((c, i) => {
|
|
205
|
+
if (i === 0) ctx.moveTo(c.start.x, c.start.y);
|
|
206
|
+
else ctx.lineTo(c.start.x, c.start.y);
|
|
207
|
+
if (c.radius > 0) {
|
|
208
|
+
const a0 = Math.atan2(c.start.y - c.center.y, c.start.x - c.center.x);
|
|
209
|
+
ctx.arc(c.center.x, c.center.y, c.radius, a0, a0 + c.sweep, c.sweep < 0);
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
ctx.closePath();
|
|
213
|
+
}
|
|
214
|
+
function drawCover(ctx, img, box) {
|
|
215
|
+
if (!img.width || !img.height || !box.width || !box.height) return;
|
|
216
|
+
const s = Math.max(box.width / img.width, box.height / img.height);
|
|
217
|
+
const sw = box.width / s, sh = box.height / s;
|
|
218
|
+
ctx.drawImage(img, (img.width - sw) / 2, (img.height - sh) / 2, sw, sh, box.x, box.y, box.width, box.height);
|
|
219
|
+
}
|
|
220
|
+
function drawContain(ctx, img, box) {
|
|
221
|
+
if (!img.width || !img.height || !box.width || !box.height) return;
|
|
222
|
+
const s = Math.min(box.width / img.width, box.height / img.height);
|
|
223
|
+
const w = img.width * s, h = img.height * s;
|
|
224
|
+
ctx.drawImage(img, 0, 0, img.width, img.height, box.x + (box.width - w) / 2, box.y + (box.height - h) / 2, w, h);
|
|
225
|
+
}
|
|
226
|
+
function imagesUsed(layout, options = {}) {
|
|
227
|
+
const urls = /* @__PURE__ */ new Set();
|
|
228
|
+
for (const s of resolveCellStyles(layout, options)) if (s.image) urls.add(s.image);
|
|
229
|
+
if (options.backdrop) urls.add(options.backdrop);
|
|
230
|
+
return [...urls];
|
|
231
|
+
}
|
|
232
|
+
async function loadImages(urls, load = browserImage) {
|
|
233
|
+
const out = /* @__PURE__ */ new Map();
|
|
234
|
+
await Promise.all(
|
|
235
|
+
urls.map(async (url) => {
|
|
236
|
+
try {
|
|
237
|
+
out.set(url, await load(url));
|
|
238
|
+
} catch {
|
|
239
|
+
}
|
|
240
|
+
})
|
|
241
|
+
);
|
|
242
|
+
return out;
|
|
243
|
+
}
|
|
244
|
+
function browserImage(url) {
|
|
245
|
+
const Img = globalThis.Image;
|
|
246
|
+
if (!Img) return Promise.reject(new Error("No Image constructor: pass a load function"));
|
|
247
|
+
return new Promise((resolve, reject) => {
|
|
248
|
+
const img = new Img();
|
|
249
|
+
img.crossOrigin = "anonymous";
|
|
250
|
+
img.onload = () => resolve(img);
|
|
251
|
+
img.onerror = () => reject(new Error(`Could not load ${url}`));
|
|
252
|
+
img.src = url;
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
async function renderToCanvas(layout, canvas, options = {}) {
|
|
256
|
+
const { w, h } = rasterSize(layout, options);
|
|
257
|
+
canvas.width = w;
|
|
258
|
+
canvas.height = h;
|
|
259
|
+
const ctx = canvas.getContext("2d");
|
|
260
|
+
if (!ctx) throw new Error("Could not get a 2D context from the canvas");
|
|
261
|
+
const loaded = options.loaded ?? await loadImages(imagesUsed(layout, options), options.loadImage);
|
|
262
|
+
ctx.clearRect(0, 0, w, h);
|
|
263
|
+
drawLayout(ctx, layout, { ...options, loaded, width: w, height: h });
|
|
264
|
+
return canvas;
|
|
265
|
+
}
|
|
266
|
+
function rasterSize(layout, options = {}) {
|
|
267
|
+
const b = layout.bounds;
|
|
268
|
+
const cssWidth = options.width ?? b.width;
|
|
269
|
+
const ratio = options.pixelRatio ?? 1;
|
|
270
|
+
return { w: Math.max(1, Math.round(cssWidth * ratio)), h: Math.max(1, Math.round(cssWidth * b.height / (b.width || 1) * ratio)) };
|
|
271
|
+
}
|
|
272
|
+
async function renderToPng(layout, options = {}) {
|
|
273
|
+
const { w, h } = rasterSize(layout, options);
|
|
274
|
+
const canvas = options.createCanvas ? options.createCanvas(w, h) : makeCanvas(w, h);
|
|
275
|
+
await renderToCanvas(layout, canvas, options);
|
|
276
|
+
const type = options.type ?? "image/png";
|
|
277
|
+
const c = canvas;
|
|
278
|
+
if (c.convertToBlob) return c.convertToBlob({ type, quality: options.quality });
|
|
279
|
+
if (c.toBlob) {
|
|
280
|
+
return new Promise(
|
|
281
|
+
(resolve, reject) => c.toBlob((blob) => blob ? resolve(blob) : reject(new Error("The canvas could not be exported")), type, options.quality)
|
|
282
|
+
);
|
|
283
|
+
}
|
|
284
|
+
throw new Error("This canvas can't export images: use drawLayout and your canvas's own export");
|
|
285
|
+
}
|
|
286
|
+
function makeCanvas(w, h) {
|
|
287
|
+
const g = globalThis;
|
|
288
|
+
if (g.OffscreenCanvas) return new g.OffscreenCanvas(w, h);
|
|
289
|
+
if (g.document) {
|
|
290
|
+
const c = g.document.createElement("canvas");
|
|
291
|
+
c.width = w;
|
|
292
|
+
c.height = h;
|
|
293
|
+
return c;
|
|
294
|
+
}
|
|
295
|
+
throw new Error("No canvas available: pass createCanvas, or use drawLayout with your own");
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
// src/index.ts
|
|
299
|
+
var defaultPalette = ["#e9d8a6", "#94d2bd", "#ee9b00", "#0a9396", "#ca6702", "#bb3e03", "#ae2012", "#005f73"];
|
|
300
|
+
var neutralPalette = ["#e7e5e4", "#d6d3d1", "#f5f5f4", "#dedad6", "#ecebe9"];
|
|
301
|
+
var formatter = (precision) => (n) => {
|
|
302
|
+
const s = n.toFixed(precision);
|
|
303
|
+
const t = s.includes(".") ? s.replace(/\.?0+$/, "") : s;
|
|
304
|
+
return t === "-0" ? "0" : t;
|
|
305
|
+
};
|
|
306
|
+
function polygonToPath(p, precision = 3, radius = 0) {
|
|
307
|
+
if (p.length === 0) return "";
|
|
308
|
+
const f = formatter(precision);
|
|
309
|
+
if (radius <= 0) return "M" + p.map((v) => `${f(v.x)} ${f(v.y)}`).join("L") + "Z";
|
|
310
|
+
const parts = [];
|
|
311
|
+
for (const c of (0, import_core3.roundCorners)(p, radius)) {
|
|
312
|
+
parts.push(`${parts.length ? "L" : "M"}${f(c.start.x)} ${f(c.start.y)}`);
|
|
313
|
+
if (c.radius > 0) {
|
|
314
|
+
const r = f(c.radius);
|
|
315
|
+
parts.push(`A${r} ${r} 0 0 ${c.sweep > 0 ? 1 : 0} ${f(c.end.x)} ${f(c.end.y)}`);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
return parts.join("") + "Z";
|
|
319
|
+
}
|
|
320
|
+
function cellPath(layout, cell, precision = 3) {
|
|
321
|
+
const s = (0, import_core3.cellShape)(cell);
|
|
322
|
+
return [s.outline, ...s.holes].map((p) => polygonToPath(p, precision, layout.cornerRadius)).join("");
|
|
323
|
+
}
|
|
324
|
+
function viewPath(layout, precision = 3) {
|
|
325
|
+
if (!layout.view) return regionPath(layout, precision);
|
|
326
|
+
return layout.view.flatMap((v) => [v.outer, ...v.holes]).map((p) => polygonToPath(p, precision)).join("");
|
|
327
|
+
}
|
|
328
|
+
function regionPath(layout, precision = 3) {
|
|
329
|
+
return [layout.region, ...layout.holes].map((p) => polygonToPath(p, precision)).join("");
|
|
330
|
+
}
|
|
331
|
+
function resolveFills(layout, fill) {
|
|
332
|
+
if (typeof fill === "string") return layout.cells.map(() => fill);
|
|
333
|
+
const f = typeof fill === "function" ? fill : (0, import_core3.paletteFill)(fill.length ? fill : defaultPalette);
|
|
334
|
+
return layout.cells.map((c) => f(c, layout));
|
|
335
|
+
}
|
|
336
|
+
function resolveCellStyles(layout, opts = {}) {
|
|
337
|
+
const fills = resolveFills(layout, opts.fill ?? defaultPalette);
|
|
338
|
+
const images = opts.images;
|
|
339
|
+
let slots = layout.cells.map(() => -1);
|
|
340
|
+
if (Array.isArray(images) && images.length > 0) {
|
|
341
|
+
const count = opts.repeatImages ? Infinity : images.length;
|
|
342
|
+
const content = { cells: opts.contentCells, placement: opts.placement, choose: opts.choose };
|
|
343
|
+
slots = opts.imageAspects && !opts.repeatImages ? (0, import_core3.matchSlots)(layout, images.map((_, i) => opts.imageAspects[i] ?? 1), content) : (0, import_core3.contentSlots)(layout, count, content);
|
|
344
|
+
}
|
|
345
|
+
return layout.cells.map((c, i) => {
|
|
346
|
+
const slot = slots[i];
|
|
347
|
+
let image;
|
|
348
|
+
if (typeof images === "function") image = images(c);
|
|
349
|
+
else if (images && slot >= 0) image = images[slot % images.length];
|
|
350
|
+
return { fill: fills[i], image, slot };
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
var el = (tag, attrs = {}, children = []) => ({
|
|
354
|
+
tag,
|
|
355
|
+
attrs,
|
|
356
|
+
children
|
|
357
|
+
});
|
|
358
|
+
function toSvgTree(layout, opts = {}) {
|
|
359
|
+
const precision = opts.precision ?? 3;
|
|
360
|
+
const prefix = opts.idPrefix ?? "andamento";
|
|
361
|
+
const { x, y, width, height } = layout.bounds;
|
|
362
|
+
const styles = resolveCellStyles(layout, opts);
|
|
363
|
+
const r = (n) => Number(n.toFixed(precision));
|
|
364
|
+
const children = [];
|
|
365
|
+
if (opts.title) children.push(el("title", {}, [opts.title]));
|
|
366
|
+
if (opts.background) {
|
|
367
|
+
children.push(el("path", { d: regionPath(layout, precision), fill: opts.background, "fill-rule": "evenodd", class: "andamento-region" }));
|
|
368
|
+
}
|
|
369
|
+
const start = children.length;
|
|
370
|
+
if (opts.backdrop) {
|
|
371
|
+
const clipId = `${prefix}-backdrop`;
|
|
372
|
+
const shapes = layout.cells.map((cell) => el("path", { d: cellPath(layout, cell, precision), "clip-rule": "evenodd" }));
|
|
373
|
+
children.push(el("clipPath", { id: clipId }, shapes));
|
|
374
|
+
children.push(
|
|
375
|
+
el("image", {
|
|
376
|
+
class: "andamento-backdrop",
|
|
377
|
+
href: opts.backdrop,
|
|
378
|
+
x: r(x),
|
|
379
|
+
y: r(y),
|
|
380
|
+
width: r(width),
|
|
381
|
+
height: r(height),
|
|
382
|
+
preserveAspectRatio: "xMidYMid slice",
|
|
383
|
+
"clip-path": `url(#${clipId})`
|
|
384
|
+
})
|
|
385
|
+
);
|
|
386
|
+
}
|
|
387
|
+
const relief = opts.relief === true ? {} : opts.relief || void 0;
|
|
388
|
+
const bevel = relief ? relief.bevel ?? defaultBevel(layout) : 0;
|
|
389
|
+
const cast = relief && shadowOffset(relief);
|
|
390
|
+
if (cast) {
|
|
391
|
+
const regionId = `${prefix}-region`;
|
|
392
|
+
const blurId = `${prefix}-shadow`;
|
|
393
|
+
children.push(el("clipPath", { id: regionId }, [el("path", { d: regionPath(layout, precision), "clip-rule": "evenodd" })]));
|
|
394
|
+
children.push(el("filter", { id: blurId, x: "-10%", y: "-10%", width: "120%", height: "120%" }, [el("feGaussianBlur", { stdDeviation: r(cast.blur) })]));
|
|
395
|
+
children.push(
|
|
396
|
+
el("g", { class: "andamento-shadow", "clip-path": `url(#${regionId})` }, [
|
|
397
|
+
el("path", {
|
|
398
|
+
d: layout.cells.map((c) => cellPath(layout, c, precision)).join(""),
|
|
399
|
+
"fill-rule": "evenodd",
|
|
400
|
+
transform: `translate(${r(cast.dx)} ${r(cast.dy)})`,
|
|
401
|
+
fill: "#000",
|
|
402
|
+
"fill-opacity": cast.opacity,
|
|
403
|
+
filter: `url(#${blurId})`
|
|
404
|
+
})
|
|
405
|
+
])
|
|
406
|
+
);
|
|
407
|
+
}
|
|
408
|
+
layout.cells.forEach((cell, i) => {
|
|
409
|
+
const d = cellPath(layout, cell, precision);
|
|
410
|
+
const style = styles[i];
|
|
411
|
+
const holed = !!cell.holes?.length;
|
|
412
|
+
const parts = opts.backdrop ? [] : [el("path", { d, fill: style.fill, "fill-rule": holed ? "evenodd" : void 0 })];
|
|
413
|
+
if (style.image) {
|
|
414
|
+
const clipId = `${prefix}-clip-${cell.index}`;
|
|
415
|
+
parts.unshift(el("clipPath", { id: clipId }, [el("path", { d, "clip-rule": holed ? "evenodd" : void 0 })]));
|
|
416
|
+
const contain = opts.imageFit === "contain";
|
|
417
|
+
const aspect = contain && opts.imageAspects && style.slot >= 0 ? opts.imageAspects[style.slot % opts.imageAspects.length] : void 0;
|
|
418
|
+
const focus = !contain && style.slot >= 0 ? Array.isArray(opts.images) ? opts.imageFocus?.[style.slot % opts.images.length] : void 0 : void 0;
|
|
419
|
+
const shape = focus ? opts.imageAspects?.[style.slot % opts.imageAspects.length] ?? focus.aspect : void 0;
|
|
420
|
+
const box = contain ? (0, import_core3.safeArea)(cell, layout, { aspect, within: opts.containWithin }) : focus && shape ? (0, import_core3.coverRect)(cell, layout, { aspect: shape, focus }) : (0, import_core3.cellShape)(cell).bbox;
|
|
421
|
+
parts.push(
|
|
422
|
+
el("image", {
|
|
423
|
+
href: style.image,
|
|
424
|
+
x: r(box.x),
|
|
425
|
+
y: r(box.y),
|
|
426
|
+
width: r(box.width),
|
|
427
|
+
height: r(box.height),
|
|
428
|
+
preserveAspectRatio: contain ? "xMidYMid meet" : "xMidYMid slice",
|
|
429
|
+
"clip-path": `url(#${clipId})`
|
|
430
|
+
})
|
|
431
|
+
);
|
|
432
|
+
}
|
|
433
|
+
if (relief && bevel > 0) {
|
|
434
|
+
for (const p of reliefPaths(layout, cell, relief, bevel, precision)) {
|
|
435
|
+
parts.push(el("path", { class: "andamento-bevel", d: p.d, fill: p.fill, "fill-opacity": p.opacity }));
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
if (opts.stroke) {
|
|
439
|
+
parts.push(el("path", { d, fill: "none", stroke: opts.stroke, "stroke-width": opts.strokeWidth ?? 1 }));
|
|
440
|
+
}
|
|
441
|
+
children.push(
|
|
442
|
+
el(
|
|
443
|
+
"g",
|
|
444
|
+
{
|
|
445
|
+
class: "andamento-cell",
|
|
446
|
+
"data-cell-id": cell.id,
|
|
447
|
+
"data-index": cell.index,
|
|
448
|
+
"data-empty": style.image ? void 0 : "",
|
|
449
|
+
"data-boundary": cell.boundary ? "" : void 0,
|
|
450
|
+
...cellA11y(style, opts.imageAlts)
|
|
451
|
+
},
|
|
452
|
+
parts
|
|
453
|
+
)
|
|
454
|
+
);
|
|
455
|
+
});
|
|
456
|
+
if (layout.view) {
|
|
457
|
+
const viewId = `${prefix}-view`;
|
|
458
|
+
const body = children.splice(start);
|
|
459
|
+
children.push(el("clipPath", { id: viewId }, [el("path", { d: viewPath(layout, precision), "clip-rule": "evenodd" })]));
|
|
460
|
+
children.push(el("g", { class: "andamento-view", "clip-path": `url(#${viewId})` }, body));
|
|
461
|
+
}
|
|
462
|
+
return el(
|
|
463
|
+
"svg",
|
|
464
|
+
{
|
|
465
|
+
xmlns: "http://www.w3.org/2000/svg",
|
|
466
|
+
viewBox: `${r(x)} ${r(y)} ${r(width)} ${r(height)}`,
|
|
467
|
+
width: opts.width,
|
|
468
|
+
height: opts.height,
|
|
469
|
+
role: opts.imageAlts?.length ? "group" : opts.title ? "img" : void 0
|
|
470
|
+
},
|
|
471
|
+
children
|
|
472
|
+
);
|
|
473
|
+
}
|
|
474
|
+
function cellA11y(style, alts) {
|
|
475
|
+
if (!alts?.length) return {};
|
|
476
|
+
const alt = style.image && style.slot >= 0 ? alts[style.slot % alts.length] : void 0;
|
|
477
|
+
return alt ? { role: "img", "aria-label": alt } : { "aria-hidden": "true" };
|
|
478
|
+
}
|
|
479
|
+
var escapeText = (s) => s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
480
|
+
var escapeAttr = (s) => escapeText(s).replace(/"/g, """);
|
|
481
|
+
function serialize(node) {
|
|
482
|
+
if (typeof node === "string") return escapeText(node);
|
|
483
|
+
const attrs = Object.entries(node.attrs).filter(([, v]) => v !== void 0).map(([k, v]) => ` ${k}="${escapeAttr(String(v))}"`).join("");
|
|
484
|
+
if (node.children.length === 0) return `<${node.tag}${attrs}/>`;
|
|
485
|
+
return `<${node.tag}${attrs}>${node.children.map(serialize).join("")}</${node.tag}>`;
|
|
486
|
+
}
|
|
487
|
+
function renderToString(layout, opts = {}) {
|
|
488
|
+
return serialize(toSvgTree(layout, opts));
|
|
489
|
+
}
|
|
490
|
+
var SVG_NS = "http://www.w3.org/2000/svg";
|
|
491
|
+
function build(node, doc) {
|
|
492
|
+
if (typeof node === "string") return doc.createTextNode(node);
|
|
493
|
+
const elem = doc.createElementNS(SVG_NS, node.tag);
|
|
494
|
+
for (const [k, v] of Object.entries(node.attrs)) {
|
|
495
|
+
if (v !== void 0 && k !== "xmlns") elem.setAttribute(k, String(v));
|
|
496
|
+
}
|
|
497
|
+
for (const child of node.children) elem.appendChild(build(child, doc));
|
|
498
|
+
return elem;
|
|
499
|
+
}
|
|
500
|
+
function renderToElement(layout, opts = {}, doc = globalThis.document) {
|
|
501
|
+
if (!doc) throw new Error("renderToElement needs a DOM Document; pass one explicitly outside the browser");
|
|
502
|
+
return build(toSvgTree(layout, opts), doc);
|
|
503
|
+
}
|
|
504
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
505
|
+
0 && (module.exports = {
|
|
506
|
+
cellA11y,
|
|
507
|
+
cellPath,
|
|
508
|
+
defaultBevel,
|
|
509
|
+
defaultPalette,
|
|
510
|
+
drawLayout,
|
|
511
|
+
imagesUsed,
|
|
512
|
+
loadImages,
|
|
513
|
+
neutralPalette,
|
|
514
|
+
polygonToPath,
|
|
515
|
+
rasterSize,
|
|
516
|
+
regionPath,
|
|
517
|
+
reliefLayers,
|
|
518
|
+
reliefPaths,
|
|
519
|
+
renderToCanvas,
|
|
520
|
+
renderToElement,
|
|
521
|
+
renderToPng,
|
|
522
|
+
renderToString,
|
|
523
|
+
resolveCellStyles,
|
|
524
|
+
resolveFills,
|
|
525
|
+
serialize,
|
|
526
|
+
shadowOffset,
|
|
527
|
+
toSvgTree,
|
|
528
|
+
tracePolygon,
|
|
529
|
+
viewPath
|
|
530
|
+
});
|
|
531
|
+
//# sourceMappingURL=index.cjs.map
|