A sparkles component from Aceternity UI.
From Aceternity UI
1"use client";
2import React, { useId, useMemo } from "react";
3import { useEffect, useState } from "react";
4import Particles, { initParticlesEngine } from "@tsparticles/react";
5import type { Container, SingleOrMultiple } from "@tsparticles/engine";
6import { loadSlim } from "@tsparticles/slim";
7import { cn } from "@/lib/utils";
8import { motion, useAnimation } from "motion/react";
9
10type ParticlesProps = {
11 id?: string;
12 className?: string;
13 background?: string;
14 particleSize?: number;
15 minSize?: number;
16 maxSize?: number;
17 speed?: number;
18 particleColor?: string;
19 particleDensity?: number;
20};
21export const SparklesCore = (props: ParticlesProps) => {
22 const {
23 id,
24 className,
25 background,
26 minSize,
27 maxSize,
28 speed,
29 particleColor,
30 particleDensity,
31 } = props;
32 const [init, setInit] = useState(false);
33 useEffect(() => {
34 initParticlesEngine(async (engine) => {
35 await loadSlim(engine);
36 }).then(() => {
37 setInit(true);
38 });
39 }, []);
40 const controls = useAnimation();
41
42 const particlesLoaded = async (container?: Container) => {
43 if (container) {
44 controls.start({
45 opacity: 1,
46 transition: {
47 duration: 1,
48 },
49 });
50 }
51 };
52
53 const generatedId = useId();
54 return (
55 <motion.div animate={controls} className={cn("opacity-0", className)}>
56 {init && (
57 <Particles
58 id={id || generatedId}
59 className={cn("h-full w-full")}
60 particlesLoaded={particlesLoaded}
61 options={{
62 background: {
63 color: {
64 value: background || "#0d47a1",
65 },
66 },
67 fullScreen: {
68 enable: false,
69 zIndex: 1,
70 },
71
72 fpsLimit: 120,
73 interactivity: {
74 events: {
75 onClick: {
76 enable: true,
77 mode: "push",
78 },
79 onHover: {
80 enable: false,
81 mode: "repulse",
82 },
83 resize: true as any,
84 },
85 modes: {
86 push: {
87 quantity: 4,
88 },
89 repulse: {
90 distance: 200,
91 duration: 0.4,
92 },
93 },
94 },
95 particles: {
96 bounce: {
97 horizontal: {
98 value: 1,
99 },
100 vertical: {
101 value: 1,
102 },
103 },
104 collisions: {
105 absorb: {
106 speed: 2,
107 },
108 bounce: {
109 horizontal: {
110 value: 1,
111 },
112 vertical: {
113 value: 1,
114 },
115 },
116 enable: false,
117 maxSpeed: 50,
118 mode: "bounce",
119 overlap: {
120 enable: true,
121 retries: 0,
122 },
123 },
124 color: {
125 value: particleColor || "#ffffff",
126 animation: {
127 h: {
128 count: 0,
129 enable: false,
130 speed: 1,
131 decay: 0,
132 delay: 0,
133 sync: true,
134 offset: 0,
135 },
136 s: {
137 count: 0,
138 enable: false,
139 speed: 1,
140 decay: 0,
141 delay: 0,
142 sync: true,
143 offset: 0,
144 },
145 l: {
146 count: 0,
147 enable: false,
148 speed: 1,
149 decay: 0,
150 delay: 0,
151 sync: true,
152 offset: 0,
153 },
154 },
155 },
156 effect: {
157 close: true,
158 fill: true,
159 options: {},
160 type: {} as SingleOrMultiple<string> | undefined,
161 },
162 groups: {},
163 move: {
164 angle: {
165 offset: 0,
166 value: 90,
167 },
168 attract: {
169 distance: 200,
170 enable: false,
171 rotate: {
172 x: 3000,
173 y: 3000,
174 },
175 },
176 center: {
177 x: 50,
178 y: 50,
179 mode: "percent",
180 radius: 0,
181 },
182 decay: 0,
183 distance: {},
184 direction: "none",
185 drift: 0,
186 enable: true,
187 gravity: {
188 acceleration: 9.81,
189 enable: false,
190 inverse: false,
191 maxSpeed: 50,
192 },
193 path: {
194 clamp: true,
195 delay: {
196 value: 0,
197 },
198 enable: false,
199 options: {},
200 },
201 outModes: {
202 default: "out",
203 },
204 random: false,
205 size: false,
206 speed: {
207 min: 0.1,
208 max: 1,
209 },
210 spin: {
211 acceleration: 0,
212 enable: false,
213 },
214 straight: false,
215 trail: {
216 enable: false,
217 length: 10,
218 fill: {},
219 },
220 vibrate: false,
221 warp: false,
222 },
223 number: {
224 density: {
225 enable: true,
226 width: 400,
227 height: 400,
228 },
229 limit: {
230 mode: "delete",
231 value: 0,
232 },
233 value: particleDensity || 120,
234 },
235 opacity: {
236 value: {
237 min: 0.1,
238 max: 1,
239 },
240 animation: {
241 count: 0,
242 enable: true,
243 speed: speed || 4,
244 decay: 0,
245 delay: 0,
246 sync: false,
247 mode: "auto",
248 startValue: "random",
249 destroy: "none",
250 },
251 },
252 reduceDuplicates: false,
253 shadow: {
254 blur: 0,
255 color: {
256 value: "#000",
257 },
258 enable: false,
259 offset: {
260 x: 0,
261 y: 0,
262 },
263 },
264 shape: {
265 close: true,
266 fill: true,
267 options: {},
268 type: "circle",
269 },
270 size: {
271 value: {
272 min: minSize || 1,
273 max: maxSize || 3,
274 },
275 animation: {
276 count: 0,
277 enable: false,
278 speed: 5,
279 decay: 0,
280 delay: 0,
281 sync: false,
282 mode: "auto",
283 startValue: "random",
284 destroy: "none",
285 },
286 },
287 stroke: {
288 width: 0,
289 },
290 zIndex: {
291 value: 0,
292 opacityRate: 1,
293 sizeRate: 1,
294 velocityRate: 1,
295 },
296 destroy: {
297 bounds: {},
298 mode: "none",
299 split: {
300 count: 1,
301 factor: {
302 value: 3,
303 },
304 rate: {
305 value: {
306 min: 4,
307 max: 9,
308 },
309 },
310 sizeOffset: true,
311 },
312 },
313 roll: {
314 darken: {
315 enable: false,
316 value: 0,
317 },
318 enable: false,
319 enlighten: {
320 enable: false,
321 value: 0,
322 },
323 mode: "vertical",
324 speed: 25,
325 },
326 tilt: {
327 value: 0,
328 animation: {
329 enable: false,
330 speed: 0,
331 decay: 0,
332 sync: false,
333 },
334 direction: "clockwise",
335 enable: false,
336 },
337 twinkle: {
338 lines: {
339 enable: false,
340 frequency: 0.05,
341 opacity: 1,
342 },
343 particles: {
344 enable: false,
345 frequency: 0.05,
346 opacity: 1,
347 },
348 },
349 wobble: {
350 distance: 5,
351 enable: false,
352 speed: {
353 angle: 50,
354 move: 10,
355 },
356 },
357 life: {
358 count: 0,
359 delay: {
360 value: 0,
361 sync: false,
362 },
363 duration: {
364 value: 0,
365 sync: false,
366 },
367 },
368 rotate: {
369 value: 0,
370 animation: {
371 enable: false,
372 speed: 0,
373 decay: 0,
374 sync: false,
375 },
376 direction: "clockwise",
377 path: false,
378 },
379 orbit: {
380 animation: {
381 count: 0,
382 enable: false,
383 speed: 1,
384 decay: 0,
385 delay: 0,
386 sync: false,
387 },
388 enable: false,
389 opacity: 1,
390 rotation: {
391 value: 45,
392 },
393 width: 1,
394 },
395 links: {
396 blink: false,
397 color: {
398 value: "#fff",
399 },
400 consent: false,
401 distance: 100,
402 enable: false,
403 frequency: 1,
404 opacity: 1,
405 shadow: {
406 blur: 5,
407 color: {
408 value: "#000",
409 },
410 enable: false,
411 },
412 triangles: {
413 enable: false,
414 frequency: 1,
415 },
416 width: 1,
417 warp: false,
418 },
419 repulse: {
420 value: 0,
421 enabled: false,
422 distance: 1,
423 duration: 1,
424 factor: 1,
425 speed: 1,
426 },
427 },
428 detectRetina: true,
429 }}
430 />
431 )}
432 </motion.div>
433 );
434};
435