businesslawrag / web /src /components /ui /animated-gradient-background.tsx
veltrixcode's picture
Upload 54 files
c49440d verified
Raw History Blame Contribute Delete
5.54 kB
import { motion } from "framer-motion";
import React, { useEffect, useRef } from "react";
export interface AnimatedGradientBackgroundProps {
/**
* Initial size of the radial gradient, defining the starting width.
* @default 125
*/
startingGap?: number;
/**
* Enables or disables the breathing animation effect.
* @default true
*/
Breathing?: boolean;
/**
* Array of colors to use in the radial gradient.
* Each color corresponds to a stop percentage in `gradientStops`.
* @default ["#0A0A0A", "#2979FF", "#FF80AB", "#FF6D00", "#FFD600", "#00E676", "#3D5AFE"]
*/
gradientColors?: string[];
/**
* Array of percentage stops corresponding to each color in `gradientColors`.
* The values should range between 0 and 100.
* @default [35, 50, 60, 70, 80, 90, 100]
*/
gradientStops?: number[];
/**
* Speed of the breathing animation.
* Lower values result in slower animation.
* @default 0.02
*/
animationSpeed?: number;
/**
* Maximum range for the breathing animation in percentage points.
* Determines how much the gradient "breathes" by expanding and contracting.
* @default 5
*/
breathingRange?: number;
/**
* Additional inline styles for the gradient container.
* @default {}
*/
containerStyle?: React.CSSProperties;
/**
* Additional class names for the gradient container.
* @default ""
*/
containerClassName?: string;
/**
* Additional top offset for the gradient container form the top to have a more flexible control over the gradient.
* @default 0
*/
topOffset?: number;
}
export const AnimatedGradientBackground: React.FC<AnimatedGradientBackgroundProps> = ({
startingGap = 110,
Breathing = true,
gradientColors = [
"#08090E",
"#1E1B4B",
"#312E81",
"#2563EB",
"#7C3AED",
"#EC4899",
"#08090E"
],
gradientStops = [25, 45, 60, 75, 88, 95, 100],
animationSpeed = 0.04,
breathingRange = 12,
containerStyle = {},
topOffset = 0,
containerClassName = "",
}) => {
const containerRef = useRef<HTMLDivElement | null>(null);
useEffect(() => {
let animationFrame: number;
let width = startingGap;
let direction = 1;
let angle = 0;
const animateGradient = () => {
if (width >= startingGap + breathingRange) direction = -1;
if (width <= startingGap - breathingRange) direction = 1;
if (Breathing) {
width += direction * animationSpeed;
angle = (angle + 0.15) % 360;
}
const gradientStopsString = gradientStops
.map((stop, index) => `${gradientColors[index]} ${stop}%`)
.join(", ");
const gradient = `radial-gradient(${width}% ${width + topOffset}% at ${50 + Math.sin(angle * 0.02) * 15}% ${25 + Math.cos(angle * 0.02) * 10}%, ${gradientStopsString})`;
if (containerRef.current) {
containerRef.current.style.background = gradient;
}
animationFrame = requestAnimationFrame(animateGradient);
};
animationFrame = requestAnimationFrame(animateGradient);
return () => cancelAnimationFrame(animationFrame);
}, [startingGap, Breathing, gradientColors, gradientStops, animationSpeed, breathingRange, topOffset]);
return (
<div className={`absolute inset-0 pointer-events-none overflow-hidden ${containerClassName}`}>
{/* Multi-frequency breathing radial core */}
<div
ref={containerRef}
style={containerStyle}
className="absolute inset-0 transition-opacity duration-1000"
/>
{/* Floating Animated Color Orbs */}
<motion.div
animate={{
x: [0, 80, -40, 0],
y: [0, -60, 40, 0],
scale: [1, 1.25, 0.9, 1],
opacity: [0.18, 0.32, 0.18],
}}
transition={{
duration: 14,
repeat: Infinity,
ease: "easeInOut",
}}
className="absolute -top-24 left-1/4 w-[38rem] h-[38rem] rounded-full bg-gradient-to-tr from-blue-600 via-indigo-600 to-cyan-500 blur-[120px] mix-blend-screen pointer-events-none"
/>
<motion.div
animate={{
x: [0, -90, 50, 0],
y: [0, 70, -50, 0],
scale: [1.1, 0.85, 1.2, 1.1],
opacity: [0.15, 0.28, 0.15],
}}
transition={{
duration: 18,
repeat: Infinity,
ease: "easeInOut",
delay: 2,
}}
className="absolute top-1/3 -right-24 w-[36rem] h-[36rem] rounded-full bg-gradient-to-br from-violet-600 via-purple-600 to-fuchsia-600 blur-[130px] mix-blend-screen pointer-events-none"
/>
<motion.div
animate={{
x: [0, 60, -70, 0],
y: [0, -40, 60, 0],
scale: [0.9, 1.2, 1, 0.9],
opacity: [0.12, 0.22, 0.12],
}}
transition={{
duration: 22,
repeat: Infinity,
ease: "easeInOut",
delay: 4,
}}
className="absolute -bottom-32 left-1/3 w-[40rem] h-[40rem] rounded-full bg-gradient-to-r from-cyan-600 via-blue-700 to-indigo-800 blur-[140px] mix-blend-screen pointer-events-none"
/>
</div>
);
};
export default AnimatedGradientBackground;