'use client' import { useEffect, useRef, useState } from 'react' import { motion, AnimatePresence } from 'framer-motion' import { X } from 'lucide-react' const UNIT_FRAMES = 40 interface UnitRevealOverlayProps { unitId: string | null unitLabel: string unitSub: string onClose: () => void } export default function UnitRevealOverlay({ unitId, unitLabel, unitSub, onClose, }: UnitRevealOverlayProps) { const canvasRef = useRef(null) const framesRef = useRef([]) const rafRef = useRef(null) const [loadProgress, setLoadProgress] = useState(0) const [loaded, setLoaded] = useState(false) const [finished, setFinished] = useState(false) // Frame'leri preload et useEffect(() => { if (!unitId) return setLoaded(false) setFinished(false) setLoadProgress(0) framesRef.current = [] const images: HTMLImageElement[] = new Array(UNIT_FRAMES) let loadedCount = 0 let cancelled = false for (let i = 0; i < UNIT_FRAMES; i++) { const img = new Image() img.src = `/unit-frames/${unitId}/ezgif-frame-${String(i + 1).padStart(3, '0')}.jpg` img.onload = () => { if (cancelled) return loadedCount++ setLoadProgress(Math.round((loadedCount / UNIT_FRAMES) * 100)) if (loadedCount === UNIT_FRAMES) { framesRef.current = images setLoaded(true) } } images[i] = img } return () => { cancelled = true } }, [unitId]) // Canvas boyutu useEffect(() => { const canvas = canvasRef.current if (!canvas) return const resize = () => { canvas.width = window.innerWidth canvas.height = window.innerHeight } resize() window.addEventListener('resize', resize) return () => window.removeEventListener('resize', resize) }, []) // Yüklenince otomatik oynat useEffect(() => { if (!loaded) return const canvas = canvasRef.current const ctx = canvas?.getContext('2d') if (!canvas || !ctx) return let frame = 0 const fps = 24 const interval = 1000 / fps let last = 0 const draw = (imgs: HTMLImageElement[], index: number) => { const img = imgs[index] if (img?.complete && img.naturalWidth > 0) { ctx.drawImage(img, 0, 0, canvas.width, canvas.height) } } const animate = (ts: number) => { if (ts - last >= interval) { draw(framesRef.current, frame) last = ts if (frame < UNIT_FRAMES - 1) { frame++ } else { setFinished(true) return // son frame'de dur } } rafRef.current = requestAnimationFrame(animate) } rafRef.current = requestAnimationFrame(animate) return () => { if (rafRef.current) cancelAnimationFrame(rafRef.current) } }, [loaded]) // ESC ile kapat useEffect(() => { const onKey = (e: KeyboardEvent) => { if (e.key === 'Escape') onClose() } window.addEventListener('keydown', onKey) return () => window.removeEventListener('keydown', onKey) }, [onClose]) return ( {unitId && ( {/* Canvas */} {/* Loading */} {!loaded && (

{loadProgress}%

)} {/* Kapat */} {/* Bitiş bilgisi — son frame'de */} {finished && (

Vesta Muğla

{unitLabel}

{unitSub}

)}
)} ) }