@basmilius/sparkle 2.0.0 → 2.2.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.
Files changed (129) hide show
  1. package/dist/index.d.mts +1053 -28
  2. package/dist/index.d.mts.map +1 -1
  3. package/dist/index.mjs +4840 -400
  4. package/dist/index.mjs.map +1 -1
  5. package/package.json +7 -2
  6. package/src/aurora/consts.ts +3 -0
  7. package/src/aurora/index.ts +10 -0
  8. package/src/aurora/layer.ts +180 -0
  9. package/src/aurora/types.ts +13 -0
  10. package/src/balloons/consts.ts +3 -0
  11. package/src/balloons/index.ts +12 -0
  12. package/src/balloons/layer.ts +169 -0
  13. package/src/balloons/particle.ts +110 -0
  14. package/src/balloons/types.ts +14 -0
  15. package/src/bubbles/consts.ts +3 -0
  16. package/src/bubbles/index.ts +10 -0
  17. package/src/bubbles/layer.ts +246 -0
  18. package/src/bubbles/types.ts +21 -0
  19. package/src/canvas.ts +32 -1
  20. package/src/color.ts +19 -0
  21. package/src/confetti/consts.ts +13 -13
  22. package/src/confetti/index.ts +20 -2
  23. package/src/confetti/layer.ts +155 -0
  24. package/src/confetti/particle.ts +106 -0
  25. package/src/confetti/shapes.ts +104 -0
  26. package/src/confetti/types.ts +4 -1
  27. package/src/distance.ts +1 -1
  28. package/src/donuts/consts.ts +19 -0
  29. package/src/donuts/donut.ts +12 -0
  30. package/src/donuts/index.ts +9 -0
  31. package/src/donuts/layer.ts +301 -0
  32. package/src/effect.ts +107 -0
  33. package/src/fade.ts +87 -0
  34. package/src/fireflies/consts.ts +3 -0
  35. package/src/fireflies/index.ts +12 -0
  36. package/src/fireflies/layer.ts +169 -0
  37. package/src/fireflies/particle.ts +124 -0
  38. package/src/fireflies/types.ts +17 -0
  39. package/src/firepit/consts.ts +3 -0
  40. package/src/firepit/index.ts +10 -0
  41. package/src/firepit/layer.ts +193 -0
  42. package/src/firepit/types.ts +20 -0
  43. package/src/fireworks/create-explosion.ts +237 -0
  44. package/src/fireworks/explosion.ts +9 -9
  45. package/src/fireworks/firework.ts +9 -8
  46. package/src/fireworks/index.ts +19 -3
  47. package/src/fireworks/layer.ts +203 -0
  48. package/src/fireworks/spark.ts +9 -9
  49. package/src/fireworks/types.ts +2 -2
  50. package/src/glitter/consts.ts +13 -0
  51. package/src/glitter/index.ts +9 -0
  52. package/src/glitter/layer.ts +181 -0
  53. package/src/glitter/types.ts +33 -0
  54. package/src/index.ts +27 -0
  55. package/src/lanterns/consts.ts +13 -0
  56. package/src/lanterns/index.ts +9 -0
  57. package/src/lanterns/layer.ts +178 -0
  58. package/src/lanterns/types.ts +22 -0
  59. package/src/layer.ts +26 -0
  60. package/src/leaves/consts.ts +16 -0
  61. package/src/leaves/index.ts +9 -0
  62. package/src/leaves/layer.ts +258 -0
  63. package/src/leaves/types.ts +25 -0
  64. package/src/lightning/consts.ts +3 -0
  65. package/src/lightning/index.ts +11 -0
  66. package/src/lightning/layer.ts +41 -0
  67. package/src/lightning/system.ts +196 -0
  68. package/src/lightning/types.ts +20 -0
  69. package/src/matrix/consts.ts +5 -0
  70. package/src/matrix/index.ts +9 -0
  71. package/src/matrix/layer.ts +154 -0
  72. package/src/matrix/types.ts +17 -0
  73. package/src/orbits/consts.ts +13 -0
  74. package/src/orbits/index.ts +9 -0
  75. package/src/orbits/layer.ts +213 -0
  76. package/src/orbits/types.ts +27 -0
  77. package/src/particles/consts.ts +3 -0
  78. package/src/particles/index.ts +10 -0
  79. package/src/particles/layer.ts +360 -0
  80. package/src/particles/types.ts +10 -0
  81. package/src/petals/consts.ts +13 -0
  82. package/src/petals/index.ts +10 -0
  83. package/src/petals/layer.ts +174 -0
  84. package/src/petals/types.ts +15 -0
  85. package/src/plasma/consts.ts +3 -0
  86. package/src/plasma/index.ts +10 -0
  87. package/src/plasma/layer.ts +107 -0
  88. package/src/plasma/types.ts +5 -0
  89. package/src/rain/consts.ts +3 -0
  90. package/src/rain/index.ts +12 -0
  91. package/src/rain/layer.ts +194 -0
  92. package/src/rain/particle.ts +132 -0
  93. package/src/rain/types.ts +22 -0
  94. package/src/sandstorm/consts.ts +3 -0
  95. package/src/sandstorm/index.ts +10 -0
  96. package/src/sandstorm/layer.ts +152 -0
  97. package/src/sandstorm/types.ts +10 -0
  98. package/src/scene.ts +201 -0
  99. package/src/shooting-stars/index.ts +3 -0
  100. package/src/shooting-stars/system.ts +151 -0
  101. package/src/shooting-stars/types.ts +11 -0
  102. package/src/simulation-canvas.ts +83 -0
  103. package/src/snow/consts.ts +2 -2
  104. package/src/snow/index.ts +9 -2
  105. package/src/snow/{simulation.ts → layer.ts} +64 -89
  106. package/src/sparklers/consts.ts +3 -0
  107. package/src/sparklers/index.ts +16 -0
  108. package/src/sparklers/layer.ts +220 -0
  109. package/src/sparklers/particle.ts +89 -0
  110. package/src/sparklers/types.ts +13 -0
  111. package/src/stars/consts.ts +3 -0
  112. package/src/stars/index.ts +10 -0
  113. package/src/stars/layer.ts +139 -0
  114. package/src/stars/types.ts +12 -0
  115. package/src/streamers/consts.ts +14 -0
  116. package/src/streamers/index.ts +10 -0
  117. package/src/streamers/layer.ts +223 -0
  118. package/src/streamers/types.ts +14 -0
  119. package/src/trail.ts +140 -0
  120. package/src/waves/consts.ts +3 -0
  121. package/src/waves/index.ts +10 -0
  122. package/src/waves/layer.ts +164 -0
  123. package/src/waves/types.ts +10 -0
  124. package/src/wormhole/consts.ts +3 -0
  125. package/src/wormhole/index.ts +10 -0
  126. package/src/wormhole/layer.ts +197 -0
  127. package/src/wormhole/types.ts +10 -0
  128. package/src/confetti/simulation.ts +0 -221
  129. package/src/fireworks/simulation.ts +0 -493
