azulejeria 1.1.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 +91 -3
- package/package.json +4 -3
- package/src/azulejeria.css +12 -0
- package/src/azulejeria.js +272 -166
- package/src/head.js +13 -0
- package/src/knots.js +399 -0
package/README.md
CHANGED
|
@@ -15,8 +15,13 @@ presets, and, if the tile is not glazed, a material. Three walls built on
|
|
|
15
15
|
it: [Azulejería](https://stepinto.dev/art/azulejeria.html) (Talavera
|
|
16
16
|
rosettes and Mudéjar strapwork), [Jerez](https://stepinto.dev/art/jerez.html)
|
|
17
17
|
(vines, horses and sherry) and [Hidráulico](https://stepinto.dev/art/hidraulico.html)
|
|
18
|
-
(pigmented cement, pressed rather than painted).
|
|
19
|
-
|
|
18
|
+
(pigmented cement, pressed rather than painted). Underneath the wall is a
|
|
19
|
+
viewer, a canvas you paint any world into, with the drag, the pinch, the
|
|
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
|
|
23
|
+
written up [here](https://stepinto.dev/posts/tiles-painted-by-rule/) and
|
|
24
|
+
[here](https://stepinto.dev/posts/cut-tile-and-poured-cement/).
|
|
20
25
|
|
|
21
26
|
```
|
|
22
27
|
npm install azulejeria
|
|
@@ -105,7 +110,8 @@ W, H, state, pal)` is drawn over the whole wall last. None of the hooks
|
|
|
105
110
|
read the design's rng, so a material never moves a design.
|
|
106
111
|
|
|
107
112
|
**`mountPanel(wall, { fileName, defaultPreset })`** wires the standard
|
|
108
|
-
panel
|
|
113
|
+
panel for a wall. It is `mountChrome` (below) with the wall's controls,
|
|
114
|
+
which are a handful of elements found by id: selects `repeat`,
|
|
109
115
|
`motif` and `palette`; ranges `density`, `hand`, `brush` and `zoom` with
|
|
110
116
|
`densityVal`, `handVal`, `brushVal` and `zoomVal` spans beside them;
|
|
111
117
|
checkboxes `bands`, `grout` and `age`; buttons `newDesign` and `save`;
|
|
@@ -117,6 +123,31 @@ reads the query string, so `?preset=talavera` or
|
|
|
117
123
|
`?seed=4242&repeat=rotate&pal=talavera&motif=rosette&density=5&age=1` is a
|
|
118
124
|
shareable wall. Copy the markup from either of the two pages above.
|
|
119
125
|
|
|
126
|
+
**`createViewer(canvas, { paint, scale, minZoom, maxZoom, margin })`**
|
|
127
|
+
is what a wall is built on, and what any other world can be. `paint(g,
|
|
128
|
+
state, w, h, dpr, ox, oy)` paints the world into `g`, `w` by `h` css
|
|
129
|
+
pixels at `dpr`, with the world's origin at `(ox, oy)`. The viewer fills
|
|
130
|
+
the window, pans on drag, zooms on pinch or wheel, and calls
|
|
131
|
+
`onDoubleTap()` on a double tap. With a `margin` it paints into a buffer
|
|
132
|
+
that much larger than the window and only blits while you drag, which is
|
|
133
|
+
what a slow paint wants. `viewer.state` has `scale`, `panX` and `panY`;
|
|
134
|
+
add your own fields to it. Call `viewer.repaint()` when the world
|
|
135
|
+
changes and `viewer.moved()` when only the pan did.
|
|
136
|
+
|
|
137
|
+
**`mountChrome(viewer, spec)`** wires the same chrome around any viewer:
|
|
138
|
+
the panel, the tabs, the preset cards, the title that fades, the save
|
|
139
|
+
button, the query string. The spec names the presets, `applyPreset(name)`,
|
|
140
|
+
`thumb(ctx, settings)` to paint a card, and the `controls`: a list of
|
|
141
|
+
`{ id, key, q, kind, fmt, valId, keep, change }`, where `kind` is
|
|
142
|
+
`select`, `range` or `check`, `key` is the field of `viewer.state` (the
|
|
143
|
+
id by default), `q` its name in the query string (the key by default),
|
|
144
|
+
`fmt` renders a range's value beside it, `keep` leaves the preset marked
|
|
145
|
+
when the query string sets it, and `change(value)` is what to do after.
|
|
146
|
+
Then `newId` and `onNew` for the button that starts over, `mood(state)`
|
|
147
|
+
for the line under it, `query(q)` for anything else in the query string,
|
|
148
|
+
`fileName` and `suffix(state)` for the saved png, and `ready()` once
|
|
149
|
+
everything is applied. Alicatado is the worked example.
|
|
150
|
+
|
|
120
151
|
**`azulejeria.css`** styles that markup. Colour it with the custom
|
|
121
152
|
properties at the top of the file: `--az-bg`, `--az-title`,
|
|
122
153
|
`--az-whisper`, `--az-hint`, `--az-glow`, `--az-glow-far`, `--az-panel`,
|
|
@@ -152,6 +183,63 @@ bands, bold, motif)`, `paintDesign(g, els, pal, hand, lw, seed, painters)`,
|
|
|
152
183
|
`makeTile(opts, size, pixelRatio, seed)`, `tileOpts(cfg, state)` and
|
|
153
184
|
`paintThumb(cfg, preset, g, size, pixelRatio)`.
|
|
154
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
|
+
|
|
155
243
|
## Licence
|
|
156
244
|
|
|
157
245
|
MIT.
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "azulejeria",
|
|
3
|
-
"version": "
|
|
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
|
}
|
package/src/azulejeria.css
CHANGED
|
@@ -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/azulejeria.js
CHANGED
|
@@ -9,10 +9,12 @@
|
|
|
9
9
|
// This module is the engine: the seeded random, the brushwork (every
|
|
10
10
|
// line a polyline with a slow wobble along its normal, washes laid
|
|
11
11
|
// thin twice so the glaze pools), the tile (mottled glaze, the design
|
|
12
|
-
// laid once or four times), the
|
|
13
|
-
//
|
|
14
|
-
//
|
|
15
|
-
//
|
|
12
|
+
// laid once or four times), the viewer (a canvas you paint a world
|
|
13
|
+
// into, with drag, pinch, wheel and the double tap), the wall (a
|
|
14
|
+
// viewer painting tiles repeated with a symmetry, each turned and
|
|
15
|
+
// shifted a little, some crazed) and the standard chrome. Motifs,
|
|
16
|
+
// palettes and presets belong to the page that uses it. See README.md
|
|
17
|
+
// for the shape of each.
|
|
16
18
|
//
|
|
17
19
|
// A material, optionally, changes what the tile is made of without
|
|
18
20
|
// touching what is drawn on it: how the ground is laid, how a shape
|
|
@@ -342,10 +344,133 @@ export function paintThumb(cfg, p, g, T, k) {
|
|
|
342
344
|
}
|
|
343
345
|
}
|
|
344
346
|
|
|
347
|
+
// ------------------------------------------------------------- viewer ---
|
|
348
|
+
// createViewer(canvas, { paint, scale, minZoom, maxZoom, margin }) is
|
|
349
|
+
// a canvas you paint a world into. It fills the window, and handles
|
|
350
|
+
// drag, pinch, wheel and the double tap. `paint(g, state, w, h, dpr,
|
|
351
|
+
// ox, oy)` paints the world into g, w by h css pixels at dpr, with
|
|
352
|
+
// the world's origin at (ox, oy). With a `margin` the world is painted
|
|
353
|
+
// into a buffer that much larger than the window and only blitted
|
|
354
|
+
// while you drag, which is what a slow paint wants; without one it is
|
|
355
|
+
// painted straight to the canvas every time. `state` carries `scale`,
|
|
356
|
+
// `panX` and `panY`, plus whatever the page adds. Call `repaint()`
|
|
357
|
+
// when the world changes and `moved()` when only the pan did. Set
|
|
358
|
+
// `onChange(kind)` to hear 'zoom', `onInteract()` to hear the first
|
|
359
|
+
// touch, `onDoubleTap()` for the double tap and `onZoom()` for the end
|
|
360
|
+
// of a zoom.
|
|
361
|
+
export function createViewer(canvas, opts) {
|
|
362
|
+
opts = opts || {};
|
|
363
|
+
const ctx = canvas.getContext('2d');
|
|
364
|
+
const DPR = Math.min(window.devicePixelRatio || 1, 2);
|
|
365
|
+
const state = { scale: opts.scale || 120, panX: 0, panY: 0 };
|
|
366
|
+
const viewer = { canvas, ctx, state, W: 0, H: 0, DPR, paint: opts.paint,
|
|
367
|
+
minZoom: opts.minZoom || 40, maxZoom: opts.maxZoom || 260, margin: opts.margin || 0,
|
|
368
|
+
preset: '', onChange: null, onInteract: null, onDoubleTap: null, onZoom: null };
|
|
369
|
+
let dirty = true, needPaint = true, buf = null, bg = null, bufPan = null;
|
|
370
|
+
|
|
371
|
+
function render() {
|
|
372
|
+
const W = viewer.W, H = viewer.H, M = viewer.margin;
|
|
373
|
+
if (!M) { viewer.paint(ctx, state, W, H, DPR, W / 2 + state.panX, H / 2 + state.panY); return; }
|
|
374
|
+
if (needPaint || !bufPan) {
|
|
375
|
+
if (!buf) { buf = document.createElement('canvas'); bg = buf.getContext('2d'); }
|
|
376
|
+
buf.width = Math.ceil((W + 2 * M) * DPR); buf.height = Math.ceil((H + 2 * M) * DPR);
|
|
377
|
+
viewer.paint(bg, state, W + 2 * M, H + 2 * M, DPR, W / 2 + M + state.panX, H / 2 + M + state.panY);
|
|
378
|
+
bufPan = [state.panX, state.panY];
|
|
379
|
+
needPaint = false;
|
|
380
|
+
}
|
|
381
|
+
ctx.setTransform(1, 0, 0, 1, 0, 0);
|
|
382
|
+
ctx.drawImage(buf, (state.panX - bufPan[0] - M) * DPR, (state.panY - bufPan[1] - M) * DPR);
|
|
383
|
+
}
|
|
384
|
+
function repaint() { needPaint = true; dirty = true; }
|
|
385
|
+
function moved() {
|
|
386
|
+
const M = viewer.margin;
|
|
387
|
+
if (M && bufPan && (Math.abs(state.panX - bufPan[0]) > M * 0.9 || Math.abs(state.panY - bufPan[1]) > M * 0.9)) needPaint = true;
|
|
388
|
+
dirty = true;
|
|
389
|
+
}
|
|
390
|
+
function frame() {
|
|
391
|
+
if (dirty) { dirty = false; render(); }
|
|
392
|
+
requestAnimationFrame(frame);
|
|
393
|
+
}
|
|
394
|
+
function resize() {
|
|
395
|
+
viewer.W = window.innerWidth; viewer.H = window.innerHeight;
|
|
396
|
+
canvas.width = Math.round(viewer.W * DPR); canvas.height = Math.round(viewer.H * DPR);
|
|
397
|
+
repaint();
|
|
398
|
+
}
|
|
399
|
+
window.addEventListener('resize', resize);
|
|
400
|
+
|
|
401
|
+
const changed = kind => { if (viewer.onChange) viewer.onChange(kind); };
|
|
402
|
+
|
|
403
|
+
function zoomAt(sx, sy, factor) {
|
|
404
|
+
const next = Math.max(viewer.minZoom, Math.min(viewer.maxZoom, state.scale * factor));
|
|
405
|
+
factor = next / state.scale;
|
|
406
|
+
// keep the world point under (sx, sy) fixed while the scale changes
|
|
407
|
+
state.panX = sx - viewer.W / 2 - (sx - viewer.W / 2 - state.panX) * factor;
|
|
408
|
+
state.panY = sy - viewer.H / 2 - (sy - viewer.H / 2 - state.panY) * factor;
|
|
409
|
+
state.scale = next;
|
|
410
|
+
repaint();
|
|
411
|
+
if (viewer.onZoom) viewer.onZoom();
|
|
412
|
+
changed('zoom');
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
// drag to pan, pinch or wheel to zoom, double-tap for whatever the page wants
|
|
416
|
+
const pointers = new Map();
|
|
417
|
+
let pinchDist = 0, pinchMid = null, lastTap = 0, lastTapX = 0, lastTapY = 0;
|
|
418
|
+
|
|
419
|
+
canvas.addEventListener('pointerdown', e => {
|
|
420
|
+
canvas.setPointerCapture(e.pointerId);
|
|
421
|
+
pointers.set(e.pointerId, [e.clientX, e.clientY]);
|
|
422
|
+
if (pointers.size === 1) {
|
|
423
|
+
const now = performance.now();
|
|
424
|
+
if (now - lastTap < 320 && Math.hypot(e.clientX - lastTapX, e.clientY - lastTapY) < 40) {
|
|
425
|
+
if (viewer.onDoubleTap) viewer.onDoubleTap();
|
|
426
|
+
lastTap = 0;
|
|
427
|
+
} else { lastTap = now; lastTapX = e.clientX; lastTapY = e.clientY; }
|
|
428
|
+
} else if (pointers.size === 2) {
|
|
429
|
+
const [a, b] = [...pointers.values()];
|
|
430
|
+
pinchDist = Math.hypot(a[0] - b[0], a[1] - b[1]);
|
|
431
|
+
pinchMid = [(a[0] + b[0]) / 2, (a[1] + b[1]) / 2];
|
|
432
|
+
}
|
|
433
|
+
if (viewer.onInteract) viewer.onInteract();
|
|
434
|
+
});
|
|
435
|
+
canvas.addEventListener('pointermove', e => {
|
|
436
|
+
if (!pointers.has(e.pointerId)) return;
|
|
437
|
+
const prev = pointers.get(e.pointerId);
|
|
438
|
+
pointers.set(e.pointerId, [e.clientX, e.clientY]);
|
|
439
|
+
if (pointers.size === 1) {
|
|
440
|
+
state.panX += e.clientX - prev[0];
|
|
441
|
+
state.panY += e.clientY - prev[1];
|
|
442
|
+
moved();
|
|
443
|
+
} else if (pointers.size === 2) {
|
|
444
|
+
const [a, b] = [...pointers.values()];
|
|
445
|
+
const dist = Math.hypot(a[0] - b[0], a[1] - b[1]);
|
|
446
|
+
const mid = [(a[0] + b[0]) / 2, (a[1] + b[1]) / 2];
|
|
447
|
+
if (pinchMid) { state.panX += mid[0] - pinchMid[0]; state.panY += mid[1] - pinchMid[1]; }
|
|
448
|
+
if (pinchDist > 0) zoomAt(mid[0], mid[1], dist / pinchDist);
|
|
449
|
+
else moved();
|
|
450
|
+
pinchDist = dist; pinchMid = mid;
|
|
451
|
+
}
|
|
452
|
+
});
|
|
453
|
+
const endPointer = e => {
|
|
454
|
+
pointers.delete(e.pointerId);
|
|
455
|
+
if (pointers.size < 2) { pinchDist = 0; pinchMid = null; }
|
|
456
|
+
};
|
|
457
|
+
canvas.addEventListener('pointerup', endPointer);
|
|
458
|
+
canvas.addEventListener('pointercancel', endPointer);
|
|
459
|
+
canvas.addEventListener('wheel', e => {
|
|
460
|
+
e.preventDefault();
|
|
461
|
+
zoomAt(e.clientX, e.clientY, e.deltaY < 0 ? 1.08 : 1 / 1.08);
|
|
462
|
+
}, { passive: false });
|
|
463
|
+
|
|
464
|
+
Object.assign(viewer, { render, repaint, moved, zoomAt, resize });
|
|
465
|
+
resize();
|
|
466
|
+
requestAnimationFrame(frame);
|
|
467
|
+
return viewer;
|
|
468
|
+
}
|
|
469
|
+
|
|
345
470
|
// --------------------------------------------------------------- wall ---
|
|
346
|
-
// createWall(canvas, { palettes, motifs, painters, presets })
|
|
347
|
-
//
|
|
348
|
-
//
|
|
471
|
+
// createWall(canvas, { palettes, motifs, painters, presets, material })
|
|
472
|
+
// is a viewer that paints a wall of tiles. It returns the wall, which
|
|
473
|
+
// is the viewer with more on it: `state` to read or change, then
|
|
349
474
|
// `rebuild(true)` to repaint the tiles or `rebuild(false)` to redraw
|
|
350
475
|
// the wall with the tiles it has. `applyPreset(name)`, `newDesign()`
|
|
351
476
|
// and `zoomAt(x, y, factor)` do the usual things. Set `onChange(kind)`
|
|
@@ -356,8 +481,6 @@ export function paintThumb(cfg, p, g, T, k) {
|
|
|
356
481
|
// `age(ctx, t)` (what age does to one tile, drawn over it in the
|
|
357
482
|
// tile's own frame) and `finish(ctx, W, H, state, pal)` (over the wall).
|
|
358
483
|
export function createWall(canvas, cfg) {
|
|
359
|
-
const ctx = canvas.getContext('2d');
|
|
360
|
-
const DPR = Math.min(window.devicePixelRatio || 1, 2);
|
|
361
484
|
const palettes = cfg.palettes, motifs = cfg.motifs, presets = cfg.presets || {};
|
|
362
485
|
const painters = cfg.painters || PAINTERS;
|
|
363
486
|
const material = cfg.material || null, m = material || {};
|
|
@@ -365,7 +488,8 @@ export function createWall(canvas, cfg) {
|
|
|
365
488
|
const firstMotif = Object.keys(motifs)[0];
|
|
366
489
|
for (const p of Object.values(presets)) if (!p.motif) p.motif = firstMotif;
|
|
367
490
|
|
|
368
|
-
const
|
|
491
|
+
const wall = createViewer(canvas, { paint, scale: 120, minZoom: 40, maxZoom: 260 });
|
|
492
|
+
const state = Object.assign(wall.state, {
|
|
369
493
|
seed: 1,
|
|
370
494
|
repeat: 'module', // module | rotate | single | checker
|
|
371
495
|
pal: Object.keys(palettes)[0],
|
|
@@ -373,16 +497,15 @@ export function createWall(canvas, cfg) {
|
|
|
373
497
|
density: 3, // how much of the design is drawn, 1..5
|
|
374
498
|
hand: 0.5, // wobble of the brush
|
|
375
499
|
brush: 1, // line weight
|
|
376
|
-
scale: 120, // pixels per tile
|
|
377
|
-
panX: 0, panY: 0,
|
|
378
500
|
bands: true,
|
|
379
501
|
grout: true,
|
|
380
502
|
age: false,
|
|
381
|
-
};
|
|
382
|
-
|
|
503
|
+
});
|
|
504
|
+
Object.assign(wall, { palettes, motifs, painters, material, presets });
|
|
383
505
|
const tcfg = { palettes, motifs, painters, material };
|
|
506
|
+
const DPR = wall.DPR;
|
|
384
507
|
|
|
385
|
-
let tiles = { A: [], B: [] }, needTiles = true
|
|
508
|
+
let tiles = { A: [], B: [] }, needTiles = true;
|
|
386
509
|
|
|
387
510
|
function buildTiles() {
|
|
388
511
|
const o = tileOpts(tcfg, state);
|
|
@@ -394,18 +517,15 @@ export function createWall(canvas, cfg) {
|
|
|
394
517
|
needTiles = false;
|
|
395
518
|
}
|
|
396
519
|
|
|
397
|
-
function
|
|
520
|
+
function paint(ctx, st, W, H, dpr, ox, oy) {
|
|
398
521
|
if (needTiles) buildTiles();
|
|
399
522
|
const pal = palettes[state.pal];
|
|
400
|
-
|
|
401
|
-
ctx.setTransform(1, 0, 0, 1, 0, 0);
|
|
523
|
+
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
|
|
402
524
|
ctx.fillStyle = pal.grout;
|
|
403
|
-
ctx.fillRect(0, 0,
|
|
404
|
-
ctx.setTransform(DPR, 0, 0, DPR, 0, 0);
|
|
525
|
+
ctx.fillRect(0, 0, W, H);
|
|
405
526
|
|
|
406
527
|
const S = state.scale;
|
|
407
528
|
const gap = state.grout ? (m.gapOf ? m.gapOf(S, state) : Math.max(1.2, S * 0.022)) : 0;
|
|
408
|
-
const ox = W / 2 + state.panX, oy = H / 2 + state.panY;
|
|
409
529
|
const i0 = Math.floor(-ox / S) - 1, i1 = Math.ceil((W - ox) / S) + 1;
|
|
410
530
|
const j0 = Math.floor(-oy / S) - 1, j1 = Math.ceil((H - oy) / S) + 1;
|
|
411
531
|
const s = S - gap;
|
|
@@ -459,20 +579,7 @@ export function createWall(canvas, cfg) {
|
|
|
459
579
|
}
|
|
460
580
|
}
|
|
461
581
|
|
|
462
|
-
function rebuild(rebuildTiles) { if (rebuildTiles) needTiles = true;
|
|
463
|
-
function frame() {
|
|
464
|
-
if (dirty) { dirty = false; render(); }
|
|
465
|
-
requestAnimationFrame(frame);
|
|
466
|
-
}
|
|
467
|
-
function resize() {
|
|
468
|
-
wall.W = window.innerWidth; wall.H = window.innerHeight;
|
|
469
|
-
canvas.width = Math.round(wall.W * DPR); canvas.height = Math.round(wall.H * DPR);
|
|
470
|
-
rebuild(false);
|
|
471
|
-
}
|
|
472
|
-
window.addEventListener('resize', resize);
|
|
473
|
-
resize();
|
|
474
|
-
requestAnimationFrame(frame);
|
|
475
|
-
|
|
582
|
+
function rebuild(rebuildTiles) { if (rebuildTiles) needTiles = true; wall.repaint(); }
|
|
476
583
|
const changed = kind => { if (wall.onChange) wall.onChange(kind); };
|
|
477
584
|
|
|
478
585
|
function applyPreset(name) {
|
|
@@ -491,118 +598,70 @@ export function createWall(canvas, cfg) {
|
|
|
491
598
|
rebuild(true);
|
|
492
599
|
changed('design');
|
|
493
600
|
}
|
|
601
|
+
// tiles are cached at a pixel size: repaint them once the zoom settles
|
|
602
|
+
let zoomTimer = 0;
|
|
603
|
+
wall.onZoom = () => { clearTimeout(zoomTimer); zoomTimer = setTimeout(() => rebuild(true), 160); };
|
|
604
|
+
wall.onDoubleTap = newDesign;
|
|
494
605
|
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
let pinchDist = 0, pinchMid = null, lastTap = 0, lastTapX = 0, lastTapY = 0, zoomTimer = 0;
|
|
498
|
-
|
|
499
|
-
function zoomAt(sx, sy, factor) {
|
|
500
|
-
const next = Math.max(40, Math.min(260, state.scale * factor));
|
|
501
|
-
factor = next / state.scale;
|
|
502
|
-
state.panX = sx - wall.W / 2 - (sx - wall.W / 2 - state.panX) * factor;
|
|
503
|
-
state.panY = sy - wall.H / 2 - (sy - wall.H / 2 - state.panY) * factor;
|
|
504
|
-
state.scale = next;
|
|
505
|
-
// tiles are cached at a pixel size: repaint them once the zoom settles
|
|
506
|
-
clearTimeout(zoomTimer);
|
|
507
|
-
zoomTimer = setTimeout(() => rebuild(true), 160);
|
|
508
|
-
rebuild(false);
|
|
509
|
-
changed('zoom');
|
|
510
|
-
}
|
|
511
|
-
|
|
512
|
-
canvas.addEventListener('pointerdown', e => {
|
|
513
|
-
canvas.setPointerCapture(e.pointerId);
|
|
514
|
-
pointers.set(e.pointerId, [e.clientX, e.clientY]);
|
|
515
|
-
if (pointers.size === 1) {
|
|
516
|
-
const now = performance.now();
|
|
517
|
-
if (now - lastTap < 320 && Math.hypot(e.clientX - lastTapX, e.clientY - lastTapY) < 40) {
|
|
518
|
-
newDesign();
|
|
519
|
-
lastTap = 0;
|
|
520
|
-
} else { lastTap = now; lastTapX = e.clientX; lastTapY = e.clientY; }
|
|
521
|
-
} else if (pointers.size === 2) {
|
|
522
|
-
const [a, b] = [...pointers.values()];
|
|
523
|
-
pinchDist = Math.hypot(a[0] - b[0], a[1] - b[1]);
|
|
524
|
-
pinchMid = [(a[0] + b[0]) / 2, (a[1] + b[1]) / 2];
|
|
525
|
-
}
|
|
526
|
-
if (wall.onInteract) wall.onInteract();
|
|
527
|
-
});
|
|
528
|
-
canvas.addEventListener('pointermove', e => {
|
|
529
|
-
if (!pointers.has(e.pointerId)) return;
|
|
530
|
-
const prev = pointers.get(e.pointerId);
|
|
531
|
-
pointers.set(e.pointerId, [e.clientX, e.clientY]);
|
|
532
|
-
if (pointers.size === 1) {
|
|
533
|
-
state.panX += e.clientX - prev[0];
|
|
534
|
-
state.panY += e.clientY - prev[1];
|
|
535
|
-
rebuild(false);
|
|
536
|
-
} else if (pointers.size === 2) {
|
|
537
|
-
const [a, b] = [...pointers.values()];
|
|
538
|
-
const dist = Math.hypot(a[0] - b[0], a[1] - b[1]);
|
|
539
|
-
const mid = [(a[0] + b[0]) / 2, (a[1] + b[1]) / 2];
|
|
540
|
-
if (pinchMid) { state.panX += mid[0] - pinchMid[0]; state.panY += mid[1] - pinchMid[1]; }
|
|
541
|
-
if (pinchDist > 0) zoomAt(mid[0], mid[1], dist / pinchDist);
|
|
542
|
-
else rebuild(false);
|
|
543
|
-
pinchDist = dist; pinchMid = mid;
|
|
544
|
-
}
|
|
545
|
-
});
|
|
546
|
-
const endPointer = e => {
|
|
547
|
-
pointers.delete(e.pointerId);
|
|
548
|
-
if (pointers.size < 2) { pinchDist = 0; pinchMid = null; }
|
|
549
|
-
};
|
|
550
|
-
canvas.addEventListener('pointerup', endPointer);
|
|
551
|
-
canvas.addEventListener('pointercancel', endPointer);
|
|
552
|
-
canvas.addEventListener('wheel', e => {
|
|
553
|
-
e.preventDefault();
|
|
554
|
-
zoomAt(e.clientX, e.clientY, e.deltaY < 0 ? 1.08 : 1 / 1.08);
|
|
555
|
-
}, { passive: false });
|
|
556
|
-
|
|
557
|
-
Object.assign(wall, { rebuild, render, applyPreset, newDesign, zoomAt, resize, DPR });
|
|
606
|
+
Object.assign(wall, { rebuild, applyPreset, newDesign });
|
|
607
|
+
wall.rebuild(false);
|
|
558
608
|
return wall;
|
|
559
609
|
}
|
|
560
610
|
|
|
561
|
-
//
|
|
562
|
-
//
|
|
563
|
-
//
|
|
564
|
-
//
|
|
565
|
-
//
|
|
566
|
-
//
|
|
567
|
-
//
|
|
568
|
-
// string
|
|
569
|
-
//
|
|
570
|
-
//
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
611
|
+
// ------------------------------------------------------------- chrome ---
|
|
612
|
+
// mountChrome(viewer, spec) wires the standard page chrome around any
|
|
613
|
+
// viewer: the `#panel` with `#panelHead` and `#panelToggle`, the `.tab`
|
|
614
|
+
// buttons switching `#presetsBody` and `#panelBody`, the
|
|
615
|
+
// `.card[data-preset]` cards (each holding a canvas for its thumbnail),
|
|
616
|
+
// the `#title` overlay that fades on first touch, the `#save` button,
|
|
617
|
+
// the `#mood` line, and the controls the spec lists. It reads the
|
|
618
|
+
// query string and applies it. Anything missing from the page is
|
|
619
|
+
// skipped.
|
|
620
|
+
//
|
|
621
|
+
// The spec: `presets` (name -> settings), `applyPreset(name)`,
|
|
622
|
+
// `thumb(ctx, settings)` to paint a 52px card, `controls`, a list of
|
|
623
|
+
// `{ id, key, q, kind, fmt, valId, keep, change }`: `kind` is 'select',
|
|
624
|
+
// 'range' or 'check'; `key` is the field of `viewer.state` (the id by
|
|
625
|
+
// default); `q` is its name in the query string (the key by default);
|
|
626
|
+
// `fmt` renders the value beside a range; `valId` is where
|
|
627
|
+
// (`<id>Val` by default); `keep` leaves the preset marked when the
|
|
628
|
+
// query string sets it; `change(value)` is what to do after. Then
|
|
629
|
+
// `newId` and `onNew` for the button that starts over, `mood(state)`
|
|
630
|
+
// for the line under it, `query(q)` for anything else in the query
|
|
631
|
+
// string, `fileName` and `suffix(state)` for the saved png, and
|
|
632
|
+
// `ready()` once everything is applied.
|
|
633
|
+
export function mountChrome(viewer, spec) {
|
|
634
|
+
const doc = spec.document || document;
|
|
574
635
|
const $ = id => doc.getElementById(id);
|
|
575
|
-
const { state
|
|
636
|
+
const { state } = viewer;
|
|
637
|
+
const presets = spec.presets || {};
|
|
638
|
+
const controls = spec.controls || [];
|
|
576
639
|
const presetCards = doc.querySelectorAll('.card[data-preset]');
|
|
577
640
|
const markPreset = name => presetCards.forEach(c => c.classList.toggle('on', c.dataset.preset === name));
|
|
578
|
-
const set = (id, v) => { const el = $(id); if (el) el.value = v; };
|
|
579
641
|
const text = (id, v) => { const el = $(id); if (el) el.textContent = v; };
|
|
580
|
-
const
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
check('bands', state.bands); check('grout', state.grout); check('age', state.age);
|
|
594
|
-
text('mood', 'design nº ' + state.seed);
|
|
595
|
-
markPreset(wall.preset);
|
|
642
|
+
const valId = c => c.valId || c.id + 'Val';
|
|
643
|
+
const keyOf = c => c.key || c.id;
|
|
644
|
+
|
|
645
|
+
function sync() {
|
|
646
|
+
for (const c of controls) {
|
|
647
|
+
const el = $(c.id), v = state[keyOf(c)];
|
|
648
|
+
if (!el) continue;
|
|
649
|
+
if (c.kind === 'check') el.checked = !!v;
|
|
650
|
+
else el.value = v;
|
|
651
|
+
if (c.kind === 'range') text(valId(c), c.fmt ? c.fmt(v) : String(v));
|
|
652
|
+
}
|
|
653
|
+
if (spec.mood) text('mood', spec.mood(state));
|
|
654
|
+
markPreset(viewer.preset);
|
|
596
655
|
}
|
|
597
|
-
|
|
656
|
+
viewer.onChange = () => sync();
|
|
598
657
|
|
|
599
|
-
presetCards.forEach(c => c.addEventListener('click', () =>
|
|
658
|
+
presetCards.forEach(c => c.addEventListener('click', () => { spec.applyPreset(c.dataset.preset); sync(); }));
|
|
600
659
|
|
|
601
|
-
// each preset card shows its own
|
|
602
|
-
for (const card of presetCards) {
|
|
660
|
+
// each preset card shows its own small piece of wall
|
|
661
|
+
if (spec.thumb) for (const card of presetCards) {
|
|
603
662
|
const p = presets[card.dataset.preset], tc = card.querySelector('canvas');
|
|
604
663
|
if (!p || !tc) continue;
|
|
605
|
-
|
|
664
|
+
spec.thumb(tc.getContext('2d'), p);
|
|
606
665
|
}
|
|
607
666
|
|
|
608
667
|
// tabs
|
|
@@ -616,25 +675,27 @@ export function mountPanel(wall, opts) {
|
|
|
616
675
|
});
|
|
617
676
|
}
|
|
618
677
|
|
|
619
|
-
const edited = () => {
|
|
678
|
+
const edited = () => { viewer.preset = ''; markPreset(''); };
|
|
679
|
+
const parse = (c, el) => c.kind === 'check' ? el.checked : c.kind === 'range' ? (c.int ? parseInt(el.value, 10) : parseFloat(el.value)) : el.value;
|
|
680
|
+
for (const c of controls) {
|
|
681
|
+
const el = $(c.id);
|
|
682
|
+
if (!el) continue;
|
|
683
|
+
el.addEventListener(c.kind === 'range' ? 'input' : 'change', () => {
|
|
684
|
+
const v = parse(c, el);
|
|
685
|
+
state[keyOf(c)] = v;
|
|
686
|
+
if (c.kind === 'range') text(valId(c), c.fmt ? c.fmt(v) : String(v));
|
|
687
|
+
if (!c.keep) edited();
|
|
688
|
+
if (c.change) c.change(v);
|
|
689
|
+
});
|
|
690
|
+
}
|
|
620
691
|
const on = (id, ev, fn) => { const el = $(id); if (el) el.addEventListener(ev, fn); };
|
|
621
|
-
on('
|
|
622
|
-
on('palette', 'change', e => { state.pal = e.target.value; edited(); wall.rebuild(true); });
|
|
623
|
-
on('motif', 'change', e => { state.motif = e.target.value; edited(); wall.rebuild(true); });
|
|
624
|
-
on('density', 'input', e => { state.density = parseInt(e.target.value, 10); text('densityVal', state.density); edited(); wall.rebuild(true); });
|
|
625
|
-
on('hand', 'input', e => { state.hand = parseFloat(e.target.value); text('handVal', state.hand.toFixed(2)); edited(); wall.rebuild(true); });
|
|
626
|
-
on('brush', 'input', e => { state.brush = parseFloat(e.target.value); text('brushVal', state.brush.toFixed(1)); edited(); wall.rebuild(true); });
|
|
627
|
-
on('zoom', 'input', e => { state.scale = parseInt(e.target.value, 10); text('zoomVal', state.scale + 'px'); edited(); wall.rebuild(true); });
|
|
628
|
-
on('bands', 'change', e => { state.bands = e.target.checked; edited(); wall.rebuild(true); });
|
|
629
|
-
on('grout', 'change', e => { state.grout = e.target.checked; wall.rebuild(false); });
|
|
630
|
-
on('age', 'change', e => { state.age = e.target.checked; wall.rebuild(false); });
|
|
631
|
-
on('newDesign', 'click', () => wall.newDesign());
|
|
692
|
+
if (spec.onNew) on(spec.newId || 'newDesign', 'click', () => { spec.onNew(); sync(); });
|
|
632
693
|
|
|
633
694
|
on('save', 'click', () => {
|
|
634
|
-
|
|
695
|
+
viewer.canvas.toBlob(blob => {
|
|
635
696
|
const a = doc.createElement('a');
|
|
636
697
|
a.href = URL.createObjectURL(blob);
|
|
637
|
-
a.download = (
|
|
698
|
+
a.download = (spec.fileName || 'wall') + '-' + (spec.suffix ? spec.suffix(state) : state.seed) + '.png';
|
|
638
699
|
a.click();
|
|
639
700
|
setTimeout(() => URL.revokeObjectURL(a.href), 4000);
|
|
640
701
|
});
|
|
@@ -646,6 +707,7 @@ export function mountPanel(wall, opts) {
|
|
|
646
707
|
panel.classList.toggle('closed');
|
|
647
708
|
text('panelToggle', panel.classList.contains('closed') ? '+' : '−');
|
|
648
709
|
});
|
|
710
|
+
// on a phone the open panel would cover the art; start it folded
|
|
649
711
|
if (window.innerWidth < 520) {
|
|
650
712
|
panel.classList.add('closed');
|
|
651
713
|
text('panelToggle', '+');
|
|
@@ -659,25 +721,69 @@ export function mountPanel(wall, opts) {
|
|
|
659
721
|
titleGone = true;
|
|
660
722
|
if (titleEl) titleEl.classList.add('gone');
|
|
661
723
|
}
|
|
662
|
-
|
|
724
|
+
viewer.onInteract = dismissTitle;
|
|
663
725
|
setTimeout(dismissTitle, 7000);
|
|
664
726
|
|
|
665
|
-
//
|
|
727
|
+
// the query string: a preset, then any control by its key, within
|
|
728
|
+
// what the page's own element allows
|
|
666
729
|
const q = new URLSearchParams(location.search);
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
730
|
+
spec.applyPreset(q.get('preset') || spec.defaultPreset || Object.keys(presets)[0]);
|
|
731
|
+
for (const c of controls) {
|
|
732
|
+
const k = keyOf(c), qn = c.q || k;
|
|
733
|
+
if (!q.has(qn)) continue;
|
|
734
|
+
const el = $(c.id), raw = q.get(qn);
|
|
735
|
+
let v;
|
|
736
|
+
if (c.kind === 'check') { if (raw !== '0' && raw !== '1') continue; v = raw === '1'; }
|
|
737
|
+
else if (c.kind === 'range') {
|
|
738
|
+
v = c.int ? parseInt(raw, 10) : parseFloat(raw);
|
|
739
|
+
if (!(v >= parseFloat(el ? el.min : -Infinity) && v <= parseFloat(el ? el.max : Infinity))) continue;
|
|
740
|
+
} else {
|
|
741
|
+
if (el && ![...el.options].some(o => o.value === raw)) continue;
|
|
742
|
+
v = raw;
|
|
743
|
+
}
|
|
744
|
+
state[k] = v;
|
|
745
|
+
if (!c.keep) viewer.preset = '';
|
|
746
|
+
}
|
|
747
|
+
if (spec.query) spec.query(q);
|
|
748
|
+
sync();
|
|
749
|
+
if (spec.ready) spec.ready();
|
|
750
|
+
return { sync, dismissTitle, markPreset };
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
// -------------------------------------------------------------- panel ---
|
|
754
|
+
// mountPanel(wall, { fileName, defaultPreset }) is mountChrome with the
|
|
755
|
+
// tile wall's controls: selects `repeat`, `motif` and `palette`; ranges
|
|
756
|
+
// `density`, `hand`, `brush` and `zoom` with `densityVal`, `handVal`,
|
|
757
|
+
// `brushVal` and `zoomVal` beside them; checkboxes `bands`, `grout` and
|
|
758
|
+
// `age`; the `newDesign` button. The query string takes ?preset=,
|
|
759
|
+
// ?seed=, ?repeat=, ?pal=, ?motif=, ?density=, ?hand=, ?zoom=, ?bands=0
|
|
760
|
+
// and ?age=1.
|
|
761
|
+
export function mountPanel(wall, opts) {
|
|
762
|
+
opts = opts || {};
|
|
763
|
+
const { state } = wall;
|
|
764
|
+
const tiles = () => wall.rebuild(true), face = () => wall.rebuild(false);
|
|
765
|
+
return mountChrome(wall, {
|
|
766
|
+
document: opts.document,
|
|
767
|
+
presets: wall.presets,
|
|
768
|
+
defaultPreset: opts.defaultPreset,
|
|
769
|
+
applyPreset: name => wall.applyPreset(name),
|
|
770
|
+
thumb: (g, p) => paintThumb(wall, p, g, 52, 2),
|
|
771
|
+
controls: [
|
|
772
|
+
{ id: 'repeat', kind: 'select', change: tiles },
|
|
773
|
+
{ id: 'palette', key: 'pal', kind: 'select', change: tiles },
|
|
774
|
+
{ id: 'motif', kind: 'select', change: tiles },
|
|
775
|
+
{ id: 'density', kind: 'range', int: true, change: tiles },
|
|
776
|
+
{ id: 'hand', kind: 'range', fmt: v => Number(v).toFixed(2), change: tiles },
|
|
777
|
+
{ id: 'brush', kind: 'range', fmt: v => Number(v).toFixed(1), change: tiles },
|
|
778
|
+
{ id: 'zoom', key: 'scale', q: 'zoom', kind: 'range', int: true, fmt: v => Math.round(v) + 'px', keep: true, change: tiles },
|
|
779
|
+
{ id: 'bands', kind: 'check', keep: true, change: tiles },
|
|
780
|
+
{ id: 'grout', kind: 'check', keep: true, change: face },
|
|
781
|
+
{ id: 'age', kind: 'check', keep: true, change: face },
|
|
782
|
+
],
|
|
783
|
+
newId: 'newDesign', onNew: () => wall.newDesign(),
|
|
784
|
+
mood: s => 'design nº ' + s.seed,
|
|
785
|
+
query: q => { if (q.has('seed')) { state.seed = parseInt(q.get('seed'), 10) || state.seed; wall.preset = ''; } },
|
|
786
|
+
fileName: opts.fileName,
|
|
787
|
+
ready: tiles,
|
|
788
|
+
});
|
|
683
789
|
}
|
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
|
+
}
|