azulejeria 2.1.0 → 2.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.
Files changed (3) hide show
  1. package/README.md +42 -2
  2. package/package.json +4 -3
  3. 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 its own paint, and
22
- [Knotwork](https://stepinto.dev/art/knotwork.html) is the viewer with the knots below. The design is
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
 
@@ -183,6 +185,44 @@ bands, bold, motif)`, `paintDesign(g, els, pal, hand, lw, seed, painters)`,
183
185
  `makeTile(opts, size, pixelRatio, seed)`, `tileOpts(cfg, state)` and
184
186
  `paintThumb(cfg, preset, g, size, pixelRatio)`.
185
187
 
188
+ ## Pieces
189
+
190
+ `azulejeria/pieces` is the cutter: what happens to a polygon between
191
+ the drawing and the wall. It is the geometry of the hammer and the
192
+ trowel, for [Alicatado](https://stepinto.dev/art/alicatado.html) (faces
193
+ of a star pattern) and [Trencadís](https://stepinto.dev/art/trencadis.html)
194
+ (shards of a broken tile); the glaze on a piece is the page's own.
195
+
196
+ ```js
197
+ import { voronoi, cutPiece, relief, pathPoly } from 'azulejeria/pieces';
198
+
199
+ const cells = voronoi(sites, 0.75); // one convex cell per site
200
+ for (const poly of cells) {
201
+ const { inner } = cutPiece({ poly }, { cut: 0.6, mortar: 0.02 }, rng);
202
+ pathPoly(g, inner); g.fillStyle = glaze; g.fill();
203
+ relief(g, inner, Lx, Ly, 0.007); // a lit edge and a shaded one
204
+ }
205
+ ```
206
+
207
+ **`cutPiece({ poly, area }, { cut, mortar }, rng)`** takes a
208
+ counter-clockwise polygon and returns `{ outer, inner, c }`: the piece
209
+ shifted and turned by the fraction a hand allows, each corner landing
210
+ a touch off the drawing, long edges bowed; then `inner`, the same
211
+ inset by half the `mortar` with a mitred offset, so the joint is one
212
+ width along an edge and at a point. `cut` (0 to 1) is how far it
213
+ strays. The rng is drawn from in a fixed order, so seed it per piece
214
+ and the piece is stable. **`offsetPoly(poly, d)`** is the inset on its
215
+ own.
216
+
217
+ **`voronoi(sites, reach)`** returns one convex cell per `[x, y]`
218
+ site, clipped from a square of half-width `reach` by the bisectors
219
+ with its neighbours; `reach` should be more than any gap between
220
+ neighbouring sites. **`relief(g, poly, Lx, Ly, w)`** strokes each edge
221
+ lit or shaded by where it faces the light. **`grain(seed, size)`** is
222
+ a small speckle tile for mortar. `centroid`, `signedArea`,
223
+ `ensureCCW`, `regularPoly` and `pathPoly` are the small polygon maths
224
+ every page wanted.
225
+
186
226
  ## Knots
187
227
 
188
228
  `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.0",
3
+ "version": "2.2.0",
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/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
+ }