// app/layout.tsx export const metadata = { title: 'My Next.js App', description: 'A…
- User
// app/layout.tsx
export const metadata = { title: 'My Next.js App', description: 'A Next.js App with draggable windows', };
import './globals.css'; import { WindowManagerProvider } from '../contexts/WindowManagerContext'; import AnimatedBackground from '../components/AnimatedBackground';
export default function RootLayout({ children, }: { children: React.ReactNode; }) { return ( {/* Include the AnimatedBackground as a client component */} {children} ); }
TASK: Wirte code for teh animated background file which will use tailwindcss to display a regular grid of dots in a 25px by 25px grid spacing between them. Then, it will use framer motion to track user's mouse position and create a magnetic feild hover effect for the dots in relation to teh mouse.
- Assistant
// components/AnimatedBackground.tsx 'use client'; import React, { useEffect, useState, useRef } from 'react'; import { motion } from 'framer-motion'; const AnimatedBackground: React.FC = () => { const [windowSize, setWindowSize] = useState<{ width: number; height: number }>({ width: 0, height: 0, }); const mousePosition = useRef<{ x: number; y: number }>({ x: 0, y: 0 }); useEffect(() => { // Update window size on resize function handleResize() { setWindowSize({ width: window.innerWidth, height: window.innerHeight }); } handleResize(); // Initial call window.addEventListener('resize', handleResize); return () => window.removeEventListener('resize', handleResize); }, []); useEffect(() => { // Update mouse position on mouse move function handleMouseMove(event: MouseEvent) { mousePosition.current = { x: event.clientX, y: event.clientY }; } window.addEventListener('mousemove', handleMouseMove); return () => window.removeEventListener('mousemove', handleMouseMove); }, []); // Calculate number of columns and rows based on window size and grid spacing const gridSpacing = 25; const columns = Math.ceil(windowSize.width / gridSpacing); const rows = Math.ceil(windowSize.height / gridSpacing); const dots = []; for (let row = 0; row <= rows; row++) { for (let col = 0; col <= columns; col++) { const dotX = col * gridSpacing; const dotY = row * gridSpacing; // Calculate distance from mouse const dx = dotX - mousePosition.current.x; const dy = dotY - mousePosition.current.y; const distance = Math.sqrt(dx * dx + dy * dy); // Repel dots if within a certain radius const radius = 100; // Adjust as needed const maxDisplacement = 15; // Adjust as needed let displacementX = 0; let displacementY = 0; if (distance < radius) { const angle = Math.atan2(dy, dx); const strength = (radius - distance) / radius; displacementX = Math.cos(angle) * strength * maxDisplacement; displacementY = Math.sin(angle) * strength * maxDisplacement; } dots.push( <motion.div key={`${row}-${col}`} className="absolute w-1 h-1 bg-gray-500 rounded-full" style={{ left: dotX, top: dotY, }} animate={{ x: displacementX, y: displacementY, }} transition={{ ease: 'linear', duration: 0.1, }} /> ); } } return <div className="absolute inset-0">{dots}</div>; }; export default AnimatedBackground;This code defines the
AnimatedBackgroundcomponent that:- Uses the
'use client'directive to ensure it runs on the client side. - Tracks the window size to determine how many dots to render.
- Listens to mouse movements to update the position of the mouse.
- Creates a grid of dots spaced 25px apart using Tailwind CSS classes.
- Calculates the distance of each dot to the mouse position and applies a repelling force if the dot is within a certain radius, creating a magnetic field effect.
- Uses Framer Motion's
motion.divto animate the dots based on their calculated displacement.
Make sure to install
framer-motionand have Tailwind CSS set up in your Next.js project for this code to work. - Uses the