azulejeria 2.1.0 → 2.3.1
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/README.md +47 -5
- package/package.json +4 -3
- package/src/azulejeria.js +17 -10
- package/src/pieces.js +225 -0
package/README.md
CHANGED
|
@@ -18,8 +18,10 @@ rosettes and Mudéjar strapwork), [Jerez](https://stepinto.dev/art/jerez.html)
|
|
|
18
18
|
(pigmented cement, pressed rather than painted). Underneath the wall is a
|
|
19
19
|
viewer, a canvas you paint any world into, with the drag, the pinch, the
|
|
20
20
|
wheel and the page chrome; [Alicatado](https://stepinto.dev/art/alicatado.html)
|
|
21
|
-
(cut tile set in lime) is the viewer with
|
|
22
|
-
[
|
|
21
|
+
(cut tile set in lime) is the viewer with the cutter below,
|
|
22
|
+
[Trencadís](https://stepinto.dev/art/trencadis.html) is the viewer breaking
|
|
23
|
+
Azulejería's tiles with it, and
|
|
24
|
+
[Knotwork](https://stepinto.dev/art/knotwork.html) is the viewer with the knots. The design is
|
|
23
25
|
written up [here](https://stepinto.dev/posts/tiles-painted-by-rule/) and
|
|
24
26
|
[here](https://stepinto.dev/posts/cut-tile-and-poured-cement/).
|
|
25
27
|
|
|
@@ -81,9 +83,11 @@ tap. Drive it from your own controls through `wall.state` and
|
|
|
81
83
|
|
|
82
84
|
## The parts
|
|
83
85
|
|
|
84
|
-
**`createWall(canvas, { palettes, motifs, painters, presets, material })`** returns
|
|
86
|
+
**`createWall(canvas, { palettes, motifs, painters, presets, material, variants })`** returns
|
|
85
87
|
the wall. `wall.state` holds `seed`, `repeat` (`module`, `rotate`,
|
|
86
|
-
`single` or `
|
|
88
|
+
`single`, `checker`, or `whole` for a tile that is a picture, every one
|
|
89
|
+
laid the same way up; `variants` is how many paintings of the design
|
|
90
|
+
such a wall draws from, four by default), `pal`, `motif`, `density` (1 to 5), `hand` (0 to
|
|
87
91
|
1, how far the brush drifts), `brush` (line weight), `scale` (pixels per
|
|
88
92
|
tile), `panX`, `panY`, `bands`, `grout` and `age`. After changing any of
|
|
89
93
|
them call `wall.rebuild(true)` to repaint the tiles, or `wall.rebuild(false)`
|
|
@@ -179,10 +183,48 @@ diamond), `ring` (a scalloped ring around the main corner), `lattice` (a
|
|
|
179
183
|
line edge to edge that mirrors into a lozenge net) and `dots`.
|
|
180
184
|
|
|
181
185
|
**The lower level** is there if you want it: `makeDesign(seed, density,
|
|
182
|
-
bands, bold, motif)`, `paintDesign(g, els, pal, hand, lw, seed, painters)`,
|
|
186
|
+
bands, bold, motif)`, `paintDesign(g, els, pal, hand, lw, seed, painters, material, brush)`,
|
|
183
187
|
`makeTile(opts, size, pixelRatio, seed)`, `tileOpts(cfg, state)` and
|
|
184
188
|
`paintThumb(cfg, preset, g, size, pixelRatio)`.
|
|
185
189
|
|
|
190
|
+
## Pieces
|
|
191
|
+
|
|
192
|
+
`azulejeria/pieces` is the cutter: what happens to a polygon between
|
|
193
|
+
the drawing and the wall. It is the geometry of the hammer and the
|
|
194
|
+
trowel, for [Alicatado](https://stepinto.dev/art/alicatado.html) (faces
|
|
195
|
+
of a star pattern) and [Trencadís](https://stepinto.dev/art/trencadis.html)
|
|
196
|
+
(shards of a broken tile); the glaze on a piece is the page's own.
|
|
197
|
+
|
|
198
|
+
```js
|
|
199
|
+
import { voronoi, cutPiece, relief, pathPoly } from 'azulejeria/pieces';
|
|
200
|
+
|
|
201
|
+
const cells = voronoi(sites, 0.75); // one convex cell per site
|
|
202
|
+
for (const poly of cells) {
|
|
203
|
+
const { inner } = cutPiece({ poly }, { cut: 0.6, mortar: 0.02 }, rng);
|
|
204
|
+
pathPoly(g, inner); g.fillStyle = glaze; g.fill();
|
|
205
|
+
relief(g, inner, Lx, Ly, 0.007); // a lit edge and a shaded one
|
|
206
|
+
}
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
**`cutPiece({ poly, area }, { cut, mortar }, rng)`** takes a
|
|
210
|
+
counter-clockwise polygon and returns `{ outer, inner, c }`: the piece
|
|
211
|
+
shifted and turned by the fraction a hand allows, each corner landing
|
|
212
|
+
a touch off the drawing, long edges bowed; then `inner`, the same
|
|
213
|
+
inset by half the `mortar` with a mitred offset, so the joint is one
|
|
214
|
+
width along an edge and at a point. `cut` (0 to 1) is how far it
|
|
215
|
+
strays. The rng is drawn from in a fixed order, so seed it per piece
|
|
216
|
+
and the piece is stable. **`offsetPoly(poly, d)`** is the inset on its
|
|
217
|
+
own.
|
|
218
|
+
|
|
219
|
+
**`voronoi(sites, reach)`** returns one convex cell per `[x, y]`
|
|
220
|
+
site, clipped from a square of half-width `reach` by the bisectors
|
|
221
|
+
with its neighbours; `reach` should be more than any gap between
|
|
222
|
+
neighbouring sites. **`relief(g, poly, Lx, Ly, w)`** strokes each edge
|
|
223
|
+
lit or shaded by where it faces the light. **`grain(seed, size)`** is
|
|
224
|
+
a small speckle tile for mortar. `centroid`, `signedArea`,
|
|
225
|
+
`ensureCCW`, `regularPoly` and `pathPoly` are the small polygon maths
|
|
226
|
+
every page wanted.
|
|
227
|
+
|
|
186
228
|
## Knots
|
|
187
229
|
|
|
188
230
|
`azulejeria/knots` is interlace by rule, for the
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "azulejeria",
|
|
3
|
-
"version": "2.1
|
|
3
|
+
"version": "2.3.1",
|
|
4
4
|
"description": "Tin-glazed tiles painted by rule. A seeded canvas engine for Spanish azulejo walls: a diagonal-symmetric design, brushwork that drifts, washes that pool, and four ways to repeat the tile.",
|
|
5
|
-
"keywords": ["canvas", "generative", "tiles", "azulejo", "mudejar", "sevilla", "pattern", "procedural", "art", "wallpaper", "interlace", "knotwork", "celtic"],
|
|
5
|
+
"keywords": ["canvas", "generative", "tiles", "azulejo", "mudejar", "sevilla", "pattern", "procedural", "art", "wallpaper", "interlace", "knotwork", "celtic", "mosaic", "voronoi", "trencadis"],
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"author": "Andy Cross",
|
|
8
8
|
"homepage": "https://stepinto.dev/art/azulejeria.html",
|
|
@@ -12,11 +12,12 @@
|
|
|
12
12
|
"exports": {
|
|
13
13
|
".": "./src/azulejeria.js",
|
|
14
14
|
"./knots": "./src/knots.js",
|
|
15
|
+
"./pieces": "./src/pieces.js",
|
|
15
16
|
"./azulejeria.css": "./src/azulejeria.css"
|
|
16
17
|
},
|
|
17
18
|
"files": ["src", "README.md", "LICENSE"],
|
|
18
19
|
"sideEffects": false,
|
|
19
20
|
"scripts": {
|
|
20
|
-
"test": "node --check src/azulejeria.js && node --check src/knots.js && node --check src/head.js"
|
|
21
|
+
"test": "node --check src/azulejeria.js && node --check src/knots.js && node --check src/head.js && node --check src/pieces.js"
|
|
21
22
|
}
|
|
22
23
|
}
|
package/src/azulejeria.js
CHANGED
|
@@ -219,14 +219,16 @@ export const PAINTERS = {
|
|
|
219
219
|
|
|
220
220
|
// paints one design into the unit square of g (already scaled). The
|
|
221
221
|
// material, if given, may supply `ampOf(hand)`, `shape(g, pts, col, b)`,
|
|
222
|
-
// `line(g, pts, lw, close, b)` and `dot(g, x, y, r, col, a, b)`.
|
|
223
|
-
|
|
222
|
+
// `line(g, pts, lw, close, b)` and `dot(g, x, y, r, col, a, b)`. The
|
|
223
|
+
// brush `b` also carries `hand` and `brush` as the state has them, for
|
|
224
|
+
// a painter whose line is not drawn with `lw` (a traced drawing, say).
|
|
225
|
+
export function paintDesign(g, els, pal, hand, lw, jseed, painters, material, brush) {
|
|
224
226
|
const rng = mulberry32(jseed);
|
|
225
227
|
const m = material || {};
|
|
226
228
|
const amp = m.ampOf ? m.ampOf(hand) : 0.012 * hand;
|
|
227
229
|
const Q = Math.PI / 2, D = Q / 2;
|
|
228
230
|
const dc = Math.cos(D), ds = Math.sin(D); // the diagonal
|
|
229
|
-
const b = { g, pal, lw, amp, rng, Q, D, dc, ds, col: pal.wash };
|
|
231
|
+
const b = { g, pal, lw, amp, rng, Q, D, dc, ds, col: pal.wash, hand, brush: brush === undefined ? 1 : brush };
|
|
230
232
|
const shape = m.shape
|
|
231
233
|
? (pts, col) => m.shape(g, pts, col, b)
|
|
232
234
|
: (pts, col) => { wash(g, pts, col, rng); ink(g, pts, lw, pal, rng, true); };
|
|
@@ -262,7 +264,9 @@ export function paintDesign(g, els, pal, hand, lw, jseed, painters, material) {
|
|
|
262
264
|
// --------------------------------------------------------------- tile ---
|
|
263
265
|
// One tile, painted. `o.quarter` lays the design four times mirrored
|
|
264
266
|
// about the centre; otherwise the design fills the tile with its main
|
|
265
|
-
// corner at bottom right, for the module to complete
|
|
267
|
+
// corner at bottom right, for the module to complete (or, with the
|
|
268
|
+
// 'whole' repeat, every tile laid the same way up: a tile that is a
|
|
269
|
+
// picture). `o.plain` paints
|
|
266
270
|
// the small tile a checker alternates with.
|
|
267
271
|
export function makeTile(o, S, k, jseed) {
|
|
268
272
|
const c = document.createElement('canvas');
|
|
@@ -305,18 +309,18 @@ export function makeTile(o, S, k, jseed) {
|
|
|
305
309
|
let v = 0;
|
|
306
310
|
for (const [fx, fy] of [[1, 1], [-1, 1], [1, -1], [-1, -1]]) {
|
|
307
311
|
g.save(); g.translate(S / 2, S / 2); g.scale(fx * S / 2, fy * S / 2);
|
|
308
|
-
paintDesign(g, o.design, pal, o.hand, 0.024 * o.brush, jseed + 17 + v++, o.painters, o.material);
|
|
312
|
+
paintDesign(g, o.design, pal, o.hand, 0.024 * o.brush, jseed + 17 + v++, o.painters, o.material, o.brush);
|
|
309
313
|
g.restore();
|
|
310
314
|
}
|
|
311
315
|
} else {
|
|
312
316
|
g.save(); g.translate(S, S); g.scale(-S, -S);
|
|
313
|
-
paintDesign(g, o.design, pal, o.hand, 0.012 * o.brush, jseed + 17, o.painters, o.material);
|
|
317
|
+
paintDesign(g, o.design, pal, o.hand, 0.012 * o.brush, jseed + 17, o.painters, o.material, o.brush);
|
|
314
318
|
g.restore();
|
|
315
319
|
}
|
|
316
320
|
return c;
|
|
317
321
|
}
|
|
318
322
|
|
|
319
|
-
export const REPEATS = ['module', 'rotate', 'single', 'checker'];
|
|
323
|
+
export const REPEATS = ['module', 'rotate', 'single', 'checker', 'whole'];
|
|
320
324
|
const ROT = [0, Math.PI / 2, 3 * Math.PI / 2, Math.PI];
|
|
321
325
|
|
|
322
326
|
// the tile options for a given state (preset or live)
|
|
@@ -468,8 +472,10 @@ export function createViewer(canvas, opts) {
|
|
|
468
472
|
}
|
|
469
473
|
|
|
470
474
|
// --------------------------------------------------------------- wall ---
|
|
471
|
-
// createWall(canvas, { palettes, motifs, painters, presets, material
|
|
472
|
-
// is a viewer that paints a wall of tiles.
|
|
475
|
+
// createWall(canvas, { palettes, motifs, painters, presets, material,
|
|
476
|
+
// variants }) is a viewer that paints a wall of tiles. `variants` is how
|
|
477
|
+
// many paintings of the design the wall draws from (four unless told
|
|
478
|
+
// otherwise; a wall of whole tiles, each its own picture, wants more). It returns the wall, which
|
|
473
479
|
// is the viewer with more on it: `state` to read or change, then
|
|
474
480
|
// `rebuild(true)` to repaint the tiles or `rebuild(false)` to redraw
|
|
475
481
|
// the wall with the tiles it has. `applyPreset(name)`, `newDesign()`
|
|
@@ -485,6 +491,7 @@ export function createWall(canvas, cfg) {
|
|
|
485
491
|
const painters = cfg.painters || PAINTERS;
|
|
486
492
|
const material = cfg.material || null, m = material || {};
|
|
487
493
|
const lay = m.lay || { shift: 0.014, turn: 0.016 };
|
|
494
|
+
const variants = cfg.variants || 4;
|
|
488
495
|
const firstMotif = Object.keys(motifs)[0];
|
|
489
496
|
for (const p of Object.values(presets)) if (!p.motif) p.motif = firstMotif;
|
|
490
497
|
|
|
@@ -511,7 +518,7 @@ export function createWall(canvas, cfg) {
|
|
|
511
518
|
const o = tileOpts(tcfg, state);
|
|
512
519
|
tiles.A = [];
|
|
513
520
|
tiles.B = [];
|
|
514
|
-
for (let v = 0; v <
|
|
521
|
+
for (let v = 0; v < variants; v++) tiles.A.push(makeTile(o, state.scale, DPR, state.seed * 31 + v));
|
|
515
522
|
if (state.repeat === 'checker')
|
|
516
523
|
for (let v = 0; v < 3; v++) tiles.B.push(makeTile({ ...o, plain: true }, state.scale, DPR, state.seed * 37 + v));
|
|
517
524
|
needTiles = false;
|
package/src/pieces.js
ADDED
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
// pieces — the cutter, for azulejeria.
|
|
2
|
+
//
|
|
3
|
+
// A wall of cut tile is a plane of small polygons bedded in mortar. The
|
|
4
|
+
// polygons come from somewhere (Alicatado's star geometry, Trencadís's
|
|
5
|
+
// cracks), and this module is what happens to them afterwards: each one
|
|
6
|
+
// is moved and turned by the fraction a hand allows, its corners land a
|
|
7
|
+
// touch off the drawing, its long edges bow, and then it is inset from
|
|
8
|
+
// the joint by a mitred offset so the mortar between two pieces is the
|
|
9
|
+
// same width along a long edge as at a point. There is a Voronoi here
|
|
10
|
+
// too, for pieces that are broken rather than cut, and the relief that
|
|
11
|
+
// lights one edge of a standing piece and shades the other.
|
|
12
|
+
//
|
|
13
|
+
// Nothing here paints a glaze. The piece's fill, its mottle and its
|
|
14
|
+
// shine belong to the page; this module is the geometry of the hammer
|
|
15
|
+
// and the trowel.
|
|
16
|
+
|
|
17
|
+
import { mulberry32 } from './azulejeria.js';
|
|
18
|
+
|
|
19
|
+
// ------------------------------------------------------------- polygons ---
|
|
20
|
+
// Polygons are arrays of [x, y]. The cutter wants them counter-clockwise.
|
|
21
|
+
|
|
22
|
+
export function centroid(poly) {
|
|
23
|
+
let x = 0, y = 0;
|
|
24
|
+
for (const p of poly) { x += p[0]; y += p[1]; }
|
|
25
|
+
return [x / poly.length, y / poly.length];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function signedArea(poly) {
|
|
29
|
+
let a = 0;
|
|
30
|
+
for (let i = 0; i < poly.length; i++) {
|
|
31
|
+
const p = poly[i], q = poly[(i + 1) % poly.length];
|
|
32
|
+
a += p[0] * q[1] - q[0] * p[1];
|
|
33
|
+
}
|
|
34
|
+
return a / 2;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function ensureCCW(poly) {
|
|
38
|
+
return signedArea(poly) < 0 ? poly.slice().reverse() : poly;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function regularPoly(cx, cy, n, r, a0) {
|
|
42
|
+
const p = [];
|
|
43
|
+
for (let k = 0; k < n; k++) {
|
|
44
|
+
const a = a0 + k * 2 * Math.PI / n;
|
|
45
|
+
p.push([cx + r * Math.cos(a), cy + r * Math.sin(a)]);
|
|
46
|
+
}
|
|
47
|
+
return p;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function pathPoly(g, poly) {
|
|
51
|
+
g.beginPath();
|
|
52
|
+
g.moveTo(poly[0][0], poly[0][1]);
|
|
53
|
+
for (let i = 1; i < poly.length; i++) g.lineTo(poly[i][0], poly[i][1]);
|
|
54
|
+
g.closePath();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// a CCW polygon inset by d: each vertex moves along the bisector of
|
|
58
|
+
// its two inward normals, by d over the cosine of the half angle, and
|
|
59
|
+
// never further than half the shorter edge, so a sharp point stays a point
|
|
60
|
+
export function offsetPoly(poly, d) {
|
|
61
|
+
const n = poly.length, out = [];
|
|
62
|
+
for (let i = 0; i < n; i++) {
|
|
63
|
+
const p = poly[(i + n - 1) % n], v = poly[i], q = poly[(i + 1) % n];
|
|
64
|
+
let e1x = v[0] - p[0], e1y = v[1] - p[1], l1 = Math.hypot(e1x, e1y) || 1;
|
|
65
|
+
let e2x = q[0] - v[0], e2y = q[1] - v[1], l2 = Math.hypot(e2x, e2y) || 1;
|
|
66
|
+
e1x /= l1; e1y /= l1; e2x /= l2; e2y /= l2;
|
|
67
|
+
// inward normals, polygon is CCW
|
|
68
|
+
const n1x = -e1y, n1y = e1x, n2x = -e2y, n2y = e2x;
|
|
69
|
+
let bx = n1x + n2x, by = n1y + n2y, bl = Math.hypot(bx, by);
|
|
70
|
+
if (bl < 1e-6) { bx = n1x; by = n1y; bl = 1; }
|
|
71
|
+
bx /= bl; by /= bl;
|
|
72
|
+
const cosHalf = bx * n1x + by * n1y;
|
|
73
|
+
const m = Math.min(d / Math.max(cosHalf, 0.2), Math.min(l1, l2) * 0.49);
|
|
74
|
+
out.push([v[0] + bx * m, v[1] + by * m]);
|
|
75
|
+
}
|
|
76
|
+
return out;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// --------------------------------------------------------------- cutter ---
|
|
80
|
+
// cutPiece({ poly, area }, { cut, mortar }, rng) turns a face into a
|
|
81
|
+
// piece: shifted and turned a little, its corners nicked, its long
|
|
82
|
+
// edges bowed, then inset by half the mortar. `cut` (0..1) is how far
|
|
83
|
+
// the piece strays from its drawing, `mortar` the joint width in the
|
|
84
|
+
// polygon's units. Returns { outer, inner, c }: the piece as laid, the
|
|
85
|
+
// glazed face inside the joint, and the centre it was turned about.
|
|
86
|
+
// The rng is drawn from in a fixed order, so a page that seeds it per
|
|
87
|
+
// piece gets the same piece every time.
|
|
88
|
+
export function cutPiece(pc, st, rng) {
|
|
89
|
+
const c = centroid(pc.poly);
|
|
90
|
+
const cut = st.cut;
|
|
91
|
+
// the whole piece strays: a shift and a turn
|
|
92
|
+
const dx = (rng() - 0.5) * 0.02 * cut, dy = (rng() - 0.5) * 0.02 * cut;
|
|
93
|
+
const ang = (rng() - 0.5) * 0.03 * cut, ca = Math.cos(ang), sa = Math.sin(ang);
|
|
94
|
+
const poly = pc.poly.map(p => {
|
|
95
|
+
const x = p[0] - c[0], y = p[1] - c[1];
|
|
96
|
+
// each corner lands a touch off the drawing
|
|
97
|
+
const jx = (rng() - 0.5) * 0.016 * cut, jy = (rng() - 0.5) * 0.016 * cut;
|
|
98
|
+
return [c[0] + dx + x * ca - y * sa + jx, c[1] + dy + x * sa + y * ca + jy];
|
|
99
|
+
});
|
|
100
|
+
// long edges on a hand-cut piece bow very slightly
|
|
101
|
+
const bowed = [];
|
|
102
|
+
for (let i = 0; i < poly.length; i++) {
|
|
103
|
+
const a = poly[i], b = poly[(i + 1) % poly.length];
|
|
104
|
+
bowed.push(a);
|
|
105
|
+
const len = Math.hypot(b[0] - a[0], b[1] - a[1]);
|
|
106
|
+
if (len > 0.5) {
|
|
107
|
+
const mx = (a[0] + b[0]) / 2, my = (a[1] + b[1]) / 2;
|
|
108
|
+
const nx = -(b[1] - a[1]) / len, ny = (b[0] - a[0]) / len;
|
|
109
|
+
const bow = (rng() - 0.5) * 0.012 * cut;
|
|
110
|
+
bowed.push([mx + nx * bow, my + ny * bow]);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
const area = pc.area === undefined ? Math.abs(signedArea(pc.poly)) : pc.area;
|
|
114
|
+
const half = st.mortar * (0.5 + (rng() - 0.5) * 0.3 * cut);
|
|
115
|
+
const inset = offsetPoly(bowed, Math.min(half, 0.3 * Math.sqrt(area)));
|
|
116
|
+
return { outer: bowed, inner: inset, c };
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// --------------------------------------------------------------- relief ---
|
|
120
|
+
// A piece stands proud of the mortar, so each edge of its face is lit
|
|
121
|
+
// or shaded by where it faces. (Lx, Ly) is the unit vector towards the
|
|
122
|
+
// light, `w` the stroke width; `lit` and `shade` are the colours at
|
|
123
|
+
// full strength, as `rgba(r, g, b, ` prefixes.
|
|
124
|
+
export function relief(g, poly, Lx, Ly, w, lit, shade) {
|
|
125
|
+
lit = lit || 'rgba(255, 250, 235, ';
|
|
126
|
+
shade = shade || 'rgba(30, 20, 10, ';
|
|
127
|
+
g.lineCap = 'butt';
|
|
128
|
+
g.lineWidth = w;
|
|
129
|
+
for (let i = 0; i < poly.length; i++) {
|
|
130
|
+
const a = poly[i], b = poly[(i + 1) % poly.length];
|
|
131
|
+
const ex = b[0] - a[0], ey = b[1] - a[1], el = Math.hypot(ex, ey) || 1;
|
|
132
|
+
const nx = ey / el, ny = -ex / el; // outward, polygon is CCW
|
|
133
|
+
const f = nx * Lx + ny * Ly;
|
|
134
|
+
g.strokeStyle = f > 0 ? lit + (0.45 * f) + ')' : shade + (0.4 * -f) + ')';
|
|
135
|
+
g.beginPath(); g.moveTo(a[0], a[1]); g.lineTo(b[0], b[1]); g.stroke();
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// ---------------------------------------------------------------- grain ---
|
|
140
|
+
// a small tile of speckle to pattern mortar with, dark and light flecks
|
|
141
|
+
let grainCache = new Map();
|
|
142
|
+
export function grain(seed, size, dots) {
|
|
143
|
+
seed = seed === undefined ? 99 : seed;
|
|
144
|
+
const s = size || 96, n = dots || 900, k = seed + '|' + s + '|' + n;
|
|
145
|
+
let c = grainCache.get(k);
|
|
146
|
+
if (c) return c;
|
|
147
|
+
c = document.createElement('canvas');
|
|
148
|
+
c.width = c.height = s;
|
|
149
|
+
const g = c.getContext('2d');
|
|
150
|
+
const r = mulberry32(seed);
|
|
151
|
+
for (let i = 0; i < n; i++) {
|
|
152
|
+
const x = r() * s, y = r() * s, rad = 0.4 + r() * 1.1;
|
|
153
|
+
g.fillStyle = r() < 0.55 ? `rgba(60, 45, 25, ${0.05 + r() * 0.12})` : `rgba(255, 250, 235, ${0.08 + r() * 0.16})`;
|
|
154
|
+
g.beginPath(); g.arc(x, y, rad, 0, 6.283); g.fill();
|
|
155
|
+
}
|
|
156
|
+
grainCache.set(k, c);
|
|
157
|
+
return c;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// -------------------------------------------------------------- voronoi ---
|
|
161
|
+
// Pieces that are broken rather than cut. voronoi(sites, reach) takes a
|
|
162
|
+
// list of [x, y] and returns one convex CCW cell per site, clipped from
|
|
163
|
+
// a square of half-width `reach` around it by the bisector with every
|
|
164
|
+
// site that could touch it. Sites are bucketed on a grid of `reach`, so
|
|
165
|
+
// it is fast when the sites are roughly evenly spread; cells at the
|
|
166
|
+
// edge of the set are closed by the square. `reach` should be larger
|
|
167
|
+
// than any gap between neighbouring sites.
|
|
168
|
+
export function voronoi(sites, reach) {
|
|
169
|
+
const cellOf = p => Math.floor(p[0] / reach) + ',' + Math.floor(p[1] / reach);
|
|
170
|
+
const buckets = new Map();
|
|
171
|
+
sites.forEach((p, i) => {
|
|
172
|
+
const k = cellOf(p);
|
|
173
|
+
let b = buckets.get(k);
|
|
174
|
+
if (!b) { b = []; buckets.set(k, b); }
|
|
175
|
+
b.push(i);
|
|
176
|
+
});
|
|
177
|
+
const cells = [];
|
|
178
|
+
for (let i = 0; i < sites.length; i++) {
|
|
179
|
+
const s = sites[i];
|
|
180
|
+
const bx = Math.floor(s[0] / reach), by = Math.floor(s[1] / reach);
|
|
181
|
+
let poly = [[s[0] - reach, s[1] - reach], [s[0] + reach, s[1] - reach], [s[0] + reach, s[1] + reach], [s[0] - reach, s[1] + reach]];
|
|
182
|
+
// the neighbours, nearest first, so most clips are early and the
|
|
183
|
+
// rest can stop once the cell is already inside the next bisector
|
|
184
|
+
const near = [];
|
|
185
|
+
for (let a = -1; a <= 1; a++) for (let b = -1; b <= 1; b++) {
|
|
186
|
+
const list = buckets.get((bx + a) + ',' + (by + b));
|
|
187
|
+
if (list) for (const j of list) if (j !== i) near.push(j);
|
|
188
|
+
}
|
|
189
|
+
near.sort((a, b) => dist2(s, sites[a]) - dist2(s, sites[b]));
|
|
190
|
+
for (const j of near) {
|
|
191
|
+
const q = sites[j];
|
|
192
|
+
const d = Math.sqrt(dist2(s, q));
|
|
193
|
+
// the cell lies within its own farthest vertex: a site more than
|
|
194
|
+
// twice that away cannot cut it
|
|
195
|
+
let far = 0;
|
|
196
|
+
for (const p of poly) far = Math.max(far, dist2(s, p));
|
|
197
|
+
if (d * d > 4 * far) break;
|
|
198
|
+
poly = clipHalf(poly, s, q);
|
|
199
|
+
if (poly.length < 3) break;
|
|
200
|
+
}
|
|
201
|
+
cells.push(poly);
|
|
202
|
+
}
|
|
203
|
+
return cells;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const dist2 = (a, b) => (a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2;
|
|
207
|
+
|
|
208
|
+
// keep the side of the bisector of s and q that holds s
|
|
209
|
+
function clipHalf(poly, s, q) {
|
|
210
|
+
const mx = (s[0] + q[0]) / 2, my = (s[1] + q[1]) / 2;
|
|
211
|
+
const nx = s[0] - q[0], ny = s[1] - q[1];
|
|
212
|
+
const inside = p => (p[0] - mx) * nx + (p[1] - my) * ny >= 0;
|
|
213
|
+
const out = [];
|
|
214
|
+
for (let i = 0; i < poly.length; i++) {
|
|
215
|
+
const a = poly[i], b = poly[(i + 1) % poly.length];
|
|
216
|
+
const ia = inside(a), ib = inside(b);
|
|
217
|
+
if (ia) out.push(a);
|
|
218
|
+
if (ia !== ib) {
|
|
219
|
+
const da = (a[0] - mx) * nx + (a[1] - my) * ny, db = (b[0] - mx) * nx + (b[1] - my) * ny;
|
|
220
|
+
const t = da / (da - db);
|
|
221
|
+
out.push([a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t]);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return out;
|
|
225
|
+
}
|