@basmilius/sparkle 1.0.0 → 2.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.
Files changed (148) hide show
  1. package/dist/index.d.mts +1192 -14
  2. package/dist/index.d.mts.map +1 -1
  3. package/dist/index.mjs +4552 -370
  4. package/dist/index.mjs.map +1 -1
  5. package/package.json +3 -2
  6. package/src/aurora/consts.ts +3 -0
  7. package/src/aurora/index.ts +4 -0
  8. package/src/aurora/layer.ts +152 -0
  9. package/src/aurora/simulation.ts +19 -0
  10. package/src/aurora/types.ts +13 -0
  11. package/src/balloons/consts.ts +3 -0
  12. package/src/balloons/index.ts +6 -0
  13. package/src/balloons/layer.ts +138 -0
  14. package/src/balloons/particle.ts +110 -0
  15. package/src/balloons/simulation.ts +19 -0
  16. package/src/balloons/types.ts +14 -0
  17. package/src/bubbles/consts.ts +3 -0
  18. package/src/bubbles/index.ts +4 -0
  19. package/src/bubbles/layer.ts +233 -0
  20. package/src/bubbles/simulation.ts +20 -0
  21. package/src/bubbles/types.ts +21 -0
  22. package/src/canvas.ts +20 -1
  23. package/src/color.ts +10 -0
  24. package/src/confetti/consts.ts +13 -13
  25. package/src/confetti/index.ts +6 -0
  26. package/src/confetti/layer.ts +152 -0
  27. package/src/confetti/particle.ts +105 -0
  28. package/src/confetti/shapes.ts +104 -0
  29. package/src/confetti/simulation.ts +9 -203
  30. package/src/confetti/types.ts +4 -1
  31. package/src/distance.ts +1 -1
  32. package/src/donuts/consts.ts +19 -0
  33. package/src/donuts/donut.ts +12 -0
  34. package/src/donuts/index.ts +3 -0
  35. package/src/donuts/layer.ts +270 -0
  36. package/src/donuts/simulation.ts +25 -0
  37. package/src/fireflies/consts.ts +3 -0
  38. package/src/fireflies/index.ts +6 -0
  39. package/src/fireflies/layer.ts +152 -0
  40. package/src/fireflies/particle.ts +124 -0
  41. package/src/fireflies/simulation.ts +18 -0
  42. package/src/fireflies/types.ts +17 -0
  43. package/src/firepit/consts.ts +3 -0
  44. package/src/firepit/index.ts +4 -0
  45. package/src/firepit/layer.ts +174 -0
  46. package/src/firepit/simulation.ts +17 -0
  47. package/src/firepit/types.ts +20 -0
  48. package/src/fireworks/explosion.ts +8 -8
  49. package/src/fireworks/firework.ts +9 -8
  50. package/src/fireworks/index.ts +6 -2
  51. package/src/fireworks/layer.ts +452 -0
  52. package/src/fireworks/simulation.ts +9 -484
  53. package/src/fireworks/spark.ts +7 -7
  54. package/src/glitter/consts.ts +13 -0
  55. package/src/glitter/index.ts +4 -0
  56. package/src/glitter/layer.ts +173 -0
  57. package/src/glitter/simulation.ts +19 -0
  58. package/src/glitter/types.ts +23 -0
  59. package/src/index.ts +28 -0
  60. package/src/lanterns/consts.ts +13 -0
  61. package/src/lanterns/index.ts +4 -0
  62. package/src/lanterns/layer.ts +166 -0
  63. package/src/lanterns/simulation.ts +17 -0
  64. package/src/lanterns/types.ts +14 -0
  65. package/src/layer.ts +24 -0
  66. package/src/layered.ts +185 -0
  67. package/src/leaves/consts.ts +16 -0
  68. package/src/leaves/index.ts +4 -0
  69. package/src/leaves/layer.ts +251 -0
  70. package/src/leaves/simulation.ts +18 -0
  71. package/src/leaves/types.ts +16 -0
  72. package/src/lightning/consts.ts +3 -0
  73. package/src/lightning/index.ts +6 -0
  74. package/src/lightning/layer.ts +41 -0
  75. package/src/lightning/simulation.ts +17 -0
  76. package/src/lightning/system.ts +196 -0
  77. package/src/lightning/types.ts +12 -0
  78. package/src/matrix/consts.ts +5 -0
  79. package/src/matrix/index.ts +4 -0
  80. package/src/matrix/layer.ts +146 -0
  81. package/src/matrix/simulation.ts +18 -0
  82. package/src/matrix/types.ts +8 -0
  83. package/src/orbits/consts.ts +13 -0
  84. package/src/orbits/index.ts +4 -0
  85. package/src/orbits/layer.ts +183 -0
  86. package/src/orbits/simulation.ts +19 -0
  87. package/src/orbits/types.ts +16 -0
  88. package/src/particles/consts.ts +3 -0
  89. package/src/particles/index.ts +4 -0
  90. package/src/particles/layer.ts +317 -0
  91. package/src/particles/simulation.ts +26 -0
  92. package/src/particles/types.ts +10 -0
  93. package/src/petals/consts.ts +13 -0
  94. package/src/petals/index.ts +4 -0
  95. package/src/petals/layer.ts +158 -0
  96. package/src/petals/simulation.ts +18 -0
  97. package/src/petals/types.ts +15 -0
  98. package/src/plasma/consts.ts +3 -0
  99. package/src/plasma/index.ts +4 -0
  100. package/src/plasma/layer.ts +92 -0
  101. package/src/plasma/simulation.ts +17 -0
  102. package/src/plasma/types.ts +5 -0
  103. package/src/rain/consts.ts +3 -0
  104. package/src/rain/index.ts +6 -0
  105. package/src/rain/layer.ts +172 -0
  106. package/src/rain/particle.ts +132 -0
  107. package/src/rain/simulation.ts +21 -0
  108. package/src/rain/types.ts +22 -0
  109. package/src/sandstorm/consts.ts +3 -0
  110. package/src/sandstorm/index.ts +4 -0
  111. package/src/sandstorm/layer.ts +135 -0
  112. package/src/sandstorm/simulation.ts +18 -0
  113. package/src/sandstorm/types.ts +10 -0
  114. package/src/shooting-stars/index.ts +3 -0
  115. package/src/shooting-stars/system.ts +149 -0
  116. package/src/shooting-stars/types.ts +10 -0
  117. package/src/simulation-canvas.ts +47 -0
  118. package/src/snow/consts.ts +2 -2
  119. package/src/snow/index.ts +1 -0
  120. package/src/snow/layer.ts +263 -0
  121. package/src/snow/simulation.ts +4 -288
  122. package/src/sparklers/consts.ts +3 -0
  123. package/src/sparklers/index.ts +6 -0
  124. package/src/sparklers/layer.ts +174 -0
  125. package/src/sparklers/particle.ts +89 -0
  126. package/src/sparklers/simulation.ts +30 -0
  127. package/src/sparklers/types.ts +13 -0
  128. package/src/stars/consts.ts +3 -0
  129. package/src/stars/index.ts +4 -0
  130. package/src/stars/layer.ts +133 -0
  131. package/src/stars/simulation.ts +22 -0
  132. package/src/stars/types.ts +12 -0
  133. package/src/streamers/consts.ts +14 -0
  134. package/src/streamers/index.ts +4 -0
  135. package/src/streamers/layer.ts +211 -0
  136. package/src/streamers/simulation.ts +16 -0
  137. package/src/streamers/types.ts +14 -0
  138. package/src/trail.ts +140 -0
  139. package/src/waves/consts.ts +3 -0
  140. package/src/waves/index.ts +4 -0
  141. package/src/waves/layer.ts +167 -0
  142. package/src/waves/simulation.ts +18 -0
  143. package/src/waves/types.ts +9 -0
  144. package/src/wormhole/consts.ts +3 -0
  145. package/src/wormhole/index.ts +4 -0
  146. package/src/wormhole/layer.ts +181 -0
  147. package/src/wormhole/simulation.ts +17 -0
  148. package/src/wormhole/types.ts +10 -0
