|
| 1 | +import { motion, useAnimation } from 'framer-motion'; |
| 2 | +import { useEffect } from 'react'; |
| 3 | + |
| 4 | +interface FloatItemProps { |
| 5 | + src: string; |
| 6 | + size?: string; |
| 7 | + duration?: number; |
| 8 | + interval?: number; |
| 9 | +} |
| 10 | + |
| 11 | +// 랜덤 이동 |
| 12 | +const getRandomPosition = () => ({ |
| 13 | + x: Math.floor(Math.random() * 100 - 50), |
| 14 | + y: Math.floor(Math.random() * 100 - 50), |
| 15 | +}); |
| 16 | + |
| 17 | +const FloatingItem = ({ src, size = 'w-20', duration = 6, interval = 8000 }: FloatItemProps) => { |
| 18 | + const controls = useAnimation(); |
| 19 | + |
| 20 | + useEffect(() => { |
| 21 | + const animateRandomly = () => { |
| 22 | + const { x, y } = getRandomPosition(); |
| 23 | + controls.start({ |
| 24 | + x: `${x}vw`, |
| 25 | + y: `${y}vh`, |
| 26 | + transition: { |
| 27 | + duration, |
| 28 | + ease: 'easeInOut', |
| 29 | + }, |
| 30 | + }); |
| 31 | + }; |
| 32 | + |
| 33 | + animateRandomly(); // 첫 애니메이션 |
| 34 | + const intervalId = setInterval(animateRandomly, interval); |
| 35 | + |
| 36 | + return () => clearInterval(intervalId); |
| 37 | + }, [controls, duration, interval]); |
| 38 | + |
| 39 | + return ( |
| 40 | + <motion.img |
| 41 | + src={src} |
| 42 | + className={`absolute ${size} opacity-80 pointer-events-none`} |
| 43 | + style={{ |
| 44 | + left: '50%', |
| 45 | + top: '50%', |
| 46 | + transform: 'translate(-50%, -50%)', // 중앙에서 시작 |
| 47 | + }} |
| 48 | + animate={controls} |
| 49 | + alt="floating item" |
| 50 | + /> |
| 51 | + ); |
| 52 | +}; |
| 53 | +const FloatingBackground = () => { |
| 54 | + return ( |
| 55 | + <div className="fixed inset-0 -z-1000 overflow-hidden pointer-events-none"> |
| 56 | + <FloatingItem |
| 57 | + src="https://noticon-static.tammolo.com/dgggcrkxq/image/upload/v1741777507/noticon/dihnlmrn8aopedon41ur.gif" |
| 58 | + size="w-20" |
| 59 | + duration={5} |
| 60 | + interval={7000} |
| 61 | + /> |
| 62 | + <FloatingItem |
| 63 | + src="https://noticon-static.tammolo.com/dgggcrkxq/image/upload/v1683523256/noticon/kyp3frg9corycovxokx6.gif" |
| 64 | + size="w-16" |
| 65 | + duration={6} |
| 66 | + interval={9000} |
| 67 | + /> |
| 68 | + <FloatingItem |
| 69 | + src="https://noticon-static.tammolo.com/dgggcrkxq/image/upload/v1686670279/noticon/gvasp3qtaz16spmwoorl.gif" |
| 70 | + size="w-24" |
| 71 | + duration={7} |
| 72 | + interval={10000} |
| 73 | + /> |
| 74 | + </div> |
| 75 | + ); |
| 76 | +}; |
| 77 | + |
| 78 | +export default FloatingBackground; |
0 commit comments