@once-ui-system/fx 0.0.0-stage → 3.0.0-beta.1

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 (55) hide show
  1. package/LICENSE.md +21 -0
  2. package/README.md +16 -2
  3. package/dist/BlobFx.d.ts +10 -0
  4. package/dist/BlobFx.js +86 -0
  5. package/dist/CelebrationFx.d.ts +21 -0
  6. package/dist/CelebrationFx.js +319 -0
  7. package/dist/CountFx.d.ts +19 -0
  8. package/dist/CountFx.js +226 -0
  9. package/dist/CountdownFx.d.ts +13 -0
  10. package/dist/CountdownFx.js +65 -0
  11. package/dist/Effect.d.ts +52 -0
  12. package/dist/Effect.js +33 -0
  13. package/dist/FlipFx.d.ts +20 -0
  14. package/dist/FlipFx.js +71 -0
  15. package/dist/GlitchFx.d.ts +19 -0
  16. package/dist/GlitchFx.js +41 -0
  17. package/dist/GlitchFx.module.css +80 -0
  18. package/dist/HoloFx.d.ts +34 -0
  19. package/dist/HoloFx.js +99 -0
  20. package/dist/HoloFx.module.css +31 -0
  21. package/dist/LetterFx.d.ts +24 -0
  22. package/dist/LetterFx.js +83 -0
  23. package/dist/MatrixFx.d.ts +32 -0
  24. package/dist/MatrixFx.js +886 -0
  25. package/dist/ParticleFx.d.ts +26 -0
  26. package/dist/ParticleFx.js +154 -0
  27. package/dist/RevealFx.d.ts +27 -0
  28. package/dist/RevealFx.js +65 -0
  29. package/dist/RevealFx.module.css +27 -0
  30. package/dist/ShineFx.d.ts +17 -0
  31. package/dist/ShineFx.js +17 -0
  32. package/dist/ShineFx.module.css +39 -0
  33. package/dist/TiltFx.d.ts +12 -0
  34. package/dist/TiltFx.js +79 -0
  35. package/dist/TiltFx.module.css +13 -0
  36. package/dist/TypeFx.d.ts +41 -0
  37. package/dist/TypeFx.js +102 -0
  38. package/dist/WeatherFx.d.ts +22 -0
  39. package/dist/WeatherFx.js +869 -0
  40. package/dist/index.d.ts +16 -0
  41. package/dist/index.js +19 -0
  42. package/dist/internal/FadingLetters.d.ts +8 -0
  43. package/dist/internal/FadingLetters.js +21 -0
  44. package/dist/internal/FadingLetters.module.css +226 -0
  45. package/dist/internal/cssColor.d.ts +4 -0
  46. package/dist/internal/cssColor.js +70 -0
  47. package/dist/internal/pointer.d.ts +9 -0
  48. package/dist/internal/pointer.js +11 -0
  49. package/dist/internal/reducedMotion.d.ts +7 -0
  50. package/dist/internal/reducedMotion.js +11 -0
  51. package/dist/internal/speed.d.ts +13 -0
  52. package/dist/internal/speed.js +15 -0
  53. package/dist/internal/translate.d.ts +13 -0
  54. package/dist/internal/translate.js +81 -0
  55. package/package.json +79 -4
