@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,258 @@
1
+ import { Effect } from '../effect';
2
+ import { LEAF_COLORS, MULBERRY } from './consts';
3
+ import type { Leaf, LeavesConfig } from './types';
4
+
5
+ export class Leaves extends Effect<LeavesConfig> {
6
+ readonly #scale: number;
7
+ readonly #size: number;
8
+ #speed: number;
9
+ #wind: number;
10
+ readonly #colors: string[];
11
+ #maxCount: number;
12
+ #time: number = 0;
13
+ #leaves: Leaf[] = [];
14
+ #sprites: HTMLCanvasElement[] = [];
15
+ #height: number = 540;
16
+
17
+ constructor(config: LeavesConfig = {}) {
18
+ super();
19
+
20
+ this.#scale = config.scale ?? 1;
21
+ this.#maxCount = config.count ?? 80;
22
+ this.#size = (config.size ?? 22) * this.#scale;
23
+ this.#speed = config.speed ?? 1;
24
+ this.#wind = config.wind ?? 0.3;
25
+ this.#colors = config.colors ?? LEAF_COLORS;
26
+
27
+ if (innerWidth < 991) {
28
+ this.#maxCount = Math.floor(this.#maxCount / 2);
29
+ }
30
+
31
+ this.#sprites = this.#createSprites();
32
+
33
+ for (let i = 0; i < this.#maxCount; ++i) {
34
+ this.#leaves.push(this.#createLeaf(true));
35
+ }
36
+ }
37
+
38
+ onResize(_width: number, height: number): void {
39
+ this.#height = height;
40
+ }
41
+
42
+ configure(config: Partial<LeavesConfig>): void {
43
+ if (config.speed !== undefined) {
44
+ this.#speed = config.speed;
45
+ }
46
+ if (config.wind !== undefined) {
47
+ this.#wind = config.wind;
48
+ }
49
+ }
50
+
51
+ tick(dt: number, _width: number, height: number): void {
52
+ this.#height = height;
53
+ const speedFactor = (height / 540) / this.#speed;
54
+
55
+ this.#time += 0.015 * dt;
56
+
57
+ const globalWind = Math.sin(this.#time * 0.5) * 0.4
58
+ + Math.sin(this.#time * 1.3 + 2) * 0.2
59
+ + Math.sin(this.#time * 3.1) * 0.1;
60
+
61
+ for (let index = 0; index < this.#leaves.length; index++) {
62
+ const leaf = this.#leaves[index];
63
+
64
+ const swing = Math.sin(this.#time * leaf.swingFrequency + leaf.swingOffset) * leaf.swingAmplitude;
65
+
66
+ leaf.x += (swing + (this.#wind + globalWind * 0.5) * leaf.depth) * dt / (2500 * speedFactor);
67
+ leaf.y += (leaf.fallSpeed * 2 + leaf.depth + leaf.size * 0.05) * dt / (500 * speedFactor);
68
+
69
+ leaf.rotation += leaf.rotationSpeed * dt;
70
+ leaf.flipAngle += leaf.flipSpeed * dt;
71
+
72
+ if (leaf.x > 1.15 || leaf.x < -0.15 || leaf.y > 1.05) {
73
+ const recycled = this.#createLeaf(false);
74
+
75
+ if (this.#wind + globalWind > 0.15) {
76
+ recycled.x = -0.15;
77
+ recycled.y = MULBERRY.next() * 0.7;
78
+ } else if (this.#wind + globalWind < -0.15) {
79
+ recycled.x = 1.15;
80
+ recycled.y = MULBERRY.next() * 0.7;
81
+ } else {
82
+ recycled.x = MULBERRY.next();
83
+ recycled.y = -0.05 - MULBERRY.next() * 0.15;
84
+ }
85
+
86
+ this.#leaves[index] = recycled;
87
+ }
88
+ }
89
+ }
90
+
91
+ draw(ctx: CanvasRenderingContext2D, width: number, height: number): void {
92
+
93
+ for (const leaf of this.#leaves) {
94
+ const px = leaf.x * width;
95
+ const py = leaf.y * height;
96
+ const displaySize = leaf.size * leaf.depth;
97
+ const scaleX = Math.cos(leaf.flipAngle);
98
+ const cos = Math.cos(leaf.rotation);
99
+ const sin = Math.sin(leaf.rotation);
100
+
101
+ ctx.setTransform(cos * scaleX, sin * scaleX, -sin, cos, px, py);
102
+ ctx.globalAlpha = 0.3 + leaf.depth * 0.7;
103
+ ctx.drawImage(
104
+ this.#sprites[leaf.colorIndex % this.#sprites.length],
105
+ -displaySize / 2,
106
+ -displaySize / 2,
107
+ displaySize,
108
+ displaySize
109
+ );
110
+ }
111
+
112
+ ctx.resetTransform();
113
+ ctx.globalAlpha = 1;
114
+ }
115
+
116
+ #createSprites(): HTMLCanvasElement[] {
117
+ const sprites: HTMLCanvasElement[] = [];
118
+
119
+ for (const color of this.#colors) {
120
+ sprites.push(this.#createLeafSprite(color, 0));
121
+ sprites.push(this.#createLeafSprite(color, 1));
122
+ sprites.push(this.#createLeafSprite(color, 2));
123
+ }
124
+
125
+ return sprites;
126
+ }
127
+
128
+ #createLeafSprite(color: string, shape: number): HTMLCanvasElement {
129
+ const size = 64;
130
+ const canvas = document.createElement('canvas');
131
+ canvas.width = size;
132
+ canvas.height = size;
133
+ const ctx = canvas.getContext('2d')!;
134
+
135
+ const cx = size / 2;
136
+ const cy = size / 2;
137
+
138
+ ctx.fillStyle = color;
139
+
140
+ switch (shape) {
141
+ case 0:
142
+ this.#drawOvalLeaf(ctx, cx, cy, size);
143
+ break;
144
+ case 1:
145
+ this.#drawMapleLeaf(ctx, cx, cy, size);
146
+ break;
147
+ case 2:
148
+ this.#drawPointedLeaf(ctx, cx, cy, size);
149
+ break;
150
+ }
151
+
152
+ return canvas;
153
+ }
154
+
155
+ #drawOvalLeaf(ctx: CanvasRenderingContext2D, cx: number, cy: number, size: number): void {
156
+ const hw = size * 0.3;
157
+ const hh = size * 0.42;
158
+
159
+ ctx.beginPath();
160
+ ctx.moveTo(cx, cy - hh);
161
+ ctx.bezierCurveTo(cx + hw * 1.2, cy - hh * 0.5, cx + hw, cy + hh * 0.5, cx, cy + hh);
162
+ ctx.bezierCurveTo(cx - hw, cy + hh * 0.5, cx - hw * 1.2, cy - hh * 0.5, cx, cy - hh);
163
+ ctx.fill();
164
+
165
+ ctx.strokeStyle = 'rgba(0, 0, 0, 0.15)';
166
+ ctx.lineWidth = 0.8;
167
+ ctx.beginPath();
168
+ ctx.moveTo(cx, cy - hh * 0.8);
169
+ ctx.lineTo(cx, cy + hh * 0.8);
170
+ ctx.stroke();
171
+
172
+ for (const offset of [-0.3, 0, 0.3]) {
173
+ const py = cy + hh * offset;
174
+ ctx.beginPath();
175
+ ctx.moveTo(cx, py);
176
+ ctx.quadraticCurveTo(cx + hw * 0.5, py - 3, cx + hw * 0.7, py - 1);
177
+ ctx.stroke();
178
+ ctx.beginPath();
179
+ ctx.moveTo(cx, py);
180
+ ctx.quadraticCurveTo(cx - hw * 0.5, py - 3, cx - hw * 0.7, py - 1);
181
+ ctx.stroke();
182
+ }
183
+ }
184
+
185
+ #drawMapleLeaf(ctx: CanvasRenderingContext2D, cx: number, cy: number, size: number): void {
186
+ const r = size * 0.38;
187
+
188
+ ctx.beginPath();
189
+ ctx.moveTo(cx, cy - r);
190
+
191
+ const points = 5;
192
+ for (let i = 0; i < points; i++) {
193
+ const angle = (i / points) * Math.PI * 2 - Math.PI / 2;
194
+ const nextAngle = ((i + 1) / points) * Math.PI * 2 - Math.PI / 2;
195
+ const outerR = r * (0.85 + (i % 2) * 0.15);
196
+ const innerR = r * 0.45;
197
+ const midAngle = (angle + nextAngle) / 2;
198
+
199
+ ctx.lineTo(cx + Math.cos(angle) * outerR, cy + Math.sin(angle) * outerR);
200
+ ctx.lineTo(cx + Math.cos(midAngle) * innerR, cy + Math.sin(midAngle) * innerR);
201
+ }
202
+
203
+ ctx.closePath();
204
+ ctx.fill();
205
+
206
+ ctx.strokeStyle = 'rgba(0, 0, 0, 0.12)';
207
+ ctx.lineWidth = 0.7;
208
+ for (let i = 0; i < points; i++) {
209
+ const angle = (i / points) * Math.PI * 2 - Math.PI / 2;
210
+ const outerR = r * 0.7;
211
+ ctx.beginPath();
212
+ ctx.moveTo(cx, cy);
213
+ ctx.lineTo(cx + Math.cos(angle) * outerR, cy + Math.sin(angle) * outerR);
214
+ ctx.stroke();
215
+ }
216
+ }
217
+
218
+ #drawPointedLeaf(ctx: CanvasRenderingContext2D, cx: number, cy: number, size: number): void {
219
+ const hw = size * 0.22;
220
+ const hh = size * 0.44;
221
+
222
+ ctx.beginPath();
223
+ ctx.moveTo(cx, cy - hh);
224
+ ctx.quadraticCurveTo(cx + hw * 1.6, cy - hh * 0.1, cx + hw * 0.3, cy + hh * 0.6);
225
+ ctx.quadraticCurveTo(cx, cy + hh, cx, cy + hh);
226
+ ctx.quadraticCurveTo(cx, cy + hh, cx - hw * 0.3, cy + hh * 0.6);
227
+ ctx.quadraticCurveTo(cx - hw * 1.6, cy - hh * 0.1, cx, cy - hh);
228
+ ctx.fill();
229
+
230
+ ctx.strokeStyle = 'rgba(0, 0, 0, 0.15)';
231
+ ctx.lineWidth = 0.8;
232
+ ctx.beginPath();
233
+ ctx.moveTo(cx, cy - hh * 0.7);
234
+ ctx.lineTo(cx, cy + hh * 0.9);
235
+ ctx.stroke();
236
+ }
237
+
238
+ #createLeaf(initialSpread: boolean): Leaf {
239
+ const depth = 0.3 + MULBERRY.next() * 0.7;
240
+
241
+ return {
242
+ x: MULBERRY.next(),
243
+ y: initialSpread ? MULBERRY.next() * 2 - 1 : -0.05 - MULBERRY.next() * 0.15,
244
+ size: (MULBERRY.next() * 0.6 + 0.5) * this.#size,
245
+ depth,
246
+ rotation: MULBERRY.next() * Math.PI * 2,
247
+ rotationSpeed: (MULBERRY.next() - 0.5) * 0.04,
248
+ flipAngle: MULBERRY.next() * Math.PI * 2,
249
+ flipSpeed: 0.02 + MULBERRY.next() * 0.04,
250
+ swingAmplitude: 0.4 + MULBERRY.next() * 0.8,
251
+ swingFrequency: 0.5 + MULBERRY.next() * 1.5,
252
+ swingOffset: MULBERRY.next() * Math.PI * 2,
253
+ fallSpeed: 0.3 + MULBERRY.next() * 0.7,
254
+ shape: Math.floor(MULBERRY.next() * 3),
255
+ colorIndex: Math.floor(MULBERRY.next() * this.#colors.length * 3)
256
+ };
257
+ }
258
+ }
@@ -0,0 +1,25 @@
1
+ export interface LeavesConfig {
2
+ readonly count?: number;
3
+ readonly colors?: string[];
4
+ readonly size?: number;
5
+ readonly speed?: number;
6
+ readonly wind?: number;
7
+ readonly scale?: number;
8
+ }
9
+
10
+ export type Leaf = {
11
+ x: number;
12
+ y: number;
13
+ size: number;
14
+ depth: number;
15
+ rotation: number;
16
+ rotationSpeed: number;
17
+ flipAngle: number;
18
+ flipSpeed: number;
19
+ swingAmplitude: number;
20
+ swingFrequency: number;
21
+ swingOffset: number;
22
+ fallSpeed: number;
23
+ shape: number;
24
+ colorIndex: number;
25
+ };
@@ -0,0 +1,3 @@
1
+ import { type Mulberry32, mulberry32 } from '@basmilius/utils';
2
+
3
+ export const MULBERRY: Mulberry32 = mulberry32(13);
@@ -0,0 +1,11 @@
1
+ import { Lightning } from './layer';
2
+ import type { LightningConfig } from './types';
3
+ import type { Effect } from '../effect';
4
+
5
+ export function createLightning(config?: LightningConfig): Effect<LightningConfig> {
6
+ return new Lightning(config);
7
+ }
8
+
9
+ export { LightningSystem } from './system';
10
+ export type { LightningSystemConfig } from './system';
11
+ export type { LightningConfig, LightningBolt, LightningBranch } from './types';
@@ -0,0 +1,41 @@
1
+ import { hexToRGB } from '@basmilius/utils';
2
+ import { Effect } from '../effect';
3
+ import { MULBERRY } from './consts';
4
+ import type { LightningConfig } from './types';
5
+ import { LightningSystem } from './system';
6
+
7
+ export class Lightning extends Effect<LightningConfig> {
8
+ readonly #system: LightningSystem;
9
+ readonly #enableFlash: boolean;
10
+
11
+ constructor(config: LightningConfig = {}) {
12
+ super();
13
+
14
+ this.#enableFlash = config.flash ?? true;
15
+
16
+ const [r, g, b] = hexToRGB(config.color ?? '#b4c8ff');
17
+ this.#system = new LightningSystem(
18
+ {
19
+ frequency: config.frequency ?? 1,
20
+ color: [r, g, b],
21
+ branches: config.branches ?? true,
22
+ flash: this.#enableFlash,
23
+ scale: config.scale ?? 1
24
+ },
25
+ () => MULBERRY.next()
26
+ );
27
+ }
28
+
29
+ tick(dt: number, _width: number, _height: number): void {
30
+ this.#system.tick(dt);
31
+ }
32
+
33
+ draw(ctx: CanvasRenderingContext2D, width: number, height: number): void {
34
+ if (this.#system.flashAlpha > 0) {
35
+ ctx.fillStyle = `rgba(255, 255, 255, ${this.#system.flashAlpha})`;
36
+ ctx.fillRect(0, 0, width, height);
37
+ }
38
+
39
+ this.#system.draw(ctx, width, height);
40
+ }
41
+ }
@@ -0,0 +1,196 @@
1
+ import type { LightningBolt, LightningBranch } from './types';
2
+
3
+ export interface LightningSystemConfig {
4
+ readonly frequency?: number;
5
+ readonly color?: [number, number, number];
6
+ readonly branches?: boolean;
7
+ readonly flash?: boolean;
8
+ readonly scale?: number;
9
+ readonly groundLevel?: number;
10
+ }
11
+
12
+ export class LightningSystem {
13
+ readonly #frequency: number;
14
+ readonly #colorR: number;
15
+ readonly #colorG: number;
16
+ readonly #colorB: number;
17
+ readonly #enableBranches: boolean;
18
+ readonly #enableFlash: boolean;
19
+ readonly #scale: number;
20
+ readonly #groundLevel: number;
21
+ readonly #rng: () => number;
22
+ #bolts: LightningBolt[] = [];
23
+ #flashAlpha: number = 0;
24
+ #cooldown: number = 0;
25
+
26
+ get flashAlpha(): number {
27
+ return this.#flashAlpha;
28
+ }
29
+
30
+ constructor(config: LightningSystemConfig, rng: () => number) {
31
+ this.#frequency = config.frequency ?? 1;
32
+ this.#enableBranches = config.branches ?? true;
33
+ this.#enableFlash = config.flash ?? true;
34
+ this.#scale = config.scale ?? 1;
35
+ this.#groundLevel = config.groundLevel ?? 1;
36
+ this.#rng = rng;
37
+
38
+ const color = config.color ?? [180, 200, 255];
39
+ this.#colorR = color[0];
40
+ this.#colorG = color[1];
41
+ this.#colorB = color[2];
42
+
43
+ this.#cooldown = this.#nextCooldown();
44
+ }
45
+
46
+ tick(dt: number): void {
47
+ this.#cooldown -= dt;
48
+
49
+ if (this.#cooldown <= 0) {
50
+ this.#bolts.push(this.#createBolt());
51
+
52
+ if (this.#enableFlash) {
53
+ this.#flashAlpha = 0.3;
54
+ }
55
+
56
+ this.#cooldown = this.#nextCooldown();
57
+ }
58
+
59
+ let alive = 0;
60
+
61
+ for (let i = 0; i < this.#bolts.length; i++) {
62
+ const bolt = this.#bolts[i];
63
+
64
+ bolt.ticksAlive += dt;
65
+ bolt.alpha = Math.max(0, 1 - bolt.ticksAlive / bolt.lifetime);
66
+
67
+ if (bolt.alpha > 0) {
68
+ this.#bolts[alive++] = bolt;
69
+ }
70
+ }
71
+
72
+ this.#bolts.length = alive;
73
+
74
+ if (this.#flashAlpha > 0) {
75
+ this.#flashAlpha -= 0.012 * dt;
76
+
77
+ if (this.#flashAlpha < 0) {
78
+ this.#flashAlpha = 0;
79
+ }
80
+ }
81
+ }
82
+
83
+ draw(ctx: CanvasRenderingContext2D, width: number, height: number): void {
84
+ ctx.globalCompositeOperation = 'lighter';
85
+ ctx.lineCap = 'round';
86
+ ctx.lineJoin = 'round';
87
+
88
+ for (const bolt of this.#bolts) {
89
+ if (bolt.alpha <= 0) {
90
+ continue;
91
+ }
92
+
93
+ this.#drawSegments(ctx, bolt.segments, bolt.alpha, 6 * this.#scale, 2 * this.#scale, width, height);
94
+
95
+ for (const branch of bolt.branches) {
96
+ const branchAlpha = bolt.alpha * branch.alpha;
97
+ this.#drawSegments(ctx, branch.segments, branchAlpha, 3 * this.#scale, 1 * this.#scale, width, height);
98
+ }
99
+ }
100
+
101
+ ctx.globalCompositeOperation = 'source-over';
102
+ }
103
+
104
+ #drawSegments(ctx: CanvasRenderingContext2D, segments: { x: number; y: number }[], alpha: number, outerWidth: number, innerWidth: number, width: number, height: number): void {
105
+ if (segments.length < 2) {
106
+ return;
107
+ }
108
+
109
+ // Outer glow
110
+ ctx.beginPath();
111
+ ctx.moveTo(segments[0].x * width, segments[0].y * height);
112
+
113
+ for (let i = 1; i < segments.length; i++) {
114
+ ctx.lineTo(segments[i].x * width, segments[i].y * height);
115
+ }
116
+
117
+ ctx.strokeStyle = `rgba(${this.#colorR}, ${this.#colorG}, ${this.#colorB}, ${alpha * 0.4})`;
118
+ ctx.lineWidth = outerWidth;
119
+ ctx.stroke();
120
+
121
+ // Inner bright line
122
+ ctx.beginPath();
123
+ ctx.moveTo(segments[0].x * width, segments[0].y * height);
124
+
125
+ for (let i = 1; i < segments.length; i++) {
126
+ ctx.lineTo(segments[i].x * width, segments[i].y * height);
127
+ }
128
+
129
+ ctx.strokeStyle = `rgba(255, 255, 255, ${alpha})`;
130
+ ctx.lineWidth = innerWidth;
131
+ ctx.stroke();
132
+ }
133
+
134
+ #createBolt(): LightningBolt {
135
+ const startX = 0.1 + this.#rng() * 0.8;
136
+ const segments: { x: number; y: number }[] = [{x: startX, y: 0}];
137
+ const branches: LightningBranch[] = [];
138
+
139
+ let currentX = startX;
140
+ let currentY = 0;
141
+ const steps = 10 + Math.floor(this.#rng() * 10);
142
+
143
+ for (let i = 0; i < steps; i++) {
144
+ currentX += (this.#rng() - 0.5) * 0.1;
145
+ currentY += (0.6 + this.#rng() * 0.4) / steps;
146
+ currentX = Math.max(0.02, Math.min(0.98, currentX));
147
+ segments.push({x: currentX, y: Math.min(currentY, this.#groundLevel)});
148
+
149
+ if (this.#enableBranches && i > 1 && i < steps - 2 && this.#rng() < 0.35) {
150
+ branches.push(this.#createBranch(currentX, currentY));
151
+ }
152
+
153
+ if (currentY >= this.#groundLevel) {
154
+ break;
155
+ }
156
+ }
157
+
158
+ return {
159
+ segments,
160
+ branches,
161
+ alpha: 1,
162
+ lifetime: 40 + this.#rng() * 40,
163
+ ticksAlive: 0
164
+ };
165
+ }
166
+
167
+ #createBranch(startX: number, startY: number): LightningBranch {
168
+ const segments: { x: number; y: number }[] = [{x: startX, y: startY}];
169
+ const direction = this.#rng() > 0.5 ? 1 : -1;
170
+ const branchSteps = 3 + Math.floor(this.#rng() * 5);
171
+
172
+ let bx = startX;
173
+ let by = startY;
174
+
175
+ for (let i = 0; i < branchSteps; i++) {
176
+ bx += (0.02 + this.#rng() * 0.04) * direction;
177
+ by += (0.02 + this.#rng() * 0.04);
178
+ bx = Math.max(0.02, Math.min(0.98, bx));
179
+ segments.push({x: bx, y: Math.min(by, this.#groundLevel)});
180
+
181
+ if (by >= this.#groundLevel) {
182
+ break;
183
+ }
184
+ }
185
+
186
+ return {
187
+ segments,
188
+ alpha: 0.4 + this.#rng() * 0.4
189
+ };
190
+ }
191
+
192
+ #nextCooldown(): number {
193
+ const baseCooldown = 180 / this.#frequency;
194
+ return baseCooldown + this.#rng() * baseCooldown;
195
+ }
196
+ }
@@ -0,0 +1,20 @@
1
+ export interface LightningConfig {
2
+ readonly frequency?: number;
3
+ readonly color?: string;
4
+ readonly branches?: boolean;
5
+ readonly flash?: boolean;
6
+ readonly scale?: number;
7
+ }
8
+
9
+ export type LightningBranch = {
10
+ segments: { x: number; y: number }[];
11
+ alpha: number;
12
+ };
13
+
14
+ export type LightningBolt = {
15
+ segments: { x: number; y: number }[];
16
+ branches: LightningBranch[];
17
+ alpha: number;
18
+ lifetime: number;
19
+ ticksAlive: number;
20
+ };
@@ -0,0 +1,5 @@
1
+ import { type Mulberry32, mulberry32 } from '@basmilius/utils';
2
+
3
+ export const MULBERRY: Mulberry32 = mulberry32(13);
4
+
5
+ export const MATRIX_CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
@@ -0,0 +1,9 @@
1
+ import { Matrix } from './layer';
2
+ import type { MatrixConfig } from './types';
3
+ import type { Effect } from '../effect';
4
+
5
+ export function createMatrix(config?: MatrixConfig): Effect<MatrixConfig> {
6
+ return new Matrix(config);
7
+ }
8
+
9
+ export type { MatrixConfig, MatrixColumn } from './types';