azulejeria 2.0.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.
package/README.md CHANGED
@@ -18,7 +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. 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
22
25
  written up [here](https://stepinto.dev/posts/tiles-painted-by-rule/) and
23
26
  [here](https://stepinto.dev/posts/cut-tile-and-poured-cement/).
24
27
 
@@ -182,6 +185,101 @@ bands, bold, motif)`, `paintDesign(g, els, pal, hand, lw, seed, painters)`,
182
185
  `makeTile(opts, size, pixelRatio, seed)`, `tileOpts(cfg, state)` and
183
186
  `paintThumb(cfg, preset, g, size, pixelRatio)`.
184
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
+
226
+ ## Knots
227
+
228
+ `azulejeria/knots` is interlace by rule, for the
229
+ [Knotwork](https://stepinto.dev/art/knotwork.html) page and anything
230
+ else that wants a strand to go over and under. It draws on any 2D
231
+ context and uses nothing from the wall.
232
+
233
+ ```js
234
+ import { buildKnot, paintKnot, masks } from 'azulejeria/knots';
235
+ import { mulberry32 } from 'azulejeria';
236
+
237
+ const knot = buildKnot({ nx: 8, ny: 8, rng: mulberry32(698), breaks: 0.3, cuts: 0.05 });
238
+ paintKnot(g, knot, {
239
+ place: (x, y) => [x / 2, y / 2], // doubled grid point -> where to draw it
240
+ ribbon: 0.34, inkWidth: 0.045, ink: '#2a1e14', ground: '#efe1c0',
241
+ colour: s => ['#d9a62e', '#c8562e', '#4f8a5e'][Math.floor(s.pig * 3)],
242
+ bends: 'corners', heads: true,
243
+ });
244
+ ```
245
+
246
+ The knot lives on a grid of squares in doubled coordinates: corners
247
+ at (even, even), edge midpoints at (odd, even) and (even, odd). A
248
+ strand steps diagonally from midpoint to midpoint, so two cross at
249
+ every midpoint. A break is an edge the strand turns back off; the rim
250
+ of the region is all breaks, so every strand closes. A cut is a break
251
+ the strand ends at instead, and the two ends that meet at a cut become
252
+ a beast, one end a head biting the other. No two cuts sit side by
253
+ side, so the heads have room. Over and under come from
254
+ parity (a horizontal edge takes the rising strand over, a vertical one
255
+ the falling), which alternates round every strand without walking it.
256
+
257
+ **`buildKnot({ nx, ny, rng, breaks, runs, cuts, mask, symmetry })`**:
258
+ `breaks` is the chance an interior edge is a break, `runs` the chance
259
+ a break carries on along its line for another edge or two (which is
260
+ what gives a knot corners), `cuts` the chance a break is a cut.
261
+ They are chosen a whole orbit of the grid's mirrors at a time:
262
+ `symmetry` is `'d4'` (the default for a square grid), `'d2'` or
263
+ `'none'`. `mask(i, j)` says which squares are in the region, so a
264
+ knot can fill a cross or the field around one; `masks.plus`,
265
+ `masks.grow` and `masks.not` make the carpet-page shapes. It returns
266
+ `{ strands, beasts, edge, square }`; each strand has its `pts` (kind
267
+ `cross` with `over`, `wall`, `rim` or `end`), whether it is `open`,
268
+ and `pig`, a seeded number for choosing its colour.
269
+
270
+ **`paintKnot(g, knot, opts)`**: `place(x, y)` maps a grid point to
271
+ the context (add the hand's drift there), `ribbon` and `inkWidth` are
272
+ in the context's units, `colour(strand)` gives each core, `ground` is
273
+ the fill style for the gap cut either side of an over strand, `split`
274
+ puts a line down the middle of the ribbon, `bends` is `'round'`,
275
+ `'corners'` (pointed where a strand turns in a corner of the rim) or
276
+ `'pointed'` (every bounce off the rim), and `heads` draws the beasts.
277
+ A beast's head is traced from a drawing of an Insular hound's head
278
+ (`src/head.js`): it turns off its strand on a narrower neck (`neck`,
279
+ 0.55 of the ribbon) and lies along the cut facing the other end, which
280
+ hooks round under it and into its jaws. `eye` colours its eye.
281
+ `strandPath` and `ribbon` are exported for drawing a strand yourself.
282
+
185
283
  ## Licence
186
284
 
187
285
  MIT.
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "azulejeria",
3
- "version": "2.0.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"],
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",
@@ -11,11 +11,13 @@
11
11
  "main": "./src/azulejeria.js",
12
12
  "exports": {
13
13
  ".": "./src/azulejeria.js",
14
+ "./knots": "./src/knots.js",
15
+ "./pieces": "./src/pieces.js",
14
16
  "./azulejeria.css": "./src/azulejeria.css"
15
17
  },
16
18
  "files": ["src", "README.md", "LICENSE"],
17
19
  "sideEffects": false,
18
20
  "scripts": {
19
- "test": "node --check src/azulejeria.js"
21
+ "test": "node --check src/azulejeria.js && node --check src/knots.js && node --check src/head.js && node --check src/pieces.js"
20
22
  }
21
23
  }
@@ -53,6 +53,18 @@ canvas:active { cursor: grabbing; }
53
53
  to { opacity: 1; filter: blur(0); transform: translateY(0); }
54
54
  }
55
55
 
56
+ /* the way back to the art index, top left, blooming in after the title */
57
+ #back {
58
+ position: fixed; top: 1.1rem; left: 1.3rem; z-index: 11;
59
+ font-family: "Georgia", serif; font-style: italic;
60
+ font-size: 0.85rem; letter-spacing: 0.08em;
61
+ color: var(--az-whisper); text-decoration: none;
62
+ text-shadow: 0 1px 8px rgba(0, 0, 0, 0.6);
63
+ opacity: 0; animation: bloom 2s ease forwards 2s;
64
+ transition: color 0.6s ease;
65
+ }
66
+ #back:hover { color: var(--az-title); }
67
+
56
68
  #hint {
57
69
  position: fixed; bottom: 1.2rem; left: 0; right: 0; text-align: center;
58
70
  font-family: "Courier New", monospace; font-size: 0.68rem;
package/src/head.js ADDED
@@ -0,0 +1,13 @@
1
+ // The beast's head for knots.js, traced from a flat drawing of an
2
+ // Insular hound's head (1254px, simplified to 2.5px, three decimals) by
3
+ // tools/trace-charge.js. Loops in a unit box, x times asp for the true
4
+ // width, facing right with the crown up. main is the silhouette (fill
5
+ // even-odd, the open mouth is a hole), red the eye, blue the piece of
6
+ // strand held in the jaws, sable the ink. The neck leaves the left edge
7
+ // centred at y with width w; the mouth is where the bitten strand runs.
8
+ export const HEAD = { asp: 1.473, neck: { y: 0.402, w: 0.226 }, mouth: [0.846, 0.412],
9
+ main: [[[0.308,0],[0.339,0.001],[0.364,0.008],[0.407,0.038],[0.442,0.087],[0.455,0.121],[0.46,0.152],[0.503,0.141],[0.544,0.141],[0.588,0.149],[0.67,0.18],[0.804,0.251],[0.881,0.275],[0.909,0.275],[0.93,0.269],[0.917,0.254],[0.913,0.243],[0.911,0.211],[0.918,0.189],[0.927,0.178],[0.937,0.171],[0.953,0.167],[0.97,0.172],[0.987,0.191],[0.996,0.212],[1,0.238],[0.997,0.279],[0.984,0.314],[0.971,0.331],[0.954,0.346],[0.914,0.359],[0.911,0.383],[0.9,0.419],[0.881,0.447],[0.873,0.449],[0.87,0.445],[0.864,0.494],[0.877,0.5],[0.891,0.498],[0.898,0.485],[0.9,0.458],[0.909,0.454],[0.925,0.471],[0.933,0.495],[0.933,0.523],[0.923,0.552],[0.907,0.573],[0.888,0.584],[0.853,0.589],[0.851,0.585],[0.855,0.578],[0.786,0.55],[0.781,0.55],[0.783,0.562],[0.778,0.589],[0.77,0.589],[0.773,0.553],[0.761,0.55],[0.707,0.551],[0.614,0.57],[0.576,0.569],[0.531,0.553],[0.497,0.524],[0.417,0.494],[0.376,0.487],[0.334,0.487],[0.29,0.495],[0.308,0.528],[0.325,0.545],[0.417,0.602],[0.463,0.642],[0.477,0.661],[0.494,0.697],[0.511,0.763],[0.512,0.814],[0.506,0.863],[0.493,0.904],[0.471,0.943],[0.447,0.97],[0.414,0.992],[0.384,1],[0.339,0.996],[0.296,0.971],[0.278,0.95],[0.26,0.92],[0.25,0.891],[0.244,0.848],[0.245,0.817],[0.25,0.785],[0.265,0.749],[0.277,0.732],[0.298,0.712],[0.317,0.704],[0.341,0.703],[0.367,0.716],[0.383,0.737],[0.391,0.76],[0.393,0.796],[0.387,0.82],[0.372,0.845],[0.357,0.854],[0.338,0.856],[0.321,0.843],[0.32,0.848],[0.329,0.875],[0.344,0.892],[0.366,0.9],[0.394,0.894],[0.41,0.881],[0.423,0.863],[0.432,0.84],[0.437,0.812],[0.437,0.778],[0.432,0.749],[0.422,0.722],[0.41,0.704],[0.385,0.683],[0.314,0.661],[0.264,0.63],[0.245,0.609],[0.223,0.575],[0.202,0.513],[0,0.513],[0.001,0.29],[0.107,0.289],[0.142,0.284],[0.196,0.263],[0.237,0.235],[0.264,0.186],[0.243,0.182],[0.231,0.172],[0.22,0.157],[0.211,0.124],[0.215,0.08],[0.226,0.055],[0.25,0.025],[0.276,0.008],[0.308,0.001]],[[0.786,0.371],[0.75,0.38],[0.689,0.407],[0.684,0.424],[0.685,0.439],[0.689,0.453],[0.736,0.426],[0.757,0.426],[0.787,0.437],[0.799,0.396],[0.794,0.374],[0.787,0.371]]],
10
+ red: [[[0.594,0.269],[0.601,0.271],[0.589,0.285],[0.587,0.307],[0.591,0.32],[0.603,0.331],[0.615,0.331],[0.629,0.313],[0.629,0.288],[0.618,0.271],[0.638,0.278],[0.644,0.283],[0.646,0.29],[0.646,0.311],[0.635,0.337],[0.618,0.343],[0.586,0.34],[0.581,0.336],[0.574,0.32],[0.572,0.292],[0.575,0.277],[0.594,0.271]],[[0.563,0.283],[0.563,0.308],[0.566,0.325],[0.541,0.301],[0.563,0.284]],[[0.655,0.292],[0.673,0.311],[0.651,0.328],[0.655,0.294]]],
11
+ blue: [[[0.853,0.36],[0.876,0.36],[0.88,0.392],[0.871,0.393],[0.865,0.409],[0.852,0.484],[0.848,0.485],[0.82,0.468],[0.799,0.45],[0.807,0.41],[0.822,0.365],[0.853,0.362]]],
12
+ sable: [[[0.304,0.001],[0.355,0.006],[0.384,0.019],[0.406,0.038],[0.442,0.089],[0.454,0.12],[0.46,0.153],[0.512,0.141],[0.585,0.149],[0.671,0.181],[0.805,0.252],[0.876,0.275],[0.905,0.277],[0.931,0.269],[0.914,0.246],[0.911,0.216],[0.918,0.191],[0.928,0.178],[0.942,0.17],[0.962,0.17],[0.981,0.183],[0.993,0.204],[0.999,0.234],[0.997,0.277],[0.984,0.313],[0.956,0.343],[0.914,0.358],[0.908,0.396],[0.898,0.422],[0.881,0.447],[0.869,0.447],[0.863,0.483],[0.865,0.495],[0.879,0.501],[0.891,0.499],[0.898,0.488],[0.9,0.46],[0.905,0.454],[0.919,0.464],[0.932,0.494],[0.932,0.524],[0.923,0.551],[0.909,0.57],[0.889,0.583],[0.861,0.584],[0.859,0.589],[0.853,0.589],[0.851,0.585],[0.855,0.578],[0.786,0.55],[0.781,0.55],[0.783,0.562],[0.778,0.589],[0.771,0.589],[0.774,0.553],[0.757,0.549],[0.719,0.549],[0.627,0.568],[0.581,0.569],[0.53,0.552],[0.499,0.524],[0.419,0.494],[0.371,0.485],[0.328,0.487],[0.291,0.494],[0.289,0.498],[0.296,0.511],[0.322,0.544],[0.414,0.601],[0.446,0.625],[0.467,0.648],[0.494,0.698],[0.504,0.729],[0.511,0.768],[0.512,0.819],[0.503,0.871],[0.488,0.914],[0.463,0.953],[0.436,0.978],[0.405,0.994],[0.376,1],[0.334,0.994],[0.312,0.983],[0.289,0.964],[0.271,0.939],[0.257,0.91],[0.245,0.852],[0.245,0.824],[0.25,0.788],[0.264,0.751],[0.278,0.731],[0.297,0.714],[0.32,0.704],[0.343,0.704],[0.358,0.71],[0.376,0.727],[0.389,0.752],[0.393,0.778],[0.39,0.811],[0.381,0.831],[0.363,0.851],[0.337,0.854],[0.32,0.842],[0.321,0.856],[0.334,0.882],[0.351,0.897],[0.381,0.9],[0.395,0.894],[0.411,0.881],[0.425,0.859],[0.433,0.836],[0.437,0.809],[0.437,0.78],[0.432,0.746],[0.412,0.705],[0.386,0.683],[0.313,0.66],[0.266,0.631],[0.23,0.586],[0.214,0.553],[0.201,0.512],[0.156,0.516],[0.001,0.515],[0.001,0.291],[0.099,0.29],[0.14,0.285],[0.195,0.265],[0.238,0.235],[0.264,0.184],[0.241,0.18],[0.22,0.155],[0.212,0.125],[0.215,0.083],[0.226,0.056],[0.25,0.025],[0.278,0.008],[0.304,0.002]],[[0.306,0.018],[0.267,0.033],[0.248,0.049],[0.235,0.067],[0.223,0.1],[0.222,0.118],[0.227,0.141],[0.236,0.155],[0.249,0.166],[0.268,0.167],[0.282,0.159],[0.301,0.123],[0.295,0.107],[0.286,0.093],[0.275,0.087],[0.273,0.079],[0.292,0.066],[0.329,0.063],[0.348,0.07],[0.367,0.085],[0.393,0.126],[0.403,0.167],[0.404,0.198],[0.4,0.223],[0.394,0.245],[0.386,0.25],[0.383,0.24],[0.391,0.208],[0.392,0.176],[0.386,0.144],[0.377,0.123],[0.362,0.101],[0.345,0.087],[0.32,0.079],[0.296,0.083],[0.309,0.108],[0.315,0.13],[0.321,0.193],[0.329,0.229],[0.344,0.273],[0.367,0.311],[0.395,0.336],[0.428,0.347],[0.45,0.346],[0.452,0.342],[0.452,0.328],[0.446,0.308],[0.435,0.294],[0.418,0.283],[0.418,0.275],[0.436,0.245],[0.447,0.2],[0.448,0.153],[0.438,0.11],[0.413,0.067],[0.379,0.035],[0.34,0.019],[0.307,0.018]],[[0.305,0.144],[0.249,0.24],[0.228,0.288],[0.216,0.328],[0.208,0.369],[0.203,0.415],[0.205,0.461],[0.212,0.504],[0.233,0.566],[0.247,0.589],[0.273,0.618],[0.315,0.643],[0.388,0.666],[0.421,0.694],[0.434,0.717],[0.443,0.744],[0.448,0.773],[0.449,0.808],[0.439,0.858],[0.416,0.897],[0.4,0.91],[0.381,0.917],[0.361,0.917],[0.344,0.911],[0.329,0.898],[0.319,0.883],[0.311,0.862],[0.308,0.847],[0.311,0.811],[0.315,0.801],[0.32,0.801],[0.323,0.82],[0.33,0.833],[0.342,0.839],[0.359,0.835],[0.377,0.811],[0.381,0.79],[0.38,0.765],[0.367,0.737],[0.352,0.725],[0.342,0.721],[0.311,0.725],[0.286,0.744],[0.271,0.767],[0.261,0.794],[0.256,0.823],[0.256,0.853],[0.264,0.892],[0.274,0.919],[0.288,0.941],[0.319,0.968],[0.353,0.982],[0.396,0.979],[0.439,0.956],[0.471,0.917],[0.489,0.876],[0.5,0.819],[0.5,0.778],[0.494,0.739],[0.484,0.706],[0.47,0.676],[0.434,0.635],[0.333,0.572],[0.312,0.555],[0.287,0.523],[0.268,0.476],[0.261,0.442],[0.26,0.405],[0.269,0.353],[0.29,0.296],[0.322,0.251],[0.311,0.199],[0.305,0.146]],[[0.513,0.158],[0.479,0.164],[0.46,0.174],[0.454,0.228],[0.436,0.273],[0.448,0.286],[0.46,0.307],[0.464,0.33],[0.46,0.36],[0.424,0.364],[0.387,0.35],[0.36,0.324],[0.327,0.268],[0.302,0.301],[0.281,0.353],[0.272,0.405],[0.275,0.456],[0.283,0.478],[0.328,0.47],[0.372,0.468],[0.425,0.478],[0.478,0.498],[0.464,0.472],[0.456,0.443],[0.456,0.408],[0.464,0.403],[0.466,0.407],[0.466,0.437],[0.473,0.461],[0.5,0.505],[0.533,0.535],[0.583,0.552],[0.618,0.552],[0.645,0.546],[0.628,0.505],[0.624,0.462],[0.633,0.428],[0.653,0.402],[0.687,0.382],[0.764,0.358],[0.836,0.345],[0.916,0.341],[0.944,0.333],[0.962,0.319],[0.979,0.295],[0.988,0.262],[0.987,0.227],[0.979,0.205],[0.97,0.193],[0.956,0.186],[0.941,0.188],[0.925,0.206],[0.923,0.235],[0.937,0.252],[0.946,0.252],[0.956,0.24],[0.961,0.24],[0.963,0.25],[0.957,0.266],[0.945,0.28],[0.926,0.29],[0.874,0.292],[0.789,0.263],[0.688,0.208],[0.634,0.183],[0.568,0.163],[0.514,0.158]],[[0.598,0.214],[0.626,0.216],[0.651,0.227],[0.708,0.283],[0.734,0.299],[0.734,0.306],[0.73,0.309],[0.71,0.303],[0.646,0.243],[0.608,0.231],[0.581,0.234],[0.562,0.243],[0.512,0.283],[0.485,0.292],[0.481,0.286],[0.484,0.279],[0.506,0.268],[0.549,0.232],[0.579,0.217],[0.598,0.215]],[[0.599,0.251],[0.624,0.254],[0.641,0.261],[0.701,0.316],[0.698,0.325],[0.68,0.326],[0.63,0.358],[0.597,0.36],[0.576,0.353],[0.558,0.34],[0.535,0.314],[0.516,0.308],[0.514,0.3],[0.566,0.262],[0.599,0.252]],[[0.223,0.265],[0.18,0.29],[0.14,0.302],[0.096,0.307],[0.012,0.307],[0.012,0.499],[0.197,0.495],[0.192,0.43],[0.195,0.373],[0.205,0.322],[0.224,0.265]],[[0.595,0.269],[0.575,0.277],[0.572,0.306],[0.582,0.337],[0.6,0.343],[0.618,0.343],[0.635,0.337],[0.644,0.318],[0.646,0.29],[0.644,0.283],[0.638,0.278],[0.618,0.271],[0.629,0.288],[0.629,0.313],[0.615,0.331],[0.603,0.331],[0.591,0.32],[0.587,0.307],[0.589,0.285],[0.601,0.271],[0.596,0.269]],[[0.564,0.283],[0.541,0.3],[0.566,0.325],[0.563,0.308],[0.564,0.284]],[[0.656,0.292],[0.651,0.328],[0.673,0.312],[0.657,0.292]],[[0.876,0.359],[0.806,0.368],[0.814,0.392],[0.807,0.41],[0.8,0.454],[0.848,0.485],[0.853,0.482],[0.859,0.433],[0.87,0.394],[0.881,0.392],[0.882,0.422],[0.897,0.39],[0.901,0.36],[0.877,0.359]],[[0.787,0.37],[0.748,0.38],[0.689,0.405],[0.684,0.42],[0.684,0.437],[0.689,0.454],[0.721,0.432],[0.741,0.426],[0.759,0.427],[0.787,0.438],[0.799,0.398],[0.795,0.374],[0.788,0.37]],[[0.672,0.408],[0.647,0.43],[0.636,0.462],[0.638,0.499],[0.647,0.521],[0.662,0.541],[0.72,0.532],[0.765,0.533],[0.801,0.541],[0.865,0.568],[0.893,0.563],[0.905,0.553],[0.916,0.538],[0.922,0.518],[0.922,0.5],[0.913,0.478],[0.904,0.504],[0.889,0.518],[0.876,0.518],[0.859,0.511],[0.778,0.459],[0.754,0.449],[0.725,0.451],[0.694,0.477],[0.687,0.478],[0.681,0.473],[0.673,0.453],[0.673,0.408]]],
13
+ };
package/src/knots.js ADDED
@@ -0,0 +1,399 @@
1
+ // knots — interlace by rule on a grid, for azulejeria.
2
+ //
3
+ // Take a region of squares. Every edge of every square has a midpoint,
4
+ // and a strand runs diagonally from one midpoint to the next, so at
5
+ // every midpoint two strands cross. Some edges are breaks: a strand
6
+ // that reaches a break turns back off it instead of crossing. The rim
7
+ // of the region is all breaks, so every strand closes. A cut is a break
8
+ // the strand does not turn back off but ends at, and the two ends that
9
+ // meet there become a beast: one end a head, the other the tail it bites.
10
+ //
11
+ // Over and under need no bookkeeping. A crossing on a horizontal edge
12
+ // takes the rising strand over, a crossing on a vertical edge the
13
+ // falling one. Every step along a strand changes the kind of edge and
14
+ // every bounce changes the direction, so the over-under alternates
15
+ // round every strand by construction.
16
+ //
17
+ // Coordinates are doubled: corners of squares at (even, even), edge
18
+ // midpoints at (odd, even) on horizontal edges and (even, odd) on
19
+ // vertical ones. `place(x, y)` maps them to wherever you are drawing.
20
+
21
+ import { HEAD } from './head.js';
22
+
23
+ const key = (x, y) => x * 4096 + y;
24
+ const mid = (a, b) => [(a[0] + b[0]) / 2, (a[1] + b[1]) / 2];
25
+ const lerp = (a, b, t) => [a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t];
26
+ const TAU = Math.PI * 2;
27
+
28
+ // ---------------------------------------------------------------- masks ---
29
+ // A mask says which squares (i, j) are in the region. These make the
30
+ // shapes a carpet page wants: a plus, its surround, the channel between.
31
+ export const masks = {
32
+ all: () => true,
33
+ // a plus centred in an n by m grid: arms `half` squares either side of
34
+ // the centre line, reaching `reach` squares from the centre
35
+ plus(nx, ny, half, reach) {
36
+ const cx = nx / 2, cy = ny / 2;
37
+ return (i, j) => {
38
+ const x = i + 0.5 - cx, y = j + 0.5 - cy;
39
+ return (Math.abs(x) < half && Math.abs(y) < reach) || (Math.abs(y) < half && Math.abs(x) < reach);
40
+ };
41
+ },
42
+ // grow a mask by one square in each direction
43
+ grow: mask => (i, j) => {
44
+ for (let a = -1; a <= 1; a++) for (let b = -1; b <= 1; b++) if (mask(i + a, j + b)) return true;
45
+ return false;
46
+ },
47
+ not: mask => (i, j) => !mask(i, j),
48
+ };
49
+
50
+ // ----------------------------------------------------------------- knot ---
51
+ // buildKnot({ nx, ny, rng, breaks, runs, cuts, mask, symmetry }) grows a
52
+ // knot in an nx by ny grid. `breaks` is the chance an interior edge is
53
+ // a break, `runs` the chance a break runs on along its line for one or
54
+ // two more edges, `cuts` the chance a break (or a stretch of rim) is a
55
+ // cut. Breaks and cuts are chosen a whole orbit of the grid's mirrors at
56
+ // a time: 'd4' (both mirrors and the diagonal, square grids), 'd2'
57
+ // (both mirrors) or 'none'. Returns { nx, ny, strands, beasts, edge,
58
+ // square }: each strand is { pts, open, pig } with pts of kind 'cross'
59
+ // (with `over`), 'wall', 'rim' or 'end'; each beast pairs a head end
60
+ // with a tail end; `edge(x, y)` and `square(i, j)` describe the region.
61
+ export function buildKnot(o) {
62
+ const { nx, ny, rng } = o;
63
+ const breaks = o.breaks ?? 0.3, runs = o.runs ?? 0.45, cuts = o.cuts ?? 0;
64
+ const inside = o.mask || masks.all;
65
+ const sym = o.symmetry || (nx === ny ? 'd4' : 'd2');
66
+ const X = 2 * nx, Y = 2 * ny;
67
+ const sq = (i, j) => i >= 0 && j >= 0 && i < nx && j < ny && inside(i, j);
68
+ // 2 for an edge between two squares of the region, 1 for its rim, 0 outside
69
+ const edge = (x, y) => {
70
+ const [a, b] = (x & 1) ? [sq((x - 1) / 2, y / 2 - 1), sq((x - 1) / 2, y / 2)]
71
+ : [sq(x / 2 - 1, (y - 1) / 2), sq(x / 2, (y - 1) / 2)];
72
+ return a && b ? 2 : a || b ? 1 : 0;
73
+ };
74
+ const orbit = (x, y) => {
75
+ const pts = [[x, y]];
76
+ if (sym !== 'none') pts.push([X - x, y], [x, Y - y], [X - x, Y - y]);
77
+ if (sym === 'd4') for (const [a, b] of pts.slice()) pts.push([b, a]);
78
+ return pts;
79
+ };
80
+ const midpoints = fn => { for (let y = 0; y <= Y; y++) for (let x = 0; x <= X; x++) if ((x + y) & 1) fn(x, y); };
81
+
82
+ const wall = new Set(), cut = new Set();
83
+ let seen = new Set();
84
+ midpoints((x, y) => {
85
+ if (edge(x, y) !== 2 || seen.has(key(x, y))) return;
86
+ for (const [a, b] of orbit(x, y)) seen.add(key(a, b));
87
+ if (rng() >= breaks) return;
88
+ const run = rng() < runs ? 2 + Math.floor(rng() * 2) : 1;
89
+ const hz = (x & 1) === 1; // a horizontal edge runs along x
90
+ for (let k = 0; k < run; k++) {
91
+ const ax = hz ? x + 2 * k : x, ay = hz ? y : y + 2 * k;
92
+ if (ax > 0 && ax < X && ay > 0 && ay < Y && edge(ax, ay) === 2)
93
+ for (const [a, b] of orbit(ax, ay)) if (edge(a, b) === 2) wall.add(key(a, b));
94
+ }
95
+ });
96
+ const isWall = (x, y) => { const e = edge(x, y); return e === 1 || (e === 2 && wall.has(key(x, y))); };
97
+ if (cuts > 0) {
98
+ seen = new Set();
99
+ midpoints((x, y) => {
100
+ if (!isWall(x, y) || seen.has(key(x, y))) return;
101
+ for (const [a, b] of orbit(x, y)) seen.add(key(a, b));
102
+ if (rng() >= cuts) return;
103
+ // no cut beside another, or the heads would lie on each other; a
104
+ // mirror image that would is left a plain break
105
+ for (const [a, b] of orbit(x, y)) {
106
+ if (!isWall(a, b)) continue;
107
+ let clear = true;
108
+ for (let u = -2; u <= 2 && clear; u++) for (let v = -2; v <= 2; v++)
109
+ if (Math.abs(u) + Math.abs(v) <= 2 && cut.has(key(a + u, b + v))) { clear = false; break; }
110
+ if (clear) cut.add(key(a, b));
111
+ }
112
+ });
113
+ }
114
+
115
+ // walk from a crossing until the strand comes round or reaches a cut
116
+ const done = new Set();
117
+ const axis = (dx, dy) => (dx * dy > 0 ? 0 : 1);
118
+ function walk(sx, sy, sdx, sdy) {
119
+ const pts = [];
120
+ let x = sx, y = sy, dx = sdx, dy = sdy, guard = 0;
121
+ do {
122
+ if (isWall(x, y)) {
123
+ if (cut.has(key(x, y))) { pts.push({ x, y, kind: 'end' }); return { pts, open: true }; }
124
+ if (x & 1) dy = -dy; else dx = -dx;
125
+ pts.push({ x, y, kind: edge(x, y) === 1 ? 'rim' : 'wall' });
126
+ } else {
127
+ done.add(key(x, y) * 2 + axis(dx, dy));
128
+ pts.push({ x, y, kind: 'cross', over: ((x & 1) === 1) === (dx * dy > 0) });
129
+ }
130
+ x += dx; y += dy;
131
+ } while (!(x === sx && y === sy && dx === sdx && dy === sdy) && ++guard < 200000);
132
+ return { pts, open: false };
133
+ }
134
+
135
+ const strands = [];
136
+ midpoints((x, y) => {
137
+ if (edge(x, y) !== 2 || isWall(x, y)) return;
138
+ for (const ax of [0, 1]) {
139
+ if (done.has(key(x, y) * 2 + ax)) continue;
140
+ const dy = ax === 0 ? 1 : -1;
141
+ const f = walk(x, y, 1, dy);
142
+ if (!f.open) { strands.push({ pts: f.pts, open: false, pig: rng() }); continue; }
143
+ const b = walk(x, y, -1, -dy);
144
+ strands.push({ pts: b.pts.slice(1).reverse().concat(f.pts), open: true, pig: rng() });
145
+ }
146
+ });
147
+
148
+ // the two ends that meet at a cut from the same side make one beast
149
+ const ends = new Map();
150
+ strands.forEach((s, si) => {
151
+ if (!s.open) return;
152
+ for (const i of [0, s.pts.length - 1]) {
153
+ const w = s.pts[i], nb = s.pts[i === 0 ? 1 : i - 1];
154
+ const side = (w.x & 1) ? nb.y > w.y : nb.x > w.x;
155
+ const k = key(w.x, w.y) * 2 + (side ? 1 : 0);
156
+ if (!ends.has(k)) ends.set(k, []);
157
+ ends.get(k).push({ s: si, i });
158
+ }
159
+ });
160
+ const beasts = [];
161
+ for (const pair of ends.values()) {
162
+ if (pair.length !== 2) continue;
163
+ const flip = rng() < 0.5;
164
+ beasts.push({ head: pair[flip ? 1 : 0], tail: pair[flip ? 0 : 1] });
165
+ }
166
+ return { nx, ny, strands, beasts, edge, square: sq };
167
+ }
168
+
169
+ // ---------------------------------------------------------------- paths ---
170
+ // The strand as a Path2D through the placed points. Every point is a
171
+ // control point and the curve passes through the midpoints between
172
+ // them, so a crossing is straight and a bounce is rounded. `bends`:
173
+ // 'round' leaves it so; 'corners' points the strand where it turns in a
174
+ // convex corner of the rim, as the scribes did; 'pointed' also points
175
+ // every bounce off the rim.
176
+ export function strandPath(knot, s, P, bends, place) {
177
+ const pts = s.pts, n = pts.length, p = new Path2D();
178
+ const at = k => P[(k + n) % n];
179
+ const convex = (A, B) => {
180
+ if ((A.x & 1) === (B.x & 1)) return null;
181
+ const cx = (A.x & 1) ? B.x : A.x, cy = (A.x & 1) ? A.y : B.y;
182
+ const i = cx / 2, j = cy / 2;
183
+ const c = knot.square(i - 1, j - 1) + knot.square(i, j - 1) + knot.square(i - 1, j) + knot.square(i, j);
184
+ return c === 1 ? [cx, cy] : null;
185
+ };
186
+ const first = s.open ? 1 : 0, last = s.open ? n - 2 : n - 1;
187
+ const start = s.open ? mid(P[0], P[1]) : mid(P[n - 1], P[0]);
188
+ p.moveTo(start[0], start[1]);
189
+ for (let k = first; k <= last; k++) {
190
+ const q = pts[k], e = mid(at(k), at(k + 1));
191
+ if (bends !== 'round' && q.kind === 'rim' && k + 1 <= last && pts[k + 1].kind === 'rim') {
192
+ const c = convex(q, pts[k + 1]);
193
+ if (c) {
194
+ // a point into the corner: arrive along one rim, leave along the other
195
+ // a point into the corner: arrive up one rim and leave along the
196
+ // other, each leaning in toward the corner, so the tip is sharp
197
+ const A = at(k), B = at(k + 1), C = place(c[0], c[1]);
198
+ const V = lerp(C, mid(A, B), 0.75);
199
+ const u = (x, y) => { const l = Math.hypot(x, y) || 1; return [x / l, y / l]; };
200
+ const t = u(C[0] - V[0], C[1] - V[1]), ra = u(C[0] - A[0], C[1] - A[1]), rb = u(B[0] - C[0], B[1] - C[1]);
201
+ const din = u(ra[0] + t[0], ra[1] + t[1]), dout = u(rb[0] - t[0], rb[1] - t[1]);
202
+ const len = Math.hypot(A[0] - C[0], A[1] - C[1]) * 0.7;
203
+ const c1 = lerp(mid(at(k - 1), A), A, 0.8), c4 = lerp(mid(B, at(k + 2)), B, 0.8);
204
+ const c2 = [V[0] - din[0] * len, V[1] - din[1] * len], c3 = [V[0] + dout[0] * len, V[1] + dout[1] * len];
205
+ const e2 = mid(B, at(k + 2));
206
+ p.bezierCurveTo(c1[0], c1[1], c2[0], c2[1], V[0], V[1]);
207
+ p.bezierCurveTo(c3[0], c3[1], c4[0], c4[1], e2[0], e2[1]);
208
+ k++;
209
+ continue;
210
+ }
211
+ }
212
+ if (bends === 'pointed' && q.kind === 'rim') {
213
+ // a point just short of the rim, the curve either side of it
214
+ // keeping its tangent where it meets the rest of the strand
215
+ const A = at(k), s0 = mid(at(k - 1), A);
216
+ const V = lerp(A, mid(at(k - 1), at(k + 1)), 0.12);
217
+ const c1 = lerp(s0, A, 0.55), c2 = lerp(e, A, 0.55);
218
+ p.quadraticCurveTo(c1[0], c1[1], V[0], V[1]);
219
+ p.quadraticCurveTo(c2[0], c2[1], e[0], e[1]);
220
+ continue;
221
+ }
222
+ p.quadraticCurveTo(at(k)[0], at(k)[1], e[0], e[1]);
223
+ }
224
+ if (!s.open) p.closePath();
225
+ return p;
226
+ }
227
+ // the piece of a strand either side of point k, for relaying a crossing
228
+ function piecePath(P, k) {
229
+ const n = P.length, p = new Path2D();
230
+ const s = mid(P[(k + n - 1) % n], P[k]), e = mid(P[k], P[(k + 1) % n]);
231
+ p.moveTo(s[0], s[1]); p.quadraticCurveTo(P[k][0], P[k][1], e[0], e[1]);
232
+ return p;
233
+ }
234
+
235
+ // ---------------------------------------------------------------- paint ---
236
+ // a ribbon: an ink edge either side and a core of colour, and for a
237
+ // split ribbon a line of ink down the middle
238
+ export function ribbon(g, path, col, o) {
239
+ g.strokeStyle = o.ink; g.lineWidth = o.ribbon; g.stroke(path);
240
+ g.strokeStyle = col; g.lineWidth = Math.max(o.ribbon - 2 * o.inkWidth, o.inkWidth); g.stroke(path);
241
+ if (o.split) { g.strokeStyle = o.ink; g.lineWidth = o.inkWidth * 0.75; g.stroke(path); }
242
+ }
243
+
244
+ // paintKnot(g, knot, opts) draws the whole knot. opts: `place(x, y)`
245
+ // maps a doubled grid point to [x, y] on g (add the hand's drift there);
246
+ // `ribbon` and `inkWidth` in g's units; `ink`; `colour(strand)` for each
247
+ // strand's core; `ground`, a fill style for the gap cut either side of
248
+ // an over strand; `split`; `bends` ('round', 'corners', 'pointed');
249
+ // `heads` to draw the beasts at the cuts, `neck` for how narrow their
250
+ // necks are against the ribbon, `eye` for the colour of their eyes.
251
+ export function paintKnot(g, knot, o) {
252
+ const rw = o.ribbon, iw = o.inkWidth;
253
+ const drawn = knot.strands.map(s => {
254
+ const P = s.pts.map(q => o.place(q.x, q.y));
255
+ return { s, P, col: o.colour(s), path: strandPath(knot, s, P, o.bends || 'round', o.place) };
256
+ });
257
+ g.lineJoin = 'miter'; g.miterLimit = 4; g.lineCap = 'butt';
258
+ for (const d of drawn) ribbon(g, d.path, d.col, o);
259
+
260
+ // at every crossing the strand over is laid again across a gap: the
261
+ // gap is cut inside one circle and the strand relaid inside a slightly
262
+ // larger one, so the relay covers the gap's soft edge
263
+ const gap = Math.max(iw * 0.8, rw * 0.12);
264
+ const unit = Math.hypot(drawn[0] ? drawn[0].P[0][0] - drawn[0].P[1][0] : 1, drawn[0] ? drawn[0].P[0][1] - drawn[0].P[1][1] : 1);
265
+ const R = Math.hypot(rw / 2, rw / 2 + gap) * 1.08, R2 = Math.min(R + rw * 0.18, unit * 0.5);
266
+ for (const d of drawn) {
267
+ d.s.pts.forEach((q, k) => {
268
+ if (!q.over) return;
269
+ const [cx, cy] = d.P[k], piece = piecePath(d.P, k);
270
+ g.save(); g.beginPath(); g.arc(cx, cy, R, 0, TAU); g.clip();
271
+ g.strokeStyle = o.ground; g.lineWidth = rw + 2 * gap; g.stroke(piece);
272
+ g.restore();
273
+ g.save(); g.beginPath(); g.arc(cx, cy, R2, 0, TAU); g.clip();
274
+ ribbon(g, piece, d.col, o);
275
+ g.restore();
276
+ });
277
+ }
278
+
279
+ if (o.heads) for (const b of knot.beasts) beast(g, drawn, b, o);
280
+ }
281
+
282
+ // ---------------------------------------------------------------- beast ---
283
+ // Where a strand is cut, its two ends meet from the same side. One
284
+ // becomes a head and the other a tail, curled round into its jaws. The
285
+ // head is traced (see head.js), a hound's head with an almond eye and a
286
+ // lappet, laid on in the strand's own colour. Its neck turns off the
287
+ // strand, narrowing to `o.neck` of the ribbon (0.55), so the head lies
288
+ // along the cut facing the tail, crown away from the cut.
289
+ function beast(g, drawn, b, o) {
290
+ const r = o.ribbon, iw = o.inkWidth;
291
+ const endOf = ({ s, i }) => {
292
+ const d = drawn[s], P = d.P, nb = i === 0 ? 1 : P.length - 2;
293
+ const E = mid(P[i], P[nb]);
294
+ const dl = Math.hypot(P[i][0] - P[nb][0], P[i][1] - P[nb][1]) || 1;
295
+ return { d, E, dir: [(P[i][0] - P[nb][0]) / dl, (P[i][1] - P[nb][1]) / dl] };
296
+ };
297
+ const H = endOf(b.head), T = endOf(b.tail);
298
+ // the head lies along the cut, facing the tail's end, its crown away
299
+ // from the cut and its jaws toward it
300
+ const ax = T.E[0] - H.E[0], ay = T.E[1] - H.E[1], al = Math.hypot(ax, ay) || 1;
301
+ const d = [ax / al, ay / al];
302
+ let up = [-d[1], d[0]];
303
+ if (up[0] * H.dir[0] + up[1] * H.dir[1] > 0) up = [-up[0], -up[1]];
304
+ const down = [-up[0], -up[1]];
305
+ const k = o.neck || 0.55, S = k * r / HEAD.neck.w;
306
+ const N = [H.E[0] + (H.dir[0] * 0.3 + d[0] * 0.35) * r, H.E[1] + (H.dir[1] * 0.3 + d[1] * 0.35) * r];
307
+ // a point of the drawing, (x, y) in its unit box, placed in the world
308
+ const at = (x, y) => {
309
+ const X = x * HEAD.asp * S, Y = (y - HEAD.neck.y) * S;
310
+ return [N[0] + d[0] * X + down[0] * Y, N[1] + d[1] * X + down[1] * Y];
311
+ };
312
+ const M = at(HEAD.mouth[0], HEAD.mouth[1]);
313
+
314
+ // the tail: on from its end toward the cut, then hooked round under
315
+ // the jaws and up into the mouth, narrowing as it goes, laid first so
316
+ // the head closes over it
317
+ {
318
+ const tip = [M[0] + up[0] * k * r * 0.6, M[1] + up[1] * k * r * 0.6];
319
+ const c1 = [T.E[0] + T.dir[0] * r * 1.2, T.E[1] + T.dir[1] * r * 1.2];
320
+ const c2 = [M[0] + down[0] * r * 1.6, M[1] + down[1] * r * 1.6];
321
+ taper(g, T.E, c1, c2, tip, r, k * r * 0.55, T.d.col, o, true);
322
+ }
323
+
324
+ // the head, its silhouette stroked wide first so its outline weighs
325
+ // the same as the strands', then the colours, then the ink
326
+ const loops = ls => {
327
+ const p = new Path2D();
328
+ for (const l of ls) {
329
+ l.forEach(([x, y], n) => { const q = at(x, y); n ? p.lineTo(q[0], q[1]) : p.moveTo(q[0], q[1]); });
330
+ p.closePath();
331
+ }
332
+ return p;
333
+ };
334
+ // the neck, from the strand's end round into the drawing's neck and on
335
+ // under it, so the drawing's square cut is hidden
336
+ {
337
+ const back = [H.E[0] - H.dir[0] * iw, H.E[1] - H.dir[1] * iw];
338
+ const c1 = [H.E[0] + H.dir[0] * r * 0.3, H.E[1] + H.dir[1] * r * 0.3];
339
+ taper(g, back, c1, at(0, HEAD.neck.y), at(0.08, HEAD.neck.y), r, k * r, H.d.col, o, false);
340
+ }
341
+ g.save();
342
+ const keep = new Path2D(), K = [at(0.03, -1), at(2, -1), at(2, 2), at(0.03, 2)];
343
+ K.forEach((q, n) => (n ? keep.lineTo(q[0], q[1]) : keep.moveTo(q[0], q[1])));
344
+ g.clip(keep);
345
+ const sil = loops(HEAD.main);
346
+ g.lineJoin = 'round';
347
+ g.strokeStyle = o.ink; g.lineWidth = iw; g.stroke(sil);
348
+ g.fillStyle = H.d.col; g.fill(sil, 'evenodd');
349
+ g.fillStyle = T.d.col; g.fill(loops(HEAD.blue));
350
+ g.fillStyle = o.eye || '#b8452a'; g.fill(loops(HEAD.red));
351
+ const ink = loops(HEAD.sable);
352
+ g.fillStyle = o.ink; g.fill(ink, 'evenodd');
353
+ g.lineWidth = iw * 0.2; g.strokeStyle = o.ink; g.stroke(ink);
354
+ g.restore();
355
+
356
+ g.lineJoin = 'miter';
357
+ }
358
+
359
+ // a ribbon narrowing from w0 to w1 along a cubic, its sides inked and,
360
+ // for a tail, its far end too
361
+ function taper(g, p0, c1, c2, p1, w0, w1, col, o, closed) {
362
+ const n = 32, L = [], R = [], Cn = [];
363
+ for (let s = 0; s <= n; s++) {
364
+ const t = s / n, u = 1 - t;
365
+ const B = (i) => u * u * u * p0[i] + 3 * u * u * t * c1[i] + 3 * u * t * t * c2[i] + t * t * t * p1[i];
366
+ const x = B(0), y = B(1);
367
+ let tx = 3 * u * u * (c1[0] - p0[0]) + 6 * u * t * (c2[0] - c1[0]) + 3 * t * t * (p1[0] - c2[0]);
368
+ let ty = 3 * u * u * (c1[1] - p0[1]) + 6 * u * t * (c2[1] - c1[1]) + 3 * t * t * (p1[1] - c2[1]);
369
+ const tl = Math.hypot(tx, ty) || 1; tx /= tl; ty /= tl;
370
+ const h = (w0 + (w1 - w0) * t) / 2 - o.inkWidth / 2;
371
+ L.push([x - ty * h, y + tx * h]); R.push([x + ty * h, y - tx * h]); Cn.push([x, y]);
372
+ }
373
+ const body = new Path2D();
374
+ L.forEach((q, i) => (i ? body.lineTo(q[0], q[1]) : body.moveTo(q[0], q[1])));
375
+ for (let i = R.length - 1; i >= 0; i--) body.lineTo(R[i][0], R[i][1]);
376
+ body.closePath();
377
+ g.fillStyle = col; g.fill(body);
378
+ g.strokeStyle = o.ink; g.lineJoin = 'round';
379
+ if (o.split) {
380
+ // the split runs on, stopping short of a tail's point
381
+ const e = new Path2D(), m = closed ? Math.round(n * 0.8) : n;
382
+ Cn.slice(0, m + 1).forEach((q, i) => (i ? e.lineTo(q[0], q[1]) : e.moveTo(q[0], q[1])));
383
+ g.lineWidth = o.inkWidth * 0.75; g.stroke(e);
384
+ }
385
+ g.lineWidth = o.inkWidth;
386
+ if (closed) {
387
+ // the far end rounded, the near end left open to meet the strand
388
+ const e = new Path2D();
389
+ L.forEach((q, i) => (i ? e.lineTo(q[0], q[1]) : e.moveTo(q[0], q[1])));
390
+ for (let i = R.length - 1; i >= 0; i--) e.lineTo(R[i][0], R[i][1]);
391
+ g.stroke(e);
392
+ } else {
393
+ for (const side of [L, R]) {
394
+ const e = new Path2D();
395
+ side.forEach((q, i) => (i ? e.lineTo(q[0], q[1]) : e.moveTo(q[0], q[1])));
396
+ g.stroke(e);
397
+ }
398
+ }
399
+ }
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
+ }