festive-fx 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2849 @@
1
+ 'use strict';
2
+
3
+ // src/util.ts
4
+ var TAU = Math.PI * 2;
5
+ var rand = (min, max) => min + Math.random() * (max - min);
6
+ var pick = (items) => items[Math.random() * items.length | 0];
7
+ var drag = (factor, dt) => Math.pow(factor, dt * 60);
8
+ var countFor = (w, h, base, density) => {
9
+ const scaled = base * (w * h / (1280 * 720)) * Math.max(0, density);
10
+ return Math.max(0, Math.min(Math.round(scaled), base * 4));
11
+ };
12
+ var fillPool = (pool, n, make) => {
13
+ while (pool.length < n) pool.push(make());
14
+ pool.length = n;
15
+ };
16
+ var SPRITE_SCALE = 2;
17
+ var sprite = (width, height, paint) => {
18
+ if (typeof document === "undefined") return null;
19
+ const c = document.createElement("canvas");
20
+ c.width = Math.max(1, Math.ceil(width * SPRITE_SCALE));
21
+ c.height = Math.max(1, Math.ceil(height * SPRITE_SCALE));
22
+ let ctx = null;
23
+ try {
24
+ ctx = c.getContext("2d");
25
+ } catch {
26
+ ctx = null;
27
+ }
28
+ if (ctx) {
29
+ ctx.scale(SPRITE_SCALE, SPRITE_SCALE);
30
+ paint(ctx);
31
+ }
32
+ return c;
33
+ };
34
+ var spriteCache = (make) => {
35
+ const cache = /* @__PURE__ */ new Map();
36
+ return (key) => {
37
+ let s = cache.get(key);
38
+ if (s === void 0) {
39
+ s = make(key);
40
+ cache.set(key, s);
41
+ }
42
+ return s;
43
+ };
44
+ };
45
+ var glowSprite = (color, hot = true, falloff = 0.45) => sprite(32, 32, (ctx) => {
46
+ const g = ctx.createRadialGradient(16, 16, 0, 16, 16, 16);
47
+ g.addColorStop(0, "rgba(255,255,255,1)");
48
+ g.addColorStop(falloff, "rgba(255,255,255,0.5)");
49
+ g.addColorStop(1, "rgba(255,255,255,0)");
50
+ ctx.fillStyle = g;
51
+ ctx.fillRect(0, 0, 32, 32);
52
+ ctx.globalCompositeOperation = "source-in";
53
+ ctx.fillStyle = color;
54
+ ctx.fillRect(0, 0, 32, 32);
55
+ if (hot) {
56
+ ctx.globalCompositeOperation = "source-over";
57
+ const c = ctx.createRadialGradient(16, 16, 0, 16, 16, 5);
58
+ c.addColorStop(0, "rgba(255,255,255,1)");
59
+ c.addColorStop(1, "rgba(255,255,255,0)");
60
+ ctx.fillStyle = c;
61
+ ctx.fillRect(0, 0, 32, 32);
62
+ }
63
+ });
64
+ var stamp = (ctx, img, x, y, d) => {
65
+ if (img) ctx.drawImage(img, x - d / 2, y - d / 2, d, d);
66
+ };
67
+ var gust = (t, seed = 0) => Math.sin(t * 0.21 + seed) * 0.55 + Math.sin(t * 0.57 + seed * 2.3) * 0.3 + Math.sin(t * 1.43 + seed * 4.1) * 0.15;
68
+ var clamp = (v, min, max) => v < min ? min : v > max ? max : v;
69
+ var paletteOr = (colors, fallback) => colors && colors.length ? colors : fallback;
70
+ var retain = (items, keep) => {
71
+ let j = 0;
72
+ for (let i = 0; i < items.length; i++) {
73
+ const it = items[i];
74
+ if (keep(it)) items[j++] = it;
75
+ }
76
+ items.length = j;
77
+ };
78
+ var polyline = (ctx, pts) => {
79
+ ctx.beginPath();
80
+ ctx.moveTo(pts[0], pts[1]);
81
+ for (let i = 2; i < pts.length; i += 2) ctx.lineTo(pts[i], pts[i + 1]);
82
+ ctx.stroke();
83
+ };
84
+ var place = (ctx, m, x, y, rot, sx = 1, sy = 1) => {
85
+ const cos = Math.cos(rot);
86
+ const sin = Math.sin(rot);
87
+ const la = cos * sx;
88
+ const lb = sin * sx;
89
+ const lc = -sin * sy;
90
+ const ld = cos * sy;
91
+ ctx.setTransform(
92
+ m.a * la + m.c * lb,
93
+ m.b * la + m.d * lb,
94
+ m.a * lc + m.c * ld,
95
+ m.b * lc + m.d * ld,
96
+ m.a * x + m.c * y + m.e,
97
+ m.b * x + m.d * y + m.f
98
+ );
99
+ };
100
+ var placeFlat = (ctx, m, x, y, rot, sx, sy) => {
101
+ const cos = Math.cos(rot);
102
+ const sin = Math.sin(rot);
103
+ const la = sx * cos;
104
+ const lb = sy * sin;
105
+ const lc = -sx * sin;
106
+ const ld = sy * cos;
107
+ ctx.setTransform(
108
+ m.a * la + m.c * lb,
109
+ m.b * la + m.d * lb,
110
+ m.a * lc + m.c * ld,
111
+ m.b * lc + m.d * ld,
112
+ m.a * x + m.c * y + m.e,
113
+ m.b * x + m.d * y + m.f
114
+ );
115
+ };
116
+ var baseTransform = (ctx) => {
117
+ var _a;
118
+ const t = (_a = ctx.getTransform) == null ? void 0 : _a.call(ctx);
119
+ return t && Number.isFinite(t.a) ? t : { a: 1, b: 0, c: 0, d: 1, e: 0, f: 0 };
120
+ };
121
+ var resetTransform = (ctx, m) => {
122
+ ctx.setTransform(m.a, m.b, m.c, m.d, m.e, m.f);
123
+ };
124
+ var ALPHA_STEPS = 6;
125
+ var alphaLevel = (a) => clamp(Math.ceil(a * ALPHA_STEPS), 0, ALPHA_STEPS);
126
+ var parser;
127
+ var rgbCache = /* @__PURE__ */ new Map();
128
+ var toRgb = (color) => {
129
+ let rgb = rgbCache.get(color);
130
+ if (rgb !== void 0) return rgb;
131
+ if (parser === void 0) {
132
+ try {
133
+ parser = typeof document !== "undefined" ? document.createElement("canvas").getContext("2d") : null;
134
+ } catch {
135
+ parser = null;
136
+ }
137
+ }
138
+ rgb = null;
139
+ let s = color;
140
+ if (parser) {
141
+ parser.fillStyle = "#000";
142
+ parser.fillStyle = color;
143
+ if (typeof parser.fillStyle === "string") s = parser.fillStyle;
144
+ }
145
+ const hex = /^#([0-9a-f]{6})$/i.exec(s);
146
+ if (hex) {
147
+ const n = parseInt(hex[1], 16);
148
+ rgb = [n >> 16, n >> 8 & 255, n & 255];
149
+ } else {
150
+ const m = /rgba?\(\s*([\d.]+)[\s,]+([\d.]+)[\s,]+([\d.]+)/i.exec(s);
151
+ if (m) rgb = [+m[1], +m[2], +m[3]];
152
+ }
153
+ rgbCache.set(color, rgb);
154
+ return rgb;
155
+ };
156
+ var shadeCache = /* @__PURE__ */ new Map();
157
+ var shade = (color, t) => {
158
+ const q = Math.round(clamp(t, -1, 1) * 10);
159
+ if (q === 0) return color;
160
+ const key = `${color}|${q}`;
161
+ let out = shadeCache.get(key);
162
+ if (out === void 0) {
163
+ const rgb = toRgb(color);
164
+ if (!rgb) out = color;
165
+ else {
166
+ const k = Math.abs(q) / 10;
167
+ const to = q > 0 ? 0 : 255;
168
+ out = `rgb(${rgb.map((c) => Math.round(c + (to - c) * k)).join(",")})`;
169
+ }
170
+ shadeCache.set(key, out);
171
+ }
172
+ return out;
173
+ };
174
+
175
+ // src/effects/confetti.ts
176
+ var PALETTE = ["#ff595e", "#ffca3a", "#8ac926", "#1982c4", "#6a4c93", "#ff924c", "#ff70a6", "#4cc9f0"];
177
+ var FOIL = ["#d4af37", "#e6e8ef", "#c9a227", "#f1d78a"];
178
+ var MAX_BURST = 1500;
179
+ var SHAPES = ["rect", "rect", "rect", "rect", "square", "square", "circle", "triangle", "star", "star", "rect", "streamer"];
180
+ var addShape = (ctx, p, f) => {
181
+ const cos = Math.cos(p.rot);
182
+ const sin = Math.sin(p.rot);
183
+ const X = (lx, ly) => p.x + cos * lx - sin * ly * f;
184
+ const Y = (lx, ly) => p.y + sin * lx + cos * ly * f;
185
+ const poly = (pts) => {
186
+ ctx.moveTo(X(pts[0], pts[1]), Y(pts[0], pts[1]));
187
+ for (let i = 2; i < pts.length; i += 2) ctx.lineTo(X(pts[i], pts[i + 1]), Y(pts[i], pts[i + 1]));
188
+ ctx.closePath();
189
+ };
190
+ const { w, h } = p;
191
+ switch (p.shape) {
192
+ case "circle": {
193
+ const r = h * 0.6;
194
+ ctx.moveTo(p.x + cos * r, p.y + sin * r);
195
+ ctx.ellipse(p.x, p.y, r, Math.max(0.01, r * Math.abs(f)), p.rot, 0, TAU);
196
+ break;
197
+ }
198
+ case "triangle":
199
+ poly([0, -h * 0.8, w * 0.55, h * 0.6, -w * 0.55, h * 0.6]);
200
+ break;
201
+ case "star":
202
+ poly(STAR.map((v) => v * w));
203
+ break;
204
+ case "square":
205
+ poly([-w / 2, -w / 2, w / 2, -w / 2, w / 2, w / 2, -w / 2, w / 2]);
206
+ break;
207
+ default:
208
+ poly([-w / 2, -h / 2, w / 2, -h / 2, w / 2, h / 2, -w / 2, h / 2]);
209
+ }
210
+ };
211
+ var STAR = [];
212
+ for (let i = 0; i < 10; i++) {
213
+ const r = i % 2 ? 0.27 : 0.6;
214
+ const a = i * Math.PI / 5 - Math.PI / 2;
215
+ STAR.push(Math.cos(a) * r, Math.sin(a) * r);
216
+ }
217
+ var confetti = (o) => {
218
+ let w = 0;
219
+ let h = 0;
220
+ const pieces = [];
221
+ const fills = /* @__PURE__ */ new Map();
222
+ const palette = () => paletteOr(o.colors, PALETTE);
223
+ const make = (x, y, vx, vy, recycle) => {
224
+ const size = rand(5, 10) * o.size;
225
+ const shape = pick(SHAPES);
226
+ const foil = !o.colors && Math.random() < 0.12;
227
+ return {
228
+ x,
229
+ y,
230
+ vx,
231
+ vy,
232
+ w: shape === "streamer" ? size * rand(2.2, 3.2) : size,
233
+ h: shape === "streamer" ? rand(2.2, 3) * o.size : size * rand(0.45, 0.7),
234
+ rot: rand(0, TAU),
235
+ vrot: rand(-5, 5),
236
+ tilt: rand(0, TAU),
237
+ vtilt: rand(4, 11),
238
+ wave: rand(0, TAU),
239
+ color: foil ? pick(FOIL) : pick(palette()),
240
+ shape,
241
+ foil,
242
+ recycle,
243
+ f: 1
244
+ };
245
+ };
246
+ const fromTop = (anywhere) => make(rand(0, w), anywhere ? rand(-h, h) : rand(-60, -10), 0, rand(55, 115), true);
247
+ const sync = () => {
248
+ const want = countFor(w, h, 120, o.density);
249
+ let have = 0;
250
+ for (const p of pieces) if (p.recycle) have++;
251
+ for (; have < want; have++) pieces.push(fromTop(true));
252
+ if (have > want) {
253
+ let drop = have - want;
254
+ retain(pieces, (p) => !p.recycle || drop-- <= 0);
255
+ }
256
+ };
257
+ return {
258
+ resize(nw, nh) {
259
+ w = nw;
260
+ h = nh;
261
+ sync();
262
+ },
263
+ update(dt) {
264
+ const s = dt * o.speed;
265
+ const k = drag(0.985, s);
266
+ retain(pieces, (p) => {
267
+ p.tilt += p.vtilt * s;
268
+ p.rot += p.vrot * s * (p.shape === "streamer" ? 0.4 : 1);
269
+ p.wave += 7 * s;
270
+ const face = Math.abs(Math.cos(p.tilt));
271
+ if (p.recycle) {
272
+ p.x += (Math.sin(p.tilt) * 28 + o.wind * 80) * s;
273
+ p.y += p.vy * (0.65 + 0.35 * (1 - face)) * s;
274
+ } else {
275
+ p.vx = p.vx * k + o.wind * 60 * s;
276
+ p.vy = p.vy * k + 500 * s;
277
+ if (p.vy > 150) p.vy = 150;
278
+ p.x += (p.vx + Math.sin(p.tilt) * 22) * s;
279
+ p.y += p.vy * s;
280
+ }
281
+ if (p.y > h + 30) {
282
+ if (!p.recycle) return false;
283
+ Object.assign(p, fromTop(false));
284
+ } else if (p.recycle) {
285
+ if (p.x < -30) p.x += w + 60;
286
+ else if (p.x > w + 30) p.x -= w + 60;
287
+ }
288
+ return true;
289
+ });
290
+ },
291
+ draw(ctx) {
292
+ ctx.globalAlpha = 1;
293
+ for (const p of pieces) {
294
+ if (p.shape === "streamer") continue;
295
+ const f = Math.cos(p.tilt);
296
+ const af = Math.abs(f);
297
+ const glint = af > 0.88 ? (af - 0.88) * 8 * (p.foil ? 0.75 : 0.3) : 0;
298
+ const dark = f < 0 ? 0.28 + 0.2 * (1 - af) : 0.3 * (1 - af);
299
+ p.f = f;
300
+ const color = shade(p.color, glint > 0 ? -glint : dark);
301
+ let list = fills.get(color);
302
+ if (!list) fills.set(color, list = []);
303
+ list.push(p);
304
+ }
305
+ fills.forEach((list, color) => {
306
+ if (!list.length) return;
307
+ ctx.fillStyle = color;
308
+ ctx.beginPath();
309
+ for (const p of list) addShape(ctx, p, p.f);
310
+ ctx.fill();
311
+ list.length = 0;
312
+ });
313
+ const m = baseTransform(ctx);
314
+ ctx.lineCap = "round";
315
+ ctx.lineJoin = "round";
316
+ let any = false;
317
+ for (const p of pieces) {
318
+ if (p.shape !== "streamer") continue;
319
+ any = true;
320
+ place(ctx, m, p.x, p.y, p.rot);
321
+ const segs = 7;
322
+ const amp = p.h * 1.6;
323
+ ctx.strokeStyle = shade(p.color, 0.12 + 0.1 * Math.sin(p.wave));
324
+ ctx.lineWidth = p.h;
325
+ ctx.beginPath();
326
+ for (let i = 0; i <= segs; i++) ctx.lineTo(-p.w / 2 + i / segs * p.w, Math.sin(p.wave + i * 0.95) * amp);
327
+ ctx.stroke();
328
+ }
329
+ if (any) resetTransform(ctx, m);
330
+ },
331
+ burst(x, y) {
332
+ let added = 0;
333
+ for (const p of pieces) if (!p.recycle) added++;
334
+ const n = Math.min(170, MAX_BURST - added);
335
+ const power = Math.min(1.5, Math.max(0.6, h / 700));
336
+ for (let i = 0; i < n; i++) {
337
+ const a = -Math.PI / 2 + rand(-0.85, 0.85);
338
+ const v = rand(250, 720) * power;
339
+ pieces.push(make(x, y, Math.cos(a) * v, Math.sin(a) * v, false));
340
+ }
341
+ }
342
+ };
343
+ };
344
+
345
+ // src/effects/fireworks.ts
346
+ var DIWALI = ["#ffd700", "#ff9933", "#ff5e1a", "#ff3d7f", "#e040fb", "#7cfc00", "#00e5ff", "#fff6d5"];
347
+ var GOLD = "#ffc54d";
348
+ var GLITTER = ["#fff3c4", "#ffe08a", "#ffffff"];
349
+ var ROCKET_G = 260;
350
+ var MAX_PARTICLES = 3200;
351
+ var MAX_TRAIL = 16;
352
+ var SHELLS = [
353
+ "peony",
354
+ "peony",
355
+ "chrysanthemum",
356
+ "chrysanthemum",
357
+ "ring",
358
+ "doubleRing",
359
+ "willow",
360
+ "palm",
361
+ "crossette",
362
+ "strobe",
363
+ "colorChange",
364
+ "colorChange",
365
+ "multiBreak"
366
+ ];
367
+ var fireworks = (o) => {
368
+ let w = 0;
369
+ let h = 0;
370
+ let t = 0;
371
+ let timer = 0.3;
372
+ let fountainTimer = rand(2, 5);
373
+ const rockets = [];
374
+ const sparks = [];
375
+ const pending = [];
376
+ const smoke = [];
377
+ const fountains = [];
378
+ const flashes = [];
379
+ const free = [];
380
+ const groups = /* @__PURE__ */ new Map();
381
+ const colorIds = /* @__PURE__ */ new Map();
382
+ const glow = spriteCache((c) => glowSprite(c));
383
+ const star = spriteCache(
384
+ (c) => sprite(32, 32, (ctx) => {
385
+ const halo = glowSprite(c, false, 0.2);
386
+ const core = glowSprite(c);
387
+ ctx.globalAlpha = 0.45;
388
+ if (halo) ctx.drawImage(halo, 0, 0, 32, 32);
389
+ ctx.globalAlpha = 1;
390
+ if (core) ctx.drawImage(core, 16 - 6.4, 16 - 6.4, 12.8, 12.8);
391
+ })
392
+ );
393
+ const smokeSprite = spriteCache((c) => glowSprite(c, false, 0.3));
394
+ const palette = () => paletteOr(o.colors, DIWALI);
395
+ const scale = () => o.size * clamp(h / 700, 0.7, 1.4);
396
+ const launch = (x = rand(w * 0.1, w * 0.9), targetY = rand(h * 0.1, h * 0.42)) => {
397
+ const rise = Math.max(60, h - targetY);
398
+ rockets.push({
399
+ x,
400
+ y: h + 4,
401
+ vx: rand(-20, 20),
402
+ // Initial speed so gravity brings it to rest right at targetY.
403
+ vy: -Math.sqrt(2 * ROCKET_G * rise),
404
+ color: pick(palette()),
405
+ shell: pick(SHELLS),
406
+ trail: [],
407
+ seed: rand(0, TAU)
408
+ });
409
+ };
410
+ const blank = () => ({
411
+ x: 0,
412
+ y: 0,
413
+ vx: 0,
414
+ vy: 0,
415
+ life: 0,
416
+ max: 0,
417
+ color: "",
418
+ colorTo: null,
419
+ drag: 0,
420
+ gravity: 0,
421
+ width: 0,
422
+ trail: new Float32Array(MAX_TRAIL * 2),
423
+ trailLen: 0,
424
+ trailHead: 0,
425
+ trailCount: 0,
426
+ flicker: false,
427
+ strobe: false,
428
+ glitter: 0,
429
+ split: -1,
430
+ glow: 0,
431
+ seed: 0,
432
+ a: 0
433
+ });
434
+ const spark = (s) => {
435
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
436
+ if (sparks.length >= MAX_PARTICLES) return null;
437
+ const p = (_a = free.pop()) != null ? _a : blank();
438
+ const max = (_b = s.max) != null ? _b : rand(1.1, 1.9);
439
+ p.x = s.x;
440
+ p.y = s.y;
441
+ p.vx = s.vx;
442
+ p.vy = s.vy;
443
+ p.color = s.color;
444
+ p.life = max;
445
+ p.max = max;
446
+ p.colorTo = (_c = s.colorTo) != null ? _c : null;
447
+ p.drag = (_d = s.drag) != null ? _d : 0.975;
448
+ p.gravity = (_e = s.gravity) != null ? _e : 90;
449
+ p.width = (_f = s.width) != null ? _f : 2 * o.size;
450
+ p.trailLen = Math.min(MAX_TRAIL, (_g = s.trailLen) != null ? _g : 0);
451
+ p.trailHead = 0;
452
+ p.trailCount = 0;
453
+ p.flicker = (_h = s.flicker) != null ? _h : false;
454
+ p.strobe = (_i = s.strobe) != null ? _i : false;
455
+ p.glitter = (_j = s.glitter) != null ? _j : 0;
456
+ p.split = (_k = s.split) != null ? _k : -1;
457
+ p.glow = (_l = s.glow) != null ? _l : 1;
458
+ p.seed = rand(0, TAU);
459
+ sparks.push(p);
460
+ return p;
461
+ };
462
+ const groupFor = (color, alpha, width) => {
463
+ let id = colorIds.get(color);
464
+ if (id === void 0) {
465
+ id = colorIds.size;
466
+ colorIds.set(color, id);
467
+ }
468
+ const wl = Math.min(63, Math.round(width * 4));
469
+ const key = (id * (ALPHA_STEPS + 1) + alpha) * 64 + wl;
470
+ let g = groups.get(key);
471
+ if (!g) {
472
+ g = { color, alpha: alpha / ALPHA_STEPS, width: wl / 4, items: [] };
473
+ groups.set(key, g);
474
+ }
475
+ return g;
476
+ };
477
+ const sphere = (x, y, n, power, props) => {
478
+ for (let i = 0; i < n; i++) {
479
+ const a = rand(0, TAU);
480
+ const v = Math.sqrt(Math.random()) * power;
481
+ spark({ x, y, vx: Math.cos(a) * v, vy: Math.sin(a) * v, ...props(i) });
482
+ }
483
+ };
484
+ const ring = (x, y, n, power, color) => {
485
+ const tilt = rand(0.3, 1);
486
+ const rot = rand(0, TAU);
487
+ for (let i = 0; i < n; i++) {
488
+ const a = i / n * TAU;
489
+ const cx = Math.cos(a) * power;
490
+ const cy = Math.sin(a) * power * tilt;
491
+ spark({
492
+ x,
493
+ y,
494
+ vx: cx * Math.cos(rot) - cy * Math.sin(rot),
495
+ vy: cx * Math.sin(rot) + cy * Math.cos(rot),
496
+ color,
497
+ trailLen: 3
498
+ });
499
+ }
500
+ };
501
+ const explode = (x, y, color, shell, size = 1) => {
502
+ const k = scale() * size;
503
+ const power = rand(150, 240) * k;
504
+ const second = pick(palette());
505
+ flashes.push({ x, y, life: 1, color, size: 90 * k });
506
+ for (let i = 0; i < 4 * size; i++) {
507
+ smoke.push({ x: x + rand(-25, 25) * k, y: y + rand(-25, 25) * k, r: rand(25, 45) * k, life: 0, max: rand(2.5, 4) });
508
+ }
509
+ switch (shell) {
510
+ case "chrysanthemum":
511
+ sphere(x, y, 120 * size, power, () => ({ color, trailLen: 7, glitter: 22, glow: 0.8 }));
512
+ break;
513
+ case "ring":
514
+ ring(x, y, 60, power, color);
515
+ sphere(x, y, 24, power * 0.3, () => ({ color: "#fff6d5", width: 1.4 * o.size, max: rand(0.6, 1) }));
516
+ break;
517
+ case "doubleRing":
518
+ ring(x, y, 54, power, color);
519
+ ring(x, y, 40, power * 0.6, second);
520
+ break;
521
+ case "willow":
522
+ sphere(x, y, 90 * size, power * 0.8, () => ({
523
+ color: GOLD,
524
+ trailLen: 16,
525
+ max: rand(2.4, 3.4),
526
+ drag: 0.955,
527
+ gravity: 120,
528
+ width: 1.5 * o.size,
529
+ glitter: 6,
530
+ glow: 0.6
531
+ }));
532
+ break;
533
+ case "palm": {
534
+ const arms = 7 + (Math.random() * 3 | 0);
535
+ for (let i = 0; i < arms; i++) {
536
+ const a = i / arms * TAU + rand(-0.1, 0.1);
537
+ for (let j = 0; j < 4; j++) {
538
+ const v = power * (j === 0 ? rand(0.9, 1) : rand(0.55, 0.9));
539
+ spark({
540
+ x,
541
+ y,
542
+ vx: Math.cos(a) * v,
543
+ vy: Math.sin(a) * v,
544
+ color: GOLD,
545
+ colorTo: color,
546
+ trailLen: j === 0 ? 14 : 8,
547
+ width: (j === 0 ? 3.2 : 1.8) * o.size,
548
+ glitter: j === 0 ? 40 : 0,
549
+ max: rand(1.6, 2.2),
550
+ drag: 0.965
551
+ });
552
+ }
553
+ }
554
+ break;
555
+ }
556
+ case "crossette":
557
+ sphere(x, y, 16, power * 0.9, () => ({ color, trailLen: 5, split: rand(0.4, 0.6), width: 2.6 * o.size }));
558
+ break;
559
+ case "strobe":
560
+ sphere(x, y, 70 * size, power * 0.85, () => ({
561
+ color: "#ffffff",
562
+ strobe: true,
563
+ max: rand(2, 2.8),
564
+ drag: 0.96,
565
+ width: 2.2 * o.size,
566
+ glow: 1.2
567
+ }));
568
+ break;
569
+ case "colorChange":
570
+ sphere(x, y, 110 * size, power, (i) => ({ color: i % 3 ? color : second, colorTo: i % 3 ? second : color, trailLen: 4 }));
571
+ break;
572
+ case "multiBreak": {
573
+ sphere(x, y, 50, power * 0.6, () => ({ color: GOLD, trailLen: 4, glitter: 10 }));
574
+ const n = 4 + (Math.random() * 3 | 0);
575
+ for (let i = 0; i < n; i++) {
576
+ const a = i / n * TAU + rand(-0.3, 0.3);
577
+ const d = power * rand(0.35, 0.5);
578
+ pending.push({ x: x + Math.cos(a) * d, y: y + Math.sin(a) * d, delay: rand(0.25, 0.5), color: pick(palette()) });
579
+ }
580
+ break;
581
+ }
582
+ default:
583
+ sphere(x, y, 110 * size, power, (i) => ({ color: size < 1 || i % 7 ? color : "#fff6d5", trailLen: 5 }));
584
+ }
585
+ };
586
+ const splitCross = (p) => {
587
+ const base = rand(0, TAU);
588
+ for (let i = 0; i < 4; i++) {
589
+ const a = base + i * Math.PI / 2;
590
+ const v = rand(70, 100) * o.size;
591
+ spark({
592
+ x: p.x,
593
+ y: p.y,
594
+ vx: p.vx * 0.4 + Math.cos(a) * v,
595
+ vy: p.vy * 0.4 + Math.sin(a) * v,
596
+ color: p.color,
597
+ trailLen: 4,
598
+ max: rand(0.6, 0.9),
599
+ width: 1.6 * o.size
600
+ });
601
+ }
602
+ };
603
+ const glitter = (p) => spark({
604
+ x: p.x,
605
+ y: p.y,
606
+ vx: p.vx * 0.1 + rand(-12, 12),
607
+ vy: p.vy * 0.1 + rand(-5, 20),
608
+ color: pick(GLITTER),
609
+ max: rand(0.3, 0.6),
610
+ gravity: 140,
611
+ width: 1 * o.size,
612
+ flicker: true,
613
+ glow: 0.5
614
+ });
615
+ return {
616
+ resize(nw, nh) {
617
+ w = nw;
618
+ h = nh;
619
+ },
620
+ update(dt) {
621
+ const s = dt * o.speed;
622
+ t += s;
623
+ if (o.density > 0) {
624
+ timer -= s;
625
+ if (timer <= 0) {
626
+ const salvo = Math.random() < 0.12 ? 3 : Math.random() < 0.3 ? 2 : 1;
627
+ for (let i = 0; i < salvo; i++) launch();
628
+ timer = rand(0.7, 1.7) / o.density;
629
+ }
630
+ if (o.ground) {
631
+ fountainTimer -= s;
632
+ if (fountainTimer <= 0) {
633
+ fountains.push({ x: rand(w * 0.08, w * 0.92), life: 0, max: rand(3, 4.5) });
634
+ fountainTimer = rand(5, 12) / o.density;
635
+ }
636
+ }
637
+ }
638
+ retain(rockets, (r) => {
639
+ r.vy += ROCKET_G * s;
640
+ r.vx += (o.wind * 40 + Math.sin(t * 18 + r.seed) * 35) * s;
641
+ r.x += r.vx * s;
642
+ r.y += r.vy * s;
643
+ r.trail.push(r.x, r.y);
644
+ if (r.trail.length > 24) r.trail.splice(0, 2);
645
+ if (Math.random() < 0.9) {
646
+ spark({
647
+ x: r.x + rand(-1, 1),
648
+ y: r.y + 2,
649
+ vx: rand(-18, 18),
650
+ vy: rand(10, 60),
651
+ color: pick(GLITTER),
652
+ max: rand(0.25, 0.55),
653
+ gravity: 60,
654
+ width: 1.1 * o.size,
655
+ flicker: Math.random() < 0.4,
656
+ glow: 0.5
657
+ });
658
+ }
659
+ if (r.vy < -12) return true;
660
+ explode(r.x, r.y, r.color, r.shell);
661
+ return false;
662
+ });
663
+ retain(pending, (p) => {
664
+ if ((p.delay -= s) > 0) return true;
665
+ explode(p.x, p.y, p.color, "peony", 0.4);
666
+ return false;
667
+ });
668
+ retain(fountains, (f) => {
669
+ if ((f.life += s) >= f.max) return false;
670
+ const env = Math.sin(Math.PI * f.life / f.max);
671
+ const n = Math.round(240 * s * env + Math.random());
672
+ const reach = clamp(h / 700, 0.55, 1.3) * o.size;
673
+ for (let j = 0; j < n; j++) {
674
+ const a = -Math.PI / 2 + rand(-0.3, 0.3);
675
+ const v = rand(250, 520) * reach * Math.sqrt(env);
676
+ spark({
677
+ x: f.x + rand(-2, 2),
678
+ y: h - 8,
679
+ vx: Math.cos(a) * v,
680
+ vy: Math.sin(a) * v,
681
+ color: Math.random() < 0.12 ? pick(palette()) : pick(GLITTER),
682
+ max: rand(0.7, 1.3),
683
+ gravity: 430,
684
+ drag: 0.985,
685
+ width: 1.4 * o.size,
686
+ flicker: Math.random() < 0.3,
687
+ glow: 0.6
688
+ });
689
+ }
690
+ return true;
691
+ });
692
+ retain(sparks, (p) => {
693
+ const k = drag(p.drag, s);
694
+ p.vx = p.vx * k + o.wind * 20 * s;
695
+ p.vy = p.vy * k + p.gravity * s;
696
+ p.x += p.vx * s;
697
+ p.y += p.vy * s;
698
+ p.life -= s;
699
+ if (p.trailLen) {
700
+ p.trail[p.trailHead * 2] = p.x;
701
+ p.trail[p.trailHead * 2 + 1] = p.y;
702
+ p.trailHead = (p.trailHead + 1) % p.trailLen;
703
+ if (p.trailCount < p.trailLen) p.trailCount++;
704
+ }
705
+ if (p.colorTo && p.life < p.max * 0.55) {
706
+ p.color = p.colorTo;
707
+ p.colorTo = null;
708
+ }
709
+ if (p.glitter && Math.random() < p.glitter * s) glitter(p);
710
+ if (p.split >= 0) {
711
+ p.split -= s;
712
+ if (p.split < 0) {
713
+ splitCross(p);
714
+ p.life = 0;
715
+ }
716
+ }
717
+ if (p.life > 0) return true;
718
+ free.push(p);
719
+ return false;
720
+ });
721
+ retain(smoke, (p) => {
722
+ p.r += 12 * s;
723
+ p.y -= 4 * s;
724
+ p.x += o.wind * 25 * s;
725
+ return (p.life += s) < p.max;
726
+ });
727
+ retain(flashes, (f) => (f.life -= s * 4) > 0);
728
+ },
729
+ draw(ctx) {
730
+ const puff = smokeSprite("#b8bcc8");
731
+ for (const p of smoke) {
732
+ const k = p.life / p.max;
733
+ ctx.globalAlpha = 0.07 * Math.sin(Math.PI * k);
734
+ stamp(ctx, puff, p.x, p.y, p.r * 2);
735
+ }
736
+ ctx.globalCompositeOperation = "lighter";
737
+ ctx.lineCap = "round";
738
+ ctx.lineJoin = "round";
739
+ for (const f of flashes) {
740
+ ctx.globalAlpha = f.life * 0.06;
741
+ ctx.fillStyle = f.color;
742
+ ctx.fillRect(0, 0, w, h);
743
+ ctx.globalAlpha = f.life * 0.7;
744
+ stamp(ctx, glow(f.color), f.x, f.y, f.size * (1.4 - f.life * 0.4));
745
+ }
746
+ for (const f of fountains) {
747
+ const env = Math.sin(Math.PI * f.life / f.max);
748
+ ctx.globalAlpha = 0.55 * env;
749
+ stamp(ctx, glow(GOLD), f.x, h - 6, 90 * o.size * env);
750
+ }
751
+ for (const r of rockets) {
752
+ ctx.globalAlpha = 0.5;
753
+ ctx.strokeStyle = GOLD;
754
+ ctx.lineWidth = 1.5 * o.size;
755
+ if (r.trail.length >= 4) polyline(ctx, r.trail);
756
+ ctx.globalAlpha = 1;
757
+ stamp(ctx, glow(GOLD), r.x, r.y, 14 * o.size);
758
+ }
759
+ for (const p of sparks) {
760
+ const life = p.life / p.max;
761
+ let a = life < 0.3 ? life / 0.3 : 1;
762
+ if (p.flicker) a *= Math.random() < 0.5 ? 0.15 : 1;
763
+ if (p.strobe) a *= Math.sin((p.max - p.life) * 28 + p.seed) > 0.2 ? 1 : 0.04;
764
+ p.a = a;
765
+ if (a < 0.02) continue;
766
+ const trail = p.trailCount >= 2;
767
+ const lv = alphaLevel(trail ? a * 0.6 : a);
768
+ if (lv > 0) groupFor(p.color, lv, trail ? p.width * 0.7 : p.width).items.push(p);
769
+ }
770
+ groups.forEach((g) => {
771
+ if (!g.items.length) return;
772
+ ctx.strokeStyle = g.color;
773
+ ctx.globalAlpha = g.alpha;
774
+ ctx.lineWidth = g.width;
775
+ ctx.beginPath();
776
+ for (const p of g.items) {
777
+ if (p.trailCount >= 2) {
778
+ const n = p.trailLen;
779
+ let idx = (p.trailHead - p.trailCount + n) % n;
780
+ ctx.moveTo(p.trail[idx * 2], p.trail[idx * 2 + 1]);
781
+ for (let i = 1; i < p.trailCount; i++) {
782
+ idx = (idx + 1) % n;
783
+ ctx.lineTo(p.trail[idx * 2], p.trail[idx * 2 + 1]);
784
+ }
785
+ } else {
786
+ ctx.moveTo(p.x, p.y);
787
+ ctx.lineTo(p.x - p.vx * 0.03, p.y - p.vy * 0.03);
788
+ }
789
+ }
790
+ ctx.stroke();
791
+ g.items.length = 0;
792
+ });
793
+ for (const p of sparks) {
794
+ if (!p.glow || p.a < 0.02) continue;
795
+ ctx.globalAlpha = p.a;
796
+ stamp(ctx, star(p.color), p.x, p.y, p.width * 4.5 * p.glow);
797
+ }
798
+ },
799
+ burst(x, y) {
800
+ launch(x, y);
801
+ }
802
+ };
803
+ };
804
+
805
+ // src/effects/diyas.ts
806
+ var LANTERNS = ["#ffb347", "#ff8c42", "#ffd166", "#ff6f3c", "#ffa94d"];
807
+ var FLAME_OUTER = "#ff8a1c";
808
+ var FLAME_MID = "#ffd23f";
809
+ var diyaSprite = () => sprite(48, 24, (ctx) => {
810
+ ctx.translate(24, 10);
811
+ ctx.beginPath();
812
+ ctx.moveTo(-17, -1);
813
+ ctx.bezierCurveTo(-16, 12, 12, 13, 16, 2);
814
+ ctx.quadraticCurveTo(19, -1, 21, -5);
815
+ ctx.quadraticCurveTo(14, -3, 8, -2);
816
+ ctx.quadraticCurveTo(-5, -4, -17, -1);
817
+ ctx.closePath();
818
+ const g = ctx.createLinearGradient(0, -4, 0, 12);
819
+ g.addColorStop(0, "#d9622b");
820
+ g.addColorStop(0.5, "#a63c10");
821
+ g.addColorStop(1, "#5e1f05");
822
+ ctx.fillStyle = g;
823
+ ctx.fill();
824
+ ctx.fillStyle = "#3b1a06";
825
+ ctx.beginPath();
826
+ ctx.ellipse(-2, -1.5, 13, 2, 0, 0, TAU);
827
+ ctx.fill();
828
+ ctx.strokeStyle = "#f08a4b";
829
+ ctx.lineWidth = 1;
830
+ ctx.beginPath();
831
+ ctx.moveTo(-17, -1);
832
+ ctx.quadraticCurveTo(-5, -4, 8, -2);
833
+ ctx.quadraticCurveTo(14, -3, 21, -5);
834
+ ctx.stroke();
835
+ ctx.fillStyle = "#ffd27a";
836
+ for (let i = 0; i < 7; i++) {
837
+ ctx.beginPath();
838
+ ctx.arc(-12 + i * 4, 4.5 + Math.sin(i * 0.9) * 0.4, 0.9, 0, TAU);
839
+ ctx.fill();
840
+ }
841
+ ctx.fillStyle = "#ffffff";
842
+ for (let i = 0; i < 6; i++) {
843
+ ctx.beginPath();
844
+ ctx.arc(-10 + i * 4, 7.5, 0.6, 0, TAU);
845
+ ctx.fill();
846
+ }
847
+ });
848
+ var lanternSprite = (color) => sprite(32, 40, (ctx) => {
849
+ ctx.translate(16, 20);
850
+ ctx.beginPath();
851
+ ctx.moveTo(-12, -16);
852
+ ctx.lineTo(12, -16);
853
+ ctx.quadraticCurveTo(13, 2, 8, 16);
854
+ ctx.lineTo(-8, 16);
855
+ ctx.quadraticCurveTo(-13, 2, -12, -16);
856
+ ctx.closePath();
857
+ ctx.fillStyle = color;
858
+ ctx.fill();
859
+ ctx.save();
860
+ ctx.clip();
861
+ const g = ctx.createRadialGradient(0, 12, 0, 0, 6, 22);
862
+ g.addColorStop(0, "rgba(255,250,210,0.95)");
863
+ g.addColorStop(0.5, "rgba(255,220,140,0.35)");
864
+ g.addColorStop(1, "rgba(120,30,0,0.35)");
865
+ ctx.fillStyle = g;
866
+ ctx.fillRect(-16, -20, 32, 40);
867
+ ctx.strokeStyle = "rgba(140,50,0,0.35)";
868
+ ctx.lineWidth = 0.8;
869
+ ctx.beginPath();
870
+ for (const x of [-6, 0, 6]) {
871
+ ctx.moveTo(x * 1.1, -16);
872
+ ctx.lineTo(x * 0.75, 16);
873
+ }
874
+ ctx.moveTo(-13, -4);
875
+ ctx.lineTo(13, -4);
876
+ ctx.stroke();
877
+ ctx.restore();
878
+ ctx.strokeStyle = "#7a2e00";
879
+ ctx.lineWidth = 1.2;
880
+ ctx.beginPath();
881
+ ctx.moveTo(-8, 16);
882
+ ctx.lineTo(8, 16);
883
+ ctx.stroke();
884
+ });
885
+ var flamePath = (ctx, x, y, fw, fh, lean) => {
886
+ ctx.beginPath();
887
+ ctx.moveTo(x, y);
888
+ ctx.bezierCurveTo(x + fw, y - fh * 0.15, x + fw * 0.6 + lean * 0.5, y - fh * 0.7, x + lean, y - fh);
889
+ ctx.bezierCurveTo(x - fw * 0.6 + lean * 0.5, y - fh * 0.7, x - fw, y - fh * 0.15, x, y);
890
+ ctx.fill();
891
+ };
892
+ var diyas = (o) => {
893
+ let w = 0;
894
+ let h = 0;
895
+ let t = 0;
896
+ const row = [];
897
+ const lanterns = [];
898
+ const embers = [];
899
+ const bowl = spriteCache(() => diyaSprite());
900
+ const paper = spriteCache((c) => lanternSprite(c));
901
+ const glow = spriteCache((c) => glowSprite(c, true, 0.3));
902
+ const palette = () => paletteOr(o.colors, LANTERNS);
903
+ const makeLantern = (anywhere, x = rand(0, w), y) => ({
904
+ x,
905
+ y: y != null ? y : anywhere ? rand(h * 0.1, h * 1.1) : h + rand(30, 200),
906
+ z: rand(0.6, 1.2),
907
+ vy: rand(18, 34),
908
+ sway: rand(6, 16),
909
+ phase: rand(0, TAU),
910
+ color: pick(palette()),
911
+ seed: rand(0, 100)
912
+ });
913
+ const flicker = (seed) => 1 + 0.1 * Math.sin(t * 13 + seed) + 0.07 * Math.sin(t * 29 + seed * 1.7) + rand(-0.04, 0.04);
914
+ const wickY = () => h - 14 * o.size;
915
+ const wick = (d) => d.x + 13 * o.size;
916
+ return {
917
+ resize(nw, nh) {
918
+ w = nw;
919
+ h = nh;
920
+ const spacing = 90 * o.size;
921
+ const n = o.ground ? Math.max(1, Math.floor(w / spacing)) : 0;
922
+ const offset = (w - (n - 1) * spacing) / 2;
923
+ while (row.length < n) row.push({ x: 0, seed: rand(0, 100) });
924
+ row.length = n;
925
+ row.forEach((d, i) => d.x = offset + i * spacing);
926
+ fillPool(lanterns, countFor(w, h, 9, o.density), () => makeLantern(true));
927
+ },
928
+ update(dt) {
929
+ const s = dt * o.speed;
930
+ t += s;
931
+ for (const l of lanterns) {
932
+ l.phase += 0.6 * s;
933
+ l.y -= l.vy * l.z * s;
934
+ l.x += (Math.sin(l.phase) * l.sway + o.wind * 30) * s;
935
+ if (l.y < -60) Object.assign(l, makeLantern(false));
936
+ }
937
+ if (o.density > 0) {
938
+ for (const d of row) {
939
+ if (embers.length < 220 && Math.random() < 1.8 * o.density * s) {
940
+ const max = rand(1, 2.2);
941
+ embers.push({ x: wick(d), y: wickY() - 14 * o.size, vx: rand(-8, 8), vy: rand(-55, -25), life: max, max, seed: rand(0, TAU) });
942
+ }
943
+ }
944
+ }
945
+ retain(embers, (e) => {
946
+ e.x += (e.vx + Math.sin(t * 3 + e.seed) * 10 + o.wind * 30) * s;
947
+ e.y += e.vy * s;
948
+ return (e.life -= s) > 0;
949
+ });
950
+ },
951
+ draw(ctx) {
952
+ const sz = o.size;
953
+ for (const l of lanterns) {
954
+ const k = l.z * (0.7 + 0.5 * clamp(l.y / h, 0, 1)) * sz;
955
+ const fl = flicker(l.seed);
956
+ const a = clamp(0.35 + 0.65 * (l.y / h), 0.3, 1);
957
+ ctx.globalCompositeOperation = "lighter";
958
+ ctx.globalAlpha = 0.45 * a * fl;
959
+ stamp(ctx, glow(l.color), l.x, l.y + 5 * k, 120 * k);
960
+ ctx.globalCompositeOperation = "source-over";
961
+ ctx.globalAlpha = a;
962
+ const img = paper(l.color);
963
+ if (img) ctx.drawImage(img, l.x - 17 * k, l.y - 21 * k, 34 * k, 42 * k);
964
+ ctx.globalCompositeOperation = "lighter";
965
+ ctx.globalAlpha = a * fl;
966
+ stamp(ctx, glow(FLAME_MID), l.x, l.y + 17 * k, 12 * k);
967
+ }
968
+ ctx.globalCompositeOperation = "lighter";
969
+ for (const e of embers) {
970
+ const k = e.life / e.max;
971
+ ctx.globalAlpha = k * (Math.random() < 0.15 ? 0.3 : 0.9);
972
+ stamp(ctx, glow(FLAME_OUTER), e.x, e.y, 5 * sz);
973
+ }
974
+ if (!row.length) return;
975
+ const wy = wickY();
976
+ const lean = Math.sin(t * 2.3) * 1.5 + o.wind * 5;
977
+ const m = baseTransform(ctx);
978
+ const pool = glow(FLAME_OUTER);
979
+ for (const d of row) {
980
+ const fl = flicker(d.seed);
981
+ ctx.globalAlpha = 0.35 * fl;
982
+ place(ctx, m, wick(d), h - 4, 0, 1, 0.35);
983
+ stamp(ctx, pool, 0, 0, 120 * sz * fl);
984
+ }
985
+ resetTransform(ctx, m);
986
+ ctx.globalCompositeOperation = "source-over";
987
+ ctx.globalAlpha = 1;
988
+ const b = bowl("");
989
+ for (const d of row) if (b) ctx.drawImage(b, d.x - 24 * sz, h - 20 * sz, 48 * sz, 24 * sz);
990
+ for (const d of row) {
991
+ const fl = flicker(d.seed);
992
+ const x = wick(d);
993
+ const fh = 20 * sz * fl;
994
+ const ln = lean + Math.sin(t * 7 + d.seed) * 1.2;
995
+ ctx.globalCompositeOperation = "source-over";
996
+ ctx.globalAlpha = 0.95;
997
+ ctx.fillStyle = FLAME_OUTER;
998
+ flamePath(ctx, x, wy, 5 * sz, fh, ln);
999
+ ctx.fillStyle = FLAME_MID;
1000
+ flamePath(ctx, x, wy - 1, 3.5 * sz, fh * 0.72, ln * 0.8);
1001
+ ctx.fillStyle = "#fff8e0";
1002
+ flamePath(ctx, x, wy - 1.5, 2 * sz, fh * 0.42, ln * 0.6);
1003
+ ctx.globalAlpha = 0.6;
1004
+ ctx.fillStyle = "#8fc8ff";
1005
+ ctx.beginPath();
1006
+ ctx.ellipse(x, wy - 1, 1.6 * sz, 2.2 * sz, 0, 0, TAU);
1007
+ ctx.fill();
1008
+ ctx.globalCompositeOperation = "lighter";
1009
+ ctx.globalAlpha = 0.55 * fl;
1010
+ stamp(ctx, glow(FLAME_OUTER), x + ln * 0.5, wy - fh * 0.45, 70 * sz * fl);
1011
+ }
1012
+ },
1013
+ burst(x, y) {
1014
+ lanterns.push(makeLantern(false, x, y));
1015
+ if (lanterns.length > 60) lanterns.shift();
1016
+ }
1017
+ };
1018
+ };
1019
+
1020
+ // src/effects/fireflies.ts
1021
+ var fireflies = (o) => {
1022
+ let w = 0;
1023
+ let h = 0;
1024
+ const swarm = [];
1025
+ const color = () => {
1026
+ var _a, _b;
1027
+ return (_b = (_a = o.colors) == null ? void 0 : _a[0]) != null ? _b : "#d8ff6a";
1028
+ };
1029
+ const glow = spriteCache((c) => glowSprite(c, true, 0.2));
1030
+ const top = () => o.ground ? h * 0.3 : 0;
1031
+ const make = () => ({
1032
+ x: rand(0, w),
1033
+ y: rand(top(), h),
1034
+ heading: rand(0, TAU),
1035
+ turn: 0,
1036
+ speed: rand(12, 32),
1037
+ clock: rand(0, 5),
1038
+ period: rand(2.5, 5.5),
1039
+ startle: 0,
1040
+ pushX: 0,
1041
+ pushY: 0,
1042
+ z: rand(0.6, 1.2)
1043
+ });
1044
+ const brightness = (f) => {
1045
+ const p = f.clock / f.period;
1046
+ let b = 0;
1047
+ if (p < 0.08) b = p / 0.08;
1048
+ else if (p < 0.22) b = 1;
1049
+ else if (p < 0.45) b = 1 - (p - 0.22) / 0.23;
1050
+ return clamp(b + f.startle, 0, 1);
1051
+ };
1052
+ return {
1053
+ resize(nw, nh) {
1054
+ w = nw;
1055
+ h = nh;
1056
+ fillPool(swarm, countFor(w, h, 45, o.density), make);
1057
+ },
1058
+ update(dt) {
1059
+ const s = dt * o.speed;
1060
+ const lo = top();
1061
+ for (const f of swarm) {
1062
+ f.turn += rand(-3, 3) * s;
1063
+ f.turn *= 0.96;
1064
+ f.heading += f.turn * s;
1065
+ const margin = 30;
1066
+ if (f.x < margin || f.x > w - margin || f.y < lo + margin || f.y > h - margin) {
1067
+ const want = Math.atan2((lo + h) / 2 - f.y, w / 2 - f.x);
1068
+ const diff = Math.atan2(Math.sin(want - f.heading), Math.cos(want - f.heading));
1069
+ f.heading += diff * Math.min(1, 2 * s);
1070
+ }
1071
+ const k = drag(0.94, s);
1072
+ f.pushX *= k;
1073
+ f.pushY *= k;
1074
+ f.x += (Math.cos(f.heading) * f.speed + o.wind * 20 + f.pushX) * s;
1075
+ f.y += (Math.sin(f.heading) * f.speed * 0.7 + f.pushY) * s;
1076
+ f.x = clamp(f.x, -10, w + 10);
1077
+ f.y = clamp(f.y, lo - 10, h + 10);
1078
+ f.clock += s;
1079
+ if (f.clock >= f.period) {
1080
+ f.clock -= f.period;
1081
+ f.period = rand(2.5, 5.5);
1082
+ }
1083
+ f.startle = Math.max(0, f.startle - s * 0.8);
1084
+ }
1085
+ },
1086
+ draw(ctx) {
1087
+ const c = color();
1088
+ const g = glow(c);
1089
+ ctx.globalCompositeOperation = "lighter";
1090
+ for (const f of swarm) {
1091
+ const b = brightness(f);
1092
+ ctx.globalAlpha = 0.18 + b * 0.82;
1093
+ stamp(ctx, g, f.x, f.y, 4 * f.z * o.size);
1094
+ if (b > 0.02) {
1095
+ ctx.globalAlpha = b * 0.55;
1096
+ stamp(ctx, g, f.x, f.y, 34 * f.z * o.size * (0.7 + b * 0.3));
1097
+ }
1098
+ }
1099
+ },
1100
+ burst(x, y) {
1101
+ for (const f of swarm) {
1102
+ const dx = f.x - x;
1103
+ const dy = f.y - y;
1104
+ const d = Math.hypot(dx, dy);
1105
+ if (d < 220 && d > 0) {
1106
+ const k = 1 - d / 220;
1107
+ f.startle = Math.max(f.startle, k);
1108
+ f.pushX += dx / d * 160 * k;
1109
+ f.pushY += dy / d * 160 * k;
1110
+ }
1111
+ }
1112
+ }
1113
+ };
1114
+ };
1115
+
1116
+ // src/effects/holi.ts
1117
+ var GULAL = ["#ff1f8e", "#ffd400", "#00c853", "#2979ff", "#ff6d00", "#aa00ff", "#ff1744"];
1118
+ var MAX_CLOUDS = 700;
1119
+ var MAX_GRAINS = 1500;
1120
+ var splatSprite = (color) => sprite(128, 128, (ctx) => {
1121
+ ctx.translate(64, 64);
1122
+ ctx.fillStyle = color;
1123
+ const pts = 16;
1124
+ ctx.beginPath();
1125
+ for (let i = 0; i <= pts; i++) {
1126
+ const a = i / pts * TAU;
1127
+ const r = 30 * rand(0.75, 1.15);
1128
+ const x = Math.cos(a) * r;
1129
+ const y = Math.sin(a) * r;
1130
+ if (i === 0) ctx.moveTo(x, y);
1131
+ else ctx.quadraticCurveTo(Math.cos(a - 0.2) * r * 1.15, Math.sin(a - 0.2) * r * 1.15, x, y);
1132
+ }
1133
+ ctx.fill();
1134
+ const n = 7 + (Math.random() * 6 | 0);
1135
+ for (let i = 0; i < n; i++) {
1136
+ const a = rand(0, TAU);
1137
+ const d = rand(36, 58);
1138
+ ctx.beginPath();
1139
+ ctx.arc(Math.cos(a) * d, Math.sin(a) * d, rand(1.5, 5), 0, TAU);
1140
+ ctx.fill();
1141
+ }
1142
+ const g = ctx.createRadialGradient(-10, -12, 0, -10, -12, 26);
1143
+ g.addColorStop(0, "rgba(255,255,255,0.35)");
1144
+ g.addColorStop(1, "rgba(255,255,255,0)");
1145
+ ctx.globalCompositeOperation = "source-atop";
1146
+ ctx.fillStyle = g;
1147
+ ctx.fillRect(-64, -64, 128, 128);
1148
+ });
1149
+ var holi = (o) => {
1150
+ let w = 0;
1151
+ let h = 0;
1152
+ let throwTimer = 0.2;
1153
+ let splatTimer = rand(1.5, 3);
1154
+ const clouds = [];
1155
+ const grains = [];
1156
+ const splats = [];
1157
+ const grainGroups = /* @__PURE__ */ new Map();
1158
+ const puff = spriteCache((c) => glowSprite(c, false, 0.4));
1159
+ const palette = () => paletteOr(o.colors, GULAL);
1160
+ const toss = (x, y, dir = -Math.PI / 2 + rand(-1, 1)) => {
1161
+ const main = pick(palette());
1162
+ const mix = Math.random() < 0.35 ? pick(palette()) : main;
1163
+ const k = o.size;
1164
+ for (let i = 0; i < 42 && clouds.length < MAX_CLOUDS; i++) {
1165
+ const a = dir + rand(-0.9, 0.9);
1166
+ const v = rand(40, 380) * k;
1167
+ const max = rand(1.8, 3.2);
1168
+ clouds.push({
1169
+ x: x + rand(-6, 6),
1170
+ y: y + rand(-6, 6),
1171
+ vx: Math.cos(a) * v,
1172
+ vy: Math.sin(a) * v,
1173
+ r: rand(6, 18) * k,
1174
+ vr: rand(12, 30) * k,
1175
+ life: max,
1176
+ max,
1177
+ color: i % 4 === 3 ? mix : main
1178
+ });
1179
+ }
1180
+ for (let i = 0; i < 140 && grains.length < MAX_GRAINS; i++) {
1181
+ const a = dir + rand(-1.1, 1.1);
1182
+ const v = rand(180, 620) * k;
1183
+ const max = rand(0.9, 2);
1184
+ grains.push({
1185
+ x,
1186
+ y,
1187
+ vx: Math.cos(a) * v,
1188
+ vy: Math.sin(a) * v,
1189
+ r: rand(0.8, 2.2) * k,
1190
+ life: max,
1191
+ max,
1192
+ color: Math.random() < 0.7 ? main : mix
1193
+ });
1194
+ }
1195
+ };
1196
+ const splat = (x = rand(w * 0.05, w * 0.95), y = rand(h * 0.05, h * 0.85)) => {
1197
+ const color = pick(palette());
1198
+ const ndrips = 1 + (Math.random() * 3 | 0);
1199
+ splats.push({
1200
+ x,
1201
+ y,
1202
+ size: rand(55, 110) * o.size,
1203
+ img: splatSprite(color),
1204
+ life: 0,
1205
+ max: rand(3, 4.5),
1206
+ color,
1207
+ drips: Array.from({ length: ndrips }, () => ({ dx: rand(-0.18, 0.18), len: rand(0.4, 1), w: rand(2, 4) }))
1208
+ });
1209
+ if (splats.length > 25) splats.shift();
1210
+ };
1211
+ return {
1212
+ resize(nw, nh) {
1213
+ w = nw;
1214
+ h = nh;
1215
+ },
1216
+ update(dt) {
1217
+ const s = dt * o.speed;
1218
+ if (o.density > 0) {
1219
+ throwTimer -= s;
1220
+ if (throwTimer <= 0) {
1221
+ toss(rand(w * 0.1, w * 0.9), rand(h * 0.35, h * 0.95));
1222
+ throwTimer = rand(0.6, 1.6) / o.density;
1223
+ }
1224
+ splatTimer -= s;
1225
+ if (splatTimer <= 0) {
1226
+ splat();
1227
+ splatTimer = rand(2.5, 6) / o.density;
1228
+ }
1229
+ }
1230
+ const kc = drag(0.92, s);
1231
+ const kr = drag(0.985, s);
1232
+ retain(clouds, (c) => {
1233
+ c.vx = c.vx * kc + o.wind * 30 * s;
1234
+ c.vy = c.vy * kc + 18 * s;
1235
+ c.x += c.vx * s;
1236
+ c.y += c.vy * s;
1237
+ c.r += c.vr * s;
1238
+ c.vr *= kr;
1239
+ return (c.life -= s) > 0;
1240
+ });
1241
+ const kg = drag(0.965, s);
1242
+ retain(grains, (g) => {
1243
+ g.vx = g.vx * kg + o.wind * 40 * s;
1244
+ g.vy = g.vy * kg + 260 * s;
1245
+ g.x += g.vx * s;
1246
+ g.y += g.vy * s;
1247
+ return (g.life -= s) > 0 && g.y <= h + 10;
1248
+ });
1249
+ retain(splats, (p) => (p.life += s) < p.max);
1250
+ },
1251
+ draw(ctx) {
1252
+ for (const p of splats) {
1253
+ const k = p.life / p.max;
1254
+ const a = Math.min(1, p.life * 8) * Math.min(1, (1 - k) / 0.4) * 0.75;
1255
+ const grow = Math.min(1, p.life * 6);
1256
+ const d = p.size * (0.7 + 0.3 * grow);
1257
+ ctx.globalAlpha = a;
1258
+ stamp(ctx, p.img, p.x, p.y, d);
1259
+ const run = Math.min(1, p.life / 1.2);
1260
+ ctx.fillStyle = p.color;
1261
+ ctx.lineCap = "round";
1262
+ ctx.strokeStyle = p.color;
1263
+ for (const dr of p.drips) {
1264
+ const x = p.x + dr.dx * d;
1265
+ const y0 = p.y + d * 0.15;
1266
+ const y1 = y0 + d * 0.55 * dr.len * run;
1267
+ ctx.lineWidth = dr.w * o.size;
1268
+ ctx.beginPath();
1269
+ ctx.moveTo(x, y0);
1270
+ ctx.lineTo(x, y1);
1271
+ ctx.stroke();
1272
+ ctx.beginPath();
1273
+ ctx.arc(x, y1, dr.w * o.size * 0.9, 0, TAU);
1274
+ ctx.fill();
1275
+ }
1276
+ }
1277
+ for (const c of clouds) {
1278
+ const k = c.life / c.max;
1279
+ ctx.globalAlpha = Math.pow(k, 0.8) * 0.6;
1280
+ stamp(ctx, puff(c.color), c.x, c.y, c.r * 2);
1281
+ }
1282
+ for (const g of grains) {
1283
+ let groups = grainGroups.get(g.color);
1284
+ if (!groups) {
1285
+ groups = Array.from({ length: ALPHA_STEPS + 1 }, () => []);
1286
+ grainGroups.set(g.color, groups);
1287
+ }
1288
+ groups[alphaLevel(g.life / g.max * 1.5)].push(g);
1289
+ }
1290
+ grainGroups.forEach((groups, color) => {
1291
+ ctx.fillStyle = color;
1292
+ for (let lv = 1; lv <= ALPHA_STEPS; lv++) {
1293
+ const group = groups[lv];
1294
+ if (!group.length) continue;
1295
+ ctx.globalAlpha = lv / ALPHA_STEPS;
1296
+ ctx.beginPath();
1297
+ for (const g of group) ctx.rect(g.x - g.r / 2, g.y - g.r / 2, g.r, g.r);
1298
+ ctx.fill();
1299
+ group.length = 0;
1300
+ }
1301
+ });
1302
+ },
1303
+ burst(x, y) {
1304
+ toss(x, y, rand(0, TAU));
1305
+ if (Math.random() < 0.5) splat(x + rand(-40, 40), y + rand(-40, 40));
1306
+ }
1307
+ };
1308
+ };
1309
+
1310
+ // src/effects/kites.ts
1311
+ var PAPER = ["#e53935", "#fdd835", "#43a047", "#1e88e5", "#8e24aa", "#fb8c00", "#00acc1", "#f5f5f5", "#d81b60"];
1312
+ var PATTERNS = ["split", "band", "top", "eye", "quarter"];
1313
+ var kiteSprite = (look) => sprite(40, 50, (ctx) => {
1314
+ const [a, b, pattern] = look.split("|");
1315
+ ctx.translate(20, 22);
1316
+ const diamond = () => {
1317
+ ctx.beginPath();
1318
+ ctx.moveTo(0, -20);
1319
+ ctx.lineTo(18, 0);
1320
+ ctx.lineTo(0, 21);
1321
+ ctx.lineTo(-18, 0);
1322
+ ctx.closePath();
1323
+ };
1324
+ ctx.fillStyle = b;
1325
+ ctx.beginPath();
1326
+ ctx.moveTo(0, 20);
1327
+ ctx.lineTo(-4, 27);
1328
+ ctx.lineTo(0, 25);
1329
+ ctx.lineTo(4, 27);
1330
+ ctx.closePath();
1331
+ ctx.fill();
1332
+ diamond();
1333
+ ctx.fillStyle = a;
1334
+ ctx.fill();
1335
+ ctx.save();
1336
+ ctx.clip();
1337
+ ctx.fillStyle = b;
1338
+ switch (pattern) {
1339
+ case "split":
1340
+ ctx.fillRect(0, -22, 20, 44);
1341
+ break;
1342
+ case "band":
1343
+ ctx.fillRect(-20, -4, 40, 8);
1344
+ break;
1345
+ case "top":
1346
+ ctx.fillRect(-20, -22, 40, 22);
1347
+ break;
1348
+ case "eye":
1349
+ ctx.beginPath();
1350
+ ctx.arc(0, 0, 7, 0, TAU);
1351
+ ctx.fill();
1352
+ ctx.fillStyle = a;
1353
+ ctx.beginPath();
1354
+ ctx.arc(0, 0, 3, 0, TAU);
1355
+ ctx.fill();
1356
+ break;
1357
+ case "quarter":
1358
+ ctx.fillRect(0, -22, 20, 22);
1359
+ ctx.fillRect(-20, 0, 20, 22);
1360
+ break;
1361
+ }
1362
+ const g = ctx.createLinearGradient(-18, -20, 18, 20);
1363
+ g.addColorStop(0, "rgba(255,255,255,0.28)");
1364
+ g.addColorStop(0.5, "rgba(255,255,255,0)");
1365
+ g.addColorStop(1, "rgba(0,0,0,0.18)");
1366
+ ctx.fillStyle = g;
1367
+ ctx.fillRect(-20, -22, 40, 44);
1368
+ ctx.restore();
1369
+ ctx.strokeStyle = "rgba(70,40,15,0.75)";
1370
+ ctx.lineWidth = 1;
1371
+ ctx.beginPath();
1372
+ ctx.moveTo(0, -20);
1373
+ ctx.lineTo(0, 21);
1374
+ ctx.moveTo(-18, 0);
1375
+ ctx.quadraticCurveTo(0, -9, 18, 0);
1376
+ ctx.stroke();
1377
+ diamond();
1378
+ ctx.strokeStyle = "rgba(0,0,0,0.25)";
1379
+ ctx.stroke();
1380
+ });
1381
+ var kites = (o) => {
1382
+ let w = 0;
1383
+ let h = 0;
1384
+ let t = 0;
1385
+ const flock = [];
1386
+ const sprites = spriteCache((look) => kiteSprite(look));
1387
+ const palette = () => o.colors && o.colors.length >= 2 ? o.colors : PAPER;
1388
+ const make = (slot, of, state, delay = 0) => {
1389
+ const by = rand(h * 0.12, h * 0.5);
1390
+ const z = clamp(0.6 + by / h * 1.1, 0.6, 1.15);
1391
+ const bx = w * (slot + 0.5) / Math.max(1, of) + rand(-40, 40);
1392
+ const pal = palette();
1393
+ const a = pick(pal);
1394
+ let b = pick(pal);
1395
+ while (b === a && pal.length > 1) b = pick(pal);
1396
+ return {
1397
+ state,
1398
+ bx,
1399
+ by,
1400
+ x: bx,
1401
+ y: state === "launching" ? h + 60 : by,
1402
+ rot: 0,
1403
+ z,
1404
+ ax: clamp(bx + rand(-w * 0.25, w * 0.25) - o.wind * w * 0.2, -w * 0.2, w * 1.2),
1405
+ look: `${a}|${b}|${pick(PATTERNS)}`,
1406
+ seeds: Array.from({ length: 8 }, () => rand(0, TAU)),
1407
+ progress: -delay,
1408
+ dive: 0,
1409
+ diveDir: 1,
1410
+ vx: 0,
1411
+ vy: 0,
1412
+ spin: 0
1413
+ };
1414
+ };
1415
+ const cut = (k) => {
1416
+ k.state = "cut";
1417
+ k.vx = o.wind * 40 + rand(20, 45) * (Math.random() < 0.5 ? -1 : 1);
1418
+ k.vy = rand(4, 16);
1419
+ k.spin = rand(-0.8, 0.8);
1420
+ };
1421
+ const target = () => countFor(w, h, 5, o.density);
1422
+ return {
1423
+ resize(nw, nh) {
1424
+ w = nw;
1425
+ h = nh;
1426
+ const n = target();
1427
+ while (flock.length < n) flock.push(make(flock.length, n, "flying"));
1428
+ flock.length = n;
1429
+ },
1430
+ update(dt) {
1431
+ const s = dt * o.speed;
1432
+ t += s;
1433
+ const n = flock.length;
1434
+ flock.forEach((k, i) => {
1435
+ const [p1, p2, p3, p4, f1, f2, f3, f4] = k.seeds;
1436
+ if (k.state === "cut") {
1437
+ k.vx += (o.wind * 50 - k.vx * 0.2) * s;
1438
+ k.x += k.vx * s;
1439
+ k.y += k.vy * s;
1440
+ k.rot = Math.sin(t * 3 + p1) * 0.6 + k.spin * t;
1441
+ if (k.y > h + 80 || k.x < -120 || k.x > w + 120) flock[i] = make(i, n, "launching", rand(1.5, 4));
1442
+ return;
1443
+ }
1444
+ let hx = k.bx + (Math.sin(t * (0.3 + f1 * 0.05) + p1) * 30 + Math.sin(t * (0.9 + f2 * 0.05) + p2) * 10) * k.z;
1445
+ let hy = k.by + Math.sin(t * (0.5 + f3 * 0.05) + p3) * 16 * k.z;
1446
+ let rot = Math.sin(t * (1.3 + f4 * 0.1) + p4) * 0.22 + o.wind * 0.3;
1447
+ if (k.dive > 0) {
1448
+ k.dive -= s;
1449
+ const theta = k.diveDir * TAU * (1 - k.dive / 1.6);
1450
+ const R = 45 * k.z;
1451
+ hx += Math.sin(theta) * R;
1452
+ hy += (1 - Math.cos(theta)) * R;
1453
+ rot += theta;
1454
+ } else if (k.state === "flying" && Math.random() < 0.035 * s) {
1455
+ k.dive = 1.6;
1456
+ k.diveDir = Math.random() < 0.5 ? -1 : 1;
1457
+ }
1458
+ if (k.state === "launching") {
1459
+ k.progress += s / 3.5;
1460
+ if (k.progress < 0) {
1461
+ k.y = h + 60;
1462
+ return;
1463
+ }
1464
+ const e = 1 - Math.pow(1 - Math.min(1, k.progress), 3);
1465
+ k.x = k.ax + (hx - k.ax) * e;
1466
+ k.y = h + 60 + (hy - h - 60) * e;
1467
+ k.rot = rot * e + Math.sin(t * 8) * 0.3 * (1 - e);
1468
+ if (k.progress >= 1) k.state = "flying";
1469
+ return;
1470
+ }
1471
+ k.x = hx;
1472
+ k.y = hy;
1473
+ k.rot = rot;
1474
+ if (o.density > 0 && Math.random() < 0.012 * o.density * s) cut(k);
1475
+ });
1476
+ },
1477
+ draw(ctx) {
1478
+ const m = baseTransform(ctx);
1479
+ const size = 46 * o.size;
1480
+ ctx.lineCap = "round";
1481
+ for (const k of flock) {
1482
+ if (k.state === "launching" && k.progress < 0) continue;
1483
+ const d = size * k.z;
1484
+ const bx = k.x - Math.sin(k.rot) * d * 0.1;
1485
+ const by = k.y + Math.cos(k.rot) * d * 0.1;
1486
+ ctx.globalAlpha = 0.55;
1487
+ ctx.strokeStyle = "#8f8576";
1488
+ ctx.lineWidth = 0.8;
1489
+ ctx.beginPath();
1490
+ if (k.state === "cut") {
1491
+ ctx.moveTo(bx, by);
1492
+ for (let i = 1; i <= 8; i++) {
1493
+ ctx.lineTo(bx + Math.sin(t * 4 + i * 0.8) * 4 * (i / 8) - k.vx * 0.04 * i, by + i * 9);
1494
+ }
1495
+ } else {
1496
+ const ay = h + 60;
1497
+ const cx = (bx + k.ax) / 2 + o.wind * 40;
1498
+ const cy = (by + ay) / 2 + 50 * k.z;
1499
+ ctx.moveTo(bx, by);
1500
+ ctx.quadraticCurveTo(cx, cy, k.ax, ay);
1501
+ }
1502
+ ctx.stroke();
1503
+ place(ctx, m, k.x, k.y, k.rot);
1504
+ ctx.globalAlpha = 1;
1505
+ const img = sprites(k.look);
1506
+ if (img) ctx.drawImage(img, -d * 0.5, -d * 0.55, d, d * 1.25);
1507
+ resetTransform(ctx, m);
1508
+ }
1509
+ },
1510
+ burst(x, y) {
1511
+ let best = null;
1512
+ let bd = 140 * o.size;
1513
+ for (const k of flock) {
1514
+ if (k.state === "cut") continue;
1515
+ const d = Math.hypot(k.x - x, k.y - y);
1516
+ if (d < bd) {
1517
+ bd = d;
1518
+ best = k;
1519
+ }
1520
+ }
1521
+ if (best) cut(best);
1522
+ else if (flock.length < Math.max(2, target() * 2)) {
1523
+ const k = make(flock.length, Math.max(1, flock.length), "launching");
1524
+ k.bx = x;
1525
+ k.by = clamp(y, h * 0.1, h * 0.6);
1526
+ k.ax = x + rand(-60, 60);
1527
+ flock.push(k);
1528
+ }
1529
+ }
1530
+ };
1531
+ };
1532
+
1533
+ // src/effects/litter.ts
1534
+ var Litter = class {
1535
+ constructor(max) {
1536
+ this.max = max;
1537
+ this.pieces = [];
1538
+ }
1539
+ /** Drop something at x on the ground line `h`. `d` is its sprite diameter. */
1540
+ add(x, h, d, img, squash = 0.45) {
1541
+ if (this.pieces.length >= this.max) return;
1542
+ this.pieces.push({ x, y: h - rand(1, 3), d, rot: rand(0, Math.PI * 2), squash, img, life: rand(6, 11) });
1543
+ }
1544
+ update(s, enabled) {
1545
+ if (!enabled) {
1546
+ this.pieces.length = 0;
1547
+ return;
1548
+ }
1549
+ retain(this.pieces, (p) => (p.life -= s) > 0);
1550
+ }
1551
+ draw(ctx, m) {
1552
+ for (const p of this.pieces) {
1553
+ ctx.globalAlpha = Math.min(1, p.life / 1.5) * 0.9;
1554
+ placeFlat(ctx, m, p.x, p.y, p.rot, 1, p.squash);
1555
+ stamp(ctx, p.img, 0, 0, p.d);
1556
+ }
1557
+ if (this.pieces.length) resetTransform(ctx, m);
1558
+ }
1559
+ };
1560
+
1561
+ // src/effects/marigold.ts
1562
+ var MARIGOLD = ["#ff8c00", "#ffa600", "#ffc300", "#ff7400", "#ffb300"];
1563
+ var LEAF = "#3f8f2b";
1564
+ var MAX_EXTRA = 600;
1565
+ var flowerSprite = (color) => sprite(48, 48, (ctx) => {
1566
+ ctx.translate(24, 24);
1567
+ const rings = [
1568
+ { r: 14, n: 18, rx: 4.6, ry: 7.5, shade: "rgba(90,30,0,0.22)" },
1569
+ { r: 9, n: 15, rx: 4, ry: 6, shade: "rgba(90,30,0,0.08)" },
1570
+ { r: 4.5, n: 11, rx: 3.2, ry: 4.5, shade: "rgba(255,255,200,0.15)" }
1571
+ ];
1572
+ for (const ring of rings) {
1573
+ for (let i = 0; i < ring.n; i++) {
1574
+ const a = i / ring.n * TAU + rand(-0.12, 0.12);
1575
+ ctx.save();
1576
+ ctx.rotate(a);
1577
+ ctx.translate(0, -ring.r);
1578
+ ctx.beginPath();
1579
+ ctx.ellipse(0, 0, ring.rx, ring.ry, 0, 0, TAU);
1580
+ ctx.fillStyle = color;
1581
+ ctx.fill();
1582
+ ctx.fillStyle = ring.shade;
1583
+ ctx.fill();
1584
+ ctx.strokeStyle = "rgba(120,40,0,0.35)";
1585
+ ctx.lineWidth = 0.6;
1586
+ ctx.stroke();
1587
+ ctx.restore();
1588
+ }
1589
+ }
1590
+ ctx.fillStyle = "rgba(150,60,0,0.55)";
1591
+ ctx.beginPath();
1592
+ ctx.arc(0, 0, 2.4, 0, TAU);
1593
+ ctx.fill();
1594
+ });
1595
+ var petalSprite = (color) => sprite(16, 16, (ctx) => {
1596
+ ctx.translate(8, 8);
1597
+ ctx.beginPath();
1598
+ ctx.moveTo(0, 6.5);
1599
+ ctx.bezierCurveTo(5, 4, 5, -3, 3.5, -5);
1600
+ ctx.lineTo(2, -4);
1601
+ ctx.lineTo(1, -6);
1602
+ ctx.lineTo(0, -4.5);
1603
+ ctx.lineTo(-1, -6);
1604
+ ctx.lineTo(-2, -4);
1605
+ ctx.lineTo(-3.5, -5);
1606
+ ctx.bezierCurveTo(-5, -3, -5, 4, 0, 6.5);
1607
+ ctx.closePath();
1608
+ ctx.fillStyle = color;
1609
+ ctx.fill();
1610
+ const g = ctx.createLinearGradient(0, -6, 0, 6.5);
1611
+ g.addColorStop(0, "rgba(255,255,220,0.25)");
1612
+ g.addColorStop(1, "rgba(120,40,0,0.3)");
1613
+ ctx.fillStyle = g;
1614
+ ctx.fill();
1615
+ });
1616
+ var leafSprite = () => sprite(16, 28, (ctx) => {
1617
+ ctx.translate(8, 14);
1618
+ ctx.beginPath();
1619
+ ctx.moveTo(0, -13);
1620
+ ctx.bezierCurveTo(7, -6, 6, 8, 0, 13);
1621
+ ctx.bezierCurveTo(-6, 8, -7, -6, 0, -13);
1622
+ ctx.fillStyle = LEAF;
1623
+ ctx.fill();
1624
+ ctx.strokeStyle = "rgba(200,255,180,0.4)";
1625
+ ctx.lineWidth = 0.7;
1626
+ ctx.beginPath();
1627
+ ctx.moveTo(0, -12);
1628
+ ctx.lineTo(0, 12);
1629
+ ctx.stroke();
1630
+ });
1631
+ var marigold = (o) => {
1632
+ let w = 0;
1633
+ let h = 0;
1634
+ let t = 0;
1635
+ const bits = [];
1636
+ const litter = new Litter(80);
1637
+ let recycled = 0;
1638
+ const flowers = spriteCache((c) => flowerSprite(c));
1639
+ const petals = spriteCache((c) => petalSprite(c));
1640
+ const leaf = spriteCache(() => leafSprite());
1641
+ const palette = () => paletteOr(o.colors, MARIGOLD);
1642
+ const make = (extra, x = rand(0, w), y, vx = 0, vy = 0, forceFlower = false) => {
1643
+ const flower = forceFlower || !extra && Math.random() < 0.18;
1644
+ const z = rand(0.6, 1.2);
1645
+ const color = pick(palette());
1646
+ const s = (flower ? rand(9, 13) * (forceFlower ? 1.7 : 1) : rand(5, 8)) * o.size * z;
1647
+ return {
1648
+ x,
1649
+ y: y != null ? y : rand(-80, -10),
1650
+ vx,
1651
+ vy,
1652
+ s,
1653
+ z,
1654
+ rot: rand(0, TAU),
1655
+ vrot: rand(-2, 2),
1656
+ flip: rand(0, TAU),
1657
+ vflip: flower ? rand(0.5, 1.5) : rand(2, 5),
1658
+ sway: rand(15, 45) * z,
1659
+ phase: rand(0, TAU),
1660
+ color,
1661
+ flower,
1662
+ extra,
1663
+ img: flower ? flowers(color) : petals(color),
1664
+ d: flower ? s * 2.4 : s * 2.2
1665
+ };
1666
+ };
1667
+ return {
1668
+ resize(nw, nh) {
1669
+ w = nw;
1670
+ h = nh;
1671
+ const want = countFor(w, h, 55, o.density);
1672
+ for (; recycled < want; recycled++) bits.push(make(false, rand(0, w), rand(-h * 0.2, h)));
1673
+ if (recycled > want) {
1674
+ let drop = recycled - want;
1675
+ retain(bits, (b) => b.extra || drop-- <= 0);
1676
+ recycled = want;
1677
+ }
1678
+ },
1679
+ update(dt) {
1680
+ const s = dt * o.speed;
1681
+ t += s;
1682
+ const k = drag(0.97, s);
1683
+ retain(bits, (b) => {
1684
+ const terminal = (b.flower ? 75 : 42) * b.z;
1685
+ b.vy += (terminal - b.vy) * Math.min(1, 1.6 * s);
1686
+ b.vx *= k;
1687
+ b.phase += 1.3 * s;
1688
+ b.rot += b.vrot * s;
1689
+ b.flip += b.vflip * s;
1690
+ b.x += (b.vx + Math.sin(b.phase) * b.sway + o.wind * 70 * b.z) * s;
1691
+ b.y += b.vy * s;
1692
+ const landed = o.ground && b.y >= h - b.s * 0.3;
1693
+ if (landed || b.y - b.s * 2 > h) {
1694
+ if (landed) litter.add(b.x, h, b.d, b.img, b.flower ? 0.75 : 0.5);
1695
+ if (b.extra) return false;
1696
+ Object.assign(b, make(false));
1697
+ return true;
1698
+ }
1699
+ if (!b.extra) {
1700
+ if (b.x < -30) b.x += w + 60;
1701
+ else if (b.x > w + 30) b.x -= w + 60;
1702
+ }
1703
+ return true;
1704
+ });
1705
+ litter.update(s, o.ground);
1706
+ },
1707
+ draw(ctx) {
1708
+ const m = baseTransform(ctx);
1709
+ litter.draw(ctx, m);
1710
+ for (const b of bits) {
1711
+ place(ctx, m, b.x, b.y, b.rot, b.flower ? 0.8 + 0.2 * Math.cos(b.flip) : Math.cos(b.flip), 1);
1712
+ ctx.globalAlpha = 0.65 + 0.35 * Math.min(1, b.z);
1713
+ stamp(ctx, b.img, 0, 0, b.d);
1714
+ }
1715
+ resetTransform(ctx, m);
1716
+ if (!o.ground || w < 120) return;
1717
+ const pal = palette();
1718
+ const fs = 15 * o.size;
1719
+ const swags = Math.max(1, Math.round(w / (240 * o.size)));
1720
+ const span = w / swags;
1721
+ const step = fs * 0.78;
1722
+ ctx.globalAlpha = 1;
1723
+ for (let i = 0; i < swags; i++) {
1724
+ const x0 = i * span;
1725
+ const sag = 46 * o.size * (1 + 0.05 * Math.sin(t * 1.3 + i));
1726
+ const swing = Math.sin(t * 1.1 + i * 1.7) * 3;
1727
+ const n = Math.floor(span / step);
1728
+ for (let j = 0; j <= n; j++) {
1729
+ const u = j / n;
1730
+ const bow = 4 * u * (1 - u);
1731
+ const color = pal[(Math.floor(j / 3) + i) % 2 === 0 ? 0 : Math.min(2, pal.length - 1)];
1732
+ place(ctx, m, x0 + u * span + swing * bow, 4 * o.size + sag * bow, j * 1.7);
1733
+ stamp(ctx, flowers(color), 0, 0, fs * 1.6);
1734
+ }
1735
+ }
1736
+ for (let i = 0; i <= swags; i++) {
1737
+ const ax = Math.min(w - 6, Math.max(6, i * span));
1738
+ const angle = Math.sin(t * 1.7 + i * 2.3) * 0.12;
1739
+ const dx = -Math.sin(angle);
1740
+ const dy = Math.cos(angle);
1741
+ for (let j = 0; j < 5; j++) {
1742
+ const along2 = 6 * o.size + j * fs * 0.75;
1743
+ place(ctx, m, ax + dx * along2, 2 + dy * along2, angle + j * 2.1);
1744
+ stamp(ctx, flowers(pal[j % 2 === 0 ? 0 : Math.min(2, pal.length - 1)]), 0, 0, fs * 1.5);
1745
+ }
1746
+ const along = 6 * o.size + 5 * fs * 0.75 + 6 * o.size;
1747
+ place(ctx, m, ax + dx * along, 2 + dy * along, angle);
1748
+ const lg = leaf("");
1749
+ if (lg) ctx.drawImage(lg, -6 * o.size, -10.5 * o.size, 12 * o.size, 21 * o.size);
1750
+ }
1751
+ resetTransform(ctx, m);
1752
+ },
1753
+ burst(x, y) {
1754
+ const n = Math.min(50, MAX_EXTRA - (bits.length - recycled));
1755
+ for (let i = 0; i < n; i++) {
1756
+ const a = -Math.PI / 2 + rand(-1.2, 1.2);
1757
+ const v = rand(140, 420);
1758
+ bits.push(make(true, x, y, Math.cos(a) * v, Math.sin(a) * v, i < 4));
1759
+ }
1760
+ }
1761
+ };
1762
+ };
1763
+
1764
+ // src/effects/stars.ts
1765
+ var TINTS = ["#ffffff", "#ffffff", "#ffffff", "#dfe8ff", "#cfdcff", "#fff1d6", "#ffe2b8"];
1766
+ var stars = (o) => {
1767
+ let w = 0;
1768
+ let h = 0;
1769
+ let t = 0;
1770
+ let nextMeteor = rand(1, 3);
1771
+ const field = [];
1772
+ const meteors = [];
1773
+ const glow = spriteCache((c) => glowSprite(c, true, 0.25));
1774
+ const palette = () => paletteOr(o.colors, TINTS);
1775
+ const make = () => {
1776
+ const bright = Math.random() < 0.06;
1777
+ return {
1778
+ x: rand(0, w),
1779
+ // Denser toward the top, thinning toward the horizon
1780
+ y: h * Math.pow(Math.random(), 1.4),
1781
+ r: (bright ? rand(1.4, 2.2) : rand(0.4, 1.3)) * o.size,
1782
+ base: bright ? rand(0.8, 1) : rand(0.25, 0.75),
1783
+ freq: rand(0.6, 3),
1784
+ phase: rand(0, TAU),
1785
+ color: pick(palette()),
1786
+ spikes: bright
1787
+ };
1788
+ };
1789
+ const meteor = (x = rand(w * 0.1, w * 0.9), y = rand(0, h * 0.35)) => {
1790
+ const dir = o.wind !== 0 ? Math.sign(o.wind) : Math.random() < 0.5 ? -1 : 1;
1791
+ const a = rand(0.35, 0.7);
1792
+ const v = rand(650, 950);
1793
+ const max = rand(0.6, 1.1);
1794
+ meteors.push({ x, y, vx: Math.cos(a) * v * dir, vy: Math.sin(a) * v, len: rand(110, 200) * o.size, life: max, max });
1795
+ };
1796
+ return {
1797
+ resize(nw, nh) {
1798
+ const rescale = w > 0 && h > 0;
1799
+ for (const s of field) {
1800
+ if (rescale) {
1801
+ s.x *= nw / w;
1802
+ s.y *= nh / h;
1803
+ }
1804
+ }
1805
+ w = nw;
1806
+ h = nh;
1807
+ fillPool(field, countFor(w, h, 170, o.density), make);
1808
+ },
1809
+ update(dt) {
1810
+ const s = dt * o.speed;
1811
+ t += s;
1812
+ if (o.density > 0) {
1813
+ nextMeteor -= s;
1814
+ if (nextMeteor <= 0) {
1815
+ meteor();
1816
+ nextMeteor = rand(2.5, 7) / o.density;
1817
+ }
1818
+ }
1819
+ retain(meteors, (m) => {
1820
+ m.x += m.vx * s;
1821
+ m.y += m.vy * s;
1822
+ return (m.life -= s) > 0;
1823
+ });
1824
+ },
1825
+ draw(ctx) {
1826
+ ctx.globalCompositeOperation = "lighter";
1827
+ for (const s of field) {
1828
+ const tw = 0.65 + 0.25 * Math.sin(t * s.freq + s.phase) + 0.1 * Math.sin(t * s.freq * 3.7 + s.phase);
1829
+ const a = s.base * tw;
1830
+ ctx.globalAlpha = a;
1831
+ stamp(ctx, glow(s.color), s.x, s.y, s.r * 4);
1832
+ if (s.spikes) {
1833
+ const L = s.r * 6 * tw;
1834
+ ctx.globalAlpha = a * 0.5;
1835
+ ctx.strokeStyle = s.color;
1836
+ ctx.lineWidth = 0.7;
1837
+ ctx.beginPath();
1838
+ ctx.moveTo(s.x - L, s.y);
1839
+ ctx.lineTo(s.x + L, s.y);
1840
+ ctx.moveTo(s.x, s.y - L);
1841
+ ctx.lineTo(s.x, s.y + L);
1842
+ ctx.stroke();
1843
+ }
1844
+ }
1845
+ ctx.lineCap = "round";
1846
+ for (const m of meteors) {
1847
+ const life = m.life / m.max;
1848
+ const a = Math.min(1, (1 - life) * 6) * Math.min(1, life * 2.5);
1849
+ const sp = Math.hypot(m.vx, m.vy);
1850
+ const tx = m.x - m.vx / sp * m.len;
1851
+ const ty = m.y - m.vy / sp * m.len;
1852
+ const g = ctx.createLinearGradient(tx, ty, m.x, m.y);
1853
+ g.addColorStop(0, "rgba(255,255,255,0)");
1854
+ g.addColorStop(0.7, "rgba(200,220,255,0.35)");
1855
+ g.addColorStop(1, "rgba(255,255,255,1)");
1856
+ ctx.globalAlpha = a;
1857
+ ctx.strokeStyle = g;
1858
+ ctx.lineWidth = 1.8 * o.size;
1859
+ ctx.beginPath();
1860
+ ctx.moveTo(tx, ty);
1861
+ ctx.lineTo(m.x, m.y);
1862
+ ctx.stroke();
1863
+ stamp(ctx, glow("#cfe0ff"), m.x, m.y, 12 * o.size);
1864
+ }
1865
+ },
1866
+ burst(x, y) {
1867
+ meteor(x, y);
1868
+ }
1869
+ };
1870
+ };
1871
+
1872
+ // src/effects/leaves.ts
1873
+ var AUTUMN = ["#d2691e", "#e8a33d", "#b5452b", "#8b4513", "#f4c430", "#a0522d", "#c0392b", "#9c6b1f"];
1874
+ var SAKURA = ["#ffd1dc", "#ffb7c5", "#fcc9d3", "#f8a5c2", "#fff0f5"];
1875
+ var MAPLE = [
1876
+ [0, -20],
1877
+ [3, -11],
1878
+ [9, -15],
1879
+ [8, -7],
1880
+ [17, -10],
1881
+ [14, -3],
1882
+ [19, 1],
1883
+ [11, 2],
1884
+ [13, 9],
1885
+ [5, 6],
1886
+ [2, 10],
1887
+ [0, 11]
1888
+ ];
1889
+ var OAK = [
1890
+ [0, -20],
1891
+ [6, -17],
1892
+ [4, -12],
1893
+ [10, -9],
1894
+ [6, -4],
1895
+ [11, 0],
1896
+ [6, 4],
1897
+ [9, 9],
1898
+ [3, 11],
1899
+ [0, 14]
1900
+ ];
1901
+ var mirrored = (half) => [...half, ...half.slice(0, -1).reverse().map(([x, y]) => [-x, y])];
1902
+ var outline = (ctx, shape) => {
1903
+ ctx.beginPath();
1904
+ if (shape === "maple") {
1905
+ for (const [x, y] of mirrored(MAPLE)) ctx.lineTo(x, y);
1906
+ } else if (shape === "oak") {
1907
+ const pts = mirrored(OAK);
1908
+ ctx.moveTo(pts[0][0], pts[0][1]);
1909
+ for (let i = 1; i < pts.length - 1; i++) {
1910
+ const [x, y] = pts[i];
1911
+ const [nx, ny] = pts[i + 1];
1912
+ ctx.quadraticCurveTo(x, y, (x + nx) / 2, (y + ny) / 2);
1913
+ }
1914
+ } else if (shape === "beech") {
1915
+ ctx.moveTo(0, -20);
1916
+ ctx.bezierCurveTo(13, -10, 11, 8, 0, 15);
1917
+ ctx.bezierCurveTo(-11, 8, -13, -10, 0, -20);
1918
+ } else {
1919
+ ctx.moveTo(0, 14);
1920
+ ctx.bezierCurveTo(15, 6, 12, -15, 3.5, -15);
1921
+ ctx.lineTo(0, -10);
1922
+ ctx.lineTo(-3.5, -15);
1923
+ ctx.bezierCurveTo(-12, -15, -15, 6, 0, 14);
1924
+ }
1925
+ ctx.closePath();
1926
+ };
1927
+ var leafSprite2 = (shape, color, back) => sprite(48, 48, (ctx) => {
1928
+ ctx.translate(24, 24);
1929
+ const petal = shape === "petal";
1930
+ if (!petal) {
1931
+ ctx.strokeStyle = "#5a3216";
1932
+ ctx.lineWidth = 1.3;
1933
+ ctx.lineCap = "round";
1934
+ ctx.beginPath();
1935
+ ctx.moveTo(0, 8);
1936
+ ctx.quadraticCurveTo(1, 16, 3, 21);
1937
+ ctx.stroke();
1938
+ }
1939
+ outline(ctx, shape);
1940
+ ctx.fillStyle = color;
1941
+ ctx.fill();
1942
+ ctx.save();
1943
+ ctx.clip();
1944
+ const g = ctx.createLinearGradient(-15, -15, 15, 15);
1945
+ if (petal) {
1946
+ g.addColorStop(0, "rgba(255,255,255,0.35)");
1947
+ g.addColorStop(1, "rgba(255,255,255,0)");
1948
+ } else {
1949
+ g.addColorStop(0, "rgba(255,240,200,0.25)");
1950
+ g.addColorStop(1, "rgba(40,10,0,0.3)");
1951
+ }
1952
+ ctx.fillStyle = g;
1953
+ ctx.fillRect(-24, -24, 48, 48);
1954
+ if (petal) {
1955
+ const b = ctx.createRadialGradient(0, 14, 0, 0, 14, 14);
1956
+ b.addColorStop(0, "rgba(230,80,120,0.45)");
1957
+ b.addColorStop(1, "rgba(230,80,120,0)");
1958
+ ctx.fillStyle = b;
1959
+ ctx.fillRect(-24, -24, 48, 48);
1960
+ }
1961
+ ctx.strokeStyle = petal ? "rgba(200,90,120,0.25)" : "rgba(60,25,5,0.4)";
1962
+ ctx.lineWidth = petal ? 0.6 : 0.8;
1963
+ ctx.beginPath();
1964
+ if (shape === "maple") {
1965
+ for (const [x, y] of [[0, -18], [16, -9], [-16, -9], [17, 1], [-17, 1]]) {
1966
+ ctx.moveTo(0, 9);
1967
+ ctx.lineTo(x * 0.85, y * 0.85);
1968
+ }
1969
+ } else if (petal) {
1970
+ for (const x of [-6, -2, 2, 6]) {
1971
+ ctx.moveTo(0, 13);
1972
+ ctx.quadraticCurveTo(x * 0.6, 0, x, -10);
1973
+ }
1974
+ } else {
1975
+ ctx.moveTo(0, -18);
1976
+ ctx.lineTo(0, 13);
1977
+ for (let i = 0; i < 5; i++) {
1978
+ const y = -12 + i * 5.5;
1979
+ ctx.moveTo(0, y + 4);
1980
+ ctx.lineTo(8, y - 2);
1981
+ ctx.moveTo(0, y + 4);
1982
+ ctx.lineTo(-8, y - 2);
1983
+ }
1984
+ }
1985
+ ctx.stroke();
1986
+ if (back) {
1987
+ ctx.fillStyle = "rgba(255,255,255,0.2)";
1988
+ ctx.fillRect(-24, -24, 48, 48);
1989
+ }
1990
+ ctx.restore();
1991
+ });
1992
+ var makeLeaves = (kind) => (o) => {
1993
+ const petals = kind === "sakura";
1994
+ let w = 0;
1995
+ let h = 0;
1996
+ let t = rand(0, 100);
1997
+ const pool = [];
1998
+ const litter = new Litter(60);
1999
+ const sprites = spriteCache((key) => {
2000
+ const [shape, color, back] = key.split("|");
2001
+ return leafSprite2(shape, color, back === "1");
2002
+ });
2003
+ const palette = () => paletteOr(o.colors, petals ? SAKURA : AUTUMN);
2004
+ const make = (anywhere, z = rand(0.55, 1.25)) => {
2005
+ const s = (petals ? rand(6, 9) : rand(11, 17)) * o.size * z;
2006
+ const color = pick(palette());
2007
+ const shape = petals ? "petal" : pick(["maple", "maple", "oak", "beech"]);
2008
+ return {
2009
+ x: rand(-20, w + 20),
2010
+ y: anywhere ? rand(-h * 0.2, h) : rand(-80, -s * 2),
2011
+ vx: 0,
2012
+ s,
2013
+ z,
2014
+ vy: (petals ? rand(28, 50) : rand(38, 70)) * z,
2015
+ rot: rand(0, TAU),
2016
+ vrot: rand(-1.4, 1.4),
2017
+ flip: rand(0, TAU),
2018
+ vflip: rand(1.5, 3.8) * (petals ? 1.3 : 1),
2019
+ sway: rand(25, 65) * z,
2020
+ phase: rand(0, TAU),
2021
+ front: sprites(`${shape}|${color}|0`),
2022
+ back: sprites(`${shape}|${color}|1`)
2023
+ };
2024
+ };
2025
+ const sortByDepth = () => pool.sort((a, b) => a.z - b.z);
2026
+ return {
2027
+ resize(nw, nh) {
2028
+ w = nw;
2029
+ h = nh;
2030
+ const n = countFor(w, h, petals ? 70 : 35, o.density);
2031
+ while (pool.length < n) pool.push(make(true));
2032
+ pool.length = n;
2033
+ sortByDepth();
2034
+ },
2035
+ update(dt) {
2036
+ const s = dt * o.speed;
2037
+ t += s;
2038
+ const g = gust(t, 1.7);
2039
+ const wind = o.wind + g * 0.45;
2040
+ const lift = g > 0.45 ? (g - 0.45) * 1.4 : 0;
2041
+ const k = drag(0.97, s);
2042
+ for (let i = 0; i < pool.length; i++) {
2043
+ const l = pool[i];
2044
+ l.phase += 1.1 * s;
2045
+ l.flip += l.vflip * (1 + lift * 2) * s;
2046
+ l.rot += l.vrot * (1 + lift) * s;
2047
+ l.vx *= k;
2048
+ l.y += l.vy * (1 - lift * 0.8) * s;
2049
+ l.x += (Math.sin(l.phase) * l.sway + wind * 90 * l.z + l.vx) * s;
2050
+ if (o.ground && l.y >= h - l.s * 0.3) {
2051
+ litter.add(l.x, h, l.s * 2.4, l.front);
2052
+ pool[i] = make(false, l.z);
2053
+ } else if (l.y - l.s * 2 > h) {
2054
+ pool[i] = make(false, l.z);
2055
+ }
2056
+ const m = l.s * 3;
2057
+ if (pool[i].x < -m) pool[i].x += w + m * 2;
2058
+ else if (pool[i].x > w + m) pool[i].x -= w + m * 2;
2059
+ }
2060
+ litter.update(s, o.ground);
2061
+ },
2062
+ draw(ctx) {
2063
+ const m = baseTransform(ctx);
2064
+ litter.draw(ctx, m);
2065
+ for (const l of pool) {
2066
+ const sx = Math.cos(l.flip);
2067
+ place(ctx, m, l.x, l.y, l.rot, sx, 1);
2068
+ ctx.globalAlpha = 0.5 + 0.45 * Math.min(1, l.z);
2069
+ stamp(ctx, sx < 0 ? l.back : l.front, 0, 0, l.s * 2.4);
2070
+ }
2071
+ resetTransform(ctx, m);
2072
+ },
2073
+ burst(x, y) {
2074
+ for (const l of pool) {
2075
+ const dx = l.x - x;
2076
+ const dy = l.y - y;
2077
+ const d = Math.hypot(dx, dy);
2078
+ if (d < 200 && d > 0) {
2079
+ const f = 1 - d / 200;
2080
+ l.vx += dx / d * 400 * f;
2081
+ l.y -= 30 * f;
2082
+ l.vflip = Math.min(l.vflip + 4 * f, 12);
2083
+ }
2084
+ }
2085
+ }
2086
+ };
2087
+ };
2088
+ var leaves = makeLeaves("leaves");
2089
+ var sakura = makeLeaves("sakura");
2090
+
2091
+ // src/effects/rain.ts
2092
+ var LAYERS = [
2093
+ { share: 0.55, len: [6, 11], width: 0.7, alpha: 0.16, fall: 0.75 },
2094
+ { share: 0.45, len: [16, 28], width: 1.3, alpha: 0.42, fall: 1.15 }
2095
+ ];
2096
+ var GRAVITY = 980;
2097
+ var MAX_SPLASH = 900;
2098
+ var flashCurve = (t) => {
2099
+ if (t < 0.07) return 1;
2100
+ if (t < 0.13) return 0.25;
2101
+ if (t < 0.21) return 0.85;
2102
+ return Math.max(0, 0.85 * Math.exp(-(t - 0.21) * 7));
2103
+ };
2104
+ var makeRain = (storm2) => (o) => {
2105
+ let w = 0;
2106
+ let h = 0;
2107
+ let t = rand(0, 100);
2108
+ const layers = [[], []];
2109
+ const droplets = [];
2110
+ const ripples = [];
2111
+ const bolts = [];
2112
+ let nextStrike = rand(1.5, 4);
2113
+ const slant = () => (o.wind + (storm2 ? 0.35 + gust(t) * 0.3 : gust(t) * 0.1)) * 0.35;
2114
+ const make = (layer, anywhere) => {
2115
+ const L = LAYERS[layer];
2116
+ const spread = Math.abs(slant()) * h * 1.2;
2117
+ const sl = slant();
2118
+ return {
2119
+ x: rand(sl > 0 ? -spread : 0, sl < 0 ? w + spread : w),
2120
+ y: anywhere ? rand(-h, h) : rand(-h * 0.25, -20),
2121
+ len: rand(L.len[0], L.len[1]) * o.size * (storm2 ? 1.25 : 1),
2122
+ vy: rand(700, 980) * L.fall,
2123
+ // Far drops "land" somewhere in the distance, near ones on the bottom edge.
2124
+ ground: layer === 0 ? rand(h * 0.6, h) : h - rand(0, Math.min(36, h * 0.07)),
2125
+ bright: Math.random() < 0.3
2126
+ };
2127
+ };
2128
+ const splash = (x, y, power = 1) => {
2129
+ if (droplets.length < MAX_SPLASH) {
2130
+ const n = 3 + (Math.random() * 4 | 0);
2131
+ for (let i = 0; i < n; i++) {
2132
+ const a = -Math.PI / 2 + rand(-1.1, 1.1);
2133
+ const v = rand(90, 230) * power * Math.sqrt(o.size);
2134
+ const max = rand(0.25, 0.5);
2135
+ droplets.push({
2136
+ x,
2137
+ y,
2138
+ vx: Math.cos(a) * v + slant() * 120,
2139
+ vy: Math.sin(a) * v,
2140
+ life: max,
2141
+ max,
2142
+ r: rand(0.7, 1.5) * o.size
2143
+ });
2144
+ }
2145
+ }
2146
+ if (ripples.length < MAX_SPLASH / 2) ripples.push({ x, y, max: rand(6, 14) * o.size * power, life: 1 });
2147
+ };
2148
+ const strike = (x = rand(w * 0.1, w * 0.9)) => {
2149
+ const trunk = [x, -10];
2150
+ const paths = [trunk];
2151
+ const end = rand(h * 0.55, h * 0.95);
2152
+ let cx = x;
2153
+ let cy = -10;
2154
+ while (cy < end) {
2155
+ cx += rand(-28, 28);
2156
+ cy += rand(14, 34);
2157
+ trunk.push(cx, cy);
2158
+ if (Math.random() < 0.22 && paths.length < 7) {
2159
+ const branch = [cx, cy];
2160
+ let bx = cx;
2161
+ let by = cy;
2162
+ const dir = Math.random() < 0.5 ? -1 : 1;
2163
+ const segs = 3 + (Math.random() * 6 | 0);
2164
+ for (let i = 0; i < segs; i++) {
2165
+ bx += dir * rand(6, 26);
2166
+ by += rand(8, 24);
2167
+ branch.push(bx, by);
2168
+ }
2169
+ paths.push(branch);
2170
+ }
2171
+ }
2172
+ bolts.push({ paths, age: 0 });
2173
+ };
2174
+ const rippleGroups = Array.from({ length: ALPHA_STEPS + 1 }, () => []);
2175
+ const dropletGroups = Array.from({ length: ALPHA_STEPS + 1 }, () => []);
2176
+ return {
2177
+ resize(nw, nh) {
2178
+ w = nw;
2179
+ h = nh;
2180
+ const total = countFor(w, h, 240, o.density * (storm2 ? 1.7 : 1));
2181
+ LAYERS.forEach((L, i) => {
2182
+ for (const d of layers[i]) d.ground = make(i, false).ground;
2183
+ fillPool(layers[i], Math.round(total * L.share), () => make(i, true));
2184
+ });
2185
+ },
2186
+ update(dt) {
2187
+ const s = dt * o.speed;
2188
+ t += s;
2189
+ const sl = slant();
2190
+ for (let li = 0; li < 2; li++) {
2191
+ for (const d of layers[li]) {
2192
+ d.y += d.vy * s;
2193
+ d.x += d.vy * sl * s;
2194
+ if (d.y >= d.ground) {
2195
+ if (li === 1 && Math.random() < 0.85) splash(d.x, d.ground);
2196
+ Object.assign(d, make(li, false));
2197
+ }
2198
+ }
2199
+ }
2200
+ retain(droplets, (p) => {
2201
+ p.vy += GRAVITY * s;
2202
+ p.x += p.vx * s;
2203
+ p.y += p.vy * s;
2204
+ return (p.life -= s) > 0;
2205
+ });
2206
+ retain(ripples, (r) => (r.life -= s * 1.8) > 0);
2207
+ if (storm2) {
2208
+ if (o.density > 0) {
2209
+ nextStrike -= s;
2210
+ if (nextStrike <= 0) {
2211
+ strike();
2212
+ nextStrike = rand(3, 9) / Math.max(0.3, o.density);
2213
+ }
2214
+ }
2215
+ retain(bolts, (b) => (b.age += s) <= 0.9);
2216
+ }
2217
+ },
2218
+ draw(ctx) {
2219
+ var _a, _b;
2220
+ const color = (_b = (_a = o.colors) == null ? void 0 : _a[0]) != null ? _b : "rgb(174, 194, 224)";
2221
+ const sl = slant();
2222
+ let flash = 0;
2223
+ for (const b of bolts) flash = Math.max(flash, flashCurve(b.age));
2224
+ if (flash > 0) {
2225
+ ctx.globalAlpha = flash * 0.2;
2226
+ ctx.fillStyle = "rgb(205, 215, 255)";
2227
+ ctx.fillRect(0, 0, w, h);
2228
+ }
2229
+ ctx.strokeStyle = color;
2230
+ ctx.lineCap = "round";
2231
+ for (let li = 0; li < 2; li++) {
2232
+ const L = LAYERS[li];
2233
+ if (!layers[li].length) continue;
2234
+ for (const bright of [false, true]) {
2235
+ const a = L.alpha * (bright ? 1.35 : 1) * (1 + flash * 0.8);
2236
+ ctx.globalAlpha = Math.min(1, a * 0.45);
2237
+ ctx.lineWidth = L.width * o.size;
2238
+ ctx.beginPath();
2239
+ for (const d of layers[li]) {
2240
+ if (d.bright !== bright) continue;
2241
+ ctx.moveTo(d.x, d.y);
2242
+ ctx.lineTo(d.x - d.len * sl, d.y - d.len);
2243
+ }
2244
+ ctx.stroke();
2245
+ ctx.globalAlpha = Math.min(1, a);
2246
+ ctx.lineWidth = (L.width + 0.35) * o.size;
2247
+ ctx.beginPath();
2248
+ for (const d of layers[li]) {
2249
+ if (d.bright !== bright) continue;
2250
+ ctx.moveTo(d.x, d.y);
2251
+ ctx.lineTo(d.x - d.len * 0.35 * sl, d.y - d.len * 0.35);
2252
+ }
2253
+ ctx.stroke();
2254
+ }
2255
+ }
2256
+ ctx.lineWidth = 1;
2257
+ for (const r of ripples) rippleGroups[alphaLevel(r.life)].push(r);
2258
+ for (let lv = 1; lv <= ALPHA_STEPS; lv++) {
2259
+ const group = rippleGroups[lv];
2260
+ if (!group.length) continue;
2261
+ const a = lv / ALPHA_STEPS;
2262
+ ctx.globalAlpha = a * 0.45;
2263
+ ctx.beginPath();
2264
+ for (const r of group) {
2265
+ const rx = r.max * (1 - r.life);
2266
+ ctx.moveTo(r.x + rx, r.y);
2267
+ ctx.ellipse(r.x, r.y, rx, rx * 0.28, 0, 0, TAU);
2268
+ }
2269
+ ctx.stroke();
2270
+ ctx.globalAlpha = a * 0.3;
2271
+ ctx.beginPath();
2272
+ for (const r of group) {
2273
+ const k = 1 - r.life;
2274
+ if (k <= 0.35) continue;
2275
+ const rx = r.max * ((k - 0.35) / 0.65) * 0.6;
2276
+ ctx.moveTo(r.x + rx, r.y);
2277
+ ctx.ellipse(r.x, r.y, rx, rx * 0.28, 0, 0, TAU);
2278
+ }
2279
+ ctx.stroke();
2280
+ group.length = 0;
2281
+ }
2282
+ ctx.fillStyle = color;
2283
+ for (const p of droplets) dropletGroups[alphaLevel(p.life / p.max)].push(p);
2284
+ for (let lv = 1; lv <= ALPHA_STEPS; lv++) {
2285
+ const group = dropletGroups[lv];
2286
+ if (!group.length) continue;
2287
+ ctx.globalAlpha = lv / ALPHA_STEPS * 0.85;
2288
+ ctx.beginPath();
2289
+ for (const p of group) {
2290
+ ctx.moveTo(p.x + p.r, p.y);
2291
+ ctx.arc(p.x, p.y, p.r, 0, TAU);
2292
+ }
2293
+ ctx.fill();
2294
+ group.length = 0;
2295
+ }
2296
+ if (o.ground && o.density > 0) {
2297
+ const mh = Math.min(70, h * 0.15);
2298
+ const g = ctx.createLinearGradient(0, h - mh, 0, h);
2299
+ g.addColorStop(0, "rgba(200,210,230,0)");
2300
+ g.addColorStop(1, `rgba(200,210,230,${storm2 ? 0.16 : 0.1})`);
2301
+ ctx.globalAlpha = 1;
2302
+ ctx.fillStyle = g;
2303
+ ctx.fillRect(0, h - mh, w, mh);
2304
+ }
2305
+ if (bolts.length) {
2306
+ ctx.globalCompositeOperation = "lighter";
2307
+ ctx.lineJoin = "round";
2308
+ for (const b of bolts) {
2309
+ const a = flashCurve(b.age);
2310
+ if (a <= 0.01) continue;
2311
+ b.paths.forEach((p, i) => {
2312
+ const k = i === 0 ? 1 : 0.5;
2313
+ ctx.globalAlpha = a * 0.25;
2314
+ ctx.strokeStyle = "#8fa8ff";
2315
+ ctx.lineWidth = 9 * k;
2316
+ polyline(ctx, p);
2317
+ ctx.globalAlpha = a * 0.6;
2318
+ ctx.strokeStyle = "#cfdcff";
2319
+ ctx.lineWidth = 3.2 * k;
2320
+ polyline(ctx, p);
2321
+ ctx.globalAlpha = a;
2322
+ ctx.strokeStyle = "#ffffff";
2323
+ ctx.lineWidth = 1.4 * k;
2324
+ polyline(ctx, p);
2325
+ });
2326
+ }
2327
+ }
2328
+ },
2329
+ burst(x, y) {
2330
+ if (storm2) strike(x);
2331
+ else for (let i = 0; i < 6; i++) splash(x + rand(-18, 18), y + rand(-4, 4), 1.4);
2332
+ }
2333
+ };
2334
+ };
2335
+ var rain = makeRain(false);
2336
+ var storm = makeRain(true);
2337
+
2338
+ // src/effects/snow.ts
2339
+ var LAYERS2 = [
2340
+ { share: 0.5, r: [0.5, 1.3], alpha: [0.2, 0.4], fall: 0.55, drift: 0.5, sway: [4, 12] },
2341
+ { share: 0.42, r: [1.3, 2.8], alpha: [0.55, 0.85], fall: 1, drift: 1, sway: [10, 28] },
2342
+ { share: 0.08, r: [3.5, 7], alpha: [0.75, 0.95], fall: 1.35, drift: 1.6, sway: [18, 40] }
2343
+ ];
2344
+ var COL = 4;
2345
+ var crystal = (color, variant) => sprite(64, 64, (ctx) => {
2346
+ ctx.translate(32, 32);
2347
+ ctx.strokeStyle = color;
2348
+ ctx.fillStyle = color;
2349
+ ctx.lineCap = "round";
2350
+ ctx.lineJoin = "round";
2351
+ ctx.shadowColor = color;
2352
+ ctx.shadowBlur = 5;
2353
+ const branches = [
2354
+ [[9, 6], [16, 5]],
2355
+ [[7, 8], [13, 6], [19, 4]],
2356
+ [[12, 9]]
2357
+ ][variant];
2358
+ for (let i = 0; i < 6; i++) {
2359
+ ctx.save();
2360
+ ctx.rotate(i * Math.PI / 3);
2361
+ ctx.lineWidth = 2.4;
2362
+ ctx.beginPath();
2363
+ ctx.moveTo(0, 0);
2364
+ ctx.lineTo(0, -25);
2365
+ for (const [at, len] of branches) {
2366
+ ctx.moveTo(0, -at);
2367
+ ctx.lineTo(-len * 0.7, -at - len);
2368
+ ctx.moveTo(0, -at);
2369
+ ctx.lineTo(len * 0.7, -at - len);
2370
+ }
2371
+ ctx.stroke();
2372
+ if (variant === 2) {
2373
+ ctx.beginPath();
2374
+ ctx.arc(0, -25, 2.6, 0, TAU);
2375
+ ctx.fill();
2376
+ }
2377
+ ctx.restore();
2378
+ }
2379
+ ctx.globalAlpha = 0.85;
2380
+ ctx.beginPath();
2381
+ for (let i = 0; i < 6; i++) {
2382
+ const a = i * Math.PI / 3;
2383
+ ctx.lineTo(Math.cos(a) * 5, Math.sin(a) * 5);
2384
+ }
2385
+ ctx.closePath();
2386
+ ctx.fill();
2387
+ });
2388
+ var snow = (o) => {
2389
+ let w = 0;
2390
+ let h = 0;
2391
+ let t = rand(0, 100);
2392
+ const layers = [[], [], []];
2393
+ let pile = new Float32Array(0);
2394
+ let smoothTimer = 0;
2395
+ const color = () => {
2396
+ var _a, _b;
2397
+ return (_b = (_a = o.colors) == null ? void 0 : _a[0]) != null ? _b : "#ffffff";
2398
+ };
2399
+ const dot = spriteCache((c) => glowSprite(c, false, 0.35));
2400
+ const crystals = spriteCache((key) => {
2401
+ const [c, v] = key.split("|");
2402
+ return crystal(c, Number(v));
2403
+ });
2404
+ const pileMax = () => Math.min(26, h * 0.05) * o.size;
2405
+ const pileAt = (x) => {
2406
+ var _a;
2407
+ return (_a = pile[clamp(x / COL | 0, 0, pile.length - 1)]) != null ? _a : 0;
2408
+ };
2409
+ const make = (layer, anywhere) => {
2410
+ const L = LAYERS2[layer];
2411
+ const r = rand(L.r[0], L.r[1]) * o.size;
2412
+ return {
2413
+ x: rand(-20, w + 20),
2414
+ y: anywhere ? rand(-20, h) : -r * 4 - rand(0, 60),
2415
+ r,
2416
+ vy: (22 + r * 9) * L.fall,
2417
+ sway: rand(L.sway[0], L.sway[1]),
2418
+ phase: rand(0, TAU),
2419
+ freq: rand(0.4, 1.3),
2420
+ alpha: rand(L.alpha[0], L.alpha[1]),
2421
+ rot: rand(0, TAU),
2422
+ vrot: rand(-0.7, 0.7),
2423
+ tumble: rand(0, TAU),
2424
+ variant: Math.random() * 3 | 0
2425
+ };
2426
+ };
2427
+ const deposit = (x, amount) => {
2428
+ const i = clamp(x / COL | 0, 0, pile.length - 1);
2429
+ const max = pileMax();
2430
+ const weights = [0.12, 0.2, 0.36, 0.2, 0.12];
2431
+ for (let k = 0; k < 5; k++) {
2432
+ const j = i + k - 2;
2433
+ if (j >= 0 && j < pile.length) pile[j] = Math.min(max, pile[j] + amount * weights[k]);
2434
+ }
2435
+ };
2436
+ return {
2437
+ resize(nw, nh) {
2438
+ w = nw;
2439
+ h = nh;
2440
+ const total = countFor(w, h, 220, o.density);
2441
+ LAYERS2.forEach((L, i) => fillPool(layers[i], Math.round(total * L.share), () => make(i, true)));
2442
+ const cols = Math.ceil(w / COL) + 1;
2443
+ if (cols !== pile.length) pile = new Float32Array(cols);
2444
+ },
2445
+ update(dt) {
2446
+ const s = dt * o.speed;
2447
+ t += s;
2448
+ const wind = o.wind + gust(t) * 0.3;
2449
+ const ground = o.ground;
2450
+ for (let li = 0; li < 3; li++) {
2451
+ const L = LAYERS2[li];
2452
+ for (const f of layers[li]) {
2453
+ f.phase += f.freq * s;
2454
+ f.rot += f.vrot * s;
2455
+ f.tumble += 0.8 * s;
2456
+ f.y += f.vy * s;
2457
+ f.x += (Math.cos(f.phase) * f.sway + wind * 55 * L.drift) * s;
2458
+ if (ground && li > 0 && f.y + f.r * 0.5 >= h - pileAt(f.x)) {
2459
+ deposit(f.x, f.r * 2.2);
2460
+ Object.assign(f, make(li, false));
2461
+ } else if (f.y - f.r * 3 > h) {
2462
+ Object.assign(f, make(li, false));
2463
+ }
2464
+ const m = f.r * 3;
2465
+ if (f.x < -m) f.x += w + m * 2;
2466
+ else if (f.x > w + m) f.x -= w + m * 2;
2467
+ }
2468
+ }
2469
+ if (!ground) {
2470
+ pile.fill(0);
2471
+ } else {
2472
+ smoothTimer -= s;
2473
+ if (smoothTimer <= 0) {
2474
+ smoothTimer = 0.1;
2475
+ const max = pileMax();
2476
+ for (let i = 1; i < pile.length - 1; i++) {
2477
+ pile[i] = pile[i] * 0.8 + (pile[i - 1] + pile[i + 1]) * 0.1;
2478
+ pile[i] = Math.max(0, pile[i] - 0.06 * (pile[i] / max) ** 3);
2479
+ }
2480
+ }
2481
+ }
2482
+ },
2483
+ draw(ctx) {
2484
+ const c = color();
2485
+ const d = dot(c);
2486
+ for (let li = 0; li < 2; li++) {
2487
+ for (const f of layers[li]) {
2488
+ ctx.globalAlpha = f.alpha;
2489
+ stamp(ctx, d, f.x, f.y, f.r * 3.2);
2490
+ }
2491
+ }
2492
+ if (o.ground) {
2493
+ let top = 0;
2494
+ for (let i = 0; i < pile.length; i++) if (pile[i] > top) top = pile[i];
2495
+ if (top > 0.4) {
2496
+ ctx.beginPath();
2497
+ ctx.moveTo(0, h);
2498
+ for (let i = 0; i < pile.length; i++) ctx.lineTo(i * COL, h - pile[i]);
2499
+ ctx.lineTo(w, h);
2500
+ ctx.closePath();
2501
+ ctx.globalAlpha = 0.95;
2502
+ ctx.fillStyle = c;
2503
+ ctx.fill();
2504
+ const g = ctx.createLinearGradient(0, h - top, 0, h);
2505
+ g.addColorStop(0, "rgba(150,180,220,0)");
2506
+ g.addColorStop(1, "rgba(150,180,220,0.45)");
2507
+ ctx.fillStyle = g;
2508
+ ctx.fill();
2509
+ ctx.globalAlpha = 0.9;
2510
+ ctx.strokeStyle = "#ffffff";
2511
+ ctx.lineWidth = 1;
2512
+ ctx.beginPath();
2513
+ for (let i = 0; i < pile.length; i++) ctx.lineTo(i * COL, h - pile[i] + 0.5);
2514
+ ctx.stroke();
2515
+ }
2516
+ }
2517
+ const near = layers[2];
2518
+ if (near.length) {
2519
+ const m = baseTransform(ctx);
2520
+ const variants = [crystals(`${c}|0`), crystals(`${c}|1`), crystals(`${c}|2`)];
2521
+ for (const f of near) {
2522
+ place(ctx, m, f.x, f.y, f.rot, 0.7 + 0.3 * Math.cos(f.tumble), 1);
2523
+ ctx.globalAlpha = f.alpha * 0.35;
2524
+ stamp(ctx, d, 0, 0, f.r * 4.5);
2525
+ ctx.globalAlpha = f.alpha;
2526
+ stamp(ctx, variants[f.variant], 0, 0, f.r * 5);
2527
+ }
2528
+ resetTransform(ctx, m);
2529
+ }
2530
+ }
2531
+ };
2532
+ };
2533
+
2534
+ // src/effects/index.ts
2535
+ var registry = /* @__PURE__ */ new Map([
2536
+ ["snow", snow],
2537
+ ["rain", rain],
2538
+ ["storm", storm],
2539
+ ["thunderstorm", storm],
2540
+ ["fireworks", fireworks],
2541
+ // Aliases so festival names work out of the box.
2542
+ ["rockets", fireworks],
2543
+ ["diwali", fireworks],
2544
+ ["deepawali", fireworks],
2545
+ ["tihar", fireworks],
2546
+ ["confetti", confetti],
2547
+ ["leaves", leaves],
2548
+ ["autumn", leaves],
2549
+ ["sakura", sakura],
2550
+ ["petals", sakura],
2551
+ ["diyas", diyas],
2552
+ ["lanterns", diyas],
2553
+ ["lights", diyas],
2554
+ ["stars", stars],
2555
+ ["night", stars],
2556
+ ["fireflies", fireflies],
2557
+ ["marigold", marigold],
2558
+ ["sayapatri", marigold],
2559
+ ["garland", marigold],
2560
+ ["holi", holi],
2561
+ ["gulal", holi],
2562
+ ["kites", kites],
2563
+ ["changa", kites],
2564
+ ["dashain", kites]
2565
+ ]);
2566
+ function registerEffect(name, factory) {
2567
+ registry.set(name, factory);
2568
+ }
2569
+ function getEffect(name) {
2570
+ return registry.get(name);
2571
+ }
2572
+ function listEffects() {
2573
+ return [...registry.keys()];
2574
+ }
2575
+
2576
+ // src/engine.ts
2577
+ var isBrowser = () => typeof window !== "undefined" && typeof document !== "undefined";
2578
+ var num = (v, fallback) => typeof v === "number" && Number.isFinite(v) ? v : fallback;
2579
+ function applyEffectOptions(target, o) {
2580
+ var _a;
2581
+ if ("density" in o) target.density = Math.max(0, num(o.density, 1));
2582
+ if ("speed" in o) target.speed = Math.max(0, num(o.speed, 1));
2583
+ if ("wind" in o) target.wind = num(o.wind, 0);
2584
+ if ("size" in o) target.size = Math.max(0.1, num(o.size, 1));
2585
+ if ("ground" in o) target.ground = (_a = o.ground) != null ? _a : true;
2586
+ if ("colors" in o) target.colors = o.colors && o.colors.length ? [...o.colors] : void 0;
2587
+ }
2588
+ function resolveTarget(t) {
2589
+ if (!t) return null;
2590
+ if (typeof t === "string") {
2591
+ const el = document.querySelector(t);
2592
+ if (!el) throw new Error(`[festive-fx] target "${t}" not found`);
2593
+ return el;
2594
+ }
2595
+ return t;
2596
+ }
2597
+ function prefersReducedMotion() {
2598
+ return typeof window.matchMedia === "function" && window.matchMedia("(prefers-reduced-motion: reduce)").matches;
2599
+ }
2600
+ function noopController() {
2601
+ const noop = () => {
2602
+ };
2603
+ return { start: noop, stop: noop, clear: noop, update: noop, burst: noop, destroy: noop, running: false, canvas: null };
2604
+ }
2605
+ var specKey = (spec) => Array.isArray(spec) ? spec.join(",") : spec;
2606
+ function instantiateOne(name, opts) {
2607
+ const factory = getEffect(name);
2608
+ if (!factory) throw new Error(`[festive-fx] unknown effect "${name}". Register it with registerEffect().`);
2609
+ return factory(opts);
2610
+ }
2611
+ function instantiate(spec, opts) {
2612
+ const names = Array.isArray(spec) ? spec : [spec];
2613
+ if (names.length === 0) throw new Error("[festive-fx] effect list is empty");
2614
+ if (names.length === 1) return instantiateOne(names[0], opts);
2615
+ const layers = names.map((n) => instantiateOne(n, opts));
2616
+ return {
2617
+ resize: (w, h) => layers.forEach((l) => l.resize(w, h)),
2618
+ update: (dt) => layers.forEach((l) => l.update(dt)),
2619
+ draw(ctx) {
2620
+ for (const l of layers) {
2621
+ ctx.save();
2622
+ l.draw(ctx);
2623
+ ctx.restore();
2624
+ }
2625
+ },
2626
+ burst: (x, y) => layers.forEach((l) => {
2627
+ var _a;
2628
+ return (_a = l.burst) == null ? void 0 : _a.call(l, x, y);
2629
+ })
2630
+ };
2631
+ }
2632
+ function createFx(options = {}) {
2633
+ var _a, _b, _c, _d, _e, _f, _g;
2634
+ if (!isBrowser()) return noopController();
2635
+ const host = (_a = resolveTarget(options.target)) != null ? _a : document.body;
2636
+ const fullscreen = (_b = options.fullscreen) != null ? _b : !options.target;
2637
+ const maxDpr = num(options.maxDpr, 2);
2638
+ const respectRM = (_c = options.respectReducedMotion) != null ? _c : true;
2639
+ const eff = { density: 1, speed: 1, wind: 0, size: 1, colors: void 0, ground: true };
2640
+ applyEffectOptions(eff, options);
2641
+ let effectName = specKey((_d = options.effect) != null ? _d : "snow");
2642
+ let effect = instantiate((_e = options.effect) != null ? _e : "snow", eff);
2643
+ const canvas = document.createElement("canvas");
2644
+ canvas.setAttribute("aria-hidden", "true");
2645
+ canvas.className = "festive-fx-canvas";
2646
+ Object.assign(canvas.style, {
2647
+ position: fullscreen ? "fixed" : "absolute",
2648
+ top: "0",
2649
+ left: "0",
2650
+ width: "100%",
2651
+ height: "100%",
2652
+ pointerEvents: "none",
2653
+ display: "block",
2654
+ zIndex: String(num(options.zIndex, 9999)),
2655
+ transition: "opacity 600ms ease"
2656
+ });
2657
+ let restorePosition = null;
2658
+ if (!fullscreen && ["", "static"].includes(getComputedStyle(host).position)) {
2659
+ restorePosition = host.style.position;
2660
+ host.style.position = "relative";
2661
+ }
2662
+ ((_f = options.container) != null ? _f : host).appendChild(canvas);
2663
+ let ctx = null;
2664
+ try {
2665
+ ctx = canvas.getContext("2d");
2666
+ } catch {
2667
+ ctx = null;
2668
+ }
2669
+ let w = 0;
2670
+ let h = 0;
2671
+ let dpr = 0;
2672
+ let raf = 0;
2673
+ let last = 0;
2674
+ let running = false;
2675
+ let destroyed = false;
2676
+ let onScreen = true;
2677
+ let durationTimer;
2678
+ const measure = () => {
2679
+ let nw;
2680
+ let nh;
2681
+ if (fullscreen) {
2682
+ nw = window.innerWidth;
2683
+ nh = window.innerHeight;
2684
+ } else {
2685
+ const r = host.getBoundingClientRect();
2686
+ nw = r.width;
2687
+ nh = r.height;
2688
+ }
2689
+ const ndpr = Math.min(window.devicePixelRatio || 1, maxDpr);
2690
+ if (nw === w && nh === h && ndpr === dpr) return;
2691
+ w = nw;
2692
+ h = nh;
2693
+ dpr = ndpr;
2694
+ canvas.width = Math.max(1, Math.round(w * dpr));
2695
+ canvas.height = Math.max(1, Math.round(h * dpr));
2696
+ ctx == null ? void 0 : ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
2697
+ effect.resize(w, h);
2698
+ };
2699
+ const render = () => {
2700
+ if (!ctx) return;
2701
+ ctx.clearRect(0, 0, w, h);
2702
+ ctx.globalAlpha = 1;
2703
+ ctx.globalCompositeOperation = "source-over";
2704
+ effect.draw(ctx);
2705
+ };
2706
+ const frame = (t) => {
2707
+ raf = requestAnimationFrame(frame);
2708
+ const dt = last ? Math.min((t - last) / 1e3, 0.05) : 1 / 60;
2709
+ last = t;
2710
+ effect.update(dt);
2711
+ render();
2712
+ };
2713
+ const loop = (on) => {
2714
+ if (on && !raf) {
2715
+ last = 0;
2716
+ raf = requestAnimationFrame(frame);
2717
+ } else if (!on && raf) {
2718
+ cancelAnimationFrame(raf);
2719
+ raf = 0;
2720
+ }
2721
+ };
2722
+ const sync = () => loop(running && onScreen && !document.hidden);
2723
+ document.addEventListener("visibilitychange", sync);
2724
+ let io = null;
2725
+ if (!fullscreen && typeof IntersectionObserver !== "undefined") {
2726
+ io = new IntersectionObserver((entries) => {
2727
+ onScreen = entries[entries.length - 1].isIntersecting;
2728
+ sync();
2729
+ });
2730
+ io.observe(host);
2731
+ }
2732
+ let ro = null;
2733
+ const onWindowResize = () => measure();
2734
+ if (!fullscreen && typeof ResizeObserver !== "undefined") {
2735
+ ro = new ResizeObserver(() => measure());
2736
+ ro.observe(host);
2737
+ } else {
2738
+ window.addEventListener("resize", onWindowResize);
2739
+ }
2740
+ let interactive = !!options.interactive;
2741
+ const onPointer = (e) => {
2742
+ var _a2, _b2;
2743
+ if (!interactive) return;
2744
+ const el = e.target;
2745
+ if ((_a2 = el == null ? void 0 : el.closest) == null ? void 0 : _a2.call(el, 'button, a, input, select, textarea, label, [contenteditable], [role="button"]')) return;
2746
+ const r = canvas.getBoundingClientRect();
2747
+ (_b2 = effect.burst) == null ? void 0 : _b2.call(effect, e.clientX - r.left, e.clientY - r.top);
2748
+ };
2749
+ const pointerTarget = fullscreen ? window : host;
2750
+ pointerTarget.addEventListener("pointerdown", onPointer);
2751
+ const scheduleDuration = (ms) => {
2752
+ clearTimeout(durationTimer);
2753
+ if (ms && ms > 0) {
2754
+ durationTimer = setTimeout(() => {
2755
+ canvas.style.opacity = "0";
2756
+ durationTimer = setTimeout(() => controller.stop(), 650);
2757
+ }, ms);
2758
+ }
2759
+ };
2760
+ const controller = {
2761
+ start() {
2762
+ if (destroyed || running) return;
2763
+ if (respectRM && prefersReducedMotion()) return;
2764
+ running = true;
2765
+ canvas.style.opacity = "1";
2766
+ sync();
2767
+ },
2768
+ stop() {
2769
+ running = false;
2770
+ loop(false);
2771
+ },
2772
+ clear() {
2773
+ ctx == null ? void 0 : ctx.clearRect(0, 0, w, h);
2774
+ },
2775
+ update(next) {
2776
+ if (destroyed) return;
2777
+ applyEffectOptions(eff, next);
2778
+ if (next.effect && specKey(next.effect) !== effectName) {
2779
+ effect = instantiate(next.effect, eff);
2780
+ effectName = specKey(next.effect);
2781
+ }
2782
+ if ("zIndex" in next) canvas.style.zIndex = String(num(next.zIndex, 9999));
2783
+ if ("interactive" in next) interactive = !!next.interactive;
2784
+ if ("duration" in next) scheduleDuration(next.duration);
2785
+ effect.resize(w, h);
2786
+ if (!running) render();
2787
+ },
2788
+ burst(x, y) {
2789
+ if (destroyed || !effect.burst) return;
2790
+ effect.burst(x != null ? x : w * (0.2 + Math.random() * 0.6), y != null ? y : h * (0.2 + Math.random() * 0.3));
2791
+ if (!running) controller.start();
2792
+ },
2793
+ destroy() {
2794
+ if (destroyed) return;
2795
+ controller.stop();
2796
+ destroyed = true;
2797
+ clearTimeout(durationTimer);
2798
+ document.removeEventListener("visibilitychange", sync);
2799
+ io == null ? void 0 : io.disconnect();
2800
+ window.removeEventListener("resize", onWindowResize);
2801
+ pointerTarget.removeEventListener("pointerdown", onPointer);
2802
+ ro == null ? void 0 : ro.disconnect();
2803
+ canvas.remove();
2804
+ if (restorePosition !== null) host.style.position = restorePosition;
2805
+ },
2806
+ get running() {
2807
+ return running;
2808
+ },
2809
+ get canvas() {
2810
+ return destroyed ? null : canvas;
2811
+ }
2812
+ };
2813
+ measure();
2814
+ if ((_g = options.autoStart) != null ? _g : true) controller.start();
2815
+ scheduleDuration(options.duration);
2816
+ return controller;
2817
+ }
2818
+
2819
+ exports.ALPHA_STEPS = ALPHA_STEPS;
2820
+ exports.alphaLevel = alphaLevel;
2821
+ exports.baseTransform = baseTransform;
2822
+ exports.confetti = confetti;
2823
+ exports.createFx = createFx;
2824
+ exports.diyas = diyas;
2825
+ exports.fireflies = fireflies;
2826
+ exports.fireworks = fireworks;
2827
+ exports.getEffect = getEffect;
2828
+ exports.glowSprite = glowSprite;
2829
+ exports.holi = holi;
2830
+ exports.kites = kites;
2831
+ exports.leaves = leaves;
2832
+ exports.listEffects = listEffects;
2833
+ exports.marigold = marigold;
2834
+ exports.place = place;
2835
+ exports.placeFlat = placeFlat;
2836
+ exports.rain = rain;
2837
+ exports.registerEffect = registerEffect;
2838
+ exports.resetTransform = resetTransform;
2839
+ exports.retain = retain;
2840
+ exports.sakura = sakura;
2841
+ exports.shade = shade;
2842
+ exports.snow = snow;
2843
+ exports.sprite = sprite;
2844
+ exports.spriteCache = spriteCache;
2845
+ exports.stamp = stamp;
2846
+ exports.stars = stars;
2847
+ exports.storm = storm;
2848
+ //# sourceMappingURL=chunk-JYL2RG4A.cjs.map
2849
+ //# sourceMappingURL=chunk-JYL2RG4A.cjs.map