Animated Gradient with SVG
Animated Gradient with SVG is a React and Tailwind CSS background component from Fancy Components, licensed MIT. Copy it and paste it into your project.
Fancy Components
MIT
backgrounds
What it is
Animated Gradient with SVG is a React and Tailwind CSS background component from Fancy Components, licensed MIT. Copy it and paste it into your project.
How to use it
Open it in the catalogue, press copy, and paste it into your project. The prompt you copy carries the code and the rules that tell your agent to reproduce it without changing anything.
Five free copies a month. No card.
No packages beyond the project baseline
Licence
This component comes from Fancy Components and is redistributed under the MIT licence, which permits commercial use. The copyright notice travels with the code.
Animated Gradient with SVG
"use client"
import React, { useMemo, useRef } from "react"
import { cn } from "@/lib/utils"
import { useDimensions } from "@/hooks/use-debounced-dimensions"
interface AnimatedGradientProps {
colors: string[]
speed?: number
blur?: "light" | "medium" | "heavy"
}
const randomInt = (min: number, max: number) => {
return Math.floor(Math.random() * (max - min + 1)) + min
}
const AnimatedGradient: React.FC<AnimatedGradientProps> = ({
colors,
speed = 5,
blur = "light",
}) => {
const containerRef = useRef<HTMLDivElement>(null)
const dimensions = useDimensions(containerRef)
const circleSize = useMemo(
() => Math.max(dimensions.width, dimensions.height),
[dimensions.width, dimensions.height]
)
const blurClass =
blur === "light"
? "blur-2xl"
: blur === "medium"
? "blur-3xl"
: "blur-[100px]"
return (
<div ref={containerRef} className="absolute inset-0 overflow-hidden">
<div className={cn(`absolute inset-0`, blurClass)}>
{colors.map((color, index) => {
const animationProps = {
animation: `background-gradient ${speed}s infinite ease-in-out`,
animationDuration: `${speed}s`,
top: `${Math.random() * 50}%`,
left: `${Math.random() * 50}%`,
"--tx-1": Math.random() - 0.5,
"--ty-1": Math.random() - 0.5,
"--tx-2": Math.random() - 0.5,
"--ty-2": Math.random() - 0.5,
"--tx-3": Math.random() - 0.5,
"--ty-3": Math.random() - 0.5,
"--tx-4": Math.random() - 0.5,
"--ty-4": Math.random() - 0.5,
} as React.CSSProperties
return (
<svg
key={index}
className={cn("absolute", "animate-background-gradient")}
width={circleSize * randomInt(0.5, 1.5)}
height={circleSize * randomInt(0.5, 1.5)}
viewBox="0 0 100 100"
style={animationProps}
>
<circle cx="50" cy="50" r="50" fill={color} />
</svg>
)
})}
</div>
</div>
)
}
export default AnimatedGradient