@idle-screens/savers-classic 0.1.0 → 2.0.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.
@@ -0,0 +1,3232 @@
1
+ // src/warp.ts
2
+ var warpManifest = {
3
+ id: "warp",
4
+ label: "Warp",
5
+ passthrough: false,
6
+ minBackend: "canvas2d",
7
+ costTier: "low",
8
+ motionIntensity: "energetic",
9
+ reducedMotionFallback: "static",
10
+ a11y: { flashSafe: true },
11
+ workerReady: true
12
+ };
13
+ var STAR_COUNT = 520;
14
+ var SPEED = 0.012;
15
+ var WarpInstance = class {
16
+ ctxSaver;
17
+ canvas;
18
+ ctx;
19
+ w = 0;
20
+ h = 0;
21
+ stars = [];
22
+ frameId = null;
23
+ paused = false;
24
+ constructor(ctx) {
25
+ this.ctxSaver = ctx;
26
+ let canvas;
27
+ if (ctx.surface) {
28
+ canvas = ctx.surface;
29
+ } else {
30
+ const el = document.createElement("canvas");
31
+ el.style.cssText = "display:block;width:100%;height:100%";
32
+ el.setAttribute("aria-hidden", "true");
33
+ ctx.host.appendChild(el);
34
+ canvas = el;
35
+ }
36
+ this.canvas = canvas;
37
+ const c2d = canvas.getContext("2d", { alpha: false });
38
+ if (!c2d) throw new Error("warp: no 2d context");
39
+ this.ctx = c2d;
40
+ this.w = ctx.width;
41
+ this.h = ctx.height;
42
+ this.sizeCanvas();
43
+ this.buildStars();
44
+ this.paused = ctx.reducedMotion;
45
+ if (this.paused) this.renderStill();
46
+ else this.start();
47
+ }
48
+ sizeCanvas() {
49
+ const dpr = Math.min(this.ctxSaver.dpr, 2);
50
+ this.canvas.width = Math.max(1, Math.round(this.w * dpr));
51
+ this.canvas.height = Math.max(1, Math.round(this.h * dpr));
52
+ this.ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
53
+ }
54
+ buildStars() {
55
+ const rng = this.ctxSaver.rng;
56
+ this.stars = new Array(STAR_COUNT);
57
+ for (let i = 0; i < STAR_COUNT; i++) {
58
+ this.stars[i] = this.spawn(rng.range(0.01, 1));
59
+ }
60
+ }
61
+ spawn(z) {
62
+ const rng = this.ctxSaver.rng;
63
+ return {
64
+ x: rng.range(-1, 1),
65
+ y: rng.range(-1, 1),
66
+ z,
67
+ pz: z
68
+ };
69
+ }
70
+ start() {
71
+ if (this.frameId !== null || typeof requestAnimationFrame === "undefined") return;
72
+ this.loop();
73
+ }
74
+ stop() {
75
+ if (this.frameId !== null) {
76
+ cancelAnimationFrame(this.frameId);
77
+ this.frameId = null;
78
+ }
79
+ }
80
+ loop() {
81
+ this.frameId = requestAnimationFrame(() => this.loop());
82
+ this.step();
83
+ this.render();
84
+ }
85
+ step() {
86
+ for (const s of this.stars) {
87
+ s.pz = s.z;
88
+ s.z -= SPEED;
89
+ if (s.z <= 0.01) {
90
+ const respawned = this.spawn(1);
91
+ s.x = respawned.x;
92
+ s.y = respawned.y;
93
+ s.z = 1;
94
+ s.pz = 1;
95
+ }
96
+ }
97
+ }
98
+ render() {
99
+ const ctx = this.ctx;
100
+ const w = this.w;
101
+ const h = this.h;
102
+ const cx = w / 2;
103
+ const cy = h / 2;
104
+ const focal = Math.min(w, h) * 0.9;
105
+ ctx.fillStyle = "rgba(17,17,17,0.45)";
106
+ ctx.fillRect(0, 0, w, h);
107
+ ctx.lineCap = "round";
108
+ for (const s of this.stars) {
109
+ const sx = cx + s.x / s.z * focal;
110
+ const sy = cy + s.y / s.z * focal;
111
+ const px = cx + s.x / s.pz * focal;
112
+ const py = cy + s.y / s.pz * focal;
113
+ if (sx < -50 || sx > w + 50 || sy < -50 || sy > h + 50) continue;
114
+ const depth = 1 - s.z;
115
+ const alpha = Math.min(1, 0.15 + depth * 1.1);
116
+ const size = Math.max(0.4, depth * 2.6);
117
+ ctx.strokeStyle = `rgba(220,235,255,${alpha.toFixed(3)})`;
118
+ ctx.lineWidth = size;
119
+ ctx.beginPath();
120
+ ctx.moveTo(px, py);
121
+ ctx.lineTo(sx, sy);
122
+ ctx.stroke();
123
+ }
124
+ }
125
+ renderStill() {
126
+ const ctx = this.ctx;
127
+ ctx.fillStyle = "#111";
128
+ ctx.fillRect(0, 0, this.w, this.h);
129
+ const cx = this.w / 2;
130
+ const cy = this.h / 2;
131
+ const focal = Math.min(this.w, this.h) * 0.9;
132
+ for (const s of this.stars) {
133
+ const sx = cx + s.x / s.z * focal;
134
+ const sy = cy + s.y / s.z * focal;
135
+ if (sx < 0 || sx > this.w || sy < 0 || sy > this.h) continue;
136
+ const depth = 1 - s.z;
137
+ const alpha = Math.min(1, 0.15 + depth * 1.1);
138
+ const size = Math.max(0.4, depth * 2.6);
139
+ ctx.fillStyle = `rgba(220,235,255,${alpha.toFixed(3)})`;
140
+ ctx.beginPath();
141
+ ctx.arc(sx, sy, size, 0, Math.PI * 2);
142
+ ctx.fill();
143
+ }
144
+ }
145
+ setPaused(paused) {
146
+ this.paused = paused;
147
+ if (paused) {
148
+ this.stop();
149
+ this.renderStill();
150
+ } else {
151
+ this.start();
152
+ }
153
+ }
154
+ resize(width, height, dpr) {
155
+ this.w = width;
156
+ this.h = height;
157
+ if (dpr !== void 0) this.ctxSaver.dpr = dpr;
158
+ this.sizeCanvas();
159
+ if (this.paused) this.renderStill();
160
+ }
161
+ dispose() {
162
+ this.stop();
163
+ if (typeof HTMLCanvasElement !== "undefined" && this.canvas instanceof HTMLCanvasElement) this.canvas.remove();
164
+ }
165
+ };
166
+ var warp = {
167
+ manifest: warpManifest,
168
+ mount: (ctx) => new WarpInstance(ctx)
169
+ };
170
+
171
+ // src/hard-rain.ts
172
+ var hardRainManifest = {
173
+ id: "hard-rain",
174
+ label: "Hard Rain",
175
+ passthrough: false,
176
+ minBackend: "canvas2d",
177
+ costTier: "low",
178
+ motionIntensity: "moderate",
179
+ reducedMotionFallback: "static",
180
+ a11y: { flashSafe: true, notes: "Slow-expanding coloured rings on black; no flashing." },
181
+ workerReady: true
182
+ };
183
+ var CYCLE = 4500;
184
+ var POSITIONS = {
185
+ "p0-1": [0.05, -0.02],
186
+ "p0-2": [0.25, -0.17],
187
+ "p0-3": [0.45, 0.04],
188
+ "p0-4": [0.65, -0.04],
189
+ "p0-5": [0.85, 0.01],
190
+ "p1-1": [0.05, 0.18],
191
+ "p1-2": [0.25, 0.07],
192
+ "p1-3": [0.45, 0.24],
193
+ "p1-4": [0.65, 0.16],
194
+ "p1-5": [0.85, 0.21],
195
+ "p2-1": [0.15, 0.38],
196
+ "p2-2": [0.35, 0.27],
197
+ "p2-3": [0.55, 0.44],
198
+ "p2-4": [0.75, 0.36],
199
+ "p2-5": [0.95, 0.41],
200
+ "p3-1": [0.05, 0.58],
201
+ "p3-2": [0.25, 0.47],
202
+ "p3-3": [0.45, 0.64],
203
+ "p3-4": [0.65, 0.56],
204
+ "p3-5": [0.85, 0.61],
205
+ "p4-1": [0.15, 0.78],
206
+ "p4-2": [0.35, 0.67],
207
+ "p4-3": [0.55, 0.84],
208
+ "p4-4": [0.75, 0.76],
209
+ "p4-5": [0.95, 0.81],
210
+ "p5-1": [0.15, 0.98],
211
+ "p5-2": [0.35, 0.87],
212
+ "p5-3": [0.55, 1.04],
213
+ "p5-4": [0.75, 0.96],
214
+ "p5-5": [0.95, 1.01]
215
+ };
216
+ var SIZES = { s140: 70, s200: 100, s250: 125, s300: 150, s350: 175 };
217
+ var COLOURS = {
218
+ dkblue: "#00006e",
219
+ lime: "#c8d354",
220
+ ltgray: "#c2c2c2",
221
+ red: "#861f23",
222
+ ltblue: "#45a0cc",
223
+ pink: "#9a3368",
224
+ yellow: "#efda1d",
225
+ green: "#397132"
226
+ };
227
+ function timingPhase(t) {
228
+ const map = {
229
+ t1: -4.35,
230
+ t2: -4.2,
231
+ t3: -4.05,
232
+ t4: -3.9,
233
+ t5: -3.75,
234
+ t6: -3.6,
235
+ t7: -3.45,
236
+ t8: -3.3,
237
+ t9: -3.15,
238
+ t10: -3,
239
+ t11: -2.85,
240
+ t12: -2.7,
241
+ t13: -2.55,
242
+ t14: -2.4,
243
+ t15: -2.25,
244
+ t16: -2.1,
245
+ t17: -1.95,
246
+ t18: -1.8,
247
+ t19: -1.65,
248
+ t20: -1.5,
249
+ t21: -1.35,
250
+ t22: -1.2,
251
+ t23: -1.05,
252
+ t24: -0.9,
253
+ t25: -0.75,
254
+ t26: -0.6,
255
+ t27: -0.45,
256
+ t28: 0.3,
257
+ t29: -0.15,
258
+ t30: 0
259
+ };
260
+ return (map[t] ?? 0) * 1e3;
261
+ }
262
+ var DROP_SPECS = [
263
+ "t7 p0-1 s200 dkblue",
264
+ "t23 p0-2 s350 lime",
265
+ "t17 p0-3 s140 ltgray",
266
+ "t11 p0-4 s300 red",
267
+ "t14 p0-5 s250 ltblue",
268
+ "t29 p1-1 s350 pink",
269
+ "t18 p1-2 s300 yellow",
270
+ "t30 p1-3 s140 green",
271
+ "t12 p1-4 s200 ltgray",
272
+ "t27 p1-5 s250 dkblue",
273
+ "t10 p2-1 s140 pink",
274
+ "t4 p2-2 s200 red",
275
+ "t8 p2-3 s300 ltblue",
276
+ "t22 p2-4 s350 yellow",
277
+ "t1 p2-5 s250 lime",
278
+ "t3 p3-1 s300 green",
279
+ "t6 p3-2 s250 dkblue",
280
+ "t15 p3-3 s350 ltgray",
281
+ "t24 p3-4 s140 pink",
282
+ "t21 p3-5 s200 ltblue",
283
+ "t13 p4-1 s250 yellow",
284
+ "t28 p4-2 s140 red",
285
+ "t2 p4-3 s200 ltgray",
286
+ "t19 p4-4 s300 green",
287
+ "t25 p4-5 s350 lime",
288
+ "t5 p5-1 s350 dkblue",
289
+ "t26 p5-2 s300 pink",
290
+ "t20 p5-3 s140 ltblue",
291
+ "t16 p5-4 s250 yellow",
292
+ "t9 p5-5 s200 ltgray",
293
+ "t22 p0-1 s200 dkblue",
294
+ "t8 p0-2 s350 lime",
295
+ "t2 p0-3 s140 ltgray",
296
+ "t26 p0-4 s300 red",
297
+ "t29 p0-5 s250 ltblue",
298
+ "t14 p1-1 s350 pink",
299
+ "t3 p1-2 s300 yellow",
300
+ "t15 p1-3 s140 green",
301
+ "t27 p1-4 s200 ltgray",
302
+ "t12 p1-5 s250 dkblue",
303
+ "t25 p2-1 s140 pink",
304
+ "t19 p2-2 s200 red",
305
+ "t23 p2-3 s300 ltblue",
306
+ "t7 p2-4 s350 yellow",
307
+ "t16 p2-5 s250 lime",
308
+ "t18 p3-1 s300 green",
309
+ "t21 p3-2 s250 dkblue",
310
+ "t30 p3-3 s350 ltgray",
311
+ "t9 p3-4 s140 pink",
312
+ "t6 p3-5 s200 ltblue",
313
+ "t28 p4-1 s250 yellow",
314
+ "t13 p4-2 s140 red",
315
+ "t17 p4-3 s200 ltgray",
316
+ "t4 p4-4 s300 green",
317
+ "t10 p4-5 s350 lime",
318
+ "t20 p5-1 s350 dkblue",
319
+ "t11 p5-2 s300 pink",
320
+ "t5 p5-3 s140 ltblue",
321
+ "t1 p5-4 s250 yellow",
322
+ "t24 p5-5 s200 ltgray"
323
+ ];
324
+ function hexToRgb(hex) {
325
+ const n = parseInt(hex.slice(1), 16);
326
+ return `${n >> 16 & 255},${n >> 8 & 255},${n & 255}`;
327
+ }
328
+ var HardRainInstance = class {
329
+ ctx;
330
+ canvas;
331
+ c2d;
332
+ w = 0;
333
+ h = 0;
334
+ scale = 1;
335
+ drops = [];
336
+ frameId = null;
337
+ paused = false;
338
+ last = 0;
339
+ clock = 0;
340
+ constructor(ctx) {
341
+ this.ctx = ctx;
342
+ let canvas;
343
+ if (ctx.surface) {
344
+ canvas = ctx.surface;
345
+ } else {
346
+ const el = document.createElement("canvas");
347
+ el.style.cssText = "display:block;width:100%;height:100%";
348
+ ctx.host.appendChild(el);
349
+ canvas = el;
350
+ }
351
+ this.canvas = canvas;
352
+ const c2d = canvas.getContext("2d", { alpha: false });
353
+ if (!c2d) throw new Error("hard-rain: no 2d context");
354
+ this.c2d = c2d;
355
+ this.w = ctx.width;
356
+ this.h = ctx.height;
357
+ this.buildDrops();
358
+ this.sizeCanvas();
359
+ this.setPaused(ctx.reducedMotion);
360
+ if (this.paused) this.renderStill();
361
+ }
362
+ buildDrops() {
363
+ this.drops = DROP_SPECS.map((spec) => {
364
+ const parts = spec.split(" ");
365
+ const [tCls, pCls, sCls, cCls] = parts;
366
+ const pos = POSITIONS[pCls] ?? [0.5, 0.5];
367
+ return {
368
+ fx: pos[0],
369
+ fy: pos[1],
370
+ maxR: SIZES[sCls] ?? 100,
371
+ rgb: hexToRgb(COLOURS[cCls] ?? "#c2c2c2"),
372
+ phase: timingPhase(tCls)
373
+ };
374
+ });
375
+ }
376
+ sizeCanvas() {
377
+ const dpr = Math.min(this.ctx.dpr, 2);
378
+ this.canvas.width = Math.max(1, Math.round(this.w * dpr));
379
+ this.canvas.height = Math.max(1, Math.round(this.h * dpr));
380
+ this.c2d.setTransform(dpr, 0, 0, dpr, 0, 0);
381
+ this.scale = Math.min(this.w, this.h * 1.4) / 900;
382
+ }
383
+ // ---- loop ----
384
+ start() {
385
+ if (this.frameId !== null || typeof requestAnimationFrame === "undefined") return;
386
+ this.last = 0;
387
+ this.frameId = requestAnimationFrame((now) => this.loop(now));
388
+ }
389
+ stop() {
390
+ if (this.frameId !== null) {
391
+ cancelAnimationFrame(this.frameId);
392
+ this.frameId = null;
393
+ }
394
+ }
395
+ loop(now) {
396
+ this.frameId = requestAnimationFrame((n) => this.loop(n));
397
+ if (this.last === 0) this.last = now;
398
+ const dt = now - this.last;
399
+ this.last = now;
400
+ this.clock += Math.min(50, dt);
401
+ this.render();
402
+ }
403
+ render() {
404
+ const ctx = this.c2d;
405
+ ctx.fillStyle = "#000";
406
+ ctx.fillRect(0, 0, this.w, this.h);
407
+ for (const d of this.drops) {
408
+ let k = (this.clock - d.phase) % CYCLE / CYCLE;
409
+ if (k < 0) k += 1;
410
+ if (k >= 0.89) continue;
411
+ const g = k / 0.89;
412
+ const r = g * d.maxR * this.scale;
413
+ if (r < 0.5) continue;
414
+ const alpha = Math.min(1, g * 4) * (1 - g * 0.55);
415
+ ctx.strokeStyle = `rgba(${d.rgb},${alpha.toFixed(3)})`;
416
+ ctx.lineWidth = 2;
417
+ ctx.beginPath();
418
+ ctx.arc(d.fx * this.w, d.fy * this.h, r, 0, Math.PI * 2);
419
+ ctx.stroke();
420
+ }
421
+ }
422
+ renderStill() {
423
+ this.clock = CYCLE * 0.4;
424
+ this.render();
425
+ }
426
+ previewAt(t) {
427
+ this.clock = t;
428
+ this.render();
429
+ }
430
+ // ---- SaverInstance ----
431
+ setPaused(paused) {
432
+ this.paused = paused;
433
+ if (paused) {
434
+ this.stop();
435
+ this.renderStill();
436
+ } else {
437
+ this.start();
438
+ }
439
+ }
440
+ resize(width, height, dpr) {
441
+ this.w = width;
442
+ this.h = height;
443
+ if (dpr !== void 0) this.ctx.dpr = dpr;
444
+ this.sizeCanvas();
445
+ if (this.paused) this.renderStill();
446
+ }
447
+ dispose() {
448
+ this.stop();
449
+ if (typeof HTMLCanvasElement !== "undefined" && this.canvas instanceof HTMLCanvasElement) this.canvas.remove();
450
+ }
451
+ };
452
+ var hardRain = {
453
+ manifest: hardRainManifest,
454
+ mount: (ctx) => new HardRainInstance(ctx)
455
+ };
456
+
457
+ // src/rainstorm.ts
458
+ var rainstormManifest = {
459
+ id: "rainstorm",
460
+ label: "Rainstorm",
461
+ passthrough: false,
462
+ minBackend: "canvas2d",
463
+ costTier: "low",
464
+ motionIntensity: "moderate",
465
+ reducedMotionFallback: "static",
466
+ a11y: { flashSafe: true, notes: "Rain streaks over a dark sky; the lightning is a brief, gentle brightening (no rapid strobe)." },
467
+ workerReady: true
468
+ };
469
+ var FLASH_PERIOD = 8e3;
470
+ var RainstormInstance = class {
471
+ ctx;
472
+ canvas;
473
+ c2d;
474
+ w = 0;
475
+ h = 0;
476
+ layers = [];
477
+ frameId = null;
478
+ paused = false;
479
+ last = 0;
480
+ clock = 0;
481
+ constructor(ctx) {
482
+ this.ctx = ctx;
483
+ let canvas;
484
+ if (ctx.surface) {
485
+ canvas = ctx.surface;
486
+ } else {
487
+ const el = document.createElement("canvas");
488
+ el.style.cssText = "display:block;width:100%;height:100%";
489
+ ctx.host.appendChild(el);
490
+ canvas = el;
491
+ }
492
+ this.canvas = canvas;
493
+ const c2d = canvas.getContext("2d", { alpha: false });
494
+ if (!c2d) throw new Error("rainstorm: no 2d context");
495
+ this.c2d = c2d;
496
+ this.w = ctx.width;
497
+ this.h = ctx.height;
498
+ this.sizeCanvas();
499
+ this.buildLayers();
500
+ this.setPaused(ctx.reducedMotion);
501
+ if (this.paused) this.renderStill();
502
+ }
503
+ sizeCanvas() {
504
+ const dpr = Math.min(this.ctx.dpr, 2);
505
+ this.canvas.width = Math.max(1, Math.round(this.w * dpr));
506
+ this.canvas.height = Math.max(1, Math.round(this.h * dpr));
507
+ this.c2d.setTransform(dpr, 0, 0, dpr, 0, 0);
508
+ }
509
+ /** Three depth layers: near (fast, long, bright), mid, distant (slow, faint). */
510
+ buildLayers() {
511
+ const rng = this.ctx.rng;
512
+ const area = this.w * this.h / (1280 * 800);
513
+ const specs = [
514
+ { count: 220, minLen: 26, maxLen: 46, minSpd: 900, maxSpd: 1150, width: 1.6, alpha: 0.5, slant: 0.19 },
515
+ { count: 160, minLen: 16, maxLen: 30, minSpd: 520, maxSpd: 700, width: 1.2, alpha: 0.34, slant: 0.19 },
516
+ { count: 120, minLen: 10, maxLen: 20, minSpd: 300, maxSpd: 420, width: 0.9, alpha: 0.22, slant: 0.19 }
517
+ ];
518
+ this.layers = specs.map((s) => {
519
+ const n = Math.max(8, Math.round(s.count * area));
520
+ const drops = new Array(n);
521
+ for (let i = 0; i < n; i++) {
522
+ drops[i] = {
523
+ x: rng.range(-40, this.w + 40),
524
+ y: rng.range(-this.h, this.h),
525
+ len: rng.range(s.minLen, s.maxLen),
526
+ speed: rng.range(s.minSpd, s.maxSpd),
527
+ width: s.width,
528
+ alpha: rng.range(s.alpha * 0.7, s.alpha)
529
+ };
530
+ }
531
+ return { drops, slant: s.slant };
532
+ });
533
+ }
534
+ // ---- lightning ----
535
+ /** 0..1 flash intensity given the wall clock (ms). A short white pulse near the
536
+ * top of each period, echoing the CSS `flash` keyframe (98% black → 99% white). */
537
+ flashLevel() {
538
+ const p = (this.clock % FLASH_PERIOD + FLASH_PERIOD) % FLASH_PERIOD;
539
+ const start = FLASH_PERIOD - 140;
540
+ if (p < start) return 0;
541
+ const k = (p - start) / 140;
542
+ return k < 0.4 ? k / 0.4 : Math.max(0, 1 - (k - 0.4) / 0.6);
543
+ }
544
+ // ---- loop ----
545
+ start() {
546
+ if (this.frameId !== null || typeof requestAnimationFrame === "undefined") return;
547
+ this.last = 0;
548
+ this.frameId = requestAnimationFrame((now) => this.loop(now));
549
+ }
550
+ stop() {
551
+ if (this.frameId !== null) {
552
+ cancelAnimationFrame(this.frameId);
553
+ this.frameId = null;
554
+ }
555
+ }
556
+ loop(now) {
557
+ this.frameId = requestAnimationFrame((n) => this.loop(n));
558
+ if (this.last === 0) this.last = now;
559
+ const dt = Math.min(0.05, (now - this.last) / 1e3);
560
+ this.last = now;
561
+ this.clock += dt * 1e3;
562
+ this.update(dt);
563
+ this.render();
564
+ }
565
+ update(dt) {
566
+ for (const layer of this.layers) {
567
+ for (const d of layer.drops) {
568
+ d.y += d.speed * dt;
569
+ d.x += d.speed * layer.slant * dt;
570
+ if (d.y - d.len > this.h) {
571
+ d.y = -d.len - this.ctx.rng.range(0, this.h * 0.4);
572
+ d.x = this.ctx.rng.range(-40, this.w + 40);
573
+ }
574
+ if (d.x - d.len > this.w) d.x = -40;
575
+ }
576
+ }
577
+ }
578
+ render() {
579
+ const ctx = this.c2d;
580
+ const flash = this.flashLevel();
581
+ const base = 8 + flash * 235;
582
+ ctx.fillStyle = `rgb(${base | 0},${base + 2 | 0},${base + 8 | 0})`;
583
+ ctx.fillRect(0, 0, this.w, this.h);
584
+ ctx.lineCap = "round";
585
+ for (const layer of this.layers) {
586
+ const dx = -layer.slant;
587
+ for (const d of layer.drops) {
588
+ ctx.strokeStyle = `rgba(200,215,235,${d.alpha.toFixed(3)})`;
589
+ ctx.lineWidth = d.width;
590
+ ctx.beginPath();
591
+ ctx.moveTo(d.x, d.y);
592
+ ctx.lineTo(d.x + dx * d.len, d.y - d.len);
593
+ ctx.stroke();
594
+ }
595
+ }
596
+ }
597
+ renderStill() {
598
+ this.clock = 0;
599
+ this.render();
600
+ }
601
+ previewAt(t) {
602
+ this.clock = t;
603
+ this.render();
604
+ }
605
+ // ---- SaverInstance ----
606
+ setPaused(paused) {
607
+ this.paused = paused;
608
+ if (paused) {
609
+ this.stop();
610
+ this.renderStill();
611
+ } else {
612
+ this.start();
613
+ }
614
+ }
615
+ resize(width, height, dpr) {
616
+ this.w = width;
617
+ this.h = height;
618
+ if (dpr !== void 0) this.ctx.dpr = dpr;
619
+ this.sizeCanvas();
620
+ this.buildLayers();
621
+ if (this.paused) this.renderStill();
622
+ }
623
+ dispose() {
624
+ this.stop();
625
+ if (typeof HTMLCanvasElement !== "undefined" && this.canvas instanceof HTMLCanvasElement) this.canvas.remove();
626
+ }
627
+ };
628
+ var rainstorm = {
629
+ manifest: rainstormManifest,
630
+ mount: (ctx) => new RainstormInstance(ctx)
631
+ };
632
+
633
+ // src/globe.ts
634
+ var globeManifest = {
635
+ id: "globe",
636
+ label: "Globe",
637
+ passthrough: false,
638
+ minBackend: "canvas2d",
639
+ costTier: "low",
640
+ motionIntensity: "calm",
641
+ reducedMotionFallback: "static",
642
+ a11y: { flashSafe: true, notes: "A slowly spinning dotted globe drifting on black; no flashing." },
643
+ workerReady: true
644
+ };
645
+ var RADIUS = 120;
646
+ var SPIN = 0.9;
647
+ var LAT_LINES = 11;
648
+ var LON_LINES = 21;
649
+ var GlobeInstance = class {
650
+ ctx;
651
+ canvas;
652
+ c2d;
653
+ w = 0;
654
+ h = 0;
655
+ // globe centre + velocity (DVD bounce)
656
+ cx = 0;
657
+ cy = 0;
658
+ vx = 0;
659
+ vy = 0;
660
+ spinPhase = 0;
661
+ // precomputed unit-sphere lattice (spun about Y at render time)
662
+ lattice = [];
663
+ frameId = null;
664
+ paused = false;
665
+ last = 0;
666
+ constructor(ctx) {
667
+ this.ctx = ctx;
668
+ let canvas;
669
+ if (ctx.surface) {
670
+ canvas = ctx.surface;
671
+ } else {
672
+ const el = document.createElement("canvas");
673
+ el.style.cssText = "display:block;width:100%;height:100%";
674
+ ctx.host.appendChild(el);
675
+ canvas = el;
676
+ }
677
+ this.canvas = canvas;
678
+ const c2d = canvas.getContext("2d", { alpha: false });
679
+ if (!c2d) throw new Error("globe: no 2d context");
680
+ this.c2d = c2d;
681
+ this.buildLattice();
682
+ this.w = ctx.width;
683
+ this.h = ctx.height;
684
+ this.sizeCanvas();
685
+ const rng = ctx.rng;
686
+ this.cx = rng.range(RADIUS, Math.max(RADIUS + 1, this.w - RADIUS));
687
+ this.cy = rng.range(RADIUS, Math.max(RADIUS + 1, this.h - RADIUS));
688
+ const dir = rng.range(0, Math.PI * 2);
689
+ const speed = 90;
690
+ this.vx = Math.cos(dir) * speed;
691
+ this.vy = Math.sin(dir) * speed;
692
+ this.setPaused(ctx.reducedMotion);
693
+ if (this.paused) this.renderStill();
694
+ }
695
+ /** Dotted lat/long lattice on the unit sphere. */
696
+ buildLattice() {
697
+ for (let i = 1; i < LAT_LINES; i++) {
698
+ const lat = -Math.PI / 2 + Math.PI * i / LAT_LINES;
699
+ const ringR = Math.cos(lat);
700
+ const y = Math.sin(lat);
701
+ const dots = Math.max(6, Math.round(LON_LINES * ringR + 4));
702
+ for (let j = 0; j < dots; j++) {
703
+ const lon = Math.PI * 2 * j / dots;
704
+ this.lattice.push({ x: ringR * Math.cos(lon), y, z: ringR * Math.sin(lon) });
705
+ }
706
+ }
707
+ for (let m = 0; m < LON_LINES; m++) {
708
+ const lon = Math.PI * 2 * m / LON_LINES;
709
+ for (let s = 0; s <= 24; s++) {
710
+ const lat = -Math.PI / 2 + Math.PI * s / 24;
711
+ const ringR = Math.cos(lat);
712
+ this.lattice.push({ x: ringR * Math.cos(lon), y: Math.sin(lat), z: ringR * Math.sin(lon) });
713
+ }
714
+ }
715
+ }
716
+ sizeCanvas() {
717
+ const dpr = Math.min(this.ctx.dpr, 2);
718
+ this.canvas.width = Math.max(1, Math.round(this.w * dpr));
719
+ this.canvas.height = Math.max(1, Math.round(this.h * dpr));
720
+ this.c2d.setTransform(dpr, 0, 0, dpr, 0, 0);
721
+ }
722
+ // ---- loop ----
723
+ start() {
724
+ if (this.frameId !== null || typeof requestAnimationFrame === "undefined") return;
725
+ this.last = 0;
726
+ this.frameId = requestAnimationFrame((now) => this.loop(now));
727
+ }
728
+ stop() {
729
+ if (this.frameId !== null) {
730
+ cancelAnimationFrame(this.frameId);
731
+ this.frameId = null;
732
+ }
733
+ }
734
+ loop(now) {
735
+ this.frameId = requestAnimationFrame((n) => this.loop(n));
736
+ if (this.last === 0) this.last = now;
737
+ const dt = Math.min(0.05, (now - this.last) / 1e3);
738
+ this.last = now;
739
+ this.update(dt);
740
+ this.render();
741
+ }
742
+ update(dt) {
743
+ this.spinPhase += SPIN * dt;
744
+ this.cx += this.vx * dt;
745
+ this.cy += this.vy * dt;
746
+ const minX = RADIUS;
747
+ const maxX = this.w - RADIUS;
748
+ const minY = RADIUS;
749
+ const maxY = this.h - RADIUS;
750
+ if (this.cx < minX) {
751
+ this.cx = minX;
752
+ this.vx = Math.abs(this.vx);
753
+ } else if (this.cx > maxX) {
754
+ this.cx = maxX;
755
+ this.vx = -Math.abs(this.vx);
756
+ }
757
+ if (this.cy < minY) {
758
+ this.cy = minY;
759
+ this.vy = Math.abs(this.vy);
760
+ } else if (this.cy > maxY) {
761
+ this.cy = maxY;
762
+ this.vy = -Math.abs(this.vy);
763
+ }
764
+ }
765
+ render() {
766
+ const ctx = this.c2d;
767
+ ctx.fillStyle = "#000";
768
+ ctx.fillRect(0, 0, this.w, this.h);
769
+ const body = ctx.createRadialGradient(
770
+ this.cx - RADIUS * 0.35,
771
+ this.cy - RADIUS * 0.35,
772
+ RADIUS * 0.1,
773
+ this.cx,
774
+ this.cy,
775
+ RADIUS
776
+ );
777
+ body.addColorStop(0, "rgba(30,60,90,0.55)");
778
+ body.addColorStop(1, "rgba(6,14,26,0.15)");
779
+ ctx.fillStyle = body;
780
+ ctx.beginPath();
781
+ ctx.arc(this.cx, this.cy, RADIUS, 0, Math.PI * 2);
782
+ ctx.fill();
783
+ const cos = Math.cos(this.spinPhase);
784
+ const sin = Math.sin(this.spinPhase);
785
+ for (const p of this.lattice) {
786
+ const rx = p.x * cos + p.z * sin;
787
+ const rz = -p.x * sin + p.z * cos;
788
+ const x = this.cx + rx * RADIUS;
789
+ const y = this.cy + p.y * RADIUS;
790
+ const depth = (rz + 1) / 2;
791
+ const alpha = 0.12 + depth * 0.7;
792
+ const size = 0.7 + depth * 1.2;
793
+ ctx.fillStyle = `rgba(120,200,255,${alpha.toFixed(3)})`;
794
+ ctx.beginPath();
795
+ ctx.arc(x, y, size, 0, Math.PI * 2);
796
+ ctx.fill();
797
+ }
798
+ ctx.strokeStyle = "rgba(120,200,255,0.35)";
799
+ ctx.lineWidth = 1;
800
+ ctx.beginPath();
801
+ ctx.arc(this.cx, this.cy, RADIUS, 0, Math.PI * 2);
802
+ ctx.stroke();
803
+ }
804
+ renderStill() {
805
+ this.render();
806
+ }
807
+ // ---- SaverInstance ----
808
+ setPaused(paused) {
809
+ this.paused = paused;
810
+ if (paused) {
811
+ this.stop();
812
+ this.renderStill();
813
+ } else {
814
+ this.start();
815
+ }
816
+ }
817
+ resize(width, height, dpr) {
818
+ this.w = width;
819
+ this.h = height;
820
+ if (dpr !== void 0) this.ctx.dpr = dpr;
821
+ this.sizeCanvas();
822
+ this.cx = Math.min(Math.max(this.cx, RADIUS), Math.max(RADIUS, this.w - RADIUS));
823
+ this.cy = Math.min(Math.max(this.cy, RADIUS), Math.max(RADIUS, this.h - RADIUS));
824
+ if (this.paused) this.renderStill();
825
+ }
826
+ dispose() {
827
+ this.stop();
828
+ if (typeof HTMLCanvasElement !== "undefined" && this.canvas instanceof HTMLCanvasElement) this.canvas.remove();
829
+ }
830
+ };
831
+ var globe = {
832
+ manifest: globeManifest,
833
+ mount: (ctx) => new GlobeInstance(ctx)
834
+ };
835
+
836
+ // src/spotlight.ts
837
+ var spotlightManifest = {
838
+ id: "spotlight",
839
+ label: "Spotlight",
840
+ passthrough: true,
841
+ minBackend: "canvas2d",
842
+ costTier: "low",
843
+ motionIntensity: "calm",
844
+ reducedMotionFallback: "static",
845
+ a11y: { flashSafe: true, notes: "A soft light circle drifting over the live page; no flashing." },
846
+ workerReady: true
847
+ };
848
+ var HOLE_RADIUS = 220;
849
+ var SPEED_X = 5e3;
850
+ var SPEED_Y = 5600;
851
+ var SpotlightInstance = class {
852
+ ctx;
853
+ canvas;
854
+ c2d;
855
+ w = 0;
856
+ h = 0;
857
+ frameId = null;
858
+ paused = false;
859
+ last = 0;
860
+ clock = 0;
861
+ constructor(ctx) {
862
+ this.ctx = ctx;
863
+ let canvas;
864
+ if (ctx.surface) {
865
+ canvas = ctx.surface;
866
+ } else {
867
+ const el = document.createElement("canvas");
868
+ el.style.cssText = "display:block;width:100%;height:100%";
869
+ ctx.host.appendChild(el);
870
+ canvas = el;
871
+ }
872
+ this.canvas = canvas;
873
+ const c2d = canvas.getContext("2d", { alpha: true });
874
+ if (!c2d) throw new Error("spotlight: no 2d context");
875
+ this.c2d = c2d;
876
+ this.w = ctx.width;
877
+ this.h = ctx.height;
878
+ this.sizeCanvas();
879
+ this.setPaused(ctx.reducedMotion);
880
+ if (this.paused) this.render();
881
+ }
882
+ sizeCanvas() {
883
+ const dpr = Math.min(this.ctx.dpr, 2);
884
+ this.canvas.width = Math.max(1, Math.round(this.w * dpr));
885
+ this.canvas.height = Math.max(1, Math.round(this.h * dpr));
886
+ this.c2d.setTransform(dpr, 0, 0, dpr, 0, 0);
887
+ }
888
+ centre() {
889
+ const tri = (t, period) => {
890
+ const p = (t % (period * 2) + period * 2) % (period * 2);
891
+ return p < period ? p / period : 2 - p / period;
892
+ };
893
+ const kx = tri(this.clock, SPEED_X);
894
+ const ky = tri(this.clock, SPEED_Y);
895
+ const x = HOLE_RADIUS + kx * (this.w - HOLE_RADIUS * 2);
896
+ const y = HOLE_RADIUS + ky * (this.h - HOLE_RADIUS * 2);
897
+ return [x, y];
898
+ }
899
+ start() {
900
+ if (this.frameId !== null || typeof requestAnimationFrame === "undefined") return;
901
+ this.last = 0;
902
+ this.frameId = requestAnimationFrame((now) => this.loop(now));
903
+ }
904
+ stop() {
905
+ if (this.frameId !== null) {
906
+ cancelAnimationFrame(this.frameId);
907
+ this.frameId = null;
908
+ }
909
+ }
910
+ loop(now) {
911
+ this.frameId = requestAnimationFrame((n) => this.loop(n));
912
+ if (this.last === 0) this.last = now;
913
+ this.clock += Math.min(50, now - this.last);
914
+ this.last = now;
915
+ this.render();
916
+ }
917
+ render() {
918
+ const ctx = this.c2d;
919
+ const [sx, sy] = this.centre();
920
+ ctx.clearRect(0, 0, this.w, this.h);
921
+ ctx.fillStyle = "rgba(0, 0, 0, 0.92)";
922
+ ctx.fillRect(0, 0, this.w, this.h);
923
+ ctx.globalCompositeOperation = "destination-out";
924
+ const hole = ctx.createRadialGradient(sx, sy, 0, sx, sy, HOLE_RADIUS * 1.15);
925
+ hole.addColorStop(0, "rgba(0,0,0,1)");
926
+ hole.addColorStop(0.62, "rgba(0,0,0,1)");
927
+ hole.addColorStop(0.85, "rgba(0,0,0,0.5)");
928
+ hole.addColorStop(1, "rgba(0,0,0,0)");
929
+ ctx.fillStyle = hole;
930
+ ctx.fillRect(sx - HOLE_RADIUS * 1.2, sy - HOLE_RADIUS * 1.2, HOLE_RADIUS * 2.4, HOLE_RADIUS * 2.4);
931
+ ctx.globalCompositeOperation = "source-over";
932
+ const glow = ctx.createRadialGradient(sx, sy, 0, sx, sy, HOLE_RADIUS);
933
+ glow.addColorStop(0, "rgba(255,250,230,0.12)");
934
+ glow.addColorStop(1, "rgba(255,250,230,0)");
935
+ ctx.fillStyle = glow;
936
+ ctx.beginPath();
937
+ ctx.arc(sx, sy, HOLE_RADIUS, 0, Math.PI * 2);
938
+ ctx.fill();
939
+ }
940
+ previewAt(t) {
941
+ this.clock = t;
942
+ this.render();
943
+ }
944
+ setPaused(paused) {
945
+ this.paused = paused;
946
+ if (paused) {
947
+ this.stop();
948
+ this.render();
949
+ } else {
950
+ this.start();
951
+ }
952
+ }
953
+ resize(width, height, dpr) {
954
+ this.w = width;
955
+ this.h = height;
956
+ if (dpr !== void 0) this.ctx.dpr = dpr;
957
+ this.sizeCanvas();
958
+ if (this.paused) this.render();
959
+ }
960
+ dispose() {
961
+ this.stop();
962
+ if (typeof HTMLCanvasElement !== "undefined" && this.canvas instanceof HTMLCanvasElement) this.canvas.remove();
963
+ }
964
+ };
965
+ var spotlight = {
966
+ manifest: spotlightManifest,
967
+ mount: (ctx) => new SpotlightInstance(ctx)
968
+ };
969
+
970
+ // src/mystify.ts
971
+ var mystifyManifest = {
972
+ id: "mystify",
973
+ label: "Mystify",
974
+ passthrough: false,
975
+ minBackend: "canvas2d",
976
+ costTier: "low",
977
+ motionIntensity: "moderate",
978
+ reducedMotionFallback: "static",
979
+ a11y: { flashSafe: true, notes: "Slow-fade polygon trails on black; no strobing." },
980
+ workerReady: true
981
+ };
982
+ var SHAPE_COUNT = 3;
983
+ var TRAIL_ALPHA = 0.12;
984
+ var MystifyInstance = class {
985
+ ctxSaver;
986
+ canvas;
987
+ ctx;
988
+ w = 0;
989
+ h = 0;
990
+ shapes = [];
991
+ frameId = null;
992
+ last = 0;
993
+ paused = false;
994
+ constructor(ctx) {
995
+ this.ctxSaver = ctx;
996
+ let canvas;
997
+ if (ctx.surface) {
998
+ canvas = ctx.surface;
999
+ } else {
1000
+ const el = document.createElement("canvas");
1001
+ el.style.cssText = "display:block;width:100%;height:100%";
1002
+ el.setAttribute("aria-hidden", "true");
1003
+ ctx.host.appendChild(el);
1004
+ canvas = el;
1005
+ }
1006
+ this.canvas = canvas;
1007
+ const c2d = canvas.getContext("2d", { alpha: false });
1008
+ if (!c2d) throw new Error("mystify: no 2d context");
1009
+ this.ctx = c2d;
1010
+ this.w = ctx.width;
1011
+ this.h = ctx.height;
1012
+ this.sizeCanvas();
1013
+ this.shapes = this.buildShapes(ctx.rng);
1014
+ this.clearFull();
1015
+ this.paused = ctx.reducedMotion;
1016
+ if (this.paused) this.renderStill();
1017
+ else this.start();
1018
+ }
1019
+ buildShapes(rng) {
1020
+ const out = [];
1021
+ for (let s = 0; s < SHAPE_COUNT; s++) {
1022
+ const vertCount = rng.int(3, 5);
1023
+ const hue = rng.int(0, 359);
1024
+ const verts = [];
1025
+ for (let v = 0; v < vertCount; v++) {
1026
+ const x = rng.range(this.w * 0.15, this.w * 0.85);
1027
+ const y = rng.range(this.h * 0.15, this.h * 0.85);
1028
+ const dir = rng.range(0, Math.PI * 2);
1029
+ const speed = rng.range(70, 150);
1030
+ verts.push({
1031
+ x,
1032
+ y,
1033
+ vx: Math.cos(dir) * speed,
1034
+ vy: Math.sin(dir) * speed
1035
+ });
1036
+ }
1037
+ out.push({ hue, verts });
1038
+ }
1039
+ return out;
1040
+ }
1041
+ sizeCanvas() {
1042
+ const dpr = Math.min(this.ctxSaver.dpr, 2);
1043
+ this.canvas.width = Math.max(1, Math.round(this.w * dpr));
1044
+ this.canvas.height = Math.max(1, Math.round(this.h * dpr));
1045
+ this.ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
1046
+ }
1047
+ clearFull() {
1048
+ this.ctx.fillStyle = "#000";
1049
+ this.ctx.fillRect(0, 0, this.w, this.h);
1050
+ }
1051
+ start() {
1052
+ if (this.frameId !== null || typeof requestAnimationFrame === "undefined") return;
1053
+ this.last = 0;
1054
+ this.frameId = requestAnimationFrame((now) => this.loop(now));
1055
+ }
1056
+ stop() {
1057
+ if (this.frameId !== null) {
1058
+ cancelAnimationFrame(this.frameId);
1059
+ this.frameId = null;
1060
+ }
1061
+ }
1062
+ loop(now) {
1063
+ this.frameId = requestAnimationFrame((n) => this.loop(n));
1064
+ if (this.last === 0) this.last = now;
1065
+ const dt = Math.min(0.05, (now - this.last) / 1e3);
1066
+ this.last = now;
1067
+ this.update(dt);
1068
+ this.render();
1069
+ }
1070
+ update(dt) {
1071
+ for (const shape of this.shapes) {
1072
+ for (const v of shape.verts) {
1073
+ v.x += v.vx * dt;
1074
+ v.y += v.vy * dt;
1075
+ if (v.x <= 0) {
1076
+ v.x = 0;
1077
+ v.vx = Math.abs(v.vx);
1078
+ } else if (v.x >= this.w) {
1079
+ v.x = this.w;
1080
+ v.vx = -Math.abs(v.vx);
1081
+ }
1082
+ if (v.y <= 0) {
1083
+ v.y = 0;
1084
+ v.vy = Math.abs(v.vy);
1085
+ } else if (v.y >= this.h) {
1086
+ v.y = this.h;
1087
+ v.vy = -Math.abs(v.vy);
1088
+ }
1089
+ }
1090
+ }
1091
+ }
1092
+ drawShapes() {
1093
+ const ctx = this.ctx;
1094
+ ctx.lineCap = "round";
1095
+ ctx.lineJoin = "round";
1096
+ for (const shape of this.shapes) {
1097
+ const { verts } = shape;
1098
+ if (verts.length < 2) continue;
1099
+ ctx.strokeStyle = `hsla(${shape.hue}, 90%, 62%, 0.92)`;
1100
+ ctx.lineWidth = 2;
1101
+ ctx.beginPath();
1102
+ ctx.moveTo(verts[0].x, verts[0].y);
1103
+ for (let i = 1; i < verts.length; i++) ctx.lineTo(verts[i].x, verts[i].y);
1104
+ ctx.closePath();
1105
+ ctx.stroke();
1106
+ }
1107
+ }
1108
+ render() {
1109
+ this.ctx.fillStyle = `rgba(0,0,0,${TRAIL_ALPHA})`;
1110
+ this.ctx.fillRect(0, 0, this.w, this.h);
1111
+ this.drawShapes();
1112
+ }
1113
+ renderStill() {
1114
+ this.clearFull();
1115
+ this.drawShapes();
1116
+ }
1117
+ setPaused(paused) {
1118
+ this.paused = paused;
1119
+ if (paused) {
1120
+ this.stop();
1121
+ this.renderStill();
1122
+ } else {
1123
+ this.clearFull();
1124
+ this.start();
1125
+ }
1126
+ }
1127
+ resize(width, height, dpr) {
1128
+ this.w = width;
1129
+ this.h = height;
1130
+ if (dpr !== void 0) this.ctxSaver.dpr = dpr;
1131
+ this.sizeCanvas();
1132
+ for (const shape of this.shapes) {
1133
+ for (const v of shape.verts) {
1134
+ v.x = Math.min(Math.max(v.x, 0), this.w);
1135
+ v.y = Math.min(Math.max(v.y, 0), this.h);
1136
+ }
1137
+ }
1138
+ if (this.paused) this.renderStill();
1139
+ else this.clearFull();
1140
+ }
1141
+ dispose() {
1142
+ this.stop();
1143
+ if (typeof HTMLCanvasElement !== "undefined" && this.canvas instanceof HTMLCanvasElement) {
1144
+ this.canvas.remove();
1145
+ }
1146
+ }
1147
+ };
1148
+ var mystify = {
1149
+ manifest: mystifyManifest,
1150
+ mount: (ctx) => new MystifyInstance(ctx)
1151
+ };
1152
+
1153
+ // src/pipes.ts
1154
+ var pipesManifest = {
1155
+ id: "pipes",
1156
+ label: "Pipes",
1157
+ passthrough: false,
1158
+ minBackend: "canvas2d",
1159
+ costTier: "low",
1160
+ motionIntensity: "moderate",
1161
+ reducedMotionFallback: "static",
1162
+ a11y: { flashSafe: true },
1163
+ workerReady: true
1164
+ };
1165
+ var CELL = 20;
1166
+ var PIPE_WIDTH = 8;
1167
+ var STEPS_PER_FRAME = 3;
1168
+ var FILL_THRESHOLD = 0.65;
1169
+ var PALETTE = ["#c0392b", "#2980b9", "#27ae60", "#f39c12", "#8e44ad", "#d4a259", "#7f8c8d"];
1170
+ var DX = { 0: 1, 1: 0, 2: -1, 3: 0 };
1171
+ var DY = { 0: 0, 1: 1, 2: 0, 3: -1 };
1172
+ var PipesInstance = class {
1173
+ ctxSaver;
1174
+ canvas;
1175
+ ctx;
1176
+ w = 0;
1177
+ h = 0;
1178
+ cols = 0;
1179
+ rows = 0;
1180
+ grid = [];
1181
+ filled = 0;
1182
+ pipe = null;
1183
+ rng;
1184
+ frameId = null;
1185
+ paused = false;
1186
+ constructor(ctx) {
1187
+ this.ctxSaver = ctx;
1188
+ this.rng = ctx.rng;
1189
+ let canvas;
1190
+ if (ctx.surface) {
1191
+ canvas = ctx.surface;
1192
+ } else {
1193
+ const el = document.createElement("canvas");
1194
+ el.style.cssText = "display:block;width:100%;height:100%";
1195
+ el.setAttribute("aria-hidden", "true");
1196
+ ctx.host.appendChild(el);
1197
+ canvas = el;
1198
+ }
1199
+ this.canvas = canvas;
1200
+ const c2d = canvas.getContext("2d", { alpha: false });
1201
+ if (!c2d) throw new Error("pipes: no 2d context");
1202
+ this.ctx = c2d;
1203
+ this.w = ctx.width;
1204
+ this.h = ctx.height;
1205
+ this.sizeCanvas();
1206
+ this.rebuild();
1207
+ this.paused = ctx.reducedMotion;
1208
+ if (this.paused) this.renderStill();
1209
+ else this.start();
1210
+ }
1211
+ sizeCanvas() {
1212
+ const dpr = Math.min(this.ctxSaver.dpr, 2);
1213
+ this.canvas.width = Math.max(1, Math.round(this.w * dpr));
1214
+ this.canvas.height = Math.max(1, Math.round(this.h * dpr));
1215
+ this.ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
1216
+ }
1217
+ rebuild() {
1218
+ this.cols = Math.max(2, Math.floor(this.w / CELL));
1219
+ this.rows = Math.max(2, Math.floor(this.h / CELL));
1220
+ this.grid = new Array(this.cols * this.rows).fill(false);
1221
+ this.filled = 0;
1222
+ this.pipe = null;
1223
+ this.paintBackground();
1224
+ }
1225
+ paintBackground() {
1226
+ this.ctx.fillStyle = "#111";
1227
+ this.ctx.fillRect(0, 0, this.w, this.h);
1228
+ }
1229
+ idx(col, row) {
1230
+ return row * this.cols + col;
1231
+ }
1232
+ inBounds(col, row) {
1233
+ return col >= 0 && col < this.cols && row >= 0 && row < this.rows;
1234
+ }
1235
+ spawnPipe() {
1236
+ const empty = [];
1237
+ for (let i = 0; i < this.grid.length; i++) {
1238
+ if (!this.grid[i]) empty.push(i);
1239
+ }
1240
+ if (empty.length === 0) {
1241
+ this.clearGrid();
1242
+ return;
1243
+ }
1244
+ const cell = this.rng.pick(empty);
1245
+ const col = cell % this.cols;
1246
+ const row = Math.floor(cell / this.cols);
1247
+ this.pipe = {
1248
+ col,
1249
+ row,
1250
+ dir: this.rng.int(0, 3),
1251
+ color: this.rng.pick(PALETTE)
1252
+ };
1253
+ this.grid[cell] = true;
1254
+ this.filled++;
1255
+ this.drawJoint(col, row, this.pipe.color);
1256
+ }
1257
+ clearGrid() {
1258
+ this.grid.fill(false);
1259
+ this.filled = 0;
1260
+ this.pipe = null;
1261
+ this.paintBackground();
1262
+ }
1263
+ growStep() {
1264
+ if (this.filled / this.grid.length > FILL_THRESHOLD) {
1265
+ this.clearGrid();
1266
+ return;
1267
+ }
1268
+ if (!this.pipe) {
1269
+ this.spawnPipe();
1270
+ return;
1271
+ }
1272
+ const candidates = this.getCandidates(this.pipe);
1273
+ if (candidates.length === 0) {
1274
+ this.pipe = null;
1275
+ return;
1276
+ }
1277
+ const oldCol = this.pipe.col;
1278
+ const oldRow = this.pipe.row;
1279
+ const oldDir = this.pipe.dir;
1280
+ const newDir = this.rng.pick(candidates);
1281
+ this.pipe.dir = newDir;
1282
+ const nc = oldCol + DX[newDir];
1283
+ const nr = oldRow + DY[newDir];
1284
+ this.drawSegment(oldCol, oldRow, nc, nr, this.pipe.color);
1285
+ if (newDir !== oldDir) {
1286
+ this.drawJoint(oldCol, oldRow, this.pipe.color);
1287
+ }
1288
+ this.pipe.col = nc;
1289
+ this.pipe.row = nr;
1290
+ this.grid[this.idx(nc, nr)] = true;
1291
+ this.filled++;
1292
+ }
1293
+ getCandidates(pipe) {
1294
+ const out = [];
1295
+ for (let d = 0; d < 4; d++) {
1296
+ const nc = pipe.col + DX[d];
1297
+ const nr = pipe.row + DY[d];
1298
+ if (this.inBounds(nc, nr) && !this.grid[this.idx(nc, nr)]) {
1299
+ out.push(d);
1300
+ }
1301
+ }
1302
+ if (out.includes(pipe.dir) && this.rng.next() < 0.65) {
1303
+ return [pipe.dir];
1304
+ }
1305
+ return out;
1306
+ }
1307
+ cellCenter(col, row) {
1308
+ return [col * CELL + CELL / 2, row * CELL + CELL / 2];
1309
+ }
1310
+ drawSegment(c1, r1, c2, r2, color) {
1311
+ const [x1, y1] = this.cellCenter(c1, r1);
1312
+ const [x2, y2] = this.cellCenter(c2, r2);
1313
+ const ctx = this.ctx;
1314
+ ctx.strokeStyle = color;
1315
+ ctx.lineWidth = PIPE_WIDTH;
1316
+ ctx.lineCap = "round";
1317
+ ctx.beginPath();
1318
+ ctx.moveTo(x1, y1);
1319
+ ctx.lineTo(x2, y2);
1320
+ ctx.stroke();
1321
+ }
1322
+ drawJoint(col, row, color) {
1323
+ const [x, y] = this.cellCenter(col, row);
1324
+ const ctx = this.ctx;
1325
+ ctx.fillStyle = color;
1326
+ ctx.beginPath();
1327
+ ctx.arc(x, y, PIPE_WIDTH * 0.65, 0, Math.PI * 2);
1328
+ ctx.fill();
1329
+ }
1330
+ start() {
1331
+ if (this.frameId !== null || typeof requestAnimationFrame === "undefined") return;
1332
+ this.loop();
1333
+ }
1334
+ stop() {
1335
+ if (this.frameId !== null) {
1336
+ cancelAnimationFrame(this.frameId);
1337
+ this.frameId = null;
1338
+ }
1339
+ }
1340
+ loop() {
1341
+ this.frameId = requestAnimationFrame(() => this.loop());
1342
+ for (let i = 0; i < STEPS_PER_FRAME; i++) this.growStep();
1343
+ }
1344
+ renderStill() {
1345
+ this.rebuild();
1346
+ for (let i = 0; i < 80; i++) this.growStep();
1347
+ }
1348
+ setPaused(paused) {
1349
+ this.paused = paused;
1350
+ if (paused) {
1351
+ this.stop();
1352
+ } else {
1353
+ this.start();
1354
+ }
1355
+ }
1356
+ resize(width, height, dpr) {
1357
+ this.w = width;
1358
+ this.h = height;
1359
+ if (dpr !== void 0) this.ctxSaver.dpr = dpr;
1360
+ this.sizeCanvas();
1361
+ this.rebuild();
1362
+ if (this.paused) this.renderStill();
1363
+ }
1364
+ dispose() {
1365
+ this.stop();
1366
+ if (typeof HTMLCanvasElement !== "undefined" && this.canvas instanceof HTMLCanvasElement) this.canvas.remove();
1367
+ }
1368
+ };
1369
+ var pipes = {
1370
+ manifest: pipesManifest,
1371
+ mount: (ctx) => new PipesInstance(ctx)
1372
+ };
1373
+
1374
+ // src/flurry.ts
1375
+ var flurryManifest = {
1376
+ id: "flurry",
1377
+ label: "Flurry",
1378
+ passthrough: false,
1379
+ minBackend: "canvas2d",
1380
+ costTier: "low",
1381
+ motionIntensity: "calm",
1382
+ reducedMotionFallback: "static",
1383
+ a11y: { flashSafe: true },
1384
+ workerReady: true
1385
+ };
1386
+ var STREAM_COUNT = 5;
1387
+ var PARTICLES_PER_STREAM = 12;
1388
+ var TRAIL_ALPHA2 = 0.06;
1389
+ var BASE_RADIUS = 6;
1390
+ var FlurryInstance = class {
1391
+ ctxSaver;
1392
+ canvas;
1393
+ ctx;
1394
+ w = 0;
1395
+ h = 0;
1396
+ streams;
1397
+ frameId = null;
1398
+ t = 0;
1399
+ lastTime = 0;
1400
+ paused = false;
1401
+ constructor(ctx) {
1402
+ this.ctxSaver = ctx;
1403
+ this.w = ctx.width;
1404
+ this.h = ctx.height;
1405
+ let canvas;
1406
+ if (ctx.surface) {
1407
+ canvas = ctx.surface;
1408
+ } else {
1409
+ const el = document.createElement("canvas");
1410
+ el.style.cssText = "display:block;width:100%;height:100%";
1411
+ el.setAttribute("aria-hidden", "true");
1412
+ ctx.host.appendChild(el);
1413
+ canvas = el;
1414
+ }
1415
+ this.canvas = canvas;
1416
+ const c2d = canvas.getContext("2d", { alpha: false });
1417
+ if (!c2d) throw new Error("flurry: no 2d context");
1418
+ this.ctx = c2d;
1419
+ this.streams = this.buildStreams(ctx.rng);
1420
+ this.sizeCanvas();
1421
+ this.clearFull();
1422
+ this.paused = ctx.reducedMotion;
1423
+ if (this.paused) {
1424
+ this.renderStill();
1425
+ } else {
1426
+ this.start();
1427
+ }
1428
+ }
1429
+ buildStreams(rng) {
1430
+ const out = [];
1431
+ for (let i = 0; i < STREAM_COUNT; i++) {
1432
+ out.push({
1433
+ hue: (i * 360 / STREAM_COUNT + rng.range(0, 30)) % 360,
1434
+ freqX1: rng.range(0.3, 0.9),
1435
+ freqY1: rng.range(0.3, 0.9),
1436
+ freqX2: rng.range(0.1, 0.4),
1437
+ freqY2: rng.range(0.1, 0.4),
1438
+ phaseX: rng.range(0, Math.PI * 2),
1439
+ phaseY: rng.range(0, Math.PI * 2),
1440
+ ampX: rng.range(0.2, 0.4),
1441
+ ampY: rng.range(0.2, 0.4),
1442
+ speed: rng.range(0.6, 1.2)
1443
+ });
1444
+ }
1445
+ return out;
1446
+ }
1447
+ sizeCanvas() {
1448
+ const dpr = Math.min(this.ctxSaver.dpr, 2);
1449
+ this.canvas.width = Math.max(1, Math.round(this.w * dpr));
1450
+ this.canvas.height = Math.max(1, Math.round(this.h * dpr));
1451
+ this.ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
1452
+ }
1453
+ clearFull() {
1454
+ this.ctx.fillStyle = "#000";
1455
+ this.ctx.fillRect(0, 0, this.w, this.h);
1456
+ }
1457
+ streamPos(s, t) {
1458
+ const cx = this.w / 2;
1459
+ const cy = this.h / 2;
1460
+ const x = cx + Math.sin(t * s.freqX1 + s.phaseX) * this.w * s.ampX + Math.sin(t * s.freqX2 + s.phaseX * 1.7) * this.w * s.ampX * 0.3;
1461
+ const y = cy + Math.cos(t * s.freqY1 + s.phaseY) * this.h * s.ampY + Math.cos(t * s.freqY2 + s.phaseY * 2.1) * this.h * s.ampY * 0.3;
1462
+ return [x, y];
1463
+ }
1464
+ drawFrame(_dt) {
1465
+ const ctx = this.ctx;
1466
+ ctx.globalCompositeOperation = "source-over";
1467
+ ctx.fillStyle = `rgba(0,0,0,${TRAIL_ALPHA2})`;
1468
+ ctx.fillRect(0, 0, this.w, this.h);
1469
+ ctx.globalCompositeOperation = "lighter";
1470
+ for (const s of this.streams) {
1471
+ const st = this.t * s.speed;
1472
+ for (let p = 0; p < PARTICLES_PER_STREAM; p++) {
1473
+ const offset = p * 0.012;
1474
+ const [x, y] = this.streamPos(s, st - offset);
1475
+ const hue = (s.hue + this.t * 8 + p * 3) % 360;
1476
+ const alpha = 0.15 + 0.1 * (1 - p / PARTICLES_PER_STREAM);
1477
+ const r = BASE_RADIUS * (1 - p * 0.04);
1478
+ const grad = ctx.createRadialGradient(x, y, 0, x, y, r * 3);
1479
+ grad.addColorStop(0, `hsla(${hue},90%,60%,${alpha})`);
1480
+ grad.addColorStop(0.4, `hsla(${hue},80%,50%,${alpha * 0.5})`);
1481
+ grad.addColorStop(1, `hsla(${hue},70%,40%,0)`);
1482
+ ctx.fillStyle = grad;
1483
+ ctx.beginPath();
1484
+ ctx.arc(x, y, r * 3, 0, Math.PI * 2);
1485
+ ctx.fill();
1486
+ }
1487
+ }
1488
+ ctx.globalCompositeOperation = "source-over";
1489
+ }
1490
+ start() {
1491
+ if (this.frameId !== null || typeof requestAnimationFrame === "undefined") return;
1492
+ this.lastTime = 0;
1493
+ this.loop(0);
1494
+ }
1495
+ stop() {
1496
+ if (this.frameId !== null) {
1497
+ cancelAnimationFrame(this.frameId);
1498
+ this.frameId = null;
1499
+ }
1500
+ }
1501
+ loop(now) {
1502
+ this.frameId = requestAnimationFrame((n) => this.loop(n));
1503
+ const dt = this.lastTime ? (now - this.lastTime) / 1e3 : 1 / 60;
1504
+ this.lastTime = now;
1505
+ this.t += Math.min(dt, 0.1);
1506
+ this.drawFrame(dt);
1507
+ }
1508
+ renderStill() {
1509
+ this.clearFull();
1510
+ for (let i = 0; i < 120; i++) {
1511
+ this.t += 1 / 60;
1512
+ this.drawFrame(1 / 60);
1513
+ }
1514
+ }
1515
+ setPaused(paused) {
1516
+ this.paused = paused;
1517
+ if (paused) {
1518
+ this.stop();
1519
+ } else {
1520
+ this.start();
1521
+ }
1522
+ }
1523
+ resize(width, height, dpr) {
1524
+ this.w = width;
1525
+ this.h = height;
1526
+ if (dpr !== void 0) this.ctxSaver.dpr = dpr;
1527
+ this.sizeCanvas();
1528
+ this.clearFull();
1529
+ if (this.paused) this.renderStill();
1530
+ }
1531
+ dispose() {
1532
+ this.stop();
1533
+ if (typeof HTMLCanvasElement !== "undefined" && this.canvas instanceof HTMLCanvasElement) this.canvas.remove();
1534
+ }
1535
+ };
1536
+ var flurry = {
1537
+ manifest: flurryManifest,
1538
+ mount: (ctx) => new FlurryInstance(ctx)
1539
+ };
1540
+
1541
+ // src/fluid-shared.ts
1542
+ var DT = 0.025;
1543
+ var DENS_DECAY = 0.993;
1544
+ var VEL_DECAY = 0.98;
1545
+ var DYE_RATE = 1400;
1546
+ var FORCE_RATE = 5;
1547
+ var EMITTER_N = 4;
1548
+ function hue2rgb(h) {
1549
+ h = (h % 360 + 360) % 360;
1550
+ const s = h / 60;
1551
+ const x = 1 - Math.abs(s % 2 - 1);
1552
+ if (s < 1) return [1, x, 0];
1553
+ if (s < 2) return [x, 1, 0];
1554
+ if (s < 3) return [0, 1, x];
1555
+ if (s < 4) return [0, x, 1];
1556
+ if (s < 5) return [x, 0, 1];
1557
+ return [1, 0, x];
1558
+ }
1559
+ function buildEmitters(rng, N3) {
1560
+ const out = [];
1561
+ for (let i = 0; i < EMITTER_N; i++) {
1562
+ const px = rng.range(0, Math.PI * 2);
1563
+ const py = rng.range(0, Math.PI * 2);
1564
+ out.push({
1565
+ hue: (i * 360 / EMITTER_N + rng.range(0, 40)) % 360,
1566
+ fx: rng.range(0.3, 0.8),
1567
+ fy: rng.range(0.3, 0.8),
1568
+ px,
1569
+ py,
1570
+ speed: rng.range(0.5, 1),
1571
+ prevGx: (Math.sin(px) * 0.35 + 0.5) * N3 + 1,
1572
+ prevGy: (Math.cos(py) * 0.35 + 0.5) * N3 + 1
1573
+ });
1574
+ }
1575
+ return out;
1576
+ }
1577
+ function stepEmitters(t, emitters, N3) {
1578
+ return emitters.map((e) => {
1579
+ const st = t * e.speed;
1580
+ const gx = (Math.sin(st * e.fx + e.px) * 0.35 + 0.5) * N3 + 1;
1581
+ const gy = (Math.cos(st * e.fy + e.py) * 0.35 + 0.5) * N3 + 1;
1582
+ const dx = gx - e.prevGx;
1583
+ const dy = gy - e.prevGy;
1584
+ const [r, g, b] = hue2rgb(e.hue + t * 15);
1585
+ e.prevGx = gx;
1586
+ e.prevGy = gy;
1587
+ return { gx, gy, dx, dy, r, g, b };
1588
+ });
1589
+ }
1590
+
1591
+ // src/fluid-gpu.ts
1592
+ var GPU_N = 256;
1593
+ var GPU_STRIDE = GPU_N + 2;
1594
+ var GPU_SZ = GPU_STRIDE * GPU_STRIDE;
1595
+ var GPU_DIFF = 3e-5 * (96 / GPU_N) ** 2;
1596
+ var GPU_VISC = 3e-4 * (96 / GPU_N) ** 2;
1597
+ var GPU_INJECT_R = Math.round(4 * (GPU_N / 96));
1598
+ var GPU_FORCE_RATE = FORCE_RATE * (96 / GPU_N);
1599
+ var JACOBI_DIFF = 10;
1600
+ var JACOBI_PRES = 40;
1601
+ var COPY_WGSL = (
1602
+ /* wgsl */
1603
+ `
1604
+ override SZ: u32;
1605
+ @group(0) @binding(0) var<storage, read_write> dst: array<f32>;
1606
+ @group(0) @binding(1) var<storage, read> src: array<f32>;
1607
+ @compute @workgroup_size(256)
1608
+ fn main(@builtin(global_invocation_id) gid: vec3u) {
1609
+ if (gid.x >= SZ) { return; }
1610
+ dst[gid.x] = src[gid.x];
1611
+ }`
1612
+ );
1613
+ var ADD_SOURCE_WGSL = (
1614
+ /* wgsl */
1615
+ `
1616
+ override SZ: u32;
1617
+ struct P { dt: f32 }
1618
+ @group(0) @binding(0) var<uniform> p: P;
1619
+ @group(0) @binding(1) var<storage, read_write> dst: array<f32>;
1620
+ @group(0) @binding(2) var<storage, read> src: array<f32>;
1621
+ @compute @workgroup_size(256)
1622
+ fn main(@builtin(global_invocation_id) gid: vec3u) {
1623
+ if (gid.x >= SZ) { return; }
1624
+ dst[gid.x] += p.dt * src[gid.x];
1625
+ }`
1626
+ );
1627
+ var SCALE_WGSL = (
1628
+ /* wgsl */
1629
+ `
1630
+ override SZ: u32;
1631
+ struct P { factor: f32 }
1632
+ @group(0) @binding(0) var<uniform> p: P;
1633
+ @group(0) @binding(1) var<storage, read_write> field: array<f32>;
1634
+ @compute @workgroup_size(256)
1635
+ fn main(@builtin(global_invocation_id) gid: vec3u) {
1636
+ if (gid.x >= SZ) { return; }
1637
+ field[gid.x] *= p.factor;
1638
+ }`
1639
+ );
1640
+ var JACOBI_WGSL = (
1641
+ /* wgsl */
1642
+ `
1643
+ override N: u32;
1644
+ override STRIDE: u32;
1645
+ struct P { a: f32, c: f32 }
1646
+ @group(0) @binding(0) var<uniform> p: P;
1647
+ @group(0) @binding(1) var<storage, read_write> dst: array<f32>;
1648
+ @group(0) @binding(2) var<storage, read> x0: array<f32>;
1649
+ @group(0) @binding(3) var<storage, read> prev: array<f32>;
1650
+ @compute @workgroup_size(8, 8)
1651
+ fn main(@builtin(global_invocation_id) gid: vec3u) {
1652
+ let i = gid.x + 1u;
1653
+ let j = gid.y + 1u;
1654
+ if (i > N || j > N) { return; }
1655
+ let idx = i + STRIDE * j;
1656
+ dst[idx] = (x0[idx] + p.a * (prev[idx - 1u] + prev[idx + 1u]
1657
+ + prev[idx - STRIDE] + prev[idx + STRIDE])) / p.c;
1658
+ }`
1659
+ );
1660
+ var BOUNDARY_WGSL = (
1661
+ /* wgsl */
1662
+ `
1663
+ override N: u32;
1664
+ override STRIDE: u32;
1665
+ struct P { b: u32 }
1666
+ @group(0) @binding(0) var<uniform> p: P;
1667
+ @group(0) @binding(1) var<storage, read_write> x: array<f32>;
1668
+ @compute @workgroup_size(64)
1669
+ fn main(@builtin(global_invocation_id) gid: vec3u) {
1670
+ let i = gid.x + 1u;
1671
+ if (i > N) { return; }
1672
+ let s = STRIDE;
1673
+ let sl = select(1.0, -1.0, p.b == 1u);
1674
+ let st = select(1.0, -1.0, p.b == 2u);
1675
+ x[s * i] = sl * x[1u + s * i];
1676
+ x[(N + 1u) + s * i] = sl * x[N + s * i];
1677
+ x[i] = st * x[i + s];
1678
+ x[i + s * (N + 1u)] = st * x[i + s * N];
1679
+ if (i == 1u) {
1680
+ x[0u] = x[1u + s];
1681
+ x[s * (N + 1u)] = x[1u + s * N];
1682
+ x[N + 1u] = x[N + s];
1683
+ x[(N + 1u) + s * (N + 1u)] = x[N + s * N];
1684
+ }
1685
+ }`
1686
+ );
1687
+ var ADVECT_WGSL = (
1688
+ /* wgsl */
1689
+ `
1690
+ override N: u32;
1691
+ override STRIDE: u32;
1692
+ struct P { dt0: f32 }
1693
+ @group(0) @binding(0) var<uniform> p: P;
1694
+ @group(0) @binding(1) var<storage, read_write> d: array<f32>;
1695
+ @group(0) @binding(2) var<storage, read> d0: array<f32>;
1696
+ @group(0) @binding(3) var<storage, read> u: array<f32>;
1697
+ @group(0) @binding(4) var<storage, read> v: array<f32>;
1698
+ @compute @workgroup_size(8, 8)
1699
+ fn main(@builtin(global_invocation_id) gid: vec3u) {
1700
+ let i = gid.x + 1u;
1701
+ let j = gid.y + 1u;
1702
+ if (i > N || j > N) { return; }
1703
+ let idx = i + STRIDE * j;
1704
+ var bx = f32(i) - p.dt0 * u[idx];
1705
+ var by = f32(j) - p.dt0 * v[idx];
1706
+ bx = clamp(bx, 0.5, f32(N) + 0.5);
1707
+ by = clamp(by, 0.5, f32(N) + 0.5);
1708
+ let i0 = u32(floor(bx));
1709
+ let j0 = u32(floor(by));
1710
+ let s1 = bx - f32(i0);
1711
+ let s0 = 1.0 - s1;
1712
+ let t1 = by - f32(j0);
1713
+ let t0 = 1.0 - t1;
1714
+ let s = STRIDE;
1715
+ d[idx] = s0 * (t0 * d0[i0 + s * j0] + t1 * d0[i0 + s * (j0 + 1u)])
1716
+ + s1 * (t0 * d0[(i0 + 1u) + s * j0] + t1 * d0[(i0 + 1u) + s * (j0 + 1u)]);
1717
+ }`
1718
+ );
1719
+ var DIVERGENCE_WGSL = (
1720
+ /* wgsl */
1721
+ `
1722
+ override N: u32;
1723
+ override STRIDE: u32;
1724
+ @group(0) @binding(0) var<storage, read_write> div: array<f32>;
1725
+ @group(0) @binding(1) var<storage, read> u: array<f32>;
1726
+ @group(0) @binding(2) var<storage, read> v: array<f32>;
1727
+ @compute @workgroup_size(8, 8)
1728
+ fn main(@builtin(global_invocation_id) gid: vec3u) {
1729
+ let i = gid.x + 1u;
1730
+ let j = gid.y + 1u;
1731
+ if (i > N || j > N) { return; }
1732
+ let idx = i + STRIDE * j;
1733
+ let h = 1.0 / f32(N);
1734
+ div[idx] = -0.5 * h * (u[idx + 1u] - u[idx - 1u] + v[idx + STRIDE] - v[idx - STRIDE]);
1735
+ }`
1736
+ );
1737
+ var GRADIENT_SUB_WGSL = (
1738
+ /* wgsl */
1739
+ `
1740
+ override N: u32;
1741
+ override STRIDE: u32;
1742
+ @group(0) @binding(0) var<storage, read_write> u: array<f32>;
1743
+ @group(0) @binding(1) var<storage, read_write> v: array<f32>;
1744
+ @group(0) @binding(2) var<storage, read> p: array<f32>;
1745
+ @compute @workgroup_size(8, 8)
1746
+ fn main(@builtin(global_invocation_id) gid: vec3u) {
1747
+ let i = gid.x + 1u;
1748
+ let j = gid.y + 1u;
1749
+ if (i > N || j > N) { return; }
1750
+ let idx = i + STRIDE * j;
1751
+ u[idx] -= 0.5 * f32(N) * (p[idx + 1u] - p[idx - 1u]);
1752
+ v[idx] -= 0.5 * f32(N) * (p[idx + STRIDE] - p[idx - STRIDE]);
1753
+ }`
1754
+ );
1755
+ var INJECT_WGSL = (
1756
+ /* wgsl */
1757
+ `
1758
+ override N: u32;
1759
+ override STRIDE: u32;
1760
+ struct EmitterData { pos: vec4f, col: vec4f }
1761
+ struct IP {
1762
+ inject_r: f32, dye_rate: f32, force_rate: f32, count: u32,
1763
+ data: array<EmitterData, 4>,
1764
+ }
1765
+ @group(0) @binding(0) var<uniform> p: IP;
1766
+ @group(0) @binding(1) var<storage, read_write> u0: array<f32>;
1767
+ @group(0) @binding(2) var<storage, read_write> v0: array<f32>;
1768
+ @group(0) @binding(3) var<storage, read_write> dr0: array<f32>;
1769
+ @group(0) @binding(4) var<storage, read_write> dg0: array<f32>;
1770
+ @group(0) @binding(5) var<storage, read_write> db0: array<f32>;
1771
+ @compute @workgroup_size(8, 8)
1772
+ fn main(@builtin(global_invocation_id) gid: vec3u) {
1773
+ let i = gid.x;
1774
+ let j = gid.y;
1775
+ if (i >= STRIDE || j >= STRIDE) { return; }
1776
+ let idx = i + STRIDE * j;
1777
+ u0[idx] = 0.0; v0[idx] = 0.0;
1778
+ dr0[idx] = 0.0; dg0[idx] = 0.0; db0[idx] = 0.0;
1779
+ if (i < 1u || i > N || j < 1u || j > N) { return; }
1780
+ let fi = f32(i);
1781
+ let fj = f32(j);
1782
+ for (var e = 0u; e < p.count; e = e + 1u) {
1783
+ let em = p.data[e];
1784
+ let dx = fi - em.pos.x;
1785
+ let dy = fj - em.pos.y;
1786
+ let dist = sqrt(dx * dx + dy * dy);
1787
+ if (dist > p.inject_r) { continue; }
1788
+ let w = 1.0 - dist / p.inject_r;
1789
+ dr0[idx] += em.col.x * p.dye_rate * w;
1790
+ dg0[idx] += em.col.y * p.dye_rate * w;
1791
+ db0[idx] += em.col.z * p.dye_rate * w;
1792
+ u0[idx] += em.pos.z * p.force_rate * w;
1793
+ v0[idx] += em.pos.w * p.force_rate * w;
1794
+ }
1795
+ }`
1796
+ );
1797
+ var RENDER_WGSL = (
1798
+ /* wgsl */
1799
+ `
1800
+ override N: u32;
1801
+ override STRIDE: u32;
1802
+ struct VO { @builtin(position) pos: vec4f, @location(0) uv: vec2f }
1803
+ @vertex fn vs(@builtin(vertex_index) vi: u32) -> VO {
1804
+ var o: VO;
1805
+ let x = f32(i32(vi) / 2) * 4.0 - 1.0;
1806
+ let y = f32(i32(vi) % 2) * 4.0 - 1.0;
1807
+ o.pos = vec4f(x, y, 0.0, 1.0);
1808
+ o.uv = vec2f((x + 1.0) * 0.5, (1.0 - y) * 0.5);
1809
+ return o;
1810
+ }
1811
+ @group(0) @binding(0) var<storage, read> dr: array<f32>;
1812
+ @group(0) @binding(1) var<storage, read> dg: array<f32>;
1813
+ @group(0) @binding(2) var<storage, read> db: array<f32>;
1814
+ @fragment fn fs(@location(0) uv: vec2f) -> @location(0) vec4f {
1815
+ let gx = clamp(uv.x * f32(N) + 0.5, 1.0, f32(N));
1816
+ let gy = clamp(uv.y * f32(N) + 0.5, 1.0, f32(N));
1817
+ let i0 = u32(floor(gx));
1818
+ let j0 = u32(floor(gy));
1819
+ let s1 = gx - f32(i0);
1820
+ let s0 = 1.0 - s1;
1821
+ let t1 = gy - f32(j0);
1822
+ let t0 = 1.0 - t1;
1823
+ let s = STRIDE;
1824
+ let a00 = i0 + s * j0;
1825
+ let a01 = i0 + s * (j0 + 1u);
1826
+ let a10 = (i0 + 1u) + s * j0;
1827
+ let a11 = (i0 + 1u) + s * (j0 + 1u);
1828
+ let r = s0 * (t0 * dr[a00] + t1 * dr[a01]) + s1 * (t0 * dr[a10] + t1 * dr[a11]);
1829
+ let g = s0 * (t0 * dg[a00] + t1 * dg[a01]) + s1 * (t0 * dg[a10] + t1 * dg[a11]);
1830
+ let b = s0 * (t0 * db[a00] + t1 * db[a01]) + s1 * (t0 * db[a10] + t1 * db[a11]);
1831
+ return vec4f(clamp(r / 255.0, 0.0, 1.0), clamp(g / 255.0, 0.0, 1.0),
1832
+ clamp(b / 255.0, 0.0, 1.0), 1.0);
1833
+ }`
1834
+ );
1835
+ var WG1D = Math.ceil(GPU_SZ / 256);
1836
+ var WG2D = Math.ceil(GPU_N / 8);
1837
+ var WG_BND = Math.ceil(GPU_N / 64);
1838
+ var WG_INJ = Math.ceil(GPU_STRIDE / 8);
1839
+ function mkUniform(dev, data) {
1840
+ const size = Math.ceil(Math.max(data.byteLength, 16) / 16) * 16;
1841
+ const buf = dev.createBuffer({
1842
+ size,
1843
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
1844
+ });
1845
+ dev.queue.writeBuffer(buf, 0, data.buffer, data.byteOffset, data.byteLength);
1846
+ return buf;
1847
+ }
1848
+ function mkStorage(dev) {
1849
+ return dev.createBuffer({
1850
+ size: GPU_SZ * 4,
1851
+ usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST
1852
+ });
1853
+ }
1854
+ function mkPipeline(dev, code, constants) {
1855
+ return dev.createComputePipeline({
1856
+ layout: "auto",
1857
+ compute: { module: dev.createShaderModule({ code }), constants }
1858
+ });
1859
+ }
1860
+ var FIELDS = [
1861
+ "u",
1862
+ "v",
1863
+ "dr",
1864
+ "dg",
1865
+ "db",
1866
+ "u0",
1867
+ "v0",
1868
+ "dr0",
1869
+ "dg0",
1870
+ "db0",
1871
+ "sA",
1872
+ "sB",
1873
+ "div",
1874
+ "p"
1875
+ ];
1876
+ var FluidGPU = class _FluidGPU {
1877
+ device;
1878
+ rng;
1879
+ canvas;
1880
+ gpuCtx;
1881
+ format;
1882
+ dpr;
1883
+ f;
1884
+ copyPL;
1885
+ addSrcPL;
1886
+ scalePL;
1887
+ jacobiPL;
1888
+ bndPL;
1889
+ advectPL;
1890
+ divPL;
1891
+ gradPL;
1892
+ injectPL;
1893
+ renderPL;
1894
+ dtUni;
1895
+ viscUni;
1896
+ diffUni;
1897
+ presUni;
1898
+ densDecUni;
1899
+ velDecUni;
1900
+ zeroUni;
1901
+ bndUni;
1902
+ advUni;
1903
+ emitterUni;
1904
+ emitters;
1905
+ t = 0;
1906
+ w;
1907
+ h;
1908
+ frameId = null;
1909
+ lost = false;
1910
+ lastPreviewMs = -1;
1911
+ static PREVIEW_FRAME_MS = 1e3 / 60;
1912
+ constructor(ctx, device) {
1913
+ this.device = device;
1914
+ this.rng = ctx.rng;
1915
+ this.w = ctx.width;
1916
+ this.h = ctx.height;
1917
+ this.dpr = Math.min(ctx.dpr, 2);
1918
+ this.canvas = document.createElement("canvas");
1919
+ this.canvas.style.cssText = "display:block;width:100%;height:100%";
1920
+ this.canvas.setAttribute("aria-hidden", "true");
1921
+ this.canvas.setAttribute("data-fluid-backend", "webgpu");
1922
+ ctx.host.appendChild(this.canvas);
1923
+ this.canvas.width = Math.max(1, Math.round(this.w * this.dpr));
1924
+ this.canvas.height = Math.max(1, Math.round(this.h * this.dpr));
1925
+ this.gpuCtx = this.canvas.getContext("webgpu");
1926
+ this.format = navigator.gpu.getPreferredCanvasFormat();
1927
+ this.gpuCtx.configure({
1928
+ device,
1929
+ format: this.format,
1930
+ alphaMode: "opaque"
1931
+ });
1932
+ this.f = {};
1933
+ for (const n of FIELDS) this.f[n] = mkStorage(device);
1934
+ this.initPipelines();
1935
+ this.initUniforms();
1936
+ this.emitters = buildEmitters(ctx.rng, GPU_N);
1937
+ device.lost.then(() => {
1938
+ this.lost = true;
1939
+ this.stop();
1940
+ });
1941
+ if (ctx.reducedMotion) {
1942
+ this.renderStill();
1943
+ } else {
1944
+ this.start();
1945
+ }
1946
+ }
1947
+ // --- pipeline creation -----------------------------------------------------
1948
+ initPipelines() {
1949
+ const dev = this.device;
1950
+ const ov1 = { SZ: GPU_SZ };
1951
+ const ov2 = { N: GPU_N, STRIDE: GPU_STRIDE };
1952
+ this.copyPL = mkPipeline(dev, COPY_WGSL, ov1);
1953
+ this.addSrcPL = mkPipeline(dev, ADD_SOURCE_WGSL, ov1);
1954
+ this.scalePL = mkPipeline(dev, SCALE_WGSL, ov1);
1955
+ this.jacobiPL = mkPipeline(dev, JACOBI_WGSL, ov2);
1956
+ this.bndPL = mkPipeline(dev, BOUNDARY_WGSL, ov2);
1957
+ this.advectPL = mkPipeline(dev, ADVECT_WGSL, ov2);
1958
+ this.divPL = mkPipeline(dev, DIVERGENCE_WGSL, ov2);
1959
+ this.gradPL = mkPipeline(dev, GRADIENT_SUB_WGSL, ov2);
1960
+ this.injectPL = mkPipeline(dev, INJECT_WGSL, ov2);
1961
+ const renderMod = dev.createShaderModule({ code: RENDER_WGSL });
1962
+ this.renderPL = dev.createRenderPipeline({
1963
+ layout: "auto",
1964
+ vertex: {
1965
+ module: renderMod,
1966
+ entryPoint: "vs",
1967
+ constants: ov2
1968
+ },
1969
+ fragment: {
1970
+ module: renderMod,
1971
+ entryPoint: "fs",
1972
+ targets: [{ format: this.format }],
1973
+ constants: ov2
1974
+ },
1975
+ primitive: { topology: "triangle-list" }
1976
+ });
1977
+ }
1978
+ // --- uniform creation ------------------------------------------------------
1979
+ initUniforms() {
1980
+ const dev = this.device;
1981
+ this.dtUni = mkUniform(dev, new Float32Array([DT]));
1982
+ const aDiff = DT * GPU_DIFF * GPU_N * GPU_N;
1983
+ this.diffUni = mkUniform(dev, new Float32Array([aDiff, 1 + 4 * aDiff]));
1984
+ const aVisc = DT * GPU_VISC * GPU_N * GPU_N;
1985
+ this.viscUni = mkUniform(dev, new Float32Array([aVisc, 1 + 4 * aVisc]));
1986
+ this.presUni = mkUniform(dev, new Float32Array([1, 4]));
1987
+ this.densDecUni = mkUniform(dev, new Float32Array([DENS_DECAY]));
1988
+ this.velDecUni = mkUniform(dev, new Float32Array([VEL_DECAY]));
1989
+ this.zeroUni = mkUniform(dev, new Float32Array([0]));
1990
+ this.bndUni = [
1991
+ mkUniform(dev, new Uint32Array([0])),
1992
+ mkUniform(dev, new Uint32Array([1])),
1993
+ mkUniform(dev, new Uint32Array([2]))
1994
+ ];
1995
+ this.advUni = mkUniform(dev, new Float32Array([DT * GPU_N]));
1996
+ this.emitterUni = dev.createBuffer({
1997
+ size: 256,
1998
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
1999
+ });
2000
+ }
2001
+ // --- bind-group helper -----------------------------------------------------
2002
+ bg(pl, ...bufs) {
2003
+ return this.device.createBindGroup({
2004
+ layout: pl.getBindGroupLayout(0),
2005
+ entries: bufs.map((buffer, binding) => ({ binding, resource: { buffer } }))
2006
+ });
2007
+ }
2008
+ // --- encoding helpers (each creates its own compute pass for barriers) -----
2009
+ eCopy(enc, dst, src) {
2010
+ const p = enc.beginComputePass();
2011
+ p.setPipeline(this.copyPL);
2012
+ p.setBindGroup(0, this.bg(this.copyPL, dst, src));
2013
+ p.dispatchWorkgroups(WG1D);
2014
+ p.end();
2015
+ }
2016
+ eAddSrc(enc, dst, src) {
2017
+ const p = enc.beginComputePass();
2018
+ p.setPipeline(this.addSrcPL);
2019
+ p.setBindGroup(0, this.bg(this.addSrcPL, this.dtUni, dst, src));
2020
+ p.dispatchWorkgroups(WG1D);
2021
+ p.end();
2022
+ }
2023
+ eScale(enc, field, uni) {
2024
+ const p = enc.beginComputePass();
2025
+ p.setPipeline(this.scalePL);
2026
+ p.setBindGroup(0, this.bg(this.scalePL, uni, field));
2027
+ p.dispatchWorkgroups(WG1D);
2028
+ p.end();
2029
+ }
2030
+ eJacobi(enc, dst, x0, prev, uni) {
2031
+ const p = enc.beginComputePass();
2032
+ p.setPipeline(this.jacobiPL);
2033
+ p.setBindGroup(0, this.bg(this.jacobiPL, uni, dst, x0, prev));
2034
+ p.dispatchWorkgroups(WG2D, WG2D);
2035
+ p.end();
2036
+ }
2037
+ eBnd(enc, field, b) {
2038
+ const p = enc.beginComputePass();
2039
+ p.setPipeline(this.bndPL);
2040
+ p.setBindGroup(0, this.bg(this.bndPL, this.bndUni[b], field));
2041
+ p.dispatchWorkgroups(WG_BND);
2042
+ p.end();
2043
+ }
2044
+ eAdvect(enc, d, d0, u, v) {
2045
+ const p = enc.beginComputePass();
2046
+ p.setPipeline(this.advectPL);
2047
+ p.setBindGroup(0, this.bg(this.advectPL, this.advUni, d, d0, u, v));
2048
+ p.dispatchWorkgroups(WG2D, WG2D);
2049
+ p.end();
2050
+ }
2051
+ eDiv(enc) {
2052
+ const p = enc.beginComputePass();
2053
+ p.setPipeline(this.divPL);
2054
+ p.setBindGroup(0, this.bg(this.divPL, this.f.div, this.f.u, this.f.v));
2055
+ p.dispatchWorkgroups(WG2D, WG2D);
2056
+ p.end();
2057
+ }
2058
+ eGrad(enc) {
2059
+ const p = enc.beginComputePass();
2060
+ p.setPipeline(this.gradPL);
2061
+ p.setBindGroup(0, this.bg(this.gradPL, this.f.u, this.f.v, this.f.p));
2062
+ p.dispatchWorkgroups(WG2D, WG2D);
2063
+ p.end();
2064
+ }
2065
+ eInject(enc) {
2066
+ const p = enc.beginComputePass();
2067
+ p.setPipeline(this.injectPL);
2068
+ p.setBindGroup(
2069
+ 0,
2070
+ this.bg(this.injectPL, this.emitterUni, this.f.u0, this.f.v0, this.f.dr0, this.f.dg0, this.f.db0)
2071
+ );
2072
+ p.dispatchWorkgroups(WG_INJ, WG_INJ);
2073
+ p.end();
2074
+ }
2075
+ // --- composite steps -------------------------------------------------------
2076
+ encodeDiffuse(enc, field, uni, b) {
2077
+ this.eCopy(enc, this.f.sA, field);
2078
+ this.eCopy(enc, this.f.sB, this.f.sA);
2079
+ for (let k = 0; k < JACOBI_DIFF; k++) {
2080
+ if (k % 2 === 0) {
2081
+ this.eJacobi(enc, field, this.f.sA, this.f.sB, uni);
2082
+ } else {
2083
+ this.eJacobi(enc, this.f.sB, this.f.sA, field, uni);
2084
+ }
2085
+ }
2086
+ if (JACOBI_DIFF % 2 === 0) this.eCopy(enc, field, this.f.sB);
2087
+ this.eBnd(enc, field, b);
2088
+ }
2089
+ encodeProject(enc) {
2090
+ this.eDiv(enc);
2091
+ this.eBnd(enc, this.f.div, 0);
2092
+ this.eScale(enc, this.f.p, this.zeroUni);
2093
+ this.eScale(enc, this.f.sA, this.zeroUni);
2094
+ for (let k = 0; k < JACOBI_PRES; k++) {
2095
+ if (k % 2 === 0) {
2096
+ this.eJacobi(enc, this.f.sA, this.f.div, this.f.p, this.presUni);
2097
+ } else {
2098
+ this.eJacobi(enc, this.f.p, this.f.div, this.f.sA, this.presUni);
2099
+ }
2100
+ }
2101
+ if (JACOBI_PRES % 2 !== 0) this.eCopy(enc, this.f.p, this.f.sA);
2102
+ this.eBnd(enc, this.f.p, 0);
2103
+ this.eGrad(enc);
2104
+ this.eBnd(enc, this.f.u, 1);
2105
+ this.eBnd(enc, this.f.v, 2);
2106
+ }
2107
+ encodeVelStep(enc) {
2108
+ this.eAddSrc(enc, this.f.u, this.f.u0);
2109
+ this.eAddSrc(enc, this.f.v, this.f.v0);
2110
+ this.encodeDiffuse(enc, this.f.u, this.viscUni, 1);
2111
+ this.encodeDiffuse(enc, this.f.v, this.viscUni, 2);
2112
+ this.encodeProject(enc);
2113
+ this.eCopy(enc, this.f.sA, this.f.u);
2114
+ this.eCopy(enc, this.f.sB, this.f.v);
2115
+ this.eAdvect(enc, this.f.u, this.f.sA, this.f.sA, this.f.sB);
2116
+ this.eAdvect(enc, this.f.v, this.f.sB, this.f.sA, this.f.sB);
2117
+ this.eBnd(enc, this.f.u, 1);
2118
+ this.eBnd(enc, this.f.v, 2);
2119
+ this.encodeProject(enc);
2120
+ this.eScale(enc, this.f.u, this.velDecUni);
2121
+ this.eScale(enc, this.f.v, this.velDecUni);
2122
+ }
2123
+ encodeDensStep(enc, d, d0) {
2124
+ this.eAddSrc(enc, this.f[d], this.f[d0]);
2125
+ this.encodeDiffuse(enc, this.f[d], this.diffUni, 0);
2126
+ this.eCopy(enc, this.f.sA, this.f[d]);
2127
+ this.eAdvect(enc, this.f[d], this.f.sA, this.f.u, this.f.v);
2128
+ this.eBnd(enc, this.f[d], 0);
2129
+ this.eScale(enc, this.f[d], this.densDecUni);
2130
+ }
2131
+ // --- emitter upload --------------------------------------------------------
2132
+ writeEmitters() {
2133
+ const frames = stepEmitters(this.t, this.emitters, GPU_N);
2134
+ const buf = new ArrayBuffer(256);
2135
+ const fv = new Float32Array(buf);
2136
+ const uv = new Uint32Array(buf);
2137
+ fv[0] = GPU_INJECT_R;
2138
+ fv[1] = DYE_RATE;
2139
+ fv[2] = GPU_FORCE_RATE;
2140
+ uv[3] = frames.length;
2141
+ for (let i = 0; i < frames.length; i++) {
2142
+ const off = 4 + i * 8;
2143
+ const fr = frames[i];
2144
+ fv[off] = fr.gx;
2145
+ fv[off + 1] = fr.gy;
2146
+ fv[off + 2] = fr.dx;
2147
+ fv[off + 3] = fr.dy;
2148
+ fv[off + 4] = fr.r;
2149
+ fv[off + 5] = fr.g;
2150
+ fv[off + 6] = fr.b;
2151
+ fv[off + 7] = 0;
2152
+ }
2153
+ this.device.queue.writeBuffer(this.emitterUni, 0, buf);
2154
+ }
2155
+ // --- simulation step -------------------------------------------------------
2156
+ simStep() {
2157
+ this.writeEmitters();
2158
+ const enc = this.device.createCommandEncoder();
2159
+ this.eInject(enc);
2160
+ this.encodeVelStep(enc);
2161
+ this.encodeDensStep(enc, "dr", "dr0");
2162
+ this.encodeDensStep(enc, "dg", "dg0");
2163
+ this.encodeDensStep(enc, "db", "db0");
2164
+ this.device.queue.submit([enc.finish()]);
2165
+ }
2166
+ // --- render ----------------------------------------------------------------
2167
+ render() {
2168
+ if (this.lost) return;
2169
+ const tex = this.gpuCtx.getCurrentTexture();
2170
+ const enc = this.device.createCommandEncoder();
2171
+ const pass = enc.beginRenderPass({
2172
+ colorAttachments: [
2173
+ {
2174
+ view: tex.createView(),
2175
+ loadOp: "clear",
2176
+ storeOp: "store",
2177
+ clearValue: { r: 0, g: 0, b: 0, a: 1 }
2178
+ }
2179
+ ]
2180
+ });
2181
+ pass.setPipeline(this.renderPL);
2182
+ pass.setBindGroup(0, this.bg(this.renderPL, this.f.dr, this.f.dg, this.f.db));
2183
+ pass.draw(3);
2184
+ pass.end();
2185
+ this.device.queue.submit([enc.finish()]);
2186
+ }
2187
+ // --- animation loop --------------------------------------------------------
2188
+ start() {
2189
+ if (this.frameId !== null || typeof requestAnimationFrame === "undefined") return;
2190
+ this.loop();
2191
+ }
2192
+ stop() {
2193
+ if (this.frameId !== null) {
2194
+ cancelAnimationFrame(this.frameId);
2195
+ this.frameId = null;
2196
+ }
2197
+ }
2198
+ loop() {
2199
+ this.frameId = requestAnimationFrame(() => this.loop());
2200
+ this.t += DT;
2201
+ this.simStep();
2202
+ this.render();
2203
+ }
2204
+ renderStill() {
2205
+ for (let i = 0; i < 200; i++) {
2206
+ this.t += DT;
2207
+ this.simStep();
2208
+ }
2209
+ this.render();
2210
+ }
2211
+ hardReset() {
2212
+ if (this.lost) return;
2213
+ const enc = this.device.createCommandEncoder();
2214
+ for (const n of FIELDS) {
2215
+ this.eScale(enc, this.f[n], this.zeroUni);
2216
+ }
2217
+ this.device.queue.submit([enc.finish()]);
2218
+ this.t = 0;
2219
+ this.emitters = buildEmitters(this.rng, GPU_N);
2220
+ this.lastPreviewMs = -1;
2221
+ }
2222
+ previewAt(ms) {
2223
+ if (this.lost) return;
2224
+ this.stop();
2225
+ const frameMs = _FluidGPU.PREVIEW_FRAME_MS;
2226
+ if (ms < this.lastPreviewMs || this.lastPreviewMs < 0) {
2227
+ this.hardReset();
2228
+ }
2229
+ const fromFrame = Math.floor(Math.max(0, this.lastPreviewMs) / frameMs);
2230
+ const toFrame = Math.floor(ms / frameMs);
2231
+ if (toFrame === 0 && fromFrame === 0) {
2232
+ for (let i = 0; i < 200; i++) {
2233
+ this.t += DT;
2234
+ this.simStep();
2235
+ }
2236
+ } else {
2237
+ for (let f = fromFrame; f < toFrame; f++) {
2238
+ this.t += DT;
2239
+ this.simStep();
2240
+ }
2241
+ }
2242
+ this.lastPreviewMs = ms;
2243
+ this.render();
2244
+ }
2245
+ // --- SaverInstance interface ------------------------------------------------
2246
+ setPaused(paused) {
2247
+ if (paused) this.stop();
2248
+ else this.start();
2249
+ }
2250
+ resize(width, height, dpr) {
2251
+ this.w = width;
2252
+ this.h = height;
2253
+ if (dpr !== void 0) this.dpr = Math.min(dpr, 2);
2254
+ this.canvas.width = Math.max(1, Math.round(this.w * this.dpr));
2255
+ this.canvas.height = Math.max(1, Math.round(this.h * this.dpr));
2256
+ if (!this.lost) {
2257
+ this.gpuCtx.configure({
2258
+ device: this.device,
2259
+ format: this.format,
2260
+ alphaMode: "opaque"
2261
+ });
2262
+ this.render();
2263
+ }
2264
+ }
2265
+ dispose() {
2266
+ this.stop();
2267
+ for (const b of Object.values(this.f)) b.destroy();
2268
+ this.dtUni.destroy();
2269
+ this.diffUni.destroy();
2270
+ this.viscUni.destroy();
2271
+ this.presUni.destroy();
2272
+ this.densDecUni.destroy();
2273
+ this.velDecUni.destroy();
2274
+ this.zeroUni.destroy();
2275
+ for (const b of this.bndUni) b.destroy();
2276
+ this.advUni.destroy();
2277
+ this.emitterUni.destroy();
2278
+ this.canvas.remove();
2279
+ this.device.destroy();
2280
+ }
2281
+ };
2282
+
2283
+ // src/gpu-eligible.ts
2284
+ function gpuMainThreadEligible(ctx) {
2285
+ if (ctx.surface) return false;
2286
+ if (typeof window !== "undefined" && "__idleScreensMac" in window) return false;
2287
+ return true;
2288
+ }
2289
+
2290
+ // src/fluid.ts
2291
+ var fluidManifest = {
2292
+ id: "fluid",
2293
+ label: "Fluid",
2294
+ passthrough: false,
2295
+ minBackend: "canvas2d",
2296
+ costTier: "low",
2297
+ motionIntensity: "calm",
2298
+ reducedMotionFallback: "static",
2299
+ a11y: { flashSafe: true },
2300
+ workerReady: true
2301
+ };
2302
+ var N = 96;
2303
+ var SZ = (N + 2) * (N + 2);
2304
+ var DIFF = 3e-5;
2305
+ var VISC = 3e-4;
2306
+ var GS_ITER = 4;
2307
+ var INJECT_R = 4;
2308
+ function IX(i, j) {
2309
+ return i + (N + 2) * j;
2310
+ }
2311
+ var FluidCPU = class _FluidCPU {
2312
+ ctxSaver;
2313
+ canvas;
2314
+ gc;
2315
+ buf;
2316
+ bufCtx;
2317
+ img;
2318
+ w = 0;
2319
+ h = 0;
2320
+ u = new Float32Array(SZ);
2321
+ v = new Float32Array(SZ);
2322
+ u0 = new Float32Array(SZ);
2323
+ v0 = new Float32Array(SZ);
2324
+ dr = new Float32Array(SZ);
2325
+ dg = new Float32Array(SZ);
2326
+ db = new Float32Array(SZ);
2327
+ dr0 = new Float32Array(SZ);
2328
+ dg0 = new Float32Array(SZ);
2329
+ db0 = new Float32Array(SZ);
2330
+ tmp = new Float32Array(SZ);
2331
+ emitters;
2332
+ t = 0;
2333
+ frameId = null;
2334
+ paused = false;
2335
+ lastPreviewMs = -1;
2336
+ static PREVIEW_FRAME_MS = 1e3 / 60;
2337
+ constructor(ctx) {
2338
+ this.ctxSaver = ctx;
2339
+ this.w = ctx.width;
2340
+ this.h = ctx.height;
2341
+ let canvas;
2342
+ if (ctx.surface) {
2343
+ canvas = ctx.surface;
2344
+ } else {
2345
+ const el = document.createElement("canvas");
2346
+ el.style.cssText = "display:block;width:100%;height:100%";
2347
+ el.setAttribute("aria-hidden", "true");
2348
+ el.setAttribute("data-fluid-backend", "canvas2d");
2349
+ ctx.host.appendChild(el);
2350
+ canvas = el;
2351
+ }
2352
+ this.canvas = canvas;
2353
+ const gc = canvas.getContext("2d", { alpha: false });
2354
+ if (!gc) throw new Error("fluid: no 2d context");
2355
+ this.gc = gc;
2356
+ this.buf = typeof OffscreenCanvas !== "undefined" ? new OffscreenCanvas(N, N) : Object.assign(document.createElement("canvas"), { width: N, height: N });
2357
+ const bc = this.buf.getContext("2d");
2358
+ if (!bc) throw new Error("fluid: no buffer 2d context");
2359
+ this.bufCtx = bc;
2360
+ this.img = new ImageData(N, N);
2361
+ this.emitters = this.buildEmitters(ctx.rng);
2362
+ this.sizeCanvas();
2363
+ this.paused = ctx.reducedMotion;
2364
+ if (this.paused) {
2365
+ this.renderStill();
2366
+ } else {
2367
+ this.start();
2368
+ }
2369
+ }
2370
+ buildEmitters(rng) {
2371
+ const out = [];
2372
+ for (let i = 0; i < EMITTER_N; i++) {
2373
+ const cx = N / 2 + 1;
2374
+ out.push({
2375
+ hue: (i * 360 / EMITTER_N + rng.range(0, 40)) % 360,
2376
+ fx: rng.range(0.3, 0.8),
2377
+ fy: rng.range(0.3, 0.8),
2378
+ px: rng.range(0, Math.PI * 2),
2379
+ py: rng.range(0, Math.PI * 2),
2380
+ speed: rng.range(0.5, 1),
2381
+ prevGx: cx,
2382
+ prevGy: cx
2383
+ });
2384
+ }
2385
+ return out;
2386
+ }
2387
+ sizeCanvas() {
2388
+ const dpr = Math.min(this.ctxSaver.dpr, 2);
2389
+ this.canvas.width = Math.max(1, Math.round(this.w * dpr));
2390
+ this.canvas.height = Math.max(1, Math.round(this.h * dpr));
2391
+ this.gc.setTransform(dpr, 0, 0, dpr, 0, 0);
2392
+ }
2393
+ // --- Stam stable-fluids solver ---
2394
+ setBnd(b, x) {
2395
+ for (let i = 1; i <= N; i++) {
2396
+ x[IX(0, i)] = b === 1 ? -x[IX(1, i)] : x[IX(1, i)];
2397
+ x[IX(N + 1, i)] = b === 1 ? -x[IX(N, i)] : x[IX(N, i)];
2398
+ x[IX(i, 0)] = b === 2 ? -x[IX(i, 1)] : x[IX(i, 1)];
2399
+ x[IX(i, N + 1)] = b === 2 ? -x[IX(i, N)] : x[IX(i, N)];
2400
+ }
2401
+ x[IX(0, 0)] = 0.5 * (x[IX(1, 0)] + x[IX(0, 1)]);
2402
+ x[IX(0, N + 1)] = 0.5 * (x[IX(1, N + 1)] + x[IX(0, N)]);
2403
+ x[IX(N + 1, 0)] = 0.5 * (x[IX(N, 0)] + x[IX(N + 1, 1)]);
2404
+ x[IX(N + 1, N + 1)] = 0.5 * (x[IX(N, N + 1)] + x[IX(N + 1, N)]);
2405
+ }
2406
+ diffuse(b, x, x0, diff) {
2407
+ const a = DT * diff * N * N;
2408
+ if (a === 0) {
2409
+ x.set(x0);
2410
+ return;
2411
+ }
2412
+ const denom = 1 + 4 * a;
2413
+ for (let k = 0; k < GS_ITER; k++) {
2414
+ for (let j = 1; j <= N; j++) {
2415
+ for (let i = 1; i <= N; i++) {
2416
+ const idx = IX(i, j);
2417
+ x[idx] = (x0[idx] + a * (x[IX(i - 1, j)] + x[IX(i + 1, j)] + x[IX(i, j - 1)] + x[IX(i, j + 1)])) / denom;
2418
+ }
2419
+ }
2420
+ this.setBnd(b, x);
2421
+ }
2422
+ }
2423
+ advect(b, d, d0, u, v) {
2424
+ const dt0 = DT * N;
2425
+ for (let j = 1; j <= N; j++) {
2426
+ for (let i = 1; i <= N; i++) {
2427
+ const idx = IX(i, j);
2428
+ let x = i - dt0 * u[idx];
2429
+ let y = j - dt0 * v[idx];
2430
+ if (x < 0.5) x = 0.5;
2431
+ if (x > N + 0.5) x = N + 0.5;
2432
+ if (y < 0.5) y = 0.5;
2433
+ if (y > N + 0.5) y = N + 0.5;
2434
+ const i0 = Math.floor(x);
2435
+ const j0 = Math.floor(y);
2436
+ const s1 = x - i0;
2437
+ const s0 = 1 - s1;
2438
+ const t1 = y - j0;
2439
+ const t0 = 1 - t1;
2440
+ d[idx] = s0 * (t0 * d0[IX(i0, j0)] + t1 * d0[IX(i0, j0 + 1)]) + s1 * (t0 * d0[IX(i0 + 1, j0)] + t1 * d0[IX(i0 + 1, j0 + 1)]);
2441
+ }
2442
+ }
2443
+ this.setBnd(b, d);
2444
+ }
2445
+ project(u, v, p, div) {
2446
+ const h = 1 / N;
2447
+ for (let j = 1; j <= N; j++) {
2448
+ for (let i = 1; i <= N; i++) {
2449
+ const idx = IX(i, j);
2450
+ div[idx] = -0.5 * h * (u[IX(i + 1, j)] - u[IX(i - 1, j)] + v[IX(i, j + 1)] - v[IX(i, j - 1)]);
2451
+ p[idx] = 0;
2452
+ }
2453
+ }
2454
+ this.setBnd(0, div);
2455
+ this.setBnd(0, p);
2456
+ for (let k = 0; k < GS_ITER; k++) {
2457
+ for (let j = 1; j <= N; j++) {
2458
+ for (let i = 1; i <= N; i++) {
2459
+ const idx = IX(i, j);
2460
+ p[idx] = (div[idx] + p[IX(i - 1, j)] + p[IX(i + 1, j)] + p[IX(i, j - 1)] + p[IX(i, j + 1)]) / 4;
2461
+ }
2462
+ }
2463
+ this.setBnd(0, p);
2464
+ }
2465
+ for (let j = 1; j <= N; j++) {
2466
+ for (let i = 1; i <= N; i++) {
2467
+ const idx = IX(i, j);
2468
+ u[idx] -= 0.5 * N * (p[IX(i + 1, j)] - p[IX(i - 1, j)]);
2469
+ v[idx] -= 0.5 * N * (p[IX(i, j + 1)] - p[IX(i, j - 1)]);
2470
+ }
2471
+ }
2472
+ this.setBnd(1, u);
2473
+ this.setBnd(2, v);
2474
+ }
2475
+ velStep() {
2476
+ for (let i = 0; i < SZ; i++) {
2477
+ this.u[i] += DT * this.u0[i];
2478
+ this.v[i] += DT * this.v0[i];
2479
+ }
2480
+ [this.u, this.u0] = [this.u0, this.u];
2481
+ this.diffuse(1, this.u, this.u0, VISC);
2482
+ [this.v, this.v0] = [this.v0, this.v];
2483
+ this.diffuse(2, this.v, this.v0, VISC);
2484
+ this.project(this.u, this.v, this.u0, this.v0);
2485
+ [this.u, this.u0] = [this.u0, this.u];
2486
+ [this.v, this.v0] = [this.v0, this.v];
2487
+ this.advect(1, this.u, this.u0, this.u0, this.v0);
2488
+ this.advect(2, this.v, this.v0, this.u0, this.v0);
2489
+ this.project(this.u, this.v, this.u0, this.v0);
2490
+ for (let i = 0; i < SZ; i++) {
2491
+ this.u[i] *= VEL_DECAY;
2492
+ this.v[i] *= VEL_DECAY;
2493
+ }
2494
+ }
2495
+ densStep(d, d0) {
2496
+ for (let i = 0; i < SZ; i++) d[i] += DT * d0[i];
2497
+ this.tmp.set(d);
2498
+ this.diffuse(0, d, this.tmp, DIFF);
2499
+ this.tmp.set(d);
2500
+ this.advect(0, d, this.tmp, this.u, this.v);
2501
+ for (let i = 0; i < SZ; i++) d[i] *= DENS_DECAY;
2502
+ }
2503
+ // --- Source injection ---
2504
+ injectSources() {
2505
+ this.u0.fill(0);
2506
+ this.v0.fill(0);
2507
+ this.dr0.fill(0);
2508
+ this.dg0.fill(0);
2509
+ this.db0.fill(0);
2510
+ for (const e of this.emitters) {
2511
+ const st = this.t * e.speed;
2512
+ const gx = (Math.sin(st * e.fx + e.px) * 0.35 + 0.5) * N + 1;
2513
+ const gy = (Math.cos(st * e.fy + e.py) * 0.35 + 0.5) * N + 1;
2514
+ const dx = gx - e.prevGx;
2515
+ const dy = gy - e.prevGy;
2516
+ const ci = Math.round(gx);
2517
+ const cj = Math.round(gy);
2518
+ const [cr, cg, cb] = hue2rgb(e.hue + this.t * 15);
2519
+ for (let di = -INJECT_R; di <= INJECT_R; di++) {
2520
+ for (let dj = -INJECT_R; dj <= INJECT_R; dj++) {
2521
+ const ii = ci + di;
2522
+ const jj = cj + dj;
2523
+ if (ii < 1 || ii > N || jj < 1 || jj > N) continue;
2524
+ const dist = Math.sqrt(di * di + dj * dj);
2525
+ if (dist > INJECT_R) continue;
2526
+ const s = 1 - dist / INJECT_R;
2527
+ const idx = IX(ii, jj);
2528
+ this.dr0[idx] += cr * DYE_RATE * s;
2529
+ this.dg0[idx] += cg * DYE_RATE * s;
2530
+ this.db0[idx] += cb * DYE_RATE * s;
2531
+ this.u0[idx] += dx * FORCE_RATE * s;
2532
+ this.v0[idx] += dy * FORCE_RATE * s;
2533
+ }
2534
+ }
2535
+ e.prevGx = gx;
2536
+ e.prevGy = gy;
2537
+ }
2538
+ }
2539
+ // --- Simulation step ---
2540
+ simStep() {
2541
+ this.injectSources();
2542
+ this.velStep();
2543
+ this.densStep(this.dr, this.dr0);
2544
+ this.densStep(this.dg, this.dg0);
2545
+ this.densStep(this.db, this.db0);
2546
+ }
2547
+ // --- Rendering ---
2548
+ render() {
2549
+ const data = this.img.data;
2550
+ for (let j = 0; j < N; j++) {
2551
+ for (let i = 0; i < N; i++) {
2552
+ const si = IX(i + 1, j + 1);
2553
+ const pi = (j * N + i) * 4;
2554
+ data[pi] = Math.min(255, this.dr[si] | 0);
2555
+ data[pi + 1] = Math.min(255, this.dg[si] | 0);
2556
+ data[pi + 2] = Math.min(255, this.db[si] | 0);
2557
+ data[pi + 3] = 255;
2558
+ }
2559
+ }
2560
+ this.bufCtx.putImageData(this.img, 0, 0);
2561
+ this.gc.imageSmoothingEnabled = true;
2562
+ this.gc.drawImage(this.buf, 0, 0, this.w, this.h);
2563
+ }
2564
+ // --- Animation ---
2565
+ start() {
2566
+ if (this.frameId !== null || typeof requestAnimationFrame === "undefined") return;
2567
+ this.loop();
2568
+ }
2569
+ stop() {
2570
+ if (this.frameId !== null) {
2571
+ cancelAnimationFrame(this.frameId);
2572
+ this.frameId = null;
2573
+ }
2574
+ }
2575
+ loop() {
2576
+ this.frameId = requestAnimationFrame(() => this.loop());
2577
+ this.t += DT;
2578
+ this.simStep();
2579
+ this.render();
2580
+ }
2581
+ renderStill() {
2582
+ for (let i = 0; i < 200; i++) {
2583
+ this.t += DT;
2584
+ this.simStep();
2585
+ }
2586
+ this.render();
2587
+ }
2588
+ hardReset() {
2589
+ this.u.fill(0);
2590
+ this.v.fill(0);
2591
+ this.u0.fill(0);
2592
+ this.v0.fill(0);
2593
+ this.dr.fill(0);
2594
+ this.dg.fill(0);
2595
+ this.db.fill(0);
2596
+ this.dr0.fill(0);
2597
+ this.dg0.fill(0);
2598
+ this.db0.fill(0);
2599
+ this.t = 0;
2600
+ this.emitters = this.buildEmitters(this.ctxSaver.rng);
2601
+ this.lastPreviewMs = -1;
2602
+ }
2603
+ previewAt(ms) {
2604
+ this.stop();
2605
+ const frameMs = _FluidCPU.PREVIEW_FRAME_MS;
2606
+ if (ms < this.lastPreviewMs || this.lastPreviewMs < 0) {
2607
+ this.hardReset();
2608
+ }
2609
+ const fromFrame = Math.floor(Math.max(0, this.lastPreviewMs) / frameMs);
2610
+ const toFrame = Math.floor(ms / frameMs);
2611
+ if (toFrame === 0 && fromFrame === 0) {
2612
+ for (let i = 0; i < 200; i++) {
2613
+ this.t += DT;
2614
+ this.simStep();
2615
+ }
2616
+ } else {
2617
+ for (let f = fromFrame; f < toFrame; f++) {
2618
+ this.t += DT;
2619
+ this.simStep();
2620
+ }
2621
+ }
2622
+ this.lastPreviewMs = ms;
2623
+ this.render();
2624
+ }
2625
+ setPaused(paused) {
2626
+ this.paused = paused;
2627
+ if (paused) this.stop();
2628
+ else this.start();
2629
+ }
2630
+ resize(width, height, dpr) {
2631
+ this.w = width;
2632
+ this.h = height;
2633
+ if (dpr !== void 0) this.ctxSaver.dpr = dpr;
2634
+ this.sizeCanvas();
2635
+ this.render();
2636
+ }
2637
+ dispose() {
2638
+ this.stop();
2639
+ if (typeof HTMLCanvasElement !== "undefined" && this.canvas instanceof HTMLCanvasElement) this.canvas.remove();
2640
+ }
2641
+ };
2642
+ var fluid = {
2643
+ manifest: fluidManifest,
2644
+ async mount(ctx) {
2645
+ if (gpuMainThreadEligible(ctx)) {
2646
+ try {
2647
+ const adapter = await navigator.gpu?.requestAdapter();
2648
+ if (adapter) {
2649
+ try {
2650
+ const device = await adapter.requestDevice();
2651
+ return new FluidGPU(ctx, device);
2652
+ } catch {
2653
+ }
2654
+ }
2655
+ } catch {
2656
+ }
2657
+ }
2658
+ return new FluidCPU(ctx);
2659
+ }
2660
+ };
2661
+
2662
+ // src/reaction-diffusion-shared.ts
2663
+ var RD_DU = 0.16;
2664
+ var RD_DV = 0.08;
2665
+ var RD_F = 0.055;
2666
+ var RD_K = 0.062;
2667
+ var RD_DT = 1;
2668
+ var SEED_R = 5;
2669
+ var SEED_COUNT = 15;
2670
+ var RESEED_INTERVAL = 360;
2671
+ var RESEED_BATCH = 3;
2672
+ function generateSeeds(rng, N3, count) {
2673
+ const seeds = [];
2674
+ for (let i = 0; i < count; i++) {
2675
+ seeds.push({
2676
+ x: rng.int(SEED_R + 1, N3 - SEED_R - 2),
2677
+ y: rng.int(SEED_R + 1, N3 - SEED_R - 2)
2678
+ });
2679
+ }
2680
+ return seeds;
2681
+ }
2682
+ function applySeedsCPU(u, v, N3, seeds, r) {
2683
+ for (const s of seeds) {
2684
+ for (let di = -r; di <= r; di++) {
2685
+ for (let dj = -r; dj <= r; dj++) {
2686
+ if (di * di + dj * dj > r * r) continue;
2687
+ const x = (s.x + di + N3) % N3;
2688
+ const y = (s.y + dj + N3) % N3;
2689
+ const idx = x + y * N3;
2690
+ u[idx] = 0.5;
2691
+ v[idx] = 0.25;
2692
+ }
2693
+ }
2694
+ }
2695
+ }
2696
+ var LUT = buildColorLUT();
2697
+ function buildColorLUT() {
2698
+ const lut = new Uint8Array(256 * 3);
2699
+ for (let i = 0; i < 256; i++) {
2700
+ const t = i / 255;
2701
+ let r, g, b;
2702
+ if (t < 0.33) {
2703
+ const s = t / 0.33;
2704
+ r = 10 + s * 3;
2705
+ g = 10 + s * 67;
2706
+ b = 46 + s * 56;
2707
+ } else if (t < 0.66) {
2708
+ const s = (t - 0.33) / 0.33;
2709
+ r = 13 + s * 211;
2710
+ g = 77 + s * 35;
2711
+ b = 102 - s * 38;
2712
+ } else {
2713
+ const s = (t - 0.66) / 0.34;
2714
+ r = 224 + s * 16;
2715
+ g = 112 + s * 97;
2716
+ b = 64 + s * 96;
2717
+ }
2718
+ lut[i * 3] = Math.round(r);
2719
+ lut[i * 3 + 1] = Math.round(g);
2720
+ lut[i * 3 + 2] = Math.round(b);
2721
+ }
2722
+ return lut;
2723
+ }
2724
+ function colorV(v) {
2725
+ const i = Math.min(255, Math.max(0, v * 768 | 0));
2726
+ return [LUT[i * 3], LUT[i * 3 + 1], LUT[i * 3 + 2]];
2727
+ }
2728
+
2729
+ // src/reaction-diffusion-gpu.ts
2730
+ var GPU_N2 = 512;
2731
+ var GPU_SEED_R = Math.round(SEED_R * (GPU_N2 / 256));
2732
+ var GPU_STEPS = 32;
2733
+ var WG = 8;
2734
+ var SIM_WGSL = (
2735
+ /* wgsl */
2736
+ `
2737
+ override N: u32;
2738
+
2739
+ const Du = ${RD_DU};
2740
+ const Dv = ${RD_DV};
2741
+ const f = ${RD_F};
2742
+ const k = ${RD_K};
2743
+ const dt = ${RD_DT};
2744
+
2745
+ @group(0) @binding(0) var<storage, read> src: array<vec2f>;
2746
+ @group(0) @binding(1) var<storage, read_write> dst: array<vec2f>;
2747
+
2748
+ @compute @workgroup_size(${WG}, ${WG})
2749
+ fn main(@builtin(global_invocation_id) id: vec3u) {
2750
+ let x = id.x;
2751
+ let y = id.y;
2752
+ if (x >= N || y >= N) { return; }
2753
+
2754
+ let idx = x + y * N;
2755
+ let c = src[idx];
2756
+
2757
+ let lx = ((x + N - 1u) % N) + y * N;
2758
+ let rx = ((x + 1u) % N) + y * N;
2759
+ let uy = x + ((y + N - 1u) % N) * N;
2760
+ let dy = x + ((y + 1u) % N) * N;
2761
+
2762
+ let lap = src[lx] + src[rx] + src[uy] + src[dy] - 4.0 * c;
2763
+
2764
+ let uvv = c.x * c.y * c.y;
2765
+ let nu = c.x + dt * (Du * lap.x - uvv + f * (1.0 - c.x));
2766
+ let nv = c.y + dt * (Dv * lap.y + uvv - (f + k) * c.y);
2767
+
2768
+ dst[idx] = clamp(vec2f(nu, nv), vec2f(0.0), vec2f(1.0));
2769
+ }`
2770
+ );
2771
+ var RENDER_WGSL2 = (
2772
+ /* wgsl */
2773
+ `
2774
+ override N: u32;
2775
+
2776
+ @group(0) @binding(0) var<storage, read> field: array<vec2f>;
2777
+
2778
+ struct VOut {
2779
+ @builtin(position) pos: vec4f,
2780
+ @location(0) uv: vec2f,
2781
+ }
2782
+
2783
+ @vertex
2784
+ fn vs(@builtin(vertex_index) i: u32) -> VOut {
2785
+ var p = array<vec2f, 3>(vec2f(-1,-1), vec2f(3,-1), vec2f(-1,3));
2786
+ var o: VOut;
2787
+ o.pos = vec4f(p[i], 0, 1);
2788
+ o.uv = (p[i] + 1.0) * 0.5;
2789
+ o.uv.y = 1.0 - o.uv.y;
2790
+ return o;
2791
+ }
2792
+
2793
+ @fragment
2794
+ fn fs(v: VOut) -> @location(0) vec4f {
2795
+ let x = min(u32(v.uv.x * f32(N)), N - 1u);
2796
+ let y = min(u32(v.uv.y * f32(N)), N - 1u);
2797
+ let val = field[x + y * N];
2798
+
2799
+ let t = clamp(val.y * 3.0, 0.0, 1.0);
2800
+
2801
+ let c0 = vec3f(0.039, 0.039, 0.180);
2802
+ let c1 = vec3f(0.051, 0.302, 0.400);
2803
+ let c2 = vec3f(0.878, 0.439, 0.251);
2804
+ let c3 = vec3f(0.941, 0.820, 0.627);
2805
+
2806
+ var color: vec3f;
2807
+ if (t < 0.33) {
2808
+ color = mix(c0, c1, t / 0.33);
2809
+ } else if (t < 0.66) {
2810
+ color = mix(c1, c2, (t - 0.33) / 0.33);
2811
+ } else {
2812
+ color = mix(c2, c3, (t - 0.66) / 0.34);
2813
+ }
2814
+
2815
+ return vec4f(color, 1.0);
2816
+ }`
2817
+ );
2818
+ var ReactionDiffusionGPU = class {
2819
+ device;
2820
+ canvas;
2821
+ gpuCtx;
2822
+ format;
2823
+ dpr;
2824
+ w;
2825
+ h;
2826
+ bufA;
2827
+ bufB;
2828
+ simPL;
2829
+ renderPL;
2830
+ simBG_AB;
2831
+ simBG_BA;
2832
+ renderBG_A;
2833
+ renderBG_B;
2834
+ parity = 0;
2835
+ frameId = null;
2836
+ frameCount = 0;
2837
+ lost = false;
2838
+ rng;
2839
+ constructor(ctx, device) {
2840
+ this.device = device;
2841
+ this.w = ctx.width;
2842
+ this.h = ctx.height;
2843
+ this.dpr = Math.min(ctx.dpr, 2);
2844
+ this.rng = ctx.rng;
2845
+ this.canvas = document.createElement("canvas");
2846
+ this.canvas.style.cssText = "display:block;width:100%;height:100%";
2847
+ this.canvas.setAttribute("aria-hidden", "true");
2848
+ this.canvas.setAttribute("data-rd-backend", "webgpu");
2849
+ ctx.host.appendChild(this.canvas);
2850
+ this.sizeCanvas();
2851
+ this.gpuCtx = this.canvas.getContext("webgpu");
2852
+ this.format = navigator.gpu.getPreferredCanvasFormat();
2853
+ this.gpuCtx.configure({ device, format: this.format, alphaMode: "opaque" });
2854
+ const sz = GPU_N2 * GPU_N2 * 8;
2855
+ const usage = GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST;
2856
+ this.bufA = device.createBuffer({ size: sz, usage });
2857
+ this.bufB = device.createBuffer({ size: sz, usage });
2858
+ this.initField();
2859
+ const simLayout = device.createBindGroupLayout({
2860
+ entries: [
2861
+ { binding: 0, visibility: GPUShaderStage.COMPUTE, buffer: { type: "read-only-storage" } },
2862
+ { binding: 1, visibility: GPUShaderStage.COMPUTE, buffer: { type: "storage" } }
2863
+ ]
2864
+ });
2865
+ const simModule = device.createShaderModule({ code: SIM_WGSL });
2866
+ this.simPL = device.createComputePipeline({
2867
+ layout: device.createPipelineLayout({ bindGroupLayouts: [simLayout] }),
2868
+ compute: { module: simModule, entryPoint: "main", constants: { N: GPU_N2 } }
2869
+ });
2870
+ this.simBG_AB = device.createBindGroup({
2871
+ layout: simLayout,
2872
+ entries: [
2873
+ { binding: 0, resource: { buffer: this.bufA } },
2874
+ { binding: 1, resource: { buffer: this.bufB } }
2875
+ ]
2876
+ });
2877
+ this.simBG_BA = device.createBindGroup({
2878
+ layout: simLayout,
2879
+ entries: [
2880
+ { binding: 0, resource: { buffer: this.bufB } },
2881
+ { binding: 1, resource: { buffer: this.bufA } }
2882
+ ]
2883
+ });
2884
+ const renderLayout = device.createBindGroupLayout({
2885
+ entries: [
2886
+ { binding: 0, visibility: GPUShaderStage.FRAGMENT, buffer: { type: "read-only-storage" } }
2887
+ ]
2888
+ });
2889
+ const renderModule = device.createShaderModule({ code: RENDER_WGSL2 });
2890
+ this.renderPL = device.createRenderPipeline({
2891
+ layout: device.createPipelineLayout({ bindGroupLayouts: [renderLayout] }),
2892
+ vertex: { module: renderModule, entryPoint: "vs" },
2893
+ fragment: {
2894
+ module: renderModule,
2895
+ entryPoint: "fs",
2896
+ targets: [{ format: this.format }],
2897
+ constants: { N: GPU_N2 }
2898
+ }
2899
+ });
2900
+ this.renderBG_A = device.createBindGroup({
2901
+ layout: renderLayout,
2902
+ entries: [{ binding: 0, resource: { buffer: this.bufA } }]
2903
+ });
2904
+ this.renderBG_B = device.createBindGroup({
2905
+ layout: renderLayout,
2906
+ entries: [{ binding: 0, resource: { buffer: this.bufB } }]
2907
+ });
2908
+ device.lost.then(() => {
2909
+ this.lost = true;
2910
+ this.stop();
2911
+ });
2912
+ if (ctx.reducedMotion) {
2913
+ this.renderStill();
2914
+ } else {
2915
+ this.start();
2916
+ }
2917
+ }
2918
+ initField() {
2919
+ const data = new Float32Array(GPU_N2 * GPU_N2 * 2);
2920
+ for (let i = 0; i < GPU_N2 * GPU_N2; i++) {
2921
+ data[i * 2] = 1;
2922
+ data[i * 2 + 1] = 0;
2923
+ }
2924
+ this.seedInto(data, SEED_COUNT);
2925
+ this.device.queue.writeBuffer(this.bufA, 0, data);
2926
+ this.device.queue.writeBuffer(this.bufB, 0, data);
2927
+ this.parity = 0;
2928
+ }
2929
+ seedInto(data, count) {
2930
+ const seeds = generateSeeds(this.rng, GPU_N2, count);
2931
+ for (const s of seeds) {
2932
+ for (let di = -GPU_SEED_R; di <= GPU_SEED_R; di++) {
2933
+ for (let dj = -GPU_SEED_R; dj <= GPU_SEED_R; dj++) {
2934
+ if (di * di + dj * dj > GPU_SEED_R * GPU_SEED_R) continue;
2935
+ const x = (s.x + di + GPU_N2) % GPU_N2;
2936
+ const y = (s.y + dj + GPU_N2) % GPU_N2;
2937
+ const idx = (x + y * GPU_N2) * 2;
2938
+ data[idx] = 0.5;
2939
+ data[idx + 1] = 0.25;
2940
+ }
2941
+ }
2942
+ }
2943
+ }
2944
+ reseed() {
2945
+ const seeds = generateSeeds(this.rng, GPU_N2, RESEED_BATCH);
2946
+ const buf = this.parity === 0 ? this.bufA : this.bufB;
2947
+ const cell = new Float32Array([0.5, 0.25]);
2948
+ for (const s of seeds) {
2949
+ for (let di = -GPU_SEED_R; di <= GPU_SEED_R; di++) {
2950
+ for (let dj = -GPU_SEED_R; dj <= GPU_SEED_R; dj++) {
2951
+ if (di * di + dj * dj > GPU_SEED_R * GPU_SEED_R) continue;
2952
+ const x = (s.x + di + GPU_N2) % GPU_N2;
2953
+ const y = (s.y + dj + GPU_N2) % GPU_N2;
2954
+ this.device.queue.writeBuffer(buf, (x + y * GPU_N2) * 8, cell);
2955
+ }
2956
+ }
2957
+ }
2958
+ }
2959
+ sizeCanvas() {
2960
+ this.canvas.width = Math.max(1, Math.round(this.w * this.dpr));
2961
+ this.canvas.height = Math.max(1, Math.round(this.h * this.dpr));
2962
+ }
2963
+ simStep() {
2964
+ if (this.lost) return;
2965
+ const wgCount = Math.ceil(GPU_N2 / WG);
2966
+ const enc = this.device.createCommandEncoder();
2967
+ for (let i = 0; i < GPU_STEPS; i++) {
2968
+ const bg = this.parity === 0 ? this.simBG_AB : this.simBG_BA;
2969
+ const pass = enc.beginComputePass();
2970
+ pass.setPipeline(this.simPL);
2971
+ pass.setBindGroup(0, bg);
2972
+ pass.dispatchWorkgroups(wgCount, wgCount);
2973
+ pass.end();
2974
+ this.parity ^= 1;
2975
+ }
2976
+ this.device.queue.submit([enc.finish()]);
2977
+ }
2978
+ render() {
2979
+ if (this.lost) return;
2980
+ const tex = this.gpuCtx.getCurrentTexture();
2981
+ const enc = this.device.createCommandEncoder();
2982
+ const pass = enc.beginRenderPass({
2983
+ colorAttachments: [{
2984
+ view: tex.createView(),
2985
+ loadOp: "clear",
2986
+ storeOp: "store",
2987
+ clearValue: { r: 0, g: 0, b: 0, a: 1 }
2988
+ }]
2989
+ });
2990
+ pass.setPipeline(this.renderPL);
2991
+ pass.setBindGroup(0, this.parity === 0 ? this.renderBG_A : this.renderBG_B);
2992
+ pass.draw(3);
2993
+ pass.end();
2994
+ this.device.queue.submit([enc.finish()]);
2995
+ }
2996
+ start() {
2997
+ if (this.frameId !== null || typeof requestAnimationFrame === "undefined") return;
2998
+ this.loop();
2999
+ }
3000
+ stop() {
3001
+ if (this.frameId !== null) {
3002
+ cancelAnimationFrame(this.frameId);
3003
+ this.frameId = null;
3004
+ }
3005
+ }
3006
+ loop() {
3007
+ this.frameId = requestAnimationFrame(() => this.loop());
3008
+ this.frameCount++;
3009
+ if (this.frameCount % RESEED_INTERVAL === 0) this.reseed();
3010
+ this.simStep();
3011
+ this.render();
3012
+ }
3013
+ renderStill() {
3014
+ for (let i = 0; i < 200; i++) {
3015
+ this.simStep();
3016
+ }
3017
+ this.render();
3018
+ }
3019
+ setPaused(paused) {
3020
+ if (paused) this.stop();
3021
+ else this.start();
3022
+ }
3023
+ resize(width, height, dpr) {
3024
+ this.w = width;
3025
+ this.h = height;
3026
+ if (dpr !== void 0) this.dpr = Math.min(dpr, 2);
3027
+ this.sizeCanvas();
3028
+ if (!this.lost) {
3029
+ this.gpuCtx.configure({
3030
+ device: this.device,
3031
+ format: this.format,
3032
+ alphaMode: "opaque"
3033
+ });
3034
+ this.render();
3035
+ }
3036
+ }
3037
+ dispose() {
3038
+ this.stop();
3039
+ this.bufA.destroy();
3040
+ this.bufB.destroy();
3041
+ this.canvas.remove();
3042
+ this.device.destroy();
3043
+ }
3044
+ };
3045
+
3046
+ // src/reaction-diffusion.ts
3047
+ var reactionDiffusionManifest = {
3048
+ id: "reaction-diffusion",
3049
+ label: "Reaction Diffusion",
3050
+ passthrough: false,
3051
+ minBackend: "canvas2d",
3052
+ costTier: "low",
3053
+ motionIntensity: "calm",
3054
+ reducedMotionFallback: "static",
3055
+ a11y: { flashSafe: true },
3056
+ workerReady: true
3057
+ };
3058
+ var N2 = 256;
3059
+ var STEPS = 32;
3060
+ var ReactionDiffusionCPU = class {
3061
+ ctxSaver;
3062
+ canvas;
3063
+ gc;
3064
+ buf;
3065
+ bufCtx;
3066
+ img;
3067
+ w;
3068
+ h;
3069
+ u = new Float32Array(N2 * N2);
3070
+ v = new Float32Array(N2 * N2);
3071
+ uN = new Float32Array(N2 * N2);
3072
+ vN = new Float32Array(N2 * N2);
3073
+ rng;
3074
+ frameId = null;
3075
+ frameCount = 0;
3076
+ constructor(ctx) {
3077
+ this.ctxSaver = ctx;
3078
+ this.w = ctx.width;
3079
+ this.h = ctx.height;
3080
+ this.rng = ctx.rng;
3081
+ let canvas;
3082
+ if (ctx.surface) {
3083
+ canvas = ctx.surface;
3084
+ } else {
3085
+ const el = document.createElement("canvas");
3086
+ el.style.cssText = "display:block;width:100%;height:100%";
3087
+ el.setAttribute("aria-hidden", "true");
3088
+ el.setAttribute("data-rd-backend", "canvas2d");
3089
+ ctx.host.appendChild(el);
3090
+ canvas = el;
3091
+ }
3092
+ this.canvas = canvas;
3093
+ const gc = canvas.getContext("2d", { alpha: false });
3094
+ if (!gc) throw new Error("reaction-diffusion: no 2d context");
3095
+ this.gc = gc;
3096
+ this.buf = typeof OffscreenCanvas !== "undefined" ? new OffscreenCanvas(N2, N2) : Object.assign(document.createElement("canvas"), { width: N2, height: N2 });
3097
+ const bc = this.buf.getContext("2d");
3098
+ if (!bc) throw new Error("reaction-diffusion: no buffer 2d context");
3099
+ this.bufCtx = bc;
3100
+ this.img = new ImageData(N2, N2);
3101
+ this.u.fill(1);
3102
+ applySeedsCPU(this.u, this.v, N2, generateSeeds(this.rng, N2, SEED_COUNT), SEED_R);
3103
+ this.sizeCanvas();
3104
+ if (ctx.reducedMotion) {
3105
+ this.renderStill();
3106
+ } else {
3107
+ this.start();
3108
+ }
3109
+ }
3110
+ sizeCanvas() {
3111
+ const dpr = Math.min(this.ctxSaver.dpr, 2);
3112
+ this.canvas.width = Math.max(1, Math.round(this.w * dpr));
3113
+ this.canvas.height = Math.max(1, Math.round(this.h * dpr));
3114
+ this.gc.setTransform(dpr, 0, 0, dpr, 0, 0);
3115
+ }
3116
+ simStep() {
3117
+ const { u, v, uN, vN } = this;
3118
+ for (let y = 0; y < N2; y++) {
3119
+ for (let x = 0; x < N2; x++) {
3120
+ const idx = x + y * N2;
3121
+ const lx = (x - 1 + N2) % N2 + y * N2;
3122
+ const rx = (x + 1) % N2 + y * N2;
3123
+ const uy = x + (y - 1 + N2) % N2 * N2;
3124
+ const dy = x + (y + 1) % N2 * N2;
3125
+ const lapU = u[lx] + u[rx] + u[uy] + u[dy] - 4 * u[idx];
3126
+ const lapV = v[lx] + v[rx] + v[uy] + v[dy] - 4 * v[idx];
3127
+ const uvv = u[idx] * v[idx] * v[idx];
3128
+ uN[idx] = Math.max(0, Math.min(
3129
+ 1,
3130
+ u[idx] + RD_DT * (RD_DU * lapU - uvv + RD_F * (1 - u[idx]))
3131
+ ));
3132
+ vN[idx] = Math.max(0, Math.min(
3133
+ 1,
3134
+ v[idx] + RD_DT * (RD_DV * lapV + uvv - (RD_F + RD_K) * v[idx])
3135
+ ));
3136
+ }
3137
+ }
3138
+ [this.u, this.uN] = [this.uN, this.u];
3139
+ [this.v, this.vN] = [this.vN, this.v];
3140
+ }
3141
+ render() {
3142
+ const data = this.img.data;
3143
+ for (let i = 0; i < N2 * N2; i++) {
3144
+ const [r, g, b] = colorV(this.v[i]);
3145
+ const pi = i * 4;
3146
+ data[pi] = r;
3147
+ data[pi + 1] = g;
3148
+ data[pi + 2] = b;
3149
+ data[pi + 3] = 255;
3150
+ }
3151
+ this.bufCtx.putImageData(this.img, 0, 0);
3152
+ this.gc.imageSmoothingEnabled = true;
3153
+ this.gc.drawImage(this.buf, 0, 0, this.w, this.h);
3154
+ }
3155
+ start() {
3156
+ if (this.frameId !== null || typeof requestAnimationFrame === "undefined") return;
3157
+ this.loop();
3158
+ }
3159
+ stop() {
3160
+ if (this.frameId !== null) {
3161
+ cancelAnimationFrame(this.frameId);
3162
+ this.frameId = null;
3163
+ }
3164
+ }
3165
+ loop() {
3166
+ this.frameId = requestAnimationFrame(() => this.loop());
3167
+ this.frameCount++;
3168
+ if (this.frameCount % RESEED_INTERVAL === 0) {
3169
+ applySeedsCPU(
3170
+ this.u,
3171
+ this.v,
3172
+ N2,
3173
+ generateSeeds(this.rng, N2, RESEED_BATCH),
3174
+ SEED_R
3175
+ );
3176
+ }
3177
+ for (let i = 0; i < STEPS; i++) this.simStep();
3178
+ this.render();
3179
+ }
3180
+ renderStill() {
3181
+ for (let i = 0; i < 200; i++) this.simStep();
3182
+ this.render();
3183
+ }
3184
+ setPaused(paused) {
3185
+ if (paused) this.stop();
3186
+ else this.start();
3187
+ }
3188
+ resize(width, height, dpr) {
3189
+ this.w = width;
3190
+ this.h = height;
3191
+ if (dpr !== void 0) this.ctxSaver.dpr = dpr;
3192
+ this.sizeCanvas();
3193
+ this.render();
3194
+ }
3195
+ dispose() {
3196
+ this.stop();
3197
+ if (typeof HTMLCanvasElement !== "undefined" && this.canvas instanceof HTMLCanvasElement) this.canvas.remove();
3198
+ }
3199
+ };
3200
+ var reactionDiffusion = {
3201
+ manifest: reactionDiffusionManifest,
3202
+ async mount(ctx) {
3203
+ if (gpuMainThreadEligible(ctx)) {
3204
+ try {
3205
+ const adapter = await navigator.gpu?.requestAdapter();
3206
+ if (adapter) {
3207
+ try {
3208
+ const device = await adapter.requestDevice();
3209
+ return new ReactionDiffusionGPU(ctx, device);
3210
+ } catch {
3211
+ }
3212
+ }
3213
+ } catch {
3214
+ }
3215
+ }
3216
+ return new ReactionDiffusionCPU(ctx);
3217
+ }
3218
+ };
3219
+
3220
+ export {
3221
+ warp,
3222
+ hardRain,
3223
+ rainstorm,
3224
+ globe,
3225
+ spotlight,
3226
+ mystify,
3227
+ pipes,
3228
+ flurry,
3229
+ fluid,
3230
+ reactionDiffusion
3231
+ };
3232
+ //# sourceMappingURL=chunk-MIVXP2IL.js.map