@mar7th/firework 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,875 @@
1
+ var A = Object.defineProperty;
2
+ var N = (n, e, t) => e in n ? A(n, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[e] = t;
3
+ var M = (n, e, t) => N(n, typeof e != "symbol" ? e + "" : e, t);
4
+ const p = {
5
+ particleCount: 180,
6
+ particleMinSize: 2.5,
7
+ particleMaxSize: 7,
8
+ sizeEndMultiplier: 0.35,
9
+ gravity: 0.18,
10
+ damping: 0.982,
11
+ initialSpeedMin: 2.8,
12
+ initialSpeedMax: 8.2,
13
+ particleLife: 94,
14
+ rotationSpeed: 0.025,
15
+ colorShiftRate: 0.035,
16
+ colorStart: "#ffbf4d",
17
+ colorEnd: "#ff5a3c",
18
+ useRandomColors: !1,
19
+ textureType: "circle",
20
+ glowIntensity: 0.9,
21
+ explosionShape: "sphere",
22
+ enableSecondaryBurst: !1,
23
+ secondaryBurstRatio: 0.2,
24
+ enableTrails: !0,
25
+ showLaunchTrail: !0,
26
+ windEnabled: !1,
27
+ windX: 0,
28
+ windY: 0,
29
+ flicker: !0,
30
+ fadeTrail: 0.18,
31
+ autoLaunch: !1,
32
+ autoInterval: 900,
33
+ autoLaunchIntervalSeconds: 0.9,
34
+ backgroundType: "starfield",
35
+ backgroundColor: "#170d17",
36
+ backgroundColor2: "#3a1a24",
37
+ backgroundImageMode: "cover"
38
+ }, V = [
39
+ {
40
+ title: "样式",
41
+ controls: [
42
+ { key: "colorStart", label: "起始颜色", type: "color" },
43
+ { key: "colorEnd", label: "结束颜色", type: "color" },
44
+ { key: "useRandomColors", label: "随机颜色", type: "boolean" },
45
+ {
46
+ key: "textureType",
47
+ label: "粒子形状",
48
+ type: "select",
49
+ options: [
50
+ ["circle", "圆形"],
51
+ ["square", "方形"],
52
+ ["star", "星形"],
53
+ ["heart", "心形"]
54
+ ]
55
+ },
56
+ { key: "glowIntensity", label: "辉光强度", type: "range", min: 0, max: 1.5, step: 0.05 }
57
+ ]
58
+ },
59
+ {
60
+ title: "特效",
61
+ controls: [
62
+ {
63
+ key: "explosionShape",
64
+ label: "爆炸形状",
65
+ type: "select",
66
+ options: [
67
+ ["sphere", "球形"],
68
+ ["ring", "环形"],
69
+ ["starburst", "星形"],
70
+ ["flower", "花朵"],
71
+ ["willow", "柳絮"],
72
+ ["scatter", "随机散落"],
73
+ ["heart", "爱心"]
74
+ ]
75
+ },
76
+ {
77
+ key: "enableSecondaryBurst",
78
+ label: "二次爆炸",
79
+ type: "boolean",
80
+ hint: "二次爆炸对性能影响较大,谨慎开启。"
81
+ },
82
+ {
83
+ key: "secondaryBurstRatio",
84
+ label: "二次比例",
85
+ type: "range",
86
+ min: 0,
87
+ max: 1,
88
+ step: 0.05,
89
+ hint: "这里只控制二次爆炸比例;自定义二次爆炸效果需要使用高级模式自定义脚本。"
90
+ },
91
+ { key: "enableTrails", label: "粒子拖尾", type: "boolean", hint: "拖尾会影响性能,酌情开启。" },
92
+ { key: "showLaunchTrail", label: "升空轨迹", type: "boolean" },
93
+ { key: "windEnabled", label: "风场", type: "boolean" },
94
+ { key: "windX", label: "水平风", type: "range", min: -0.35, max: 0.35, step: 0.01 },
95
+ { key: "windY", label: "垂直风", type: "range", min: -0.35, max: 0.35, step: 0.01 },
96
+ { key: "flicker", label: "闪烁", type: "boolean" },
97
+ { key: "fadeTrail", label: "拖尾长度", type: "range", min: 0.04, max: 0.55, step: 0.01 }
98
+ ]
99
+ },
100
+ {
101
+ title: "粒子",
102
+ controls: [
103
+ { key: "particleCount", label: "粒子数量", type: "range", min: 10, max: 1e3, step: 10 },
104
+ { key: "initialSpeedMin", label: "最小速度", type: "range", min: 0, max: 20, step: 0.1 },
105
+ { key: "initialSpeedMax", label: "最大速度", type: "range", min: 0, max: 20, step: 0.1 },
106
+ { key: "damping", label: "速度衰减", type: "range", min: 0.95, max: 1, step: 1e-3 },
107
+ { key: "gravity", label: "重力影响", type: "range", min: -1, max: 2, step: 0.01 },
108
+ { key: "particleLife", label: "生命周期", type: "range", min: 30, max: 180, step: 1 },
109
+ { key: "particleMinSize", label: "最小大小", type: "range", min: 2, max: 15, step: 0.5 },
110
+ { key: "particleMaxSize", label: "最大大小", type: "range", min: 2, max: 15, step: 0.5 },
111
+ { key: "sizeEndMultiplier", label: "结束比例", type: "range", min: 0, max: 2, step: 0.05 },
112
+ { key: "rotationSpeed", label: "旋转速度", type: "range", min: 0, max: 0.105, step: 5e-3 },
113
+ { key: "colorShiftRate", label: "变色速率", type: "range", min: 0, max: 0.1, step: 5e-3 },
114
+ { key: "autoLaunchIntervalSeconds", label: "自动间隔(秒)", type: "range", min: 0.25, max: 3, step: 0.05 }
115
+ ]
116
+ },
117
+ {
118
+ title: "背景",
119
+ controls: [
120
+ {
121
+ key: "backgroundType",
122
+ label: "背景样式",
123
+ type: "select",
124
+ options: [
125
+ ["solid", "纯色"],
126
+ ["linear", "线性渐变"],
127
+ ["radial", "径向渐变"],
128
+ ["starfield", "星空"],
129
+ ["image", "图片"]
130
+ ]
131
+ },
132
+ { key: "backgroundColor", label: "背景色 A", type: "color" },
133
+ { key: "backgroundColor2", label: "背景色 B", type: "color" },
134
+ {
135
+ key: "backgroundImageMode",
136
+ label: "图片填充",
137
+ type: "select",
138
+ options: [
139
+ ["cover", "覆盖"],
140
+ ["contain", "完整"],
141
+ ["tile", "平铺"]
142
+ ]
143
+ },
144
+ { key: "backgroundImage", label: "上传图片", type: "file" }
145
+ ]
146
+ }
147
+ ], T = {
148
+ "classic-gold": {
149
+ name: "经典礼花",
150
+ config: {
151
+ colorStart: "#ffd166",
152
+ colorEnd: "#ff3d2e",
153
+ explosionShape: "sphere",
154
+ particleCount: 220,
155
+ gravity: 0.22,
156
+ damping: 0.981,
157
+ glowIntensity: 0.9,
158
+ enableSecondaryBurst: !1,
159
+ textureType: "circle",
160
+ fadeTrail: 0.18
161
+ }
162
+ },
163
+ aurora: {
164
+ name: "梦幻极光",
165
+ config: {
166
+ colorStart: "#4cc9f0",
167
+ colorEnd: "#80ffdb",
168
+ explosionShape: "ring",
169
+ particleCount: 260,
170
+ gravity: 0.03,
171
+ damping: 0.992,
172
+ glowIntensity: 1.1,
173
+ fadeTrail: 0.08,
174
+ textureType: "circle"
175
+ }
176
+ },
177
+ fountain: {
178
+ name: "星光喷泉",
179
+ config: {
180
+ colorStart: "#f7f3a1",
181
+ colorEnd: "#fca311",
182
+ explosionShape: "flower",
183
+ particleCount: 420,
184
+ particleMinSize: 2,
185
+ particleMaxSize: 4.5,
186
+ initialSpeedMin: 4,
187
+ initialSpeedMax: 11,
188
+ gravity: 0.32,
189
+ damping: 0.976,
190
+ textureType: "star"
191
+ }
192
+ },
193
+ ghost: {
194
+ name: "幽灵鬼火",
195
+ config: {
196
+ colorStart: "#7bdff2",
197
+ colorEnd: "#b2f7ef",
198
+ explosionShape: "scatter",
199
+ particleCount: 160,
200
+ initialSpeedMin: 0.8,
201
+ initialSpeedMax: 4.6,
202
+ gravity: -0.01,
203
+ damping: 0.989,
204
+ enableSecondaryBurst: !0,
205
+ glowIntensity: 1.2,
206
+ fadeTrail: 0.12
207
+ }
208
+ },
209
+ meteor: {
210
+ name: "节日流星",
211
+ config: {
212
+ colorStart: "#ffafcc",
213
+ colorEnd: "#ffffff",
214
+ explosionShape: "willow",
215
+ particleCount: 300,
216
+ initialSpeedMin: 7,
217
+ initialSpeedMax: 13,
218
+ damping: 0.965,
219
+ gravity: 0.16,
220
+ windEnabled: !0,
221
+ windX: 0.12,
222
+ textureType: "square"
223
+ }
224
+ },
225
+ heart: {
226
+ name: "爱心爆发",
227
+ config: {
228
+ colorStart: "#ff4d6d",
229
+ colorEnd: "#ffd6e0",
230
+ explosionShape: "heart",
231
+ particleCount: 260,
232
+ gravity: 0.08,
233
+ damping: 0.986,
234
+ textureType: "heart",
235
+ glowIntensity: 1
236
+ }
237
+ }
238
+ };
239
+ function g(n) {
240
+ const e = { ...p };
241
+ if (!n || typeof n != "object")
242
+ return e;
243
+ const t = V.flatMap((i) => i.controls);
244
+ for (const i of t)
245
+ if (i.key in n) {
246
+ if (i.type === "range") {
247
+ const a = Number(n[i.key]);
248
+ Number.isFinite(a) && (e[i.key] = Math.min(i.max, Math.max(i.min, a)));
249
+ } else if (i.type === "boolean")
250
+ e[i.key] = !!n[i.key];
251
+ else if (i.type === "select")
252
+ i.options.map(([r]) => r).includes(n[i.key]) && (e[i.key] = n[i.key]);
253
+ else if (i.type === "color") {
254
+ const a = String(n[i.key]);
255
+ /^#[0-9a-f]{6}$/i.test(a) && (e[i.key] = a);
256
+ }
257
+ }
258
+ if ("autoLaunch" in n && (e.autoLaunch = !!n.autoLaunch), "autoInterval" in n) {
259
+ const i = Number(n.autoInterval);
260
+ Number.isFinite(i) && (e.autoInterval = x(i, 250, 3e3), e.autoLaunchIntervalSeconds = $(e.autoInterval));
261
+ }
262
+ if ("autoLaunchIntervalSeconds" in n) {
263
+ const i = Number(n.autoLaunchIntervalSeconds);
264
+ Number.isFinite(i) && (e.autoLaunchIntervalSeconds = x(i, 0.25, 3), e.autoInterval = X(e.autoLaunchIntervalSeconds));
265
+ }
266
+ return e;
267
+ }
268
+ function x(n, e, t) {
269
+ return Math.min(t, Math.max(e, n));
270
+ }
271
+ function X(n) {
272
+ return Math.round(n * 1e3);
273
+ }
274
+ function $(n) {
275
+ return Math.round(n / 1e3 * 100) / 100;
276
+ }
277
+ class j {
278
+ constructor() {
279
+ M(this, "listeners", /* @__PURE__ */ new Map());
280
+ }
281
+ on(e, t) {
282
+ return this.listeners.has(e) || this.listeners.set(e, /* @__PURE__ */ new Set()), this.listeners.get(e).add(t), () => this.off(e, t);
283
+ }
284
+ off(e, t) {
285
+ var i;
286
+ (i = this.listeners.get(e)) == null || i.delete(t);
287
+ }
288
+ emit(e, t) {
289
+ var i;
290
+ (i = this.listeners.get(e)) == null || i.forEach((a) => a(t));
291
+ }
292
+ }
293
+ function C(n, e, t) {
294
+ return Math.min(t, Math.max(e, n));
295
+ }
296
+ function b(n, e, t) {
297
+ return n + (e - n) * t;
298
+ }
299
+ function h(n, e) {
300
+ return Math.random() * (e - n) + n;
301
+ }
302
+ function y(n, e) {
303
+ return Math.floor(h(n, e + 1));
304
+ }
305
+ class O {
306
+ constructor() {
307
+ this.image = null, this.imageUrl = "", this.stars = [];
308
+ }
309
+ resize(e, t) {
310
+ const i = Math.round(Math.min(260, Math.max(90, e * t / 9e3)));
311
+ this.stars = Array.from({ length: i }, () => ({
312
+ x: Math.random() * e,
313
+ y: Math.random() * t,
314
+ r: Math.random() * 1.4 + 0.25,
315
+ a: Math.random() * 0.65 + 0.18
316
+ }));
317
+ }
318
+ setImage(e, t, i) {
319
+ if (!e) return;
320
+ if (e.size > 2 * 1024 * 1024) {
321
+ i == null || i("背景图片不能超过 2MB");
322
+ return;
323
+ }
324
+ this.imageUrl && URL.revokeObjectURL(this.imageUrl), this.imageUrl = URL.createObjectURL(e);
325
+ const a = new Image();
326
+ a.onload = () => {
327
+ this.image = a, t == null || t();
328
+ }, a.onerror = () => {
329
+ this.image = null, i == null || i("背景图片读取失败");
330
+ }, a.src = this.imageUrl;
331
+ }
332
+ draw(e, t, i, a) {
333
+ if (a.backgroundType === "image" && this.image) {
334
+ this.drawImage(e, t, i, a.backgroundImageMode);
335
+ return;
336
+ }
337
+ if (a.backgroundType === "linear") {
338
+ const r = e.createLinearGradient(0, 0, t, i);
339
+ r.addColorStop(0, a.backgroundColor), r.addColorStop(1, a.backgroundColor2), e.fillStyle = r, e.fillRect(0, 0, t, i);
340
+ return;
341
+ }
342
+ if (a.backgroundType === "radial" || a.backgroundType === "starfield") {
343
+ const r = e.createRadialGradient(t * 0.5, i * 0.35, 0, t * 0.5, i * 0.5, Math.max(t, i));
344
+ r.addColorStop(0, a.backgroundColor2), r.addColorStop(1, a.backgroundColor), e.fillStyle = r, e.fillRect(0, 0, t, i), a.backgroundType === "starfield" && this.drawStars(e);
345
+ return;
346
+ }
347
+ e.fillStyle = a.backgroundColor, e.fillRect(0, 0, t, i);
348
+ }
349
+ drawStars(e) {
350
+ e.save();
351
+ for (const t of this.stars)
352
+ e.fillStyle = `rgba(255, 255, 255, ${t.a})`, e.beginPath(), e.arc(t.x, t.y, t.r, 0, Math.PI * 2), e.fill();
353
+ e.restore();
354
+ }
355
+ drawImage(e, t, i, a) {
356
+ if (a === "tile") {
357
+ const u = e.createPattern(this.image, "repeat");
358
+ e.fillStyle = u, e.fillRect(0, 0, t, i);
359
+ return;
360
+ }
361
+ const r = a === "contain" ? Math.min(t / this.image.width, i / this.image.height) : Math.max(t / this.image.width, i / this.image.height), s = this.image.width * r, o = this.image.height * r, l = (t - s) / 2, c = (i - o) / 2;
362
+ e.fillStyle = "#050711", e.fillRect(0, 0, t, i), e.drawImage(this.image, l, c, s, o);
363
+ }
364
+ }
365
+ function k(n) {
366
+ const e = n.replace("#", ""), t = Number.parseInt(e, 16);
367
+ return {
368
+ r: t >> 16 & 255,
369
+ g: t >> 8 & 255,
370
+ b: t & 255
371
+ };
372
+ }
373
+ function v({ r: n, g: e, b: t }, i = 1) {
374
+ return `rgba(${Math.round(n)}, ${Math.round(e)}, ${Math.round(t)}, ${C(i, 0, 1)})`;
375
+ }
376
+ function U(n, e, t) {
377
+ const i = typeof n == "string" ? k(n) : n, a = typeof e == "string" ? k(e) : e;
378
+ return {
379
+ r: b(i.r, a.r, t),
380
+ g: b(i.g, a.g, t),
381
+ b: b(i.b, a.b, t)
382
+ };
383
+ }
384
+ function m() {
385
+ const n = y(0, 360);
386
+ return Y(n, y(72, 96), y(54, 68));
387
+ }
388
+ function Y(n, e, t) {
389
+ e /= 100, t /= 100;
390
+ const i = (1 - Math.abs(2 * t - 1)) * e, a = i * (1 - Math.abs(n / 60 % 2 - 1)), r = t - i / 2;
391
+ let s = 0, o = 0, l = 0;
392
+ return n < 60 ? [s, o, l] = [i, a, 0] : n < 120 ? [s, o, l] = [a, i, 0] : n < 180 ? [s, o, l] = [0, i, a] : n < 240 ? [s, o, l] = [0, a, i] : n < 300 ? [s, o, l] = [a, 0, i] : [s, o, l] = [i, 0, a], `#${w((s + r) * 255)}${w((o + r) * 255)}${w((l + r) * 255)}`;
393
+ }
394
+ function w(n) {
395
+ return Math.round(n).toString(16).padStart(2, "0");
396
+ }
397
+ class q {
398
+ constructor(e) {
399
+ this.getConfig = e;
400
+ }
401
+ createExplosion(e, t, i = {}) {
402
+ const a = { ...this.getConfig(), ...i }, r = Math.min(1e3, Math.max(10, Math.round(a.particleCount))), s = [], o = a.useRandomColors ? m() : a.colorStart, l = a.useRandomColors ? m() : a.colorEnd, c = a.enableSecondaryBurst ? Math.round(r * D(a.secondaryBurstRatio)) : 0, u = H(r, c);
403
+ for (let d = 0; d < r; d += 1) {
404
+ const f = W(a.explosionShape, d, r), S = f.speedMultiplier * h(a.initialSpeedMin, a.initialSpeedMax), P = a.explosionShape === "heart" ? f.offsetX : 0, F = a.explosionShape === "heart" ? f.offsetY : 0, R = a.useRandomColors && d % 4 === 0 ? m() : o, E = a.useRandomColors && d % 5 === 0 ? m() : l, B = h(a.particleMinSize, a.particleMaxSize), z = h(a.particleLife * 0.78, a.particleLife * 1.18);
405
+ s.push({
406
+ x: e + P,
407
+ y: t + F,
408
+ vx: f.x * S,
409
+ vy: f.y * S,
410
+ size: B,
411
+ life: z,
412
+ startColor: R,
413
+ endColor: E,
414
+ textureType: a.textureType,
415
+ rotationVelocity: h(-a.rotationSpeed, a.rotationSpeed),
416
+ secondaryEligible: u.has(d)
417
+ });
418
+ }
419
+ return s;
420
+ }
421
+ createSubExplosion(e, t, i = null) {
422
+ const a = (i == null ? void 0 : i.config) || this.getConfig(), r = this.createConfigFromParticle(i, a), s = (i == null ? void 0 : i.secondaryBurstConfig) || {}, o = {
423
+ ...r,
424
+ ...s,
425
+ enableSecondaryBurst: !1
426
+ }, l = "particleCount" in s ? o.particleCount : Math.max(12, Math.round(r.particleCount * 0.16)), c = "particleLife" in s ? o.particleLife : Math.max(28, r.particleLife * 0.48), u = "initialSpeedMin" in s ? o.initialSpeedMin : Math.max(0.4, r.initialSpeedMin * 0.38), d = "initialSpeedMax" in s ? o.initialSpeedMax : Math.max(1, r.initialSpeedMax * 0.56);
427
+ return this.createExplosion(e, t, {
428
+ ...o,
429
+ particleCount: l,
430
+ particleLife: c,
431
+ initialSpeedMin: u,
432
+ initialSpeedMax: d,
433
+ enableSecondaryBurst: !1
434
+ });
435
+ }
436
+ createConfigFromParticle(e, t) {
437
+ return e ? {
438
+ ...t,
439
+ colorStart: e.startColor || t.colorStart,
440
+ colorEnd: e.endColor || t.colorEnd,
441
+ useRandomColors: !1,
442
+ textureType: e.textureType || t.textureType,
443
+ particleMinSize: e.size,
444
+ particleMaxSize: e.size,
445
+ particleLife: e.maxLife,
446
+ enableSecondaryBurst: !1
447
+ } : t;
448
+ }
449
+ createLaunch(e, t, i, a) {
450
+ const r = this.getConfig();
451
+ return new K(e, t, i, a, r);
452
+ }
453
+ }
454
+ function D(n) {
455
+ return Number.isFinite(Number(n)) ? Math.min(1, Math.max(0, Number(n))) : 0;
456
+ }
457
+ function H(n, e) {
458
+ const t = /* @__PURE__ */ new Set();
459
+ for (; t.size < e; )
460
+ t.add(y(0, n - 1));
461
+ return t;
462
+ }
463
+ class K {
464
+ constructor(e, t, i, a, r) {
465
+ this.x = e, this.y = t, this.targetX = i, this.targetY = a, this.color = r.colorStart, this.life = 1, this.path = [];
466
+ const s = i - e, o = a - t, l = Math.hypot(s, o) || 1, c = Math.min(18, Math.max(9, l / 28));
467
+ this.vx = s / l * c, this.vy = o / l * c;
468
+ }
469
+ update(e) {
470
+ return this.path.push({ x: this.x, y: this.y }), this.path.length > 18 && this.path.shift(), this.x += this.vx * e, this.y += this.vy * e, this.vy += 0.03 * e, Math.hypot(this.targetX - this.x, this.targetY - this.y) < 14 || this.vy > 1.5;
471
+ }
472
+ draw(e, t) {
473
+ e.save(), e.strokeStyle = "rgba(255, 230, 170, 0.35)", e.fillStyle = this.color, e.shadowColor = this.color, e.shadowBlur = 16, t && this.path.length > 1 && (e.beginPath(), this.path.forEach((i, a) => {
474
+ a === 0 ? e.moveTo(i.x, i.y) : e.lineTo(i.x, i.y);
475
+ }), e.stroke()), e.beginPath(), e.arc(this.x, this.y, 3, 0, Math.PI * 2), e.fill(), e.restore();
476
+ }
477
+ }
478
+ function W(n, e, t) {
479
+ if (n === "ring") {
480
+ const a = e / t * Math.PI * 2;
481
+ return { x: Math.cos(a), y: Math.sin(a), speedMultiplier: 1 };
482
+ }
483
+ if (n === "starburst") {
484
+ const s = y(0, 4) / 5 * Math.PI * 2 - Math.PI / 2 + h(-0.16, 0.16);
485
+ return { x: Math.cos(s), y: Math.sin(s), speedMultiplier: e % 3 === 0 ? 1.25 : 0.62 };
486
+ }
487
+ if (n === "flower") {
488
+ const a = e / t * Math.PI * 2, r = 0.58 + Math.abs(Math.sin(a * 6)) * 0.72;
489
+ return { x: Math.cos(a), y: Math.sin(a), speedMultiplier: r };
490
+ }
491
+ if (n === "willow") {
492
+ const a = h(-Math.PI * 0.92, -Math.PI * 0.08);
493
+ return { x: Math.cos(a), y: Math.sin(a), speedMultiplier: h(0.28, 1.35) };
494
+ }
495
+ if (n === "scatter") {
496
+ const a = h(0, Math.PI * 2);
497
+ return { x: Math.cos(a), y: Math.sin(a), speedMultiplier: h(0.2, 1.55) };
498
+ }
499
+ if (n === "heart") {
500
+ const a = h(0, Math.PI * 2), r = 16 * Math.pow(Math.sin(a), 3), s = -(13 * Math.cos(a) - 5 * Math.cos(2 * a) - 2 * Math.cos(3 * a) - Math.cos(4 * a)), o = Math.hypot(r, s) || 1;
501
+ return {
502
+ x: r / o,
503
+ y: s / o,
504
+ offsetX: r * 1.8,
505
+ offsetY: s * 1.8,
506
+ speedMultiplier: h(0.72, 1.12)
507
+ };
508
+ }
509
+ const i = h(0, Math.PI * 2);
510
+ return { x: Math.cos(i), y: Math.sin(i), speedMultiplier: Math.sqrt(Math.random()) };
511
+ }
512
+ class G {
513
+ constructor() {
514
+ M(this, "active", !1);
515
+ }
516
+ reset(e) {
517
+ this.active = !0, this.x = e.x, this.y = e.y, this.vx = e.vx, this.vy = e.vy, this.size = e.size, this.maxLife = e.life, this.life = e.life, this.startColor = e.startColor, this.endColor = e.endColor, this.textureType = e.textureType, this.config = e.config || null, this.rotation = h(0, Math.PI * 2), this.rotationVelocity = e.rotationVelocity, this.secondaryEligible = e.secondaryEligible, this.secondaryBurstConfig = e.secondaryBurstConfig || null, this.secondaryTriggered = !1, this.flickerSeed = h(0, 1e3), this.trail = [];
518
+ }
519
+ update(e, t) {
520
+ if (!this.active) return null;
521
+ const i = this.config || t, a = i.windEnabled ? i.windX : 0, r = i.windEnabled ? i.windY : 0;
522
+ return this.vx += a * e, this.vy += (i.gravity + r) * e, this.vx *= Math.pow(i.damping, e), this.vy *= Math.pow(i.damping, e), this.x += this.vx * e, this.y += this.vy * e, this.rotation += this.rotationVelocity * e, this.life -= e, i.enableTrails ? (this.trail.push({ x: this.x, y: this.y, life: this.life }), this.trail.length > 8 && this.trail.shift()) : this.trail.length = 0, i.enableSecondaryBurst && this.secondaryEligible && !this.secondaryTriggered && this.life < this.maxLife * 0.52 ? (this.secondaryTriggered = !0, this.active = !1, {
523
+ x: this.x,
524
+ y: this.y,
525
+ source: {
526
+ startColor: this.startColor,
527
+ endColor: this.endColor,
528
+ textureType: this.textureType,
529
+ size: this.size,
530
+ maxLife: this.maxLife,
531
+ config: i,
532
+ secondaryBurstConfig: this.secondaryBurstConfig
533
+ }
534
+ }) : (this.life <= 0 && (this.active = !1), null);
535
+ }
536
+ draw(e, t) {
537
+ if (!this.active) return;
538
+ const i = this.config || t, a = C(this.life / this.maxLife, 0, 1), r = 1 - a, s = b(this.size * i.sizeEndMultiplier, this.size, a), o = C(r + i.colorShiftRate * r * 8, 0, 1), l = U(this.startColor, this.endColor, o);
539
+ let c = a;
540
+ i.flicker && (c *= 0.72 + Math.abs(Math.sin(this.flickerSeed + this.life * 0.34)) * 0.34), i.enableTrails && this.trail.length > 1 && this.drawTrail(e, l, c, s), e.save(), e.translate(this.x, this.y), e.rotate(this.rotation), e.fillStyle = v(l, c), e.shadowColor = v(l, c), e.shadowBlur = 18 * i.glowIntensity, _(e, this.textureType || i.textureType, s), e.restore();
541
+ }
542
+ drawTrail(e, t, i, a) {
543
+ e.save(), e.lineCap = "round", e.lineJoin = "round";
544
+ for (let r = 1; r < this.trail.length; r += 1) {
545
+ const s = this.trail[r - 1], o = this.trail[r], l = r / this.trail.length;
546
+ e.strokeStyle = v(t, i * l * 0.26), e.lineWidth = Math.max(1, a * l * 0.8), e.beginPath(), e.moveTo(s.x, s.y), e.lineTo(o.x, o.y), e.stroke();
547
+ }
548
+ e.restore();
549
+ }
550
+ }
551
+ function _(n, e, t) {
552
+ if (e === "square") {
553
+ n.fillRect(-t, -t, t * 2, t * 2);
554
+ return;
555
+ }
556
+ if (e === "star") {
557
+ J(n, 5, t * 1.45, t * 0.58);
558
+ return;
559
+ }
560
+ if (e === "heart") {
561
+ Q(n, t * 1.25);
562
+ return;
563
+ }
564
+ n.beginPath(), n.arc(0, 0, t, 0, Math.PI * 2), n.fill();
565
+ }
566
+ function J(n, e, t, i) {
567
+ n.beginPath();
568
+ for (let a = 0; a < e * 2; a += 1) {
569
+ const r = a % 2 === 0 ? t : i, s = a * Math.PI / e - Math.PI / 2, o = Math.cos(s) * r, l = Math.sin(s) * r;
570
+ a === 0 ? n.moveTo(o, l) : n.lineTo(o, l);
571
+ }
572
+ n.closePath(), n.fill();
573
+ }
574
+ function Q(n, e) {
575
+ n.beginPath(), n.moveTo(0, e * 0.5), n.bezierCurveTo(e * 1.25, -e * 0.35, e * 0.65, -e * 1.35, 0, -e * 0.62), n.bezierCurveTo(-e * 0.65, -e * 1.35, -e * 1.25, -e * 0.35, 0, e * 0.5), n.fill();
576
+ }
577
+ class Z {
578
+ constructor(e, t = 1400) {
579
+ this.factory = e, this.pool = Array.from({ length: t }, () => new G()), this.activeCount = 0;
580
+ }
581
+ spawn(e) {
582
+ const t = this.pool.find((i) => !i.active);
583
+ return t ? (t.reset(e), this.activeCount += 1, !0) : !1;
584
+ }
585
+ addExplosion(e) {
586
+ for (const t of e)
587
+ this.spawn(t);
588
+ }
589
+ update(e, t) {
590
+ const i = [];
591
+ let a = 0;
592
+ for (const r of this.pool) {
593
+ if (!r.active) continue;
594
+ const s = r.update(e, t);
595
+ s && i.push(s), r.active && (a += 1);
596
+ }
597
+ this.activeCount = a;
598
+ for (const r of i.slice(0, 12))
599
+ this.addExplosion(this.factory.createSubExplosion(r.x, r.y, r.source));
600
+ }
601
+ draw(e, t) {
602
+ for (const i of this.pool)
603
+ i.draw(e, t);
604
+ }
605
+ clear() {
606
+ for (const e of this.pool)
607
+ e.active = !1, e.trail.length = 0;
608
+ this.activeCount = 0;
609
+ }
610
+ }
611
+ class ee {
612
+ constructor(e, t) {
613
+ this.canvas = e, this.ctx = e.getContext("2d", { alpha: !1 }), this.backgroundManager = t, this.width = 0, this.height = 0, this.pixelRatio = 1;
614
+ }
615
+ resize() {
616
+ const e = this.canvas.getBoundingClientRect();
617
+ this.pixelRatio = Math.min(window.devicePixelRatio || 1, 2), this.width = Math.max(1, Math.floor(e.width)), this.height = Math.max(1, Math.floor(e.height)), this.canvas.width = Math.floor(this.width * this.pixelRatio), this.canvas.height = Math.floor(this.height * this.pixelRatio), this.ctx.setTransform(this.pixelRatio, 0, 0, this.pixelRatio, 0, 0), this.backgroundManager.resize(this.width, this.height);
618
+ }
619
+ clearFrame(e, t = !1) {
620
+ if (!e.enableTrails || t) {
621
+ this.backgroundManager.draw(this.ctx, this.width, this.height, e);
622
+ return;
623
+ }
624
+ this.ctx.save(), this.ctx.globalCompositeOperation = "source-over", this.ctx.fillStyle = `rgba(3, 6, 12, ${e.fadeTrail})`, this.ctx.fillRect(0, 0, this.width, this.height), this.ctx.restore();
625
+ }
626
+ draw(e, t, i) {
627
+ this.ctx.save(), this.ctx.globalCompositeOperation = e.glowIntensity > 0 ? "lighter" : "source-over";
628
+ for (const a of i)
629
+ a.draw(this.ctx, e.showLaunchTrail);
630
+ t.draw(this.ctx, e), this.ctx.restore();
631
+ }
632
+ }
633
+ class L {
634
+ constructor(e = {}) {
635
+ var i, a;
636
+ const t = te(e.canvasId ?? e.canvas);
637
+ if (!t)
638
+ throw new Error("FireworksSimulator requires a valid canvas or canvasId.");
639
+ this.canvas = t, this.prefersReducedMotion = e.prefersReducedMotion ?? ((a = (i = window.matchMedia) == null ? void 0 : i.call(window, "(prefers-reduced-motion: reduce)")) == null ? void 0 : a.matches) ?? !1, this.initialConfig = this.createInitialConfig(e.config), this.config = { ...this.initialConfig }, this.eventBus = e.eventBus ?? new j(), this.onConfigChange = e.onConfigChange ?? (() => {
640
+ }), this.onFpsChange = e.onFpsChange ?? (() => {
641
+ }), this.onLowFps = e.onLowFps ?? (() => {
642
+ }), this.advancedMode = !!e.advancedMode, this.createParticles = typeof e.createParticles == "function" ? e.createParticles : null, this.backgroundManager = new O(), this.renderer = new ee(t, this.backgroundManager), this.fireworkFactory = new q(() => this.config), this.particleSystem = new Z(this.fireworkFactory), this.shells = [], this.paused = !1, this.destroyed = !1, this.lastTime = performance.now(), this.autoTimer = 0, this.fpsFrames = 0, this.fpsElapsed = 0, this.lowFpsTime = 0, this.warnedAboutPerformance = !1, this.handlePointerDown = this.handlePointerDown.bind(this), this.handleKeyDown = this.handleKeyDown.bind(this), this.handleResize = this.resize.bind(this), this.handleVisibilityChange = this.handleVisibilityChange.bind(this), this.animate = this.animate.bind(this), e.clickToLaunch !== !1 && this.canvas.addEventListener("pointerdown", this.handlePointerDown), e.keyboard !== !1 && this.canvas.addEventListener("keydown", this.handleKeyDown), e.autoResize !== !1 && window.addEventListener("resize", this.handleResize), e.pauseOnHidden !== !1 && document.addEventListener("visibilitychange", this.handleVisibilityChange), this.resize(), this.renderer.clearFrame(this.config, !0), e.initialLaunch !== !1 && window.setTimeout(() => {
643
+ this.destroyed || (this.launch(this.renderer.width * 0.34, this.renderer.height * 0.42), this.launch(this.renderer.width * 0.62, this.renderer.height * 0.34));
644
+ }, 300), this.animationId = requestAnimationFrame(this.animate);
645
+ }
646
+ createInitialConfig(e = {}) {
647
+ const t = {
648
+ ...p,
649
+ ...this.prefersReducedMotion ? {
650
+ particleCount: 70,
651
+ initialSpeedMin: 1.4,
652
+ initialSpeedMax: 4.2,
653
+ autoLaunch: !1,
654
+ enableTrails: !1
655
+ } : {},
656
+ ...e
657
+ };
658
+ return this.normalizeAutoLaunchIntervalPriority(t, e), g(t);
659
+ }
660
+ getConfig() {
661
+ return { ...this.config };
662
+ }
663
+ setConfig(e = {}) {
664
+ const t = { ...this.config, ...e };
665
+ return this.normalizeAutoLaunchIntervalPriority(t, e), this.config = g(t), this.notifyConfigChange(), this.renderer.clearFrame(this.config, !0), this.getConfig();
666
+ }
667
+ selectPreset(e) {
668
+ var a;
669
+ if (e === "default")
670
+ return this.setConfig(p);
671
+ const t = (a = e == null ? void 0 : e.startsWith) != null && a.call(e, "builtin:") ? e.replace("builtin:", "") : e, i = T[t];
672
+ if (!i)
673
+ throw new Error(`Unknown preset: ${e}`);
674
+ return this.setConfig({ ...p, ...i.config });
675
+ }
676
+ resetConfig(e) {
677
+ return e && (this.initialConfig = this.createInitialConfig(e)), this.config = { ...this.initialConfig }, this.clearParticles(), this.autoTimer = 0, this.warnedAboutPerformance = !1, this.notifyConfigChange(), this.renderer.clearFrame(this.config, !0), this.getConfig();
678
+ }
679
+ startAutoPlay(e) {
680
+ const t = { autoLaunch: !0 };
681
+ return Number.isFinite(Number(e)) && Object.assign(t, this.normalizeAutoPlayIntervalInput(e)), this.setConfig(t);
682
+ }
683
+ stopAutoPlay() {
684
+ return this.setConfig({ autoLaunch: !1 });
685
+ }
686
+ setAutoPlay(e, t) {
687
+ return e ? this.startAutoPlay(t) : this.stopAutoPlay();
688
+ }
689
+ setAdvancedMode(e, t) {
690
+ this.advancedMode = !!e, typeof t == "function" && (this.createParticles = t);
691
+ }
692
+ setParticleCreator(e) {
693
+ this.createParticles = typeof e == "function" ? e : null;
694
+ }
695
+ clearParticles() {
696
+ this.particleSystem.clear(), this.shells.length = 0, this.renderer.clearFrame(this.config, !0);
697
+ }
698
+ launch(e, t, i = {}) {
699
+ const a = i.withShell ?? this.config.showLaunchTrail;
700
+ this.launchAt(e, t, a);
701
+ }
702
+ launchRandom() {
703
+ const e = h(this.renderer.width * 0.16, this.renderer.width * 0.84), t = h(this.renderer.height * 0.16, this.renderer.height * 0.62);
704
+ this.launchAt(e, t, this.config.showLaunchTrail);
705
+ }
706
+ pause() {
707
+ this.paused = !0;
708
+ }
709
+ resume() {
710
+ this.paused = !1, this.lastTime = performance.now();
711
+ }
712
+ togglePause() {
713
+ return this.paused ? this.resume() : this.pause(), this.paused;
714
+ }
715
+ resize() {
716
+ this.renderer.resize(), this.renderer.clearFrame(this.config, !0);
717
+ }
718
+ setBackgroundImage(e, t, i) {
719
+ this.backgroundManager.setImage(
720
+ e,
721
+ () => {
722
+ this.setConfig({ backgroundType: "image" }), t == null || t();
723
+ },
724
+ i
725
+ );
726
+ }
727
+ destroy() {
728
+ this.destroyed = !0, cancelAnimationFrame(this.animationId), this.canvas.removeEventListener("pointerdown", this.handlePointerDown), this.canvas.removeEventListener("keydown", this.handleKeyDown), window.removeEventListener("resize", this.handleResize), document.removeEventListener("visibilitychange", this.handleVisibilityChange);
729
+ }
730
+ on(e, t) {
731
+ return this.eventBus.on(e, t);
732
+ }
733
+ launchAt(e, t, i = !0) {
734
+ if (i && this.config.showLaunchTrail) {
735
+ this.shells.push(this.fireworkFactory.createLaunch(e, this.renderer.height + 16, e, t));
736
+ return;
737
+ }
738
+ this.particleSystem.addExplosion(this.createExplosionParticles(e, t));
739
+ }
740
+ createExplosionParticles(e, t) {
741
+ if (!this.advancedMode || !this.createParticles)
742
+ return this.fireworkFactory.createExplosion(e, t);
743
+ const { particles: i, scopedConfig: a } = this.parseAdvancedResult(this.createParticles(this.getCanvasData(e, t)));
744
+ return Array.isArray(i) ? i.map((r) => this.normalizeParticle(r, e, t, a)) : [];
745
+ }
746
+ parseAdvancedResult(e) {
747
+ return Array.isArray(e) ? {
748
+ particles: e,
749
+ scopedConfig: null
750
+ } : e && typeof e == "object" && Array.isArray(e.particles) ? {
751
+ particles: e.particles,
752
+ scopedConfig: e.config ? g({ ...this.config, ...e.config }) : null
753
+ } : {
754
+ particles: [],
755
+ scopedConfig: null
756
+ };
757
+ }
758
+ getCanvasData(e, t) {
759
+ return {
760
+ canvas: this.canvas,
761
+ ctx: this.renderer.ctx,
762
+ x: e,
763
+ y: t,
764
+ width: this.renderer.width,
765
+ height: this.renderer.height,
766
+ pixelRatio: this.renderer.pixelRatio,
767
+ config: this.getConfig(),
768
+ random: h
769
+ };
770
+ }
771
+ normalizeParticle(e, t, i, a = null) {
772
+ const r = a || this.config, s = e.secondaryBurstConfig && typeof e.secondaryBurstConfig == "object" ? this.normalizeSecondaryBurstConfig(e.secondaryBurstConfig, r) : null;
773
+ return {
774
+ x: Number.isFinite(e.x) ? e.x : t,
775
+ y: Number.isFinite(e.y) ? e.y : i,
776
+ vx: Number.isFinite(e.vx) ? e.vx : 0,
777
+ vy: Number.isFinite(e.vy) ? e.vy : 0,
778
+ size: Number.isFinite(e.size) ? e.size : r.particleMinSize,
779
+ life: Number.isFinite(e.life) ? e.life : r.particleLife,
780
+ startColor: e.startColor || e.color || r.colorStart,
781
+ endColor: e.endColor || e.color || r.colorEnd,
782
+ textureType: e.textureType || r.textureType,
783
+ rotationVelocity: Number.isFinite(e.rotationVelocity) ? e.rotationVelocity : h(-r.rotationSpeed, r.rotationSpeed),
784
+ secondaryEligible: !!e.secondaryEligible,
785
+ secondaryBurstConfig: s,
786
+ config: a
787
+ };
788
+ }
789
+ normalizeSecondaryBurstConfig(e, t) {
790
+ const i = g({
791
+ ...t,
792
+ ...e,
793
+ enableSecondaryBurst: !1
794
+ }), a = { enableSecondaryBurst: !1 };
795
+ for (const r of Object.keys(e))
796
+ r !== "enableSecondaryBurst" && r in i && (a[r] = i[r]);
797
+ return a;
798
+ }
799
+ animate(e) {
800
+ if (this.destroyed) return;
801
+ const t = Math.min(50, e - this.lastTime);
802
+ if (this.lastTime = e, !this.paused) {
803
+ const i = t / 16.6667;
804
+ this.renderer.clearFrame(this.config);
805
+ for (let a = this.shells.length - 1; a >= 0; a -= 1)
806
+ if (this.shells[a].update(i)) {
807
+ const r = this.shells.splice(a, 1)[0];
808
+ this.particleSystem.addExplosion(this.createExplosionParticles(r.targetX, r.targetY));
809
+ }
810
+ this.particleSystem.update(i, this.config), this.renderer.draw(this.config, this.particleSystem, this.shells), this.config.autoLaunch && (this.autoTimer += t, this.autoTimer >= this.getAutoLaunchIntervalMs() && (this.autoTimer = 0, this.launchRandom())), this.updateFps(t);
811
+ }
812
+ this.animationId = requestAnimationFrame(this.animate);
813
+ }
814
+ updateFps(e) {
815
+ if (this.fpsFrames += 1, this.fpsElapsed += e, this.fpsElapsed < 500) return;
816
+ const t = Math.round(this.fpsFrames * 1e3 / this.fpsElapsed);
817
+ this.onFpsChange(t), this.eventBus.emit("fps:change", t), this.lowFpsTime = t < 45 ? this.lowFpsTime + this.fpsElapsed : 0, this.lowFpsTime > 2200 && !this.warnedAboutPerformance && (this.warnedAboutPerformance = !0, this.config.particleCount = Math.max(60, Math.round(this.config.particleCount * 0.72)), this.notifyConfigChange(), this.onLowFps(this.getConfig()), this.eventBus.emit("performance:low-fps", this.getConfig())), this.fpsFrames = 0, this.fpsElapsed = 0;
818
+ }
819
+ notifyConfigChange() {
820
+ const e = this.getConfig();
821
+ this.onConfigChange(e), this.eventBus.emit("simulator:config-change", { config: e });
822
+ }
823
+ handlePointerDown(e) {
824
+ const t = this.canvas.getBoundingClientRect();
825
+ this.launch(e.clientX - t.left, e.clientY - t.top);
826
+ }
827
+ handleKeyDown(e) {
828
+ e.code === "Space" ? (e.preventDefault(), this.togglePause()) : e.code === "Enter" && (e.preventDefault(), this.launchRandom());
829
+ }
830
+ handleVisibilityChange() {
831
+ document.hidden ? this.pause() : this.lastTime = performance.now();
832
+ }
833
+ getAutoLaunchIntervalMs() {
834
+ const e = Number(this.config.autoLaunchIntervalSeconds);
835
+ return Number.isFinite(e) ? e * 1e3 : this.config.autoInterval;
836
+ }
837
+ normalizeAutoPlayIntervalInput(e) {
838
+ const t = Number(e);
839
+ return t > 10 ? { autoInterval: t } : { autoLaunchIntervalSeconds: t };
840
+ }
841
+ normalizeAutoLaunchIntervalPriority(e, t) {
842
+ "autoInterval" in t && !("autoLaunchIntervalSeconds" in t) && delete e.autoLaunchIntervalSeconds, "autoLaunchIntervalSeconds" in t && !("autoInterval" in t) && delete e.autoInterval;
843
+ }
844
+ }
845
+ function te(n) {
846
+ return typeof n == "string" ? document.getElementById(n) : n;
847
+ }
848
+ function I(n, e = {}) {
849
+ return new L(ne(n, e));
850
+ }
851
+ function ie(n, e = {}) {
852
+ return I(n, e);
853
+ }
854
+ const ae = {
855
+ create: I,
856
+ init: ie,
857
+ FireworksSimulator: L,
858
+ builtInPresets: T,
859
+ defaultConfig: p,
860
+ sanitizeConfig: g
861
+ };
862
+ typeof window < "u" && (window.SparkFireworks = ae);
863
+ function ne(n, e) {
864
+ const t = typeof n == "string" ? { canvasId: n } : { canvas: n };
865
+ return e && typeof e == "object" && ("config" in e || "onConfigChange" in e || "onFpsChange" in e || "onLowFps" in e || "clickToLaunch" in e || "keyboard" in e || "autoResize" in e || "pauseOnHidden" in e || "initialLaunch" in e || "advancedMode" in e || "createParticles" in e) ? { ...e, ...t } : { ...t, config: e };
866
+ }
867
+ export {
868
+ L as FireworksSimulator,
869
+ T as builtInPresets,
870
+ I as create,
871
+ ae as default,
872
+ p as defaultConfig,
873
+ ie as init,
874
+ g as sanitizeConfig
875
+ };