@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.
- 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 +3 -2
- 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
package/src/snow/simulation.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import type { Snowflake } from './snowflake';
|
|
1
|
+
import { SimulationCanvas } from '../simulation-canvas';
|
|
2
|
+
import { SnowLayer } from './layer';
|
|
4
3
|
|
|
5
4
|
export interface SnowSimulationConfig {
|
|
6
5
|
readonly fillStyle?: string;
|
|
@@ -11,291 +10,8 @@ export interface SnowSimulationConfig {
|
|
|
11
10
|
readonly canvasOptions?: CanvasRenderingContext2DSettings;
|
|
12
11
|
}
|
|
13
12
|
|
|
14
|
-
|
|
15
|
-
const SPRITE_CENTER = SPRITE_SIZE / 2;
|
|
16
|
-
const SPRITE_RADIUS = SPRITE_SIZE / 2;
|
|
17
|
-
|
|
18
|
-
export class SnowSimulation extends LimitedFrameRateCanvas {
|
|
19
|
-
readonly #scale: number;
|
|
20
|
-
readonly #size: number;
|
|
21
|
-
readonly #speed: number;
|
|
22
|
-
readonly #baseOpacity: number;
|
|
23
|
-
#maxParticles: number;
|
|
24
|
-
#time: number = 0;
|
|
25
|
-
#ratio: number = 1;
|
|
26
|
-
#snowflakes: Snowflake[] = [];
|
|
27
|
-
#sprites: HTMLCanvasElement[] = [];
|
|
28
|
-
|
|
13
|
+
export class SnowSimulation extends SimulationCanvas {
|
|
29
14
|
constructor(canvas: HTMLCanvasElement, config: SnowSimulationConfig = {}) {
|
|
30
|
-
super(canvas, 60, config.canvasOptions ?? {colorSpace: 'display-p3'});
|
|
31
|
-
|
|
32
|
-
this.#scale = config.scale ?? 1;
|
|
33
|
-
this.#maxParticles = config.particles ?? 200;
|
|
34
|
-
this.#size = (config.size ?? 9) * this.#scale;
|
|
35
|
-
this.#speed = config.speed ?? 2;
|
|
36
|
-
|
|
37
|
-
const {r, g, b, a} = this.#parseColor(config.fillStyle ?? 'rgb(255 255 255 / .75)');
|
|
38
|
-
this.#baseOpacity = a;
|
|
39
|
-
|
|
40
|
-
this.canvas.style.position = 'absolute';
|
|
41
|
-
this.canvas.style.top = '0';
|
|
42
|
-
this.canvas.style.left = '0';
|
|
43
|
-
this.canvas.style.height = '100%';
|
|
44
|
-
this.canvas.style.width = '100%';
|
|
45
|
-
|
|
46
|
-
if (this.isSmall) {
|
|
47
|
-
this.#maxParticles = Math.floor(this.#maxParticles / 2);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
this.#sprites = this.#createSprites(r, g, b);
|
|
51
|
-
|
|
52
|
-
for (let i = 0; i < this.#maxParticles; ++i) {
|
|
53
|
-
this.#snowflakes.push(this.#createSnowflake(true));
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
draw(): void {
|
|
58
|
-
this.canvas.height = this.height;
|
|
59
|
-
this.canvas.width = this.width;
|
|
60
|
-
|
|
61
|
-
const ctx = this.context;
|
|
62
|
-
ctx.clearRect(0, 0, this.width, this.height);
|
|
63
|
-
|
|
64
|
-
for (const snowflake of this.#snowflakes) {
|
|
65
|
-
const px = snowflake.x * this.width;
|
|
66
|
-
const py = snowflake.y * this.height;
|
|
67
|
-
const displayRadius = snowflake.radius * snowflake.depth * this.#ratio;
|
|
68
|
-
const displaySize = displayRadius * 2;
|
|
69
|
-
|
|
70
|
-
if (displaySize < 0.5) {
|
|
71
|
-
continue;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
ctx.globalAlpha = this.#baseOpacity * (0.15 + snowflake.depth * 0.85);
|
|
75
|
-
|
|
76
|
-
if (snowflake.spriteIndex === 3) {
|
|
77
|
-
ctx.save();
|
|
78
|
-
ctx.translate(px, py);
|
|
79
|
-
ctx.rotate(snowflake.rotation);
|
|
80
|
-
ctx.drawImage(
|
|
81
|
-
this.#sprites[snowflake.spriteIndex],
|
|
82
|
-
-displayRadius,
|
|
83
|
-
-displayRadius,
|
|
84
|
-
displaySize,
|
|
85
|
-
displaySize
|
|
86
|
-
);
|
|
87
|
-
ctx.restore();
|
|
88
|
-
} else {
|
|
89
|
-
ctx.drawImage(
|
|
90
|
-
this.#sprites[snowflake.spriteIndex],
|
|
91
|
-
px - displayRadius,
|
|
92
|
-
py - displayRadius,
|
|
93
|
-
displaySize,
|
|
94
|
-
displaySize
|
|
95
|
-
);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
ctx.globalAlpha = 1;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
tick(): void {
|
|
103
|
-
const speedFactor = (this.height / (420 * this.#ratio) / this.#speed) * this.deltaFactor;
|
|
104
|
-
|
|
105
|
-
this.#time += 0.015 * speedFactor;
|
|
106
|
-
|
|
107
|
-
// Multi-frequency wind for organic movement
|
|
108
|
-
const wind = Math.sin(this.#time * 0.7) * 0.5
|
|
109
|
-
+ Math.sin(this.#time * 1.9 + 3) * 0.25
|
|
110
|
-
+ Math.sin(this.#time * 4.3 + 1) * 0.1;
|
|
111
|
-
|
|
112
|
-
for (let index = 0; index < this.#snowflakes.length; index++) {
|
|
113
|
-
const snowflake = this.#snowflakes[index];
|
|
114
|
-
|
|
115
|
-
// Individual swing oscillation
|
|
116
|
-
const swing = Math.sin(this.#time * snowflake.swingFrequency + snowflake.swingOffset) * snowflake.swingAmplitude;
|
|
117
|
-
|
|
118
|
-
// Horizontal: personal swing + global wind (deeper = more wind influence)
|
|
119
|
-
snowflake.x += (swing + wind * snowflake.depth * 2) / (4000 * speedFactor);
|
|
120
|
-
|
|
121
|
-
// Vertical: individual speed + depth + size influence
|
|
122
|
-
snowflake.y += (snowflake.fallSpeed * 2 + snowflake.depth + snowflake.radius * 0.15) / (700 * speedFactor);
|
|
123
|
-
|
|
124
|
-
// Rotation (only visually relevant for crystal sprites)
|
|
125
|
-
snowflake.rotation += snowflake.rotationSpeed / speedFactor;
|
|
126
|
-
|
|
127
|
-
// Recycle out-of-bounds particles
|
|
128
|
-
if (snowflake.x > 1.15 || snowflake.x < -0.15 || snowflake.y > 1.05) {
|
|
129
|
-
const recycled = this.#createSnowflake(false);
|
|
130
|
-
|
|
131
|
-
if (index % 3 > 0) {
|
|
132
|
-
recycled.x = MULBERRY.next();
|
|
133
|
-
recycled.y = -0.05 - MULBERRY.next() * 0.15;
|
|
134
|
-
} else if (wind > 0.2) {
|
|
135
|
-
recycled.x = -0.15;
|
|
136
|
-
recycled.y = MULBERRY.next() * 0.8;
|
|
137
|
-
} else if (wind < -0.2) {
|
|
138
|
-
recycled.x = 1.15;
|
|
139
|
-
recycled.y = MULBERRY.next() * 0.8;
|
|
140
|
-
} else {
|
|
141
|
-
recycled.x = MULBERRY.next();
|
|
142
|
-
recycled.y = -0.05 - MULBERRY.next() * 0.15;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
this.#snowflakes[index] = recycled;
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
#parseColor(fillStyle: string): {r: number; g: number; b: number; a: number} {
|
|
151
|
-
const canvas = document.createElement('canvas');
|
|
152
|
-
canvas.width = 1;
|
|
153
|
-
canvas.height = 1;
|
|
154
|
-
const ctx = canvas.getContext('2d')!;
|
|
155
|
-
ctx.fillStyle = fillStyle;
|
|
156
|
-
ctx.fillRect(0, 0, 1, 1);
|
|
157
|
-
const data = ctx.getImageData(0, 0, 1, 1).data;
|
|
158
|
-
return {r: data[0], g: data[1], b: data[2], a: data[3] / 255};
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
#createSprites(r: number, g: number, b: number): HTMLCanvasElement[] {
|
|
162
|
-
const sprites: HTMLCanvasElement[] = [];
|
|
163
|
-
|
|
164
|
-
const gradientProfiles: [number, number][][] = [
|
|
165
|
-
// 0: Soft glow
|
|
166
|
-
[[0, 0.8], [0.3, 0.4], [0.7, 0.1], [1, 0]],
|
|
167
|
-
// 1: Bright center
|
|
168
|
-
[[0, 1], [0.15, 0.7], [0.5, 0.2], [1, 0]],
|
|
169
|
-
// 2: Compact dot
|
|
170
|
-
[[0, 0.9], [0.25, 0.5], [0.5, 0.1], [1, 0]]
|
|
171
|
-
];
|
|
172
|
-
|
|
173
|
-
for (const profile of gradientProfiles) {
|
|
174
|
-
const canvas = document.createElement('canvas');
|
|
175
|
-
canvas.width = SPRITE_SIZE;
|
|
176
|
-
canvas.height = SPRITE_SIZE;
|
|
177
|
-
const ctx = canvas.getContext('2d')!;
|
|
178
|
-
|
|
179
|
-
const gradient = ctx.createRadialGradient(
|
|
180
|
-
SPRITE_CENTER, SPRITE_CENTER, 0,
|
|
181
|
-
SPRITE_CENTER, SPRITE_CENTER, SPRITE_RADIUS
|
|
182
|
-
);
|
|
183
|
-
|
|
184
|
-
for (const [stop, alpha] of profile) {
|
|
185
|
-
gradient.addColorStop(stop, `rgba(${r}, ${g}, ${b}, ${alpha})`);
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
ctx.fillStyle = gradient;
|
|
189
|
-
ctx.beginPath();
|
|
190
|
-
ctx.arc(SPRITE_CENTER, SPRITE_CENTER, SPRITE_RADIUS, 0, Math.PI * 2);
|
|
191
|
-
ctx.fill();
|
|
192
|
-
|
|
193
|
-
sprites.push(canvas);
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
// 3: Crystal snowflake
|
|
197
|
-
sprites.push(this.#createCrystalSprite(r, g, b));
|
|
198
|
-
|
|
199
|
-
return sprites;
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
#createCrystalSprite(r: number, g: number, b: number): HTMLCanvasElement {
|
|
203
|
-
const canvas = document.createElement('canvas');
|
|
204
|
-
canvas.width = SPRITE_SIZE;
|
|
205
|
-
canvas.height = SPRITE_SIZE;
|
|
206
|
-
const ctx = canvas.getContext('2d')!;
|
|
207
|
-
|
|
208
|
-
// Soft glow base
|
|
209
|
-
const glow = ctx.createRadialGradient(
|
|
210
|
-
SPRITE_CENTER, SPRITE_CENTER, 0,
|
|
211
|
-
SPRITE_CENTER, SPRITE_CENTER, SPRITE_RADIUS
|
|
212
|
-
);
|
|
213
|
-
glow.addColorStop(0, `rgba(${r}, ${g}, ${b}, 0.6)`);
|
|
214
|
-
glow.addColorStop(0.25, `rgba(${r}, ${g}, ${b}, 0.25)`);
|
|
215
|
-
glow.addColorStop(0.6, `rgba(${r}, ${g}, ${b}, 0.05)`);
|
|
216
|
-
glow.addColorStop(1, `rgba(${r}, ${g}, ${b}, 0)`);
|
|
217
|
-
ctx.fillStyle = glow;
|
|
218
|
-
ctx.beginPath();
|
|
219
|
-
ctx.arc(SPRITE_CENTER, SPRITE_CENTER, SPRITE_RADIUS, 0, Math.PI * 2);
|
|
220
|
-
ctx.fill();
|
|
221
|
-
|
|
222
|
-
// Crystal arms
|
|
223
|
-
ctx.strokeStyle = `rgba(${r}, ${g}, ${b}, 0.7)`;
|
|
224
|
-
ctx.lineWidth = 1.5;
|
|
225
|
-
ctx.lineCap = 'round';
|
|
226
|
-
|
|
227
|
-
const armLength = SPRITE_RADIUS * 0.75;
|
|
228
|
-
|
|
229
|
-
for (let arm = 0; arm < 6; arm++) {
|
|
230
|
-
const angle = (arm / 6) * Math.PI * 2 - Math.PI / 2;
|
|
231
|
-
const tipX = SPRITE_CENTER + Math.cos(angle) * armLength;
|
|
232
|
-
const tipY = SPRITE_CENTER + Math.sin(angle) * armLength;
|
|
233
|
-
|
|
234
|
-
// Main arm
|
|
235
|
-
ctx.beginPath();
|
|
236
|
-
ctx.moveTo(SPRITE_CENTER, SPRITE_CENTER);
|
|
237
|
-
ctx.lineTo(tipX, tipY);
|
|
238
|
-
ctx.stroke();
|
|
239
|
-
|
|
240
|
-
// Side branches at 40% and 65% along the arm
|
|
241
|
-
for (const position of [0.4, 0.65]) {
|
|
242
|
-
const branchX = SPRITE_CENTER + Math.cos(angle) * armLength * position;
|
|
243
|
-
const branchY = SPRITE_CENTER + Math.sin(angle) * armLength * position;
|
|
244
|
-
const branchLength = armLength * (0.4 - position * 0.3);
|
|
245
|
-
|
|
246
|
-
for (const side of [-1, 1]) {
|
|
247
|
-
const branchAngle = angle + side * Math.PI / 3;
|
|
248
|
-
ctx.beginPath();
|
|
249
|
-
ctx.moveTo(branchX, branchY);
|
|
250
|
-
ctx.lineTo(
|
|
251
|
-
branchX + Math.cos(branchAngle) * branchLength,
|
|
252
|
-
branchY + Math.sin(branchAngle) * branchLength
|
|
253
|
-
);
|
|
254
|
-
ctx.stroke();
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
// Center dot
|
|
260
|
-
const centerGlow = ctx.createRadialGradient(
|
|
261
|
-
SPRITE_CENTER, SPRITE_CENTER, 0,
|
|
262
|
-
SPRITE_CENTER, SPRITE_CENTER, SPRITE_RADIUS * 0.12
|
|
263
|
-
);
|
|
264
|
-
centerGlow.addColorStop(0, `rgba(${r}, ${g}, ${b}, 0.9)`);
|
|
265
|
-
centerGlow.addColorStop(1, `rgba(${r}, ${g}, ${b}, 0)`);
|
|
266
|
-
ctx.fillStyle = centerGlow;
|
|
267
|
-
ctx.beginPath();
|
|
268
|
-
ctx.arc(SPRITE_CENTER, SPRITE_CENTER, SPRITE_RADIUS * 0.12, 0, Math.PI * 2);
|
|
269
|
-
ctx.fill();
|
|
270
|
-
|
|
271
|
-
return canvas;
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
#createSnowflake(initialSpread: boolean): Snowflake {
|
|
275
|
-
const depth = 0.3 + MULBERRY.next() * 0.7;
|
|
276
|
-
const radius = MULBERRY.next() * this.#size + 2 * this.#scale;
|
|
277
|
-
|
|
278
|
-
let spriteIndex: number;
|
|
279
|
-
if (depth > 0.85 && radius > this.#size * 0.6 && MULBERRY.next() > 0.65) {
|
|
280
|
-
spriteIndex = 3;
|
|
281
|
-
} else if (depth < 0.45) {
|
|
282
|
-
spriteIndex = 2;
|
|
283
|
-
} else {
|
|
284
|
-
spriteIndex = MULBERRY.next() > 0.5 ? 0 : 1;
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
return {
|
|
288
|
-
x: MULBERRY.next(),
|
|
289
|
-
y: initialSpread ? MULBERRY.next() * 2 - 1 : -0.05 - MULBERRY.next() * 0.15,
|
|
290
|
-
depth,
|
|
291
|
-
radius,
|
|
292
|
-
rotation: MULBERRY.next() * Math.PI * 2,
|
|
293
|
-
rotationSpeed: (MULBERRY.next() - 0.5) * 0.03,
|
|
294
|
-
swingAmplitude: 0.3 + MULBERRY.next() * 0.7,
|
|
295
|
-
swingFrequency: 0.5 + MULBERRY.next() * 1.5,
|
|
296
|
-
swingOffset: MULBERRY.next() * Math.PI * 2,
|
|
297
|
-
fallSpeed: 0.5 + MULBERRY.next() * 0.5,
|
|
298
|
-
spriteIndex
|
|
299
|
-
};
|
|
15
|
+
super(canvas, new SnowLayer(config), 60, config.canvasOptions ?? {colorSpace: 'display-p3'});
|
|
300
16
|
}
|
|
301
17
|
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { SparklerLayer } from './layer';
|
|
2
|
+
export { SparklerParticle } from './particle';
|
|
3
|
+
export { SparklerSimulation } from './simulation';
|
|
4
|
+
export type { SparklerParticleConfig } from './particle';
|
|
5
|
+
export type { SparklerSimulationConfig } from './simulation';
|
|
6
|
+
export type { SparklerSpark } from './types';
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { hexToRGB } from '@basmilius/utils';
|
|
2
|
+
import { SimulationLayer } from '../layer';
|
|
3
|
+
import { MULBERRY } from './consts';
|
|
4
|
+
import type { SparklerSimulationConfig } from './simulation';
|
|
5
|
+
import type { SparklerSpark } from './types';
|
|
6
|
+
|
|
7
|
+
const DEFAULT_COLORS = ['#ffcc33', '#ff9900', '#ffffff', '#ffee88'];
|
|
8
|
+
|
|
9
|
+
export class SparklerLayer extends SimulationLayer {
|
|
10
|
+
readonly #scale: number;
|
|
11
|
+
readonly #emitRate: number;
|
|
12
|
+
readonly #maxSparks: number;
|
|
13
|
+
readonly #colorRGBs: [number, number, number][];
|
|
14
|
+
readonly #speedRange: [number, number];
|
|
15
|
+
readonly #friction: number;
|
|
16
|
+
readonly #gravity: number;
|
|
17
|
+
readonly #decayRange: [number, number];
|
|
18
|
+
readonly #trailLength: number;
|
|
19
|
+
readonly #hoverMode: boolean;
|
|
20
|
+
readonly #onMouseMoveBound: (evt: MouseEvent) => void;
|
|
21
|
+
readonly #onMouseLeaveBound: () => void;
|
|
22
|
+
#emitX: number = 0.5;
|
|
23
|
+
#emitY: number = 0.5;
|
|
24
|
+
#mouseOnCanvas: boolean = false;
|
|
25
|
+
#sparks: SparklerSpark[] = [];
|
|
26
|
+
|
|
27
|
+
constructor(config: SparklerSimulationConfig = {}) {
|
|
28
|
+
super();
|
|
29
|
+
|
|
30
|
+
this.#scale = config.scale ?? 1;
|
|
31
|
+
this.#emitRate = config.emitRate ?? 8;
|
|
32
|
+
this.#maxSparks = config.maxSparks ?? 300;
|
|
33
|
+
this.#speedRange = config.speed ?? [2, 8];
|
|
34
|
+
this.#friction = config.friction ?? 0.96;
|
|
35
|
+
this.#gravity = config.gravity ?? 0.8;
|
|
36
|
+
this.#decayRange = config.decay ?? [0.02, 0.05];
|
|
37
|
+
this.#trailLength = config.trailLength ?? 3;
|
|
38
|
+
this.#hoverMode = config.hoverMode ?? false;
|
|
39
|
+
|
|
40
|
+
const colors = config.colors ?? DEFAULT_COLORS;
|
|
41
|
+
this.#colorRGBs = colors.map(c => hexToRGB(c));
|
|
42
|
+
|
|
43
|
+
this.#onMouseMoveBound = this.#onMouseMove.bind(this);
|
|
44
|
+
this.#onMouseLeaveBound = this.#onMouseLeave.bind(this);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
setPosition(x: number, y: number): void {
|
|
48
|
+
this.#emitX = x;
|
|
49
|
+
this.#emitY = y;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
onMount(canvas: HTMLCanvasElement): void {
|
|
53
|
+
if (this.#hoverMode) {
|
|
54
|
+
canvas.addEventListener('mousemove', this.#onMouseMoveBound, {passive: true});
|
|
55
|
+
canvas.addEventListener('mouseleave', this.#onMouseLeaveBound, {passive: true});
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
onUnmount(canvas: HTMLCanvasElement): void {
|
|
60
|
+
canvas.removeEventListener('mousemove', this.#onMouseMoveBound);
|
|
61
|
+
canvas.removeEventListener('mouseleave', this.#onMouseLeaveBound);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
tick(dt: number, width: number, height: number): void {
|
|
65
|
+
if (!this.#hoverMode || this.#mouseOnCanvas) {
|
|
66
|
+
const emitCount = Math.min(this.#emitRate, this.#maxSparks - this.#sparks.length);
|
|
67
|
+
|
|
68
|
+
for (let i = 0; i < emitCount; i++) {
|
|
69
|
+
this.#sparks.push(this.#createSpark(width, height));
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
let alive = 0;
|
|
74
|
+
|
|
75
|
+
for (let i = 0; i < this.#sparks.length; i++) {
|
|
76
|
+
const spark = this.#sparks[i];
|
|
77
|
+
|
|
78
|
+
spark.trail.push({x: spark.x, y: spark.y});
|
|
79
|
+
|
|
80
|
+
if (spark.trail.length > this.#trailLength) {
|
|
81
|
+
spark.trail.shift();
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
spark.vx *= Math.pow(this.#friction, dt);
|
|
85
|
+
spark.vy *= Math.pow(this.#friction, dt);
|
|
86
|
+
spark.vy += this.#gravity * this.#scale * dt;
|
|
87
|
+
|
|
88
|
+
spark.x += spark.vx * dt;
|
|
89
|
+
spark.y += spark.vy * dt;
|
|
90
|
+
|
|
91
|
+
spark.alpha -= spark.decay * dt;
|
|
92
|
+
|
|
93
|
+
if (spark.alpha > 0) {
|
|
94
|
+
this.#sparks[alive++] = spark;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
this.#sparks.length = alive;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
draw(ctx: CanvasRenderingContext2D, width: number, height: number): void {
|
|
102
|
+
ctx.globalCompositeOperation = 'lighter';
|
|
103
|
+
|
|
104
|
+
if (!this.#hoverMode || this.#mouseOnCanvas) {
|
|
105
|
+
const cx = this.#emitX * width;
|
|
106
|
+
const cy = this.#emitY * height;
|
|
107
|
+
const glowRadius = 15 * this.#scale;
|
|
108
|
+
const glow = ctx.createRadialGradient(cx, cy, 0, cx, cy, glowRadius);
|
|
109
|
+
glow.addColorStop(0, 'rgba(255, 220, 100, 0.8)');
|
|
110
|
+
glow.addColorStop(0.3, 'rgba(255, 180, 50, 0.3)');
|
|
111
|
+
glow.addColorStop(1, 'rgba(255, 150, 0, 0)');
|
|
112
|
+
ctx.fillStyle = glow;
|
|
113
|
+
ctx.beginPath();
|
|
114
|
+
ctx.arc(cx, cy, glowRadius, 0, Math.PI * 2);
|
|
115
|
+
ctx.fill();
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
for (const spark of this.#sparks) {
|
|
119
|
+
const [r, g, b] = spark.color;
|
|
120
|
+
|
|
121
|
+
for (let t = 0; t < spark.trail.length; t++) {
|
|
122
|
+
const trailAlpha = spark.alpha * (t / spark.trail.length) * 0.5;
|
|
123
|
+
|
|
124
|
+
if (trailAlpha < 0.01) {
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const trailSize = spark.size * (t / spark.trail.length) * this.#scale;
|
|
129
|
+
|
|
130
|
+
ctx.beginPath();
|
|
131
|
+
ctx.arc(spark.trail[t].x, spark.trail[t].y, trailSize, 0, Math.PI * 2);
|
|
132
|
+
ctx.fillStyle = `rgba(${r}, ${g}, ${b}, ${trailAlpha})`;
|
|
133
|
+
ctx.fill();
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
ctx.beginPath();
|
|
137
|
+
ctx.arc(spark.x, spark.y, spark.size * this.#scale, 0, Math.PI * 2);
|
|
138
|
+
ctx.fillStyle = `rgba(${r}, ${g}, ${b}, ${spark.alpha})`;
|
|
139
|
+
ctx.fill();
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
ctx.globalCompositeOperation = 'source-over';
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
#onMouseMove(evt: MouseEvent): void {
|
|
146
|
+
const target = evt.currentTarget as HTMLCanvasElement;
|
|
147
|
+
const rect = target.getBoundingClientRect();
|
|
148
|
+
this.#emitX = (evt.clientX - rect.left) / rect.width;
|
|
149
|
+
this.#emitY = (evt.clientY - rect.top) / rect.height;
|
|
150
|
+
this.#mouseOnCanvas = true;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
#onMouseLeave(): void {
|
|
154
|
+
this.#mouseOnCanvas = false;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
#createSpark(width: number, height: number): SparklerSpark {
|
|
158
|
+
const angle = MULBERRY.next() * Math.PI * 2;
|
|
159
|
+
const speed = this.#speedRange[0] + MULBERRY.next() * (this.#speedRange[1] - this.#speedRange[0]);
|
|
160
|
+
const colorIndex = Math.floor(MULBERRY.next() * this.#colorRGBs.length);
|
|
161
|
+
|
|
162
|
+
return {
|
|
163
|
+
x: this.#emitX * width,
|
|
164
|
+
y: this.#emitY * height,
|
|
165
|
+
vx: Math.cos(angle) * speed * this.#scale,
|
|
166
|
+
vy: Math.sin(angle) * speed * this.#scale,
|
|
167
|
+
alpha: 0.8 + MULBERRY.next() * 0.2,
|
|
168
|
+
color: this.#colorRGBs[colorIndex],
|
|
169
|
+
size: 1 + MULBERRY.next() * 2,
|
|
170
|
+
decay: this.#decayRange[0] + MULBERRY.next() * (this.#decayRange[1] - this.#decayRange[0]),
|
|
171
|
+
trail: []
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import type { Point } from '../point';
|
|
2
|
+
|
|
3
|
+
export interface SparklerParticleConfig {
|
|
4
|
+
readonly decay?: number;
|
|
5
|
+
readonly friction?: number;
|
|
6
|
+
readonly gravity?: number;
|
|
7
|
+
readonly scale?: number;
|
|
8
|
+
readonly size?: number;
|
|
9
|
+
readonly trailLength?: number;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export class SparklerParticle {
|
|
13
|
+
readonly #color: [number, number, number];
|
|
14
|
+
readonly #decay: number;
|
|
15
|
+
readonly #friction: number;
|
|
16
|
+
readonly #gravity: number;
|
|
17
|
+
readonly #scale: number;
|
|
18
|
+
readonly #size: number;
|
|
19
|
+
readonly #trailLength: number;
|
|
20
|
+
readonly #trail: Point[] = [];
|
|
21
|
+
#x: number;
|
|
22
|
+
#y: number;
|
|
23
|
+
#vx: number;
|
|
24
|
+
#vy: number;
|
|
25
|
+
#alpha: number = 1;
|
|
26
|
+
|
|
27
|
+
get isDead(): boolean {
|
|
28
|
+
return this.#alpha <= 0;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
get position(): Point {
|
|
32
|
+
return {x: this.#x, y: this.#y};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
constructor(position: Point, velocity: Point, color: [number, number, number], config: SparklerParticleConfig = {}) {
|
|
36
|
+
this.#x = position.x;
|
|
37
|
+
this.#y = position.y;
|
|
38
|
+
this.#vx = velocity.x;
|
|
39
|
+
this.#vy = velocity.y;
|
|
40
|
+
this.#color = color;
|
|
41
|
+
this.#decay = config.decay ?? (0.02 + Math.random() * 0.03);
|
|
42
|
+
this.#friction = config.friction ?? 0.96;
|
|
43
|
+
this.#gravity = config.gravity ?? 0.8;
|
|
44
|
+
this.#scale = config.scale ?? 1;
|
|
45
|
+
this.#size = config.size ?? (1 + Math.random() * 2);
|
|
46
|
+
this.#trailLength = config.trailLength ?? 3;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
draw(ctx: CanvasRenderingContext2D): void {
|
|
50
|
+
const [r, g, b] = this.#color;
|
|
51
|
+
|
|
52
|
+
for (let t = 0; t < this.#trail.length; t++) {
|
|
53
|
+
const trailAlpha = this.#alpha * (t / this.#trail.length) * 0.5;
|
|
54
|
+
|
|
55
|
+
if (trailAlpha < 0.01) {
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const trailSize = this.#size * (t / this.#trail.length) * this.#scale;
|
|
60
|
+
|
|
61
|
+
ctx.beginPath();
|
|
62
|
+
ctx.arc(this.#trail[t].x, this.#trail[t].y, trailSize, 0, Math.PI * 2);
|
|
63
|
+
ctx.fillStyle = `rgba(${r}, ${g}, ${b}, ${trailAlpha})`;
|
|
64
|
+
ctx.fill();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
ctx.beginPath();
|
|
68
|
+
ctx.arc(this.#x, this.#y, this.#size * this.#scale, 0, Math.PI * 2);
|
|
69
|
+
ctx.fillStyle = `rgba(${r}, ${g}, ${b}, ${this.#alpha})`;
|
|
70
|
+
ctx.fill();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
tick(dt: number = 1): void {
|
|
74
|
+
this.#trail.push({x: this.#x, y: this.#y});
|
|
75
|
+
|
|
76
|
+
if (this.#trail.length > this.#trailLength) {
|
|
77
|
+
this.#trail.shift();
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
this.#vx *= Math.pow(this.#friction, dt);
|
|
81
|
+
this.#vy *= Math.pow(this.#friction, dt);
|
|
82
|
+
this.#vy += this.#gravity * this.#scale * dt;
|
|
83
|
+
|
|
84
|
+
this.#x += this.#vx * dt;
|
|
85
|
+
this.#y += this.#vy * dt;
|
|
86
|
+
|
|
87
|
+
this.#alpha -= this.#decay * dt;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { SimulationCanvas } from '../simulation-canvas';
|
|
2
|
+
import { SparklerLayer } from './layer';
|
|
3
|
+
|
|
4
|
+
export interface SparklerSimulationConfig {
|
|
5
|
+
readonly emitRate?: number;
|
|
6
|
+
readonly maxSparks?: number;
|
|
7
|
+
readonly colors?: string[];
|
|
8
|
+
readonly speed?: [number, number];
|
|
9
|
+
readonly friction?: number;
|
|
10
|
+
readonly gravity?: number;
|
|
11
|
+
readonly decay?: [number, number];
|
|
12
|
+
readonly trailLength?: number;
|
|
13
|
+
readonly hoverMode?: boolean;
|
|
14
|
+
readonly scale?: number;
|
|
15
|
+
readonly canvasOptions?: CanvasRenderingContext2DSettings;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export class SparklerSimulation extends SimulationCanvas {
|
|
19
|
+
readonly #layer: SparklerLayer;
|
|
20
|
+
|
|
21
|
+
constructor(canvas: HTMLCanvasElement, config: SparklerSimulationConfig = {}) {
|
|
22
|
+
const layer = new SparklerLayer(config);
|
|
23
|
+
super(canvas, layer, 60, config.canvasOptions ?? {colorSpace: 'display-p3'});
|
|
24
|
+
this.#layer = layer;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
setPosition(x: number, y: number): void {
|
|
28
|
+
this.#layer.setPosition(x, y);
|
|
29
|
+
}
|
|
30
|
+
}
|