@@ -0,0 +1,869 @@
1
+ "use client";
2
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
+ import { Flex, useReducedMotion } from "@once-ui-system/core";
4
+ import { useEffect, useRef } from "react";
5
+ import { speedFactor } from "./internal/speed.js";
6
+ const WeatherFx = ({ ref: forwardedRef, type = "rain", speed: speedProp, colors = ["brand-solid-medium"], intensity = 50, angle = 0, duration, trigger = "mount", active = false, reducedMotion = "auto", children, ...rest }) => {
7
+ const speed = speedFactor(speedProp);
8
+ const { shouldAnimate } = useReducedMotion(reducedMotion);
9
+ const containerRef = useRef(null);
10
+ const canvasRef = useRef(null);
11
+ const animationRef = useRef(undefined);
12
+ const particlesRef = useRef([]);
13
+ const timeRef = useRef(0);
14
+ // Manual starts off: the manual-trigger effect below turns emission on for
15
+ // `active`, and only does so (creating the particles) when it is off. Seeding
16
+ // it with `active` meant `<WeatherFx trigger="manual" active />` drew nothing.
17
+ const isEmittingRef = useRef(trigger === "mount");
18
+ const emitStartTimeRef = useRef(Date.now());
19
+ const isHoveredRef = useRef(false);
20
+ const lastLightningTimeRef = useRef(0);
21
+ const lastBoltCountRef = useRef(0);
22
+ useEffect(() => {
23
+ if (forwardedRef) {
24
+ if ("current" in forwardedRef) {
25
+ forwardedRef.current = containerRef.current;
26
+ }
27
+ else if (typeof forwardedRef === "function") {
28
+ forwardedRef(containerRef.current);
29
+ }
30
+ }
31
+ }, [forwardedRef]);
32
+ useEffect(() => {
33
+ const canvas = canvasRef.current;
34
+ const container = containerRef.current;
35
+ if (!canvas || !container)
36
+ return;
37
+ const ctx = canvas.getContext("2d");
38
+ if (!ctx)
39
+ return;
40
+ // Set canvas size
41
+ let canvasWidth = 0;
42
+ let canvasHeight = 0;
43
+ const updateSize = () => {
44
+ const rect = container.getBoundingClientRect();
45
+ canvasWidth = rect.width;
46
+ canvasHeight = rect.height;
47
+ canvas.width = rect.width * 2; // 2x for retina
48
+ canvas.height = rect.height * 2;
49
+ canvas.style.width = `${rect.width}px`;
50
+ canvas.style.height = `${rect.height}px`;
51
+ ctx.scale(2, 2); // Scale for retina
52
+ };
53
+ updateSize();
54
+ window.addEventListener("resize", updateSize);
55
+ // Parse colors - convert token names to CSS variables
56
+ const parsedColors = colors.map((color) => {
57
+ const computedColor = getComputedStyle(container).getPropertyValue(`--${color}`);
58
+ return computedColor || color;
59
+ });
60
+ // Initialize particles based on type
61
+ const initializeRain = () => {
62
+ const particles = [];
63
+ // Calculate spawn area based on angle
64
+ const angleRad = (angle * Math.PI) / 180;
65
+ const horizontalOffset = Math.abs(Math.tan(angleRad) * canvasHeight);
66
+ // Create initial rain drops
67
+ for (let i = 0; i < intensity; i++) {
68
+ // Expand spawn width to account for angle
69
+ const spawnWidth = canvasWidth + horizontalOffset * 2;
70
+ const spawnX = Math.random() * spawnWidth - horizontalOffset;
71
+ particles.push({
72
+ x: spawnX,
73
+ y: Math.random() * canvasHeight - canvasHeight, // Start above canvas
74
+ length: 10 + Math.random() * 20, // Varying lengths
75
+ speed: (2 + Math.random() * 3) * speed, // Varying speeds
76
+ color: parsedColors[Math.floor(Math.random() * parsedColors.length)],
77
+ opacity: 0.3 + Math.random() * 0.5,
78
+ thickness: 1 + Math.random() * 1.5,
79
+ });
80
+ }
81
+ return particles;
82
+ };
83
+ // Initialize snow particles
84
+ const initializeSnow = () => {
85
+ const particles = [];
86
+ // Calculate spawn area based on angle
87
+ const angleRad = (angle * Math.PI) / 180;
88
+ const horizontalOffset = Math.abs(Math.tan(angleRad) * canvasHeight);
89
+ for (let i = 0; i < intensity; i++) {
90
+ const depth = Math.random(); // 0 = far, 1 = near
91
+ const size = 2 + depth * 4; // 2-6px, bigger = closer
92
+ // Expand spawn width to account for angle
93
+ const spawnWidth = canvasWidth + horizontalOffset * 2;
94
+ const spawnX = Math.random() * spawnWidth - horizontalOffset;
95
+ particles.push({
96
+ x: spawnX,
97
+ y: Math.random() * canvasHeight - canvasHeight,
98
+ size,
99
+ speed: (0.3 + depth * 0.7) * speed, // 0.3-1.0x speed, closer = faster
100
+ color: parsedColors[Math.floor(Math.random() * parsedColors.length)],
101
+ opacity: 0.4 + depth * 0.5, // 0.4-0.9, closer = more opaque
102
+ swayAmplitude: 20 + Math.random() * 30, // 20-50px horizontal sway
103
+ swaySpeed: 0.5 + Math.random() * 1, // Varying sway speeds
104
+ swayOffset: Math.random() * Math.PI * 2, // Random starting phase
105
+ depth,
106
+ });
107
+ }
108
+ return particles;
109
+ };
110
+ // Initialize leaves particles
111
+ const initializeLeaves = () => {
112
+ const particles = [];
113
+ // Calculate spawn area based on angle
114
+ const angleRad = (angle * Math.PI) / 180;
115
+ const horizontalOffset = Math.abs(Math.tan(angleRad) * canvasHeight);
116
+ for (let i = 0; i < intensity; i++) {
117
+ const depth = Math.random(); // 0 = far, 1 = near
118
+ const width = 8 + depth * 12; // 8-20px, bigger = closer
119
+ const height = width * (0.6 + Math.random() * 0.4); // Leaf aspect ratio
120
+ // Expand spawn width to account for angle
121
+ const spawnWidth = canvasWidth + horizontalOffset * 2;
122
+ const spawnX = Math.random() * spawnWidth - horizontalOffset;
123
+ // Pick two colors for gradient (either from same color or adjacent colors)
124
+ const colorIndex = Math.floor(Math.random() * parsedColors.length);
125
+ const color1 = parsedColors[colorIndex];
126
+ const color2 = parsedColors[Math.min(colorIndex + 1, parsedColors.length - 1)];
127
+ particles.push({
128
+ x: spawnX,
129
+ y: Math.random() * canvasHeight - canvasHeight,
130
+ width,
131
+ height,
132
+ speed: (0.4 + depth * 0.8) * speed, // 0.4-1.2x speed
133
+ color1,
134
+ color2,
135
+ opacity: 0.6 + depth * 0.3, // 0.6-0.9
136
+ swayAmplitude: 30 + Math.random() * 50, // 30-80px stronger sway
137
+ swaySpeed: 0.3 + Math.random() * 0.7, // Slower sway for leaves
138
+ swayOffset: Math.random() * Math.PI * 2,
139
+ rotation: Math.random() * Math.PI * 2, // Random starting rotation
140
+ rotationSpeed: (Math.random() - 0.5) * 0.08, // -0.04 to 0.04 rad/frame
141
+ rotation3D: Math.random() * Math.PI * 2, // Random 3D flip angle
142
+ rotation3DSpeed: (Math.random() - 0.5) * 0.06, // 3D tumbling speed
143
+ depth,
144
+ });
145
+ }
146
+ return particles;
147
+ };
148
+ // Recursive function to generate chaotic, organic branches
149
+ const generateBranch = (startX, startY, baseSegmentLength, branchLevel, maxLevel, angle, parentThickness) => {
150
+ const segments = [];
151
+ const children = [];
152
+ // Longer branches that decrease more gradually with depth
153
+ const lengthMultiplier = 0.7 ** branchLevel;
154
+ const branchLength = 3 + Math.floor(Math.random() * 4); // 3-6 segments
155
+ let currentX = startX;
156
+ let currentY = startY;
157
+ let currentAngle = angle;
158
+ for (let i = 0; i < branchLength; i++) {
159
+ // Larger segments for longer branches
160
+ const segmentLength = baseSegmentLength * lengthMultiplier * (0.6 + Math.random() * 0.6);
161
+ // Add angular variation for organic chaos
162
+ currentAngle += (Math.random() - 0.5) * 0.8;
163
+ // Move in the current angle direction
164
+ currentX += Math.cos(currentAngle) * segmentLength;
165
+ currentY += Math.sin(currentAngle) * segmentLength;
166
+ segments.push({ x: currentX, y: currentY });
167
+ // Moderate probability of branching
168
+ if (branchLevel < maxLevel && i > 0) {
169
+ // Only primary branches get multiple attempts
170
+ const branchAttempts = branchLevel === 1 ? 1 : 1;
171
+ for (let attempt = 0; attempt < branchAttempts; attempt++) {
172
+ // Lower probability: 35% at level 1, 25% at level 2, etc.
173
+ const branchProbability = 0.35 - branchLevel * 0.1;
174
+ if (Math.random() < branchProbability) {
175
+ // Random angle deviation from current direction (-90 to +90 degrees)
176
+ const angleDeviation = (Math.random() - 0.5) * Math.PI;
177
+ const childAngle = currentAngle + angleDeviation;
178
+ const childBranch = generateBranch(currentX, currentY, baseSegmentLength, branchLevel + 1, maxLevel, childAngle, parentThickness);
179
+ if (childBranch.segments.length > 0) {
180
+ children.push({
181
+ startIndex: i,
182
+ segments: childBranch.segments,
183
+ thickness: parentThickness * 0.6 ** (branchLevel + 1),
184
+ children: childBranch.children,
185
+ });
186
+ }
187
+ }
188
+ }
189
+ }
190
+ }
191
+ return { segments, children };
192
+ };
193
+ // Generate lightning bolt with chaotic branches
194
+ const generateLightningBolt = (startX, startY, endY) => {
195
+ const segments = [];
196
+ const branches = [];
197
+ let currentX = startX;
198
+ let currentY = startY;
199
+ const baseSegmentLength = 12 + Math.random() * 15; // Smaller segments for detail
200
+ const mainThickness = 2.5 + Math.random() * 1.5;
201
+ segments.push({ x: currentX, y: currentY });
202
+ // Main bolt with more erratic path
203
+ let currentAngle = Math.PI / 2; // Start going down (90 degrees)
204
+ while (currentY < endY) {
205
+ // Vary segment length significantly
206
+ const segmentLength = baseSegmentLength * (0.5 + Math.random() * 0.8);
207
+ // Add significant angular variation for jagged appearance
208
+ currentAngle += (Math.random() - 0.5) * 0.6;
209
+ // Ensure we're generally moving downward
210
+ if (currentAngle < Math.PI / 4)
211
+ currentAngle = Math.PI / 4; // Min 45 degrees
212
+ if (currentAngle > (3 * Math.PI) / 4)
213
+ currentAngle = (3 * Math.PI) / 4; // Max 135 degrees
214
+ currentX += Math.cos(currentAngle) * segmentLength;
215
+ currentY += Math.sin(currentAngle) * segmentLength;
216
+ segments.push({ x: currentX, y: currentY });
217
+ // Moderate chance of branching (35%) for balanced appearance
218
+ if (Math.random() < 0.35 && segments.length > 2) {
219
+ // Random angle deviation (-120 to +120 degrees from current)
220
+ const angleDeviation = (Math.random() - 0.5) * ((2 * Math.PI) / 3);
221
+ const branchAngle = currentAngle + angleDeviation;
222
+ const primaryBranch = generateBranch(currentX, currentY, baseSegmentLength, 1, 3, // Max 3 levels to reduce chaos
223
+ branchAngle, mainThickness);
224
+ if (primaryBranch.segments.length > 0) {
225
+ branches.push({
226
+ startIndex: segments.length - 1,
227
+ segments: primaryBranch.segments,
228
+ thickness: mainThickness * 0.6,
229
+ children: primaryBranch.children,
230
+ });
231
+ }
232
+ }
233
+ }
234
+ return {
235
+ x: startX,
236
+ y: startY,
237
+ segments,
238
+ color: parsedColors[Math.floor(Math.random() * parsedColors.length)],
239
+ opacity: 0.8 + Math.random() * 0.2,
240
+ thickness: mainThickness,
241
+ lifetime: 0.4 + Math.random() * 0.2,
242
+ age: 0,
243
+ branches,
244
+ revealDuration: 0.08 + Math.random() * 0.04,
245
+ };
246
+ };
247
+ // Initialize lightning (empty array, lightning spawns dynamically)
248
+ const initializeLightning = () => {
249
+ return [];
250
+ };
251
+ // Initialize particles only for mount trigger (manual/click handled by their respective handlers)
252
+ // Only initialize if particles don't already exist to preserve active particles across re-renders
253
+ if (particlesRef.current.length === 0) {
254
+ const shouldInitialize = trigger === "mount";
255
+ particlesRef.current = shouldInitialize
256
+ ? type === "rain"
257
+ ? initializeRain()
258
+ : type === "snow"
259
+ ? initializeSnow()
260
+ : type === "leaves"
261
+ ? initializeLeaves()
262
+ : type === "lightning"
263
+ ? initializeLightning()
264
+ : []
265
+ : [];
266
+ }
267
+ // Animation loop
268
+ const angleRad = (angle * Math.PI) / 180; // Convert angle to radians
269
+ const dx = Math.sin(angleRad); // Horizontal component
270
+ const dy = Math.cos(angleRad); // Vertical component
271
+ const horizontalOffset = Math.abs(Math.tan(angleRad) * canvasHeight);
272
+ const animate = () => {
273
+ timeRef.current += 0.016; // Approximate frame time
274
+ ctx.clearRect(0, 0, canvasWidth, canvasHeight);
275
+ // Check if we should still be emitting new particles
276
+ const shouldEmit = trigger === "hover" ? isHoveredRef.current : isEmittingRef.current;
277
+ // Check duration limit
278
+ if (duration && isEmittingRef.current) {
279
+ const elapsed = Date.now() - emitStartTimeRef.current;
280
+ if (elapsed > duration) {
281
+ isEmittingRef.current = false;
282
+ }
283
+ }
284
+ if (type === "rain") {
285
+ // Update and draw rain drops
286
+ particlesRef.current.forEach((drop) => {
287
+ // Update position with angle
288
+ drop.y += drop.speed * dy;
289
+ drop.x += drop.speed * dx;
290
+ // Reset if it goes below canvas or off the sides (with expanded bounds)
291
+ const leftBound = -horizontalOffset - 50;
292
+ const rightBound = canvasWidth + horizontalOffset + 50;
293
+ if (drop.y > canvasHeight || drop.x < leftBound || drop.x > rightBound) {
294
+ if (shouldEmit) {
295
+ // Respawn at top
296
+ drop.y = -drop.length;
297
+ const spawnWidth = canvasWidth + horizontalOffset * 2;
298
+ drop.x = Math.random() * spawnWidth - horizontalOffset;
299
+ }
300
+ else {
301
+ // Mark for removal by moving far off screen
302
+ drop.y = canvasHeight + 1000;
303
+ }
304
+ }
305
+ // Draw rain drop as an angled line with gradient
306
+ const x1 = drop.x;
307
+ const y1 = drop.y;
308
+ const x2 = drop.x + dx * drop.length;
309
+ const y2 = drop.y + dy * drop.length;
310
+ // Create gradient from solid to transparent
311
+ const gradient = ctx.createLinearGradient(x1, y1, x2, y2);
312
+ gradient.addColorStop(0, "transparent");
313
+ gradient.addColorStop(1, drop.color);
314
+ ctx.beginPath();
315
+ ctx.strokeStyle = gradient;
316
+ ctx.globalAlpha = drop.opacity;
317
+ ctx.lineWidth = drop.thickness;
318
+ ctx.lineCap = "round";
319
+ ctx.moveTo(x1, y1);
320
+ ctx.lineTo(x2, y2);
321
+ ctx.stroke();
322
+ });
323
+ }
324
+ else if (type === "snow") {
325
+ // Update and draw snowflakes
326
+ particlesRef.current.forEach((flake) => {
327
+ // Calculate sway motion (sine wave)
328
+ const swayX = Math.sin(timeRef.current * flake.swaySpeed + flake.swayOffset) * flake.swayAmplitude;
329
+ // Update position with angle and sway
330
+ flake.y += flake.speed * dy;
331
+ flake.x += flake.speed * dx;
332
+ // Reset if it goes below canvas or off the sides
333
+ const leftBound = -horizontalOffset - 100;
334
+ const rightBound = canvasWidth + horizontalOffset + 100;
335
+ if (flake.y > canvasHeight || flake.x < leftBound || flake.x > rightBound) {
336
+ if (shouldEmit) {
337
+ flake.y = -flake.size;
338
+ const spawnWidth = canvasWidth + horizontalOffset * 2;
339
+ flake.x = Math.random() * spawnWidth - horizontalOffset;
340
+ }
341
+ else {
342
+ flake.y = canvasHeight + 1000;
343
+ }
344
+ }
345
+ // Draw snowflake (no blur filter for performance)
346
+ ctx.globalAlpha = flake.opacity;
347
+ ctx.fillStyle = flake.color;
348
+ // Draw as circle with sway applied
349
+ ctx.beginPath();
350
+ ctx.arc(flake.x + swayX, flake.y, flake.size / 2, 0, Math.PI * 2);
351
+ ctx.fill();
352
+ });
353
+ }
354
+ else if (type === "leaves") {
355
+ // Update and draw leaves
356
+ particlesRef.current.forEach((leaf) => {
357
+ // Calculate sway motion
358
+ const swayX = Math.sin(timeRef.current * leaf.swaySpeed + leaf.swayOffset) * leaf.swayAmplitude;
359
+ // Update rotation (2D spin)
360
+ leaf.rotation += leaf.rotationSpeed;
361
+ // Update 3D rotation (flip/tumble)
362
+ leaf.rotation3D += leaf.rotation3DSpeed;
363
+ // Update position with angle and sway
364
+ leaf.y += leaf.speed * dy;
365
+ leaf.x += leaf.speed * dx;
366
+ // Reset if it goes below canvas or off the sides
367
+ const leftBound = -horizontalOffset - 100;
368
+ const rightBound = canvasWidth + horizontalOffset + 100;
369
+ if (leaf.y > canvasHeight || leaf.x < leftBound || leaf.x > rightBound) {
370
+ if (shouldEmit) {
371
+ leaf.y = -leaf.height;
372
+ const spawnWidth = canvasWidth + horizontalOffset * 2;
373
+ leaf.x = Math.random() * spawnWidth - horizontalOffset;
374
+ leaf.rotation = Math.random() * Math.PI * 2;
375
+ }
376
+ else {
377
+ leaf.y = canvasHeight + 1000;
378
+ }
379
+ }
380
+ // Calculate 3D scale effect (flipping around vertical axis)
381
+ const scaleXRaw = Math.cos(leaf.rotation3D);
382
+ // Clamp scale to minimum 0.15 so leaves stay visible when edge-on
383
+ const scaleX = scaleXRaw > 0 ? Math.max(0.15, scaleXRaw) : Math.min(-0.15, scaleXRaw);
384
+ // Draw leaf with rotation and gradient
385
+ ctx.save();
386
+ ctx.translate(leaf.x + swayX, leaf.y);
387
+ ctx.rotate(leaf.rotation);
388
+ ctx.scale(scaleX, 1); // Apply 3D flip effect with minimum width
389
+ // Fade when edge-on but maintain minimum 50% opacity
390
+ const opacityMultiplier = Math.max(0.5, Math.abs(scaleXRaw * 0.3 + 0.7));
391
+ ctx.globalAlpha = leaf.opacity * opacityMultiplier;
392
+ // Create gradient from color1 to color2
393
+ const gradient = ctx.createLinearGradient(-leaf.width / 2, 0, leaf.width / 2, 0);
394
+ gradient.addColorStop(0, leaf.color1);
395
+ gradient.addColorStop(0.5, leaf.color2);
396
+ gradient.addColorStop(1, leaf.color1);
397
+ // Draw leaf shape (pointed oval using bezier curves)
398
+ ctx.fillStyle = gradient;
399
+ ctx.beginPath();
400
+ const w = leaf.width / 2;
401
+ const h = leaf.height / 2;
402
+ // Start at top tip
403
+ ctx.moveTo(0, -h);
404
+ // Curve to right side (widest point)
405
+ ctx.bezierCurveTo(w * 0.7, -h * 0.5, w * 0.7, h * 0.5, 0, h);
406
+ // Curve to left side and back to top
407
+ ctx.bezierCurveTo(-w * 0.7, h * 0.5, -w * 0.7, -h * 0.5, 0, -h);
408
+ ctx.fill();
409
+ ctx.restore();
410
+ });
411
+ }
412
+ else if (type === "lightning") {
413
+ // Lightning spawning logic
414
+ const currentTime = Date.now();
415
+ const timeSinceLastLightning = (currentTime - lastLightningTimeRef.current) / 1000;
416
+ // Reduced frequency: intensity 1 = ~15s, intensity 10 = ~3s, intensity 50 = ~0.6s, intensity 100 = ~0.3s
417
+ const spawnInterval = Math.max(0.3, 15 / intensity);
418
+ if (shouldEmit && timeSinceLastLightning > spawnInterval) {
419
+ // Calculate number of simultaneous bolts based on intensity
420
+ // After a multi-bolt strike, force a single bolt for variation
421
+ let boltCount = 1;
422
+ if (lastBoltCountRef.current > 1) {
423
+ // Previous strike was multi-bolt, do single bolt this time
424
+ boltCount = 1;
425
+ }
426
+ else {
427
+ // Previous was single, can do multi-bolt based on intensity
428
+ if (intensity > 60) {
429
+ // 60% chance of multi-bolt at high intensity
430
+ boltCount = Math.random() < 0.6 ? 2 + Math.floor(Math.random() * 3) : 1; // 2-4 bolts or 1
431
+ }
432
+ else if (intensity > 30) {
433
+ // 50% chance of multi-bolt at medium intensity
434
+ boltCount = Math.random() < 0.5 ? 2 + Math.floor(Math.random() * 2) : 1; // 2-3 bolts or 1
435
+ }
436
+ else if (intensity > 15) {
437
+ // 30% chance of double bolt at low-medium intensity
438
+ boltCount = Math.random() < 0.3 ? 2 : 1;
439
+ }
440
+ // intensity <= 15: always single bolt
441
+ }
442
+ // Store bolt count for next iteration
443
+ lastBoltCountRef.current = boltCount;
444
+ // Spawn multiple bolts with slight time offset for realism
445
+ for (let i = 0; i < boltCount; i++) {
446
+ const startX = Math.random() * canvasWidth;
447
+ const bolt = generateLightningBolt(startX, 0, canvasHeight);
448
+ // Add slight age offset for staggered appearance
449
+ bolt.age = -i * 0.025; // 25ms offset between bolts
450
+ particlesRef.current.push(bolt);
451
+ }
452
+ lastLightningTimeRef.current = currentTime;
453
+ }
454
+ // Update and draw lightning bolts
455
+ particlesRef.current.forEach((bolt, index) => {
456
+ bolt.age += 0.016; // Increment age
457
+ // Skip if not yet started (negative age for staggered bolts)
458
+ if (bolt.age < 0)
459
+ return;
460
+ // Remove expired bolts
461
+ if (bolt.age > bolt.lifetime) {
462
+ particlesRef.current.splice(index, 1);
463
+ return;
464
+ }
465
+ // Calculate reveal progress (how much of the bolt to draw)
466
+ const revealProgress = Math.min(1, bolt.age / bolt.revealDuration);
467
+ // Calculate opacity fade (flash effect)
468
+ const lifeProgress = bolt.age / bolt.lifetime;
469
+ let currentOpacity = bolt.opacity;
470
+ // During reveal: full brightness
471
+ // After reveal: hold briefly, then fade out
472
+ if (lifeProgress < bolt.revealDuration / bolt.lifetime) {
473
+ currentOpacity *= revealProgress; // Fade in during reveal
474
+ }
475
+ else if (lifeProgress < 0.3) {
476
+ currentOpacity *= 1; // Hold at full brightness
477
+ }
478
+ else {
479
+ // Fade out in the remaining time
480
+ const fadeProgress = (lifeProgress - 0.3) / 0.7;
481
+ currentOpacity *= 1 - fadeProgress;
482
+ }
483
+ // Calculate how many segments to draw based on reveal progress
484
+ const totalSegments = bolt.segments.length;
485
+ const segmentsToDraw = Math.ceil(totalSegments * revealProgress);
486
+ // Draw main bolt with reveal effect
487
+ ctx.globalAlpha = currentOpacity;
488
+ ctx.strokeStyle = bolt.color;
489
+ ctx.lineWidth = bolt.thickness;
490
+ ctx.lineCap = "round";
491
+ ctx.lineJoin = "round";
492
+ // Draw glow effect
493
+ ctx.shadowBlur = 15;
494
+ ctx.shadowColor = bolt.color;
495
+ ctx.beginPath();
496
+ for (let i = 0; i < segmentsToDraw; i++) {
497
+ const segment = bolt.segments[i];
498
+ if (i === 0) {
499
+ ctx.moveTo(segment.x, segment.y);
500
+ }
501
+ else {
502
+ ctx.lineTo(segment.x, segment.y);
503
+ }
504
+ }
505
+ ctx.stroke();
506
+ // Recursive function to draw hierarchical branches
507
+ const drawBranch = (branch, parentSegments, parentSegmentsToDraw) => {
508
+ // Only draw if parent bolt has reached this branch point
509
+ if (branch.startIndex < parentSegmentsToDraw) {
510
+ const startSegment = parentSegments[branch.startIndex];
511
+ // Calculate how much of this branch to reveal
512
+ const segmentsPastBranch = parentSegmentsToDraw - branch.startIndex;
513
+ const branchRevealProgress = Math.min(1, segmentsPastBranch / 3);
514
+ const branchSegmentsToDraw = Math.ceil(branch.segments.length * branchRevealProgress);
515
+ // Draw this branch
516
+ ctx.beginPath();
517
+ ctx.moveTo(startSegment.x, startSegment.y);
518
+ for (let i = 0; i < branchSegmentsToDraw; i++) {
519
+ const segment = branch.segments[i];
520
+ ctx.lineTo(segment.x, segment.y);
521
+ }
522
+ ctx.lineWidth = branch.thickness;
523
+ ctx.stroke();
524
+ // Recursively draw child branches
525
+ if (branch.children && branch.children.length > 0) {
526
+ branch.children.forEach((childBranch) => {
527
+ drawBranch(childBranch, branch.segments, branchSegmentsToDraw);
528
+ });
529
+ }
530
+ }
531
+ };
532
+ // Draw all primary branches and their hierarchies
533
+ bolt.branches.forEach((branch) => {
534
+ drawBranch(branch, bolt.segments, segmentsToDraw);
535
+ });
536
+ // Reset shadow
537
+ ctx.shadowBlur = 0;
538
+ });
539
+ }
540
+ ctx.globalAlpha = 1;
541
+ animationRef.current = requestAnimationFrame(animate);
542
+ };
543
+ if (!shouldAnimate) {
544
+ // When reduced motion is active, don't start the animation loop at all
545
+ return () => {
546
+ window.removeEventListener("resize", updateSize);
547
+ };
548
+ }
549
+ animate();
550
+ return () => {
551
+ window.removeEventListener("resize", updateSize);
552
+ if (animationRef.current) {
553
+ cancelAnimationFrame(animationRef.current);
554
+ }
555
+ };
556
+ }, [type, colors, speed, intensity, angle, duration, trigger, shouldAnimate]);
557
+ // Respond to external control in manual mode
558
+ useEffect(() => {
559
+ if (trigger !== "manual")
560
+ return;
561
+ if (active && !isEmittingRef.current) {
562
+ // Initialize particles if starting emission
563
+ if (particlesRef.current.length === 0) {
564
+ const canvas = canvasRef.current;
565
+ const container = containerRef.current;
566
+ if (canvas && container) {
567
+ const canvasWidth = canvas.width / 2;
568
+ const canvasHeight = canvas.height / 2;
569
+ const parsedColors = colors.map((color) => {
570
+ const computedColor = getComputedStyle(container).getPropertyValue(`--${color}`);
571
+ return computedColor || color;
572
+ });
573
+ if (type === "rain") {
574
+ const particles = [];
575
+ const angleRad = (angle * Math.PI) / 180;
576
+ const horizontalOffset = Math.abs(Math.tan(angleRad) * canvasHeight);
577
+ for (let i = 0; i < intensity; i++) {
578
+ const spawnWidth = canvasWidth + horizontalOffset * 2;
579
+ const spawnX = Math.random() * spawnWidth - horizontalOffset;
580
+ particles.push({
581
+ x: spawnX,
582
+ y: Math.random() * canvasHeight - canvasHeight,
583
+ length: 10 + Math.random() * 20,
584
+ speed: (2 + Math.random() * 3) * speed,
585
+ color: parsedColors[Math.floor(Math.random() * parsedColors.length)],
586
+ opacity: 0.3 + Math.random() * 0.5,
587
+ thickness: 1 + Math.random() * 1.5,
588
+ });
589
+ }
590
+ particlesRef.current = particles;
591
+ }
592
+ else if (type === "snow") {
593
+ const particles = [];
594
+ const angleRad = (angle * Math.PI) / 180;
595
+ const horizontalOffset = Math.abs(Math.tan(angleRad) * canvasHeight);
596
+ for (let i = 0; i < intensity; i++) {
597
+ const depth = Math.random();
598
+ const size = 2 + depth * 4;
599
+ const spawnWidth = canvasWidth + horizontalOffset * 2;
600
+ const spawnX = Math.random() * spawnWidth - horizontalOffset;
601
+ particles.push({
602
+ x: spawnX,
603
+ y: Math.random() * canvasHeight - canvasHeight,
604
+ size,
605
+ speed: (0.3 + depth * 0.7) * speed,
606
+ color: parsedColors[Math.floor(Math.random() * parsedColors.length)],
607
+ opacity: 0.4 + depth * 0.5,
608
+ swayAmplitude: 20 + Math.random() * 30,
609
+ swaySpeed: 0.5 + Math.random() * 1,
610
+ swayOffset: Math.random() * Math.PI * 2,
611
+ depth,
612
+ });
613
+ }
614
+ particlesRef.current = particles;
615
+ }
616
+ else if (type === "leaves") {
617
+ const particles = [];
618
+ const angleRad = (angle * Math.PI) / 180;
619
+ const horizontalOffset = Math.abs(Math.tan(angleRad) * canvasHeight);
620
+ for (let i = 0; i < intensity; i++) {
621
+ const depth = Math.random();
622
+ const width = 8 + depth * 12;
623
+ const height = width * (0.6 + Math.random() * 0.4);
624
+ const spawnWidth = canvasWidth + horizontalOffset * 2;
625
+ const spawnX = Math.random() * spawnWidth - horizontalOffset;
626
+ const colorIndex = Math.floor(Math.random() * parsedColors.length);
627
+ const color1 = parsedColors[colorIndex];
628
+ const color2 = parsedColors[Math.min(colorIndex + 1, parsedColors.length - 1)];
629
+ particles.push({
630
+ x: spawnX,
631
+ y: Math.random() * canvasHeight - canvasHeight,
632
+ width,
633
+ height,
634
+ speed: (0.4 + depth * 0.8) * speed,
635
+ color1,
636
+ color2,
637
+ opacity: 0.6 + depth * 0.3,
638
+ swayAmplitude: 30 + Math.random() * 50,
639
+ swaySpeed: 0.3 + Math.random() * 0.7,
640
+ swayOffset: Math.random() * Math.PI * 2,
641
+ rotation: Math.random() * Math.PI * 2,
642
+ rotationSpeed: (Math.random() - 0.5) * 0.08,
643
+ rotation3D: Math.random() * Math.PI * 2,
644
+ rotation3DSpeed: (Math.random() - 0.5) * 0.06,
645
+ depth,
646
+ });
647
+ }
648
+ particlesRef.current = particles;
649
+ }
650
+ }
651
+ }
652
+ isEmittingRef.current = true;
653
+ emitStartTimeRef.current = Date.now();
654
+ }
655
+ else if (!active && isEmittingRef.current) {
656
+ isEmittingRef.current = false;
657
+ }
658
+ }, [active, trigger]);
659
+ const handleMouseEnter = () => {
660
+ if (trigger === "hover" && !isHoveredRef.current) {
661
+ isHoveredRef.current = true;
662
+ isEmittingRef.current = true;
663
+ emitStartTimeRef.current = Date.now();
664
+ // Initialize particles if none exist yet
665
+ if (particlesRef.current.length === 0) {
666
+ const canvas = canvasRef.current;
667
+ const container = containerRef.current;
668
+ if (canvas && container) {
669
+ const cw = canvas.width / 2;
670
+ const ch = canvas.height / 2;
671
+ const parsedColors = colors.map((color) => {
672
+ const computedColor = getComputedStyle(container).getPropertyValue(`--${color}`);
673
+ return computedColor || color;
674
+ });
675
+ const angleRad = (angle * Math.PI) / 180;
676
+ const horizontalOffset = Math.abs(Math.tan(angleRad) * ch);
677
+ if (type === "rain") {
678
+ const particles = [];
679
+ for (let i = 0; i < intensity; i++) {
680
+ const spawnWidth = cw + horizontalOffset * 2;
681
+ const spawnX = Math.random() * spawnWidth - horizontalOffset;
682
+ particles.push({
683
+ x: spawnX,
684
+ y: Math.random() * ch - ch,
685
+ length: 10 + Math.random() * 20,
686
+ speed: (2 + Math.random() * 3) * speed,
687
+ color: parsedColors[Math.floor(Math.random() * parsedColors.length)],
688
+ opacity: 0.3 + Math.random() * 0.5,
689
+ thickness: 1 + Math.random() * 1.5,
690
+ });
691
+ }
692
+ particlesRef.current = particles;
693
+ }
694
+ else if (type === "snow") {
695
+ const particles = [];
696
+ for (let i = 0; i < intensity; i++) {
697
+ const depth = Math.random();
698
+ const size = 2 + depth * 4;
699
+ const spawnWidth = cw + horizontalOffset * 2;
700
+ const spawnX = Math.random() * spawnWidth - horizontalOffset;
701
+ particles.push({
702
+ x: spawnX,
703
+ y: Math.random() * ch - ch,
704
+ size,
705
+ speed: (0.3 + depth * 0.7) * speed,
706
+ color: parsedColors[Math.floor(Math.random() * parsedColors.length)],
707
+ opacity: 0.4 + depth * 0.5,
708
+ swayAmplitude: 20 + Math.random() * 30,
709
+ swaySpeed: 0.5 + Math.random() * 1,
710
+ swayOffset: Math.random() * Math.PI * 2,
711
+ depth,
712
+ });
713
+ }
714
+ particlesRef.current = particles;
715
+ }
716
+ else if (type === "leaves") {
717
+ const particles = [];
718
+ for (let i = 0; i < intensity; i++) {
719
+ const depth = Math.random();
720
+ const width = 8 + depth * 12;
721
+ const height = width * (0.6 + Math.random() * 0.4);
722
+ const spawnWidth = cw + horizontalOffset * 2;
723
+ const spawnX = Math.random() * spawnWidth - horizontalOffset;
724
+ const colorIndex = Math.floor(Math.random() * parsedColors.length);
725
+ const color1 = parsedColors[colorIndex];
726
+ const color2 = parsedColors[Math.min(colorIndex + 1, parsedColors.length - 1)];
727
+ particles.push({
728
+ x: spawnX,
729
+ y: Math.random() * ch - ch,
730
+ width,
731
+ height,
732
+ speed: (0.4 + depth * 0.8) * speed,
733
+ color1,
734
+ color2,
735
+ opacity: 0.6 + depth * 0.3,
736
+ swayAmplitude: 30 + Math.random() * 50,
737
+ swaySpeed: 0.3 + Math.random() * 0.7,
738
+ swayOffset: Math.random() * Math.PI * 2,
739
+ rotation: Math.random() * Math.PI * 2,
740
+ rotationSpeed: (Math.random() - 0.5) * 0.08,
741
+ rotation3D: Math.random() * Math.PI * 2,
742
+ rotation3DSpeed: (Math.random() - 0.5) * 0.06,
743
+ depth,
744
+ });
745
+ }
746
+ particlesRef.current = particles;
747
+ }
748
+ // Lightning doesn't need pre-initialization (spawns dynamically)
749
+ }
750
+ }
751
+ }
752
+ };
753
+ const handleMouseLeave = () => {
754
+ if (trigger === "hover" && isHoveredRef.current) {
755
+ isHoveredRef.current = false;
756
+ }
757
+ };
758
+ const handleClick = () => {
759
+ if (trigger !== "click")
760
+ return;
761
+ if (!isEmittingRef.current) {
762
+ // Initialize particles if starting emission
763
+ if (particlesRef.current.length === 0) {
764
+ const canvas = canvasRef.current;
765
+ const container = containerRef.current;
766
+ if (canvas && container) {
767
+ const canvasWidth = canvas.width / 2;
768
+ const canvasHeight = canvas.height / 2;
769
+ const parsedColors = colors.map((color) => {
770
+ const computedColor = getComputedStyle(container).getPropertyValue(`--${color}`);
771
+ return computedColor || color;
772
+ });
773
+ if (type === "rain") {
774
+ const particles = [];
775
+ const angleRad = (angle * Math.PI) / 180;
776
+ const horizontalOffset = Math.abs(Math.tan(angleRad) * canvasHeight);
777
+ for (let i = 0; i < intensity; i++) {
778
+ const spawnWidth = canvasWidth + horizontalOffset * 2;
779
+ const spawnX = Math.random() * spawnWidth - horizontalOffset;
780
+ particles.push({
781
+ x: spawnX,
782
+ y: Math.random() * canvasHeight - canvasHeight,
783
+ length: 10 + Math.random() * 20,
784
+ speed: (2 + Math.random() * 3) * speed,
785
+ color: parsedColors[Math.floor(Math.random() * parsedColors.length)],
786
+ opacity: 0.3 + Math.random() * 0.5,
787
+ thickness: 1 + Math.random() * 1.5,
788
+ });
789
+ }
790
+ particlesRef.current = particles;
791
+ }
792
+ else if (type === "snow") {
793
+ const particles = [];
794
+ const angleRad = (angle * Math.PI) / 180;
795
+ const horizontalOffset = Math.abs(Math.tan(angleRad) * canvasHeight);
796
+ for (let i = 0; i < intensity; i++) {
797
+ const depth = Math.random();
798
+ const size = 2 + depth * 4;
799
+ const spawnWidth = canvasWidth + horizontalOffset * 2;
800
+ const spawnX = Math.random() * spawnWidth - horizontalOffset;
801
+ particles.push({
802
+ x: spawnX,
803
+ y: Math.random() * canvasHeight - canvasHeight,
804
+ size,
805
+ speed: (0.3 + depth * 0.7) * speed,
806
+ color: parsedColors[Math.floor(Math.random() * parsedColors.length)],
807
+ opacity: 0.4 + depth * 0.5,
808
+ swayAmplitude: 20 + Math.random() * 30,
809
+ swaySpeed: 0.5 + Math.random() * 1,
810
+ swayOffset: Math.random() * Math.PI * 2,
811
+ depth,
812
+ });
813
+ }
814
+ particlesRef.current = particles;
815
+ }
816
+ else if (type === "leaves") {
817
+ const particles = [];
818
+ const angleRad = (angle * Math.PI) / 180;
819
+ const horizontalOffset = Math.abs(Math.tan(angleRad) * canvasHeight);
820
+ for (let i = 0; i < intensity; i++) {
821
+ const depth = Math.random();
822
+ const width = 8 + depth * 12;
823
+ const height = width * (0.6 + Math.random() * 0.4);
824
+ const spawnWidth = canvasWidth + horizontalOffset * 2;
825
+ const spawnX = Math.random() * spawnWidth - horizontalOffset;
826
+ const colorIndex = Math.floor(Math.random() * parsedColors.length);
827
+ const color1 = parsedColors[colorIndex];
828
+ const color2 = parsedColors[Math.min(colorIndex + 1, parsedColors.length - 1)];
829
+ particles.push({
830
+ x: spawnX,
831
+ y: Math.random() * canvasHeight - canvasHeight,
832
+ width,
833
+ height,
834
+ speed: (0.4 + depth * 0.8) * speed,
835
+ color1,
836
+ color2,
837
+ opacity: 0.6 + depth * 0.3,
838
+ swayAmplitude: 30 + Math.random() * 50,
839
+ swaySpeed: 0.3 + Math.random() * 0.7,
840
+ swayOffset: Math.random() * Math.PI * 2,
841
+ rotation: Math.random() * Math.PI * 2,
842
+ rotationSpeed: (Math.random() - 0.5) * 0.08,
843
+ rotation3D: Math.random() * Math.PI * 2,
844
+ rotation3DSpeed: (Math.random() - 0.5) * 0.06,
845
+ depth,
846
+ });
847
+ }
848
+ particlesRef.current = particles;
849
+ }
850
+ }
851
+ }
852
+ isEmittingRef.current = true;
853
+ emitStartTimeRef.current = Date.now();
854
+ }
855
+ else {
856
+ isEmittingRef.current = false;
857
+ }
858
+ };
859
+ return (_jsxs(Flex, { ref: containerRef, fill: true, overflow: "hidden", onMouseEnter: handleMouseEnter, onMouseLeave: handleMouseLeave, onClick: handleClick, ...rest, children: [_jsx("canvas", { ref: canvasRef, "aria-hidden": "true", style: {
860
+ position: "absolute",
861
+ top: 0,
862
+ left: 0,
863
+ width: "100%",
864
+ height: "100%",
865
+ pointerEvents: "none",
866
+ } }), children] }));
867
+ };
868
+ WeatherFx.displayName = "WeatherFx";
869
+ export { WeatherFx };