From b903cd0ecdeb399e211791aa4729e40c76293281 Mon Sep 17 00:00:00 2001 From: vshnosngh Date: Wed, 4 Nov 2026 12:51:14 +0530 Subject: [PATCH] feat: implement full-image generative diffusion grain synthesis without sliding --- src/components/ui/GrainTransformShowcase.tsx | 619 ++++++++++--------- 1 file changed, 319 insertions(+), 300 deletions(-) diff --git a/src/components/ui/GrainTransformShowcase.tsx b/src/components/ui/GrainTransformShowcase.tsx index 1ba1812..04f778d 100644 --- a/src/components/ui/GrainTransformShowcase.tsx +++ b/src/components/ui/GrainTransformShowcase.tsx @@ -6,12 +6,12 @@ import { Play, Pause, RotateCcw, - SlidersHorizontal, Zap, + Eye, + SlidersHorizontal, Layers, - Maximize2 + Wand2 } from 'lucide-react'; -import { BeforeAfterSlider } from './BeforeAfterSlider'; interface GrainTransformShowcaseProps { beforeUrl: string; @@ -27,117 +27,139 @@ export function GrainTransformShowcase({ beforeUrl, afterUrl, beforeLabel = 'RAW INPUT PHOTO', - afterLabel = 'AI SYNTHESIZED', + afterLabel = 'AI SYNTHESIZED (4K)', aspectRatio = 'aspect-[4/3] lg:aspect-auto lg:h-full', className = '', - initialMode = 'transform', }: GrainTransformShowcaseProps) { - const [mode, setMode] = useState<'transform' | 'slider'>(initialMode); const [isPlaying, setIsPlaying] = useState(true); - const [progress, setProgress] = useState(0); // 0.0 to 1.0 + const [progress, setProgress] = useState(0); // 0.0 to 1.0 (Diffusion Step) const [grainSeed, setGrainSeed] = useState(1); const [isScrubbing, setIsScrubbing] = useState(false); + const [peekRaw, setPeekRaw] = useState(false); const containerRef = useRef(null); - const canvasRef = useRef(null); + const noiseCanvasRef = useRef(null); const animFrameRef = useRef(0); const lastTimeRef = useRef(0); const phaseRef = useRef<'forward' | 'holdAfter' | 'reverse' | 'holdBefore'>('forward'); const holdTimerRef = useRef(0); - // Grain particle canvas effect + // Full-Frame Generative Neural Latent Grain Canvas Simulation + // Creates dancing multicolored grain particles across the ENTIRE image like diffusion models useEffect(() => { - if (mode !== 'transform') return; - const canvas = canvasRef.current; + const canvas = noiseCanvasRef.current; if (!canvas) return; const ctx = canvas.getContext('2d'); if (!ctx) return; - let particleAnimId: number; - let particles: Array<{ + let noiseAnimId: number; + const resize = () => { + const rect = canvas.getBoundingClientRect(); + if (rect.width > 0 && rect.height > 0) { + // Scaled internal buffer for high density granular performance + canvas.width = Math.min(rect.width, 640); + canvas.height = Math.min(rect.height, 480); + } + }; + resize(); + window.addEventListener('resize', resize); + + // Dynamic Grain Particles that swirl in latent space + const particles: Array<{ x: number; y: number; vx: number; vy: number; size: number; alpha: number; - color: string; - life: number; + hue: number; }> = []; - const resize = () => { - const rect = canvas.getBoundingClientRect(); - canvas.width = rect.width; - canvas.height = rect.height; - }; - resize(); - window.addEventListener('resize', resize); - - const colors = ['#818cf8', '#a78bfa', '#38bdf8', '#f43f5e', '#ffffff']; + // Pre-seed ambient latent particles + for (let i = 0; i < 45; i++) { + particles.push({ + x: Math.random() * (canvas.width || 600), + y: Math.random() * (canvas.height || 450), + vx: (Math.random() - 0.5) * 1.8, + vy: (Math.random() - 0.5) * 1.8, + size: Math.random() * 2.2 + 0.6, + alpha: Math.random() * 0.7 + 0.2, + hue: Math.floor(Math.random() * 60) + 220, // Blue-Indigo-Violet + }); + } const render = () => { + if (canvas.width === 0 || canvas.height === 0) { + noiseAnimId = requestAnimationFrame(render); + return; + } + ctx.clearRect(0, 0, canvas.width, canvas.height); - // Only spawn particles if transition is active (not resting at 0 or 1) - if (progress > 0.02 && progress < 0.98) { - const sweepX = canvas.width * progress; + // Noise intensity peaks at progress = 0.5 (deep latent space) + // 0.0 -> 0.0 intensity, 0.5 -> 1.0 intensity, 1.0 -> 0.0 intensity + const noiseIntensity = Math.sin(progress * Math.PI); - // Spawn particles along the transition sweep boundary - for (let i = 0; i < 4; i++) { - particles.push({ - x: sweepX + (Math.random() - 0.5) * 30, - y: Math.random() * canvas.height, - vx: (Math.random() - 0.3) * 3, - vy: (Math.random() - 0.5) * 4, - size: Math.random() * 2.5 + 0.8, - alpha: Math.random() * 0.9 + 0.3, - color: colors[Math.floor(Math.random() * colors.length)], - life: 1.0, - }); + if (noiseIntensity > 0.04) { + // 1. Procedural High-Density Grain Static + const imgData = ctx.createImageData(canvas.width, canvas.height); + const buffer = new Uint32Array(imgData.data.buffer); + const len = buffer.length; + + // Density increases dramatically during midpoint + const grainAlpha = Math.floor(noiseIntensity * 140); + const colorShift = Math.sin(Date.now() * 0.003) * 15; + + for (let i = 0; i < len; i += 2) { + if (Math.random() < noiseIntensity * 0.85) { + const val = (Math.random() * 255) | 0; + const r = Math.min(255, val + colorShift); + const g = Math.min(255, val + 10); + const b = Math.min(255, val + 35); + // Little-endian RGBA packing: ABGR + buffer[i] = (grainAlpha << 24) | (b << 16) | (g << 8) | r; + } + } + ctx.putImageData(imgData, 0, 0); + + // 2. Swirling Latent Sparkle Particles + for (let i = 0; i < particles.length; i++) { + const p = particles[i]; + p.x += p.vx * (1 + noiseIntensity * 2); + p.y += p.vy * (1 + noiseIntensity * 2); + + if (p.x < 0) p.x = canvas.width; + if (p.x > canvas.width) p.x = 0; + if (p.y < 0) p.y = canvas.height; + if (p.y > canvas.height) p.y = 0; + + ctx.fillStyle = `hsla(${p.hue}, 85%, 70%, ${p.alpha * noiseIntensity})`; + ctx.beginPath(); + ctx.arc(p.x, p.y, p.size * (1 + noiseIntensity * 0.5), 0, Math.PI * 2); + ctx.fill(); } } - // Update and draw particles - for (let i = particles.length - 1; i >= 0; i--) { - const p = particles[i]; - p.x += p.vx; - p.y += p.vy; - p.life -= 0.025; - p.alpha = Math.max(0, p.life); - - if (p.life <= 0) { - particles.splice(i, 1); - continue; - } - - ctx.fillStyle = p.color; - ctx.globalAlpha = p.alpha; - ctx.beginPath(); - ctx.arc(p.x, p.y, p.size, 0, Math.PI * 2); - ctx.fill(); - } - ctx.globalAlpha = 1.0; - - particleAnimId = requestAnimationFrame(render); + noiseAnimId = requestAnimationFrame(render); }; render(); return () => { - cancelAnimationFrame(particleAnimId); + cancelAnimationFrame(noiseAnimId); window.removeEventListener('resize', resize); }; - }, [mode, progress]); + }, [progress]); - // Main animation clock loop: Smooth cycle through Raw -> Grain Morph Sweep -> Synthesized Result -> Rest + // Main Diffusion Synthesis Animation Clock (Procedural sampling loop) useEffect(() => { - if (mode !== 'transform' || !isPlaying || isScrubbing) { + if (!isPlaying || isScrubbing) { cancelAnimationFrame(animFrameRef.current); return; } - const duration = 2800; // 2.8s sweep duration - const holdDuration = 1800; // 1.8s hold on result + const duration = 3200; // 3.2s full diffusion synthesis + const holdDuration = 2200; // 2.2s admiring hold on 4K result const loop = (timestamp: number) => { if (!lastTimeRef.current) lastTimeRef.current = timestamp; @@ -163,7 +185,8 @@ export function GrainTransformShowcase({ } } else if (phaseRef.current === 'reverse') { setProgress((prev) => { - const next = prev - (delta / (duration * 0.7)); // faster rewind + // Smooth rewind back to raw photo + const next = prev - (delta / 1400); if (next <= 0.0) { phaseRef.current = 'holdBefore'; holdTimerRef.current = 0; @@ -173,7 +196,7 @@ export function GrainTransformShowcase({ }); } else if (phaseRef.current === 'holdBefore') { holdTimerRef.current += delta; - if (holdTimerRef.current >= 800) { + if (holdTimerRef.current >= 900) { phaseRef.current = 'forward'; } } @@ -187,7 +210,7 @@ export function GrainTransformShowcase({ cancelAnimationFrame(animFrameRef.current); lastTimeRef.current = 0; }; - }, [mode, isPlaying, isScrubbing]); + }, [isPlaying, isScrubbing]); const handleManualScrub = (e: React.ChangeEvent) => { setIsScrubbing(true); @@ -206,263 +229,259 @@ export function GrainTransformShowcase({ setIsPlaying(true); }; - // Status pill text based on progress - const getStatusText = () => { - if (progress < 0.1) return 'Input Raw Photo'; - if (progress > 0.9) return 'AI Synthesized (Lossless 4K)'; - return `Synthesizing Neural Grains • ${Math.round(progress * 100)}%`; + // Math for Generative Latent Diffusion Transition + // Entire Before image transforms into grains across the whole screen, then crystallizes into After + const effectiveProgress = peekRaw ? 0 : progress; + + // Diffusion Curve (smooth S-curve) + const diffusionStep = Math.min(30, Math.floor(effectiveProgress * 30)); + const noiseIntensity = Math.sin(effectiveProgress * Math.PI); // 0 -> 1 -> 0 + + // Pixel displacement scale: breaks image into scattered granular particles + const displacementScale = Math.round(noiseIntensity * 36); + + // Before image dissolves out from 0.0 to 0.55 + const beforeOpacity = effectiveProgress <= 0.5 + ? Math.max(0, 1 - Math.pow(effectiveProgress / 0.48, 1.4)) + : 0; + + // After image crystallizes in from 0.45 to 1.0 + const afterOpacity = effectiveProgress >= 0.45 + ? Math.min(1, Math.pow((effectiveProgress - 0.45) / 0.52, 1.3)) + : 0; + + // Dynamic status text resembling generative diffusion models + const getStatusInfo = () => { + if (peekRaw) return { text: 'Original Raw Input Photo', color: 'bg-slate-400' }; + if (effectiveProgress < 0.12) return { text: 'Raw Input Photo • Ready for Synthesis', color: 'bg-blue-400' }; + if (effectiveProgress < 0.45) return { text: `Forward Diffusion • Dissolving into Grains (${diffusionStep}/30)`, color: 'bg-amber-400 animate-pulse' }; + if (effectiveProgress < 0.65) return { text: `Neural Latent Synthesis • Denoising Tensor (${diffusionStep}/30)`, color: 'bg-indigo-400 animate-ping' }; + if (effectiveProgress < 0.94) return { text: `Reverse Diffusion • Crystallizing 4K Details (${diffusionStep}/30)`, color: 'bg-sky-400 animate-pulse' }; + return { text: 'AI Synthesis Complete • Lossless 4K Masterpiece', color: 'bg-emerald-400' }; }; - const percent = Math.round(progress * 100); + const status = getStatusInfo(); + const percent = Math.round(effectiveProgress * 100); return ( -
- {/* Top Floating Glass Header */} +
+ {/* Hidden SVG Filter Definition for Physical Procedural Grain Displacement */} + + + {/* Top Floating Glass Header Bar */}
- {/* Dynamic Status Pill */} -
- 0.1 && progress < 0.9 - ? 'bg-amber-400 animate-ping' - : progress >= 0.9 - ? 'bg-emerald-400' - : 'bg-blue-400' - }`} /> - {getStatusText()} + {/* Dynamic Generative Model Status Pill */} +
+ + {status.text}
- {/* Mode Switcher: Grain Transform vs Compare Slider */} -
+ {/* Quick Actions (Peek Raw Photo / Diffusion Step Counter) */} +
+ {/* Hold to Peek Raw Photo */} - + + {/* Model Iteration Pill */} +
+ + Step {diffusionStep}/30 +
- {/* Main Visual Display Stage */} + {/* Main Full-Frame Generative Diffusion Stage (NO SLIDING - WHOLE IMAGE TRANSFORMS) */}
- {mode === 'slider' ? ( - + + {/* Layer 1: Ambient Backdrop Gradient */} +
+ + {/* Layer 2: The Raw Input Photo (Whole Image converts into grains via displacement & blur) */} + {/* eslint-disable-next-line @next/next/no-img-element */} + {beforeLabel} 2 + ? `url(#neural-grain-displace-${grainSeed}) blur(${noiseIntensity * 3.5}px)` + : 'none', + transform: `scale(${1 + noiseIntensity * 0.04})`, + }} /> - ) : ( -
- {/* Layer 1: The AI Synthesized Result (Bottom Layer) */} - {/* eslint-disable-next-line @next/next/no-img-element */} - {afterLabel} - {/* Layer 2: Raw Input Photo (Clipped by Transition Progress) */} -
- {/* eslint-disable-next-line @next/next/no-img-element */} - {beforeLabel} -
+ {/* Layer 3: The AI Synthesized Result (Resolves cleanly out of the noise) */} + {/* eslint-disable-next-line @next/next/no-img-element */} + {afterLabel} 2 + ? `url(#neural-grain-displace-${grainSeed}) blur(${noiseIntensity * 2.8}px)` + : 'none', + transform: `scale(${1.02 - (1 - noiseIntensity) * 0.02})`, + }} + /> - {/* Layer 3: Generative Neural Grain Front (Visible along the transform edge) */} - {progress > 0.01 && progress < 0.99 && ( -
- {/* Latent Neural Laser Beam */} -
+ {/* Layer 4: Whole-Image Generative Noise Canvas (Procedural Dancing Grain Particles) */} + - {/* Intense High-Frequency Animated Noise Filter along boundary */} - - - - - - - -
- )} - - {/* Layer 4: Global Film Grain Shimmer (Simulates Real Photographic Latent Emulsion) */} -
- - - - - - - -
- - {/* Layer 5: Canvas Particle Spark Emitter along transition wave */} - - - {/* Corner Badges */} -
- - {beforeLabel} - -
-
- = 0.5 - ? 'bg-blue-600/90 text-white border-blue-400/40 opacity-100 shadow-md' - : 'bg-black/40 text-slate-400 border-transparent opacity-40' - }`}> - {afterLabel} - -
+ {/* Layer 5: High-Frequency Film Grain Overlay (Subtle photographic latent emulsion) */} +
+ + + + + + +
- )} + + {/* Layer 6: Mid-Diffusion Latent Glow Wave (Aurora Pulse across center at peak grain state) */} + {noiseIntensity > 0.3 && ( +
+ )} + + {/* Layer 7: Final Completion Flash Sheen (Runs momentarily when reaching 100% synthesis) */} + {effectiveProgress > 0.94 && ( +
+ )} + + {/* Corner Dynamic Tags */} +
+ + {beforeLabel} + +
+
+ = 0.55 + ? 'bg-blue-600/90 text-white border-blue-400/40 shadow-lg shadow-blue-500/20' + : 'bg-black/30 text-slate-500 border-transparent opacity-30' + }`}> + {afterLabel} + +
+
- {/* Bottom Interactive Playback & Scrub Bar (For Grain Morph Mode) */} - {mode === 'transform' && ( -
-
- {/* Play / Pause Toggle */} - - - {/* Replay Button */} - - - {/* Manual Timeline Scrubber */} -
- - {/* Progress Glow Fill */} -
-
- - {/* Numerical Progress Indicator */} - - {percent}% - -
- -
- - - Generative Latent Grain Diffusion - - - Drag slider to scrub frame-by-frame - -
-
- )} - - {/* Hint for Split Slider Mode */} - {mode === 'slider' && ( -
- - - Drag divider line to inspect textures - + {/* Bottom Interactive Generative Timeline & Controls */} +
+
+ {/* Play / Pause Toggle */} + + {/* Replay Full Synthesis from Beginning */} + + + {/* Generative Diffusion Progress Scrubber */} +
+ + {/* Glowing Gradient Progress Fill */} +
+
+ + {/* Numerical % Progress Indicator */} + + {percent}% +
- )} + + {/* Informative Sub-Bar */} +
+ + + Neural Diffusion Generative Model (Whole-Image Transformation) + + + Drag slider to inspect grain sampling frame-by-frame + +
+
); }