azulejeria 2.5.1 → 2.7.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 +211 -23
- package/package.json +7 -4
- package/src/azulejeria.css +43 -0
- package/src/azulejeria.js +103 -16
- package/src/draw.js +448 -0
- package/src/loom.js +175 -0
- package/src/shadow.js +83 -0
- package/test/draw.mjs +53 -0
- package/test/loom.mjs +52 -0
package/src/shadow.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// shadow: a sun that goes round, and the shadows it casts.
|
|
2
|
+
//
|
|
3
|
+
// Nothing here is drawn in shadow; the shadows are cast. A caster is
|
|
4
|
+
// whatever stands off the surface behind it: a course of brick proud of
|
|
5
|
+
// a wall, an iron grille a hand's width in front of whitewash, a stone
|
|
6
|
+
// screen with a floor below it. Its shadow is its own outline, moved
|
|
7
|
+
// along the sun's slant by how far it stands off, with anything that
|
|
8
|
+
// must stay lit cut back out, softened at the edge and multiplied onto
|
|
9
|
+
// the surface in the colour of the sky.
|
|
10
|
+
//
|
|
11
|
+
// Sebka needed it first, Rejería second and Jali third, which is when
|
|
12
|
+
// it moved here.
|
|
13
|
+
|
|
14
|
+
// The wall faces south. At six the sun is on the horizon to the left;
|
|
15
|
+
// at one it is high and in front; at eight it sets on the right. A
|
|
16
|
+
// point standing h proud of the wall throws its shadow h times `slant`
|
|
17
|
+
// along the wall (x to the right, y down); `light` is how squarely the
|
|
18
|
+
// sun hits the face. `reach` is the longest slant allowed, so dawn and
|
|
19
|
+
// dusk don't throw shadows off the page. `az` and `e` are the sun's
|
|
20
|
+
// angle along the wall and its height, for a page that needs its own
|
|
21
|
+
// projection; `warm` is 1 at the horizon and 0 at the top of the day.
|
|
22
|
+
const EMAX = 64 * Math.PI / 180;
|
|
23
|
+
export function sunOf(hour, opts) {
|
|
24
|
+
const reach = (opts && opts.reach) || 9;
|
|
25
|
+
const t = Math.min(0.995, Math.max(0.005, (hour - 6) / 14));
|
|
26
|
+
const az = Math.PI * t;
|
|
27
|
+
const e = EMAX * Math.sin(az);
|
|
28
|
+
let sx = Math.cos(az) / Math.sin(az), sy = Math.tan(e) / Math.sin(az);
|
|
29
|
+
const len = Math.hypot(sx, sy);
|
|
30
|
+
if (len > reach) { sx *= reach / len; sy *= reach / len; }
|
|
31
|
+
const light = Math.cos(e) * Math.sin(az);
|
|
32
|
+
// the light itself: white at height, orange at the horizon. What the
|
|
33
|
+
// sun doesn't reach is lit by the sky, so the shade is blue, and
|
|
34
|
+
// bluer against a warm sun
|
|
35
|
+
const warm = 1 - e / EMAX;
|
|
36
|
+
const w2 = warm * warm;
|
|
37
|
+
const sunCol = [255, Math.round(248 - 70 * w2), Math.round(232 - 125 * w2)];
|
|
38
|
+
const shade = [Math.round(112 - 22 * warm), Math.round(118 - 18 * warm), Math.round(162 + 8 * warm)];
|
|
39
|
+
const dl = Math.hypot(sx, sy) || 1;
|
|
40
|
+
return { slant: [sx, sy], light, warm, e, az, sunCol, shade, dir: [-sx / dl, -sy / dl] };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// offscreen layers, kept by number and resized as the window is
|
|
44
|
+
const layers = [];
|
|
45
|
+
export function shadowLayer(n, pw, ph) {
|
|
46
|
+
let c = layers[n];
|
|
47
|
+
if (!c) c = layers[n] = document.createElement('canvas');
|
|
48
|
+
if (c.width !== pw || c.height !== ph) { c.width = pw; c.height = ph; }
|
|
49
|
+
return c;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// castShadow(g, { w, h, dpr, shade, alpha, blur, layer, cast, cut })
|
|
53
|
+
// lays one shadow over what is already on g. `cast(lg)` draws the
|
|
54
|
+
// caster where its shadow falls, on a clear layer the size of the
|
|
55
|
+
// window whose fill and stroke are already the shade colour; it sets
|
|
56
|
+
// its own transform, and may composite as it likes. `cut(lg)`, if
|
|
57
|
+
// given, is then drawn with destination-out: whatever it paints is
|
|
58
|
+
// taken out of the shadow (the caster itself, or a level the shadow
|
|
59
|
+
// can't reach). The layer is multiplied onto g at `alpha`, softened by
|
|
60
|
+
// `blur` device pixels (none while something is moving: the blur is
|
|
61
|
+
// most of a frame). `shade` is an [r, g, b] or a css colour; `layer`
|
|
62
|
+
// picks which kept layer to use, for a page that casts more than once
|
|
63
|
+
// and wants each kept.
|
|
64
|
+
export function castShadow(g, o) {
|
|
65
|
+
const pw = Math.ceil(o.w * o.dpr), ph = Math.ceil(o.h * o.dpr);
|
|
66
|
+
const lc = shadowLayer(o.layer || 0, pw, ph);
|
|
67
|
+
const lg = lc.getContext('2d');
|
|
68
|
+
lg.setTransform(1, 0, 0, 1, 0, 0);
|
|
69
|
+
lg.globalCompositeOperation = 'source-over';
|
|
70
|
+
lg.globalAlpha = 1;
|
|
71
|
+
lg.clearRect(0, 0, pw, ph);
|
|
72
|
+
lg.fillStyle = lg.strokeStyle = Array.isArray(o.shade) ? `rgb(${o.shade})` : o.shade;
|
|
73
|
+
o.cast(lg);
|
|
74
|
+
if (o.cut) { lg.globalCompositeOperation = 'destination-out'; o.cut(lg); }
|
|
75
|
+
g.save();
|
|
76
|
+
g.setTransform(1, 0, 0, 1, 0, 0);
|
|
77
|
+
g.globalCompositeOperation = 'multiply';
|
|
78
|
+
g.globalAlpha = o.alpha == null ? 1 : o.alpha;
|
|
79
|
+
if (o.blur) g.filter = `blur(${o.blur.toFixed(2)}px)`;
|
|
80
|
+
g.drawImage(lc, 0, 0);
|
|
81
|
+
g.restore();
|
|
82
|
+
return lc;
|
|
83
|
+
}
|
package/test/draw.mjs
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// The drawing's encoding, the stroke settling and the print export's PNG chunk.
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { encodeDrawing, decodeDrawing, settle, simplify, smooth, drawnMotif } from '../src/draw.js';
|
|
4
|
+
import { crc32, pngWithDpi } from '../src/azulejeria.js';
|
|
5
|
+
|
|
6
|
+
const near = (a, b) => Math.abs(a - b) <= 1 / 255;
|
|
7
|
+
const drawing = [
|
|
8
|
+
{ k: 'shape', c: 0, w: 3, pts: [[0.1, 0.05], [0.4, 0.1], [0.3, 0.25]] },
|
|
9
|
+
{ k: 'line', c: 4, w: 1, pts: [[0.5, 0], [0.8, 0.2], [1, 0.5]] },
|
|
10
|
+
{ k: 'line', c: 2, w: 8, pts: [[0.5, 0.1], [0.9, 0.4]] },
|
|
11
|
+
{ k: 'dot', c: 3, w: 5, pts: [[0.7, 0.3]] },
|
|
12
|
+
];
|
|
13
|
+
const back = decodeDrawing(encodeDrawing(drawing));
|
|
14
|
+
assert.equal(back.length, drawing.length);
|
|
15
|
+
back.forEach((s, i) => {
|
|
16
|
+
assert.equal(s.k, drawing[i].k); assert.equal(s.c, drawing[i].c); assert.equal(s.w, drawing[i].w);
|
|
17
|
+
s.pts.forEach((p, j) => assert.ok(near(p[0], drawing[i].pts[j][0]) && near(p[1], drawing[i].pts[j][1])));
|
|
18
|
+
});
|
|
19
|
+
// encoding what was decoded gives the same string: a link is stable
|
|
20
|
+
assert.equal(encodeDrawing(back), encodeDrawing(drawing));
|
|
21
|
+
assert.match(encodeDrawing(drawing), /^[A-Za-z0-9_-]+$/);
|
|
22
|
+
assert.equal(encodeDrawing([]), '');
|
|
23
|
+
for (const bad of ['', null, '!!!', 'AAAA', 'AQ', 'AQEB', 'not a drawing']) assert.deepEqual(decodeDrawing(bad), []);
|
|
24
|
+
|
|
25
|
+
// a stroke drawn on the far side of the diagonal is reflected over
|
|
26
|
+
const s = settle({ k: 'line', c: 0, w: 2, pts: [[0.1, 0.5], [0.2, 0.9], [-0.3, 1.4]] });
|
|
27
|
+
assert.ok(s.pts.every(([x, y]) => y <= x && x >= 0 && x <= 1 && y >= 0 && y <= 1));
|
|
28
|
+
assert.equal(settle({ k: 'shape', c: 0, w: 2, pts: [[0, 0], [1, 0]] }), null);
|
|
29
|
+
assert.equal(settle({ k: 'dot', c: 0, w: 2, pts: [[0.2, 0.1], [0.5, 0.5]] }).pts.length, 1);
|
|
30
|
+
// a long scribble comes down to a stroke that fits
|
|
31
|
+
const scribble = Array.from({ length: 4000 }, (_, i) => [0.5 + 0.4 * Math.cos(i / 9) * (i / 4000), 0.3 + 0.2 * Math.sin(i / 7)]);
|
|
32
|
+
assert.ok(settle({ k: 'line', c: 0, w: 1, pts: scribble }).pts.length <= 96);
|
|
33
|
+
assert.equal(simplify([[0, 0], [0.5, 0.0001], [1, 0]], 0.004).length, 2);
|
|
34
|
+
assert.equal(smooth([[0, 0], [1, 0], [1, 1]], false)[0][0], 0);
|
|
35
|
+
|
|
36
|
+
// the motif reads the drawing from the state it is given
|
|
37
|
+
const els = drawnMotif()({ state: { d: encodeDrawing(drawing) }, bands: true });
|
|
38
|
+
assert.equal(els.length, 5);
|
|
39
|
+
assert.equal(els[0].t, 'stroke'); assert.equal(els[4].t, 'band');
|
|
40
|
+
assert.deepEqual(drawnMotif()({ state: {}, bands: false }), []);
|
|
41
|
+
|
|
42
|
+
// crc32 of "IEND" is the last four bytes of every PNG
|
|
43
|
+
assert.equal(crc32(new TextEncoder().encode('IEND')), 0xae426082);
|
|
44
|
+
// a 1x1 PNG gains a pHYs chunk after IHDR, and keeps one rather than two
|
|
45
|
+
const png = Uint8Array.from(Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==', 'base64'));
|
|
46
|
+
const out = pngWithDpi(png, 300);
|
|
47
|
+
assert.equal(out.length, png.length + 21);
|
|
48
|
+
assert.equal(String.fromCharCode(...out.subarray(37, 41)), 'pHYs');
|
|
49
|
+
assert.equal(new DataView(out.buffer).getUint32(41), 11811);
|
|
50
|
+
const again = pngWithDpi(out, 150);
|
|
51
|
+
assert.equal(again.length, out.length);
|
|
52
|
+
assert.equal(new DataView(again.buffer).getUint32(41), 5906);
|
|
53
|
+
console.log('draw: encoding, settling, motif and the print chunk hold');
|
package/test/loom.mjs
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// The loom's rule: the drafts, the sett and the cloth they make.
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { DRAFTS, DRAFT_NAMES, lifted, drawdown, parseSett, threadsOf, writeSett, dyeOf, weavePixels } from '../src/loom.js';
|
|
4
|
+
|
|
5
|
+
// plain weave is a chequer; 2/2 twill is over two, under two, stepping one a pick
|
|
6
|
+
for (let j = -3; j < 5; j++) for (let i = -3; i < 5; i++) {
|
|
7
|
+
assert.equal(lifted(DRAFTS.plain, i, j), (((i - j) % 2) + 2) % 2 === 0);
|
|
8
|
+
assert.equal(lifted(DRAFTS.twill, i, j), (((i - j) % 4) + 4) % 4 < 2);
|
|
9
|
+
}
|
|
10
|
+
// every draft is balanced enough to be a cloth: no end and no pick floats the whole repeat
|
|
11
|
+
for (const name of DRAFT_NAMES) {
|
|
12
|
+
const d = drawdown(DRAFTS[name]);
|
|
13
|
+
for (let j = 0; j < d.h; j++) { const s = d.m.slice(j * d.w, (j + 1) * d.w).reduce((a, b) => a + b, 0); assert.ok(s > 0 && s < d.w, name + ' pick ' + j); }
|
|
14
|
+
for (let i = 0; i < d.w; i++) { let s = 0; for (let j = 0; j < d.h; j++) s += d.m[j * d.w + i]; assert.ok(s > 0 && s < d.h, name + ' end ' + i); }
|
|
15
|
+
}
|
|
16
|
+
// in a 2/2 twill half of every repeat is warp; in drill three quarters
|
|
17
|
+
assert.equal(drawdown(DRAFTS.twill).m.reduce((a, b) => a + b, 0), 8);
|
|
18
|
+
assert.equal(drawdown(DRAFTS.drill).m.reduce((a, b) => a + b, 0), 12);
|
|
19
|
+
|
|
20
|
+
// a sett turns back at its pivots, which are counted once
|
|
21
|
+
const s = parseSett('K4 R24 K24 Y4');
|
|
22
|
+
assert.deepEqual(s.stripes, [{ code: 'K', n: 4 }, { code: 'R', n: 24 }, { code: 'K', n: 24 }, { code: 'Y', n: 4 }]);
|
|
23
|
+
assert.equal(s.mirror, true);
|
|
24
|
+
assert.equal(threadsOf(s).length, 4 + 24 + 24 + 4 + 24 + 24);
|
|
25
|
+
assert.equal(threadsOf(s).join('').replace(/(.)\1*/g, '$1'), 'KRKYKR');
|
|
26
|
+
assert.equal(writeSett(s), 'K/4 R24 K24 Y/4');
|
|
27
|
+
// the register's slashes are read; Royal Stewart is 190 threads
|
|
28
|
+
assert.equal(threadsOf(parseSett('R/72 B8 K12 Y2 K2 W2 K2 G16 R8 K2 R4 W/2')).length, 190);
|
|
29
|
+
// "..." says it repeats without turning
|
|
30
|
+
const h = parseSett('k4 w4 ...');
|
|
31
|
+
assert.equal(h.mirror, false);
|
|
32
|
+
assert.equal(threadsOf(h).join(''), 'KKKKWWWW');
|
|
33
|
+
// two stripes have nothing between the pivots to turn back through
|
|
34
|
+
assert.equal(threadsOf(parseSett('R/8 K/8')).length, 16);
|
|
35
|
+
// hex colours, light and dark, nonsense
|
|
36
|
+
assert.deepEqual(parseSett('#C8102E 6 LB 2').stripes, [{ code: '#c8102e', n: 6 }, { code: 'LB', n: 2 }]);
|
|
37
|
+
assert.deepEqual(dyeOf('#c8102e', 'modern'), [200, 16, 46]);
|
|
38
|
+
assert.deepEqual(dyeOf('K', 'modern'), [16, 16, 16]);
|
|
39
|
+
assert.ok(dyeOf('LB', 'modern')[2] > dyeOf('B', 'modern')[2] && dyeOf('DB', 'modern')[2] < dyeOf('B', 'modern')[2]);
|
|
40
|
+
assert.deepEqual(dyeOf('ZZ', 'modern'), dyeOf('N', 'modern'));
|
|
41
|
+
assert.deepEqual(threadsOf(parseSett('')), ['N']);
|
|
42
|
+
assert.deepEqual(parseSett('hello world').stripes, []);
|
|
43
|
+
|
|
44
|
+
// the cloth: red warp, white weft, plain weave is a chequer of the two
|
|
45
|
+
const px = weavePixels(new Uint8ClampedArray(4 * 4 * 4), 4, 4, 0, 0, [[200, 0, 0]], [[255, 255, 255]], DRAFTS.plain, 0);
|
|
46
|
+
assert.deepEqual([...px.slice(0, 8)], [200, 0, 0, 255, 255, 255, 255, 255]);
|
|
47
|
+
assert.deepEqual([...px.slice(16, 24)], [255, 255, 255, 255, 200, 0, 0, 255]);
|
|
48
|
+
// and it is the same cloth wherever the window starts
|
|
49
|
+
const a = weavePixels(new Uint8ClampedArray(8 * 8 * 4), 8, 8, -5, 3, [[1, 2, 3], [9, 9, 9], [50, 60, 70]], [[7, 7, 7], [100, 0, 0]], DRAFTS.herringbone, 0.2);
|
|
50
|
+
const b = weavePixels(new Uint8ClampedArray(9 * 9 * 4), 9, 9, -6, 2, [[1, 2, 3], [9, 9, 9], [50, 60, 70]], [[7, 7, 7], [100, 0, 0]], DRAFTS.herringbone, 0.2);
|
|
51
|
+
for (let y = 0; y < 8; y++) for (let x = 0; x < 8; x++) for (let k = 0; k < 4; k++) assert.equal(a[(y * 8 + x) * 4 + k], b[((y + 1) * 9 + x + 1) * 4 + k]);
|
|
52
|
+
console.log('loom ok');
|