azulejeria 0.0.0-stage → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +136 -2
- package/package.json +19 -4
- package/src/azulejeria.css +126 -0
- package/src/azulejeria.js +653 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Andy Cross
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,137 @@
|
|
|
1
|
-
#
|
|
1
|
+
# azulejeria
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Tin-glazed tiles painted by rule.
|
|
4
|
+
|
|
5
|
+
A seed grows a design in one square, symmetric across its diagonal. Lay
|
|
6
|
+
it as a quarter of a tile and the tile carries the whole motif. Lay it as
|
|
7
|
+
a whole tile and the motif only completes where four tiles meet, mirrored
|
|
8
|
+
or turned, so the wall reads at a scale no single tile has. Every line is
|
|
9
|
+
a polyline with a slow wobble along its normal, the way a loaded brush
|
|
10
|
+
drifts. Washes are laid thin twice so the glaze pools. Each tile gets its
|
|
11
|
+
own mottled glaze, a slight turn, a slight shift, and a few of them craze.
|
|
12
|
+
|
|
13
|
+
This package is the engine. You bring the motifs, the palettes and the
|
|
14
|
+
presets. Two walls built on it: [Azulejería](https://stepinto.dev/art/azulejeria.html)
|
|
15
|
+
(Talavera rosettes and Mudéjar strapwork) and [Jerez](https://stepinto.dev/art/jerez.html)
|
|
16
|
+
(vines, horses and sherry). The design is written up
|
|
17
|
+
[here](https://stepinto.dev/posts/tiles-painted-by-rule/).
|
|
18
|
+
|
|
19
|
+
```
|
|
20
|
+
npm install azulejeria
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
No dependencies. ES module. Browser only (it paints on canvas).
|
|
24
|
+
|
|
25
|
+
## A wall in forty lines
|
|
26
|
+
|
|
27
|
+
```html
|
|
28
|
+
<link rel="stylesheet" href="node_modules/azulejeria/src/azulejeria.css">
|
|
29
|
+
<canvas id="c"></canvas>
|
|
30
|
+
<script type="module">
|
|
31
|
+
import { createWall, mountPanel, PAINTERS, arcPts, ink } from 'azulejeria';
|
|
32
|
+
|
|
33
|
+
const palettes = {
|
|
34
|
+
// glaze: the tin white. tint: the mottle in it. ink: the trace line.
|
|
35
|
+
// wash: the main fill. accents: other glazes. grout: between tiles.
|
|
36
|
+
talavera: { glaze: '#f3f1ea', tint: '#e2e5de', ink: '#1b3a7e', wash: '#2b5cab', accents: [], grout: '#b6b0a2' },
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const motifs = {
|
|
40
|
+
// grows a list of elements from a seeded context; each element names
|
|
41
|
+
// a painter and carries its parameters. Corner (0,0) is the main
|
|
42
|
+
// corner, (1,1) the far one, the edge midpoints are shared with the
|
|
43
|
+
// neighbouring tile.
|
|
44
|
+
rosette({ rng, density, bands, bold, pick, role, dots }) {
|
|
45
|
+
const els = [{ t: 'fan', n: 2 + Math.floor(rng() * 3), r: (0.3 + rng() * 0.1) * bold, ...role() }];
|
|
46
|
+
if (bands) els.push({ t: 'band', kind: pick(['circle', 'diamond']), w: 0.04, ...role() });
|
|
47
|
+
if (density >= 4) els.push({ t: 'ring', r: 0.5 * bold, lobes: 7 });
|
|
48
|
+
if (density >= 5) els.push(dots(3, 4, 0.55, 0.4, 0.85));
|
|
49
|
+
return els;
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const painters = {
|
|
54
|
+
...PAINTERS, // band, ring, lattice, dots
|
|
55
|
+
fan(el, b) { // petals out from the main corner
|
|
56
|
+
for (let k = 0; k < el.n; k++) b.petal(0, 0, (k + 0.5) * b.Q / el.n, 0.08, el.r, 0.7, b.col);
|
|
57
|
+
ink(b.g, arcPts(0, 0, 0.06, 0, b.Q, b.amp, b.rng), b.lw, b.pal, b.rng, false);
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const presets = {
|
|
62
|
+
talavera: { seed: 71, repeat: 'module', pal: 'talavera', motif: 'rosette', density: 4, hand: 0.5, brush: 1, scale: 120, bands: true, age: false },
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const wall = createWall(document.getElementById('c'), { palettes, motifs, painters, presets });
|
|
66
|
+
mountPanel(wall, { fileName: 'wall', defaultPreset: 'talavera' });
|
|
67
|
+
</script>
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
`mountPanel` is optional. Without it, `createWall` still fills the canvas,
|
|
71
|
+
pans on drag, zooms on pinch or wheel, and paints a new design on a double
|
|
72
|
+
tap. Drive it from your own controls through `wall.state` and
|
|
73
|
+
`wall.rebuild(true)`.
|
|
74
|
+
|
|
75
|
+
## The parts
|
|
76
|
+
|
|
77
|
+
**`createWall(canvas, { palettes, motifs, painters, presets })`** returns
|
|
78
|
+
the wall. `wall.state` holds `seed`, `repeat` (`module`, `rotate`,
|
|
79
|
+
`single` or `checker`), `pal`, `motif`, `density` (1 to 5), `hand` (0 to
|
|
80
|
+
1, how far the brush drifts), `brush` (line weight), `scale` (pixels per
|
|
81
|
+
tile), `panX`, `panY`, `bands`, `grout` and `age`. After changing any of
|
|
82
|
+
them call `wall.rebuild(true)` to repaint the tiles, or `wall.rebuild(false)`
|
|
83
|
+
for things the wall draws itself (pan, grout, age). `wall.applyPreset(name)`,
|
|
84
|
+
`wall.newDesign()` and `wall.zoomAt(x, y, factor)` do what they say. Set
|
|
85
|
+
`wall.onChange(kind)` to be told about them.
|
|
86
|
+
|
|
87
|
+
**`mountPanel(wall, { fileName, defaultPreset })`** wires the standard
|
|
88
|
+
panel, which is a handful of elements found by id: selects `repeat`,
|
|
89
|
+
`motif` and `palette`; ranges `density`, `hand`, `brush` and `zoom` with
|
|
90
|
+
`densityVal`, `handVal`, `brushVal` and `zoomVal` spans beside them;
|
|
91
|
+
checkboxes `bands`, `grout` and `age`; buttons `newDesign` and `save`;
|
|
92
|
+
a `mood` line for the seed; `.card[data-preset]` cards each holding a
|
|
93
|
+
canvas for a thumbnail; `.tab[data-tab]` buttons switching `presetsBody`
|
|
94
|
+
and `panelBody`; `panel` with `panelHead` and `panelToggle`; and a `title`
|
|
95
|
+
overlay that fades on first touch. Anything missing is skipped. It also
|
|
96
|
+
reads the query string, so `?preset=talavera` or
|
|
97
|
+
`?seed=4242&repeat=rotate&pal=talavera&motif=rosette&density=5&age=1` is a
|
|
98
|
+
shareable wall. Copy the markup from either of the two pages above.
|
|
99
|
+
|
|
100
|
+
**`azulejeria.css`** styles that markup. Colour it with the custom
|
|
101
|
+
properties at the top of the file: `--az-bg`, `--az-title`,
|
|
102
|
+
`--az-whisper`, `--az-hint`, `--az-glow`, `--az-glow-far`, `--az-panel`,
|
|
103
|
+
`--az-card`, `--az-card-on`, `--az-field`, `--az-accent` and
|
|
104
|
+
`--az-accent-soft`.
|
|
105
|
+
|
|
106
|
+
**A motif** is `(ctx) => elements`. The context has `rng` (seeded, so the
|
|
107
|
+
same seed always grows the same design), `density`, `bands`, `bold`
|
|
108
|
+
(1.35 when the design fills a whole tile, so the motif can be drawn
|
|
109
|
+
larger), `pick(list)`, `role()` (wash, or one of the accents, as the
|
|
110
|
+
palette allows) and `dots(kMin, kSpan, x0, xSpan, yk)` for the scatter
|
|
111
|
+
that the highest density adds. Read the rng in the same order every time
|
|
112
|
+
and the design is reproducible.
|
|
113
|
+
|
|
114
|
+
**A painter** is `(el, b) => void`. `b` is the brush for the design:
|
|
115
|
+
`g` (a 2D context scaled to the unit square), `pal`, `lw`, `amp`, `rng`,
|
|
116
|
+
`col` (the element's role resolved to a colour), `Q` (a quarter turn),
|
|
117
|
+
`D` (the diagonal) with `dc` and `ds`, and the strokes `shape(pts, col)`
|
|
118
|
+
(wash then ink), `dot(x, y, r, col, alpha)`, `petal(cx, cy, ang, rIn, rOut, wd, col)`,
|
|
119
|
+
`kite(...)` (the same with straight sides), `lobes(cx, cy, r, a0, a1, n)`
|
|
120
|
+
and `altOf(el)` (the next accent along). The geometry helpers are
|
|
121
|
+
exported too: `arcPts`, `linePts`, `petalPts`, `kitePts`, `strapPts`,
|
|
122
|
+
`polyPts`, each taking the wobble amplitude and rng, and `trace`, `ink`,
|
|
123
|
+
`wash` to put them on the canvas.
|
|
124
|
+
|
|
125
|
+
**The four painters that ship** are `band` (a quarter circle or straight
|
|
126
|
+
edge across the far corner, which four tiles close into a ring or a
|
|
127
|
+
diamond), `ring` (a scalloped ring around the main corner), `lattice` (a
|
|
128
|
+
line edge to edge that mirrors into a lozenge net) and `dots`.
|
|
129
|
+
|
|
130
|
+
**The lower level** is there if you want it: `makeDesign(seed, density,
|
|
131
|
+
bands, bold, motif)`, `paintDesign(g, els, pal, hand, lw, seed, painters)`,
|
|
132
|
+
`makeTile(opts, size, pixelRatio, seed)`, `tileOpts(cfg, state)` and
|
|
133
|
+
`paintThumb(cfg, preset, g, size, pixelRatio)`.
|
|
134
|
+
|
|
135
|
+
## Licence
|
|
136
|
+
|
|
137
|
+
MIT.
|
package/package.json
CHANGED
|
@@ -1,6 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "azulejeria",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.1",
|
|
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"],
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "Andy Cross",
|
|
8
|
+
"homepage": "https://stepinto.dev/art/azulejeria.html",
|
|
9
|
+
"repository": { "type": "git", "url": "git+https://github.com/AndyCross/mcp-blogs.git", "directory": "packages/azulejeria" },
|
|
10
|
+
"type": "module",
|
|
11
|
+
"main": "./src/azulejeria.js",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": "./src/azulejeria.js",
|
|
14
|
+
"./azulejeria.css": "./src/azulejeria.css"
|
|
15
|
+
},
|
|
16
|
+
"files": ["src", "README.md", "LICENSE"],
|
|
17
|
+
"sideEffects": false,
|
|
18
|
+
"scripts": {
|
|
19
|
+
"test": "node --check src/azulejeria.js"
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/* azulejeria: the standard page chrome for a tile wall. The wall fills
|
|
2
|
+
the viewport, a title blooms over it and fades, a hint sits at the
|
|
3
|
+
foot, and a control panel sits top right with a presets tab and an
|
|
4
|
+
advanced tab. Colour it with the custom properties below. */
|
|
5
|
+
:root {
|
|
6
|
+
--az-bg: #b7b0a2; /* behind the canvas while it loads */
|
|
7
|
+
--az-title: #f4f1e6; /* the title */
|
|
8
|
+
--az-whisper: rgba(244, 241, 230, 0.85); /* the line under it */
|
|
9
|
+
--az-hint: rgba(244, 241, 230, 0.7); /* the hint at the foot */
|
|
10
|
+
--az-glow: rgba(20, 50, 110, 0.9); /* the title's near glow */
|
|
11
|
+
--az-glow-far: rgba(10, 30, 80, 0.9); /* its far glow, and the whisper's */
|
|
12
|
+
--az-panel: rgba(8, 11, 18, 0.82); /* the panel */
|
|
13
|
+
--az-card: rgba(16, 21, 31, 0.6); /* a preset card */
|
|
14
|
+
--az-card-on: #2a3550; /* the chosen one, and a pressed button */
|
|
15
|
+
--az-field: #10151f; /* selects and buttons */
|
|
16
|
+
--az-accent: #e8c96a; /* slider, tab underline, values */
|
|
17
|
+
--az-accent-soft: rgba(232, 201, 106, 0.75); /* the chosen card's border */
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
* { box-sizing: border-box; }
|
|
21
|
+
html, body { margin: 0; padding: 0; height: 100%; overflow: hidden; background: var(--az-bg); }
|
|
22
|
+
canvas { display: block; width: 100vw; height: 100vh; cursor: grab; touch-action: none; }
|
|
23
|
+
canvas:active { cursor: grabbing; }
|
|
24
|
+
|
|
25
|
+
#title {
|
|
26
|
+
position: fixed; inset: 0;
|
|
27
|
+
display: flex; flex-direction: column; align-items: center; justify-content: center;
|
|
28
|
+
pointer-events: none; z-index: 10; text-align: center; padding: 1.5rem;
|
|
29
|
+
transition: opacity 3s ease;
|
|
30
|
+
}
|
|
31
|
+
#title.gone { opacity: 0; }
|
|
32
|
+
#title h1 {
|
|
33
|
+
font-family: "Georgia", "Times New Roman", serif;
|
|
34
|
+
font-weight: 400; font-style: italic;
|
|
35
|
+
font-size: clamp(2.4rem, 7vw, 4.5rem);
|
|
36
|
+
letter-spacing: 0.06em;
|
|
37
|
+
color: var(--az-title);
|
|
38
|
+
margin: 0;
|
|
39
|
+
text-shadow: 0 0 30px var(--az-glow), 0 0 90px var(--az-glow-far);
|
|
40
|
+
opacity: 0;
|
|
41
|
+
animation: bloom 4s ease forwards 0.5s;
|
|
42
|
+
}
|
|
43
|
+
#whisper {
|
|
44
|
+
font-family: "Georgia", serif; font-style: italic;
|
|
45
|
+
font-size: clamp(0.9rem, 2.3vw, 1.1rem);
|
|
46
|
+
color: var(--az-whisper);
|
|
47
|
+
text-shadow: 0 0 20px var(--az-glow-far);
|
|
48
|
+
margin-top: 1.3rem; opacity: 0;
|
|
49
|
+
animation: bloom 4s ease forwards 2s;
|
|
50
|
+
}
|
|
51
|
+
@keyframes bloom {
|
|
52
|
+
from { opacity: 0; filter: blur(10px); transform: translateY(10px); }
|
|
53
|
+
to { opacity: 1; filter: blur(0); transform: translateY(0); }
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
#hint {
|
|
57
|
+
position: fixed; bottom: 1.2rem; left: 0; right: 0; text-align: center;
|
|
58
|
+
font-family: "Courier New", monospace; font-size: 0.68rem;
|
|
59
|
+
letter-spacing: 0.3em; text-transform: uppercase;
|
|
60
|
+
color: var(--az-hint); pointer-events: none; z-index: 9;
|
|
61
|
+
text-shadow: 0 1px 4px rgba(0,0,0,0.6);
|
|
62
|
+
opacity: 0; animation: bloom 3s ease forwards 5.5s;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
#panel {
|
|
66
|
+
position: fixed; top: 1rem; right: 1rem; z-index: 20;
|
|
67
|
+
width: 250px;
|
|
68
|
+
max-height: calc(100vh - 2rem); overflow-y: auto;
|
|
69
|
+
font-family: "Courier New", monospace; font-size: 0.72rem;
|
|
70
|
+
color: rgba(242, 234, 214, 0.85);
|
|
71
|
+
background: var(--az-panel);
|
|
72
|
+
border: 1px solid rgba(242, 234, 214, 0.15);
|
|
73
|
+
border-radius: 10px;
|
|
74
|
+
backdrop-filter: blur(8px);
|
|
75
|
+
user-select: none;
|
|
76
|
+
}
|
|
77
|
+
#panelHead {
|
|
78
|
+
display: flex; justify-content: space-between; align-items: center;
|
|
79
|
+
padding: 0.55rem 0.8rem; cursor: pointer;
|
|
80
|
+
letter-spacing: 0.18em; text-transform: uppercase; font-size: 0.66rem;
|
|
81
|
+
}
|
|
82
|
+
#panelBody { padding: 0 0.8rem 0.8rem; }
|
|
83
|
+
#tabs { display: flex; border-top: 1px solid rgba(242, 234, 214, 0.12); border-bottom: 1px solid rgba(242, 234, 214, 0.12); }
|
|
84
|
+
.tab {
|
|
85
|
+
flex: 1; padding: 0.5rem 0;
|
|
86
|
+
background: none; border: none; cursor: pointer;
|
|
87
|
+
color: rgba(242, 234, 214, 0.5);
|
|
88
|
+
font-family: inherit; font-size: 0.64rem;
|
|
89
|
+
letter-spacing: 0.2em; text-transform: uppercase;
|
|
90
|
+
}
|
|
91
|
+
.tab.on { color: #f2ead6; box-shadow: inset 0 -2px 0 var(--az-accent); }
|
|
92
|
+
#presetsBody { padding: 0.6rem 0.55rem; display: flex; flex-direction: column; gap: 6px; }
|
|
93
|
+
.card {
|
|
94
|
+
display: flex; gap: 0.6rem; align-items: center;
|
|
95
|
+
padding: 0.35rem; border-radius: 8px; cursor: pointer;
|
|
96
|
+
border: 1px solid rgba(242, 234, 214, 0.12);
|
|
97
|
+
background: var(--az-card);
|
|
98
|
+
}
|
|
99
|
+
.card:hover { border-color: rgba(242, 234, 214, 0.4); }
|
|
100
|
+
.card.on { border-color: var(--az-accent-soft); background: var(--az-card-on); }
|
|
101
|
+
.card canvas { width: 52px; height: 52px; border-radius: 6px; display: block; flex: none; }
|
|
102
|
+
.card b { display: block; font-weight: normal; color: #f2ead6; font-size: 0.72rem; letter-spacing: 0.08em; }
|
|
103
|
+
.card span { display: block; font-size: 0.58rem; opacity: 0.55; margin-top: 3px; letter-spacing: 0.05em; }
|
|
104
|
+
#panel.closed #panelBody, #panel.closed #tabs, #panel.closed #presetsBody { display: none; }
|
|
105
|
+
.row { margin-top: 0.7rem; }
|
|
106
|
+
.row label { display: block; margin-bottom: 0.3rem; letter-spacing: 0.12em; opacity: 0.75; }
|
|
107
|
+
.row .val { float: right; opacity: 1; color: var(--az-accent); }
|
|
108
|
+
select {
|
|
109
|
+
width: 100%; padding: 0.3rem;
|
|
110
|
+
background: var(--az-field); color: inherit;
|
|
111
|
+
border: 1px solid rgba(242, 234, 214, 0.2); border-radius: 5px;
|
|
112
|
+
font-family: inherit; font-size: inherit;
|
|
113
|
+
}
|
|
114
|
+
input[type="range"] { width: 100%; accent-color: var(--az-accent); }
|
|
115
|
+
.checks { display: flex; flex-wrap: wrap; gap: 0.45rem 0.9rem; margin-top: 0.7rem; }
|
|
116
|
+
.checks label { display: flex; align-items: center; gap: 0.35rem; cursor: pointer; opacity: 0.85; }
|
|
117
|
+
input[type="checkbox"] { accent-color: var(--az-accent); }
|
|
118
|
+
.btn {
|
|
119
|
+
width: 100%; margin-top: 0.8rem; padding: 0.4rem 0;
|
|
120
|
+
background: var(--az-field); color: rgba(242, 234, 214, 0.85);
|
|
121
|
+
border: 1px solid rgba(242, 234, 214, 0.2); border-radius: 5px;
|
|
122
|
+
font-family: inherit; font-size: 0.66rem; cursor: pointer;
|
|
123
|
+
letter-spacing: 0.15em; text-transform: uppercase;
|
|
124
|
+
}
|
|
125
|
+
.btn:active { background: var(--az-card-on); }
|
|
126
|
+
#mood { font-size: 0.62rem; opacity: 0.5; margin-top: 0.45rem; letter-spacing: 0.15em; text-align: center; }
|
|
@@ -0,0 +1,653 @@
|
|
|
1
|
+
// azulejeria: tin-glazed tiles painted by rule.
|
|
2
|
+
//
|
|
3
|
+
// A design lives in the unit square and is symmetric across its
|
|
4
|
+
// diagonal, which is the trick every four-tile módulo relies on. Lay it as a
|
|
5
|
+
// quarter of a tile and the tile carries the whole motif. Lay it as a
|
|
6
|
+
// whole tile and the motif only completes where four tiles meet,
|
|
7
|
+
// mirrored or turned, so the wall reads at a scale no single tile has.
|
|
8
|
+
//
|
|
9
|
+
// This module is the engine: the seeded random, the brushwork (every
|
|
10
|
+
// line a polyline with a slow wobble along its normal, washes laid
|
|
11
|
+
// thin twice so the glaze pools), the tile (mottled glaze, the design
|
|
12
|
+
// laid once or four times), the wall (tiles repeated with a symmetry,
|
|
13
|
+
// each turned and shifted a little, some crazed) and the standard
|
|
14
|
+
// control panel. Motifs, palettes and presets belong to the page that
|
|
15
|
+
// uses it. See README.md for the shape of each.
|
|
16
|
+
|
|
17
|
+
// ------------------------------------------------------------- random ---
|
|
18
|
+
export function mulberry32(a) {
|
|
19
|
+
return function () {
|
|
20
|
+
a |= 0; a = a + 0x6D2B79F5 | 0;
|
|
21
|
+
let t = Math.imul(a ^ a >>> 15, 1 | a);
|
|
22
|
+
t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t;
|
|
23
|
+
return ((t ^ t >>> 14) >>> 0) / 4294967296;
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
export const hash2 = (i, j) => Math.abs((i * 73856093) ^ (j * 19349663) ^ ((i * 7 + j * 13) * 83492791)) | 0;
|
|
27
|
+
|
|
28
|
+
export function hexA(hex, a) {
|
|
29
|
+
const n = parseInt(hex.slice(1), 16);
|
|
30
|
+
return `rgba(${n >> 16 & 255}, ${n >> 8 & 255}, ${n & 255}, ${a})`;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// ------------------------------------------------------------- design ---
|
|
34
|
+
// A motif is a function of the design context that returns a list of
|
|
35
|
+
// elements, each `{ t: 'painterName', ...params }`. The context carries
|
|
36
|
+
// the seeded rng and a few helpers every motif wants: `pick` one of a
|
|
37
|
+
// list, `role` (wash or one of the accents), and `dots` for the
|
|
38
|
+
// scatter that higher densities add.
|
|
39
|
+
export function makeDesign(seed, density, bands, bold, motif) {
|
|
40
|
+
const rng = mulberry32(seed);
|
|
41
|
+
bold = bold || 1; // single tiles carry the motif larger
|
|
42
|
+
const pick = arr => arr[Math.floor(rng() * arr.length)];
|
|
43
|
+
const role = () => (rng() < 0.6 ? { role: 'wash' } : { role: 'accent', ai: Math.floor(rng() * 3) });
|
|
44
|
+
const dots = (kMin, kSpan, x0, xSpan, yk) => {
|
|
45
|
+
const d = [], k = kMin + Math.floor(rng() * kSpan);
|
|
46
|
+
for (let i = 0; i < k; i++) {
|
|
47
|
+
const x = x0 + rng() * xSpan;
|
|
48
|
+
d.push([x, rng() * x * yk, 0.012 + rng() * 0.012]);
|
|
49
|
+
}
|
|
50
|
+
return { t: 'dots', dots: d };
|
|
51
|
+
};
|
|
52
|
+
return motif({ rng, density, bands, bold, pick, role, dots });
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// ---------------------------------------------------------- brushwork ---
|
|
56
|
+
// A loaded brush drifts: every curve gets a slow sideways wobble
|
|
57
|
+
// along its length, two sines of different pitch, never a jitter.
|
|
58
|
+
export function wobble(rng) {
|
|
59
|
+
const p1 = rng() * 6.283, p2 = rng() * 6.283, f1 = 1.5 + rng() * 2, f2 = 4 + rng() * 4;
|
|
60
|
+
return t => Math.sin(t * 6.283 * f1 + p1) * 0.65 + Math.sin(t * 6.283 * f2 + p2) * 0.35;
|
|
61
|
+
}
|
|
62
|
+
export function arcPts(cx, cy, r, a0, a1, amp, rng, n) {
|
|
63
|
+
const w = wobble(rng), pts = [];
|
|
64
|
+
n = n || 28;
|
|
65
|
+
for (let i = 0; i <= n; i++) {
|
|
66
|
+
const t = i / n, a = a0 + (a1 - a0) * t, rr = r + amp * w(t);
|
|
67
|
+
pts.push([cx + rr * Math.cos(a), cy + rr * Math.sin(a)]);
|
|
68
|
+
}
|
|
69
|
+
return pts;
|
|
70
|
+
}
|
|
71
|
+
export function linePts(x0, y0, x1, y1, amp, rng, n) {
|
|
72
|
+
const w = wobble(rng), pts = [];
|
|
73
|
+
const dx = x1 - x0, dy = y1 - y0, dl = Math.hypot(dx, dy) || 1;
|
|
74
|
+
const nx = -dy / dl, ny = dx / dl;
|
|
75
|
+
n = n || 20;
|
|
76
|
+
for (let i = 0; i <= n; i++) {
|
|
77
|
+
const t = i / n, o = amp * w(t);
|
|
78
|
+
pts.push([x0 + dx * t + nx * o, y0 + dy * t + ny * o]);
|
|
79
|
+
}
|
|
80
|
+
return pts;
|
|
81
|
+
}
|
|
82
|
+
// a leaf, petal or drop pointing along `ang` from (cx, cy), between two radii
|
|
83
|
+
export function petalPts(cx, cy, ang, rIn, rOut, wd, amp, rng) {
|
|
84
|
+
const w1 = wobble(rng), w2 = wobble(rng);
|
|
85
|
+
const ux = Math.cos(ang), uy = Math.sin(ang), nx = -uy, ny = ux;
|
|
86
|
+
const n = 16, pts = [], half = wd * (rOut - rIn) * 0.5;
|
|
87
|
+
for (let i = 0; i <= n; i++) {
|
|
88
|
+
const t = i / n, d = rIn + (rOut - rIn) * t, h = half * Math.sin(Math.PI * t) + amp * w1(t) * Math.sin(Math.PI * t);
|
|
89
|
+
pts.push([cx + ux * d + nx * h, cy + uy * d + ny * h]);
|
|
90
|
+
}
|
|
91
|
+
for (let i = n; i >= 0; i--) {
|
|
92
|
+
const t = i / n, d = rIn + (rOut - rIn) * t, h = half * Math.sin(Math.PI * t) + amp * w2(t) * Math.sin(Math.PI * t);
|
|
93
|
+
pts.push([cx + ux * d - nx * h, cy + uy * d - ny * h]);
|
|
94
|
+
}
|
|
95
|
+
return pts;
|
|
96
|
+
}
|
|
97
|
+
// a kite pointing along `ang`: straight sides, the way arista tiles
|
|
98
|
+
// are cut rather than painted
|
|
99
|
+
export function kitePts(cx, cy, ang, rIn, rOut, wd, amp, rng) {
|
|
100
|
+
const ux = Math.cos(ang), uy = Math.sin(ang), nx = -uy, ny = ux;
|
|
101
|
+
const mid = rIn + (rOut - rIn) * 0.55, half = wd * (rOut - rIn) * 0.5;
|
|
102
|
+
const c = [[cx + ux * rIn, cy + uy * rIn], [cx + ux * mid + nx * half, cy + uy * mid + ny * half],
|
|
103
|
+
[cx + ux * rOut, cy + uy * rOut], [cx + ux * mid - nx * half, cy + uy * mid - ny * half]];
|
|
104
|
+
const pts = [];
|
|
105
|
+
for (let i = 0; i < 4; i++) {
|
|
106
|
+
const a = c[i], b = c[(i + 1) % 4];
|
|
107
|
+
const seg = linePts(a[0], a[1], b[0], b[1], amp * 0.6, rng, 8);
|
|
108
|
+
for (let k = 0; k < seg.length - 1; k++) pts.push(seg[k]);
|
|
109
|
+
}
|
|
110
|
+
return pts;
|
|
111
|
+
}
|
|
112
|
+
// a straight strap between two points, a band with a body to it
|
|
113
|
+
export function strapPts(x0, y0, x1, y1, w, amp, rng) {
|
|
114
|
+
const dx = x1 - x0, dy = y1 - y0, dl = Math.hypot(dx, dy) || 1;
|
|
115
|
+
const nx = -dy / dl * w / 2, ny = dx / dl * w / 2;
|
|
116
|
+
const c = [[x0 + nx, y0 + ny], [x1 + nx, y1 + ny], [x1 - nx, y1 - ny], [x0 - nx, y0 - ny]];
|
|
117
|
+
const pts = [];
|
|
118
|
+
for (let i = 0; i < 4; i++) {
|
|
119
|
+
const a = c[i], b = c[(i + 1) % 4];
|
|
120
|
+
const seg = linePts(a[0], a[1], b[0], b[1], (i & 1) ? 0 : amp * 0.6, rng, (i & 1) ? 2 : 14);
|
|
121
|
+
for (let k = 0; k < seg.length - 1; k++) pts.push(seg[k]);
|
|
122
|
+
}
|
|
123
|
+
return pts;
|
|
124
|
+
}
|
|
125
|
+
// a polygon from vertices, each side given the brush's drift
|
|
126
|
+
export function polyPts(verts, closeIt, amp, rng) {
|
|
127
|
+
const pts = [], m = verts.length - (closeIt ? 0 : 1);
|
|
128
|
+
for (let i = 0; i < m; i++) {
|
|
129
|
+
const p = verts[i], q = verts[(i + 1) % verts.length];
|
|
130
|
+
const seg = linePts(p[0], p[1], q[0], q[1], amp, rng, 5);
|
|
131
|
+
for (let k = 0; k < seg.length - 1; k++) pts.push(seg[k]);
|
|
132
|
+
}
|
|
133
|
+
if (!closeIt) pts.push(verts[verts.length - 1]);
|
|
134
|
+
return pts;
|
|
135
|
+
}
|
|
136
|
+
export function trace(g, pts, close) {
|
|
137
|
+
g.beginPath();
|
|
138
|
+
g.moveTo(pts[0][0], pts[0][1]);
|
|
139
|
+
for (let i = 1; i < pts.length; i++) g.lineTo(pts[i][0], pts[i][1]);
|
|
140
|
+
if (close) g.closePath();
|
|
141
|
+
}
|
|
142
|
+
export function ink(g, pts, lw, pal, rng, close) {
|
|
143
|
+
trace(g, pts, close);
|
|
144
|
+
g.strokeStyle = pal.ink;
|
|
145
|
+
g.lineWidth = lw * (0.8 + 0.4 * rng());
|
|
146
|
+
g.globalAlpha = 0.88;
|
|
147
|
+
g.stroke();
|
|
148
|
+
g.globalAlpha = 1;
|
|
149
|
+
}
|
|
150
|
+
// a wash is laid thin, then a second pass slides a little and pools
|
|
151
|
+
export function wash(g, pts, col, rng) {
|
|
152
|
+
trace(g, pts, true);
|
|
153
|
+
g.fillStyle = col;
|
|
154
|
+
g.globalAlpha = 0.5 + 0.18 * rng();
|
|
155
|
+
g.fill();
|
|
156
|
+
g.save();
|
|
157
|
+
g.translate((rng() - 0.5) * 0.012, (rng() - 0.5) * 0.012);
|
|
158
|
+
g.globalAlpha = 0.22;
|
|
159
|
+
g.fill();
|
|
160
|
+
g.restore();
|
|
161
|
+
g.globalAlpha = 1;
|
|
162
|
+
}
|
|
163
|
+
export function colourOf(el, pal) {
|
|
164
|
+
if (el.role === 'accent' && pal.accents.length) return pal.accents[el.ai % pal.accents.length];
|
|
165
|
+
return pal.wash;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// ------------------------------------------------------------ painters ---
|
|
169
|
+
// A painter is `(el, b) => void`. `b` is the brush for this design: the
|
|
170
|
+
// context `g` (already scaled to the unit square), the palette, the
|
|
171
|
+
// line weight `lw`, the wobble amplitude `amp`, the rng, the colour
|
|
172
|
+
// `col` the element's role resolved to, the quarter turn `Q`, the
|
|
173
|
+
// diagonal `D` with its cosine `dc` and sine `ds`, and the strokes
|
|
174
|
+
// every motif reaches for: `shape` (wash then ink), `dot`, `petal`,
|
|
175
|
+
// `kite`, `lobes`, and `altOf` (the next accent along).
|
|
176
|
+
//
|
|
177
|
+
// These four are the ones every motif so far has shared.
|
|
178
|
+
export const PAINTERS = {
|
|
179
|
+
// a border across the far corner: a quarter circle (which four tiles
|
|
180
|
+
// close into a ring) or a straight edge (which they close into a
|
|
181
|
+
// diamond)
|
|
182
|
+
band(el, b) {
|
|
183
|
+
const { g, pal, lw, amp, rng, col, Q } = b;
|
|
184
|
+
const pts = el.kind === 'circle'
|
|
185
|
+
? arcPts(1, 1, 0.5, Math.PI, Math.PI + Q, amp, rng, 32)
|
|
186
|
+
: linePts(1, 0.5, 0.5, 1, amp, rng, 24);
|
|
187
|
+
trace(g, pts, false);
|
|
188
|
+
g.strokeStyle = pal.ink; g.lineWidth = el.w; g.globalAlpha = 0.88; g.stroke();
|
|
189
|
+
g.strokeStyle = col; g.lineWidth = el.w - 2.2 * lw; g.globalAlpha = 0.75; g.stroke();
|
|
190
|
+
g.globalAlpha = 1;
|
|
191
|
+
},
|
|
192
|
+
// a scalloped ring around the main corner
|
|
193
|
+
ring(el, b) {
|
|
194
|
+
b.lobes(0, 0, el.r, 0, b.Q, el.lobes);
|
|
195
|
+
},
|
|
196
|
+
// a line from one edge to the other, parallel to the diamond band;
|
|
197
|
+
// mirrors into a lozenge net
|
|
198
|
+
lattice(el, b) {
|
|
199
|
+
const { g, pal, lw, amp, rng, col } = b;
|
|
200
|
+
const pts = linePts(1, el.a, el.a, 1, amp, rng, 24);
|
|
201
|
+
trace(g, pts, false);
|
|
202
|
+
g.strokeStyle = pal.ink; g.lineWidth = el.w; g.globalAlpha = 0.88; g.stroke();
|
|
203
|
+
g.strokeStyle = col; g.lineWidth = Math.max(el.w - 2.2 * lw, lw * 0.5); g.globalAlpha = 0.75; g.stroke();
|
|
204
|
+
g.globalAlpha = 1;
|
|
205
|
+
},
|
|
206
|
+
// a scatter in the spare space, mirrored across the diagonal
|
|
207
|
+
dots(el, b) {
|
|
208
|
+
for (const [x, y, r] of el.dots)
|
|
209
|
+
for (const [px, py] of [[x, y], [y, x]]) b.dot(px, py, r);
|
|
210
|
+
},
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
// paints one design into the unit square of g (already scaled)
|
|
214
|
+
export function paintDesign(g, els, pal, hand, lw, jseed, painters) {
|
|
215
|
+
const rng = mulberry32(jseed);
|
|
216
|
+
const amp = 0.012 * hand;
|
|
217
|
+
const Q = Math.PI / 2, D = Q / 2;
|
|
218
|
+
const dc = Math.cos(D), ds = Math.sin(D); // the diagonal
|
|
219
|
+
const shape = (pts, col) => { wash(g, pts, col, rng); ink(g, pts, lw, pal, rng, true); };
|
|
220
|
+
const dot = (x, y, r, col, a) => {
|
|
221
|
+
g.beginPath(); g.arc(x, y, r, 0, 6.283);
|
|
222
|
+
g.fillStyle = col || pal.ink; g.globalAlpha = a || 0.85; g.fill(); g.globalAlpha = 1;
|
|
223
|
+
};
|
|
224
|
+
const petal = (cx, cy, ang, rIn, rOut, wd, col) => shape(petalPts(cx, cy, ang, rIn, rOut, wd, amp, rng), col);
|
|
225
|
+
const kite = (cx, cy, ang, rIn, rOut, wd, col) => shape(kitePts(cx, cy, ang, rIn, rOut, wd, amp, rng), col);
|
|
226
|
+
const lobes = (cx, cy, r, a0, a1, count) => {
|
|
227
|
+
const step = (a1 - a0) / count, s = r * Math.sin(step / 2) * 1.05;
|
|
228
|
+
for (let k = 0; k < count; k++) {
|
|
229
|
+
const a = a0 + (k + 0.5) * step;
|
|
230
|
+
const lx = cx + r * Math.cos(a), ly = cy + r * Math.sin(a);
|
|
231
|
+
ink(g, arcPts(lx, ly, s, a - Q, a + Q, amp * 0.5, rng, 12), lw * 0.8, pal, rng, false);
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
const altOf = el => pal.accents.length ? pal.accents[((el.ai || 0) + 1) % pal.accents.length] : pal.ink;
|
|
235
|
+
const b = { g, pal, lw, amp, rng, Q, D, dc, ds, shape, dot, petal, kite, lobes, altOf, col: pal.wash };
|
|
236
|
+
for (const el of els) {
|
|
237
|
+
const painter = painters[el.t];
|
|
238
|
+
if (!painter) continue;
|
|
239
|
+
b.col = colourOf(el, pal);
|
|
240
|
+
painter(el, b);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// --------------------------------------------------------------- tile ---
|
|
245
|
+
// One tile, painted. `o.quarter` lays the design four times mirrored
|
|
246
|
+
// about the centre; otherwise the design fills the tile with its main
|
|
247
|
+
// corner at bottom right, for the module to complete. `o.plain` paints
|
|
248
|
+
// the small tile a checker alternates with.
|
|
249
|
+
export function makeTile(o, S, k, jseed) {
|
|
250
|
+
const c = document.createElement('canvas');
|
|
251
|
+
c.width = c.height = Math.ceil(S * k);
|
|
252
|
+
const g = c.getContext('2d');
|
|
253
|
+
g.scale(k, k);
|
|
254
|
+
const rng = mulberry32(jseed);
|
|
255
|
+
const pal = o.pal;
|
|
256
|
+
g.fillStyle = pal.glaze;
|
|
257
|
+
g.fillRect(0, 0, S, S);
|
|
258
|
+
for (let i = 0; i < 3; i++) {
|
|
259
|
+
const x = rng() * S, y = rng() * S, rr = S * (0.3 + rng() * 0.5);
|
|
260
|
+
const gr = g.createRadialGradient(x, y, 0, x, y, rr);
|
|
261
|
+
gr.addColorStop(0, hexA(pal.tint, 0.22));
|
|
262
|
+
gr.addColorStop(1, hexA(pal.tint, 0));
|
|
263
|
+
g.fillStyle = gr;
|
|
264
|
+
g.fillRect(0, 0, S, S);
|
|
265
|
+
}
|
|
266
|
+
// glaze thickens at the edge
|
|
267
|
+
g.strokeStyle = hexA(pal.ink, 0.06);
|
|
268
|
+
g.lineWidth = S * 0.06;
|
|
269
|
+
g.strokeRect(0, 0, S, S);
|
|
270
|
+
g.lineCap = 'round'; g.lineJoin = 'round';
|
|
271
|
+
if (o.plain) {
|
|
272
|
+
g.save(); g.translate(S / 2, S / 2); g.scale(S, S);
|
|
273
|
+
const r2 = mulberry32(jseed + 3);
|
|
274
|
+
const amp = 0.012 * o.hand;
|
|
275
|
+
ink(g, arcPts(0, 0, 0.05, 0, 6.283, amp, r2, 24), 0.012 * o.brush, pal, r2, true);
|
|
276
|
+
for (let q = 0; q < 4; q++) {
|
|
277
|
+
g.beginPath(); g.arc(Math.cos(q * Math.PI / 2) * 0.12, Math.sin(q * Math.PI / 2) * 0.12, 0.014, 0, 6.283);
|
|
278
|
+
g.fillStyle = pal.ink; g.globalAlpha = 0.8; g.fill(); g.globalAlpha = 1;
|
|
279
|
+
}
|
|
280
|
+
g.restore();
|
|
281
|
+
} else if (o.quarter) {
|
|
282
|
+
let v = 0;
|
|
283
|
+
for (const [fx, fy] of [[1, 1], [-1, 1], [1, -1], [-1, -1]]) {
|
|
284
|
+
g.save(); g.translate(S / 2, S / 2); g.scale(fx * S / 2, fy * S / 2);
|
|
285
|
+
paintDesign(g, o.design, pal, o.hand, 0.024 * o.brush, jseed + 17 + v++, o.painters);
|
|
286
|
+
g.restore();
|
|
287
|
+
}
|
|
288
|
+
} else {
|
|
289
|
+
g.save(); g.translate(S, S); g.scale(-S, -S);
|
|
290
|
+
paintDesign(g, o.design, pal, o.hand, 0.012 * o.brush, jseed + 17, o.painters);
|
|
291
|
+
g.restore();
|
|
292
|
+
}
|
|
293
|
+
return c;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
export const REPEATS = ['module', 'rotate', 'single', 'checker'];
|
|
297
|
+
const ROT = [0, Math.PI / 2, 3 * Math.PI / 2, Math.PI];
|
|
298
|
+
|
|
299
|
+
// the tile options for a given state (preset or live)
|
|
300
|
+
export function tileOpts(cfg, s) {
|
|
301
|
+
const pal = cfg.palettes[s.pal];
|
|
302
|
+
const quarter = s.repeat === 'single' || s.repeat === 'checker';
|
|
303
|
+
const design = makeDesign(s.seed, s.density, s.bands, quarter ? 1.35 : 1, cfg.motifs[s.motif]);
|
|
304
|
+
return { pal, design, quarter, hand: s.hand, brush: s.brush, painters: cfg.painters };
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// a small wall of four tiles, for a preset card
|
|
308
|
+
export function paintThumb(cfg, p, g, T, k) {
|
|
309
|
+
const o = tileOpts(cfg, p);
|
|
310
|
+
const A = makeTile(o, T, k, p.seed * 31);
|
|
311
|
+
const B = p.repeat === 'checker' ? makeTile({ ...o, plain: true }, T, k, p.seed * 37) : null;
|
|
312
|
+
g.fillStyle = o.pal.grout; g.fillRect(0, 0, 2 * T, 2 * T);
|
|
313
|
+
for (let j = 0; j < 2; j++) for (let i = 0; i < 2; i++) {
|
|
314
|
+
let fx = 1, fy = 1, rot = 0, img = A;
|
|
315
|
+
if (p.repeat === 'module') { fx = i ? -1 : 1; fy = j ? -1 : 1; }
|
|
316
|
+
else if (p.repeat === 'rotate') rot = ROT[i + (j << 1)];
|
|
317
|
+
else if (p.repeat === 'checker' && ((i + j) & 1)) img = B;
|
|
318
|
+
g.save(); g.translate((i + 0.5) * T, (j + 0.5) * T); g.rotate(rot); g.scale(fx, fy);
|
|
319
|
+
g.drawImage(img, -T / 2 + 1, -T / 2 + 1, T - 2, T - 2);
|
|
320
|
+
g.restore();
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
// --------------------------------------------------------------- wall ---
|
|
325
|
+
// createWall(canvas, { palettes, motifs, painters, presets }) paints a
|
|
326
|
+
// wall that fills the canvas, and handles drag, pinch, wheel and the
|
|
327
|
+
// double tap. It returns the wall: `state` to read or change, then
|
|
328
|
+
// `rebuild(true)` to repaint the tiles or `rebuild(false)` to redraw
|
|
329
|
+
// the wall with the tiles it has. `applyPreset(name)`, `newDesign()`
|
|
330
|
+
// and `zoomAt(x, y, factor)` do the usual things. Set `onChange(kind)`
|
|
331
|
+
// to hear about them ('preset', 'design', 'zoom') and `onInteract()`
|
|
332
|
+
// to hear the first touch.
|
|
333
|
+
export function createWall(canvas, cfg) {
|
|
334
|
+
const ctx = canvas.getContext('2d');
|
|
335
|
+
const DPR = Math.min(window.devicePixelRatio || 1, 2);
|
|
336
|
+
const palettes = cfg.palettes, motifs = cfg.motifs, presets = cfg.presets || {};
|
|
337
|
+
const painters = cfg.painters || PAINTERS;
|
|
338
|
+
const firstMotif = Object.keys(motifs)[0];
|
|
339
|
+
for (const p of Object.values(presets)) if (!p.motif) p.motif = firstMotif;
|
|
340
|
+
|
|
341
|
+
const state = {
|
|
342
|
+
seed: 1,
|
|
343
|
+
repeat: 'module', // module | rotate | single | checker
|
|
344
|
+
pal: Object.keys(palettes)[0],
|
|
345
|
+
motif: firstMotif,
|
|
346
|
+
density: 3, // how much of the design is drawn, 1..5
|
|
347
|
+
hand: 0.5, // wobble of the brush
|
|
348
|
+
brush: 1, // line weight
|
|
349
|
+
scale: 120, // pixels per tile
|
|
350
|
+
panX: 0, panY: 0,
|
|
351
|
+
bands: true,
|
|
352
|
+
grout: true,
|
|
353
|
+
age: false,
|
|
354
|
+
};
|
|
355
|
+
const wall = { canvas, ctx, state, palettes, motifs, painters, presets, preset: '', W: 0, H: 0, onChange: null, onInteract: null };
|
|
356
|
+
const tcfg = { palettes, motifs, painters };
|
|
357
|
+
|
|
358
|
+
let tiles = { A: [], B: [] }, needTiles = true, dirty = true;
|
|
359
|
+
|
|
360
|
+
function buildTiles() {
|
|
361
|
+
const o = tileOpts(tcfg, state);
|
|
362
|
+
tiles.A = [];
|
|
363
|
+
tiles.B = [];
|
|
364
|
+
for (let v = 0; v < 4; v++) tiles.A.push(makeTile(o, state.scale, DPR, state.seed * 31 + v));
|
|
365
|
+
if (state.repeat === 'checker')
|
|
366
|
+
for (let v = 0; v < 3; v++) tiles.B.push(makeTile({ ...o, plain: true }, state.scale, DPR, state.seed * 37 + v));
|
|
367
|
+
needTiles = false;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
function render() {
|
|
371
|
+
if (needTiles) buildTiles();
|
|
372
|
+
const pal = palettes[state.pal];
|
|
373
|
+
const W = wall.W, H = wall.H;
|
|
374
|
+
ctx.setTransform(1, 0, 0, 1, 0, 0);
|
|
375
|
+
ctx.fillStyle = pal.grout;
|
|
376
|
+
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
|
377
|
+
ctx.setTransform(DPR, 0, 0, DPR, 0, 0);
|
|
378
|
+
|
|
379
|
+
const S = state.scale;
|
|
380
|
+
const gap = state.grout ? Math.max(1.2, S * 0.022) : 0;
|
|
381
|
+
const ox = W / 2 + state.panX, oy = H / 2 + state.panY;
|
|
382
|
+
const i0 = Math.floor(-ox / S) - 1, i1 = Math.ceil((W - ox) / S) + 1;
|
|
383
|
+
const j0 = Math.floor(-oy / S) - 1, j1 = Math.ceil((H - oy) / S) + 1;
|
|
384
|
+
const s = S - gap;
|
|
385
|
+
for (let j = j0; j <= j1; j++) {
|
|
386
|
+
for (let i = i0; i <= i1; i++) {
|
|
387
|
+
const rng = mulberry32(hash2(i, j) + state.seed);
|
|
388
|
+
const cx = ox + (i + 0.5) * S, cy = oy + (j + 0.5) * S;
|
|
389
|
+
let fx = 1, fy = 1, rot = 0, set = tiles.A;
|
|
390
|
+
if (state.repeat === 'module') { fx = i & 1 ? -1 : 1; fy = j & 1 ? -1 : 1; }
|
|
391
|
+
else if (state.repeat === 'rotate') rot = ROT[(i & 1) + ((j & 1) << 1)];
|
|
392
|
+
else if (state.repeat === 'checker' && ((i + j) & 1)) set = tiles.B;
|
|
393
|
+
const img = set[Math.floor(rng() * set.length)];
|
|
394
|
+
ctx.save();
|
|
395
|
+
ctx.translate(cx + (rng() - 0.5) * S * 0.014 * state.hand, cy + (rng() - 0.5) * S * 0.014 * state.hand);
|
|
396
|
+
ctx.rotate(rot + (rng() - 0.5) * 0.016 * state.hand);
|
|
397
|
+
ctx.scale(fx, fy);
|
|
398
|
+
ctx.drawImage(img, -s / 2, -s / 2, s, s);
|
|
399
|
+
if (state.age) {
|
|
400
|
+
// a warm cast on some, a craze on a few, a chipped corner on fewer
|
|
401
|
+
if (rng() < 0.5) { ctx.fillStyle = `rgba(120, 85, 30, ${0.04 + rng() * 0.1})`; ctx.fillRect(-s / 2, -s / 2, s, s); }
|
|
402
|
+
if (rng() < 0.18) {
|
|
403
|
+
ctx.strokeStyle = 'rgba(60, 45, 30, 0.35)';
|
|
404
|
+
ctx.lineWidth = 0.7;
|
|
405
|
+
const pts = linePts(-s / 2, (rng() - 0.5) * s, s / 2, (rng() - 0.5) * s, s * 0.08, rng, 14);
|
|
406
|
+
trace(ctx, pts, false); ctx.stroke();
|
|
407
|
+
if (rng() < 0.5) {
|
|
408
|
+
const q = linePts((rng() - 0.5) * s, -s / 2, (rng() - 0.5) * s, s / 2, s * 0.08, rng, 14);
|
|
409
|
+
trace(ctx, q, false); ctx.stroke();
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
if (rng() < 0.05) {
|
|
413
|
+
const cxs = rng() < 0.5 ? -1 : 1, cys = rng() < 0.5 ? -1 : 1;
|
|
414
|
+
ctx.fillStyle = pal.grout;
|
|
415
|
+
ctx.beginPath();
|
|
416
|
+
ctx.moveTo(cxs * s / 2, cys * s / 2);
|
|
417
|
+
ctx.lineTo(cxs * (s / 2 - s * (0.08 + rng() * 0.1)), cys * s / 2);
|
|
418
|
+
ctx.lineTo(cxs * (s / 2 - s * 0.03), cys * (s / 2 - s * 0.05));
|
|
419
|
+
ctx.lineTo(cxs * s / 2, cys * (s / 2 - s * (0.08 + rng() * 0.1)));
|
|
420
|
+
ctx.closePath(); ctx.fill();
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
ctx.restore();
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
if (state.age) {
|
|
427
|
+
ctx.fillStyle = 'rgba(90, 60, 20, 0.05)';
|
|
428
|
+
ctx.fillRect(0, 0, W, H);
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
function rebuild(rebuildTiles) { if (rebuildTiles) needTiles = true; dirty = true; }
|
|
433
|
+
function frame() {
|
|
434
|
+
if (dirty) { dirty = false; render(); }
|
|
435
|
+
requestAnimationFrame(frame);
|
|
436
|
+
}
|
|
437
|
+
function resize() {
|
|
438
|
+
wall.W = window.innerWidth; wall.H = window.innerHeight;
|
|
439
|
+
canvas.width = Math.round(wall.W * DPR); canvas.height = Math.round(wall.H * DPR);
|
|
440
|
+
rebuild(false);
|
|
441
|
+
}
|
|
442
|
+
window.addEventListener('resize', resize);
|
|
443
|
+
resize();
|
|
444
|
+
requestAnimationFrame(frame);
|
|
445
|
+
|
|
446
|
+
const changed = kind => { if (wall.onChange) wall.onChange(kind); };
|
|
447
|
+
|
|
448
|
+
function applyPreset(name) {
|
|
449
|
+
const p = presets[name];
|
|
450
|
+
if (!p) return false;
|
|
451
|
+
Object.assign(state, p);
|
|
452
|
+
state.panX = 0; state.panY = 0;
|
|
453
|
+
wall.preset = name;
|
|
454
|
+
rebuild(true);
|
|
455
|
+
changed('preset');
|
|
456
|
+
return true;
|
|
457
|
+
}
|
|
458
|
+
function newDesign() {
|
|
459
|
+
state.seed = 1 + Math.floor(Math.random() * 9999);
|
|
460
|
+
wall.preset = '';
|
|
461
|
+
rebuild(true);
|
|
462
|
+
changed('design');
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
// drag to pan, pinch or wheel to zoom, double-tap for a new design
|
|
466
|
+
const pointers = new Map();
|
|
467
|
+
let pinchDist = 0, pinchMid = null, lastTap = 0, lastTapX = 0, lastTapY = 0, zoomTimer = 0;
|
|
468
|
+
|
|
469
|
+
function zoomAt(sx, sy, factor) {
|
|
470
|
+
const next = Math.max(40, Math.min(260, state.scale * factor));
|
|
471
|
+
factor = next / state.scale;
|
|
472
|
+
state.panX = sx - wall.W / 2 - (sx - wall.W / 2 - state.panX) * factor;
|
|
473
|
+
state.panY = sy - wall.H / 2 - (sy - wall.H / 2 - state.panY) * factor;
|
|
474
|
+
state.scale = next;
|
|
475
|
+
// tiles are cached at a pixel size: repaint them once the zoom settles
|
|
476
|
+
clearTimeout(zoomTimer);
|
|
477
|
+
zoomTimer = setTimeout(() => rebuild(true), 160);
|
|
478
|
+
rebuild(false);
|
|
479
|
+
changed('zoom');
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
canvas.addEventListener('pointerdown', e => {
|
|
483
|
+
canvas.setPointerCapture(e.pointerId);
|
|
484
|
+
pointers.set(e.pointerId, [e.clientX, e.clientY]);
|
|
485
|
+
if (pointers.size === 1) {
|
|
486
|
+
const now = performance.now();
|
|
487
|
+
if (now - lastTap < 320 && Math.hypot(e.clientX - lastTapX, e.clientY - lastTapY) < 40) {
|
|
488
|
+
newDesign();
|
|
489
|
+
lastTap = 0;
|
|
490
|
+
} else { lastTap = now; lastTapX = e.clientX; lastTapY = e.clientY; }
|
|
491
|
+
} else if (pointers.size === 2) {
|
|
492
|
+
const [a, b] = [...pointers.values()];
|
|
493
|
+
pinchDist = Math.hypot(a[0] - b[0], a[1] - b[1]);
|
|
494
|
+
pinchMid = [(a[0] + b[0]) / 2, (a[1] + b[1]) / 2];
|
|
495
|
+
}
|
|
496
|
+
if (wall.onInteract) wall.onInteract();
|
|
497
|
+
});
|
|
498
|
+
canvas.addEventListener('pointermove', e => {
|
|
499
|
+
if (!pointers.has(e.pointerId)) return;
|
|
500
|
+
const prev = pointers.get(e.pointerId);
|
|
501
|
+
pointers.set(e.pointerId, [e.clientX, e.clientY]);
|
|
502
|
+
if (pointers.size === 1) {
|
|
503
|
+
state.panX += e.clientX - prev[0];
|
|
504
|
+
state.panY += e.clientY - prev[1];
|
|
505
|
+
rebuild(false);
|
|
506
|
+
} else if (pointers.size === 2) {
|
|
507
|
+
const [a, b] = [...pointers.values()];
|
|
508
|
+
const dist = Math.hypot(a[0] - b[0], a[1] - b[1]);
|
|
509
|
+
const mid = [(a[0] + b[0]) / 2, (a[1] + b[1]) / 2];
|
|
510
|
+
if (pinchMid) { state.panX += mid[0] - pinchMid[0]; state.panY += mid[1] - pinchMid[1]; }
|
|
511
|
+
if (pinchDist > 0) zoomAt(mid[0], mid[1], dist / pinchDist);
|
|
512
|
+
else rebuild(false);
|
|
513
|
+
pinchDist = dist; pinchMid = mid;
|
|
514
|
+
}
|
|
515
|
+
});
|
|
516
|
+
const endPointer = e => {
|
|
517
|
+
pointers.delete(e.pointerId);
|
|
518
|
+
if (pointers.size < 2) { pinchDist = 0; pinchMid = null; }
|
|
519
|
+
};
|
|
520
|
+
canvas.addEventListener('pointerup', endPointer);
|
|
521
|
+
canvas.addEventListener('pointercancel', endPointer);
|
|
522
|
+
canvas.addEventListener('wheel', e => {
|
|
523
|
+
e.preventDefault();
|
|
524
|
+
zoomAt(e.clientX, e.clientY, e.deltaY < 0 ? 1.08 : 1 / 1.08);
|
|
525
|
+
}, { passive: false });
|
|
526
|
+
|
|
527
|
+
Object.assign(wall, { rebuild, render, applyPreset, newDesign, zoomAt, resize, DPR });
|
|
528
|
+
return wall;
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
// -------------------------------------------------------------- panel ---
|
|
532
|
+
// mountPanel(wall, { fileName, defaultPreset }) wires the standard
|
|
533
|
+
// control panel: the elements with the ids repeat, motif, palette,
|
|
534
|
+
// density, hand, brush, zoom, bands, grout, age, newDesign, mood,
|
|
535
|
+
// save, panel, panelHead, panelToggle, presetsBody, panelBody, the
|
|
536
|
+
// `.tab` buttons, the `.card[data-preset]` cards (each with a canvas
|
|
537
|
+
// for its thumbnail) and the `#title` overlay. It reads the query
|
|
538
|
+
// string (?preset=, ?seed=, ?repeat=, ?pal=, ?motif=, ?density=,
|
|
539
|
+
// ?hand=, ?zoom=, ?bands=0, ?age=1) and applies it. Anything missing
|
|
540
|
+
// from the page is skipped.
|
|
541
|
+
export function mountPanel(wall, opts) {
|
|
542
|
+
opts = opts || {};
|
|
543
|
+
const doc = opts.document || document;
|
|
544
|
+
const $ = id => doc.getElementById(id);
|
|
545
|
+
const { state, presets } = wall;
|
|
546
|
+
const presetCards = doc.querySelectorAll('.card[data-preset]');
|
|
547
|
+
const markPreset = name => presetCards.forEach(c => c.classList.toggle('on', c.dataset.preset === name));
|
|
548
|
+
const set = (id, v) => { const el = $(id); if (el) el.value = v; };
|
|
549
|
+
const text = (id, v) => { const el = $(id); if (el) el.textContent = v; };
|
|
550
|
+
const check = (id, v) => { const el = $(id); if (el) el.checked = v; };
|
|
551
|
+
|
|
552
|
+
function syncZoom() {
|
|
553
|
+
set('zoom', Math.round(state.scale)); text('zoomVal', Math.round(state.scale) + 'px');
|
|
554
|
+
}
|
|
555
|
+
function syncControls() {
|
|
556
|
+
set('repeat', state.repeat);
|
|
557
|
+
set('palette', state.pal);
|
|
558
|
+
set('motif', state.motif);
|
|
559
|
+
set('density', state.density); text('densityVal', state.density);
|
|
560
|
+
set('hand', state.hand); text('handVal', Number(state.hand).toFixed(2));
|
|
561
|
+
set('brush', state.brush); text('brushVal', Number(state.brush).toFixed(1));
|
|
562
|
+
syncZoom();
|
|
563
|
+
check('bands', state.bands); check('grout', state.grout); check('age', state.age);
|
|
564
|
+
text('mood', 'design nº ' + state.seed);
|
|
565
|
+
markPreset(wall.preset);
|
|
566
|
+
}
|
|
567
|
+
wall.onChange = kind => { if (kind === 'zoom') syncZoom(); else syncControls(); };
|
|
568
|
+
|
|
569
|
+
presetCards.forEach(c => c.addEventListener('click', () => wall.applyPreset(c.dataset.preset)));
|
|
570
|
+
|
|
571
|
+
// each preset card shows its own wall, four tiles of it
|
|
572
|
+
for (const card of presetCards) {
|
|
573
|
+
const p = presets[card.dataset.preset], tc = card.querySelector('canvas');
|
|
574
|
+
if (!p || !tc) continue;
|
|
575
|
+
paintThumb(wall, p, tc.getContext('2d'), 52, 2);
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
// tabs
|
|
579
|
+
const panelBody = $('panelBody'), presetsBody = $('presetsBody');
|
|
580
|
+
for (const tab of doc.querySelectorAll('.tab')) {
|
|
581
|
+
tab.addEventListener('click', () => {
|
|
582
|
+
doc.querySelectorAll('.tab').forEach(x => x.classList.toggle('on', x === tab));
|
|
583
|
+
const adv = tab.dataset.tab === 'advanced';
|
|
584
|
+
if (panelBody) panelBody.style.display = adv ? '' : 'none';
|
|
585
|
+
if (presetsBody) presetsBody.style.display = adv ? 'none' : '';
|
|
586
|
+
});
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
const edited = () => { wall.preset = ''; markPreset(''); };
|
|
590
|
+
const on = (id, ev, fn) => { const el = $(id); if (el) el.addEventListener(ev, fn); };
|
|
591
|
+
on('repeat', 'change', e => { state.repeat = e.target.value; edited(); wall.rebuild(true); });
|
|
592
|
+
on('palette', 'change', e => { state.pal = e.target.value; edited(); wall.rebuild(true); });
|
|
593
|
+
on('motif', 'change', e => { state.motif = e.target.value; edited(); wall.rebuild(true); });
|
|
594
|
+
on('density', 'input', e => { state.density = parseInt(e.target.value, 10); text('densityVal', state.density); edited(); wall.rebuild(true); });
|
|
595
|
+
on('hand', 'input', e => { state.hand = parseFloat(e.target.value); text('handVal', state.hand.toFixed(2)); edited(); wall.rebuild(true); });
|
|
596
|
+
on('brush', 'input', e => { state.brush = parseFloat(e.target.value); text('brushVal', state.brush.toFixed(1)); edited(); wall.rebuild(true); });
|
|
597
|
+
on('zoom', 'input', e => { state.scale = parseInt(e.target.value, 10); text('zoomVal', state.scale + 'px'); edited(); wall.rebuild(true); });
|
|
598
|
+
on('bands', 'change', e => { state.bands = e.target.checked; edited(); wall.rebuild(true); });
|
|
599
|
+
on('grout', 'change', e => { state.grout = e.target.checked; wall.rebuild(false); });
|
|
600
|
+
on('age', 'change', e => { state.age = e.target.checked; wall.rebuild(false); });
|
|
601
|
+
on('newDesign', 'click', () => wall.newDesign());
|
|
602
|
+
|
|
603
|
+
on('save', 'click', () => {
|
|
604
|
+
wall.canvas.toBlob(blob => {
|
|
605
|
+
const a = doc.createElement('a');
|
|
606
|
+
a.href = URL.createObjectURL(blob);
|
|
607
|
+
a.download = (opts.fileName || 'wall') + '-' + state.seed + '.png';
|
|
608
|
+
a.click();
|
|
609
|
+
setTimeout(() => URL.revokeObjectURL(a.href), 4000);
|
|
610
|
+
});
|
|
611
|
+
});
|
|
612
|
+
|
|
613
|
+
const panel = $('panel');
|
|
614
|
+
if (panel) {
|
|
615
|
+
on('panelHead', 'click', () => {
|
|
616
|
+
panel.classList.toggle('closed');
|
|
617
|
+
text('panelToggle', panel.classList.contains('closed') ? '+' : '−');
|
|
618
|
+
});
|
|
619
|
+
if (window.innerWidth < 520) {
|
|
620
|
+
panel.classList.add('closed');
|
|
621
|
+
text('panelToggle', '+');
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
const titleEl = $('title');
|
|
626
|
+
let titleGone = false;
|
|
627
|
+
function dismissTitle() {
|
|
628
|
+
if (titleGone) return;
|
|
629
|
+
titleGone = true;
|
|
630
|
+
if (titleEl) titleEl.classList.add('gone');
|
|
631
|
+
}
|
|
632
|
+
wall.onInteract = dismissTitle;
|
|
633
|
+
setTimeout(dismissTitle, 7000);
|
|
634
|
+
|
|
635
|
+
// shareable settings, e.g. ?preset=triana or ?seed=4242&repeat=rotate&pal=coimbra&motif=laceria
|
|
636
|
+
const q = new URLSearchParams(location.search);
|
|
637
|
+
wall.applyPreset(q.get('preset') || opts.defaultPreset || Object.keys(presets)[0]);
|
|
638
|
+
if (q.has('seed')) { state.seed = parseInt(q.get('seed'), 10) || state.seed; wall.preset = ''; }
|
|
639
|
+
if (q.has('repeat') && REPEATS.includes(q.get('repeat'))) state.repeat = q.get('repeat');
|
|
640
|
+
if (q.has('pal') && wall.palettes[q.get('pal')]) state.pal = q.get('pal');
|
|
641
|
+
if (q.has('motif') && wall.motifs[q.get('motif')]) state.motif = q.get('motif');
|
|
642
|
+
const de = parseInt(q.get('density'), 10);
|
|
643
|
+
if (de >= 1 && de <= 5) state.density = de;
|
|
644
|
+
const ha = parseFloat(q.get('hand'));
|
|
645
|
+
if (ha >= 0 && ha <= 1) state.hand = ha;
|
|
646
|
+
const z = parseInt(q.get('zoom'), 10);
|
|
647
|
+
if (z >= 40 && z <= 260) state.scale = z;
|
|
648
|
+
if (q.get('bands') === '0') state.bands = false;
|
|
649
|
+
if (q.get('age') === '1') state.age = true;
|
|
650
|
+
syncControls();
|
|
651
|
+
wall.rebuild(true);
|
|
652
|
+
return { syncControls, dismissTitle };
|
|
653
|
+
}
|