@logomarks/generate 0.0.0-stage → 0.2.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/CHANGELOG.md +56 -0
- package/LICENSE +21 -0
- package/README.md +46 -2
- package/package.json +29 -4
- package/src/controls.js +41 -0
- package/src/draw.js +60 -0
- package/src/finish.js +172 -0
- package/src/generate.js +274 -0
- package/src/guard.js +66 -0
- package/src/index.d.ts +111 -0
- package/src/index.js +10 -0
- package/src/presets.js +37 -0
- package/src/random.js +23 -0
- package/src/raster.js +52 -0
- package/src/recipes/arcs.js +83 -0
- package/src/recipes/arcstep.js +45 -0
- package/src/recipes/bands.js +90 -0
- package/src/recipes/bars.js +106 -0
- package/src/recipes/bite.js +65 -0
- package/src/recipes/blades.js +38 -0
- package/src/recipes/bloom.js +53 -0
- package/src/recipes/burst.js +44 -0
- package/src/recipes/capsules.js +36 -0
- package/src/recipes/chevrons.js +45 -0
- package/src/recipes/cut.js +80 -0
- package/src/recipes/flare.js +39 -0
- package/src/recipes/halfsq.js +53 -0
- package/src/recipes/hex.js +58 -0
- package/src/recipes/horizon.js +51 -0
- package/src/recipes/index.js +95 -0
- package/src/recipes/invert.js +40 -0
- package/src/recipes/iso.js +35 -0
- package/src/recipes/leaves.js +45 -0
- package/src/recipes/letter.js +204 -0
- package/src/recipes/links.js +63 -0
- package/src/recipes/lobes.js +28 -0
- package/src/recipes/loops.js +28 -0
- package/src/recipes/pixelarc.js +51 -0
- package/src/recipes/rings.js +82 -0
- package/src/recipes/route.js +107 -0
- package/src/recipes/seam.js +30 -0
- package/src/recipes/shear.js +61 -0
- package/src/recipes/spiral.js +42 -0
- package/src/recipes/split.js +54 -0
- package/src/recipes/stack.js +71 -0
- package/src/recipes/steps.js +35 -0
- package/src/recipes/stripes.js +66 -0
- package/src/recipes/strips.js +39 -0
- package/src/recipes/tiles.js +60 -0
- package/src/recipes/trine.js +37 -0
- package/src/recipes/turns.js +41 -0
- package/src/recipes/twin.js +64 -0
- package/src/recipes/venn.js +54 -0
- package/src/recipes/wavecut.js +36 -0
- package/src/recipes/waves.js +40 -0
- package/src/shapes.js +54 -0
- package/src/tuning.js +67 -0
package/src/generate.js
ADDED
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Making marks: one from a recipe, its params and a seed; random params; a grid of variations with locked controls.
|
|
3
|
+
*/
|
|
4
|
+
import { robust, svg as toSvg, toPath, mapPts } from '@logomarks/core/geometry';
|
|
5
|
+
import { fitMark } from '@logomarks/core/fit';
|
|
6
|
+
import { RECIPES } from './recipes/index.js';
|
|
7
|
+
import { resolve, randomValue } from './controls.js';
|
|
8
|
+
import { rng, seedOf } from './random.js';
|
|
9
|
+
import { problems } from './guard.js';
|
|
10
|
+
import { fill, thinShareBits } from './raster.js';
|
|
11
|
+
|
|
12
|
+
robust();
|
|
13
|
+
|
|
14
|
+
/** A recipe by id; throws for an unknown one. */
|
|
15
|
+
export function recipe(id) {
|
|
16
|
+
const r = RECIPES[id];
|
|
17
|
+
if (!r) throw new Error(`Unknown recipe "${id}". Known: ${Object.keys(RECIPES).join(', ')}`);
|
|
18
|
+
return r;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** The recipe's shape for params, its problems, and the mark (see `generate()`). */
|
|
22
|
+
function draw(id, params) {
|
|
23
|
+
const r = recipe(id), p = resolve(r.controls, params);
|
|
24
|
+
// core's robust booleans retry a failed operation with a tiny jitter read from Math.random: seeded from the params
|
|
25
|
+
// while the mark is built, so the same params always draw the same bytes
|
|
26
|
+
const random = Math.random;
|
|
27
|
+
let shape;
|
|
28
|
+
Math.random = rng(`${id}:${JSON.stringify(p)}`);
|
|
29
|
+
try { shape = r.build(p); } finally { Math.random = random; }
|
|
30
|
+
const found = problems(shape, r.guard);
|
|
31
|
+
if (found.includes('nothing drawn')) return { shape: null, g: { recipe: id, params: p, svg: '', problems: found } };
|
|
32
|
+
const { svg } = fitMark(toSvg(shape));
|
|
33
|
+
return { shape, g: { recipe: id, params: p, svg, pieces: piecesOf(shape), problems: found } };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* A mark from a recipe and its params, fitted to the Logomarks standard (512 canvas, longest side 448, centred,
|
|
38
|
+
* currentColor). Missing params take their defaults; out-of-range ones are clamped.
|
|
39
|
+
*/
|
|
40
|
+
export function generate(id, params = {}) {
|
|
41
|
+
return draw(id, params).g;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** 1 inside [a, b], falling in a straight line to 0 at `lo` below and `hi` above. */
|
|
45
|
+
const band = (v, lo, a, b, hi) => (v < a ? Math.max(0, (v - lo) / (a - lo)) : v > b ? Math.max(0, (hi - v) / (hi - b)) : 1);
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* How good a drawn shape is as a mark, 0–1, measured on a small bitmap of it fitted to the house box: enough ink (about
|
|
49
|
+
* a third to three fifths of the box: neither spindly nor a slab), a box near square, few pieces, nothing thinner than
|
|
50
|
+
* the house gap, no gaps thinner than it either, the ink's weight near the middle, and an outline that isn't busy.
|
|
51
|
+
* Returns the score and its parts.
|
|
52
|
+
*/
|
|
53
|
+
export function qualityOf(shape) {
|
|
54
|
+
const polys = !shape || !shape.length ? [] : typeof shape[0][0][0] === 'number' ? [shape] : shape;
|
|
55
|
+
if (!polys.length) return { score: 0 };
|
|
56
|
+
let x0 = Infinity, y0 = Infinity, x1 = -Infinity, y1 = -Infinity;
|
|
57
|
+
for (const poly of polys) for (const [x, y] of poly[0]) { x0 = Math.min(x0, x); y0 = Math.min(y0, y); x1 = Math.max(x1, x); y1 = Math.max(y1, y); }
|
|
58
|
+
const k = 448 / Math.max(x1 - x0, y1 - y0, 1e-9), mx = (x0 + x1) / 2, my = (y0 + y1) / 2;
|
|
59
|
+
const N = 128, px = 512 / N, bits = fill(polys.map((poly) => mapPts(poly, ([x, y]) => [256 + (x - mx) * k, 256 + (y - my) * k])), N);
|
|
60
|
+
let ink = 0, sx = 0, sy = 0, edges = 0;
|
|
61
|
+
const back = new Uint8Array(N * N);
|
|
62
|
+
for (let y = 0; y < N; y++) for (let x = 0; x < N; x++) {
|
|
63
|
+
const v = bits[y * N + x];
|
|
64
|
+
back[y * N + x] = 1 - v;
|
|
65
|
+
if (!v) continue;
|
|
66
|
+
ink++; sx += x; sy += y;
|
|
67
|
+
if (!x || !bits[y * N + x - 1]) edges++;
|
|
68
|
+
if (!y || !bits[(y - 1) * N + x]) edges++;
|
|
69
|
+
}
|
|
70
|
+
if (!ink) return { score: 0 };
|
|
71
|
+
let ground = 0;
|
|
72
|
+
for (let i = 0; i < back.length; i++) ground += back[i];
|
|
73
|
+
const inkShare = (ink * px * px) / (448 * 448);
|
|
74
|
+
const parts = {
|
|
75
|
+
ink: band(inkShare, 0.14, 0.3, 0.6, 0.88),
|
|
76
|
+
square: band(Math.min(x1 - x0, y1 - y0) / Math.max(x1 - x0, y1 - y0), 0.3, 0.68, 1, 2),
|
|
77
|
+
pieces: band(polys.length, -1, 1, 6, 16),
|
|
78
|
+
thin: 1 - Math.min(1, thinShareBits(bits, N, 3) / 0.2),
|
|
79
|
+
gaps: 1 - Math.min(1, (thinShareBits(back, N, 2) * ground) / ink / 0.1),
|
|
80
|
+
balance: 1 - Math.min(1, Math.hypot(sx / ink - (N - 1) / 2, sy / ink - (N - 1) / 2) / (0.14 * N)),
|
|
81
|
+
calm: band((edges * 2 * px) / 448, 0, 0, 9, 22),
|
|
82
|
+
};
|
|
83
|
+
const score = 0.24 * parts.ink + 0.08 * parts.square + 0.14 * parts.pieces + 0.16 * parts.thin + 0.14 * parts.gaps + 0.1 * parts.balance + 0.14 * parts.calm;
|
|
84
|
+
return { score, ...parts };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* The best of a few shuffles: `tries` random params from `seed` on (see `randomParams()`), each drawn; the one that
|
|
89
|
+
* passes the guard with the highest quality (`qualityOf`) is returned with its `seed` and `quality`, or null if none
|
|
90
|
+
* passes. What a Shuffle button and a random grid should call, rather than taking the first draw that passes.
|
|
91
|
+
*/
|
|
92
|
+
export function shuffled(id, seed, { tries = 4, base = {}, locked = [], finish = false } = {}) {
|
|
93
|
+
let best = null;
|
|
94
|
+
for (let i = 0, s = seedOf(seed); i < tries; i++, s = (s + 1) >>> 0) {
|
|
95
|
+
try {
|
|
96
|
+
const { shape, g } = draw(id, randomParams(id, s, { base, locked, finish }));
|
|
97
|
+
if (g.problems.length || !g.svg) continue;
|
|
98
|
+
const quality = qualityOf(shape).score;
|
|
99
|
+
if (!best || quality > best.quality) best = { ...g, seed: s, quality };
|
|
100
|
+
} catch { /* the next seed */ }
|
|
101
|
+
}
|
|
102
|
+
return best;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* The mark's pieces as SVG path data, one string per piece (its outline and its holes, even-odd), in the fitted 512
|
|
107
|
+
* canvas's coordinates (longest side 448, centred, as `fitMark()` places it), in reading order: by rows of the canvas
|
|
108
|
+
* (top to bottom, rows a twelfth of the box apart), then left to right. For two colours and for motion.
|
|
109
|
+
*/
|
|
110
|
+
export function piecesOf(shape) {
|
|
111
|
+
const polys = !shape || !shape.length ? [] : typeof shape[0][0][0] === 'number' ? [shape] : shape;
|
|
112
|
+
if (!polys.length) return [];
|
|
113
|
+
let x0 = Infinity, y0 = Infinity, x1 = -Infinity, y1 = -Infinity;
|
|
114
|
+
for (const poly of polys) for (const [x, y] of poly[0]) { x0 = Math.min(x0, x); y0 = Math.min(y0, y); x1 = Math.max(x1, x); y1 = Math.max(y1, y); }
|
|
115
|
+
const k = 448 / Math.max(x1 - x0, y1 - y0), mx = (x0 + x1) / 2, my = (y0 + y1) / 2;
|
|
116
|
+
const fitted = polys.map((poly) => mapPts(poly, ([x, y]) => [256 + (x - mx) * k, 256 + (y - my) * k]));
|
|
117
|
+
const key = (poly) => { let t = Infinity, l = Infinity; for (const [x, y] of poly[0]) { t = Math.min(t, y); l = Math.min(l, x); } return [Math.round(t / (448 / 12)), l]; };
|
|
118
|
+
return fitted.map((poly) => ({ poly, k: key(poly) })).sort((a, b) => a.k[0] - b.k[0] || a.k[1] - b.k[1]).map(({ poly }) => toPath([poly]));
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** The signed area of path data made of straight segments (M, L, Z), every subpath summed (holes run the other way). */
|
|
122
|
+
function areaOfPath(d) {
|
|
123
|
+
let a = 0;
|
|
124
|
+
for (const sub of d.split(/(?=M)/)) {
|
|
125
|
+
const pts = [...sub.matchAll(/(-?[\d.]+)[ ,](-?[\d.]+)/g)].map((m) => [Number(m[1]), Number(m[2])]);
|
|
126
|
+
let s = 0;
|
|
127
|
+
for (let i = 0; i < pts.length; i++) { const [ax, ay] = pts[i], [bx, by] = pts[(i + 1) % pts.length]; s += ax * by - bx * ay; }
|
|
128
|
+
a += s / 2;
|
|
129
|
+
}
|
|
130
|
+
return Math.abs(a);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** The top of path data (its smallest y), for rows. */
|
|
134
|
+
function topOf(d) {
|
|
135
|
+
let t = Infinity;
|
|
136
|
+
for (const m of d.matchAll(/(-?[\d.]+)[ ,](-?[\d.]+)/g)) t = Math.min(t, Number(m[2]));
|
|
137
|
+
return t;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** A path's box from its coordinates: [x0, y0, x1, y1]. */
|
|
141
|
+
function boxOfPath(d) {
|
|
142
|
+
const n = (d.match(/-?\d*\.?\d+/g) || []).map(Number);
|
|
143
|
+
let x0 = Infinity, y0 = Infinity, x1 = -Infinity, y1 = -Infinity;
|
|
144
|
+
for (let i = 0; i + 1 < n.length; i += 2) { x0 = Math.min(x0, n[i]); x1 = Math.max(x1, n[i]); y0 = Math.min(y0, n[i + 1]); y1 = Math.max(y1, n[i + 1]); }
|
|
145
|
+
return [x0, y0, x1, y1];
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** The SVG colour patterns `svgOf` knows, in a picker's order. */
|
|
149
|
+
export const PATTERNS = ['alternate', 'first', 'last', 'rows', 'size', 'smallest', 'inner', 'outer', 'left', 'top', 'gradient'];
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* An SVG of the pieces in two colours: `colors` [base, accent]; `pattern` 'alternate' (every other piece in the accent),
|
|
153
|
+
* 'first' or 'last' (that piece), 'rows' (every other row of pieces), 'size' (the biggest piece), 'smallest' (the
|
|
154
|
+
* smallest: an accent dot), 'inner' (pieces clear of the mark's outer edge), 'outer' (pieces on it), 'left' or 'top'
|
|
155
|
+
* (pieces whose middle lies in that half), or 'gradient' (the whole mark in one gradient from base to accent, top left
|
|
156
|
+
* to bottom right). Each piece is its own even-odd path, in order.
|
|
157
|
+
*/
|
|
158
|
+
export function svgOf(pieces, { colors = ['currentColor', '#c8ff00'], pattern = 'alternate' } = {}) {
|
|
159
|
+
const [base, accent] = colors;
|
|
160
|
+
if (pattern === 'gradient') {
|
|
161
|
+
const paths = pieces.map((d) => `<path fill="url(#lm-grad)" fill-rule="evenodd" d="${d}"/>`).join('');
|
|
162
|
+
return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><defs><linearGradient id="lm-grad" x1="32" y1="32" x2="480" y2="480" gradientUnits="userSpaceOnUse"><stop offset="0" stop-color="${base}"/><stop offset="1" stop-color="${accent}"/></linearGradient></defs>${paths}</svg>`;
|
|
163
|
+
}
|
|
164
|
+
const boxes = pieces.map(boxOfPath);
|
|
165
|
+
const all = boxes.reduce((m, b) => [Math.min(m[0], b[0]), Math.min(m[1], b[1]), Math.max(m[2], b[2]), Math.max(m[3], b[3])], [Infinity, Infinity, -Infinity, -Infinity]);
|
|
166
|
+
const onEdge = (b) => b[0] <= all[0] + 2 || b[1] <= all[1] + 2 || b[2] >= all[2] - 2 || b[3] >= all[3] - 2;
|
|
167
|
+
let pick = (i) => i % 2 === 1;
|
|
168
|
+
if (pattern === 'first') pick = (i) => i === 0;
|
|
169
|
+
if (pattern === 'last') pick = (i) => i === pieces.length - 1;
|
|
170
|
+
if (pattern === 'size') { let big = 0, best = -1; pieces.forEach((d, i) => { const a = areaOfPath(d); if (a > best) { best = a; big = i; } }); pick = (i) => i === big; }
|
|
171
|
+
if (pattern === 'smallest') { let small = 0, best = Infinity; pieces.forEach((d, i) => { const a = areaOfPath(d); if (a < best) { best = a; small = i; } }); pick = (i) => i === small; }
|
|
172
|
+
if (pattern === 'inner') pick = (i) => !onEdge(boxes[i]);
|
|
173
|
+
if (pattern === 'outer') pick = (i) => onEdge(boxes[i]);
|
|
174
|
+
if (pattern === 'left') pick = (i) => (boxes[i][0] + boxes[i][2]) / 2 < (all[0] + all[2]) / 2;
|
|
175
|
+
if (pattern === 'top') pick = (i) => (boxes[i][1] + boxes[i][3]) / 2 < (all[1] + all[3]) / 2;
|
|
176
|
+
if (pattern === 'rows') {
|
|
177
|
+
const rows = [...new Set(pieces.map((d) => Math.round(topOf(d) / (448 / 12))))].sort((a, b) => a - b);
|
|
178
|
+
pick = (i) => rows.indexOf(Math.round(topOf(pieces[i]) / (448 / 12))) % 2 === 1;
|
|
179
|
+
}
|
|
180
|
+
const paths = pieces.map((d, i) => `<path fill="${pick(i) ? accent : base}" fill-rule="evenodd" d="${d}"/>`).join('');
|
|
181
|
+
return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">${paths}</svg>`;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Random params for a recipe from a seed, keeping the values of `locked` keys from `base`. The finish controls (turn,
|
|
186
|
+
* mirror, round, outline) stay at `base` (or their defaults) unless `finish` is true: a shuffle varies the construction.
|
|
187
|
+
*/
|
|
188
|
+
export function randomParams(id, seed, { base = {}, locked = [], finish = false } = {}) {
|
|
189
|
+
const r = recipe(id), rand = rng(seed);
|
|
190
|
+
const out = {};
|
|
191
|
+
for (const c of r.controls) {
|
|
192
|
+
const keep = (locked.includes(c.key) || (c.group === 'finish' && !finish)) && c.key in base;
|
|
193
|
+
const v = randomValue(c, rand); // drawn even when kept, so a seed draws the same construction whatever is locked
|
|
194
|
+
out[c.key] = keep ? base[c.key] : c.group === 'finish' && !finish ? c.value : v;
|
|
195
|
+
}
|
|
196
|
+
return resolve(r.controls, out);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* `count` marks from a recipe, each the best of three shuffles (`shuffled()`), from seeds `seed` on, the `locked`
|
|
201
|
+
* controls held at `base`; no two the same drawing. Slots where nothing passes the guard are skipped, up to `count` × 4.
|
|
202
|
+
*/
|
|
203
|
+
export function variations(id, { count = 24, seed = 1, base = {}, locked = [], finish = false, tries = 3 } = {}) {
|
|
204
|
+
const out = [], drawn = new Set();
|
|
205
|
+
for (let i = 0, s = seedOf(seed); out.length < count && i < count * 4; i++, s = (s + tries) >>> 0) {
|
|
206
|
+
const g = shuffled(id, s, { tries, base, locked, finish });
|
|
207
|
+
if (g && !drawn.has(g.svg)) { drawn.add(g.svg); out.push(g); }
|
|
208
|
+
}
|
|
209
|
+
return out;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Params close to `params`: every range nudged by up to `amount` of its span, a choice swapped for another now and then
|
|
214
|
+
* (about `amount` × 1.5 of the time), toggles, the finish and `locked` keys kept. The same seed, the same nudge.
|
|
215
|
+
*/
|
|
216
|
+
export function nearby(id, params, { seed = 1, amount = 0.15, locked = [] } = {}) {
|
|
217
|
+
const r = recipe(id), rand = rng(seed), base = resolve(r.controls, params), out = {};
|
|
218
|
+
for (const c of r.controls) {
|
|
219
|
+
const a = rand(), b = rand();
|
|
220
|
+
if (locked.includes(c.key) || c.group === 'finish' || c.type === 'toggle') { out[c.key] = base[c.key]; continue; }
|
|
221
|
+
if (c.type === 'range') {
|
|
222
|
+
const stepsIn = (c.max - c.min) / c.step;
|
|
223
|
+
// a few whole steps (rows, heights, counts): a nudge would round back, so step by one now and then;
|
|
224
|
+
// a layout number: now and then the next few layouts along
|
|
225
|
+
if (c.key === 'layout') out[c.key] = b < amount * 3 ? base[c.key] + 1 + Math.floor(a * 5) : base[c.key];
|
|
226
|
+
else if (stepsIn <= 8) out[c.key] = b < amount * 2.5 ? base[c.key] + (a < 0.5 ? -c.step : c.step) : base[c.key];
|
|
227
|
+
else out[c.key] = base[c.key] + (a * 2 - 1) * amount * (c.max - c.min);
|
|
228
|
+
}
|
|
229
|
+
else out[c.key] = b < amount * 1.5 ? c.options[Math.floor(a * c.options.length)] : base[c.key];
|
|
230
|
+
}
|
|
231
|
+
return resolve(r.controls, out);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** `count` marks close to `params` (see `nearby()`), each from its own seed, failed draws skipped; none the same as another. */
|
|
235
|
+
export function similar(id, params, { count = 12, seed = 1, amount = 0.15, locked = [] } = {}) {
|
|
236
|
+
const out = [], seen = new Set([JSON.stringify(resolve(recipe(id).controls, params))]), drawn = new Set();
|
|
237
|
+
try { drawn.add(generate(id, params).svg); } catch { /* the base may not draw */ }
|
|
238
|
+
for (let i = 0, s = seedOf(seed); out.length < count && i < count * 5; i++, s = (s + 1) >>> 0) {
|
|
239
|
+
const p = nearby(id, params, { seed: s, amount, locked }), key = JSON.stringify(p);
|
|
240
|
+
if (seen.has(key)) continue;
|
|
241
|
+
seen.add(key);
|
|
242
|
+
// some controls do nothing in some settings (a cut's size with no cut): skip a drawing already made
|
|
243
|
+
try { const g = generate(id, p); if (!g.problems.length && !drawn.has(g.svg)) { drawn.add(g.svg); out.push({ ...g, seed: s }); } } catch { /* the next seed */ }
|
|
244
|
+
}
|
|
245
|
+
return out;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* How often each recipe comes up in `mix()`, relative to 1: the ones whose shuffles most often give a mark worth
|
|
250
|
+
* keeping lead; the textured ones (iso, hex, tiles) come up less.
|
|
251
|
+
*/
|
|
252
|
+
export const MIX_WEIGHT = { twin: 1.6, cut: 1.6, split: 1.4, chevrons: 1.3, spiral: 1.2, links: 1.2, arcs: 1.2, bands: 1.2, stripes: 1.2, rings: 0.8, turns: 1.1, bloom: 1, burst: 1, bars: 1, waves: 0.9, stack: 0.8, steps: 0.8, letter: 0.8, shear: 0.6, venn: 0.5, route: 0.5, horizon: 0.5, tiles: 0.6, hex: 0.5, iso: 0.5, arcstep: 1.4, leaves: 1.3, seam: 1.3, trine: 1.2, blades: 1.1, invert: 1, wavecut: 1, capsules: 1, bite: 0.9, loops: 0.8, strips: 0.8, flare: 0.8, lobes: 0.7, halfsq: 0.7, pixelarc: 0.5 };
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* `count` marks from ANY recipe: each slot picks a recipe by its seed (from `ids`, every recipe by default, weighted by
|
|
256
|
+
* `MIX_WEIGHT`, never the one before it) and keeps the best of `tries` shuffles of it (`shuffled()`); no two the same
|
|
257
|
+
* drawing. Each has its `seed` and `quality`. For a bulk "random mix" grid.
|
|
258
|
+
*/
|
|
259
|
+
export function mix({ count = 48, seed = 1, ids = Object.keys(RECIPES), tries = 4 } = {}) {
|
|
260
|
+
const out = [], drawn = new Set(), total = ids.reduce((t, id) => t + (MIX_WEIGHT[id] ?? 1), 0);
|
|
261
|
+
let last = '';
|
|
262
|
+
for (let i = 0, s = seedOf(seed); out.length < count && i < count * 4; i++, s = (s + tries) >>> 0) {
|
|
263
|
+
const rand = rng(`mix:${s}`);
|
|
264
|
+
let id = ids[0];
|
|
265
|
+
for (let t = 0; t < 3; t++) {
|
|
266
|
+
let at = rand() * total;
|
|
267
|
+
for (const c of ids) { at -= MIX_WEIGHT[c] ?? 1; if (at <= 0) { id = c; break; } }
|
|
268
|
+
if (id !== last || ids.length < 2) break;
|
|
269
|
+
}
|
|
270
|
+
const g = shuffled(id, s, { tries });
|
|
271
|
+
if (g && !drawn.has(g.svg)) { drawn.add(g.svg); out.push(g); last = id; }
|
|
272
|
+
}
|
|
273
|
+
return out;
|
|
274
|
+
}
|
package/src/guard.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The quality guard: what makes a generated mark unusable at small sizes, read from its geometry before fitting.
|
|
3
|
+
* A mark fails on a speck (a piece much smaller than the rest), a sliver (a piece thinner than about half the house
|
|
4
|
+
* gap on average), hairline holes (the counters an opening of radius 1 px wipes out, past `maxThinHoles`), thin parts (spikes, pinched necks or slivers that an opening of a small bitmap wipes out, past
|
|
5
|
+
* `maxThinShare` of the ink), too many pieces to read at 32 px, too plain (one solid convex shape with no hole: a bare
|
|
6
|
+
* square, disc or quarter disc is not a mark), or too sparse (the ink under `minInkShare` of its own bounding box).
|
|
7
|
+
*/
|
|
8
|
+
import { signedArea, perimeter } from '@logomarks/core/geometry';
|
|
9
|
+
import { fill, thinShareBits } from './raster.js';
|
|
10
|
+
|
|
11
|
+
/** The limits, in units of the 448 box. */
|
|
12
|
+
export const GUARD = { minPieceShare: 0.012, minMeanWidth: 14, maxPieces: 26, raster: 128, neck: 2, maxThinShare: 0.035, plainSolidity: 0.96, minInkShare: 0.14, maxThinHoles: 0.45 };
|
|
13
|
+
|
|
14
|
+
/** Each piece of a shape (a polygon: its outer ring and holes) with its area and mean width (2 × area / perimeter). */
|
|
15
|
+
export function pieces(shape) {
|
|
16
|
+
const polys = !shape || !shape.length ? [] : typeof shape[0][0][0] === 'number' ? [shape] : shape; // a lone polygon too
|
|
17
|
+
return polys.map((poly) => {
|
|
18
|
+
const a = Math.abs(signedArea(poly[0])) - poly.slice(1).reduce((s, h) => s + Math.abs(signedArea(h)), 0);
|
|
19
|
+
const per = poly.reduce((s, ring) => s + perimeter(ring), 0);
|
|
20
|
+
return { area: a, width: (2 * a) / Math.max(per, 1e-9) };
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** What is wrong with a shape, in plain words; empty when it passes. `limits` overrides GUARD's for a recipe. */
|
|
25
|
+
export function problems(shape, limits = {}) {
|
|
26
|
+
const L = { ...GUARD, ...limits };
|
|
27
|
+
const ps = pieces(shape).filter((p) => p.area > 1e-6), out = [];
|
|
28
|
+
if (!ps.length) return ['nothing drawn'];
|
|
29
|
+
const total = ps.reduce((s, p) => s + p.area, 0);
|
|
30
|
+
if (ps.length > L.maxPieces) out.push(`${ps.length} pieces, too busy`);
|
|
31
|
+
if (ps.some((p) => p.area < L.minPieceShare * total)) out.push('a speck');
|
|
32
|
+
if (ps.some((p) => p.width < L.minMeanWidth)) out.push('a sliver');
|
|
33
|
+
if (!out.length && thinShare(shape) > L.maxThinShare) out.push('thin parts');
|
|
34
|
+
const polys = typeof shape[0][0][0] === 'number' ? [shape] : shape;
|
|
35
|
+
// the holes read too: a knocked-out mark (a badge, an outline) whose holes are hairlines is unreadable at 32 px
|
|
36
|
+
const holes = polys.flatMap((poly) => poly.slice(1).map((h) => [h]));
|
|
37
|
+
const holeArea = holes.reduce((t, h) => t + Math.abs(signedArea(h[0])), 0);
|
|
38
|
+
if (!out.length && holeArea > 0.03 * total && thinShareBits(fill(holes, GUARD.raster), GUARD.raster, 1) > L.maxThinHoles) out.push('hairline holes');
|
|
39
|
+
if (polys.length === 1 && polys[0].length === 1 && total / hullArea(polys[0][0]) > L.plainSolidity) out.push('too plain');
|
|
40
|
+
const [x0, y0, x1, y1] = box(polys);
|
|
41
|
+
if (total / Math.max(1, (x1 - x0) * (y1 - y0)) < L.minInkShare) out.push('too sparse');
|
|
42
|
+
return out;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** The share of the ink in parts thinner than about twice `neck` pixels of a `raster`-wide bitmap (4 units a pixel at
|
|
46
|
+
* 128): what an opening (erode, then dilate) wipes out. */
|
|
47
|
+
export function thinShare(shape) {
|
|
48
|
+
return thinShareBits(fill(shape, GUARD.raster), GUARD.raster, GUARD.neck);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** The bounding box of polygons: [x0, y0, x1, y1]. */
|
|
52
|
+
function box(polys) {
|
|
53
|
+
let x0 = Infinity, y0 = Infinity, x1 = -Infinity, y1 = -Infinity;
|
|
54
|
+
for (const poly of polys) for (const [x, y] of poly[0]) { x0 = Math.min(x0, x); y0 = Math.min(y0, y); x1 = Math.max(x1, x); y1 = Math.max(y1, y); }
|
|
55
|
+
return [x0, y0, x1, y1];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** The area of a ring's convex hull (monotone chain). */
|
|
59
|
+
function hullArea(ring) {
|
|
60
|
+
const pts = [...ring].sort((a, b) => a[0] - b[0] || a[1] - b[1]);
|
|
61
|
+
const cross = (o, a, b) => (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0]);
|
|
62
|
+
const lower = [], upper = [];
|
|
63
|
+
for (const p of pts) { while (lower.length >= 2 && cross(lower.at(-2), lower.at(-1), p) <= 0) lower.pop(); lower.push(p); }
|
|
64
|
+
for (const p of pts.reverse()) { while (upper.length >= 2 && cross(upper.at(-2), upper.at(-1), p) <= 0) upper.pop(); upper.push(p); }
|
|
65
|
+
return Math.abs(signedArea([...lower.slice(0, -1), ...upper.slice(0, -1)]));
|
|
66
|
+
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/** A control a recipe exposes. */
|
|
2
|
+
export type Control = (
|
|
3
|
+
| { key: string; label: string; type: 'range'; min: number; max: number; step: number; value: number }
|
|
4
|
+
| { key: string; label: string; type: 'choice'; options: string[]; value: string }
|
|
5
|
+
| { key: string; label: string; type: 'toggle'; value: boolean }
|
|
6
|
+
) & {
|
|
7
|
+
/** 'finish' for the shared finish controls (turn, mirror, round, outline); none for the recipe's own. */
|
|
8
|
+
group?: 'finish';
|
|
9
|
+
/** What a shuffle draws, narrower than what a hand can set: [min, max] of a range, or some of a choice's options. */
|
|
10
|
+
random?: [number, number] | string[];
|
|
11
|
+
/** The chance, 0–1, that a shuffle moves this control off its default at all (a second move: a slant, a twist). */
|
|
12
|
+
rare?: number;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/** Param values by control key. */
|
|
16
|
+
export type Params = Record<string, number | string | boolean>;
|
|
17
|
+
|
|
18
|
+
/** A recipe: a construction and its controls. */
|
|
19
|
+
export interface Recipe {
|
|
20
|
+
id: string;
|
|
21
|
+
/** Its name in a picker ("Bars"). */
|
|
22
|
+
name: string;
|
|
23
|
+
/** One plain line about it. */
|
|
24
|
+
line: string;
|
|
25
|
+
controls: Control[];
|
|
26
|
+
/** The mark for resolved params, unfitted, as a core Shape. */
|
|
27
|
+
build(params: Params): unknown;
|
|
28
|
+
/** The guard's limits this recipe overrides (dots and pixels are many pieces by design). */
|
|
29
|
+
guard: Partial<{ minPieceShare: number; minMeanWidth: number; maxPieces: number; maxThinShare: number; plainSolidity: number; minInkShare: number }>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** A generated mark. */
|
|
33
|
+
export interface Generated {
|
|
34
|
+
recipe: string;
|
|
35
|
+
/** The params it was drawn with, resolved (every control, clamped). */
|
|
36
|
+
params: Params;
|
|
37
|
+
/** The fitted SVG: 512 canvas, longest side 448, centred, currentColor. */
|
|
38
|
+
svg: string;
|
|
39
|
+
/** The fitted mark's pieces as SVG path data, one per piece (its outline and holes, even-odd), in reading order. */
|
|
40
|
+
pieces: string[];
|
|
41
|
+
/** What the guard found wrong (a speck, a sliver, too busy); empty when it passes. */
|
|
42
|
+
problems: string[];
|
|
43
|
+
/** The seed its params were drawn from, for a variation. */
|
|
44
|
+
seed?: number;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Every recipe, by id: bars, bands, twin, stack, shear, turns, burst, tiles, route, arcs, rings, stripes, chevrons, waves,
|
|
48
|
+
* cut, split, venn, spiral, bloom, links, horizon, steps, hex, iso, letter, arcstep, halfsq, invert, leaves, lobes, loops, trine, strips, bite, blades, wavecut,
|
|
49
|
+
* capsules, flare, pixelarc and seam. */
|
|
50
|
+
export const RECIPES: Record<string, Recipe>;
|
|
51
|
+
/** A recipe by id; throws for an unknown one. */
|
|
52
|
+
export function recipe(id: string): Recipe;
|
|
53
|
+
/** A mark from a recipe and its params (defaults for missing ones, clamped), fitted to the standard. */
|
|
54
|
+
export function generate(id: string, params?: Params): Generated;
|
|
55
|
+
/** Random params from a seed, keeping `locked` keys at `base`. */
|
|
56
|
+
export function randomParams(id: string, seed: number | string, o?: { base?: Params; locked?: string[]; finish?: boolean }): Params;
|
|
57
|
+
/** `count` marks from consecutive seeds, `locked` controls held at `base`; failed draws are skipped. */
|
|
58
|
+
export function variations(id: string, o?: { count?: number; seed?: number | string; base?: Params; locked?: string[]; finish?: boolean; tries?: number }): Generated[];
|
|
59
|
+
/** Params close to `params`: ranges nudged by up to `amount` of their span, a choice swapped now and then; toggles, the
|
|
60
|
+
* finish and `locked` keys kept. */
|
|
61
|
+
export function nearby(id: string, params: Params, o?: { seed?: number | string; amount?: number; locked?: string[] }): Params;
|
|
62
|
+
/** `count` marks close to `params`, failed draws skipped, none repeated. */
|
|
63
|
+
export function similar(id: string, params: Params, o?: { count?: number; seed?: number | string; amount?: number; locked?: string[] }): Generated[];
|
|
64
|
+
/** `count` marks from any recipe (weighted by MIX_WEIGHT, never the same twice running), each the best of `tries` shuffles; none the same drawing. */
|
|
65
|
+
export function mix(o?: { count?: number; seed?: number | string; ids?: string[]; tries?: number }): (Generated & { seed: number; quality: number })[];
|
|
66
|
+
/** The best of `tries` shuffles of a recipe from `seed` on, by `qualityOf`, with its `seed` and `quality`; null when none passes the guard. */
|
|
67
|
+
export function shuffled(id: string, seed: number | string, o?: { tries?: number; base?: Params; locked?: string[]; finish?: boolean }): (Generated & { seed: number; quality: number }) | null;
|
|
68
|
+
/** How good a drawn shape is as a mark, 0–1, and the parts of the score (ink, square, pieces, thin, gaps, balance, calm). */
|
|
69
|
+
export function qualityOf(shape: unknown): { score: number; ink?: number; square?: number; pieces?: number; thin?: number; gaps?: number; balance?: number; calm?: number };
|
|
70
|
+
/** How often each recipe comes up in `mix()`, relative to 1. */
|
|
71
|
+
export const MIX_WEIGHT: Record<string, number>;
|
|
72
|
+
/** Per recipe and control key: what a shuffle draws (`random`, `rare`) and any default it replaces (`value`). */
|
|
73
|
+
export const TUNING: Record<string, Record<string, { random?: [number, number] | string[]; rare?: number; value?: number | string | boolean }>>;
|
|
74
|
+
/** Per recipe: the keys of the controls a page shows first. */
|
|
75
|
+
export const MAIN: Record<string, string[]>;
|
|
76
|
+
/** Every control's default. */
|
|
77
|
+
export function defaults(controls: Control[]): Params;
|
|
78
|
+
/** Every control's value from params, clamped, defaults for missing ones. */
|
|
79
|
+
export function resolve(controls: Control[], params?: Params): Params;
|
|
80
|
+
/** One value fitted to its control. */
|
|
81
|
+
export function clampTo(control: Control, v: unknown): number | string | boolean;
|
|
82
|
+
/** A random value for a control. */
|
|
83
|
+
export function randomValue(control: Control, r: () => number): number | string | boolean;
|
|
84
|
+
/** A random function in [0, 1) from a seed (mulberry32). */
|
|
85
|
+
export function rng(seed: number | string): () => number;
|
|
86
|
+
/** A seed from a number or a string, as an unsigned 32-bit integer. */
|
|
87
|
+
export function seedOf(seed: number | string): number;
|
|
88
|
+
/** The guard's limits, in units of the 448 box. */
|
|
89
|
+
export const GUARD: { minPieceShare: number; minMeanWidth: number; maxPieces: number };
|
|
90
|
+
/** What is wrong with an unfitted shape, in plain words; empty when it passes. */
|
|
91
|
+
export function problems(shape: unknown, limits?: Record<string, number>): string[];
|
|
92
|
+
/** A preset: a recipe and its settings, a one-click starting point. */
|
|
93
|
+
export interface Preset {
|
|
94
|
+
id: string;
|
|
95
|
+
name: string;
|
|
96
|
+
recipe: string;
|
|
97
|
+
params: Params;
|
|
98
|
+
/** One plain line about it. */
|
|
99
|
+
note: string;
|
|
100
|
+
}
|
|
101
|
+
/** The combinations the references use most, each drawing and passing the guard. */
|
|
102
|
+
export const PRESETS: Preset[];
|
|
103
|
+
/** An unfitted shape's pieces as SVG path data in the fitted 512 canvas (longest side 448, centred), in reading order. */
|
|
104
|
+
export function piecesOf(shape: unknown): string[];
|
|
105
|
+
/**
|
|
106
|
+
* An SVG of pieces in two colours: `colors` [base, accent] (default currentColor and #c8ff00); `pattern` alternate (every
|
|
107
|
+
* other piece in the accent), first (the first piece), rows (every other row) or size (the biggest piece).
|
|
108
|
+
*/
|
|
109
|
+
export function svgOf(pieces: string[], o?: { colors?: [string, string]; pattern?: 'alternate' | 'first' | 'last' | 'rows' | 'size' | 'smallest' | 'inner' | 'outer' | 'left' | 'top' | 'gradient' }): string;
|
|
110
|
+
/** The colour patterns `svgOf` knows, in a picker's order. */
|
|
111
|
+
export const PATTERNS: readonly string[];
|
package/src/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @logomarks/generate: abstract logomarks from rules and controls, no AI.
|
|
3
|
+
*/
|
|
4
|
+
export { RECIPES } from './recipes/index.js';
|
|
5
|
+
export { recipe, generate, randomParams, variations, nearby, similar, mix, shuffled, qualityOf, MIX_WEIGHT, piecesOf, svgOf, PATTERNS } from './generate.js';
|
|
6
|
+
export { defaults, resolve, clampTo, randomValue } from './controls.js';
|
|
7
|
+
export { rng, seedOf } from './random.js';
|
|
8
|
+
export { GUARD, problems } from './guard.js';
|
|
9
|
+
export { TUNING, MAIN } from './tuning.js';
|
|
10
|
+
export { PRESETS } from './presets.js';
|
package/src/presets.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Presets: a hand-picked set of marks, each a recipe and its settings, chosen by eye from ranked sheets of shuffles for
|
|
3
|
+
* a page to offer as one-click starting points. Every one draws and passes the guard (tested).
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/** @type {import('./index.js').Preset[]} */
|
|
7
|
+
export const PRESETS = [
|
|
8
|
+
{ id: 'round-twin', name: 'Round twin', recipe: 'twin', params: { arm: 0.85, thickness: 0.33, stem: 0.63, corner: 'cut', ends: 'round' }, note: 'Two rounded hooks turned about the centre.' },
|
|
9
|
+
{ id: 'twin-s', name: 'Twin S', recipe: 'twin', params: { piece: 'arc', arm: 0.83, thickness: 0.34, stem: 0.52, corner: 'cut', ends: 'round' }, note: 'Two bands that read as one S.' },
|
|
10
|
+
{ id: 'cut-twin', name: 'Cut twin', recipe: 'twin', params: { piece: 'hook', arm: 0.99, stem: 0.61, corner: 'cut', ends: 'angled', slant: -4 }, note: 'Two angular hooks, corners cut.' },
|
|
11
|
+
{ id: 'split-disc', name: 'Split disc', recipe: 'split', params: { cuts: 1, angle: -9, spacing: 1.7, slide: 0.27, spread: 0.5 }, note: 'A disc cut once, the halves slid.' },
|
|
12
|
+
{ id: 'split-hexagon', name: 'Split hexagon', recipe: 'split', params: { shape: 'hexagon', cuts: 1, angle: 1, spacing: 1.5, slide: 0.02, spread: 0.43 }, note: 'A hexagon in two slid halves.' },
|
|
13
|
+
{ id: 'slashed-disc', name: 'Slashed disc', recipe: 'split', params: { cuts: 1, angle: -49, spacing: 1.9, slide: 0.19, twist: 3.5, spread: 0.4 }, note: 'A disc with one diagonal slot.' },
|
|
14
|
+
{ id: 'six-petals', name: 'Six petals', recipe: 'bloom', params: { petal: 'ellipse', width: 0.69, inner: 0.3, join: 'tuck' }, note: 'Six petals round an open centre.' },
|
|
15
|
+
{ id: 'four-discs', name: 'Four discs', recipe: 'turns', params: { unit: 'circle', count: '4', radius: 0.11, face: '180', join: 'tuck' }, note: 'Four discs turned about a centre.' },
|
|
16
|
+
{ id: 'four-leaves', name: 'Four leaves', recipe: 'turns', params: { unit: 'leaf', count: '4', size: 0.45, radius: 0.14, face: '270' }, note: 'Four leaves turned like a pinwheel.' },
|
|
17
|
+
{ id: 'hex-pinwheel', name: 'Hex pinwheel', recipe: 'turns', params: { unit: 'triangle', count: '6', size: 0.37, stretch: 1.3, radius: 0.1, face: '180', join: 'tuck' }, note: 'Six triangles turned inside a hexagon.' },
|
|
18
|
+
{ id: 'speed-bands', name: 'Speed bands', recipe: 'bands', params: { count: 3, weight: 0.75, joinAt: 0.38, joinWidth: 0.23, extra: 'none', ends: 'round' }, note: 'Bands climbing through one bend.' },
|
|
19
|
+
{ id: 'stepped-bands', name: 'Stepped bands', recipe: 'bands', params: { weight: 0.6, joinAt: 0.42, extra: 'none' }, note: 'Two bands through one slanted step.' },
|
|
20
|
+
{ id: 'asterisk', name: 'Asterisk', recipe: 'burst', params: { rays: 6, ray: 'pill', width: 0.155, inner: 0.28 }, note: 'Rounded rays round an open centre.' },
|
|
21
|
+
{ id: 'two-links', name: 'Two links', recipe: 'links', params: { weight: 0.125, overlap: 0.24, corner: 0.41 }, note: 'Two rounded loops woven like a chain.' },
|
|
22
|
+
{ id: 'double-chevron', name: 'Double chevron', recipe: 'chevrons', params: { count: 2, angle: 111, weight: 0.52, point: 'down' }, note: 'Two heavy chevrons pointing down.' },
|
|
23
|
+
{ id: 'back-chevrons', name: 'Back chevrons', recipe: 'chevrons', params: { angle: 117, weight: 0.74, tip: 'flat', grow: 0.36, point: 'left' }, note: 'Chevrons pointing left, the first an arrowhead.' },
|
|
24
|
+
{ id: 'bitten-hexagon', name: 'Bitten hexagon', recipe: 'cut', params: { body: 'hexagon', cut: 'arc', side: 'left', at: 0.31, size: 0.45, width: 1.1 }, note: 'A hexagon with one round bite.' },
|
|
25
|
+
{ id: 'bitten-disc', name: 'Bitten disc', recipe: 'cut', params: { body: 'circle', side: 'left', at: 0.51, size: 0.21, width: 2.5, second: 'opposite' }, note: 'A disc with one deep bite.' },
|
|
26
|
+
{ id: 'coil', name: 'Coil', recipe: 'spiral', params: { turns: 1.5, weight: 0.46, start: 'square', squash: 0.81, spin: 120 }, note: 'One round band coiled once and a half.' },
|
|
27
|
+
{ id: 'arch', name: 'Arch', recipe: 'arcs', params: { count: 2, weight: 0.7, legs: 0.35, chamfer: 0.33 }, note: 'Two nested half rings on short legs.' },
|
|
28
|
+
{ id: 'corner-arcs', name: 'Corner arcs', recipe: 'arcs', params: { count: 2, sweep: 'quarter', weight: 0.56, legs: 0.18, chamfer: 0.38 }, note: 'Two quarter rings from a corner.' },
|
|
29
|
+
{ id: 'striped-arch', name: 'Striped arch', recipe: 'stripes', params: { shape: 'arch', stripes: 5, weight: 0.65, angle: 20, move: 'shift', amount: 0.02 }, note: 'An arch drawn in slanted stripes.' },
|
|
30
|
+
{ id: 'striped-hexagon', name: 'Striped hexagon', recipe: 'stripes', params: { shape: 'hexagon', stripes: 6, weight: 0.53, angle: 25, move: 'taper', amount: 0.47 }, note: 'A hexagon drawn in diagonal stripes.' },
|
|
31
|
+
{ id: 'wave-tile', name: 'Wave tile', recipe: 'waves', params: { weight: 0.64, amplitude: 0.095, waves: 1.25, clip: 'circle' }, note: 'Three wavy bands cut to a square.' },
|
|
32
|
+
{ id: 'arc-step', name: 'Arc step', recipe: 'arcstep', params: { layout: 'stair', steps: 1, weight: 0.8, corners: 'round' }, note: 'One heavy band through two quarter arcs.' },
|
|
33
|
+
{ id: 'leaf-tiles', name: 'Leaf tiles', recipe: 'leaves', params: { tile: 'leaf', order: 'outward' }, note: 'Four leaves, their points to the middle.' },
|
|
34
|
+
{ id: 'tenon-block', name: 'Tenon block', recipe: 'seam', params: { joint: 'tenon', body: 'square' }, note: 'A block in two halves, joined by a tenon.' },
|
|
35
|
+
{ id: 'shutter', name: 'Shutter', recipe: 'trine', params: { body: 'disc', offset: 0.22 }, note: 'A disc in three parts turning round an open middle.' },
|
|
36
|
+
{ id: 'blade-sweep', name: 'Blade sweep', recipe: 'blades', params: { count: 3, grow: 0.5, width: 1.6 }, note: 'Three blades, each taller than the last.' },
|
|
37
|
+
];
|
package/src/random.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Seeded random numbers: the same seed always draws the same mark.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/** A seed from a number or any string (FNV-1a), as an unsigned 32-bit integer. */
|
|
6
|
+
export function seedOf(seed) {
|
|
7
|
+
if (typeof seed === 'number' && Number.isFinite(seed)) return seed >>> 0;
|
|
8
|
+
let h = 2166136261;
|
|
9
|
+
for (const ch of String(seed)) h = Math.imul(h ^ ch.codePointAt(0), 16777619);
|
|
10
|
+
return h >>> 0;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** A random function in [0, 1) from a seed (mulberry32). */
|
|
14
|
+
export function rng(seed) {
|
|
15
|
+
let a = seedOf(seed);
|
|
16
|
+
return () => {
|
|
17
|
+
a = (a + 0x6d2b79f5) >>> 0;
|
|
18
|
+
let t = a;
|
|
19
|
+
t = Math.imul(t ^ (t >>> 15), t | 1);
|
|
20
|
+
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
|
|
21
|
+
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
22
|
+
};
|
|
23
|
+
}
|
package/src/raster.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A small bitmap of a shape, and opening it (erode, then dilate): the guard's fast test for thin parts.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/** The shape filled into an N × N bitmap of the 512 canvas (even-odd, sampled at pixel centres). */
|
|
6
|
+
export function fill(shape, N = 128) {
|
|
7
|
+
const bits = new Uint8Array(N * N), k = 512 / N, edges = [];
|
|
8
|
+
for (const poly of shape) for (const ring of poly) for (let i = 0; i < ring.length; i++) {
|
|
9
|
+
const a = ring[i], b = ring[(i + 1) % ring.length];
|
|
10
|
+
if (a[1] !== b[1]) edges.push(a[1] < b[1] ? [a[0], a[1], b[0], b[1]] : [b[0], b[1], a[0], a[1]]);
|
|
11
|
+
}
|
|
12
|
+
for (let row = 0; row < N; row++) {
|
|
13
|
+
const y = (row + 0.5) * k, xs = [];
|
|
14
|
+
for (const [x0, y0, x1, y1] of edges) if (y >= y0 && y < y1) xs.push(x0 + ((y - y0) * (x1 - x0)) / (y1 - y0));
|
|
15
|
+
xs.sort((p, q) => p - q);
|
|
16
|
+
for (let i = 0; i + 1 < xs.length; i += 2) {
|
|
17
|
+
const c0 = Math.max(0, Math.ceil(xs[i] / k - 0.5)), c1 = Math.min(N - 1, Math.floor(xs[i + 1] / k - 0.5));
|
|
18
|
+
for (let c = c0; c <= c1; c++) bits[row * N + c] = 1;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return bits;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** The offsets of a disc of radius r pixels. */
|
|
25
|
+
function disc(r) {
|
|
26
|
+
const out = [];
|
|
27
|
+
for (let dy = -r; dy <= r; dy++) for (let dx = -r; dx <= r; dx++) if (dx * dx + dy * dy <= r * r + r) out.push([dx, dy]);
|
|
28
|
+
return out;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** One pass of erosion (`all` true: kept where the whole disc is set) or dilation (set where any of it is). */
|
|
32
|
+
function morph(bits, N, r, all) {
|
|
33
|
+
const out = new Uint8Array(N * N), d = disc(r);
|
|
34
|
+
for (let y = 0; y < N; y++) for (let x = 0; x < N; x++) {
|
|
35
|
+
let v = all ? 1 : 0;
|
|
36
|
+
for (const [dx, dy] of d) {
|
|
37
|
+
const X = x + dx, Y = y + dy, b = X >= 0 && Y >= 0 && X < N && Y < N ? bits[Y * N + X] : 0;
|
|
38
|
+
if (all && !b) { v = 0; break; }
|
|
39
|
+
if (!all && b) { v = 1; break; }
|
|
40
|
+
}
|
|
41
|
+
out[y * N + x] = v;
|
|
42
|
+
}
|
|
43
|
+
return out;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** The share of set pixels an opening of radius r wipes out (parts thinner than about 2r pixels). */
|
|
47
|
+
export function thinShareBits(bits, N, r) {
|
|
48
|
+
const opened = morph(morph(bits, N, r, true), N, r, false);
|
|
49
|
+
let ink = 0, kept = 0;
|
|
50
|
+
for (let i = 0; i < bits.length; i++) { ink += bits[i]; kept += bits[i] & opened[i]; }
|
|
51
|
+
return ink ? (ink - kept) / ink : 0;
|
|
52
|
+
}
|