@@ -0,0 +1,246 @@
1
+ import { parseColor } from '../color';
2
+ import { Effect } from '../effect';
3
+ import { MULBERRY } from './consts';
4
+ import type { Bubble, PopParticle } from './types';
5
+
6
+ const DEFAULT_COLORS = ['#88ccff', '#aaddff', '#ccbbff'];
7
+
8
+ export interface BubblesConfig {
9
+ readonly count?: number;
10
+ readonly sizeRange?: [number, number];
11
+ readonly speed?: number;
12
+ readonly popOnClick?: boolean;
13
+ readonly popRadius?: number;
14
+ readonly colors?: string[];
15
+ readonly wobbleAmount?: number;
16
+ readonly scale?: number;
17
+ }
18
+
19
+ export class Bubbles extends Effect<BubblesConfig> {
20
+ readonly #scale: number;
21
+ #speed: number;
22
+ readonly #sizeRange: [number, number];
23
+ #wobbleAmount: number;
24
+ readonly #popOnClick: boolean;
25
+ readonly #popRadius: number;
26
+ readonly #baseHues: number[];
27
+ readonly #onClickBound: (evt: MouseEvent) => void;
28
+ #maxCount: number;
29
+ #time: number = 0;
30
+ #bubbles: Bubble[] = [];
31
+ #popParticles: PopParticle[] = [];
32
+ #canvas: HTMLCanvasElement | null = null;
33
+
34
+ constructor(config: BubblesConfig = {}) {
35
+ super();
36
+
37
+ this.#scale = config.scale ?? 1;
38
+ this.#maxCount = config.count ?? 30;
39
+ this.#sizeRange = config.sizeRange ?? [10, 40];
40
+ this.#speed = config.speed ?? 1;
41
+ this.#wobbleAmount = config.wobbleAmount ?? 1;
42
+ this.#popOnClick = config.popOnClick ?? true;
43
+ this.#popRadius = config.popRadius ?? 50;
44
+
45
+ const colors = config.colors ?? DEFAULT_COLORS;
46
+ this.#baseHues = colors.map(c => this.#colorToHue(c));
47
+
48
+ if (innerWidth < 991) {
49
+ this.#maxCount = Math.floor(this.#maxCount / 2);
50
+ }
51
+
52
+ for (let i = 0; i < this.#maxCount; ++i) {
53
+ this.#bubbles.push(this.#createBubble(true));
54
+ }
55
+
56
+ this.#onClickBound = this.#onClick.bind(this);
57
+ }
58
+
59
+ onMount(canvas: HTMLCanvasElement): void {
60
+ this.#canvas = canvas;
61
+
62
+ if (this.#popOnClick) {
63
+ canvas.addEventListener('click', this.#onClickBound, {passive: true});
64
+ }
65
+ }
66
+
67
+ onUnmount(canvas: HTMLCanvasElement): void {
68
+ canvas.removeEventListener('click', this.#onClickBound);
69
+ this.#canvas = null;
70
+ }
71
+
72
+ configure(config: Partial<BubblesConfig>): void {
73
+ if (config.speed !== undefined) {
74
+ this.#speed = config.speed;
75
+ }
76
+ if (config.wobbleAmount !== undefined) {
77
+ this.#wobbleAmount = config.wobbleAmount;
78
+ }
79
+ }
80
+
81
+ tick(dt: number, width: number, height: number): void {
82
+ this.#time += 0.01 * dt;
83
+
84
+ for (let i = 0; i < this.#bubbles.length; i++) {
85
+ const bubble = this.#bubbles[i];
86
+
87
+ const wobble = Math.sin(this.#time * bubble.wobbleFreq + bubble.wobblePhase) * bubble.wobbleAmp * this.#wobbleAmount;
88
+ bubble.x += wobble * dt / (width * 3);
89
+ bubble.y -= (bubble.speed * this.#speed * dt) / (height * 0.8);
90
+
91
+ bubble.hue += 0.1 * dt;
92
+
93
+ if (bubble.y < -0.1) {
94
+ this.#bubbles[i] = this.#createBubble(false);
95
+ }
96
+ }
97
+
98
+ let alive = 0;
99
+
100
+ for (let i = 0; i < this.#popParticles.length; i++) {
101
+ const particle = this.#popParticles[i];
102
+
103
+ particle.x += (particle.vx * dt) / width;
104
+ particle.y += (particle.vy * dt) / height;
105
+ particle.alpha -= particle.decay * dt;
106
+
107
+ if (particle.alpha > 0) {
108
+ this.#popParticles[alive++] = particle;
109
+ }
110
+ }
111
+
112
+ this.#popParticles.length = alive;
113
+ }
114
+
115
+ draw(ctx: CanvasRenderingContext2D, width: number, height: number): void {
116
+
117
+ for (const bubble of this.#bubbles) {
118
+ const px = bubble.x * width;
119
+ const py = bubble.y * height;
120
+ const r = bubble.radius * this.#scale;
121
+
122
+ const gradient = ctx.createRadialGradient(
123
+ px - r * 0.3, py - r * 0.3, r * 0.1,
124
+ px, py, r
125
+ );
126
+
127
+ const hue = (bubble.hue + this.#time * 10) % 360;
128
+ gradient.addColorStop(0, `hsla(${hue}, 70%, 85%, ${bubble.opacity * 0.4})`);
129
+ gradient.addColorStop(0.7, `hsla(${hue}, 60%, 70%, ${bubble.opacity * 0.15})`);
130
+ gradient.addColorStop(1, `hsla(${hue}, 50%, 60%, ${bubble.opacity * 0.05})`);
131
+
132
+ ctx.beginPath();
133
+ ctx.arc(px, py, r, 0, Math.PI * 2);
134
+ ctx.fillStyle = gradient;
135
+ ctx.fill();
136
+
137
+ ctx.beginPath();
138
+ ctx.arc(px, py, r, 0, Math.PI * 2);
139
+ ctx.strokeStyle = `hsla(${hue}, 60%, 80%, ${bubble.opacity * 0.3})`;
140
+ ctx.lineWidth = 1;
141
+ ctx.stroke();
142
+
143
+ ctx.beginPath();
144
+ ctx.ellipse(px - r * 0.25, py - r * 0.3, r * 0.2, r * 0.12, -0.5, 0, Math.PI * 2);
145
+ ctx.fillStyle = `hsla(0, 0%, 100%, ${bubble.opacity * 0.5})`;
146
+ ctx.fill();
147
+ }
148
+
149
+ for (const particle of this.#popParticles) {
150
+ ctx.beginPath();
151
+ ctx.arc(particle.x * width, particle.y * height, particle.size * this.#scale, 0, Math.PI * 2);
152
+ ctx.fillStyle = `rgba(200, 220, 255, ${particle.alpha})`;
153
+ ctx.fill();
154
+ }
155
+ }
156
+
157
+ #onClick(evt: MouseEvent): void {
158
+ if (!this.#canvas) {
159
+ return;
160
+ }
161
+
162
+ const rect = this.#canvas.getBoundingClientRect();
163
+ const mx = (evt.clientX - rect.left) / rect.width;
164
+ const my = (evt.clientY - rect.top) / rect.height;
165
+ const popRadiusNorm = this.#popRadius / this.#canvas.width;
166
+
167
+ for (let i = this.#bubbles.length - 1; i >= 0; i--) {
168
+ const bubble = this.#bubbles[i];
169
+ const dx = bubble.x - mx;
170
+ const dy = bubble.y - my;
171
+ const dist = Math.sqrt(dx * dx + dy * dy);
172
+
173
+ if (dist < popRadiusNorm + (bubble.radius * this.#scale) / this.#canvas.width) {
174
+ this.#createPopBurst(bubble.x, bubble.y);
175
+ this.#bubbles[i] = this.#createBubble(false);
176
+ }
177
+ }
178
+ }
179
+
180
+ #createBubble(initialSpread: boolean): Bubble {
181
+ const hueIndex = Math.floor(MULBERRY.next() * this.#baseHues.length);
182
+
183
+ return {
184
+ x: MULBERRY.next(),
185
+ y: initialSpread ? MULBERRY.next() : 1.1 + MULBERRY.next() * 0.1,
186
+ radius: this.#sizeRange[0] + MULBERRY.next() * (this.#sizeRange[1] - this.#sizeRange[0]),
187
+ speed: 0.5 + MULBERRY.next() * 1.5,
188
+ hue: this.#baseHues[hueIndex],
189
+ wobblePhase: MULBERRY.next() * Math.PI * 2,
190
+ wobbleFreq: 1 + MULBERRY.next() * 2,
191
+ wobbleAmp: 0.5 + MULBERRY.next() * 1,
192
+ opacity: 0.6 + MULBERRY.next() * 0.4
193
+ };
194
+ }
195
+
196
+ #createPopBurst(x: number, y: number): void {
197
+ const count = 5 + Math.floor(MULBERRY.next() * 4);
198
+
199
+ for (let i = 0; i < count; i++) {
200
+ const angle = MULBERRY.next() * Math.PI * 2;
201
+ const speed = 1 + MULBERRY.next() * 3;
202
+
203
+ this.#popParticles.push({
204
+ x,
205
+ y,
206
+ vx: Math.cos(angle) * speed,
207
+ vy: Math.sin(angle) * speed,
208
+ alpha: 0.6 + MULBERRY.next() * 0.4,
209
+ size: 1 + MULBERRY.next() * 2,
210
+ decay: 0.03 + MULBERRY.next() * 0.03
211
+ });
212
+ }
213
+ }
214
+
215
+ #colorToHue(color: string): number {
216
+ const {r: r255, g: g255, b: b255} = parseColor(color);
217
+ let r = r255 / 255;
218
+ let g = g255 / 255;
219
+ let b = b255 / 255;
220
+ const max = Math.max(r, g, b);
221
+ const min = Math.min(r, g, b);
222
+ const delta = max - min;
223
+
224
+ if (delta === 0) {
225
+ return 0;
226
+ }
227
+
228
+ let hue: number;
229
+
230
+ if (max === r) {
231
+ hue = ((g - b) / delta) % 6;
232
+ } else if (max === g) {
233
+ hue = (b - r) / delta + 2;
234
+ } else {
235
+ hue = (r - g) / delta + 4;
236
+ }
237
+
238
+ hue = Math.round(hue * 60);
239
+
240
+ if (hue < 0) {
241
+ hue += 360;
242
+ }
243
+
244
+ return hue;
245
+ }
246
+ }
@@ -0,0 +1,21 @@
1
+ export type Bubble = {
2
+ x: number;
3
+ y: number;
4
+ radius: number;
5
+ speed: number;
6
+ hue: number;
7
+ wobblePhase: number;
8
+ wobbleFreq: number;
9
+ wobbleAmp: number;
10
+ opacity: number;
11
+ };
12
+
13
+ export type PopParticle = {
14
+ x: number;
15
+ y: number;
16
+ vx: number;
17
+ vy: number;
18
+ alpha: number;
19
+ size: number;
20
+ decay: number;
21
+ };
package/src/canvas.ts CHANGED
@@ -1,4 +1,14 @@
1
1
  export class LimitedFrameRateCanvas {
2
+ static #globalSpeed: number = 1;
3
+
4
+ static get globalSpeed(): number {
5
+ return LimitedFrameRateCanvas.#globalSpeed;
6
+ }
7
+
8
+ static set globalSpeed(value: number) {
9
+ LimitedFrameRateCanvas.#globalSpeed = value;
10
+ }
11
+
2
12
  readonly #canvas: HTMLCanvasElement;
3
13
  readonly #context: CanvasRenderingContext2D;
4
14
  readonly #frameRate: number;
@@ -7,6 +17,7 @@ export class LimitedFrameRateCanvas {
7
17
  #delta: number = 0;
8
18
  #frame: number = 0;
9
19
  #now: number = 0;
20
+ #speed: number = 1;
10
21
  #then: number = 0;
11
22
  #ticks: number = 0;
12
23
  #isStopped: boolean = true;
@@ -26,7 +37,15 @@ export class LimitedFrameRateCanvas {
26
37
  }
27
38
 
28
39
  get deltaFactor(): number {
29
- return this.#then === 0 ? 1 : this.#target / this.#delta;
40
+ return this.#then === 0 ? 1 : (this.#target / this.#delta) * this.#speed * LimitedFrameRateCanvas.#globalSpeed;
41
+ }
42
+
43
+ get speed(): number {
44
+ return this.#speed;
45
+ }
46
+
47
+ set speed(value: number) {
48
+ this.#speed = value;
30
49
  }
31
50
 
32
51
  get frameRate(): number {
@@ -101,6 +120,18 @@ export class LimitedFrameRateCanvas {
101
120
  cancelAnimationFrame(this.#frame);
102
121
  }
103
122
 
123
+ pause(): void {
124
+ this.#isStopped = true;
125
+ cancelAnimationFrame(this.#frame);
126
+ }
127
+
128
+ resume(): void {
129
+ if (this.#isStopped) {
130
+ this.#isStopped = false;
131
+ this.#frame = requestAnimationFrame(this.loop.bind(this));
132
+ }
133
+ }
134
+
104
135
  draw(): void {
105
136
  throw new Error('LimitedFrameRateCanvas::draw() should be overwritten.');
106
137
  }
package/src/color.ts ADDED
@@ -0,0 +1,19 @@
1
+ const cache = new Map<string, { r: number; g: number; b: number; a: number }>();
2
+
3
+ export function parseColor(fillStyle: string): { r: number; g: number; b: number; a: number } {
4
+ const cached = cache.get(fillStyle);
5
+ if (cached) {
6
+ return cached;
7
+ }
8
+
9
+ const canvas = document.createElement('canvas');
10
+ canvas.width = 1;
11
+ canvas.height = 1;
12
+ const ctx = canvas.getContext('2d')!;
13
+ ctx.fillStyle = fillStyle;
14
+ ctx.fillRect(0, 0, 1, 1);
15
+ const data = ctx.getImageData(0, 0, 1, 1).data;
16
+ const result = {r: data[0], g: data[1], b: data[2], a: data[3] / 255};
17
+ cache.set(fillStyle, result);
18
+ return result;
19
+ }
@@ -1,21 +1,21 @@
1
- import { mulberry32 } from '@basmilius/utils';
2
- import type { Config } from './types';
1
+ import { type Mulberry32, mulberry32 } from '@basmilius/utils';
2
+ import type { Config, Palette } from './types';
3
+
4
+ export const PALETTES: Record<Palette, string[]> = {
5
+ classic: ['#26ccff', '#a25afd', '#ff5e7e', '#88ff5a', '#fcff42', '#ffa62d', '#ff36ff'],
6
+ pastel: ['#a8d8ea', '#c3aed6', '#f9b4c4', '#ffd6a5', '#caffbf', '#fdffb6'],
7
+ vibrant: ['#6366f1', '#8b5cf6', '#ec4899', '#f97316', '#eab308', '#22c55e', '#06b6d4'],
8
+ warm: ['#ff6b6b', '#feca57', '#48dbfb', '#ff9ff3', '#54a0ff', '#5f27cd', '#01a3a4']
9
+ };
3
10
 
4
11
  export const DEFAULT_CONFIG: Config = {
5
12
  angle: 90,
6
- colors: [
7
- '#26ccff',
8
- '#a25afd',
9
- '#ff5e7e',
10
- '#88ff5a',
11
- '#fcff42',
12
- '#ffa62d',
13
- '#ff36ff'
14
- ],
13
+ colors: PALETTES.vibrant,
15
14
  decay: 0.9,
16
15
  gravity: 1,
16
+ palette: 'vibrant',
17
17
  particles: 50,
18
- shapes: ['circle', 'diamond', 'ribbon', 'square', 'star', 'triangle'],
18
+ shapes: ['bowtie', 'circle', 'crescent', 'diamond', 'heart', 'hexagon', 'ribbon', 'ring', 'square', 'star', 'triangle'],
19
19
  spread: 45,
20
20
  ticks: 200,
21
21
  startVelocity: 45,
@@ -23,4 +23,4 @@ export const DEFAULT_CONFIG: Config = {
23
23
  y: 0.5
24
24
  };
25
25
 
26
- export const MULBERRY = mulberry32(13);
26
+ export const MULBERRY: Mulberry32 = mulberry32(13);
@@ -1,2 +1,20 @@
1
- export { ConfettiSimulation } from './simulation';
2
- export type { ConfettiSimulationConfig } from './simulation';
1
+ import { Confetti } from './layer';
2
+ import type { ConfettiConfig } from './layer';
3
+ import type { Config as ConfettiBurstConfig } from './types';
4
+ import type { Effect } from '../effect';
5
+
6
+ export interface ConfettiInstance extends Effect<ConfettiConfig> {
7
+ burst(config: Partial<ConfettiBurstConfig>): void;
8
+ }
9
+
10
+ export function createConfetti(config?: ConfettiConfig): ConfettiInstance {
11
+ return new Confetti(config) as ConfettiInstance;
12
+ }
13
+
14
+ export { ConfettiParticle } from './particle';
15
+ export { PALETTES } from './consts';
16
+ export { SHAPE_PATHS } from './shapes';
17
+ export type { ConfettiConfig };
18
+ export type { ConfettiBurstConfig };
19
+ export type { ConfettiParticleConfig } from './particle';
20
+ export type { Palette, Shape as ConfettiShape } from './types';
@@ -0,0 +1,155 @@
1
+ import { hexToRGB } from '@basmilius/utils';
2
+ import { Effect } from '../effect';
3
+ import { DEFAULT_CONFIG, MULBERRY, PALETTES } from './consts';
4
+ import { SHAPE_PATHS } from './shapes';
5
+ import type { Config, Particle, ParticleConfig } from './types';
6
+
7
+ const TWO_PI = Math.PI * 2;
8
+
9
+ export interface ConfettiConfig {
10
+ readonly scale?: number;
11
+ }
12
+
13
+ export class Confetti extends Effect<ConfettiConfig> {
14
+ readonly #scale: number;
15
+ #particles: Particle[] = [];
16
+ #width: number = 0;
17
+ #height: number = 0;
18
+ #isFiring: boolean = false;
19
+
20
+ constructor(config: ConfettiConfig = {}) {
21
+ super();
22
+ this.#scale = config.scale ?? 1;
23
+ }
24
+
25
+ onResize(width: number, height: number): void {
26
+ this.#width = width;
27
+ this.#height = height;
28
+ }
29
+
30
+ burst(config: Partial<Config>): void {
31
+ const width = this.#width;
32
+ const height = this.#height;
33
+
34
+ const resolved = {...DEFAULT_CONFIG, ...config};
35
+ const colors = config.colors ?? PALETTES[resolved.palette];
36
+ const {angle, decay, gravity, shapes, spread, startVelocity, ticks, x, y} = resolved;
37
+ const numberOfParticles = Math.max(1, resolved.particles);
38
+
39
+ for (let i = 0; i < numberOfParticles; i++) {
40
+ const particle = this.#createParticle({
41
+ angle,
42
+ color: hexToRGB(colors[Math.floor(MULBERRY.next() * colors.length)]),
43
+ decay,
44
+ gravity: gravity * this.#scale,
45
+ shape: shapes[Math.floor(MULBERRY.next() * shapes.length)],
46
+ spread,
47
+ startVelocity: startVelocity * this.#scale,
48
+ ticks,
49
+ x: width * x,
50
+ y: height * y
51
+ });
52
+
53
+ this.#tickParticle(particle);
54
+ this.#particles.push(particle);
55
+ }
56
+
57
+ this.#isFiring = true;
58
+ }
59
+
60
+ get hasParticles(): boolean {
61
+ return this.#particles.length > 0;
62
+ }
63
+
64
+ tick(dt: number, _width: number, _height: number): void {
65
+ const particles = this.#particles;
66
+ let alive = 0;
67
+
68
+ for (let i = 0; i < particles.length; i++) {
69
+ const p = particles[i];
70
+
71
+ if (p.tick < p.totalTicks) {
72
+ this.#tickParticle(p, dt);
73
+
74
+ if (p.tick < p.totalTicks) {
75
+ particles[alive++] = p;
76
+ }
77
+ }
78
+ }
79
+
80
+ particles.length = alive;
81
+ }
82
+
83
+ draw(ctx: CanvasRenderingContext2D, width: number, height: number): void {
84
+
85
+ const particles = this.#particles;
86
+
87
+ for (let i = 0; i < particles.length; i++) {
88
+ const p = particles[i];
89
+ const flipCos = Math.cos(p.flipAngle);
90
+ const size = p.size;
91
+
92
+ ctx.setTransform(
93
+ p.rotCos * flipCos * size,
94
+ p.rotSin * flipCos * size,
95
+ -p.rotSin * size,
96
+ p.rotCos * size,
97
+ p.x,
98
+ p.y
99
+ );
100
+ ctx.globalAlpha = 1 - p.tick / p.totalTicks;
101
+ ctx.fillStyle = p.colorStr;
102
+ ctx.fill(SHAPE_PATHS[p.shape]);
103
+ }
104
+
105
+ ctx.resetTransform();
106
+ ctx.globalAlpha = 1;
107
+ }
108
+
109
+ #createParticle(config: ParticleConfig): Particle {
110
+ const launchAngle = -(config.angle * Math.PI / 180)
111
+ + (0.5 * config.spread * Math.PI / 180)
112
+ - (MULBERRY.next() * config.spread * Math.PI / 180);
113
+
114
+ const speed = config.startVelocity * (0.5 + MULBERRY.next());
115
+ const rotAngle = MULBERRY.next() * TWO_PI;
116
+
117
+ return {
118
+ colorStr: `rgb(${config.color[0]}, ${config.color[1]}, ${config.color[2]})`,
119
+ decay: config.decay - 0.05 + MULBERRY.next() * 0.1,
120
+ flipAngle: MULBERRY.next() * TWO_PI,
121
+ flipSpeed: 0.03 + MULBERRY.next() * 0.05,
122
+ gravity: config.gravity,
123
+ rotAngle,
124
+ rotCos: Math.cos(rotAngle),
125
+ rotSin: Math.sin(rotAngle),
126
+ rotSpeed: (MULBERRY.next() - 0.5) * 0.06,
127
+ shape: config.shape,
128
+ size: (5 + MULBERRY.next() * 5) * this.#scale,
129
+ swing: MULBERRY.next() * TWO_PI,
130
+ swingAmp: 0.5 + MULBERRY.next() * 1.5,
131
+ swingSpeed: 0.025 + MULBERRY.next() * 0.035,
132
+ tick: 0,
133
+ totalTicks: config.ticks,
134
+ vx: Math.cos(launchAngle) * speed,
135
+ vy: Math.sin(launchAngle) * speed,
136
+ x: config.x,
137
+ y: config.y
138
+ };
139
+ }
140
+
141
+ #tickParticle(particle: Particle, dt: number = 1): void {
142
+ const decayFactor = Math.pow(particle.decay, dt);
143
+ particle.vx *= decayFactor;
144
+ particle.vy *= decayFactor;
145
+ particle.vy += particle.gravity * 0.35 * dt;
146
+ particle.swing += particle.swingSpeed * dt;
147
+ particle.x += (particle.vx + particle.swingAmp * Math.cos(particle.swing)) * dt;
148
+ particle.y += particle.vy * dt;
149
+ particle.rotAngle += particle.rotSpeed * dt;
150
+ particle.rotCos = Math.cos(particle.rotAngle);
151
+ particle.rotSin = Math.sin(particle.rotAngle);
152
+ particle.flipAngle += particle.flipSpeed * dt;
153
+ particle.tick += dt;
154
+ }
155
+ }
@@ -0,0 +1,106 @@
1
+ import type { Point } from '../point';
2
+ import { MULBERRY } from './consts';
3
+ import { SHAPE_PATHS } from './shapes';
4
+ import type { Shape } from './types';
5
+
6
+ export interface ConfettiParticleConfig {
7
+ readonly decay?: number;
8
+ readonly gravity?: number;
9
+ readonly scale?: number;
10
+ readonly spread?: number;
11
+ readonly startVelocity?: number;
12
+ readonly ticks?: number;
13
+ }
14
+
15
+ export class ConfettiParticle {
16
+ readonly #colorStr: string;
17
+ readonly #gravity: number;
18
+ readonly #shape: Shape;
19
+ readonly #size: number;
20
+ readonly #totalTicks: number;
21
+ #decay: number;
22
+ #flipAngle: number;
23
+ #flipSpeed: number;
24
+ #rotAngle: number;
25
+ #rotCos: number;
26
+ #rotSin: number;
27
+ #rotSpeed: number;
28
+ #swing: number;
29
+ #swingAmp: number;
30
+ #swingSpeed: number;
31
+ #tick: number = 0;
32
+ #vx: number;
33
+ #vy: number;
34
+ #x: number;
35
+ #y: number;
36
+
37
+ get isDead(): boolean {
38
+ return this.#tick >= this.#totalTicks;
39
+ }
40
+
41
+ get position(): Point {
42
+ return {x: this.#x, y: this.#y};
43
+ }
44
+
45
+ constructor(position: Point, direction: number, shape: Shape, color: string, config: ConfettiParticleConfig = {}) {
46
+ const scale = config.scale ?? 1;
47
+ const spread = config.spread ?? 45;
48
+ const startVelocity = (config.startVelocity ?? 45) * scale;
49
+ const launchAngle = -(direction * Math.PI / 180)
50
+ + (0.5 * spread * Math.PI / 180)
51
+ - (MULBERRY.next() * spread * Math.PI / 180);
52
+ const speed = startVelocity * (0.5 + MULBERRY.next());
53
+ const rotAngle = MULBERRY.next() * Math.PI * 2;
54
+
55
+ this.#colorStr = color;
56
+ this.#gravity = (config.gravity ?? 1) * scale;
57
+ this.#shape = shape;
58
+ this.#size = (5 + MULBERRY.next() * 5) * scale;
59
+ this.#totalTicks = config.ticks ?? 200;
60
+ this.#x = position.x;
61
+ this.#y = position.y;
62
+ this.#vx = Math.cos(launchAngle) * speed;
63
+ this.#vy = Math.sin(launchAngle) * speed;
64
+ this.#decay = (config.decay ?? 0.9) - 0.05 + MULBERRY.next() * 0.1;
65
+ this.#flipAngle = MULBERRY.next() * Math.PI * 2;
66
+ this.#flipSpeed = 0.03 + MULBERRY.next() * 0.05;
67
+ this.#rotAngle = rotAngle;
68
+ this.#rotCos = Math.cos(rotAngle);
69
+ this.#rotSin = Math.sin(rotAngle);
70
+ this.#rotSpeed = (MULBERRY.next() - 0.5) * 0.06;
71
+ this.#swing = MULBERRY.next() * Math.PI * 2;
72
+ this.#swingAmp = 0.5 + MULBERRY.next() * 1.5;
73
+ this.#swingSpeed = 0.025 + MULBERRY.next() * 0.035;
74
+ }
75
+
76
+ draw(ctx: CanvasRenderingContext2D): void {
77
+ ctx.save();
78
+ ctx.setTransform(
79
+ this.#rotCos * Math.cos(this.#flipAngle) * this.#size,
80
+ this.#rotSin * Math.cos(this.#flipAngle) * this.#size,
81
+ -this.#rotSin * this.#size,
82
+ this.#rotCos * this.#size,
83
+ this.#x,
84
+ this.#y
85
+ );
86
+ ctx.globalAlpha = Math.max(0, 1 - this.#tick / this.#totalTicks);
87
+ ctx.fillStyle = this.#colorStr;
88
+ ctx.fill(SHAPE_PATHS[this.#shape]);
89
+ ctx.restore();
90
+ }
91
+
92
+ tick(dt: number = 1): void {
93
+ const decayFactor = Math.pow(this.#decay, dt);
94
+ this.#vx *= decayFactor;
95
+ this.#vy *= decayFactor;
96
+ this.#vy += this.#gravity * 0.35 * dt;
97
+ this.#swing += this.#swingSpeed * dt;
98
+ this.#x += (this.#vx + this.#swingAmp * Math.cos(this.#swing)) * dt;
99
+ this.#y += this.#vy * dt;
100
+ this.#rotAngle += this.#rotSpeed * dt;
101
+ this.#rotCos = Math.cos(this.#rotAngle);
102
+ this.#rotSin = Math.sin(this.#rotAngle);
103
+ this.#flipAngle += this.#flipSpeed * dt;
104
+ this.#tick += dt;
105
+ }
106
+ }