azulejeria 2.0.0 → 2.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/README.md CHANGED
@@ -18,7 +18,8 @@ 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 its own paint, and
22
+ [Knotwork](https://stepinto.dev/art/knotwork.html) is the viewer with the knots below. The design is
22
23
  written up [here](https://stepinto.dev/posts/tiles-painted-by-rule/) and
23
24
  [here](https://stepinto.dev/posts/cut-tile-and-poured-cement/).
24
25
 
@@ -182,6 +183,63 @@ bands, bold, motif)`, `paintDesign(g, els, pal, hand, lw, seed, painters)`,
182
183
  `makeTile(opts, size, pixelRatio, seed)`, `tileOpts(cfg, state)` and
183
184
  `paintThumb(cfg, preset, g, size, pixelRatio)`.
184
185
 
186
+ ## Knots
187
+
188
+ `azulejeria/knots` is interlace by rule, for the
189
+ [Knotwork](https://stepinto.dev/art/knotwork.html) page and anything
190
+ else that wants a strand to go over and under. It draws on any 2D
191
+ context and uses nothing from the wall.
192
+
193
+ ```js
194
+ import { buildKnot, paintKnot, masks } from 'azulejeria/knots';
195
+ import { mulberry32 } from 'azulejeria';
196
+
197
+ const knot = buildKnot({ nx: 8, ny: 8, rng: mulberry32(698), breaks: 0.3, cuts: 0.05 });
198
+ paintKnot(g, knot, {
199
+ place: (x, y) => [x / 2, y / 2], // doubled grid point -> where to draw it
200
+ ribbon: 0.34, inkWidth: 0.045, ink: '#2a1e14', ground: '#efe1c0',
201
+ colour: s => ['#d9a62e', '#c8562e', '#4f8a5e'][Math.floor(s.pig * 3)],
202
+ bends: 'corners', heads: true,
203
+ });
204
+ ```
205
+
206
+ The knot lives on a grid of squares in doubled coordinates: corners
207
+ at (even, even), edge midpoints at (odd, even) and (even, odd). A
208
+ strand steps diagonally from midpoint to midpoint, so two cross at
209
+ every midpoint. A break is an edge the strand turns back off; the rim
210
+ of the region is all breaks, so every strand closes. A cut is a break
211
+ the strand ends at instead, and the two ends that meet at a cut become
212
+ a beast, one end a head biting the other. No two cuts sit side by
213
+ side, so the heads have room. Over and under come from
214
+ parity (a horizontal edge takes the rising strand over, a vertical one
215
+ the falling), which alternates round every strand without walking it.
216
+
217
+ **`buildKnot({ nx, ny, rng, breaks, runs, cuts, mask, symmetry })`**:
218
+ `breaks` is the chance an interior edge is a break, `runs` the chance
219
+ a break carries on along its line for another edge or two (which is
220
+ what gives a knot corners), `cuts` the chance a break is a cut.
221
+ They are chosen a whole orbit of the grid's mirrors at a time:
222
+ `symmetry` is `'d4'` (the default for a square grid), `'d2'` or
223
+ `'none'`. `mask(i, j)` says which squares are in the region, so a
224
+ knot can fill a cross or the field around one; `masks.plus`,
225
+ `masks.grow` and `masks.not` make the carpet-page shapes. It returns
226
+ `{ strands, beasts, edge, square }`; each strand has its `pts` (kind
227
+ `cross` with `over`, `wall`, `rim` or `end`), whether it is `open`,
228
+ and `pig`, a seeded number for choosing its colour.
229
+
230
+ **`paintKnot(g, knot, opts)`**: `place(x, y)` maps a grid point to
231
+ the context (add the hand's drift there), `ribbon` and `inkWidth` are
232
+ in the context's units, `colour(strand)` gives each core, `ground` is
233
+ the fill style for the gap cut either side of an over strand, `split`
234
+ puts a line down the middle of the ribbon, `bends` is `'round'`,
235
+ `'corners'` (pointed where a strand turns in a corner of the rim) or
236
+ `'pointed'` (every bounce off the rim), and `heads` draws the beasts.
237
+ A beast's head is traced from a drawing of an Insular hound's head
238
+ (`src/head.js`): it turns off its strand on a narrower neck (`neck`,
239
+ 0.55 of the ribbon) and lies along the cut facing the other end, which
240
+ hooks round under it and into its jaws. `eye` colours its eye.
241
+ `strandPath` and `ribbon` are exported for drawing a strand yourself.
242
+
185
243
  ## Licence
186
244
 
187
245
  MIT.
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "azulejeria",
3
- "version": "2.0.0",
3
+ "version": "2.1.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"],
6
6
  "license": "MIT",
7
7
  "author": "Andy Cross",
8
8
  "homepage": "https://stepinto.dev/art/azulejeria.html",
@@ -11,11 +11,12 @@
11
11
  "main": "./src/azulejeria.js",
12
12
  "exports": {
13
13
  ".": "./src/azulejeria.js",
14
+ "./knots": "./src/knots.js",
14
15
  "./azulejeria.css": "./src/azulejeria.css"
15
16
  },
16
17
  "files": ["src", "README.md", "LICENSE"],
17
18
  "sideEffects": false,
18
19
  "scripts": {
19
- "test": "node --check src/azulejeria.js"
20
+ "test": "node --check src/azulejeria.js && node --check src/knots.js && node --check src/head.js"
20
21
  }
21
22
  }
@@ -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
+ }