@logomarks/generate 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/CHANGELOG.md +25 -0
- package/LICENSE +21 -0
- package/README.md +41 -2
- package/package.json +29 -4
- package/src/controls.js +34 -0
- package/src/draw.js +60 -0
- package/src/finish.js +172 -0
- package/src/generate.js +194 -0
- package/src/guard.js +66 -0
- package/src/index.d.ts +96 -0
- package/src/index.js +9 -0
- package/src/presets.js +26 -0
- package/src/random.js +23 -0
- package/src/raster.js +52 -0
- package/src/recipes/arcs.js +83 -0
- package/src/recipes/bands.js +90 -0
- package/src/recipes/bars.js +106 -0
- package/src/recipes/blobs.js +74 -0
- package/src/recipes/bloom.js +53 -0
- package/src/recipes/burst.js +44 -0
- package/src/recipes/chevrons.js +45 -0
- package/src/recipes/cut.js +80 -0
- package/src/recipes/dashes.js +61 -0
- package/src/recipes/dots.js +47 -0
- package/src/recipes/fan.js +73 -0
- package/src/recipes/fold.js +66 -0
- package/src/recipes/hex.js +58 -0
- package/src/recipes/horizon.js +51 -0
- package/src/recipes/index.js +79 -0
- package/src/recipes/iso.js +35 -0
- package/src/recipes/letter.js +204 -0
- package/src/recipes/line.js +142 -0
- package/src/recipes/links.js +63 -0
- package/src/recipes/orbit.js +50 -0
- package/src/recipes/pixel.js +38 -0
- package/src/recipes/rings.js +82 -0
- package/src/recipes/route.js +107 -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/tiles.js +60 -0
- package/src/recipes/tri.js +126 -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/waves.js +40 -0
- package/src/shapes.js +54 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.1.0 (2026-11-26)
|
|
4
|
+
|
|
5
|
+
- First release (32 recipes, then the study's top 12, `line`, 18 presets; then `letter`, containers, pieces): the recipes `bars`, `bands`, `twin`, `stack` (primitives stacked, overlapped or nested, combined by
|
|
6
|
+
gap, over, cut, merge or xor), `shear`, `turns`, `burst`, `tiles`, `route`, `arcs`, `rings`, `stripes`, `dots`, `pixel`, `chevrons`, `waves`, `cut`, `split`, `venn`, `blobs`, `dashes`, `fan`, `fold`,
|
|
7
|
+
`spiral`, `orbit`, `bloom`, `links`, `horizon`, `steps`, `hex`, `tri`, `iso` and `line` (33), each with its controls and, where it needs them, its own guard limits; `tidy()` for crumbs; the shared finish
|
|
8
|
+
controls (turn, mirror, round with `roundWhich`: all, diagonal, top, bottom, left, right, alternate or inner, which
|
|
9
|
+
keeps the necks an opening would wipe out, and outline), left alone by shuffles unless asked; `mix()` (a bulk random mix across every recipe), `nearby()` and `similar()` (small whole-number controls step by one, layout numbers move a few along); the primitives (`src/shapes.js`); `generate()`, `randomParams()`,
|
|
10
|
+
`variations()` with locked controls; the quality guard (specks, slivers, thin parts by a bitmap opening, too
|
|
11
|
+
plain, too sparse); the playground (`npm run demo`; double-click a control to reset it, Reset all).
|
|
12
|
+
- From the study of the owner's references (generator-study.md): arcs `corner`, `chamfer`, `slant`, `core: quarter`;
|
|
13
|
+
shear `lengths`, `slide`; bars `spine`, `spineWeight`, `widths`, `cutAngle`, `cut: notch`; rings `shape`,
|
|
14
|
+
`breakKind`; route `frame`, `turns`; cut `angle`, `second`, `cut: arc`; turns units `blade`, `kite`; stripes
|
|
15
|
+
`move: dash`, `cover`, `ends`; the finish's `roundWhich` each-right, each-top, each-corner and the `accent` dot; the
|
|
16
|
+
recipe `line`; `PRESETS` (18). New controls default to the old drawing.
|
|
17
|
+
- Containers: the finish's `container` (circle, square, rounded, hexagon), `containerMode` badge or clip and
|
|
18
|
+
`containerSize`; badges shrink each hole so bridges stay readable, and the guard turns away hairline holes.
|
|
19
|
+
- `pieces` on every generated mark and `svgOf()` for two colours (alternate, first, rows, size); `piecesOf()`.
|
|
20
|
+
- `letter`: A–Z and 0–9 as slabs, stencil, route, tiles, blocks or pixel.
|
|
21
|
+
- Fixes: rising bars hang from a top spine (on a foot they are the signal-bars icon, on a bottom spine a bar chart);
|
|
22
|
+
`line` puts a loop only where the legs open 100° or more (a loop on a narrow V is the awareness ribbon); `tri` redraws
|
|
23
|
+
a layout that makes the three-triangle emblem or the trefoil, and lost its hexagon silhouette (hexagrams and nuts);
|
|
24
|
+
rings' `turn`, steps' `round` and tri's `outline` were overwritten by the finish's keys of the same name: now `spin`,
|
|
25
|
+
`roundTreads` and gone (a test checks no recipe repeats a key).
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ArrayPress Limited
|
|
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,42 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @logomarks/generate
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Abstract logomarks from rules and controls, no AI. Each recipe is one construction (34 of them: bars, bands, twin,
|
|
4
|
+
stack, shear, turns, burst, tiles, route, arcs, rings, stripes, dots, pixel, chevrons, waves, cut, split, venn, blobs,
|
|
5
|
+
dashes, fan, fold, spiral, orbit, bloom, links, horizon, steps, hex, tri, iso, line and letter) that lists its controls
|
|
6
|
+
and draws a mark from them, fitted to the Logomarks standard: a 512 canvas, the longest side 448, centred,
|
|
7
|
+
`currentColor`, the house gap. Every recipe also takes the finish controls (turn, mirror, round corners, outline, an
|
|
8
|
+
accent dot, a container: badge or clip). Each mark comes with its `pieces`, and `svgOf()` draws them in two colours. A
|
|
9
|
+
quality guard skips draws with specks, slivers, thin necks, hairline holes, a bare primitive or too little ink. Runs in
|
|
10
|
+
the browser and in Node.
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
npm install @logomarks/generate
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Quick start
|
|
19
|
+
|
|
20
|
+
```js
|
|
21
|
+
import { RECIPES, generate, randomParams, variations, svgOf } from '@logomarks/generate';
|
|
22
|
+
|
|
23
|
+
const mark = generate('bands', { count: 2, join: 'curve' }); // { recipe, params, svg, pieces, problems }
|
|
24
|
+
const shuffled = generate('twin', randomParams('twin', 42));
|
|
25
|
+
const grid = variations('bars', { count: 24, seed: 7, base: mark.params, locked: ['count'] });
|
|
26
|
+
const badge = generate('letter', { char: 'K', build: 'slabs', container: 'circle' });
|
|
27
|
+
const twoTone = svgOf(mark.pieces, { colors: ['#0b0b0b', '#c8ff00'], pattern: 'rows' });
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
`PRESETS` lists 18 one-click starting points. `RECIPES[id].controls` describes every control (range, choice or toggle) so a UI can build its sliders from it.
|
|
31
|
+
|
|
32
|
+
## Playground
|
|
33
|
+
|
|
34
|
+
`npm run demo`, then open http://localhost:4393/demo/.
|
|
35
|
+
|
|
36
|
+
## Docs
|
|
37
|
+
|
|
38
|
+
- [docs/recipes.md](docs/recipes.md): each recipe, its controls and the guard.
|
|
39
|
+
|
|
40
|
+
## Licence
|
|
41
|
+
|
|
42
|
+
MIT © ArrayPress Limited. What you generate is yours to use.
|
package/package.json
CHANGED
|
@@ -1,6 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@logomarks/generate",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Abstract logomarks from rules and controls, no AI: 34 recipes (bars, bands, twin, stack, shear, turns, burst, tiles, route, arcs, rings, stripes, dots, pixel, chevrons, waves, cut, split, venn, blobs, dashes, fan, fold, spiral, orbit, bloom, links, horizon, steps, hex, tri, iso, line, letter) that each list their controls and draw a mark from them and a seed, fitted to the Logomarks standard (512 canvas, 448 fit, the house gap), a shared finish (rounding, outline, an accent dot, a container), two-colour pieces, seeded variations and presets. Runs in the browser and in Node.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "src/index.js",
|
|
7
|
+
"types": "src/index.d.ts",
|
|
8
|
+
"sideEffects": false,
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./src/index.d.ts",
|
|
12
|
+
"import": "./src/index.js",
|
|
13
|
+
"default": "./src/index.js"
|
|
14
|
+
},
|
|
15
|
+
"./package.json": "./package.json"
|
|
16
|
+
},
|
|
17
|
+
"files": ["src", "README.md", "CHANGELOG.md", "LICENSE"],
|
|
18
|
+
"scripts": {
|
|
19
|
+
"test": "node --test tests/*.test.js",
|
|
20
|
+
"demo": "node demo/serve.mjs"
|
|
21
|
+
},
|
|
22
|
+
"keywords": ["logo", "logo-generator", "logomark", "generative", "svg", "geometry"],
|
|
23
|
+
"author": "David Sherlock",
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"homepage": "https://logomarks.ai",
|
|
26
|
+
"repository": { "type": "git", "url": "git+https://github.com/Logomarks/generate.git" },
|
|
27
|
+
"engines": { "node": ">=20" },
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@logomarks/core": "^0.2.3"
|
|
30
|
+
}
|
|
31
|
+
}
|
package/src/controls.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A recipe's controls: what each one is, its defaults, clamping a value to it, and drawing a random one.
|
|
3
|
+
* A control is `{ key, label, type: 'range', min, max, step, value }`, `{ key, label, type: 'choice', options, value }`
|
|
4
|
+
* or `{ key, label, type: 'toggle', value }`.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/** The defaults of every control, as `{ key: value }`. */
|
|
8
|
+
export function defaults(controls) {
|
|
9
|
+
return Object.fromEntries(controls.map((c) => [c.key, c.value]));
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** A value made to fit its control: a range clamped and snapped to its step, a choice one of its options. */
|
|
13
|
+
export function clampTo(control, v) {
|
|
14
|
+
if (control.type === 'range') {
|
|
15
|
+
const n = Number(v);
|
|
16
|
+
if (!Number.isFinite(n)) return control.value;
|
|
17
|
+
const snapped = control.min + Math.round((Math.min(control.max, Math.max(control.min, n)) - control.min) / control.step) * control.step;
|
|
18
|
+
return Number(snapped.toFixed(6));
|
|
19
|
+
}
|
|
20
|
+
if (control.type === 'choice') return control.options.includes(v) ? v : control.value;
|
|
21
|
+
return Boolean(v);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Every control's value from `params`, clamped, with the default where one is missing. */
|
|
25
|
+
export function resolve(controls, params = {}) {
|
|
26
|
+
return Object.fromEntries(controls.map((c) => [c.key, c.key in params ? clampTo(c, params[c.key]) : c.value]));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** A random value for a control, from `r` (a random function). */
|
|
30
|
+
export function randomValue(control, r) {
|
|
31
|
+
if (control.type === 'range') return clampTo(control, control.min + r() * (control.max - control.min));
|
|
32
|
+
if (control.type === 'choice') return control.options[Math.floor(r() * control.options.length)];
|
|
33
|
+
return r() < 0.5;
|
|
34
|
+
}
|
package/src/draw.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Drawing helpers shared by the recipes, on @logomarks/core's geometry: the 448 box on the 512 canvas, pieces with
|
|
3
|
+
* square, round or angled ends, shear, and clearing one shape from another by the gap.
|
|
4
|
+
*/
|
|
5
|
+
import { U, D, rect, poly, circle, mapPts, rot, grown, signedArea, perimeter } from '@logomarks/core/geometry';
|
|
6
|
+
|
|
7
|
+
/** The box every recipe draws in: 448 square, centred on the 512 canvas. */
|
|
8
|
+
export const BOX = { x0: 32, y0: 32, size: 448, x1: 480, y1: 480, cx: 256, cy: 256 };
|
|
9
|
+
/** The house gap, the narrowest space between two parts once fitted. */
|
|
10
|
+
export const GAP = 22;
|
|
11
|
+
|
|
12
|
+
/** An upright piece from (x, y0) to (x + w, y1), its two ends 'square', 'round' or 'angled' (cut at 45°, top to the right). */
|
|
13
|
+
export function upright(x, y0, y1, w, ends = 'square') {
|
|
14
|
+
const h = y1 - y0;
|
|
15
|
+
if (ends === 'round' && h > w) return U(rect(x, y0 + w / 2, w, h - w), circle(x + w / 2, y0 + w / 2, w / 2, 64), circle(x + w / 2, y1 - w / 2, w / 2, 64));
|
|
16
|
+
if (ends === 'angled' && h > w * 1.2) return poly([[x, y0 + w], [x + w, y0], [x + w, y1 - w], [x, y1]]);
|
|
17
|
+
return rect(x, y0, w, h);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** A level piece from (x0, y) to (x1, y + h), its ends as `upright()`'s (angled: left end low, right end high). */
|
|
21
|
+
export function level(x0, x1, y, h, ends = 'square') {
|
|
22
|
+
const w = x1 - x0;
|
|
23
|
+
if (ends === 'round' && w > h) return U(rect(x0 + h / 2, y, w - h, h), circle(x0 + h / 2, y + h / 2, h / 2, 64), circle(x1 - h / 2, y + h / 2, h / 2, 64));
|
|
24
|
+
if (ends === 'angled' && w > h * 1.2) return poly([[x0 + h, y], [x1, y], [x1 - h, y + h], [x0, y + h]]);
|
|
25
|
+
return rect(x0, y, w, h);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** A shape sheared sideways by `deg` about the box's middle line (tops lean right for a positive angle). */
|
|
29
|
+
export function shear(shape, deg) {
|
|
30
|
+
if (!deg) return shape;
|
|
31
|
+
const k = Math.tan((deg * Math.PI) / 180);
|
|
32
|
+
return mapPts(shape, ([x, y]) => [x - k * (y - BOX.cy), y]);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** `a` with everything within the gap `g` of `b` taken away, so the two stay at least `g` apart. */
|
|
36
|
+
export function clear(a, b, g = GAP) {
|
|
37
|
+
return D(a, grown(b, g));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** A shape turned 90° about the canvas centre. */
|
|
41
|
+
export function quarterTurn(shape) {
|
|
42
|
+
return rot(shape, 90);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* A shape with its crumbs dropped: every piece under `share` of the ink, or thinner on average than `width` (2 × area /
|
|
47
|
+
* perimeter), left out. For constructions that clip or cut (stripes in a disc, a slot through rings), where the edge
|
|
48
|
+
* pieces are too small to read.
|
|
49
|
+
*/
|
|
50
|
+
export function tidy(shape, { share = 0.015, width = 16 } = {}) {
|
|
51
|
+
if (!shape || !shape.length) return [];
|
|
52
|
+
const polys = typeof shape[0][0][0] === 'number' ? [shape] : shape;
|
|
53
|
+
const measured = polys.map((poly) => {
|
|
54
|
+
const a = Math.abs(signedArea(poly[0])) - poly.slice(1).reduce((t, h) => t + Math.abs(signedArea(h)), 0);
|
|
55
|
+
const per = poly.reduce((t, ring) => t + perimeter(ring), 0);
|
|
56
|
+
return { poly, a, w: (2 * a) / Math.max(per, 1e-9) };
|
|
57
|
+
});
|
|
58
|
+
const total = measured.reduce((t, m) => t + m.a, 0);
|
|
59
|
+
return measured.filter((m) => m.a >= share * total && m.w >= width).map((m) => m.poly);
|
|
60
|
+
}
|
package/src/finish.js
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The finish: controls every recipe shares, applied after it draws. Turn the mark, mirror it, round its corners or draw
|
|
3
|
+
* it as an outline. Rounding is an opening (shrink, then grow back), which rounds every outer corner; `roundWhich` keeps
|
|
4
|
+
* it to some of them, the designer's trick of rounding only a few: a diagonal pair, one side, every other piece, or the
|
|
5
|
+
* inner corners instead (a closing, kept under half the house gap so separate pieces never fuse), or each piece's own
|
|
6
|
+
* right end, top or outer corner (measured from the piece's own box). `accent` adds one accent dot, a house gap off the
|
|
7
|
+
* mark's right end or corner, or inside its biggest counter (never a round one: a ring with a dot is an eye).
|
|
8
|
+
* `container` puts the finished mark in a circle, square, rounded square or hexagon: clipped to it (`clip`: the mark
|
|
9
|
+
* zoomed by 1 / `containerSize` and cut at the shape's edge) or knocked out of it (`badge`: the shape solid, the mark a
|
|
10
|
+
* house gap's worth of hole, sitting at `containerSize` of the shape).
|
|
11
|
+
*/
|
|
12
|
+
import { U, D, I, rect, rot, mapPts, grow, signedArea, perimeter, circle, rr, regular } from '@logomarks/core/geometry';
|
|
13
|
+
import { BOX, GAP, tidy } from './draw.js';
|
|
14
|
+
|
|
15
|
+
/** @type {import('./index.js').Control[]} */
|
|
16
|
+
export const FINISH = [
|
|
17
|
+
{ key: 'turn', label: 'Turn', type: 'choice', options: ['0', '90', '180', '270'], value: '0', group: 'finish' },
|
|
18
|
+
{ key: 'mirror', label: 'Mirror', type: 'toggle', value: false, group: 'finish' },
|
|
19
|
+
{ key: 'round', label: 'Round corners', type: 'range', min: 0, max: 40, step: 1, value: 0, group: 'finish' },
|
|
20
|
+
{ key: 'roundWhich', label: 'Which corners', type: 'choice', options: ['all', 'diagonal', 'top', 'bottom', 'left', 'right', 'alternate', 'inner', 'each-right', 'each-top', 'each-corner'], value: 'all', group: 'finish' },
|
|
21
|
+
{ key: 'outline', label: 'Outline', type: 'range', min: 0, max: 60, step: 1, value: 0, group: 'finish' },
|
|
22
|
+
{ key: 'accent', label: 'Accent dot', type: 'choice', options: ['none', 'end', 'corner', 'inside'], value: 'none', group: 'finish' },
|
|
23
|
+
{ key: 'accentSize', label: 'Dot size', type: 'range', min: 0.8, max: 1.4, step: 0.05, value: 1, group: 'finish' },
|
|
24
|
+
{ key: 'container', label: 'Container', type: 'choice', options: ['none', 'circle', 'square', 'rounded', 'hexagon'], value: 'none', group: 'finish' },
|
|
25
|
+
{ key: 'containerMode', label: 'Container mode', type: 'choice', options: ['badge', 'clip'], value: 'badge', group: 'finish' },
|
|
26
|
+
{ key: 'containerSize', label: 'Mark size inside', type: 'range', min: 0.5, max: 0.9, step: 0.01, value: 0.78, group: 'finish' },
|
|
27
|
+
];
|
|
28
|
+
|
|
29
|
+
/** The mark with the finish applied, in this order: round, outline, accent, mirror, turn, container. */
|
|
30
|
+
export function finish(shape, p) {
|
|
31
|
+
let s = shape;
|
|
32
|
+
if (p.round > 0) s = rounded(s, p.round, p.roundWhich || 'all');
|
|
33
|
+
if (p.outline > 0) s = D(s, grow(s, -p.outline));
|
|
34
|
+
if (p.accent && p.accent !== 'none') s = accent(s, p.accent, p.accentSize || 1);
|
|
35
|
+
if (p.mirror) s = mapPts(s, ([x, y]) => [2 * BOX.cx - x, y]);
|
|
36
|
+
if (p.turn && p.turn !== '0') s = rot(s, Number(p.turn), BOX.cx, BOX.cy);
|
|
37
|
+
if (p.container && p.container !== 'none') s = contain(s, p.container, p.containerMode || 'badge', p.containerSize ?? 0.78);
|
|
38
|
+
return s;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** The container's shape, filling the box. */
|
|
42
|
+
function containerShape(kind) {
|
|
43
|
+
const { x0, y0, size, cx, cy } = BOX;
|
|
44
|
+
if (kind === 'circle') return circle(cx, cy, size / 2, 160);
|
|
45
|
+
if (kind === 'rounded') return rr(x0, y0, size, size, size * 0.22);
|
|
46
|
+
if (kind === 'hexagon') return regular(cx, cy, size / 2, 6, -90);
|
|
47
|
+
return rect(x0, y0, size, size);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** How much of a container's width a mark's box can take and still sit inside it with room round its corners. */
|
|
51
|
+
const INSCRIBE = { circle: 0.74, square: 1, rounded: 0.94, hexagon: 0.8 };
|
|
52
|
+
|
|
53
|
+
/** The mark scaled about the box's centre so its longest side is `side`, centred. */
|
|
54
|
+
function scaledTo(shape, side) {
|
|
55
|
+
const polys = typeof shape[0][0][0] === 'number' ? [shape] : shape;
|
|
56
|
+
const [x0, y0, x1, y1] = boxOf(polys), k = side / Math.max(x1 - x0, y1 - y0, 1e-9);
|
|
57
|
+
const mx = (x0 + x1) / 2, my = (y0 + y1) / 2;
|
|
58
|
+
return mapPts(polys, ([x, y]) => [BOX.cx + (x - mx) * k, BOX.cy + (y - my) * k]);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** The mark in a container: clipped to it, or knocked out of it as a badge. */
|
|
62
|
+
function contain(shape, kind, mode, size) {
|
|
63
|
+
if (!shape || !shape.length) return shape;
|
|
64
|
+
const box = containerShape(kind);
|
|
65
|
+
if (mode === 'clip') return tidy(I(scaledTo(shape, (BOX.size * 0.9) / size), box));
|
|
66
|
+
// badge: the mark sits at `size` of the shape, its pieces knocked out; a mark spilling past the shape's edge would cut
|
|
67
|
+
// it open, so it is kept inside by a house gap. Scaled down, the mark's own gaps would become hairline bridges between
|
|
68
|
+
// the holes, so each hole is shrunk by half of what the scale took off the gap (the bridges get it back). A mark too
|
|
69
|
+
// thin for this leaves hairline holes, which the guard turns away.
|
|
70
|
+
const k = (size * INSCRIBE[kind]);
|
|
71
|
+
const mark = I(scaledTo(shape, BOX.size * k), grow(box, -GAP));
|
|
72
|
+
const back = (GAP * Math.max(0, 1 - k)) / 2;
|
|
73
|
+
return tidy(D(box, back > 0.5 ? grow(mark, -back) : mark));
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** The parts of the box a `which` keeps rounded (the rest keeps its sharp corners). */
|
|
77
|
+
function region(which) {
|
|
78
|
+
const { x0, y0, cx, cy, size } = BOX, h = size / 2 + 40, o = 40;
|
|
79
|
+
if (which === 'top') return rect(x0 - o, y0 - o, size + 2 * o, h);
|
|
80
|
+
if (which === 'bottom') return rect(x0 - o, cy, size + 2 * o, h);
|
|
81
|
+
if (which === 'left') return rect(x0 - o, y0 - o, h, size + 2 * o);
|
|
82
|
+
if (which === 'right') return rect(cx, y0 - o, h, size + 2 * o);
|
|
83
|
+
if (which === 'diagonal') return U(rect(x0 - o, y0 - o, h, h), rect(cx, cy, h, h));
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* An opening that rounds corners but keeps necks: the opening also wipes out any part thinner than 2r (a band's S-bend,
|
|
89
|
+
* a joint), so whatever it took that is bigger than a corner's offcut (about r² × 0.21 for a right angle; anything over
|
|
90
|
+
* r² × 1.2 can't be one) goes back.
|
|
91
|
+
*/
|
|
92
|
+
function open(shape, r) {
|
|
93
|
+
const soft = grow(grow(shape, -r), r);
|
|
94
|
+
const lost = D(shape, soft), keep = [];
|
|
95
|
+
for (const poly of lost) {
|
|
96
|
+
const a = Math.abs(signedArea(poly[0])) - poly.slice(1).reduce((t, h) => t + Math.abs(signedArea(h)), 0);
|
|
97
|
+
if (a > r * r * 1.2) keep.push(poly);
|
|
98
|
+
}
|
|
99
|
+
return keep.length ? U(soft, ...keep.map((q) => grow([q], 0.5))) : soft;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** The shape with some or all of its corners rounded by r. */
|
|
103
|
+
function rounded(shape, r, which) {
|
|
104
|
+
if (which === 'inner') { const c = Math.min(r, 10); return grow(grow(shape, c), -c); }
|
|
105
|
+
const soft = I(open(shape, r), shape);
|
|
106
|
+
if (which === 'all') return soft;
|
|
107
|
+
if (which === 'alternate') {
|
|
108
|
+
// every other piece, in reading order (top to bottom, then left to right)
|
|
109
|
+
const pieces = [...shape].sort((a, b) => top(a) - top(b) || left(a) - left(b));
|
|
110
|
+
return U(...pieces.map((poly, k) => (k % 2 ? [poly] : I(open([poly], r), [poly]))));
|
|
111
|
+
}
|
|
112
|
+
if (which.startsWith('each-')) {
|
|
113
|
+
// each piece rounds only its own right half, top half, or the quarter of its box facing away from the mark's centre
|
|
114
|
+
return U(...[...shape].map((poly) => { const m = pieceRegion([poly], which); return U(I(open([poly], r), [poly], m), D([poly], m)); }));
|
|
115
|
+
}
|
|
116
|
+
const m = region(which);
|
|
117
|
+
return U(I(soft, m), D(shape, m));
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** The box of polygons: [x0, y0, x1, y1]. */
|
|
121
|
+
function boxOf(polys) {
|
|
122
|
+
let x0 = Infinity, y0 = Infinity, x1 = -Infinity, y1 = -Infinity;
|
|
123
|
+
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); }
|
|
124
|
+
return [x0, y0, x1, y1];
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** The part of a piece's own box an each-… rounding keeps rounded. */
|
|
128
|
+
function pieceRegion(piece, which) {
|
|
129
|
+
const [x0, y0, x1, y1] = boxOf(piece), mx = (x0 + x1) / 2, my = (y0 + y1) / 2, o = 4;
|
|
130
|
+
if (which === 'each-right') return rect(mx, y0 - o, x1 - mx + o, y1 - y0 + 2 * o);
|
|
131
|
+
if (which === 'each-top') return rect(x0 - o, y0 - o, x1 - x0 + 2 * o, my - y0 + o);
|
|
132
|
+
const right = mx >= BOX.cx, below = my >= BOX.cy; // each-corner: the quarter facing away from the centre
|
|
133
|
+
return rect(right ? mx : x0 - o, below ? my : y0 - o, (right ? x1 - mx : mx - x0) + o, (below ? y1 - my : my - y0) + o);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** A piece's area, less its holes. */
|
|
137
|
+
const areaOf = (poly) => Math.abs(signedArea(poly[0])) - poly.slice(1).reduce((t, h) => t + Math.abs(signedArea(h)), 0);
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* One accent dot, the mark's own stroke wide (its biggest piece's mean width, 2 × area / perimeter) × `size`: a house gap
|
|
141
|
+
* off the right of the mark at the height of its lowest rightmost ink ('end'), off its bottom-right corner ('corner'),
|
|
142
|
+
* or in its biggest counter ('inside', never a round counter, and only where it fits with a gap all round).
|
|
143
|
+
*/
|
|
144
|
+
function accent(shape, where, size) {
|
|
145
|
+
const polys = !shape.length ? [] : typeof shape[0][0][0] === 'number' ? [shape] : shape;
|
|
146
|
+
if (!polys.length) return shape;
|
|
147
|
+
const big = polys.reduce((a, b) => (areaOf(b) > areaOf(a) ? b : a));
|
|
148
|
+
const stroke = (2 * areaOf(big)) / big.reduce((t, ring) => t + perimeter(ring), 0);
|
|
149
|
+
const r = Math.max(GAP * 0.7, Math.min(stroke * size, 80)) * 0.5 * 1.15;
|
|
150
|
+
const [x0, y0, x1, y1] = boxOf(polys);
|
|
151
|
+
if (where === 'inside') {
|
|
152
|
+
let best = null;
|
|
153
|
+
for (const poly of polys) for (const hole of poly.slice(1)) { const a = Math.abs(signedArea(hole)); if (!best || a > best.a) best = { hole, a }; }
|
|
154
|
+
if (!best) return shape;
|
|
155
|
+
const per = perimeter(best.hole), round = (4 * Math.PI * best.a) / (per * per);
|
|
156
|
+
if (round > 0.8) return shape; // a dot in a round counter is an eye
|
|
157
|
+
const [hx0, hy0, hx1, hy1] = boxOf([[best.hole]]), cx = (hx0 + hx1) / 2, cy = (hy0 + hy1) / 2;
|
|
158
|
+
const fit = Math.min(hx1 - hx0, hy1 - hy0) / 2 - GAP;
|
|
159
|
+
if (fit < GAP * 0.5) return shape;
|
|
160
|
+
const dot = circle(cx, cy, Math.min(r, fit), 64);
|
|
161
|
+
return I(dot, shape).length ? shape : U(shape, D(dot, grow(shape, GAP)));
|
|
162
|
+
}
|
|
163
|
+
if (where === 'corner') return U(shape, circle(x1 + GAP + r * 0.7, y1 + GAP * 0.2 - r * 0.3, r, 64));
|
|
164
|
+
// end: beside the lowest point of the rightmost ink
|
|
165
|
+
let ex = -Infinity, ey = y0;
|
|
166
|
+
for (const poly of polys) for (const [x, y] of poly[0]) if (x > ex - 0.5) { if (x > ex + 0.5 || y > ey) ey = y; ex = Math.max(ex, x); }
|
|
167
|
+
return U(shape, circle(x1 + GAP + r, Math.min(y1 - r, Math.max(y0 + r, ey - r)), r, 64));
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** A polygon's top and left edges, for reading order. */
|
|
171
|
+
const top = (poly) => Math.min(...poly[0].map(([, y]) => y));
|
|
172
|
+
const left = (poly) => Math.min(...poly[0].map(([x]) => x));
|
package/src/generate.js
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
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
|
+
|
|
11
|
+
robust();
|
|
12
|
+
|
|
13
|
+
/** A recipe by id; throws for an unknown one. */
|
|
14
|
+
export function recipe(id) {
|
|
15
|
+
const r = RECIPES[id];
|
|
16
|
+
if (!r) throw new Error(`Unknown recipe "${id}". Known: ${Object.keys(RECIPES).join(', ')}`);
|
|
17
|
+
return r;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* A mark from a recipe and its params, fitted to the Logomarks standard (512 canvas, longest side 448, centred,
|
|
22
|
+
* currentColor). Missing params take their defaults; out-of-range ones are clamped.
|
|
23
|
+
*/
|
|
24
|
+
export function generate(id, params = {}) {
|
|
25
|
+
const r = recipe(id), p = resolve(r.controls, params);
|
|
26
|
+
const shape = r.build(p);
|
|
27
|
+
const found = problems(shape, r.guard);
|
|
28
|
+
if (found.includes('nothing drawn')) return { recipe: id, params: p, svg: '', problems: found };
|
|
29
|
+
const { svg } = fitMark(toSvg(shape));
|
|
30
|
+
return { recipe: id, params: p, svg, pieces: piecesOf(shape), problems: found };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The mark's pieces as SVG path data, one string per piece (its outline and its holes, even-odd), in the fitted 512
|
|
35
|
+
* canvas's coordinates (longest side 448, centred, as `fitMark()` places it), in reading order: by rows of the canvas
|
|
36
|
+
* (top to bottom, rows a twelfth of the box apart), then left to right. For two colours and for motion.
|
|
37
|
+
*/
|
|
38
|
+
export function piecesOf(shape) {
|
|
39
|
+
const polys = !shape || !shape.length ? [] : typeof shape[0][0][0] === 'number' ? [shape] : shape;
|
|
40
|
+
if (!polys.length) return [];
|
|
41
|
+
let x0 = Infinity, y0 = Infinity, x1 = -Infinity, y1 = -Infinity;
|
|
42
|
+
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); }
|
|
43
|
+
const k = 448 / Math.max(x1 - x0, y1 - y0), mx = (x0 + x1) / 2, my = (y0 + y1) / 2;
|
|
44
|
+
const fitted = polys.map((poly) => mapPts(poly, ([x, y]) => [256 + (x - mx) * k, 256 + (y - my) * k]));
|
|
45
|
+
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]; };
|
|
46
|
+
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]));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** The signed area of path data made of straight segments (M, L, Z), every subpath summed (holes run the other way). */
|
|
50
|
+
function areaOfPath(d) {
|
|
51
|
+
let a = 0;
|
|
52
|
+
for (const sub of d.split(/(?=M)/)) {
|
|
53
|
+
const pts = [...sub.matchAll(/(-?[\d.]+)[ ,](-?[\d.]+)/g)].map((m) => [Number(m[1]), Number(m[2])]);
|
|
54
|
+
let s = 0;
|
|
55
|
+
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; }
|
|
56
|
+
a += s / 2;
|
|
57
|
+
}
|
|
58
|
+
return Math.abs(a);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** The top of path data (its smallest y), for rows. */
|
|
62
|
+
function topOf(d) {
|
|
63
|
+
let t = Infinity;
|
|
64
|
+
for (const m of d.matchAll(/(-?[\d.]+)[ ,](-?[\d.]+)/g)) t = Math.min(t, Number(m[2]));
|
|
65
|
+
return t;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** A path's box from its coordinates: [x0, y0, x1, y1]. */
|
|
69
|
+
function boxOfPath(d) {
|
|
70
|
+
const n = (d.match(/-?\d*\.?\d+/g) || []).map(Number);
|
|
71
|
+
let x0 = Infinity, y0 = Infinity, x1 = -Infinity, y1 = -Infinity;
|
|
72
|
+
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]); }
|
|
73
|
+
return [x0, y0, x1, y1];
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** The SVG colour patterns `svgOf` knows, in a picker's order. */
|
|
77
|
+
export const PATTERNS = ['alternate', 'first', 'last', 'rows', 'size', 'smallest', 'inner', 'outer', 'left', 'top', 'gradient'];
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* An SVG of the pieces in two colours: `colors` [base, accent]; `pattern` 'alternate' (every other piece in the accent),
|
|
81
|
+
* 'first' or 'last' (that piece), 'rows' (every other row of pieces), 'size' (the biggest piece), 'smallest' (the
|
|
82
|
+
* smallest: an accent dot), 'inner' (pieces clear of the mark's outer edge), 'outer' (pieces on it), 'left' or 'top'
|
|
83
|
+
* (pieces whose middle lies in that half), or 'gradient' (the whole mark in one gradient from base to accent, top left
|
|
84
|
+
* to bottom right). Each piece is its own even-odd path, in order.
|
|
85
|
+
*/
|
|
86
|
+
export function svgOf(pieces, { colors = ['currentColor', '#c8ff00'], pattern = 'alternate' } = {}) {
|
|
87
|
+
const [base, accent] = colors;
|
|
88
|
+
if (pattern === 'gradient') {
|
|
89
|
+
const paths = pieces.map((d) => `<path fill="url(#lm-grad)" fill-rule="evenodd" d="${d}"/>`).join('');
|
|
90
|
+
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>`;
|
|
91
|
+
}
|
|
92
|
+
const boxes = pieces.map(boxOfPath);
|
|
93
|
+
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]);
|
|
94
|
+
const onEdge = (b) => b[0] <= all[0] + 2 || b[1] <= all[1] + 2 || b[2] >= all[2] - 2 || b[3] >= all[3] - 2;
|
|
95
|
+
let pick = (i) => i % 2 === 1;
|
|
96
|
+
if (pattern === 'first') pick = (i) => i === 0;
|
|
97
|
+
if (pattern === 'last') pick = (i) => i === pieces.length - 1;
|
|
98
|
+
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; }
|
|
99
|
+
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; }
|
|
100
|
+
if (pattern === 'inner') pick = (i) => !onEdge(boxes[i]);
|
|
101
|
+
if (pattern === 'outer') pick = (i) => onEdge(boxes[i]);
|
|
102
|
+
if (pattern === 'left') pick = (i) => (boxes[i][0] + boxes[i][2]) / 2 < (all[0] + all[2]) / 2;
|
|
103
|
+
if (pattern === 'top') pick = (i) => (boxes[i][1] + boxes[i][3]) / 2 < (all[1] + all[3]) / 2;
|
|
104
|
+
if (pattern === 'rows') {
|
|
105
|
+
const rows = [...new Set(pieces.map((d) => Math.round(topOf(d) / (448 / 12))))].sort((a, b) => a - b);
|
|
106
|
+
pick = (i) => rows.indexOf(Math.round(topOf(pieces[i]) / (448 / 12))) % 2 === 1;
|
|
107
|
+
}
|
|
108
|
+
const paths = pieces.map((d, i) => `<path fill="${pick(i) ? accent : base}" fill-rule="evenodd" d="${d}"/>`).join('');
|
|
109
|
+
return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">${paths}</svg>`;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Random params for a recipe from a seed, keeping the values of `locked` keys from `base`. The finish controls (turn,
|
|
114
|
+
* mirror, round, outline) stay at `base` (or their defaults) unless `finish` is true: a shuffle varies the construction.
|
|
115
|
+
*/
|
|
116
|
+
export function randomParams(id, seed, { base = {}, locked = [], finish = false } = {}) {
|
|
117
|
+
const r = recipe(id), rand = rng(seed);
|
|
118
|
+
const out = {};
|
|
119
|
+
for (const c of r.controls) {
|
|
120
|
+
const keep = (locked.includes(c.key) || (c.group === 'finish' && !finish)) && c.key in base;
|
|
121
|
+
const v = randomValue(c, rand); // drawn even when kept, so a seed draws the same construction whatever is locked
|
|
122
|
+
out[c.key] = keep ? base[c.key] : c.group === 'finish' && !finish ? c.value : v;
|
|
123
|
+
}
|
|
124
|
+
return resolve(r.controls, out);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* `count` marks from a recipe, each from its own seed (`seed`, `seed`+1 …), the `locked` controls held at `base`.
|
|
129
|
+
* A draw that fails (the guard finds a speck or a sliver, or the booleans give up) is skipped for the next seed, up to
|
|
130
|
+
* `count` × 4 tries.
|
|
131
|
+
*/
|
|
132
|
+
export function variations(id, { count = 24, seed = 1, base = {}, locked = [], finish = false } = {}) {
|
|
133
|
+
const out = [];
|
|
134
|
+
for (let i = 0, s = seedOf(seed); out.length < count && i < count * 4; i++, s = (s + 1) >>> 0) {
|
|
135
|
+
try {
|
|
136
|
+
const params = randomParams(id, s, { base, locked, finish });
|
|
137
|
+
const g = generate(id, params);
|
|
138
|
+
if (!g.problems.length) out.push({ ...g, seed: s });
|
|
139
|
+
} catch { /* the next seed */ }
|
|
140
|
+
}
|
|
141
|
+
return out;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Params close to `params`: every range nudged by up to `amount` of its span, a choice swapped for another now and then
|
|
146
|
+
* (about `amount` × 1.5 of the time), toggles, the finish and `locked` keys kept. The same seed, the same nudge.
|
|
147
|
+
*/
|
|
148
|
+
export function nearby(id, params, { seed = 1, amount = 0.15, locked = [] } = {}) {
|
|
149
|
+
const r = recipe(id), rand = rng(seed), base = resolve(r.controls, params), out = {};
|
|
150
|
+
for (const c of r.controls) {
|
|
151
|
+
const a = rand(), b = rand();
|
|
152
|
+
if (locked.includes(c.key) || c.group === 'finish' || c.type === 'toggle') { out[c.key] = base[c.key]; continue; }
|
|
153
|
+
if (c.type === 'range') {
|
|
154
|
+
const stepsIn = (c.max - c.min) / c.step;
|
|
155
|
+
// a few whole steps (rows, heights, counts): a nudge would round back, so step by one now and then;
|
|
156
|
+
// a layout number: now and then the next few layouts along
|
|
157
|
+
if (c.key === 'layout') out[c.key] = b < amount * 3 ? base[c.key] + 1 + Math.floor(a * 5) : base[c.key];
|
|
158
|
+
else if (stepsIn <= 8) out[c.key] = b < amount * 2.5 ? base[c.key] + (a < 0.5 ? -c.step : c.step) : base[c.key];
|
|
159
|
+
else out[c.key] = base[c.key] + (a * 2 - 1) * amount * (c.max - c.min);
|
|
160
|
+
}
|
|
161
|
+
else out[c.key] = b < amount * 1.5 ? c.options[Math.floor(a * c.options.length)] : base[c.key];
|
|
162
|
+
}
|
|
163
|
+
return resolve(r.controls, out);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** `count` marks close to `params` (see `nearby()`), each from its own seed, failed draws skipped; none the same as another. */
|
|
167
|
+
export function similar(id, params, { count = 12, seed = 1, amount = 0.15, locked = [] } = {}) {
|
|
168
|
+
const out = [], seen = new Set([JSON.stringify(resolve(recipe(id).controls, params))]), drawn = new Set();
|
|
169
|
+
try { drawn.add(generate(id, params).svg); } catch { /* the base may not draw */ }
|
|
170
|
+
for (let i = 0, s = seedOf(seed); out.length < count && i < count * 5; i++, s = (s + 1) >>> 0) {
|
|
171
|
+
const p = nearby(id, params, { seed: s, amount, locked }), key = JSON.stringify(p);
|
|
172
|
+
if (seen.has(key)) continue;
|
|
173
|
+
seen.add(key);
|
|
174
|
+
// some controls do nothing in some settings (a cut's size with no cut): skip a drawing already made
|
|
175
|
+
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 */ }
|
|
176
|
+
}
|
|
177
|
+
return out;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* `count` marks from ANY recipe: each from its own seed, its recipe picked by that seed (from `ids`, every recipe by
|
|
182
|
+
* default), random params with the finish at its defaults, failed draws skipped. For a bulk "random mix" grid.
|
|
183
|
+
*/
|
|
184
|
+
export function mix({ count = 48, seed = 1, ids = Object.keys(RECIPES) } = {}) {
|
|
185
|
+
const out = [];
|
|
186
|
+
for (let i = 0, s = seedOf(seed); out.length < count && i < count * 4; i++, s = (s + 1) >>> 0) {
|
|
187
|
+
const id = ids[Math.floor(rng(`mix:${s}`)() * ids.length)];
|
|
188
|
+
try {
|
|
189
|
+
const g = generate(id, randomParams(id, s));
|
|
190
|
+
if (!g.problems.length && g.svg) out.push({ ...g, seed: s });
|
|
191
|
+
} catch { /* the next seed */ }
|
|
192
|
+
}
|
|
193
|
+
return out;
|
|
194
|
+
}
|