@arraypress/effects-astro 0.14.0 → 0.15.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/COMPONENTS.md +101 -0
- package/package.json +1 -1
- package/src/components/LensFlare.astro +38 -0
- package/src/components/PaintDrip.astro +40 -0
- package/src/components/PhotoBokeh.astro +40 -0
- package/src/components/SunWall.astro +37 -0
- package/src/index.d.ts +9 -0
- package/src/index.js +5 -0
- package/src/runtime/glass-drop.d.ts +14 -0
- package/src/runtime/glass-drop.js +116 -6
- package/src/runtime/glass-plates.d.ts +63 -0
- package/src/runtime/glass-plates.js +327 -0
- package/src/runtime/glass-stack.d.ts +10 -0
- package/src/runtime/glass-stack.js +71 -3
- package/src/runtime/lens-flare.d.ts +45 -0
- package/src/runtime/lens-flare.js +283 -0
- package/src/runtime/paint-drip.d.ts +89 -0
- package/src/runtime/paint-drip.js +611 -0
- package/src/runtime/photo-bokeh.d.ts +57 -0
- package/src/runtime/photo-bokeh.js +351 -0
- package/src/runtime/shared-gl.d.ts +25 -0
- package/src/runtime/shared-gl.js +229 -0
- package/src/runtime/stage.js +18 -6
- package/src/runtime/sun-wall.d.ts +52 -0
- package/src/runtime/sun-wall.js +309 -0
|
@@ -0,0 +1,611 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @arraypress/effects-astro/paint-drip
|
|
3
|
+
*
|
|
4
|
+
* A mark sprayed in paint, the paint running: wet paint pools along the
|
|
5
|
+
* mark's lower edges, and where a pool grows past what surface tension holds
|
|
6
|
+
* a drip breaks away. Each drip falls under gravity against the paint's
|
|
7
|
+
* viscosity, leaving paint behind as it goes, so its head (a rounded bead)
|
|
8
|
+
* shrinks and slows and its body thins; it stops when too little is left to
|
|
9
|
+
* move it, or when it reaches something below, and dries (its wet sheen
|
|
10
|
+
* fades). Several drips of different volumes start at different times; they
|
|
11
|
+
* hold, fade, and the paint runs again, on a loop.
|
|
12
|
+
*
|
|
13
|
+
* The physics is plain JavaScript and pure (`dripRun`, `dripAnchors`,
|
|
14
|
+
* `dripPlan`): the same seed draws the same drips, and the still (every drip
|
|
15
|
+
* at its full length) is the loop's resting frame. Drawn on a 2D canvas: the
|
|
16
|
+
* ground with a soft light and grain, the mark tinted with a sprayed edge, the
|
|
17
|
+
* drips, a glow, and grain over the paint.
|
|
18
|
+
*
|
|
19
|
+
* Opaque: it paints its `background` everywhere. With prefers-reduced-motion
|
|
20
|
+
* (or `speed: 0`) it is one still frame, the drips at full length.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { createCanvas2D, grainPattern, grainOnto } from './canvas2d.js';
|
|
24
|
+
import { loadImage } from './image.js';
|
|
25
|
+
import { parseColor, mixOklab, toHex } from './color.js';
|
|
26
|
+
import { seededRandom as mulberry32 } from './random.js';
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @typedef {object} PaintDripConfig
|
|
30
|
+
* @property {string} [src] The mark, as an image URL (a white SVG works; any colour, only its alpha is used).
|
|
31
|
+
* @property {string} [color='#C8FF00'] The paint.
|
|
32
|
+
* @property {string} [background='#85857F'] The ground, painted everywhere.
|
|
33
|
+
* @property {{ x: number, y: number, size: number }} [box] Where the mark sits: its centre (0–1 from the top left) and its edge (a share of the height).
|
|
34
|
+
* @property {number} [drips=3] How many drips run.
|
|
35
|
+
* @property {number} [volume=1] How much paint each drip carries (longer runs).
|
|
36
|
+
* @property {number} [floor=0.84] No drip runs past this, a share of the height from the top (the name's room).
|
|
37
|
+
* @property {number} [glow=0.55] How much the paint glows; 0 for none.
|
|
38
|
+
* @property {number} [spray=0.6] How rough the sprayed edge is; 0 for a clean edge.
|
|
39
|
+
* @property {number} [grain=0.12] Grain over the ground and the paint; 0 for none.
|
|
40
|
+
* @property {number} [hold=2.6] Seconds the finished drips hold before they fade and run again.
|
|
41
|
+
* @property {number} [seed=0] Which drips (where, how big, when), so two cards differ.
|
|
42
|
+
* @property {DripAnchor[]} [anchors] Where the drips hang (from `dripAnchors` on the mark's coverage), when a still was
|
|
43
|
+
* made from them: the canvas then runs the same drips. Read off the image when not given.
|
|
44
|
+
* @property {number} [speed=1] Plays the clock faster or slower; 0 for one still frame.
|
|
45
|
+
*/
|
|
46
|
+
|
|
47
|
+
/** The settings' defaults. */
|
|
48
|
+
export const DRIP_DEFAULTS = {
|
|
49
|
+
color: '#C8FF00',
|
|
50
|
+
background: '#85857F',
|
|
51
|
+
box: { x: 0.5, y: 0.42, size: 0.5 },
|
|
52
|
+
drips: 3,
|
|
53
|
+
volume: 1,
|
|
54
|
+
floor: 0.84,
|
|
55
|
+
glow: 0.55,
|
|
56
|
+
spray: 0.6,
|
|
57
|
+
grain: 0.12,
|
|
58
|
+
hold: 2.6,
|
|
59
|
+
seed: 0,
|
|
60
|
+
speed: 1,
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* The paint's physics, in heights (shares of the canvas's height) and seconds. A drip's head holds `m` paint; it is
|
|
65
|
+
* a bead of radius `BEAD * √m`. Surface tension holds the first `YIELD` of it; gravity pulls the rest, viscosity
|
|
66
|
+
* drags it back, and it lays paint along its path in proportion to how fast it goes and how wide it is.
|
|
67
|
+
*/
|
|
68
|
+
const PHYSICS = {
|
|
69
|
+
/** Seconds per step. */
|
|
70
|
+
dt: 1 / 60,
|
|
71
|
+
/** Gravity on the free paint, heights / s². */
|
|
72
|
+
gravity: 1.6,
|
|
73
|
+
/** Viscous drag, per second. */
|
|
74
|
+
drag: 4.5,
|
|
75
|
+
/** Paint surface tension holds (a drip can't start below it, and stops when it falls back to it). */
|
|
76
|
+
yield: 0.35,
|
|
77
|
+
/** Bead radius per √paint, heights. */
|
|
78
|
+
bead: 0.02,
|
|
79
|
+
/** Paint laid behind, per height of path per bead radius. */
|
|
80
|
+
lay: 300,
|
|
81
|
+
/** The body's width, a share of the bead's width when it passed. */
|
|
82
|
+
body: 0.5,
|
|
83
|
+
/** Paint fed from the mark into the pool, per second, while it feeds. */
|
|
84
|
+
feed: 1.6,
|
|
85
|
+
/** Longest run, seconds (a drip that never stops is cut here). */
|
|
86
|
+
longest: 9,
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* @typedef {object} DripRun
|
|
91
|
+
* @property {number} t0 When the paint starts to pool, seconds into the loop.
|
|
92
|
+
* @property {number} duration How long until it stops, seconds from t0.
|
|
93
|
+
* @property {Float32Array} y The head's depth below its edge at each step, heights.
|
|
94
|
+
* @property {Float32Array} r The bead's radius at each step, heights.
|
|
95
|
+
* @property {Float32Array} v The head's speed at each step, heights / s.
|
|
96
|
+
* @property {Float32Array} w The body's width at each depth step (`DEPTH_STEP` apart), heights.
|
|
97
|
+
* @property {number} length Its final depth, heights.
|
|
98
|
+
*/
|
|
99
|
+
|
|
100
|
+
/** The body's widths are kept this far apart, heights. */
|
|
101
|
+
export const DEPTH_STEP = 0.002;
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Runs one drip from pooling to drying: its head's depth, size and speed at each step, and the width its body is
|
|
105
|
+
* left at along its path.
|
|
106
|
+
*
|
|
107
|
+
* @param {object} drip The drip.
|
|
108
|
+
* @param {number} drip.paint How much paint flows into it (its volume), about 0.5–1.6.
|
|
109
|
+
* @param {number} drip.room How far it can fall before it meets something, heights.
|
|
110
|
+
* @param {number} [drip.t0=0] When it starts pooling, seconds.
|
|
111
|
+
* @returns {DripRun} The run.
|
|
112
|
+
*/
|
|
113
|
+
export function dripRun({ paint, room, t0 = 0 }) {
|
|
114
|
+
const P = PHYSICS;
|
|
115
|
+
const ys = [],
|
|
116
|
+
rs = [],
|
|
117
|
+
vs = [],
|
|
118
|
+
ws = [];
|
|
119
|
+
let m = 0,
|
|
120
|
+
v = 0,
|
|
121
|
+
y = 0,
|
|
122
|
+
fed = 0,
|
|
123
|
+
moving = false,
|
|
124
|
+
t = 0;
|
|
125
|
+
const steps = Math.ceil(P.longest / P.dt);
|
|
126
|
+
for (let i = 0; i < steps; i++) {
|
|
127
|
+
// the pool fills from the mark until its paint has come
|
|
128
|
+
if (fed < paint) {
|
|
129
|
+
const add = Math.min(paint - fed, P.feed * P.dt);
|
|
130
|
+
m += add;
|
|
131
|
+
fed += add;
|
|
132
|
+
}
|
|
133
|
+
const r = P.bead * Math.sqrt(Math.max(m, 0));
|
|
134
|
+
// gravity on the free paint (above what surface tension holds), viscosity against it
|
|
135
|
+
const free = Math.max(0, m - P.yield);
|
|
136
|
+
const a = (P.gravity * free) / (m + 0.25);
|
|
137
|
+
if (free > 0) moving = true;
|
|
138
|
+
v += (a - P.drag * v) * P.dt;
|
|
139
|
+
if (v < 0) v = 0;
|
|
140
|
+
let dy = v * P.dt;
|
|
141
|
+
// it meets something below: the bead flattens against it and stops there
|
|
142
|
+
let met = false;
|
|
143
|
+
if (y + dy + r > room) {
|
|
144
|
+
dy = Math.max(0, room - r - y);
|
|
145
|
+
v = 0;
|
|
146
|
+
met = moving;
|
|
147
|
+
}
|
|
148
|
+
// paint laid along the path: the body, as wide as a share of the bead when it passed
|
|
149
|
+
if (dy > 0) {
|
|
150
|
+
const lost = Math.min(m - P.yield * 0.6, P.lay * r * dy);
|
|
151
|
+
m -= Math.max(0, lost);
|
|
152
|
+
const from = Math.floor(y / DEPTH_STEP),
|
|
153
|
+
to = Math.floor((y + dy) / DEPTH_STEP);
|
|
154
|
+
for (let k = from; k <= to; k++) if (ws[k] === undefined) ws[k] = 2 * r * P.body;
|
|
155
|
+
}
|
|
156
|
+
y += dy;
|
|
157
|
+
ys.push(y);
|
|
158
|
+
rs.push(P.bead * Math.sqrt(Math.max(m, 0)));
|
|
159
|
+
vs.push(v);
|
|
160
|
+
t += P.dt;
|
|
161
|
+
// dried: it moved, and now holds too little paint to go on (and has slowed to nothing)
|
|
162
|
+
if (moving && fed >= paint && ((v < 0.002 && m <= P.yield * 1.02) || met)) break;
|
|
163
|
+
}
|
|
164
|
+
// a body width at every depth down to the head
|
|
165
|
+
const n = Math.floor(y / DEPTH_STEP) + 1;
|
|
166
|
+
const w = new Float32Array(n);
|
|
167
|
+
let last = ws[0] ?? 2 * P.bead * P.body;
|
|
168
|
+
for (let k = 0; k < n; k++) w[k] = last = ws[k] ?? last;
|
|
169
|
+
return { t0, duration: t, y: Float32Array.from(ys), r: Float32Array.from(rs), v: Float32Array.from(vs), w, length: y };
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Where a drip runs from at a time: its head's depth, bead and speed.
|
|
174
|
+
*
|
|
175
|
+
* @param {DripRun} run The run.
|
|
176
|
+
* @param {number} t Seconds into the loop.
|
|
177
|
+
* @returns {{ y: number, r: number, v: number, started: boolean, done: boolean }} The head.
|
|
178
|
+
*/
|
|
179
|
+
export function dripAt(run, t) {
|
|
180
|
+
const s = t - run.t0;
|
|
181
|
+
if (s <= 0) return { y: 0, r: 0, v: 0, started: false, done: false };
|
|
182
|
+
const i = Math.min(run.y.length - 1, Math.floor(s / PHYSICS.dt));
|
|
183
|
+
return { y: run.y[i], r: run.r[i], v: run.v[i], started: true, done: i === run.y.length - 1 };
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* @typedef {object} DripAnchor
|
|
188
|
+
* @property {number} x Across, a share of the box (0–1).
|
|
189
|
+
* @property {number} y Down, a share of the box (0–1): the edge the paint hangs from.
|
|
190
|
+
* @property {number} room How far below it is clear, a share of the box (to the box's foot when it runs out of the box).
|
|
191
|
+
* @property {boolean} open Whether it runs clear out of the box (nothing of the mark below it).
|
|
192
|
+
*/
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* The places paint drips from: points on the mark's outer lower silhouette (ink above, nothing of the mark anywhere
|
|
196
|
+
* below, so never inside a counter or off a stripe end within it), the gentler the slope and the lower on the mark the
|
|
197
|
+
* likelier, the lowest points of a run likelier still (paint gathers where an edge turns up again), picked apart.
|
|
198
|
+
*
|
|
199
|
+
* @param {ArrayLike<number>} alpha The mark's coverage, 0–1, row by row.
|
|
200
|
+
* @param {number} w Its width, cells.
|
|
201
|
+
* @param {number} h Its height, cells.
|
|
202
|
+
* @param {number} count How many.
|
|
203
|
+
* @param {number} seed Which.
|
|
204
|
+
* @returns {DripAnchor[]} The places, left to right.
|
|
205
|
+
*/
|
|
206
|
+
export function dripAnchors(alpha, w, h, count, seed) {
|
|
207
|
+
const ink = (x, y) => x >= 0 && y >= 0 && x < w && y < h && alpha[y * w + x] >= 0.5;
|
|
208
|
+
/** The edge each column hangs from: every ink cell with 3 clear below it, and how far it is clear. */
|
|
209
|
+
const edges = /** @type {{ x: number, y: number, room: number, open: boolean }[][]} */ (Array.from({ length: w }, () => []));
|
|
210
|
+
for (let x = 0; x < w; x++)
|
|
211
|
+
for (let y = 0; y < h - 1; y++)
|
|
212
|
+
if (ink(x, y) && !ink(x, y + 1) && !ink(x, y + 2) && !ink(x, y + 3)) {
|
|
213
|
+
let k = y + 1;
|
|
214
|
+
while (k < h && !ink(x, k)) k++;
|
|
215
|
+
edges[x].push({ x, y, room: k - y - 1, open: k >= h });
|
|
216
|
+
}
|
|
217
|
+
/** The edge in column x nearest a depth. */
|
|
218
|
+
const near = (x, y) => (edges[x] ?? []).reduce((best, e) => (!best || Math.abs(e.y - y) < Math.abs(best.y - y) ? e : best), null);
|
|
219
|
+
const span = Math.max(3, Math.round(w * 0.02));
|
|
220
|
+
const candidates = [];
|
|
221
|
+
for (let x = span; x < w - span; x++)
|
|
222
|
+
for (const e of edges[x]) {
|
|
223
|
+
const a = near(x - span, e.y),
|
|
224
|
+
b = near(x + span, e.y);
|
|
225
|
+
if (!a || !b || Math.abs(a.y - e.y) > span * 2 || Math.abs(b.y - e.y) > span * 2) continue;
|
|
226
|
+
const slope = Math.abs(b.y - a.y) / (2 * span);
|
|
227
|
+
if (slope > 1.3) continue; // too steep to hold paint: it runs down the edge instead
|
|
228
|
+
const lowest = a.y <= e.y && b.y <= e.y;
|
|
229
|
+
const room = e.room / h;
|
|
230
|
+
// only the outer lower silhouette: nothing of the mark below (never a counter, never a stripe end inside it)
|
|
231
|
+
if (!e.open) continue;
|
|
232
|
+
const weight = (0.08 + (e.y / h) ** 2) * (lowest ? 3 : 1) * (1 - slope * 0.5);
|
|
233
|
+
if (weight > 0) candidates.push({ x: e.x / w, y: (e.y + 1) / h, room, open: e.open, weight });
|
|
234
|
+
}
|
|
235
|
+
const rand = mulberry32(Math.floor(seed * 1000) + 17);
|
|
236
|
+
const picked = /** @type {DripAnchor[]} */ ([]);
|
|
237
|
+
const gap = 0.11;
|
|
238
|
+
for (let n = 0; n < count && candidates.length; n++) {
|
|
239
|
+
const pool = candidates.filter((c) => picked.every((p) => Math.abs(p.x - c.x) >= gap));
|
|
240
|
+
if (!pool.length) break;
|
|
241
|
+
const total = pool.reduce((s, c) => s + c.weight, 0);
|
|
242
|
+
let at = rand() * total;
|
|
243
|
+
const c = pool.find((q) => (at -= q.weight) <= 0) ?? pool[pool.length - 1];
|
|
244
|
+
picked.push({ x: c.x, y: c.y, room: c.room, open: c.open });
|
|
245
|
+
}
|
|
246
|
+
return picked.sort((a, b) => a.x - b.x);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* @typedef {object} DripPlan
|
|
251
|
+
* @property {{ x: number, y: number, room: number, run: DripRun }[]} drips Each drip: where it hangs (heights from the top left) and its run.
|
|
252
|
+
* @property {number} cycle The loop's length, seconds.
|
|
253
|
+
* @property {number} settled When the last drip has stopped, seconds.
|
|
254
|
+
*/
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Every drip's run for a mark: where each hangs, how much paint it carries, when it starts, and the loop around them.
|
|
258
|
+
*
|
|
259
|
+
* @param {DripAnchor[]} anchors From dripAnchors.
|
|
260
|
+
* @param {object} at Where the mark is: its box's left and top, and its edge, heights.
|
|
261
|
+
* @param {number} at.left The box's left, heights.
|
|
262
|
+
* @param {number} at.top The box's top, heights.
|
|
263
|
+
* @param {number} at.size The box's edge, heights.
|
|
264
|
+
* @param {PaintDripConfig} config The settings.
|
|
265
|
+
* @returns {DripPlan} The plan.
|
|
266
|
+
*/
|
|
267
|
+
export function dripPlan(anchors, at, config) {
|
|
268
|
+
const o = { ...DRIP_DEFAULTS, ...config };
|
|
269
|
+
const rand = mulberry32(Math.floor(o.seed * 1000) + 101);
|
|
270
|
+
let t = 0.3 + rand() * 0.4;
|
|
271
|
+
const drips = anchors.map((a) => {
|
|
272
|
+
const x = at.left + a.x * at.size,
|
|
273
|
+
y = at.top + a.y * at.size;
|
|
274
|
+
const room = Math.max(0.01, a.open ? o.floor - y : a.room * at.size - 0.004);
|
|
275
|
+
const paint = o.volume * (0.55 + rand() * 1.05);
|
|
276
|
+
const run = dripRun({ paint, room, t0: t });
|
|
277
|
+
t += 0.35 + rand() * 1.1;
|
|
278
|
+
return { x, y, room, run };
|
|
279
|
+
});
|
|
280
|
+
const settled = drips.reduce((s, d) => Math.max(s, d.run.t0 + d.run.duration), 0);
|
|
281
|
+
return { drips, settled, cycle: settled + o.hold + FADE + 0.6 };
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/** Seconds the drips take to fade before the paint runs again. */
|
|
285
|
+
const FADE = 0.9;
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* How far into the loop a frame is, and how visible the drips are: they fade out at the loop's end.
|
|
289
|
+
*
|
|
290
|
+
* @param {DripPlan} plan The plan.
|
|
291
|
+
* @param {number} time Seconds since it started (already at its speed).
|
|
292
|
+
* @returns {{ t: number, fade: number }} Seconds into the loop, and the drips' opacity.
|
|
293
|
+
*/
|
|
294
|
+
export function dripClock(plan, time) {
|
|
295
|
+
const t = ((time % plan.cycle) + plan.cycle) % plan.cycle;
|
|
296
|
+
const fadeAt = plan.cycle - FADE - 0.6;
|
|
297
|
+
const fade = t < fadeAt ? 1 : t < fadeAt + FADE ? 1 - (t - fadeAt) / FADE : 0;
|
|
298
|
+
return { t, fade };
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* A drip's outline at a time, as SVG path data in heights (x from the left, y from the top): a fillet where it leaves
|
|
303
|
+
* the edge, its body down to the head, the bead (a little teardrop while it moves). The canvas fills it (`Path2D`), and
|
|
304
|
+
* a still draws it, so both show the same drip.
|
|
305
|
+
*
|
|
306
|
+
* @param {DripPlan['drips'][number]} d The drip.
|
|
307
|
+
* @param {number} t Seconds into the loop.
|
|
308
|
+
* @returns {string} The path data, or '' before it starts.
|
|
309
|
+
*/
|
|
310
|
+
export function dripPath(d, t) {
|
|
311
|
+
const head = dripAt(d.run, t);
|
|
312
|
+
if (!head.started) return '';
|
|
313
|
+
const f = (n) => (Math.round(n * 1e4) / 1e4).toString();
|
|
314
|
+
const { x: x0, y: y0 } = d;
|
|
315
|
+
const depth = head.y;
|
|
316
|
+
const rb = Math.max(head.r, 0.002);
|
|
317
|
+
const sway = (s) => Math.sin(s * 23 + d.x * 40) * Math.min(0.0036, s * 0.02);
|
|
318
|
+
const widthAt = (s) => d.run.w[Math.min(d.run.w.length - 1, Math.max(0, Math.floor(s / DEPTH_STEP)))] ?? rb;
|
|
319
|
+
const w0 = widthAt(0);
|
|
320
|
+
const fillet = Math.max(w0 * 0.9, rb * 0.8);
|
|
321
|
+
const neck = Math.max(0, depth - rb * 0.6);
|
|
322
|
+
const steps = Math.max(2, Math.ceil(neck / 0.01));
|
|
323
|
+
const stretch = 1 + Math.min(0.45, head.v * 6);
|
|
324
|
+
const bx = x0 + sway(depth),
|
|
325
|
+
by = y0 + depth;
|
|
326
|
+
const left = [],
|
|
327
|
+
right = [];
|
|
328
|
+
for (let i = 1; i <= steps; i++) {
|
|
329
|
+
const sd = (neck * i) / steps;
|
|
330
|
+
if (sd <= fillet) continue; // the fillet covers the top of the body
|
|
331
|
+
left.push(`L${f(x0 + sway(sd) - widthAt(sd) / 2)} ${f(y0 + sd)}`);
|
|
332
|
+
right.unshift(`L${f(x0 + sway(sd) + widthAt(sd) / 2)} ${f(y0 + sd)}`);
|
|
333
|
+
}
|
|
334
|
+
return (
|
|
335
|
+
`M${f(x0 - w0 / 2 - fillet)} ${f(y0 - 0.001)}Q${f(x0 - w0 / 2)} ${f(y0)} ${f(x0 - w0 / 2)} ${f(y0 + fillet)}` +
|
|
336
|
+
left.join('') +
|
|
337
|
+
`L${f(bx - rb)} ${f(by)}A${f(rb)} ${f(rb * stretch)} 0 0 0 ${f(bx + rb)} ${f(by)}` +
|
|
338
|
+
right.join('') +
|
|
339
|
+
`L${f(x0 + w0 / 2)} ${f(y0 + fillet)}Q${f(x0 + w0 / 2)} ${f(y0)} ${f(x0 + w0 / 2 + fillet)} ${f(y0 - 0.001)}Z`
|
|
340
|
+
);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* Every drip at its full length (the loop's resting frame), as one SVG path in heights: what a still shows.
|
|
345
|
+
*
|
|
346
|
+
* @param {DripPlan} plan The plan.
|
|
347
|
+
* @returns {string} The path data.
|
|
348
|
+
*/
|
|
349
|
+
export const dripsAtRest = (plan) => plan.drips.map((d) => dripPath(d, plan.settled + 0.01)).join('');
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* A CSS `background` that stands in for the ground before the canvas draws: its colour with the soft light in its
|
|
353
|
+
* middle.
|
|
354
|
+
*
|
|
355
|
+
* @param {PaintDripConfig} config The settings.
|
|
356
|
+
* @returns {string} The CSS background.
|
|
357
|
+
*/
|
|
358
|
+
export function dripStill(config) {
|
|
359
|
+
const o = { ...DRIP_DEFAULTS, ...config };
|
|
360
|
+
const lit = toHex(mixOklab(parseColor(o.background), parseColor('#ffffff'), 0.12));
|
|
361
|
+
return `radial-gradient(120% 90% at 50% 38%, ${lit} 0%, ${o.background} 70%)`;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* The paint's sheen: a lighter tint of it for the wet highlight.
|
|
366
|
+
*
|
|
367
|
+
* @param {string} color The paint.
|
|
368
|
+
* @returns {string} The highlight.
|
|
369
|
+
*/
|
|
370
|
+
const sheenOf = (color) => toHex(mixOklab(parseColor(color), parseColor('#ffffff'), 0.65));
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* Starts the paint running on an element: a canvas inside it (its first), the mark loaded from `src`.
|
|
374
|
+
*
|
|
375
|
+
* @param {HTMLElement} el The element.
|
|
376
|
+
* @param {PaintDripConfig} config The settings.
|
|
377
|
+
* @returns {() => void} Stops it.
|
|
378
|
+
*/
|
|
379
|
+
export function paintDrip(el, config) {
|
|
380
|
+
const o = { ...DRIP_DEFAULTS, ...config, box: { ...DRIP_DEFAULTS.box, ...config.box } };
|
|
381
|
+
const sheen = sheenOf(o.color);
|
|
382
|
+
/** @type {HTMLImageElement | null} */
|
|
383
|
+
let image = null;
|
|
384
|
+
/** @type {DripPlan | null} */
|
|
385
|
+
let plan = null;
|
|
386
|
+
/** The ground and the sprayed mark, drawn once a size. */
|
|
387
|
+
let ground = /** @type {HTMLCanvasElement | null} */ (null);
|
|
388
|
+
let mark = /** @type {HTMLCanvasElement | null} */ (null);
|
|
389
|
+
/** The paint's layer, redrawn each frame it changes. */
|
|
390
|
+
const paint = document.createElement('canvas');
|
|
391
|
+
const pctx = paint.getContext('2d');
|
|
392
|
+
let pattern = /** @type {CanvasPattern | null} */ (null);
|
|
393
|
+
let lastKey = '';
|
|
394
|
+
const start = performance.now();
|
|
395
|
+
const still = o.speed <= 0;
|
|
396
|
+
|
|
397
|
+
/**
|
|
398
|
+
* The mark's box on the canvas, CSS px.
|
|
399
|
+
*
|
|
400
|
+
* @param {number} W Width.
|
|
401
|
+
* @param {number} H Height.
|
|
402
|
+
*/
|
|
403
|
+
const boxOf = (W, H) => {
|
|
404
|
+
const size = o.box.size * H;
|
|
405
|
+
return { left: o.box.x * W - size / 2, top: o.box.y * H - size / 2, size };
|
|
406
|
+
};
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* Draws the ground (its colour, a soft light, grain) and the mark (tinted, its edge sprayed), and plans the drips,
|
|
410
|
+
* for a size.
|
|
411
|
+
*
|
|
412
|
+
* @param {import('./canvas2d.js').Canvas2D} c The canvas.
|
|
413
|
+
*/
|
|
414
|
+
const prepare = (c) => {
|
|
415
|
+
const { width: W, height: H, dpr } = c;
|
|
416
|
+
ground = document.createElement('canvas');
|
|
417
|
+
ground.width = Math.round(W * dpr);
|
|
418
|
+
ground.height = Math.round(H * dpr);
|
|
419
|
+
const g = ground.getContext('2d');
|
|
420
|
+
if (!g) return;
|
|
421
|
+
g.scale(dpr, dpr);
|
|
422
|
+
g.fillStyle = o.background;
|
|
423
|
+
g.fillRect(0, 0, W, H);
|
|
424
|
+
const light = g.createRadialGradient(W * 0.5, H * 0.36, 0, W * 0.5, H * 0.36, H * 0.75);
|
|
425
|
+
light.addColorStop(0, 'rgba(255,255,255,0.13)');
|
|
426
|
+
light.addColorStop(1, 'rgba(255,255,255,0)');
|
|
427
|
+
g.fillStyle = light;
|
|
428
|
+
g.fillRect(0, 0, W, H);
|
|
429
|
+
const vignette = g.createRadialGradient(W * 0.5, H * 0.45, H * 0.35, W * 0.5, H * 0.45, H * 0.85);
|
|
430
|
+
vignette.addColorStop(0, 'rgba(0,0,0,0)');
|
|
431
|
+
vignette.addColorStop(1, 'rgba(0,0,0,0.22)');
|
|
432
|
+
g.fillStyle = vignette;
|
|
433
|
+
g.fillRect(0, 0, W, H);
|
|
434
|
+
const gp = grainPattern(g);
|
|
435
|
+
if (gp && o.grain > 0) {
|
|
436
|
+
g.globalAlpha = o.grain * 0.6;
|
|
437
|
+
g.globalCompositeOperation = 'overlay';
|
|
438
|
+
g.fillStyle = gp;
|
|
439
|
+
g.fillRect(0, 0, W, H);
|
|
440
|
+
g.globalAlpha = 1;
|
|
441
|
+
g.globalCompositeOperation = 'source-over';
|
|
442
|
+
}
|
|
443
|
+
paint.width = Math.round(W * dpr);
|
|
444
|
+
paint.height = Math.round(H * dpr);
|
|
445
|
+
pattern = pctx ? grainPattern(pctx) : null;
|
|
446
|
+
if (!image) return;
|
|
447
|
+
const box = boxOf(W, H);
|
|
448
|
+
// the mark tinted, its soft edge broken into spray: a slightly blurred copy whose half-covered pixels are kept
|
|
449
|
+
// or dropped at random
|
|
450
|
+
const px = Math.max(8, Math.round(box.size * dpr));
|
|
451
|
+
mark = document.createElement('canvas');
|
|
452
|
+
mark.width = px;
|
|
453
|
+
mark.height = px;
|
|
454
|
+
const k = mark.getContext('2d', { willReadFrequently: true });
|
|
455
|
+
if (!k) return;
|
|
456
|
+
k.filter = o.spray > 0 ? `blur(${(0.25 + o.spray * 0.55) * dpr}px)` : 'none';
|
|
457
|
+
k.drawImage(image, 0, 0, px, px);
|
|
458
|
+
k.filter = 'none';
|
|
459
|
+
const data = k.getImageData(0, 0, px, px);
|
|
460
|
+
const alpha = new Float32Array(px * px);
|
|
461
|
+
const rand = mulberry32(Math.floor(o.seed * 1000) + 7);
|
|
462
|
+
const [cr, cg, cb] = parseColor(o.color).map((v) => Math.round(v * 255));
|
|
463
|
+
for (let i = 0; i < px * px; i++) {
|
|
464
|
+
const a = data.data[i * 4 + 3] / 255;
|
|
465
|
+
alpha[i] = a;
|
|
466
|
+
// inside the edge solid; in the soft fringe kept or dropped at random, the outer half rarely (a fine speckle)
|
|
467
|
+
const kept = o.spray > 0 && a > 0.08 && a < 0.92 ? (rand() < (a < 0.5 ? a * a : a) ? 1 : 0) : a >= 0.5 ? 1 : 0;
|
|
468
|
+
data.data[i * 4] = cr;
|
|
469
|
+
data.data[i * 4 + 1] = cg;
|
|
470
|
+
data.data[i * 4 + 2] = cb;
|
|
471
|
+
data.data[i * 4 + 3] = kept * 255;
|
|
472
|
+
}
|
|
473
|
+
k.putImageData(data, 0, 0);
|
|
474
|
+
// where the paint drips from: given (so a still made from the same plan matches), or read off the mark
|
|
475
|
+
if (o.anchors) {
|
|
476
|
+
plan = dripPlan(o.anchors, { left: box.left / H, top: box.top / H, size: box.size / H }, o);
|
|
477
|
+
lastKey = '';
|
|
478
|
+
return;
|
|
479
|
+
}
|
|
480
|
+
const n = 160;
|
|
481
|
+
const probe = document.createElement('canvas');
|
|
482
|
+
probe.width = n;
|
|
483
|
+
probe.height = n;
|
|
484
|
+
const q = probe.getContext('2d', { willReadFrequently: true });
|
|
485
|
+
if (!q) return;
|
|
486
|
+
q.drawImage(image, 0, 0, n, n);
|
|
487
|
+
const pd = q.getImageData(0, 0, n, n).data;
|
|
488
|
+
const cover = new Float32Array(n * n);
|
|
489
|
+
for (let i = 0; i < n * n; i++) cover[i] = pd[i * 4 + 3] / 255;
|
|
490
|
+
const anchors = dripAnchors(cover, n, n, o.drips, o.seed);
|
|
491
|
+
plan = dripPlan(anchors, { left: box.left / H, top: box.top / H, size: box.size / H }, o);
|
|
492
|
+
lastKey = '';
|
|
493
|
+
};
|
|
494
|
+
|
|
495
|
+
/**
|
|
496
|
+
* Draws one drip on the paint layer: its body down to the head, a fillet where it leaves the edge, the bead, and
|
|
497
|
+
* while wet a sheen down one side and a glint on the bead.
|
|
498
|
+
*
|
|
499
|
+
* @param {CanvasRenderingContext2D} p The paint layer, in CSS px.
|
|
500
|
+
* @param {DripPlan['drips'][number]} d The drip.
|
|
501
|
+
* @param {number} t Seconds into the loop.
|
|
502
|
+
* @param {number} H The canvas's height, px.
|
|
503
|
+
*/
|
|
504
|
+
const drawDrip = (p, d, t, H) => {
|
|
505
|
+
const head = dripAt(d.run, t);
|
|
506
|
+
if (!head.started) return;
|
|
507
|
+
p.save();
|
|
508
|
+
p.scale(H, H);
|
|
509
|
+
p.fill(new Path2D(dripPath(d, t)));
|
|
510
|
+
p.restore();
|
|
511
|
+
const x0 = d.x * H,
|
|
512
|
+
y0 = d.y * H;
|
|
513
|
+
const depth = head.y * H;
|
|
514
|
+
const rb = Math.max(head.r * H, 0.6);
|
|
515
|
+
const sway = (sp) => Math.sin((sp / H) * 23 + d.x * 40) * Math.min(0.0036, (sp / H) * 0.02) * H;
|
|
516
|
+
const widthAt = (sp) => (d.run.w[Math.min(d.run.w.length - 1, Math.max(0, Math.floor(sp / H / DEPTH_STEP)))] ?? head.r) * H;
|
|
517
|
+
const w0 = widthAt(0);
|
|
518
|
+
const fillet = Math.max(w0 * 0.9, rb * 0.8);
|
|
519
|
+
const neck = Math.max(0, depth - rb * 0.6);
|
|
520
|
+
const stretch = 1 + Math.min(0.45, head.v * 6);
|
|
521
|
+
const bx = x0 + sway(depth),
|
|
522
|
+
by = y0 + depth;
|
|
523
|
+
// wet: a sheen down the body and a glint on the bead, fading as the paint dries
|
|
524
|
+
const since = head.done ? t - (d.run.t0 + d.run.duration) : 0;
|
|
525
|
+
const wet = head.done ? Math.max(0, 1 - since / 1.8) : 1;
|
|
526
|
+
if (wet > 0.01 && depth > rb) {
|
|
527
|
+
p.save();
|
|
528
|
+
p.globalAlpha = 0.45 * wet;
|
|
529
|
+
p.strokeStyle = sheen;
|
|
530
|
+
p.lineWidth = Math.max(0.6, w0 * 0.18);
|
|
531
|
+
p.lineCap = 'round';
|
|
532
|
+
p.beginPath();
|
|
533
|
+
p.moveTo(x0 - widthAt(0) * 0.18, y0 + fillet);
|
|
534
|
+
for (let s = fillet; s < neck; s += 3) p.lineTo(x0 + sway(s) - widthAt(s) * 0.18, y0 + s);
|
|
535
|
+
p.stroke();
|
|
536
|
+
p.globalAlpha = 0.7 * wet;
|
|
537
|
+
p.fillStyle = sheen;
|
|
538
|
+
p.beginPath();
|
|
539
|
+
p.ellipse(bx - rb * 0.35, by - rb * 0.1 * stretch, rb * 0.22, rb * 0.32 * stretch, -0.3, 0, Math.PI * 2);
|
|
540
|
+
p.fill();
|
|
541
|
+
p.restore();
|
|
542
|
+
}
|
|
543
|
+
};
|
|
544
|
+
|
|
545
|
+
/**
|
|
546
|
+
* One frame: the ground, then the paint (the mark and the drips, grain on them) with its glow.
|
|
547
|
+
*
|
|
548
|
+
* @param {import('./canvas2d.js').Canvas2D} c The canvas.
|
|
549
|
+
* @param {number} now The time, ms.
|
|
550
|
+
*/
|
|
551
|
+
const draw = (c, now) => {
|
|
552
|
+
const { ctx, width: W, height: H, dpr } = c;
|
|
553
|
+
if (!ground || !pctx) return;
|
|
554
|
+
const time = still || c.still ? Infinity : ((now - start) / 1000) * o.speed;
|
|
555
|
+
const clock = plan ? (time === Infinity ? { t: plan.settled + 0.01, fade: 1 } : dripClock(plan, time)) : { t: 0, fade: 1 };
|
|
556
|
+
// only when something changed: a moving drip, the fade, or a new size
|
|
557
|
+
const moving = plan?.drips.some((d) => {
|
|
558
|
+
const h = dripAt(d.run, clock.t);
|
|
559
|
+
return h.started && (!h.done || clock.t - (d.run.t0 + d.run.duration) < 1.9);
|
|
560
|
+
});
|
|
561
|
+
const key = `${W}x${H}:${moving || clock.fade < 1 ? clock.t.toFixed(3) : 'rest'}:${clock.fade.toFixed(2)}:${Boolean(mark)}`;
|
|
562
|
+
if (key === lastKey) return;
|
|
563
|
+
lastKey = key;
|
|
564
|
+
ctx.drawImage(ground, 0, 0, W, H);
|
|
565
|
+
pctx.setTransform(1, 0, 0, 1, 0, 0);
|
|
566
|
+
pctx.clearRect(0, 0, paint.width, paint.height);
|
|
567
|
+
pctx.setTransform(dpr, 0, 0, dpr, 0, 0);
|
|
568
|
+
if (mark) {
|
|
569
|
+
const box = boxOf(W, H);
|
|
570
|
+
pctx.drawImage(mark, box.left, box.top, box.size, box.size);
|
|
571
|
+
}
|
|
572
|
+
if (plan && clock.fade > 0) {
|
|
573
|
+
pctx.save();
|
|
574
|
+
pctx.globalAlpha = clock.fade;
|
|
575
|
+
pctx.fillStyle = o.color;
|
|
576
|
+
for (const d of plan.drips) drawDrip(pctx, d, clock.t, H);
|
|
577
|
+
pctx.restore();
|
|
578
|
+
}
|
|
579
|
+
if (pattern && o.grain > 0) grainOnto({ ctx: pctx, canvas: paint, width: W, height: H, dpr, still: true }, pattern, o.grain * 0.8);
|
|
580
|
+
ctx.save();
|
|
581
|
+
if (o.glow > 0) {
|
|
582
|
+
ctx.shadowColor = o.color;
|
|
583
|
+
ctx.shadowBlur = 10 * o.glow * dpr;
|
|
584
|
+
}
|
|
585
|
+
ctx.drawImage(paint, 0, 0, W, H);
|
|
586
|
+
ctx.restore();
|
|
587
|
+
};
|
|
588
|
+
|
|
589
|
+
const { c, stop } = createCanvas2D(el, {
|
|
590
|
+
draw: (cv, now) => draw(cv, now),
|
|
591
|
+
resize: (cv) => {
|
|
592
|
+
prepare(cv);
|
|
593
|
+
draw(cv, performance.now());
|
|
594
|
+
},
|
|
595
|
+
});
|
|
596
|
+
if (!c) return stop;
|
|
597
|
+
let alive = true;
|
|
598
|
+
if (o.src)
|
|
599
|
+
loadImage(o.src)
|
|
600
|
+
.then((img) => {
|
|
601
|
+
if (!alive) return;
|
|
602
|
+
image = img;
|
|
603
|
+
prepare(c);
|
|
604
|
+
draw(c, performance.now());
|
|
605
|
+
})
|
|
606
|
+
.catch((error) => console.warn('[effects-astro]', error));
|
|
607
|
+
return () => {
|
|
608
|
+
alive = false;
|
|
609
|
+
stop();
|
|
610
|
+
};
|
|
611
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/** Round lights, or six-bladed ones. */
|
|
2
|
+
export type BokehShape = 'round' | 'blades';
|
|
3
|
+
|
|
4
|
+
export interface PhotoBokehConfig {
|
|
5
|
+
/** The photo (same origin, or served for CORS). */
|
|
6
|
+
src: string;
|
|
7
|
+
/** The widest blur, a share of the height. Default 0.03. */
|
|
8
|
+
blur?: number;
|
|
9
|
+
/** How fast the focus racks, cycles a second. Default 0.06. */
|
|
10
|
+
rate?: number;
|
|
11
|
+
/** How much the depth guess leans on the picture (its brightness and colour) against its height in the frame, 0–1. Default 0.55. */
|
|
12
|
+
near?: number;
|
|
13
|
+
/** Out-of-focus lights drifting through it (soft filled glows), up to 12; 0 for none. Default 6. */
|
|
14
|
+
discs?: number;
|
|
15
|
+
/** Their radius, a share of the height. Default 0.11. */
|
|
16
|
+
size?: number;
|
|
17
|
+
/** How bright they are. Default 0.35. */
|
|
18
|
+
glow?: number;
|
|
19
|
+
/** Round lights, or six-bladed ones. Default 'round'. */
|
|
20
|
+
shape?: BokehShape;
|
|
21
|
+
/** How fast they rise, heights a second. Default 0.02. */
|
|
22
|
+
drift?: number;
|
|
23
|
+
/** Darkening toward the edges; 0 for none. Default 0.35. */
|
|
24
|
+
vignette?: number;
|
|
25
|
+
/** Fine grain over all of it; 0 for none. Default 0.04. */
|
|
26
|
+
grain?: number;
|
|
27
|
+
/** Where the focus and the lights start, so two cards differ. Default 0. */
|
|
28
|
+
seed?: number;
|
|
29
|
+
/** Plays every clock faster or slower; 0 for one still frame. Default 1. */
|
|
30
|
+
speed?: number;
|
|
31
|
+
/** An area dimmed a little for copy: centre (0–1 from the top left), radius (share of the height), depth (0–1). */
|
|
32
|
+
quiet?: { x?: number; y?: number; radius?: number; depth?: number } | null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** The settings' defaults, shared by the shader and the JS twin. */
|
|
36
|
+
export declare const BOKEH_DEFAULTS: Required<Omit<PhotoBokehConfig, 'src' | 'speed' | 'quiet'>>;
|
|
37
|
+
|
|
38
|
+
/** The most lights the shader draws. */
|
|
39
|
+
export declare const MAX_DISCS: number;
|
|
40
|
+
|
|
41
|
+
/** Where the plane of focus is at a time: a depth from 0 (far) to 1 (near). */
|
|
42
|
+
export declare function focusAt(config: Partial<PhotoBokehConfig>, t: number): number;
|
|
43
|
+
|
|
44
|
+
/** How soft a point at a depth is at a time: the blur's radius, a share of the height. */
|
|
45
|
+
export declare function blurAt(config: Partial<PhotoBokehConfig>, depth: number, t: number): number;
|
|
46
|
+
|
|
47
|
+
/** Where an out-of-focus light is at a time: its centre (0–1 of the frame) and radius (heights). */
|
|
48
|
+
export declare function discAt(config: Partial<PhotoBokehConfig>, i: number, t: number): { x: number; y: number; r: number };
|
|
49
|
+
|
|
50
|
+
/** The photo's brightest, most coloured pixels (from RGBA 0–255), lightened, for the lights' tints. */
|
|
51
|
+
export declare function highlightTints(rgba: ArrayLike<number>): [number, number, number][];
|
|
52
|
+
|
|
53
|
+
/** A CSS `background` that stands in before the shader starts and without WebGL: the photo, covering. */
|
|
54
|
+
export declare function photoBokehStill(config: Pick<PhotoBokehConfig, 'src'>): string;
|
|
55
|
+
|
|
56
|
+
/** Starts the rack focus on an element (its first canvas). Returns a function that stops it. */
|
|
57
|
+
export declare function photoBokeh(el: HTMLElement, config: PhotoBokehConfig): () => void;
|