A background-beams-with-collision component from Aceternity UI.
From Aceternity UI
1"use client";
2import { cn } from "@/lib/utils";
3import { motion, AnimatePresence } from "motion/react";
4import React, { useRef, useState, useEffect } from "react";
5
6export const BackgroundBeamsWithCollision = ({
7 children,
8 className,
9}: {
10 children: React.ReactNode;
11 className?: string;
12}) => {
13 const containerRef = useRef<HTMLDivElement>(null);
14 const parentRef = useRef<HTMLDivElement>(null);
15
16 const beams = [
17 {
18 initialX: 10,
19 translateX: 10,
20 duration: 7,
21 repeatDelay: 3,
22 delay: 2,
23 },
24 {
25 initialX: 600,
26 translateX: 600,
27 duration: 3,
28 repeatDelay: 3,
29 delay: 4,
30 },
31 {
32 initialX: 100,
33 translateX: 100,
34 duration: 7,
35 repeatDelay: 7,
36 className: "h-6",
37 },
38 {
39 initialX: 400,
40 translateX: 400,
41 duration: 5,
42 repeatDelay: 14,
43 delay: 4,
44 },
45 {
46 initialX: 800,
47 translateX: 800,
48 duration: 11,
49 repeatDelay: 2,
50 className: "h-20",
51 },
52 {
53 initialX: 1000,
54 translateX: 1000,
55 duration: 4,
56 repeatDelay: 2,
57 className: "h-12",
58 },
59 {
60 initialX: 1200,
61 translateX: 1200,
62 duration: 6,
63 repeatDelay: 4,
64 delay: 2,
65 className: "h-6",
66 },
67 ];
68
69 return (
70 <div
71 ref={parentRef}
72 className={cn(
73 "h-96 md:h-[40rem] bg-gradient-to-b from-white to-neutral-100 dark:from-neutral-950 dark:to-neutral-800 relative flex items-center w-full justify-center overflow-hidden",
74 // h-screen if you want bigger
75 className
76 )}
77 >
78 {beams.map((beam) => (
79 <CollisionMechanism
80 key={beam.initialX + "beam-idx"}
81 beamOptions={beam}
82 containerRef={containerRef}
83 parentRef={parentRef}
84 />
85 ))}
86
87 {children}
88 <div
89 ref={containerRef}
90 className="absolute bottom-0 bg-neutral-100 w-full inset-x-0 pointer-events-none"
91 style={{
92 boxShadow:
93 "0 0 24px rgba(34, 42, 53, 0.06), 0 1px 1px rgba(0, 0, 0, 0.05), 0 0 0 1px rgba(34, 42, 53, 0.04), 0 0 4px rgba(34, 42, 53, 0.08), 0 16px 68px rgba(47, 48, 55, 0.05), 0 1px 0 rgba(255, 255, 255, 0.1) inset",
94 }}
95 ></div>
96 </div>
97 );
98};
99
100const CollisionMechanism = React.forwardRef<
101 HTMLDivElement,
102 {
103 containerRef: React.RefObject<HTMLDivElement>;
104 parentRef: React.RefObject<HTMLDivElement>;
105 beamOptions?: {
106 initialX?: number;
107 translateX?: number;
108 initialY?: number;
109 translateY?: number;
110 rotate?: number;
111 className?: string;
112 duration?: number;
113 delay?: number;
114 repeatDelay?: number;
115 };
116 }
117>(({ parentRef, containerRef, beamOptions = {} }, ref) => {
118 const beamRef = useRef<HTMLDivElement>(null);
119 const [collision, setCollision] = useState<{
120 detected: boolean;
121 coordinates: { x: number; y: number } | null;
122 }>({
123 detected: false,
124 coordinates: null,
125 });
126 const [beamKey, setBeamKey] = useState(0);
127 const [cycleCollisionDetected, setCycleCollisionDetected] = useState(false);
128
129 useEffect(() => {
130 const checkCollision = () => {
131 if (
132 beamRef.current &&
133 containerRef.current &&
134 parentRef.current &&
135 !cycleCollisionDetected
136 ) {
137 const beamRect = beamRef.current.getBoundingClientRect();
138 const containerRect = containerRef.current.getBoundingClientRect();
139 const parentRect = parentRef.current.getBoundingClientRect();
140
141 if (beamRect.bottom >= containerRect.top) {
142 const relativeX =
143 beamRect.left - parentRect.left + beamRect.width / 2;
144 const relativeY = beamRect.bottom - parentRect.top;
145
146 setCollision({
147 detected: true,
148 coordinates: {
149 x: relativeX,
150 y: relativeY,
151 },
152 });
153 setCycleCollisionDetected(true);
154 }
155 }
156 };
157
158 const animationInterval = setInterval(checkCollision, 50);
159
160 return () => clearInterval(animationInterval);
161 }, [cycleCollisionDetected, containerRef]);
162
163 useEffect(() => {
164 if (collision.detected && collision.coordinates) {
165 setTimeout(() => {
166 setCollision({ detected: false, coordinates: null });
167 setCycleCollisionDetected(false);
168 }, 2000);
169
170 setTimeout(() => {
171 setBeamKey((prevKey) => prevKey + 1);
172 }, 2000);
173 }
174 }, [collision]);
175
176 return (
177 <>
178 <motion.div
179 key={beamKey}
180 ref={beamRef}
181 animate="animate"
182 initial={{
183 translateY: beamOptions.initialY || "-200px",
184 translateX: beamOptions.initialX || "0px",
185 rotate: beamOptions.rotate || 0,
186 }}
187 variants={{
188 animate: {
189 translateY: beamOptions.translateY || "1800px",
190 translateX: beamOptions.translateX || "0px",
191 rotate: beamOptions.rotate || 0,
192 },
193 }}
194 transition={{
195 duration: beamOptions.duration || 8,
196 repeat: Infinity,
197 repeatType: "loop",
198 ease: "linear",
199 delay: beamOptions.delay || 0,
200 repeatDelay: beamOptions.repeatDelay || 0,
201 }}
202 className={cn(
203 "absolute left-0 top-20 m-auto h-14 w-px rounded-full bg-gradient-to-t from-indigo-500 via-purple-500 to-transparent",
204 beamOptions.className
205 )}
206 />
207 <AnimatePresence>
208 {collision.detected && collision.coordinates && (
209 <Explosion
210 key={`${collision.coordinates.x}-${collision.coordinates.y}`}
211 className=""
212 style={{
213 left: `${collision.coordinates.x}px`,
214 top: `${collision.coordinates.y}px`,
215 transform: "translate(-50%, -50%)",
216 }}
217 />
218 )}
219 </AnimatePresence>
220 </>
221 );
222});
223
224CollisionMechanism.displayName = "CollisionMechanism";
225
226const Explosion = ({ ...props }: React.HTMLProps<HTMLDivElement>) => {
227 const spans = Array.from({ length: 20 }, (_, index) => ({
228 id: index,
229 initialX: 0,
230 initialY: 0,
231 directionX: Math.floor(Math.random() * 80 - 40),
232 directionY: Math.floor(Math.random() * -50 - 10),
233 }));
234
235 return (
236 <div {...props} className={cn("absolute z-50 h-2 w-2", props.className)}>
237 <motion.div
238 initial={{ opacity: 0 }}
239 animate={{ opacity: 1 }}
240 exit={{ opacity: 0 }}
241 transition={{ duration: 1.5, ease: "easeOut" }}
242 className="absolute -inset-x-10 top-0 m-auto h-2 w-10 rounded-full bg-gradient-to-r from-transparent via-indigo-500 to-transparent blur-sm"
243 ></motion.div>
244 {spans.map((span) => (
245 <motion.span
246 key={span.id}
247 initial={{ x: span.initialX, y: span.initialY, opacity: 1 }}
248 animate={{
249 x: span.directionX,
250 y: span.directionY,
251 opacity: 0,
252 }}
253 transition={{ duration: Math.random() * 1.5 + 0.5, ease: "easeOut" }}
254 className="absolute h-1 w-1 rounded-full bg-gradient-to-b from-indigo-500 to-purple-500"
255 />
256 ))}
257 </div>
258 );
259};
260