components
snake-beam
Snake Beam
Snake Beam
Required
Tailwind μ€μΉ (Optional)
components/ui/snake-beam.tsx
import * as React from 'react';
export const SnakeBeam = ({ className }: { className?: string }) => {
const requestIdRef = React.useRef(0);
const [gradientValues, setGradientValues] = React.useState({
x1: 250,
x2: 350,
y2: 60,
});
const [isAnimating, setIsAnimating] = React.useState(true); // μ λλ©μ΄μ
μν κ΄λ¦¬
React.useEffect(() => {
const animate = () => {
// μ λλ©μ΄μ
μ΄ λλ¬λμ§ νμΈ
if (
gradientValues.x1 <= -100 &&
gradientValues.x2 <= 0 &&
gradientValues.y2 <= 0
) {
// μ λλ©μ΄μ
μ΄ λλ¬μΌλ©΄ 1μ΄ λμ λκΈ°
if (isAnimating) {
setIsAnimating(false); // μ λλ©μ΄μ
μ€μ§
setTimeout(() => {
setGradientValues({ x1: 250, x2: 350, y2: 60 }); // κ° λ¦¬μ
setIsAnimating(true); // μ λλ©μ΄μ
μ¬μμ
}, 1000); // 1μ΄ λκΈ°
}
return;
}
// μ λλ©μ΄μ
κ³μ μ§ν
setGradientValues((prev) => ({
x1: prev.x1 > -100 ? prev.x1 - (250 + 100) * 0.005 : prev.x1,
x2: prev.x2 > -100 ? prev.x2 - (350 + 100) * 0.005 : prev.x2,
y2: prev.y2 > 0 ? prev.y2 - 60 * 0.005 : prev.y2,
}));
if (isAnimating) {
requestIdRef.current = requestAnimationFrame(animate);
}
};
if (isAnimating) {
requestIdRef.current = requestAnimationFrame(animate);
}
return () => {
cancelAnimationFrame(requestIdRef.current);
};
}, [isAnimating, gradientValues.x1, gradientValues.x2, gradientValues.y2]);
return (
<svg
className={className}
width="236"
height="60"
viewBox="0 0 236 60"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M0.5 0.5 H90 V30 H150 V60 H235.5" // M(x y): μμμ§μ μ΄κΈ°ν, H(x): xμΆ μ΄λ, V(y): yμΆ μ΄λ
stroke="url(#paint0_linear)"
></path>
<defs>
<linearGradient
id="paint0_linear"
gradientUnits="userSpaceOnUse"
x1={`${gradientValues.x1}`}
y1="0"
x2={`${gradientValues.x2}`}
y2={`${gradientValues.y2}`}
>
<stop stopColor="#2EB9DF" stopOpacity="0"></stop>
<stop stopColor="#2EB9DF"></stop>
<stop offset="1" stopColor="#9E00FF" stopOpacity="0"></stop>
</linearGradient>
</defs>
</svg>
);
};
How to customize beam?
<path /> νκ·Έμμ d μμ±μΌλ‘ Beamμ λͺ¨μμ λ³κ²½ν μ μμ΅λλ€.
μ£Όμμ μ¨μλ― κ° μ«μλ₯Ό μμ νμ¬ Beam λͺ¨μμ λ°κΏ μ μμ΅λλ€.
- M(x y): μμμ§μ μ΄κΈ°ν
- H(x): xμΆ μ΄λ
- V(y): yμΆ μ΄λ
κ·Έλ €μ§ Beam λͺ¨μμ λ§μΆ° μμ νκ³ x1, y1, x2, y2λ₯Ό μ‘°μ νλ©΄ λλλ°
νμ¬ μνλ Beamμ κΈΈμ΄λ₯Ό μ½ 100ν½μ λ‘ μ‘κ³ x1μ -100κΉμ§ μ΄λμμΌ μ¬λΌμ§κ² νκ³ μμΌλ,
μ‘°κΈμ© 건λλ €λ³΄λ©΄μ 컀μ€ν νμλ©΄ λ©λλ€.