Skip to content

UI Library for Design Engineers. Animated components and effects you can copy and paste into your apps. Free. Open Source.

fix: fixing the flickering particles issue on resize (#471)

Arghya Dasb9cf05195e99
diff --git a/registry/default/example/particles-demo.tsx b/registry/default/example/particles-demo.tsx
index c65669ac..a35192de 100644
--- a/registry/default/example/particles-demo.tsx
+++ b/registry/default/example/particles-demo.tsx
@@ -1,7 +1,7 @@
"use client";
-import { useEffect, useState } from "react";
import { useTheme } from "next-themes";
+import { useEffect, useState } from "react";
import Particles from "@/registry/default/magicui/particles";
@@ -15,11 +15,12 @@ export default function ParticlesDemo() {
return (
<div className="relative flex h-[500px] w-full flex-col items-center justify-center overflow-hidden rounded-lg border bg-background md:shadow-xl">
- <span className="pointer-events-none whitespace-pre-wrap bg-gradient-to-b from-black to-gray-300/80 bg-clip-text text-center text-8xl font-semibold leading-none text-transparent dark:from-white dark:to-slate-900/10">
+ <span className="pointer-events-none z-10 whitespace-pre-wrap text-center text-8xl font-semibold leading-none">
Particles
</span>
+ q
<Particles
- className="absolute inset-0"
+ className="absolute inset-0 z-0"
quantity={100}
ease={80}
color={color}
diff --git a/registry/default/magicui/particles.tsx b/registry/default/magicui/particles.tsx
index c476fd4a..688c7c07 100644
--- a/registry/default/magicui/particles.tsx
+++ b/registry/default/magicui/particles.tsx
@@ -40,6 +40,7 @@ interface ParticlesProps {
vx?: number;
vy?: number;
}
+
function hexToRgb(hex: string): number[] {
hex = hex.replace("#", "");
@@ -57,6 +58,19 @@ function hexToRgb(hex: string): number[] {
return [red, green, blue];
}
+type Circle = {
+ x: number;
+ y: number;
+ translateX: number;
+ translateY: number;
+ size: number;
+ alpha: number;
+ targetAlpha: number;
+ dx: number;
+ dy: number;
+ magnetism: number;
+};
+
const Particles: React.FC<ParticlesProps> = ({
className = "",
quantity = 100,
@@ -77,6 +91,7 @@ const Particles: React.FC<ParticlesProps> = ({
const canvasSize = useRef<{ w: number; h: number }>({ w: 0, h: 0 });
const dpr = typeof window !== "undefined" ? window.devicePixelRatio : 1;
const rafID = useRef<number | null>(null);
+ const resizeTimeout = useRef<NodeJS.Timeout>();
useEffect(() => {
if (canvasRef.current) {
@@ -84,13 +99,26 @@ const Particles: React.FC<ParticlesProps> = ({
}
initCanvas();
animate();
- window.addEventListener("resize", initCanvas);
+
+ const handleResize = () => {
+ if (resizeTimeout.current) {
+ clearTimeout(resizeTimeout.current);
+ }
+ resizeTimeout.current = setTimeout(() => {
+ initCanvas();
+ }, 200);
+ };
+
+ window.addEventListener("resize", handleResize);
return () => {
if (rafID.current != null) {
window.cancelAnimationFrame(rafID.current);
}
- window.removeEventListener("resize", initCanvas);
+ if (resizeTimeout.current) {
+ clearTimeout(resizeTimeout.current);
+ }
+ window.removeEventListener("resize", handleResize);
};
}, [color]);
@@ -121,29 +149,23 @@ const Particles: React.FC<ParticlesProps> = ({
}
};
- type Circle = {
- x: number;
- y: number;
- translateX: number;
- translateY: number;
- size: number;
- alpha: number;
- targetAlpha: number;
- dx: number;
- dy: number;
- magnetism: number;
- };
-
const resizeCanvas = () => {
if (canvasContainerRef.current && canvasRef.current && context.current) {
- circles.current.length = 0;
canvasSize.current.w = canvasContainerRef.current.offsetWidth;
canvasSize.current.h = canvasContainerRef.current.offsetHeight;
+
canvasRef.current.width = canvasSize.current.w * dpr;
canvasRef.current.height = canvasSize.current.h * dpr;
canvasRef.current.style.width = `${canvasSize.current.w}px`;
canvasRef.current.style.height = `${canvasSize.current.h}px`;
context.current.scale(dpr, dpr);
+
+ // Clear existing particles and create new ones with exact quantity
+ circles.current = [];
+ for (let i = 0; i < quantity; i++) {
+ const circle = circleParams();
+ drawCircle(circle);
+ }
}
};
@@ -267,7 +289,6 @@ const Particles: React.FC<ParticlesProps> = ({
// create a new circle
const newCircle = circleParams();
drawCircle(newCircle);
- // update the circle position
}
});
rafID.current = window.requestAnimationFrame(animate);