fireworks-engine 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.
package/src/runtime.js ADDED
@@ -0,0 +1,796 @@
1
+ import { randomRange } from './mathResolvers.js';
2
+ import {
3
+ isPlainRecord,
4
+ normalizeGroupedPublicOptions,
5
+ } from './valueNormalizers.js';
6
+ import { buildEngineConfig } from './configBuilder.js';
7
+ import {
8
+ resolveFireworksEnginePreset,
9
+ resolveFireworksLaunchPreset,
10
+ } from './presets.js';
11
+ import { loop as renderLoop } from './renderer.js';
12
+ import * as lifecycle from './runtimeLifecycle.js';
13
+ import { launchBurst } from './burstSpawn.js';
14
+ import {
15
+ launchRocket,
16
+ launchRocketAt,
17
+ launchRocketFromTo,
18
+ } from './rocketLaunch.js';
19
+
20
+ export { FIREWORKS_ENGINE_PRESETS, resolveFireworksEnginePreset } from './presets.js';
21
+
22
+ const FINISH_WHEN_DONE_POLL_MS = 120;
23
+ const DEFAULT_CONFETTI_OPTIONS = Object.freeze({
24
+ timeScale: 3,
25
+ sizeScale: 1,
26
+ rocket: Object.freeze({ enabled: false }),
27
+ rocketTailEmbers: Object.freeze({ enabled: false }),
28
+ burstForms: Object.freeze({
29
+ enabled: true,
30
+ shape: 'circle|square',
31
+ colorMode: 'palette',
32
+ count: 100,
33
+ speedMin: 100,
34
+ speedMax: 600,
35
+ directionDeg: 90,
36
+ coneDeg: 100,
37
+ gravity: 80,
38
+ lifeMin: 8,
39
+ lifeMax: 8,
40
+ sizeMin: 3.3,
41
+ sizeMax: 4.4,
42
+ wobble: 0.4,
43
+ spin: 0.3,
44
+ spinCoverage: 0.92,
45
+ spinDuration: 2,
46
+ spinPause: 2,
47
+ highlight: 0,
48
+ highlightAngleDeg: 0,
49
+ highlightWidth: 0.18,
50
+ }),
51
+ burstSparks: Object.freeze({ enabled: false }),
52
+ burstCenterFlash: Object.freeze({ enabled: false }),
53
+ burstTwinkleCloud: Object.freeze({ enabled: false }),
54
+ });
55
+
56
+ const START_MODE_DEFAULTS = {
57
+ rocket: {
58
+ rocketCount: 2,
59
+ rocketDelayMs: 220,
60
+ totalDurationMs: 2200,
61
+ },
62
+ burst: {
63
+ bursts: 4,
64
+ durationMs: 1200,
65
+ finishDelayMs: 500,
66
+ xMinRatio: 0.2,
67
+ xMaxRatio: 0.8,
68
+ yMinRatio: 0.15,
69
+ yMaxRatio: 0.45,
70
+ },
71
+ };
72
+
73
+ function hasOwnOption(options, key) {
74
+ return Object.prototype.hasOwnProperty.call(options, key);
75
+ }
76
+
77
+ function mergeDefined(target, patch) {
78
+ const next = { ...target };
79
+ const keys = Object.keys(patch);
80
+
81
+ for (let index = 0; index < keys.length; index += 1) {
82
+ const key = keys[index];
83
+ if (patch[key] !== undefined) {
84
+ next[key] = patch[key];
85
+ }
86
+ }
87
+
88
+ return next;
89
+ }
90
+
91
+ function extractExplicitLaunchOptions(options = {}) {
92
+ if (!isPlainRecord(options)) {
93
+ return {};
94
+ }
95
+
96
+ const explicitOptions = {};
97
+
98
+ if (hasOwnOption(options, 'timeScale')) {
99
+ explicitOptions.timeScale = options.timeScale;
100
+ }
101
+ if (hasOwnOption(options, 'sizeScale')) {
102
+ explicitOptions.sizeScale = options.sizeScale;
103
+ }
104
+
105
+ if (isPlainRecord(options.rocket)) {
106
+ explicitOptions.rocket = { ...options.rocket };
107
+ }
108
+ if (isPlainRecord(options.rocketTailEmbers)) {
109
+ explicitOptions.rocketTailEmbers = { ...options.rocketTailEmbers };
110
+ }
111
+ if (isPlainRecord(options.burstForms)) {
112
+ explicitOptions.burstForms = { ...options.burstForms };
113
+ }
114
+ if (isPlainRecord(options.burstSparks)) {
115
+ explicitOptions.burstSparks = { ...options.burstSparks };
116
+ }
117
+ if (isPlainRecord(options.burstCenterFlash)) {
118
+ explicitOptions.burstCenterFlash = { ...options.burstCenterFlash };
119
+ }
120
+ if (isPlainRecord(options.burstTwinkleCloud)) {
121
+ explicitOptions.burstTwinkleCloud = { ...options.burstTwinkleCloud };
122
+ }
123
+
124
+ return explicitOptions;
125
+ }
126
+
127
+ function mergeExplicitLaunchOptions(currentOptions = {}, patch = {}) {
128
+ const nextOptions = { ...currentOptions };
129
+
130
+ if (hasOwnOption(patch, 'timeScale')) {
131
+ nextOptions.timeScale = patch.timeScale;
132
+ }
133
+ if (hasOwnOption(patch, 'sizeScale')) {
134
+ nextOptions.sizeScale = patch.sizeScale;
135
+ }
136
+
137
+ if (isPlainRecord(patch.rocket)) {
138
+ nextOptions.rocket = mergeDefined(nextOptions.rocket ?? {}, patch.rocket);
139
+ }
140
+ if (isPlainRecord(patch.rocketTailEmbers)) {
141
+ nextOptions.rocketTailEmbers = mergeDefined(nextOptions.rocketTailEmbers ?? {}, patch.rocketTailEmbers);
142
+ }
143
+ if (isPlainRecord(patch.burstForms)) {
144
+ nextOptions.burstForms = mergeDefined(nextOptions.burstForms ?? {}, patch.burstForms);
145
+ }
146
+ if (isPlainRecord(patch.burstSparks)) {
147
+ nextOptions.burstSparks = mergeDefined(nextOptions.burstSparks ?? {}, patch.burstSparks);
148
+ }
149
+ if (isPlainRecord(patch.burstCenterFlash)) {
150
+ nextOptions.burstCenterFlash = mergeDefined(nextOptions.burstCenterFlash ?? {}, patch.burstCenterFlash);
151
+ }
152
+ if (isPlainRecord(patch.burstTwinkleCloud)) {
153
+ nextOptions.burstTwinkleCloud = mergeDefined(nextOptions.burstTwinkleCloud ?? {}, patch.burstTwinkleCloud);
154
+ }
155
+
156
+ return nextOptions;
157
+ }
158
+
159
+ function resolveLifecycleHook(callback, fallback = null) {
160
+ if (callback === null) {
161
+ return null;
162
+ }
163
+
164
+ if (typeof callback === 'function') {
165
+ return callback;
166
+ }
167
+
168
+ return fallback;
169
+ }
170
+
171
+ function resolveLifecycleHooks(options = {}, fallbackHooks = {}) {
172
+ return {
173
+ onStart: resolveLifecycleHook(options.onStart, fallbackHooks.onStart ?? null),
174
+ onStop: resolveLifecycleHook(options.onStop, fallbackHooks.onStop ?? null),
175
+ onFinish: resolveLifecycleHook(options.onFinish, fallbackHooks.onFinish ?? null),
176
+ };
177
+ }
178
+
179
+ function invokeLifecycleHook(callback, engine) {
180
+ if (typeof callback !== 'function') {
181
+ return;
182
+ }
183
+
184
+ try {
185
+ callback(engine);
186
+ } catch {
187
+ // Keep runtime lifecycle stable even when user hooks throw.
188
+ }
189
+ }
190
+
191
+ function applyTimeScaleFromLaunchOptions(engine, options) {
192
+ if (!isPlainRecord(options) || !hasOwnOption(options, 'timeScale')) {
193
+ return;
194
+ }
195
+
196
+ const nextTimeScale = Number(options.timeScale);
197
+ if (!Number.isFinite(nextTimeScale)) {
198
+ return;
199
+ }
200
+
201
+ engine.updateOptions({ timeScale: nextTimeScale });
202
+ }
203
+
204
+ function shouldSkipHiddenLaunch(engine) {
205
+ return lifecycle.isDocumentHidden(engine);
206
+ }
207
+
208
+ function mergeConfettiOptions(engineOverrides = {}, launchOverrides = {}) {
209
+ const safeEngineOverrides = isPlainRecord(engineOverrides) ? engineOverrides : {};
210
+ const safeLaunchOverrides = isPlainRecord(launchOverrides) ? launchOverrides : {};
211
+
212
+ return {
213
+ ...DEFAULT_CONFETTI_OPTIONS,
214
+ ...safeEngineOverrides,
215
+ ...safeLaunchOverrides,
216
+ rocket: {
217
+ ...DEFAULT_CONFETTI_OPTIONS.rocket,
218
+ ...(isPlainRecord(safeEngineOverrides.rocket) ? safeEngineOverrides.rocket : {}),
219
+ ...(isPlainRecord(safeLaunchOverrides.rocket) ? safeLaunchOverrides.rocket : {}),
220
+ enabled: false,
221
+ },
222
+ rocketTailEmbers: {
223
+ ...DEFAULT_CONFETTI_OPTIONS.rocketTailEmbers,
224
+ ...(isPlainRecord(safeEngineOverrides.rocketTailEmbers) ? safeEngineOverrides.rocketTailEmbers : {}),
225
+ ...(isPlainRecord(safeLaunchOverrides.rocketTailEmbers) ? safeLaunchOverrides.rocketTailEmbers : {}),
226
+ },
227
+ burstForms: {
228
+ ...DEFAULT_CONFETTI_OPTIONS.burstForms,
229
+ ...(isPlainRecord(safeEngineOverrides.burstForms) ? safeEngineOverrides.burstForms : {}),
230
+ ...(isPlainRecord(safeLaunchOverrides.burstForms) ? safeLaunchOverrides.burstForms : {}),
231
+ },
232
+ burstSparks: {
233
+ ...DEFAULT_CONFETTI_OPTIONS.burstSparks,
234
+ ...(isPlainRecord(safeEngineOverrides.burstSparks) ? safeEngineOverrides.burstSparks : {}),
235
+ ...(isPlainRecord(safeLaunchOverrides.burstSparks) ? safeLaunchOverrides.burstSparks : {}),
236
+ },
237
+ burstCenterFlash: {
238
+ ...DEFAULT_CONFETTI_OPTIONS.burstCenterFlash,
239
+ ...(isPlainRecord(safeEngineOverrides.burstCenterFlash) ? safeEngineOverrides.burstCenterFlash : {}),
240
+ ...(isPlainRecord(safeLaunchOverrides.burstCenterFlash) ? safeLaunchOverrides.burstCenterFlash : {}),
241
+ },
242
+ burstTwinkleCloud: {
243
+ ...DEFAULT_CONFETTI_OPTIONS.burstTwinkleCloud,
244
+ ...(isPlainRecord(safeEngineOverrides.burstTwinkleCloud) ? safeEngineOverrides.burstTwinkleCloud : {}),
245
+ ...(isPlainRecord(safeLaunchOverrides.burstTwinkleCloud) ? safeLaunchOverrides.burstTwinkleCloud : {}),
246
+ },
247
+ };
248
+ }
249
+
250
+ function mergePresetLaunchOptions(presetOptions = {}, launchOverrides = {}) {
251
+ const safePresetOptions = isPlainRecord(presetOptions) ? presetOptions : {};
252
+ const safeLaunchOverrides = isPlainRecord(launchOverrides) ? launchOverrides : {};
253
+
254
+ return {
255
+ count: safeLaunchOverrides.count ?? safePresetOptions.count,
256
+ timeScale: safeLaunchOverrides.timeScale ?? safePresetOptions.timeScale,
257
+ sizeScale: safeLaunchOverrides.sizeScale ?? safePresetOptions.sizeScale,
258
+ simple: safeLaunchOverrides.simple ?? safePresetOptions.simple,
259
+ rocket: {
260
+ ...(isPlainRecord(safePresetOptions.rocket) ? safePresetOptions.rocket : {}),
261
+ ...(isPlainRecord(safeLaunchOverrides.rocket) ? safeLaunchOverrides.rocket : {}),
262
+ },
263
+ rocketTailEmbers: {
264
+ ...(isPlainRecord(safePresetOptions.rocketTailEmbers) ? safePresetOptions.rocketTailEmbers : {}),
265
+ ...(isPlainRecord(safeLaunchOverrides.rocketTailEmbers) ? safeLaunchOverrides.rocketTailEmbers : {}),
266
+ },
267
+ burstForms: {
268
+ ...(isPlainRecord(safePresetOptions.burstForms) ? safePresetOptions.burstForms : {}),
269
+ ...(isPlainRecord(safeLaunchOverrides.burstForms) ? safeLaunchOverrides.burstForms : {}),
270
+ },
271
+ burstSparks: {
272
+ ...(isPlainRecord(safePresetOptions.burstSparks) ? safePresetOptions.burstSparks : {}),
273
+ ...(isPlainRecord(safeLaunchOverrides.burstSparks) ? safeLaunchOverrides.burstSparks : {}),
274
+ },
275
+ burstCenterFlash: {
276
+ ...(isPlainRecord(safePresetOptions.burstCenterFlash) ? safePresetOptions.burstCenterFlash : {}),
277
+ ...(isPlainRecord(safeLaunchOverrides.burstCenterFlash) ? safeLaunchOverrides.burstCenterFlash : {}),
278
+ },
279
+ burstTwinkleCloud: {
280
+ ...(isPlainRecord(safePresetOptions.burstTwinkleCloud) ? safePresetOptions.burstTwinkleCloud : {}),
281
+ ...(isPlainRecord(safeLaunchOverrides.burstTwinkleCloud) ? safeLaunchOverrides.burstTwinkleCloud : {}),
282
+ },
283
+ };
284
+ }
285
+
286
+ export class FireworksEngine {
287
+ constructor(options = {}) {
288
+ const normalizedOptions = normalizeGroupedPublicOptions(options);
289
+ this.explicitLaunchOptions = extractExplicitLaunchOptions(options);
290
+ const engineConfig = buildEngineConfig(normalizedOptions);
291
+ const {
292
+ refs,
293
+ toggles,
294
+ timeScale,
295
+ sizeScale,
296
+ rocket,
297
+ rocketTailEmbers,
298
+ burstForms,
299
+ burstSparks,
300
+ burstCenterFlash,
301
+ burstTwinkleCloud,
302
+ performance,
303
+ } = engineConfig;
304
+
305
+ this.documentRef = refs.documentRef;
306
+ this.windowRef = refs.windowRef;
307
+ this.rootRef = refs.rootRef;
308
+ this.prefersReducedMotionRef = refs.prefersReducedMotionRef;
309
+ this.requestAnimationFrameRef = refs.requestAnimationFrameRef;
310
+ this.cancelAnimationFrameRef = refs.cancelAnimationFrameRef;
311
+ this.setTimeoutRef = refs.setTimeoutRef;
312
+ this.clearTimeoutRef = refs.clearTimeoutRef;
313
+ this.nowRef = refs.nowRef;
314
+ this.maxDevicePixelRatio = refs.maxDevicePixelRatio;
315
+ this.canvasZIndex = refs.canvasZIndex;
316
+
317
+ this.canvas = null;
318
+ this.ctx = null;
319
+ this.width = 0;
320
+ this.height = 0;
321
+ this.dpr = 1;
322
+
323
+ this.particles = [];
324
+ this.formPool = [];
325
+ this.sparkPool = [];
326
+ this.emberPool = [];
327
+ this.spriteCache = new Map();
328
+ this.rockets = [];
329
+ this.flashes = [];
330
+ this.twinkleClouds = [];
331
+ this.burstQueue = [];
332
+ this.running = false;
333
+ this.rafId = null;
334
+ this.lastTime = 0;
335
+ this.timeoutIds = new Set();
336
+ this.pendingStartConfig = null;
337
+ this.hasResizeListener = false;
338
+ this.hasResizeObserver = false;
339
+ this.resizeObserver = null;
340
+ this.hasDomReadyListener = false;
341
+ this.defaultLifecycleHooks = resolveLifecycleHooks(normalizedOptions);
342
+ this.activeLifecycleHooks = { ...this.defaultLifecycleHooks };
343
+ this.finishLifecycleNotified = false;
344
+
345
+ this.enableBurstQueueThrottling = toggles.enableBurstQueueThrottling;
346
+ this.timeScale = timeScale;
347
+ this.sizeScale = sizeScale;
348
+
349
+ this.rocket = rocket;
350
+ this.rocketTailEmbers = rocketTailEmbers;
351
+ this.burstForms = burstForms;
352
+ this.burstSparks = burstSparks;
353
+ this.burstCenterFlash = burstCenterFlash;
354
+ this.burstTwinkleCloud = burstTwinkleCloud;
355
+
356
+ this.performance = performance;
357
+ this.maxBurstParticlesPerFrame = this.performance.maxBurstParticlesPerFrame;
358
+ this.particlePressureThreshold = this.performance.particlePressureThreshold;
359
+ this.fastRectRadiusThreshold = this.performance.fastRectRadiusThreshold;
360
+ this.renderPressureMode = false;
361
+ this.renderCachesClearedForIdle = true;
362
+
363
+ this.resize = this.resize.bind(this);
364
+ this.loopFrame = (now) => renderLoop(this, now);
365
+ this.handleDOMContentLoaded = () => lifecycle.handleDOMContentLoaded(this);
366
+ }
367
+
368
+ resize() {
369
+ lifecycle.resizeCanvas(this);
370
+ }
371
+
372
+ launchRocket(options = {}) {
373
+ if (shouldSkipHiddenLaunch(this)) {
374
+ return false;
375
+ }
376
+ applyTimeScaleFromLaunchOptions(this, options);
377
+ return launchRocket(this, options);
378
+ }
379
+
380
+ boom(x, y, options = {}) {
381
+ if (shouldSkipHiddenLaunch(this)) {
382
+ return false;
383
+ }
384
+ applyTimeScaleFromLaunchOptions(this, options);
385
+ return launchRocketAt(this, x, y, options);
386
+ }
387
+
388
+ burst(targetX, targetY, options = {}) {
389
+ if (shouldSkipHiddenLaunch(this)) {
390
+ return false;
391
+ }
392
+ applyTimeScaleFromLaunchOptions(this, options);
393
+ if (!Number.isFinite(targetX) || !Number.isFinite(targetY)) {
394
+ return false;
395
+ }
396
+
397
+ if (this.running) {
398
+ return launchBurst(this, targetX, targetY, options);
399
+ }
400
+
401
+ return launchRocketAt(this, targetX, targetY, {
402
+ ...options,
403
+ rocket: {
404
+ ...options.rocket,
405
+ enabled: false,
406
+ },
407
+ });
408
+ }
409
+
410
+ confetti(targetX, targetY, options = {}) {
411
+ return this.burst(targetX, targetY, mergeConfettiOptions(this.explicitLaunchOptions, options));
412
+ }
413
+
414
+ launchRocketAt(targetX, targetY, options = {}) {
415
+ if (shouldSkipHiddenLaunch(this)) {
416
+ return false;
417
+ }
418
+ applyTimeScaleFromLaunchOptions(this, options);
419
+ return launchRocketAt(this, targetX, targetY, options);
420
+ }
421
+
422
+ launchPresetAt(name, targetX, targetY, options = {}) {
423
+ if (shouldSkipHiddenLaunch(this)) {
424
+ return false;
425
+ }
426
+ if (!Number.isFinite(targetX) || !Number.isFinite(targetY)) {
427
+ return false;
428
+ }
429
+
430
+ if (!lifecycle.ensureCanvas(this)) {
431
+ return false;
432
+ }
433
+
434
+ const launchPreset = resolveFireworksLaunchPreset(name);
435
+ if (launchPreset) {
436
+ const mergedOptions = mergePresetLaunchOptions(launchPreset.options ?? {}, options);
437
+ const count = Number.isFinite(mergedOptions.count)
438
+ ? Math.max(0, Math.floor(mergedOptions.count))
439
+ : 1;
440
+ if (count <= 0) {
441
+ return false;
442
+ }
443
+
444
+ const launchOptions = { ...mergedOptions };
445
+ delete launchOptions.count;
446
+
447
+ applyTimeScaleFromLaunchOptions(this, launchOptions);
448
+
449
+ let launched = false;
450
+ for (let index = 0; index < count; index += 1) {
451
+ const nextLaunched = typeof launchPreset.run === 'function'
452
+ ? launchPreset.run(this, targetX, targetY, launchOptions)
453
+ : false;
454
+ launched = launched || nextLaunched;
455
+ if (!nextLaunched) {
456
+ break;
457
+ }
458
+ }
459
+
460
+ return launched;
461
+ }
462
+
463
+ const presetOptions = resolveFireworksEnginePreset(name);
464
+ const mergedOptions = mergePresetLaunchOptions(presetOptions, options);
465
+ const count = Number.isFinite(mergedOptions.count)
466
+ ? Math.max(0, Math.floor(mergedOptions.count))
467
+ : 1;
468
+ if (count <= 0) {
469
+ return false;
470
+ }
471
+
472
+ const launchOptions = { ...mergedOptions };
473
+ delete launchOptions.count;
474
+ const shouldUseRocketFlight = presetOptions.mode === 'rocket'
475
+ && launchOptions.rocket?.enabled !== false;
476
+ if (!shouldUseRocketFlight) {
477
+ launchOptions.rocket = {
478
+ ...(isPlainRecord(launchOptions.rocket) ? launchOptions.rocket : {}),
479
+ enabled: false,
480
+ };
481
+ }
482
+
483
+ applyTimeScaleFromLaunchOptions(this, launchOptions);
484
+ let launched = false;
485
+ for (let index = 0; index < count; index += 1) {
486
+ const nextLaunched = launchRocketAt(this, targetX, targetY, launchOptions);
487
+ launched = launched || nextLaunched;
488
+ if (!nextLaunched) {
489
+ break;
490
+ }
491
+ }
492
+
493
+ return launched;
494
+ }
495
+
496
+ launchRocketFromTo(startX, startY, targetX, targetY, options = {}) {
497
+ if (shouldSkipHiddenLaunch(this)) {
498
+ return false;
499
+ }
500
+ applyTimeScaleFromLaunchOptions(this, options);
501
+ return launchRocketFromTo(this, startX, startY, targetX, targetY, options);
502
+ }
503
+
504
+ updateOptions(options = {}) {
505
+ if (!isPlainRecord(options)) {
506
+ return;
507
+ }
508
+ this.explicitLaunchOptions = mergeExplicitLaunchOptions(this.explicitLaunchOptions, options);
509
+
510
+ const normalizedOptions = normalizeGroupedPublicOptions(options);
511
+ const nextOptions = {
512
+ timeScale: this.timeScale,
513
+ sizeScale: this.sizeScale,
514
+ performance: {
515
+ enableBurstQueueThrottling: this.enableBurstQueueThrottling,
516
+ maxBurstParticlesPerFrame: this.maxBurstParticlesPerFrame,
517
+ particlePressureThreshold: this.particlePressureThreshold,
518
+ fastRectRadiusThreshold: this.fastRectRadiusThreshold,
519
+ },
520
+ rocket: this.rocket,
521
+ rocketTailEmbers: this.rocketTailEmbers,
522
+ burstForms: this.burstForms,
523
+ burstSparks: this.burstSparks,
524
+ burstCenterFlash: this.burstCenterFlash,
525
+ burstTwinkleCloud: this.burstTwinkleCloud,
526
+ };
527
+
528
+ let hasConfigPatch = false;
529
+
530
+ if (hasOwnOption(options, 'timeScale')) {
531
+ nextOptions.timeScale = normalizedOptions.timeScale;
532
+ hasConfigPatch = true;
533
+ }
534
+
535
+ if (hasOwnOption(options, 'sizeScale')) {
536
+ nextOptions.sizeScale = normalizedOptions.sizeScale;
537
+ hasConfigPatch = true;
538
+ }
539
+
540
+ if (isPlainRecord(options.performance)) {
541
+ nextOptions.performance = mergeDefined(
542
+ nextOptions.performance,
543
+ normalizedOptions.performance,
544
+ );
545
+ hasConfigPatch = true;
546
+ }
547
+
548
+ if (isPlainRecord(options.rocket)) {
549
+ nextOptions.rocket = mergeDefined(nextOptions.rocket, normalizedOptions.rocket);
550
+ hasConfigPatch = true;
551
+ }
552
+
553
+ if (isPlainRecord(options.rocketTailEmbers)) {
554
+ nextOptions.rocketTailEmbers = mergeDefined(
555
+ nextOptions.rocketTailEmbers,
556
+ normalizedOptions.rocketTailEmbers,
557
+ );
558
+ hasConfigPatch = true;
559
+ }
560
+
561
+ if (isPlainRecord(options.burstForms)) {
562
+ nextOptions.burstForms = mergeDefined(
563
+ nextOptions.burstForms,
564
+ normalizedOptions.burstForms,
565
+ );
566
+ hasConfigPatch = true;
567
+ }
568
+
569
+ if (isPlainRecord(options.burstSparks)) {
570
+ nextOptions.burstSparks = mergeDefined(
571
+ nextOptions.burstSparks,
572
+ normalizedOptions.burstSparks,
573
+ );
574
+ hasConfigPatch = true;
575
+ }
576
+
577
+ if (isPlainRecord(options.burstCenterFlash)) {
578
+ nextOptions.burstCenterFlash = mergeDefined(
579
+ nextOptions.burstCenterFlash,
580
+ normalizedOptions.burstCenterFlash,
581
+ );
582
+ hasConfigPatch = true;
583
+ }
584
+
585
+ if (isPlainRecord(options.burstTwinkleCloud)) {
586
+ nextOptions.burstTwinkleCloud = mergeDefined(
587
+ nextOptions.burstTwinkleCloud,
588
+ normalizedOptions.burstTwinkleCloud,
589
+ );
590
+ hasConfigPatch = true;
591
+ }
592
+
593
+ if (hasConfigPatch) {
594
+ const nextConfig = buildEngineConfig(nextOptions);
595
+
596
+ this.timeScale = nextConfig.timeScale;
597
+ this.sizeScale = nextConfig.sizeScale;
598
+ this.enableBurstQueueThrottling = nextConfig.toggles.enableBurstQueueThrottling;
599
+
600
+ this.rocket = nextConfig.rocket;
601
+ this.rocketTailEmbers = nextConfig.rocketTailEmbers;
602
+ this.burstForms = nextConfig.burstForms;
603
+ this.burstSparks = nextConfig.burstSparks;
604
+ this.burstCenterFlash = nextConfig.burstCenterFlash;
605
+ this.burstTwinkleCloud = nextConfig.burstTwinkleCloud;
606
+
607
+ this.performance = nextConfig.performance;
608
+ this.maxBurstParticlesPerFrame = this.performance.maxBurstParticlesPerFrame;
609
+ this.particlePressureThreshold = this.performance.particlePressureThreshold;
610
+ this.fastRectRadiusThreshold = this.performance.fastRectRadiusThreshold;
611
+ }
612
+
613
+ if (
614
+ hasOwnOption(options, 'onStart')
615
+ || hasOwnOption(options, 'onStop')
616
+ || hasOwnOption(options, 'onFinish')
617
+ ) {
618
+ this.defaultLifecycleHooks = resolveLifecycleHooks(options, this.defaultLifecycleHooks);
619
+ this.activeLifecycleHooks = resolveLifecycleHooks(options, this.activeLifecycleHooks);
620
+ }
621
+ }
622
+
623
+ // Note: Renderer logic is decoupled; use engine directly in renderer for performance.
624
+
625
+ finishWhenDone() {
626
+ const check = () => {
627
+ if (!this.running) {
628
+ return;
629
+ }
630
+
631
+ if (
632
+ this.particles.length === 0
633
+ && this.rockets.length === 0
634
+ && this.flashes.length === 0
635
+ && this.twinkleClouds.length === 0
636
+ && this.burstQueue.length === 0
637
+ ) {
638
+ if (!this.finishLifecycleNotified) {
639
+ this.finishLifecycleNotified = true;
640
+ invokeLifecycleHook(this.activeLifecycleHooks.onFinish, this);
641
+ }
642
+ this.stop(false);
643
+ return;
644
+ }
645
+
646
+ lifecycle.scheduleTimeout(
647
+ this,
648
+ check,
649
+ lifecycle.resolveScaledDelay(this, FINISH_WHEN_DONE_POLL_MS),
650
+ );
651
+ };
652
+
653
+ check();
654
+ }
655
+
656
+ start(config = {}) {
657
+ const normalizedConfig = normalizeGroupedPublicOptions(config);
658
+ if (hasOwnOption(config, 'timeScale')) {
659
+ this.timeScale = normalizedConfig.timeScale;
660
+ }
661
+ if (lifecycle.shouldReduceMotion(this)) {
662
+ this.stop();
663
+ return false;
664
+ }
665
+
666
+ if (!lifecycle.ensureCanvas(this)) {
667
+ lifecycle.schedulePendingStart(this, normalizedConfig);
668
+ return false;
669
+ }
670
+
671
+ lifecycle.clearPendingStart(this);
672
+ this.stop(false);
673
+ this.activeLifecycleHooks = resolveLifecycleHooks(normalizedConfig, this.defaultLifecycleHooks);
674
+ this.finishLifecycleNotified = false;
675
+
676
+ if (!lifecycle.startLoop(this)) {
677
+ this.activeLifecycleHooks = { ...this.defaultLifecycleHooks };
678
+ return false;
679
+ }
680
+
681
+ if (normalizedConfig.mode === 'rocket') {
682
+ const rocketCount = normalizedConfig.rocketCount ?? START_MODE_DEFAULTS.rocket.rocketCount;
683
+ const rocketDelay = normalizedConfig.rocketDelay ?? START_MODE_DEFAULTS.rocket.rocketDelayMs;
684
+
685
+ for (let index = 0; index < rocketCount; index += 1) {
686
+ lifecycle.scheduleTimeout(this, () => {
687
+ launchRocket(this, normalizedConfig);
688
+ }, lifecycle.resolveScaledDelay(this, index * rocketDelay));
689
+ }
690
+ const finishDelay = normalizedConfig.totalDuration ?? START_MODE_DEFAULTS.rocket.totalDurationMs;
691
+ lifecycle.scheduleTimeout(
692
+ this,
693
+ () => this.finishWhenDone(),
694
+ lifecycle.resolveScaledDelay(this, finishDelay),
695
+ );
696
+ } else {
697
+ const bursts = normalizedConfig.bursts ?? START_MODE_DEFAULTS.burst.bursts;
698
+ const duration = normalizedConfig.duration ?? START_MODE_DEFAULTS.burst.durationMs;
699
+
700
+ for (let index = 0; index < bursts; index += 1) {
701
+ const delay = (duration / Math.max(bursts, 1)) * index;
702
+ lifecycle.scheduleTimeout(this, () => {
703
+ const x = randomRange(
704
+ this.width * START_MODE_DEFAULTS.burst.xMinRatio,
705
+ this.width * START_MODE_DEFAULTS.burst.xMaxRatio,
706
+ );
707
+ const y = randomRange(
708
+ this.height * START_MODE_DEFAULTS.burst.yMinRatio,
709
+ this.height * START_MODE_DEFAULTS.burst.yMaxRatio,
710
+ );
711
+ launchBurst(this, x, y, normalizedConfig);
712
+ }, lifecycle.resolveScaledDelay(this, delay));
713
+ }
714
+ const finishDelay = duration + START_MODE_DEFAULTS.burst.finishDelayMs;
715
+ lifecycle.scheduleTimeout(
716
+ this,
717
+ () => this.finishWhenDone(),
718
+ lifecycle.resolveScaledDelay(this, finishDelay),
719
+ );
720
+ }
721
+
722
+ invokeLifecycleHook(this.activeLifecycleHooks.onStart, this);
723
+ return true;
724
+ }
725
+
726
+ startPreset(name) {
727
+ return this.start(resolveFireworksEnginePreset(name));
728
+ }
729
+
730
+ stop(clearCanvas = true) {
731
+ const wasRunning = this.running;
732
+ this.running = false;
733
+ lifecycle.clearPendingStart(this);
734
+ lifecycle.clearScheduledTimeouts(this);
735
+
736
+ const cancelAnimationFrameFn = lifecycle.getCancelAnimationFrame(this);
737
+ if (this.rafId !== null && typeof cancelAnimationFrameFn === 'function') {
738
+ cancelAnimationFrameFn(this.rafId);
739
+ }
740
+ this.rafId = null;
741
+
742
+ this.particles.length = 0;
743
+ this.formPool.length = 0;
744
+ this.sparkPool.length = 0;
745
+ this.emberPool.length = 0;
746
+ this.rockets.length = 0;
747
+ this.burstQueue.length = 0;
748
+ this.flashes.length = 0;
749
+ this.twinkleClouds.length = 0;
750
+ this.spriteCache.clear();
751
+ this.renderCachesClearedForIdle = true;
752
+
753
+ if (this.ctx && clearCanvas) {
754
+ this.ctx.clearRect(0, 0, this.width, this.height);
755
+ }
756
+
757
+ if (this.canvas) {
758
+ this.canvas.style.display = 'none';
759
+ }
760
+
761
+ if (wasRunning) {
762
+ invokeLifecycleHook(this.activeLifecycleHooks.onStop, this);
763
+ }
764
+
765
+ this.activeLifecycleHooks = { ...this.defaultLifecycleHooks };
766
+ this.finishLifecycleNotified = false;
767
+ }
768
+
769
+ destroy() {
770
+ this.stop();
771
+ lifecycle.removeResizeObserver(this);
772
+ lifecycle.removeResizeListener(this);
773
+
774
+ if (this.canvas) {
775
+ const parentNode = this.canvas.parentNode;
776
+ if (parentNode) {
777
+ parentNode.removeChild(this.canvas);
778
+ } else {
779
+ this.canvas.remove?.();
780
+ }
781
+ this.canvas = null;
782
+ }
783
+
784
+ this.spriteCache.clear();
785
+ this.ctx = null;
786
+ this.width = 0;
787
+ this.height = 0;
788
+ this.dpr = 1;
789
+ }
790
+ }
791
+
792
+ export function createFireworksEngine(options) {
793
+ return new FireworksEngine(options);
794
+ }
795
+
796
+ export default FireworksEngine;