azulejeria 1.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -11,9 +11,11 @@ drifts. Washes are laid thin twice so the glaze pools. Each tile gets its
11
11
  own mottled glaze, a slight turn, a slight shift, and a few of them craze.
12
12
 
13
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
14
+ presets, and, if the tile is not glazed, a material. Three walls built on
15
+ it: [Azulejería](https://stepinto.dev/art/azulejeria.html) (Talavera
16
+ rosettes and Mudéjar strapwork), [Jerez](https://stepinto.dev/art/jerez.html)
17
+ (vines, horses and sherry) and [Hidráulico](https://stepinto.dev/art/hidraulico.html)
18
+ (pigmented cement, pressed rather than painted). The design is written up
17
19
  [here](https://stepinto.dev/posts/tiles-painted-by-rule/).
18
20
 
19
21
  ```
@@ -74,7 +76,7 @@ tap. Drive it from your own controls through `wall.state` and
74
76
 
75
77
  ## The parts
76
78
 
77
- **`createWall(canvas, { palettes, motifs, painters, presets })`** returns
79
+ **`createWall(canvas, { palettes, motifs, painters, presets, material })`** returns
78
80
  the wall. `wall.state` holds `seed`, `repeat` (`module`, `rotate`,
79
81
  `single` or `checker`), `pal`, `motif`, `density` (1 to 5), `hand` (0 to
80
82
  1, how far the brush drifts), `brush` (line weight), `scale` (pixels per
@@ -84,6 +86,24 @@ for things the wall draws itself (pan, grout, age). `wall.applyPreset(name)`,
84
86
  `wall.newDesign()` and `wall.zoomAt(x, y, factor)` do what they say. Set
85
87
  `wall.onChange(kind)` to be told about them.
86
88
 
89
+ **A material** changes what the tile is made of without touching what is
90
+ drawn on it. It is an object of optional hooks, and without one the tile
91
+ is tin-glazed. `ampOf(hand)` turns the hand slider into a wobble
92
+ amplitude (glaze: `0.012 * hand`). `shape(g, pts, col, b)`,
93
+ `line(g, pts, lw, close, b)` and `dot(g, x, y, r, col, alpha, b)` replace
94
+ the brush's strokes, so a cement tile can fill flat and bleed at the edge
95
+ where glaze would wash and ink. `surface(g, S, pal, rng)` lays the ground
96
+ after the glaze colour is filled (the blooms of tint, or grain).
97
+ `plain(g, S, pal, rng, o)` paints the tile a checker alternates with.
98
+ `lay` is `{ shift, turn }`, how far a tile strays from its cell at hand
99
+ one (glaze: `0.014` of a tile and `0.016` radians). `gapOf(S, state)` is
100
+ the grout in pixels. `age(ctx, t)` is drawn over each tile in its own
101
+ frame when age is on, with `t` carrying `i`, `j`, the tile's `rng`, its
102
+ drawn size `s`, the cell size `S`, `pal`, `state` and the tile's world
103
+ centre `cx`, `cy`, so wear can follow the line people walk. `finish(ctx,
104
+ W, H, state, pal)` is drawn over the whole wall last. None of the hooks
105
+ read the design's rng, so a material never moves a design.
106
+
87
107
  **`mountPanel(wall, { fileName, defaultPreset })`** wires the standard
88
108
  panel, which is a handful of elements found by id: selects `repeat`,
89
109
  `motif` and `palette`; ranges `density`, `hand`, `brush` and `zoom` with
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "azulejeria",
3
- "version": "1.0.1",
3
+ "version": "1.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
5
  "keywords": ["canvas", "generative", "tiles", "azulejo", "mudejar", "sevilla", "pattern", "procedural", "art", "wallpaper"],
6
6
  "license": "MIT",
package/src/azulejeria.js CHANGED
@@ -13,6 +13,11 @@
13
13
  // each turned and shifted a little, some crazed) and the standard
14
14
  // control panel. Motifs, palettes and presets belong to the page that
15
15
  // uses it. See README.md for the shape of each.
16
+ //
17
+ // A material, optionally, changes what the tile is made of without
18
+ // touching what is drawn on it: how the ground is laid, how a shape
19
+ // is filled, how far a piece strays from the drawing, what the grout
20
+ // is, and what age does. Without one the tile is tin-glazed.
16
21
 
17
22
  // ------------------------------------------------------------- random ---
18
23
  export function mulberry32(a) {
@@ -210,17 +215,28 @@ export const PAINTERS = {
210
215
  },
211
216
  };
212
217
 
213
- // paints one design into the unit square of g (already scaled)
214
- export function paintDesign(g, els, pal, hand, lw, jseed, painters) {
218
+ // paints one design into the unit square of g (already scaled). The
219
+ // material, if given, may supply `ampOf(hand)`, `shape(g, pts, col, b)`,
220
+ // `line(g, pts, lw, close, b)` and `dot(g, x, y, r, col, a, b)`.
221
+ export function paintDesign(g, els, pal, hand, lw, jseed, painters, material) {
215
222
  const rng = mulberry32(jseed);
216
- const amp = 0.012 * hand;
223
+ const m = material || {};
224
+ const amp = m.ampOf ? m.ampOf(hand) : 0.012 * hand;
217
225
  const Q = Math.PI / 2, D = Q / 2;
218
226
  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
- };
227
+ const b = { g, pal, lw, amp, rng, Q, D, dc, ds, col: pal.wash };
228
+ const shape = m.shape
229
+ ? (pts, col) => m.shape(g, pts, col, b)
230
+ : (pts, col) => { wash(g, pts, col, rng); ink(g, pts, lw, pal, rng, true); };
231
+ const line = m.line
232
+ ? (pts, w, close) => m.line(g, pts, w, close, b)
233
+ : (pts, w, close) => ink(g, pts, w, pal, rng, close);
234
+ const dot = m.dot
235
+ ? (x, y, r, col, a) => m.dot(g, x, y, r, col, a, b)
236
+ : (x, y, r, col, a) => {
237
+ g.beginPath(); g.arc(x, y, r, 0, 6.283);
238
+ g.fillStyle = col || pal.ink; g.globalAlpha = a || 0.85; g.fill(); g.globalAlpha = 1;
239
+ };
224
240
  const petal = (cx, cy, ang, rIn, rOut, wd, col) => shape(petalPts(cx, cy, ang, rIn, rOut, wd, amp, rng), col);
225
241
  const kite = (cx, cy, ang, rIn, rOut, wd, col) => shape(kitePts(cx, cy, ang, rIn, rOut, wd, amp, rng), col);
226
242
  const lobes = (cx, cy, r, a0, a1, count) => {
@@ -232,7 +248,7 @@ export function paintDesign(g, els, pal, hand, lw, jseed, painters) {
232
248
  }
233
249
  };
234
250
  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 };
251
+ Object.assign(b, { shape, line, dot, petal, kite, lobes, altOf });
236
252
  for (const el of els) {
237
253
  const painter = painters[el.t];
238
254
  if (!painter) continue;
@@ -252,23 +268,28 @@ export function makeTile(o, S, k, jseed) {
252
268
  const g = c.getContext('2d');
253
269
  g.scale(k, k);
254
270
  const rng = mulberry32(jseed);
255
- const pal = o.pal;
271
+ const pal = o.pal, m = o.material || {};
256
272
  g.fillStyle = pal.glaze;
257
273
  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);
274
+ if (m.surface) m.surface(g, S, pal, rng);
275
+ else {
276
+ for (let i = 0; i < 3; i++) {
277
+ const x = rng() * S, y = rng() * S, rr = S * (0.3 + rng() * 0.5);
278
+ const gr = g.createRadialGradient(x, y, 0, x, y, rr);
279
+ gr.addColorStop(0, hexA(pal.tint, 0.22));
280
+ gr.addColorStop(1, hexA(pal.tint, 0));
281
+ g.fillStyle = gr;
282
+ g.fillRect(0, 0, S, S);
283
+ }
284
+ // glaze thickens at the edge
285
+ g.strokeStyle = hexA(pal.ink, 0.06);
286
+ g.lineWidth = S * 0.06;
287
+ g.strokeRect(0, 0, S, S);
265
288
  }
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
289
  g.lineCap = 'round'; g.lineJoin = 'round';
271
- if (o.plain) {
290
+ if (o.plain && m.plain) {
291
+ m.plain(g, S, pal, mulberry32(jseed + 3), o);
292
+ } else if (o.plain) {
272
293
  g.save(); g.translate(S / 2, S / 2); g.scale(S, S);
273
294
  const r2 = mulberry32(jseed + 3);
274
295
  const amp = 0.012 * o.hand;
@@ -282,12 +303,12 @@ export function makeTile(o, S, k, jseed) {
282
303
  let v = 0;
283
304
  for (const [fx, fy] of [[1, 1], [-1, 1], [1, -1], [-1, -1]]) {
284
305
  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);
306
+ paintDesign(g, o.design, pal, o.hand, 0.024 * o.brush, jseed + 17 + v++, o.painters, o.material);
286
307
  g.restore();
287
308
  }
288
309
  } else {
289
310
  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);
311
+ paintDesign(g, o.design, pal, o.hand, 0.012 * o.brush, jseed + 17, o.painters, o.material);
291
312
  g.restore();
292
313
  }
293
314
  return c;
@@ -301,7 +322,7 @@ export function tileOpts(cfg, s) {
301
322
  const pal = cfg.palettes[s.pal];
302
323
  const quarter = s.repeat === 'single' || s.repeat === 'checker';
303
324
  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 };
325
+ return { pal, design, quarter, hand: s.hand, brush: s.brush, painters: cfg.painters, material: cfg.material };
305
326
  }
306
327
 
307
328
  // a small wall of four tiles, for a preset card
@@ -329,12 +350,18 @@ export function paintThumb(cfg, p, g, T, k) {
329
350
  // the wall with the tiles it has. `applyPreset(name)`, `newDesign()`
330
351
  // and `zoomAt(x, y, factor)` do the usual things. Set `onChange(kind)`
331
352
  // to hear about them ('preset', 'design', 'zoom') and `onInteract()`
332
- // to hear the first touch.
353
+ // to hear the first touch. `material`, if given, may supply the
354
+ // painting hooks above plus `lay` ({ shift, turn }, how far a tile
355
+ // strays from its cell), `gapOf(S, state)` (the grout, in pixels),
356
+ // `age(ctx, t)` (what age does to one tile, drawn over it in the
357
+ // tile's own frame) and `finish(ctx, W, H, state, pal)` (over the wall).
333
358
  export function createWall(canvas, cfg) {
334
359
  const ctx = canvas.getContext('2d');
335
360
  const DPR = Math.min(window.devicePixelRatio || 1, 2);
336
361
  const palettes = cfg.palettes, motifs = cfg.motifs, presets = cfg.presets || {};
337
362
  const painters = cfg.painters || PAINTERS;
363
+ const material = cfg.material || null, m = material || {};
364
+ const lay = m.lay || { shift: 0.014, turn: 0.016 };
338
365
  const firstMotif = Object.keys(motifs)[0];
339
366
  for (const p of Object.values(presets)) if (!p.motif) p.motif = firstMotif;
340
367
 
@@ -352,8 +379,8 @@ export function createWall(canvas, cfg) {
352
379
  grout: true,
353
380
  age: false,
354
381
  };
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 };
382
+ const wall = { canvas, ctx, state, palettes, motifs, painters, material, presets, preset: '', W: 0, H: 0, onChange: null, onInteract: null };
383
+ const tcfg = { palettes, motifs, painters, material };
357
384
 
358
385
  let tiles = { A: [], B: [] }, needTiles = true, dirty = true;
359
386
 
@@ -377,7 +404,7 @@ export function createWall(canvas, cfg) {
377
404
  ctx.setTransform(DPR, 0, 0, DPR, 0, 0);
378
405
 
379
406
  const S = state.scale;
380
- const gap = state.grout ? Math.max(1.2, S * 0.022) : 0;
407
+ const gap = state.grout ? (m.gapOf ? m.gapOf(S, state) : Math.max(1.2, S * 0.022)) : 0;
381
408
  const ox = W / 2 + state.panX, oy = H / 2 + state.panY;
382
409
  const i0 = Math.floor(-ox / S) - 1, i1 = Math.ceil((W - ox) / S) + 1;
383
410
  const j0 = Math.floor(-oy / S) - 1, j1 = Math.ceil((H - oy) / S) + 1;
@@ -392,11 +419,13 @@ export function createWall(canvas, cfg) {
392
419
  else if (state.repeat === 'checker' && ((i + j) & 1)) set = tiles.B;
393
420
  const img = set[Math.floor(rng() * set.length)];
394
421
  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);
422
+ ctx.translate(cx + (rng() - 0.5) * S * lay.shift * state.hand, cy + (rng() - 0.5) * S * lay.shift * state.hand);
423
+ ctx.rotate(rot + (rng() - 0.5) * lay.turn * state.hand);
397
424
  ctx.scale(fx, fy);
398
425
  ctx.drawImage(img, -s / 2, -s / 2, s, s);
399
- if (state.age) {
426
+ if (state.age && m.age) {
427
+ m.age(ctx, { i, j, rng, s, S, pal, state, cx: (i + 0.5) * S, cy: (j + 0.5) * S, fx, fy, rot });
428
+ } else if (state.age) {
400
429
  // a warm cast on some, a craze on a few, a chipped corner on fewer
401
430
  if (rng() < 0.5) { ctx.fillStyle = `rgba(120, 85, 30, ${0.04 + rng() * 0.1})`; ctx.fillRect(-s / 2, -s / 2, s, s); }
402
431
  if (rng() < 0.18) {
@@ -423,7 +452,8 @@ export function createWall(canvas, cfg) {
423
452
  ctx.restore();
424
453
  }
425
454
  }
426
- if (state.age) {
455
+ if (m.finish) m.finish(ctx, W, H, state, pal);
456
+ else if (state.age) {
427
457
  ctx.fillStyle = 'rgba(90, 60, 20, 0.05)';
428
458
  ctx.fillRect(0, 0, W, H);
429
459
  }