@basmilius/sparkle 2.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.
- package/dist/index.d.mts +1192 -14
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +4552 -370
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -1
- package/src/aurora/consts.ts +3 -0
- package/src/aurora/index.ts +4 -0
- package/src/aurora/layer.ts +152 -0
- package/src/aurora/simulation.ts +19 -0
- package/src/aurora/types.ts +13 -0
- package/src/balloons/consts.ts +3 -0
- package/src/balloons/index.ts +6 -0
- package/src/balloons/layer.ts +138 -0
- package/src/balloons/particle.ts +110 -0
- package/src/balloons/simulation.ts +19 -0
- package/src/balloons/types.ts +14 -0
- package/src/bubbles/consts.ts +3 -0
- package/src/bubbles/index.ts +4 -0
- package/src/bubbles/layer.ts +233 -0
- package/src/bubbles/simulation.ts +20 -0
- package/src/bubbles/types.ts +21 -0
- package/src/canvas.ts +20 -1
- package/src/color.ts +10 -0
- package/src/confetti/consts.ts +13 -13
- package/src/confetti/index.ts +6 -0
- package/src/confetti/layer.ts +152 -0
- package/src/confetti/particle.ts +105 -0
- package/src/confetti/shapes.ts +104 -0
- package/src/confetti/simulation.ts +9 -203
- package/src/confetti/types.ts +4 -1
- package/src/distance.ts +1 -1
- package/src/donuts/consts.ts +19 -0
- package/src/donuts/donut.ts +12 -0
- package/src/donuts/index.ts +3 -0
- package/src/donuts/layer.ts +270 -0
- package/src/donuts/simulation.ts +25 -0
- package/src/fireflies/consts.ts +3 -0
- package/src/fireflies/index.ts +6 -0
- package/src/fireflies/layer.ts +152 -0
- package/src/fireflies/particle.ts +124 -0
- package/src/fireflies/simulation.ts +18 -0
- package/src/fireflies/types.ts +17 -0
- package/src/firepit/consts.ts +3 -0
- package/src/firepit/index.ts +4 -0
- package/src/firepit/layer.ts +174 -0
- package/src/firepit/simulation.ts +17 -0
- package/src/firepit/types.ts +20 -0
- package/src/fireworks/explosion.ts +8 -8
- package/src/fireworks/firework.ts +9 -8
- package/src/fireworks/index.ts +6 -2
- package/src/fireworks/layer.ts +452 -0
- package/src/fireworks/simulation.ts +9 -484
- package/src/fireworks/spark.ts +7 -7
- package/src/glitter/consts.ts +13 -0
- package/src/glitter/index.ts +4 -0
- package/src/glitter/layer.ts +173 -0
- package/src/glitter/simulation.ts +19 -0
- package/src/glitter/types.ts +23 -0
- package/src/index.ts +28 -0
- package/src/lanterns/consts.ts +13 -0
- package/src/lanterns/index.ts +4 -0
- package/src/lanterns/layer.ts +166 -0
- package/src/lanterns/simulation.ts +17 -0
- package/src/lanterns/types.ts +14 -0
- package/src/layer.ts +24 -0
- package/src/layered.ts +185 -0
- package/src/leaves/consts.ts +16 -0
- package/src/leaves/index.ts +4 -0
- package/src/leaves/layer.ts +251 -0
- package/src/leaves/simulation.ts +18 -0
- package/src/leaves/types.ts +16 -0
- package/src/lightning/consts.ts +3 -0
- package/src/lightning/index.ts +6 -0
- package/src/lightning/layer.ts +41 -0
- package/src/lightning/simulation.ts +17 -0
- package/src/lightning/system.ts +196 -0
- package/src/lightning/types.ts +12 -0
- package/src/matrix/consts.ts +5 -0
- package/src/matrix/index.ts +4 -0
- package/src/matrix/layer.ts +146 -0
- package/src/matrix/simulation.ts +18 -0
- package/src/matrix/types.ts +8 -0
- package/src/orbits/consts.ts +13 -0
- package/src/orbits/index.ts +4 -0
- package/src/orbits/layer.ts +183 -0
- package/src/orbits/simulation.ts +19 -0
- package/src/orbits/types.ts +16 -0
- package/src/particles/consts.ts +3 -0
- package/src/particles/index.ts +4 -0
- package/src/particles/layer.ts +317 -0
- package/src/particles/simulation.ts +26 -0
- package/src/particles/types.ts +10 -0
- package/src/petals/consts.ts +13 -0
- package/src/petals/index.ts +4 -0
- package/src/petals/layer.ts +158 -0
- package/src/petals/simulation.ts +18 -0
- package/src/petals/types.ts +15 -0
- package/src/plasma/consts.ts +3 -0
- package/src/plasma/index.ts +4 -0
- package/src/plasma/layer.ts +92 -0
- package/src/plasma/simulation.ts +17 -0
- package/src/plasma/types.ts +5 -0
- package/src/rain/consts.ts +3 -0
- package/src/rain/index.ts +6 -0
- package/src/rain/layer.ts +172 -0
- package/src/rain/particle.ts +132 -0
- package/src/rain/simulation.ts +21 -0
- package/src/rain/types.ts +22 -0
- package/src/sandstorm/consts.ts +3 -0
- package/src/sandstorm/index.ts +4 -0
- package/src/sandstorm/layer.ts +135 -0
- package/src/sandstorm/simulation.ts +18 -0
- package/src/sandstorm/types.ts +10 -0
- package/src/shooting-stars/index.ts +3 -0
- package/src/shooting-stars/system.ts +149 -0
- package/src/shooting-stars/types.ts +10 -0
- package/src/simulation-canvas.ts +47 -0
- package/src/snow/consts.ts +2 -2
- package/src/snow/index.ts +1 -0
- package/src/snow/layer.ts +263 -0
- package/src/snow/simulation.ts +4 -288
- package/src/sparklers/consts.ts +3 -0
- package/src/sparklers/index.ts +6 -0
- package/src/sparklers/layer.ts +174 -0
- package/src/sparklers/particle.ts +89 -0
- package/src/sparklers/simulation.ts +30 -0
- package/src/sparklers/types.ts +13 -0
- package/src/stars/consts.ts +3 -0
- package/src/stars/index.ts +4 -0
- package/src/stars/layer.ts +133 -0
- package/src/stars/simulation.ts +22 -0
- package/src/stars/types.ts +12 -0
- package/src/streamers/consts.ts +14 -0
- package/src/streamers/index.ts +4 -0
- package/src/streamers/layer.ts +211 -0
- package/src/streamers/simulation.ts +16 -0
- package/src/streamers/types.ts +14 -0
- package/src/trail.ts +140 -0
- package/src/waves/consts.ts +3 -0
- package/src/waves/index.ts +4 -0
- package/src/waves/layer.ts +167 -0
- package/src/waves/simulation.ts +18 -0
- package/src/waves/types.ts +9 -0
- package/src/wormhole/consts.ts +3 -0
- package/src/wormhole/index.ts +4 -0
- package/src/wormhole/layer.ts +181 -0
- package/src/wormhole/simulation.ts +17 -0
- package/src/wormhole/types.ts +10 -0
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type Mulberry32, mulberry32 } from '@basmilius/utils';
|
|
2
|
+
|
|
3
|
+
export const MULBERRY: Mulberry32 = mulberry32(13);
|
|
4
|
+
|
|
5
|
+
export const LEAF_COLORS: string[] = [
|
|
6
|
+
'#c0392b', // deep red
|
|
7
|
+
'#e74c3c', // bright red
|
|
8
|
+
'#d35400', // burnt orange
|
|
9
|
+
'#e67e22', // orange
|
|
10
|
+
'#f39c12', // amber
|
|
11
|
+
'#f1c40f', // golden yellow
|
|
12
|
+
'#d4a017', // dark gold
|
|
13
|
+
'#8b4513', // saddle brown
|
|
14
|
+
'#a0522d', // sienna
|
|
15
|
+
'#6b8e23' // olive (some green still)
|
|
16
|
+
];
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
import { SimulationLayer } from '../layer';
|
|
2
|
+
import { LEAF_COLORS, MULBERRY } from './consts';
|
|
3
|
+
import type { LeafSimulationConfig } from './simulation';
|
|
4
|
+
import type { Leaf } from './types';
|
|
5
|
+
|
|
6
|
+
export class LeafLayer extends SimulationLayer {
|
|
7
|
+
readonly #scale: number;
|
|
8
|
+
readonly #size: number;
|
|
9
|
+
readonly #speed: number;
|
|
10
|
+
readonly #wind: number;
|
|
11
|
+
readonly #colors: string[];
|
|
12
|
+
#maxCount: number;
|
|
13
|
+
#time: number = 0;
|
|
14
|
+
#leaves: Leaf[] = [];
|
|
15
|
+
#sprites: HTMLCanvasElement[] = [];
|
|
16
|
+
#height: number = 540;
|
|
17
|
+
|
|
18
|
+
constructor(config: LeafSimulationConfig = {}) {
|
|
19
|
+
super();
|
|
20
|
+
|
|
21
|
+
this.#scale = config.scale ?? 1;
|
|
22
|
+
this.#maxCount = config.count ?? 80;
|
|
23
|
+
this.#size = (config.size ?? 22) * this.#scale;
|
|
24
|
+
this.#speed = config.speed ?? 1;
|
|
25
|
+
this.#wind = config.wind ?? 0.3;
|
|
26
|
+
this.#colors = config.colors ?? LEAF_COLORS;
|
|
27
|
+
|
|
28
|
+
if (innerWidth < 991) {
|
|
29
|
+
this.#maxCount = Math.floor(this.#maxCount / 2);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
this.#sprites = this.#createSprites();
|
|
33
|
+
|
|
34
|
+
for (let i = 0; i < this.#maxCount; ++i) {
|
|
35
|
+
this.#leaves.push(this.#createLeaf(true));
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
onResize(_width: number, height: number): void {
|
|
40
|
+
this.#height = height;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
tick(dt: number, _width: number, height: number): void {
|
|
44
|
+
this.#height = height;
|
|
45
|
+
const speedFactor = (height / 540) / this.#speed;
|
|
46
|
+
|
|
47
|
+
this.#time += 0.015 * dt;
|
|
48
|
+
|
|
49
|
+
const globalWind = Math.sin(this.#time * 0.5) * 0.4
|
|
50
|
+
+ Math.sin(this.#time * 1.3 + 2) * 0.2
|
|
51
|
+
+ Math.sin(this.#time * 3.1) * 0.1;
|
|
52
|
+
|
|
53
|
+
for (let index = 0; index < this.#leaves.length; index++) {
|
|
54
|
+
const leaf = this.#leaves[index];
|
|
55
|
+
|
|
56
|
+
const swing = Math.sin(this.#time * leaf.swingFrequency + leaf.swingOffset) * leaf.swingAmplitude;
|
|
57
|
+
|
|
58
|
+
leaf.x += (swing + (this.#wind + globalWind * 0.5) * leaf.depth) * dt / (2500 * speedFactor);
|
|
59
|
+
leaf.y += (leaf.fallSpeed * 2 + leaf.depth + leaf.size * 0.05) * dt / (500 * speedFactor);
|
|
60
|
+
|
|
61
|
+
leaf.rotation += leaf.rotationSpeed * dt;
|
|
62
|
+
leaf.flipAngle += leaf.flipSpeed * dt;
|
|
63
|
+
|
|
64
|
+
if (leaf.x > 1.15 || leaf.x < -0.15 || leaf.y > 1.05) {
|
|
65
|
+
const recycled = this.#createLeaf(false);
|
|
66
|
+
|
|
67
|
+
if (this.#wind + globalWind > 0.15) {
|
|
68
|
+
recycled.x = -0.15;
|
|
69
|
+
recycled.y = MULBERRY.next() * 0.7;
|
|
70
|
+
} else if (this.#wind + globalWind < -0.15) {
|
|
71
|
+
recycled.x = 1.15;
|
|
72
|
+
recycled.y = MULBERRY.next() * 0.7;
|
|
73
|
+
} else {
|
|
74
|
+
recycled.x = MULBERRY.next();
|
|
75
|
+
recycled.y = -0.05 - MULBERRY.next() * 0.15;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
this.#leaves[index] = recycled;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
draw(ctx: CanvasRenderingContext2D, width: number, height: number): void {
|
|
84
|
+
|
|
85
|
+
for (const leaf of this.#leaves) {
|
|
86
|
+
const px = leaf.x * width;
|
|
87
|
+
const py = leaf.y * height;
|
|
88
|
+
const displaySize = leaf.size * leaf.depth;
|
|
89
|
+
const scaleX = Math.cos(leaf.flipAngle);
|
|
90
|
+
|
|
91
|
+
ctx.save();
|
|
92
|
+
ctx.translate(px, py);
|
|
93
|
+
ctx.rotate(leaf.rotation);
|
|
94
|
+
ctx.scale(scaleX, 1);
|
|
95
|
+
ctx.globalAlpha = 0.3 + leaf.depth * 0.7;
|
|
96
|
+
ctx.drawImage(
|
|
97
|
+
this.#sprites[leaf.colorIndex % this.#sprites.length],
|
|
98
|
+
-displaySize / 2,
|
|
99
|
+
-displaySize / 2,
|
|
100
|
+
displaySize,
|
|
101
|
+
displaySize
|
|
102
|
+
);
|
|
103
|
+
ctx.restore();
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
ctx.globalAlpha = 1;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
#createSprites(): HTMLCanvasElement[] {
|
|
110
|
+
const sprites: HTMLCanvasElement[] = [];
|
|
111
|
+
|
|
112
|
+
for (const color of this.#colors) {
|
|
113
|
+
sprites.push(this.#createLeafSprite(color, 0));
|
|
114
|
+
sprites.push(this.#createLeafSprite(color, 1));
|
|
115
|
+
sprites.push(this.#createLeafSprite(color, 2));
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return sprites;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
#createLeafSprite(color: string, shape: number): HTMLCanvasElement {
|
|
122
|
+
const size = 64;
|
|
123
|
+
const canvas = document.createElement('canvas');
|
|
124
|
+
canvas.width = size;
|
|
125
|
+
canvas.height = size;
|
|
126
|
+
const ctx = canvas.getContext('2d')!;
|
|
127
|
+
|
|
128
|
+
const cx = size / 2;
|
|
129
|
+
const cy = size / 2;
|
|
130
|
+
|
|
131
|
+
ctx.fillStyle = color;
|
|
132
|
+
|
|
133
|
+
switch (shape) {
|
|
134
|
+
case 0:
|
|
135
|
+
this.#drawOvalLeaf(ctx, cx, cy, size);
|
|
136
|
+
break;
|
|
137
|
+
case 1:
|
|
138
|
+
this.#drawMapleLeaf(ctx, cx, cy, size);
|
|
139
|
+
break;
|
|
140
|
+
case 2:
|
|
141
|
+
this.#drawPointedLeaf(ctx, cx, cy, size);
|
|
142
|
+
break;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return canvas;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
#drawOvalLeaf(ctx: CanvasRenderingContext2D, cx: number, cy: number, size: number): void {
|
|
149
|
+
const hw = size * 0.3;
|
|
150
|
+
const hh = size * 0.42;
|
|
151
|
+
|
|
152
|
+
ctx.beginPath();
|
|
153
|
+
ctx.moveTo(cx, cy - hh);
|
|
154
|
+
ctx.bezierCurveTo(cx + hw * 1.2, cy - hh * 0.5, cx + hw, cy + hh * 0.5, cx, cy + hh);
|
|
155
|
+
ctx.bezierCurveTo(cx - hw, cy + hh * 0.5, cx - hw * 1.2, cy - hh * 0.5, cx, cy - hh);
|
|
156
|
+
ctx.fill();
|
|
157
|
+
|
|
158
|
+
ctx.strokeStyle = 'rgba(0, 0, 0, 0.15)';
|
|
159
|
+
ctx.lineWidth = 0.8;
|
|
160
|
+
ctx.beginPath();
|
|
161
|
+
ctx.moveTo(cx, cy - hh * 0.8);
|
|
162
|
+
ctx.lineTo(cx, cy + hh * 0.8);
|
|
163
|
+
ctx.stroke();
|
|
164
|
+
|
|
165
|
+
for (const offset of [-0.3, 0, 0.3]) {
|
|
166
|
+
const py = cy + hh * offset;
|
|
167
|
+
ctx.beginPath();
|
|
168
|
+
ctx.moveTo(cx, py);
|
|
169
|
+
ctx.quadraticCurveTo(cx + hw * 0.5, py - 3, cx + hw * 0.7, py - 1);
|
|
170
|
+
ctx.stroke();
|
|
171
|
+
ctx.beginPath();
|
|
172
|
+
ctx.moveTo(cx, py);
|
|
173
|
+
ctx.quadraticCurveTo(cx - hw * 0.5, py - 3, cx - hw * 0.7, py - 1);
|
|
174
|
+
ctx.stroke();
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
#drawMapleLeaf(ctx: CanvasRenderingContext2D, cx: number, cy: number, size: number): void {
|
|
179
|
+
const r = size * 0.38;
|
|
180
|
+
|
|
181
|
+
ctx.beginPath();
|
|
182
|
+
ctx.moveTo(cx, cy - r);
|
|
183
|
+
|
|
184
|
+
const points = 5;
|
|
185
|
+
for (let i = 0; i < points; i++) {
|
|
186
|
+
const angle = (i / points) * Math.PI * 2 - Math.PI / 2;
|
|
187
|
+
const nextAngle = ((i + 1) / points) * Math.PI * 2 - Math.PI / 2;
|
|
188
|
+
const outerR = r * (0.85 + (i % 2) * 0.15);
|
|
189
|
+
const innerR = r * 0.45;
|
|
190
|
+
const midAngle = (angle + nextAngle) / 2;
|
|
191
|
+
|
|
192
|
+
ctx.lineTo(cx + Math.cos(angle) * outerR, cy + Math.sin(angle) * outerR);
|
|
193
|
+
ctx.lineTo(cx + Math.cos(midAngle) * innerR, cy + Math.sin(midAngle) * innerR);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
ctx.closePath();
|
|
197
|
+
ctx.fill();
|
|
198
|
+
|
|
199
|
+
ctx.strokeStyle = 'rgba(0, 0, 0, 0.12)';
|
|
200
|
+
ctx.lineWidth = 0.7;
|
|
201
|
+
for (let i = 0; i < points; i++) {
|
|
202
|
+
const angle = (i / points) * Math.PI * 2 - Math.PI / 2;
|
|
203
|
+
const outerR = r * 0.7;
|
|
204
|
+
ctx.beginPath();
|
|
205
|
+
ctx.moveTo(cx, cy);
|
|
206
|
+
ctx.lineTo(cx + Math.cos(angle) * outerR, cy + Math.sin(angle) * outerR);
|
|
207
|
+
ctx.stroke();
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
#drawPointedLeaf(ctx: CanvasRenderingContext2D, cx: number, cy: number, size: number): void {
|
|
212
|
+
const hw = size * 0.22;
|
|
213
|
+
const hh = size * 0.44;
|
|
214
|
+
|
|
215
|
+
ctx.beginPath();
|
|
216
|
+
ctx.moveTo(cx, cy - hh);
|
|
217
|
+
ctx.quadraticCurveTo(cx + hw * 1.6, cy - hh * 0.1, cx + hw * 0.3, cy + hh * 0.6);
|
|
218
|
+
ctx.quadraticCurveTo(cx, cy + hh, cx, cy + hh);
|
|
219
|
+
ctx.quadraticCurveTo(cx, cy + hh, cx - hw * 0.3, cy + hh * 0.6);
|
|
220
|
+
ctx.quadraticCurveTo(cx - hw * 1.6, cy - hh * 0.1, cx, cy - hh);
|
|
221
|
+
ctx.fill();
|
|
222
|
+
|
|
223
|
+
ctx.strokeStyle = 'rgba(0, 0, 0, 0.15)';
|
|
224
|
+
ctx.lineWidth = 0.8;
|
|
225
|
+
ctx.beginPath();
|
|
226
|
+
ctx.moveTo(cx, cy - hh * 0.7);
|
|
227
|
+
ctx.lineTo(cx, cy + hh * 0.9);
|
|
228
|
+
ctx.stroke();
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
#createLeaf(initialSpread: boolean): Leaf {
|
|
232
|
+
const depth = 0.3 + MULBERRY.next() * 0.7;
|
|
233
|
+
|
|
234
|
+
return {
|
|
235
|
+
x: MULBERRY.next(),
|
|
236
|
+
y: initialSpread ? MULBERRY.next() * 2 - 1 : -0.05 - MULBERRY.next() * 0.15,
|
|
237
|
+
size: (MULBERRY.next() * 0.6 + 0.5) * this.#size,
|
|
238
|
+
depth,
|
|
239
|
+
rotation: MULBERRY.next() * Math.PI * 2,
|
|
240
|
+
rotationSpeed: (MULBERRY.next() - 0.5) * 0.04,
|
|
241
|
+
flipAngle: MULBERRY.next() * Math.PI * 2,
|
|
242
|
+
flipSpeed: 0.02 + MULBERRY.next() * 0.04,
|
|
243
|
+
swingAmplitude: 0.4 + MULBERRY.next() * 0.8,
|
|
244
|
+
swingFrequency: 0.5 + MULBERRY.next() * 1.5,
|
|
245
|
+
swingOffset: MULBERRY.next() * Math.PI * 2,
|
|
246
|
+
fallSpeed: 0.3 + MULBERRY.next() * 0.7,
|
|
247
|
+
shape: Math.floor(MULBERRY.next() * 3),
|
|
248
|
+
colorIndex: Math.floor(MULBERRY.next() * this.#colors.length * 3)
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { SimulationCanvas } from '../simulation-canvas';
|
|
2
|
+
import { LeafLayer } from './layer';
|
|
3
|
+
|
|
4
|
+
export interface LeafSimulationConfig {
|
|
5
|
+
readonly count?: number;
|
|
6
|
+
readonly colors?: string[];
|
|
7
|
+
readonly size?: number;
|
|
8
|
+
readonly speed?: number;
|
|
9
|
+
readonly wind?: number;
|
|
10
|
+
readonly scale?: number;
|
|
11
|
+
readonly canvasOptions?: CanvasRenderingContext2DSettings;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export class LeafSimulation extends SimulationCanvas {
|
|
15
|
+
constructor(canvas: HTMLCanvasElement, config: LeafSimulationConfig = {}) {
|
|
16
|
+
super(canvas, new LeafLayer(config), 60, config.canvasOptions ?? {colorSpace: 'display-p3'});
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export type Leaf = {
|
|
2
|
+
x: number;
|
|
3
|
+
y: number;
|
|
4
|
+
size: number;
|
|
5
|
+
depth: number;
|
|
6
|
+
rotation: number;
|
|
7
|
+
rotationSpeed: number;
|
|
8
|
+
flipAngle: number;
|
|
9
|
+
flipSpeed: number;
|
|
10
|
+
swingAmplitude: number;
|
|
11
|
+
swingFrequency: number;
|
|
12
|
+
swingOffset: number;
|
|
13
|
+
fallSpeed: number;
|
|
14
|
+
shape: number;
|
|
15
|
+
colorIndex: number;
|
|
16
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { LightningLayer } from './layer';
|
|
2
|
+
export { LightningSimulation } from './simulation';
|
|
3
|
+
export type { LightningSimulationConfig } from './simulation';
|
|
4
|
+
export { LightningSystem } from './system';
|
|
5
|
+
export type { LightningSystemConfig } from './system';
|
|
6
|
+
export type { LightningBolt, LightningBranch } from './types';
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { hexToRGB } from '@basmilius/utils';
|
|
2
|
+
import { SimulationLayer } from '../layer';
|
|
3
|
+
import { MULBERRY } from './consts';
|
|
4
|
+
import type { LightningSimulationConfig } from './simulation';
|
|
5
|
+
import { LightningSystem } from './system';
|
|
6
|
+
|
|
7
|
+
export class LightningLayer extends SimulationLayer {
|
|
8
|
+
readonly #system: LightningSystem;
|
|
9
|
+
readonly #enableFlash: boolean;
|
|
10
|
+
|
|
11
|
+
constructor(config: LightningSimulationConfig = {}) {
|
|
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,17 @@
|
|
|
1
|
+
import { SimulationCanvas } from '../simulation-canvas';
|
|
2
|
+
import { LightningLayer } from './layer';
|
|
3
|
+
|
|
4
|
+
export interface LightningSimulationConfig {
|
|
5
|
+
readonly frequency?: number;
|
|
6
|
+
readonly color?: string;
|
|
7
|
+
readonly branches?: boolean;
|
|
8
|
+
readonly flash?: boolean;
|
|
9
|
+
readonly scale?: number;
|
|
10
|
+
readonly canvasOptions?: CanvasRenderingContext2DSettings;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export class LightningSimulation extends SimulationCanvas {
|
|
14
|
+
constructor(canvas: HTMLCanvasElement, config: LightningSimulationConfig = {}) {
|
|
15
|
+
super(canvas, new LightningLayer(config), 60, config.canvasOptions ?? {colorSpace: 'display-p3'});
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -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,12 @@
|
|
|
1
|
+
export type LightningBranch = {
|
|
2
|
+
segments: {x: number; y: number}[];
|
|
3
|
+
alpha: number;
|
|
4
|
+
};
|
|
5
|
+
|
|
6
|
+
export type LightningBolt = {
|
|
7
|
+
segments: {x: number; y: number}[];
|
|
8
|
+
branches: LightningBranch[];
|
|
9
|
+
alpha: number;
|
|
10
|
+
lifetime: number;
|
|
11
|
+
ticksAlive: number;
|
|
12
|
+
};
|