|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import Image from 'next/image'; |
| 4 | +import foreStar from '../../../../../public/1Stars.png'; |
| 5 | +import backStar from '../../../../../public/2Stars.png'; |
| 6 | +import { useEffect, useRef } from 'react'; |
| 7 | +import gsap from 'gsap'; |
| 8 | + |
| 9 | +function StarMain() { |
| 10 | + const background = useRef<HTMLDivElement>(null); |
| 11 | + const foreground = useRef<HTMLDivElement>(null); |
| 12 | + const mouse = useRef({ x: 0, y: 0 }); |
| 13 | + const rafId = useRef<number | null>(null); |
| 14 | + |
| 15 | + useEffect(() => { |
| 16 | + if (!background.current || !foreground.current) return; |
| 17 | + |
| 18 | + const bgX = gsap.quickSetter(background.current, 'x', 'px'); |
| 19 | + const bgY = gsap.quickSetter(background.current, 'x', 'px'); |
| 20 | + const bgRotate = gsap.quickSetter(background.current, 'rotate', 'deg'); |
| 21 | + |
| 22 | + const fgX = gsap.quickSetter(foreground.current, 'x', 'px'); |
| 23 | + const fgY = gsap.quickSetter(foreground.current, 'y', 'px'); |
| 24 | + const fgRotate = gsap.quickSetter(foreground.current, 'rotate', 'deg'); |
| 25 | + |
| 26 | + const update = () => { |
| 27 | + const { x, y } = mouse.current; |
| 28 | + |
| 29 | + bgX(x * -10); |
| 30 | + bgY(y * -10); |
| 31 | + bgRotate(x * -5); |
| 32 | + |
| 33 | + fgX(x * 20); |
| 34 | + fgY(y * 20); |
| 35 | + fgRotate(y * 5); |
| 36 | + |
| 37 | + rafId.current = requestAnimationFrame(update); |
| 38 | + }; |
| 39 | + |
| 40 | + const handleMouseMove = (e: MouseEvent) => { |
| 41 | + // 화면 중앙 기준으로 얼마나 벗어났는지 (-1 ~ 1 범위) |
| 42 | + const x = (e.clientX / window.innerWidth - 0.5) * 2; |
| 43 | + const y = (e.clientY / window.innerHeight - 0.5) * 2; |
| 44 | + mouse.current = { x, y }; |
| 45 | + }; |
| 46 | + |
| 47 | + const handleTouchMove = (e: TouchEvent) => { |
| 48 | + const touch = e.touches[0]; |
| 49 | + const x = (touch.clientX / window.innerWidth - 0.5) * 2; |
| 50 | + const y = (touch.clientY / window.innerHeight - 0.5) * 2; |
| 51 | + mouse.current = { x, y }; |
| 52 | + }; |
| 53 | + |
| 54 | + window.addEventListener('mousemove', handleMouseMove); |
| 55 | + window.addEventListener('touchmove', handleTouchMove); |
| 56 | + rafId.current = requestAnimationFrame(update); |
| 57 | + |
| 58 | + return () => { |
| 59 | + window.removeEventListener('mousemove', handleMouseMove); |
| 60 | + window.removeEventListener('touchmove', handleTouchMove); |
| 61 | + if (rafId.current) cancelAnimationFrame(rafId.current); |
| 62 | + }; |
| 63 | + }, []); |
| 64 | + |
| 65 | + return ( |
| 66 | + <div> |
| 67 | + <div className="absolute top-0 left-0 w-full h-full overflow-hidden pointer-events-none"> |
| 68 | + <div |
| 69 | + ref={background} |
| 70 | + className="absolute w-screen h-screen top-0 left-0 will-change-transform" |
| 71 | + > |
| 72 | + <Image src={foreStar} alt="앞쪽 별" fill className="object-cover object-center" /> |
| 73 | + </div> |
| 74 | + <div |
| 75 | + ref={foreground} |
| 76 | + className="absolute w-screen h-screen top-0 left-0 will-change-transform" |
| 77 | + > |
| 78 | + <Image src={backStar} alt="뒤쪽 별" fill className="object-cover object-center" /> |
| 79 | + </div> |
| 80 | + </div> |
| 81 | + </div> |
| 82 | + ); |
| 83 | +} |
| 84 | + |
| 85 | +export default StarMain; |
0 commit comments