@@ -0,0 +1,233 @@
1
+ import { SimulationLayer } from '../layer';
2
+ import { MULBERRY } from './consts';
3
+ import type { BubbleSimulationConfig } from './simulation';
4
+ import type { Bubble, PopParticle } from './types';
5
+
6
+ const DEFAULT_COLORS = ['#88ccff', '#aaddff', '#ccbbff'];
7
+
8
+ export class BubbleLayer extends SimulationLayer {
9
+ readonly #scale: number;
10
+ readonly #speed: number;
11
+ readonly #sizeRange: [number, number];
12
+ readonly #wobbleAmount: number;
13
+ readonly #popOnClick: boolean;
14
+ readonly #popRadius: number;
15
+ readonly #baseHues: number[];
16
+ readonly #onClickBound: (evt: MouseEvent) => void;
17
+ #maxCount: number;
18
+ #time: number = 0;
19
+ #bubbles: Bubble[] = [];
20
+ #popParticles: PopParticle[] = [];
21
+ #canvas: HTMLCanvasElement | null = null;
22
+
23
+ constructor(config: BubbleSimulationConfig = {}) {
24
+ super();
25
+
26
+ this.#scale = config.scale ?? 1;
27
+ this.#maxCount = config.count ?? 30;
28
+ this.#sizeRange = config.sizeRange ?? [10, 40];
29
+ this.#speed = config.speed ?? 1;
30
+ this.#wobbleAmount = config.wobbleAmount ?? 1;
31
+ this.#popOnClick = config.popOnClick ?? true;
32
+ this.#popRadius = config.popRadius ?? 50;
33
+
34
+ const colors = config.colors ?? DEFAULT_COLORS;
35
+ this.#baseHues = colors.map(c => this.#colorToHue(c));
36
+
37
+ if (innerWidth < 991) {
38
+ this.#maxCount = Math.floor(this.#maxCount / 2);
39
+ }
40
+
41
+ for (let i = 0; i < this.#maxCount; ++i) {
42
+ this.#bubbles.push(this.#createBubble(true));
43
+ }
44
+
45
+ this.#onClickBound = this.#onClick.bind(this);
46
+ }
47
+
48
+ onMount(canvas: HTMLCanvasElement): void {
49
+ this.#canvas = canvas;
50
+
51
+ if (this.#popOnClick) {
52
+ canvas.addEventListener('click', this.#onClickBound, {passive: true});
53
+ }
54
+ }
55
+
56
+ onUnmount(canvas: HTMLCanvasElement): void {
57
+ canvas.removeEventListener('click', this.#onClickBound);
58
+ this.#canvas = null;
59
+ }
60
+
61
+ tick(dt: number, width: number, height: number): void {
62
+ this.#time += 0.01 * dt;
63
+
64
+ for (let i = 0; i < this.#bubbles.length; i++) {
65
+ const bubble = this.#bubbles[i];
66
+
67
+ const wobble = Math.sin(this.#time * bubble.wobbleFreq + bubble.wobblePhase) * bubble.wobbleAmp * this.#wobbleAmount;
68
+ bubble.x += wobble * dt / (width * 3);
69
+ bubble.y -= (bubble.speed * this.#speed * dt) / (height * 0.8);
70
+
71
+ bubble.hue += 0.1 * dt;
72
+
73
+ if (bubble.y < -0.1) {
74
+ this.#bubbles[i] = this.#createBubble(false);
75
+ }
76
+ }
77
+
78
+ let alive = 0;
79
+
80
+ for (let i = 0; i < this.#popParticles.length; i++) {
81
+ const particle = this.#popParticles[i];
82
+
83
+ particle.x += (particle.vx * dt) / width;
84
+ particle.y += (particle.vy * dt) / height;
85
+ particle.alpha -= particle.decay * dt;
86
+
87
+ if (particle.alpha > 0) {
88
+ this.#popParticles[alive++] = particle;
89
+ }
90
+ }
91
+
92
+ this.#popParticles.length = alive;
93
+ }
94
+
95
+ draw(ctx: CanvasRenderingContext2D, width: number, height: number): void {
96
+
97
+ for (const bubble of this.#bubbles) {
98
+ const px = bubble.x * width;
99
+ const py = bubble.y * height;
100
+ const r = bubble.radius * this.#scale;
101
+
102
+ const gradient = ctx.createRadialGradient(
103
+ px - r * 0.3, py - r * 0.3, r * 0.1,
104
+ px, py, r
105
+ );
106
+
107
+ const hue = (bubble.hue + this.#time * 10) % 360;
108
+ gradient.addColorStop(0, `hsla(${hue}, 70%, 85%, ${bubble.opacity * 0.4})`);
109
+ gradient.addColorStop(0.7, `hsla(${hue}, 60%, 70%, ${bubble.opacity * 0.15})`);
110
+ gradient.addColorStop(1, `hsla(${hue}, 50%, 60%, ${bubble.opacity * 0.05})`);
111
+
112
+ ctx.beginPath();
113
+ ctx.arc(px, py, r, 0, Math.PI * 2);
114
+ ctx.fillStyle = gradient;
115
+ ctx.fill();
116
+
117
+ ctx.beginPath();
118
+ ctx.arc(px, py, r, 0, Math.PI * 2);
119
+ ctx.strokeStyle = `hsla(${hue}, 60%, 80%, ${bubble.opacity * 0.3})`;
120
+ ctx.lineWidth = 1;
121
+ ctx.stroke();
122
+
123
+ ctx.beginPath();
124
+ ctx.ellipse(px - r * 0.25, py - r * 0.3, r * 0.2, r * 0.12, -0.5, 0, Math.PI * 2);
125
+ ctx.fillStyle = `hsla(0, 0%, 100%, ${bubble.opacity * 0.5})`;
126
+ ctx.fill();
127
+ }
128
+
129
+ for (const particle of this.#popParticles) {
130
+ ctx.beginPath();
131
+ ctx.arc(particle.x * width, particle.y * height, particle.size * this.#scale, 0, Math.PI * 2);
132
+ ctx.fillStyle = `rgba(200, 220, 255, ${particle.alpha})`;
133
+ ctx.fill();
134
+ }
135
+ }
136
+
137
+ #onClick(evt: MouseEvent): void {
138
+ if (!this.#canvas) {
139
+ return;
140
+ }
141
+
142
+ const rect = this.#canvas.getBoundingClientRect();
143
+ const mx = (evt.clientX - rect.left) / rect.width;
144
+ const my = (evt.clientY - rect.top) / rect.height;
145
+ const popRadiusNorm = this.#popRadius / this.#canvas.width;
146
+
147
+ for (let i = this.#bubbles.length - 1; i >= 0; i--) {
148
+ const bubble = this.#bubbles[i];
149
+ const dx = bubble.x - mx;
150
+ const dy = bubble.y - my;
151
+ const dist = Math.sqrt(dx * dx + dy * dy);
152
+
153
+ if (dist < popRadiusNorm + (bubble.radius * this.#scale) / this.#canvas.width) {
154
+ this.#createPopBurst(bubble.x, bubble.y);
155
+ this.#bubbles[i] = this.#createBubble(false);
156
+ }
157
+ }
158
+ }
159
+
160
+ #createBubble(initialSpread: boolean): Bubble {
161
+ const hueIndex = Math.floor(MULBERRY.next() * this.#baseHues.length);
162
+
163
+ return {
164
+ x: MULBERRY.next(),
165
+ y: initialSpread ? MULBERRY.next() : 1.1 + MULBERRY.next() * 0.1,
166
+ radius: this.#sizeRange[0] + MULBERRY.next() * (this.#sizeRange[1] - this.#sizeRange[0]),
167
+ speed: 0.5 + MULBERRY.next() * 1.5,
168
+ hue: this.#baseHues[hueIndex],
169
+ wobblePhase: MULBERRY.next() * Math.PI * 2,
170
+ wobbleFreq: 1 + MULBERRY.next() * 2,
171
+ wobbleAmp: 0.5 + MULBERRY.next() * 1,
172
+ opacity: 0.6 + MULBERRY.next() * 0.4
173
+ };
174
+ }
175
+
176
+ #createPopBurst(x: number, y: number): void {
177
+ const count = 5 + Math.floor(MULBERRY.next() * 4);
178
+
179
+ for (let i = 0; i < count; i++) {
180
+ const angle = MULBERRY.next() * Math.PI * 2;
181
+ const speed = 1 + MULBERRY.next() * 3;
182
+
183
+ this.#popParticles.push({
184
+ x,
185
+ y,
186
+ vx: Math.cos(angle) * speed,
187
+ vy: Math.sin(angle) * speed,
188
+ alpha: 0.6 + MULBERRY.next() * 0.4,
189
+ size: 1 + MULBERRY.next() * 2,
190
+ decay: 0.03 + MULBERRY.next() * 0.03
191
+ });
192
+ }
193
+ }
194
+
195
+ #colorToHue(color: string): number {
196
+ const canvas = document.createElement('canvas');
197
+ canvas.width = 1;
198
+ canvas.height = 1;
199
+ const ctx = canvas.getContext('2d')!;
200
+ ctx.fillStyle = color;
201
+ ctx.fillRect(0, 0, 1, 1);
202
+ const data = ctx.getImageData(0, 0, 1, 1).data;
203
+
204
+ let r = data[0] / 255;
205
+ let g = data[1] / 255;
206
+ let b = data[2] / 255;
207
+ const max = Math.max(r, g, b);
208
+ const min = Math.min(r, g, b);
209
+ const delta = max - min;
210
+
211
+ if (delta === 0) {
212
+ return 0;
213
+ }
214
+
215
+ let hue: number;
216
+
217
+ if (max === r) {
218
+ hue = ((g - b) / delta) % 6;
219
+ } else if (max === g) {
220
+ hue = (b - r) / delta + 2;
221
+ } else {
222
+ hue = (r - g) / delta + 4;
223
+ }
224
+
225
+ hue = Math.round(hue * 60);
226
+
227
+ if (hue < 0) {
228
+ hue += 360;
229
+ }
230
+
231
+ return hue;
232
+ }
233
+ }
@@ -0,0 +1,20 @@
1
+ import { SimulationCanvas } from '../simulation-canvas';
2
+ import { BubbleLayer } from './layer';
3
+
4
+ export interface BubbleSimulationConfig {
5
+ readonly count?: number;
6
+ readonly sizeRange?: [number, number];
7
+ readonly speed?: number;
8
+ readonly popOnClick?: boolean;
9
+ readonly popRadius?: number;
10
+ readonly colors?: string[];
11
+ readonly wobbleAmount?: number;
12
+ readonly scale?: number;
13
+ readonly canvasOptions?: CanvasRenderingContext2DSettings;
14
+ }
15
+
16
+ export class BubbleSimulation extends SimulationCanvas {
17
+ constructor(canvas: HTMLCanvasElement, config: BubbleSimulationConfig = {}) {
18
+ super(canvas, new BubbleLayer(config), 60, config.canvasOptions ?? {colorSpace: 'display-p3'});
19
+ }
20
+ }
@@ -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 {
package/src/color.ts ADDED
@@ -0,0 +1,10 @@
1
+ export function parseColor(fillStyle: string): {r: number; g: number; b: number; a: number} {
2
+ const canvas = document.createElement('canvas');
3
+ canvas.width = 1;
4
+ canvas.height = 1;
5
+ const ctx = canvas.getContext('2d')!;
6
+ ctx.fillStyle = fillStyle;
7
+ ctx.fillRect(0, 0, 1, 1);
8
+ const data = ctx.getImageData(0, 0, 1, 1).data;
9
+ return {r: data[0], g: data[1], b: data[2], a: data[3] / 255};
10
+ }
@@ -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,8 @@
1
+ export { ConfettiLayer } from './layer';
2
+ export { ConfettiParticle } from './particle';
1
3
  export { ConfettiSimulation } from './simulation';
4
+ export { PALETTES } from './consts';
5
+ export { SHAPE_PATHS } from './shapes';
6
+ export type { ConfettiParticleConfig } from './particle';
2
7
  export type { ConfettiSimulationConfig } from './simulation';
8
+ export type { Palette, Shape as ConfettiShape } from './types';
@@ -0,0 +1,152 @@
1
+ import { hexToRGB } from '@basmilius/utils';
2
+ import { SimulationLayer } from '../layer';
3
+ import { DEFAULT_CONFIG, MULBERRY, PALETTES } from './consts';
4
+ import { SHAPE_PATHS } from './shapes';
5
+ import type { ConfettiSimulationConfig } from './simulation';
6
+ import type { Config, Particle, ParticleConfig } from './types';
7
+
8
+ const TWO_PI = Math.PI * 2;
9
+
10
+ export class ConfettiLayer extends SimulationLayer {
11
+ readonly #scale: number;
12
+ #particles: Particle[] = [];
13
+ #width: number = 0;
14
+ #height: number = 0;
15
+ #isFiring: boolean = false;
16
+
17
+ constructor(config: ConfettiSimulationConfig = {}) {
18
+ super();
19
+ this.#scale = config.scale ?? 1;
20
+ }
21
+
22
+ onResize(width: number, height: number): void {
23
+ this.#width = width;
24
+ this.#height = height;
25
+ }
26
+
27
+ fire(config: Partial<Config>): void {
28
+ const width = this.#width;
29
+ const height = this.#height;
30
+
31
+ const resolved = {...DEFAULT_CONFIG, ...config};
32
+ const colors = config.colors ?? PALETTES[resolved.palette];
33
+ const {angle, decay, gravity, shapes, spread, startVelocity, ticks, x, y} = resolved;
34
+ const numberOfParticles = Math.max(1, resolved.particles);
35
+
36
+ for (let i = 0; i < numberOfParticles; i++) {
37
+ const particle = this.#createParticle({
38
+ angle,
39
+ color: hexToRGB(colors[Math.floor(MULBERRY.next() * colors.length)]),
40
+ decay,
41
+ gravity: gravity * this.#scale,
42
+ shape: shapes[Math.floor(MULBERRY.next() * shapes.length)],
43
+ spread,
44
+ startVelocity: startVelocity * this.#scale,
45
+ ticks,
46
+ x: width * x,
47
+ y: height * y
48
+ });
49
+
50
+ this.#tickParticle(particle);
51
+ this.#particles.push(particle);
52
+ }
53
+
54
+ this.#isFiring = true;
55
+ }
56
+
57
+ get hasParticles(): boolean {
58
+ return this.#particles.length > 0;
59
+ }
60
+
61
+ tick(dt: number, _width: number, _height: number): void {
62
+ const particles = this.#particles;
63
+ let alive = 0;
64
+
65
+ for (let i = 0; i < particles.length; i++) {
66
+ const p = particles[i];
67
+
68
+ if (p.tick < p.totalTicks) {
69
+ this.#tickParticle(p, dt);
70
+
71
+ if (p.tick < p.totalTicks) {
72
+ particles[alive++] = p;
73
+ }
74
+ }
75
+ }
76
+
77
+ particles.length = alive;
78
+ }
79
+
80
+ draw(ctx: CanvasRenderingContext2D, width: number, height: number): void {
81
+
82
+ const particles = this.#particles;
83
+
84
+ for (let i = 0; i < particles.length; i++) {
85
+ const p = particles[i];
86
+ const flipCos = Math.cos(p.flipAngle);
87
+ const size = p.size;
88
+
89
+ ctx.setTransform(
90
+ p.rotCos * flipCos * size,
91
+ p.rotSin * flipCos * size,
92
+ -p.rotSin * size,
93
+ p.rotCos * size,
94
+ p.x,
95
+ p.y
96
+ );
97
+ ctx.globalAlpha = 1 - p.tick / p.totalTicks;
98
+ ctx.fillStyle = p.colorStr;
99
+ ctx.fill(SHAPE_PATHS[p.shape]);
100
+ }
101
+
102
+ ctx.resetTransform();
103
+ ctx.globalAlpha = 1;
104
+ }
105
+
106
+ #createParticle(config: ParticleConfig): Particle {
107
+ const launchAngle = -(config.angle * Math.PI / 180)
108
+ + (0.5 * config.spread * Math.PI / 180)
109
+ - (MULBERRY.next() * config.spread * Math.PI / 180);
110
+
111
+ const speed = config.startVelocity * (0.5 + MULBERRY.next());
112
+ const rotAngle = MULBERRY.next() * TWO_PI;
113
+
114
+ return {
115
+ colorStr: `rgb(${config.color[0]}, ${config.color[1]}, ${config.color[2]})`,
116
+ decay: config.decay - 0.05 + MULBERRY.next() * 0.1,
117
+ flipAngle: MULBERRY.next() * TWO_PI,
118
+ flipSpeed: 0.03 + MULBERRY.next() * 0.05,
119
+ gravity: config.gravity,
120
+ rotAngle,
121
+ rotCos: Math.cos(rotAngle),
122
+ rotSin: Math.sin(rotAngle),
123
+ rotSpeed: (MULBERRY.next() - 0.5) * 0.06,
124
+ shape: config.shape,
125
+ size: (5 + MULBERRY.next() * 5) * this.#scale,
126
+ swing: MULBERRY.next() * TWO_PI,
127
+ swingAmp: 0.5 + MULBERRY.next() * 1.5,
128
+ swingSpeed: 0.025 + MULBERRY.next() * 0.035,
129
+ tick: 0,
130
+ totalTicks: config.ticks,
131
+ vx: Math.cos(launchAngle) * speed,
132
+ vy: Math.sin(launchAngle) * speed,
133
+ x: config.x,
134
+ y: config.y
135
+ };
136
+ }
137
+
138
+ #tickParticle(particle: Particle, dt: number = 1): void {
139
+ const decayFactor = Math.pow(particle.decay, dt);
140
+ particle.vx *= decayFactor;
141
+ particle.vy *= decayFactor;
142
+ particle.vy += particle.gravity * 0.35 * dt;
143
+ particle.swing += particle.swingSpeed * dt;
144
+ particle.x += (particle.vx + particle.swingAmp * Math.cos(particle.swing)) * dt;
145
+ particle.y += particle.vy * dt;
146
+ particle.rotAngle += particle.rotSpeed * dt;
147
+ particle.rotCos = Math.cos(particle.rotAngle);
148
+ particle.rotSin = Math.sin(particle.rotAngle);
149
+ particle.flipAngle += particle.flipSpeed * dt;
150
+ particle.tick += dt;
151
+ }
152
+ }
@@ -0,0 +1,105 @@
1
+ import type { Point } from '../point';
2
+ import { SHAPE_PATHS } from './shapes';
3
+ import type { Shape } from './types';
4
+
5
+ export interface ConfettiParticleConfig {
6
+ readonly decay?: number;
7
+ readonly gravity?: number;
8
+ readonly scale?: number;
9
+ readonly spread?: number;
10
+ readonly startVelocity?: number;
11
+ readonly ticks?: number;
12
+ }
13
+
14
+ export class ConfettiParticle {
15
+ readonly #colorStr: string;
16
+ readonly #gravity: number;
17
+ readonly #shape: Shape;
18
+ readonly #size: number;
19
+ readonly #totalTicks: number;
20
+ #decay: number;
21
+ #flipAngle: number;
22
+ #flipSpeed: number;
23
+ #rotAngle: number;
24
+ #rotCos: number;
25
+ #rotSin: number;
26
+ #rotSpeed: number;
27
+ #swing: number;
28
+ #swingAmp: number;
29
+ #swingSpeed: number;
30
+ #tick: number = 0;
31
+ #vx: number;
32
+ #vy: number;
33
+ #x: number;
34
+ #y: number;
35
+
36
+ get isDead(): boolean {
37
+ return this.#tick >= this.#totalTicks;
38
+ }
39
+
40
+ get position(): Point {
41
+ return {x: this.#x, y: this.#y};
42
+ }
43
+
44
+ constructor(position: Point, direction: number, shape: Shape, color: string, config: ConfettiParticleConfig = {}) {
45
+ const scale = config.scale ?? 1;
46
+ const spread = config.spread ?? 45;
47
+ const startVelocity = (config.startVelocity ?? 45) * scale;
48
+ const launchAngle = -(direction * Math.PI / 180)
49
+ + (0.5 * spread * Math.PI / 180)
50
+ - (Math.random() * spread * Math.PI / 180);
51
+ const speed = startVelocity * (0.5 + Math.random());
52
+ const rotAngle = Math.random() * Math.PI * 2;
53
+
54
+ this.#colorStr = color;
55
+ this.#gravity = (config.gravity ?? 1) * scale;
56
+ this.#shape = shape;
57
+ this.#size = (5 + Math.random() * 5) * scale;
58
+ this.#totalTicks = config.ticks ?? 200;
59
+ this.#x = position.x;
60
+ this.#y = position.y;
61
+ this.#vx = Math.cos(launchAngle) * speed;
62
+ this.#vy = Math.sin(launchAngle) * speed;
63
+ this.#decay = (config.decay ?? 0.9) - 0.05 + Math.random() * 0.1;
64
+ this.#flipAngle = Math.random() * Math.PI * 2;
65
+ this.#flipSpeed = 0.03 + Math.random() * 0.05;
66
+ this.#rotAngle = rotAngle;
67
+ this.#rotCos = Math.cos(rotAngle);
68
+ this.#rotSin = Math.sin(rotAngle);
69
+ this.#rotSpeed = (Math.random() - 0.5) * 0.06;
70
+ this.#swing = Math.random() * Math.PI * 2;
71
+ this.#swingAmp = 0.5 + Math.random() * 1.5;
72
+ this.#swingSpeed = 0.025 + Math.random() * 0.035;
73
+ }
74
+
75
+ draw(ctx: CanvasRenderingContext2D): void {
76
+ ctx.save();
77
+ ctx.setTransform(
78
+ this.#rotCos * Math.cos(this.#flipAngle) * this.#size,
79
+ this.#rotSin * Math.cos(this.#flipAngle) * this.#size,
80
+ -this.#rotSin * this.#size,
81
+ this.#rotCos * this.#size,
82
+ this.#x,
83
+ this.#y
84
+ );
85
+ ctx.globalAlpha = Math.max(0, 1 - this.#tick / this.#totalTicks);
86
+ ctx.fillStyle = this.#colorStr;
87
+ ctx.fill(SHAPE_PATHS[this.#shape]);
88
+ ctx.restore();
89
+ }
90
+
91
+ tick(dt: number = 1): void {
92
+ const decayFactor = Math.pow(this.#decay, dt);
93
+ this.#vx *= decayFactor;
94
+ this.#vy *= decayFactor;
95
+ this.#vy += this.#gravity * 0.35 * dt;
96
+ this.#swing += this.#swingSpeed * dt;
97
+ this.#x += (this.#vx + this.#swingAmp * Math.cos(this.#swing)) * dt;
98
+ this.#y += this.#vy * dt;
99
+ this.#rotAngle += this.#rotSpeed * dt;
100
+ this.#rotCos = Math.cos(this.#rotAngle);
101
+ this.#rotSin = Math.sin(this.#rotAngle);
102
+ this.#flipAngle += this.#flipSpeed * dt;
103
+ this.#tick += dt;
104
+ }
105
+ }