{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"noise-reveal","type":"registry:ui","title":"NoiseReveal","description":"WebGL image reveal with a Perlin-noise dissolve mask, contracting radial gradient, and wave displacement","author":"fancy-ui <https://fancy-ui.rama.app>","categories":["media"],"dependencies":["three@^0.183.2"],"devDependencies":["@types/three@^0.183.1"],"registryDependencies":["https://fancy-ui.rama.app/r/internals.json","https://fancy-ui.rama.app/r/internals-dom.json","https://fancy-ui.rama.app/r/internals-motion.json","https://fancy-ui.rama.app/r/utils.json"],"docs":"Installs three as a dependency.","files":[{"path":"react/src/components/noise-reveal/NoiseReveal.tsx","content":"\"use client\";\n\nimport { useEffect, useState } from \"react\";\nimport * as THREE from \"three\";\nimport { cn } from \"../../utils\";\nimport { useElementRef } from \"../../internals/dom/use-element-ref\";\nimport { useLiveRef } from \"../../internals/dom/use-live-ref\";\nimport { observeInView } from \"../../internals/motion/in-view\";\n\n/**\n * NoiseReveal - A WebGL image reveal driven by a Perlin-noise dissolve mask.\n *\n * The mask lives entirely in the fragment shader: domain-warped noise plus a\n * radial gradient that contracts as `uProgress` runs 0 -> 1, clamped and\n * inverted into an alpha value. The vertex shader adds a wave displacement that\n * fades out over the same progress. Reveals once on viewport entry, or under\n * full manual control via `revealed`.\n *\n * The render loop pauses while the canvas is scrolled out of view or its tab is\n * in the background, and resumes from the noise phase it paused at.\n */\nexport interface NoiseRevealProps {\n\t/** Image URL */\n\tsrc: string;\n\t/** Accessible label for the image */\n\talt?: string;\n\t/** Reveal trigger: \"view\" = on viewport entry, \"manual\" = via `revealed` */\n\ttrigger?: \"view\" | \"manual\";\n\t/** Manual reveal state (used with trigger=\"manual\"; also allows re-hiding) */\n\trevealed?: boolean;\n\t/** Reveal animation duration in seconds */\n\tduration?: number;\n\t/** Delay before reveal in seconds (trigger=\"view\") */\n\tdelay?: number;\n\t/** Additional CSS classes */\n\tclassName?: string;\n}\n\n// The three animation helpers the Svelte source imports from the package's\n// shared animation utils, re-typed here: the React package has no port of that\n// module, and `internals/motion/easing.ts` carries a different set of curves.\n// Bodies are identical to the originals.\n\n/** Interpolate between two values */\nfunction lerp(start: number, end: number, t: number): number {\n\treturn start + (end - start) * t;\n}\n\n/** Clamp a value between min and max */\nfunction clamp(value: number, min: number, max: number): number {\n\treturn Math.min(Math.max(value, min), max);\n}\n\n/** Cubic ease in-out */\nfunction easeInOutCubic(t: number): number {\n\treturn t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2;\n}\n\n/** Calculate animation progress based on elapsed time */\nfunction getProgress(elapsed: number, duration: number): number {\n\treturn clamp(elapsed / duration, 0, 1);\n}\n\nconst vertexShader = `\n\t\tuniform float uProgress;\n\t\tvarying vec2 vUv;\n\n\t\tvoid main() {\n\t\t\tvec3 newPosition = position;\n\n\t\t\tfloat distanceToCenter = distance(vec2(0.5), uv);\n\t\t\tfloat wave = (1.0 - uProgress) * sin(distanceToCenter * 20.0 - uProgress * 5.0);\n\t\t\tnewPosition.z += wave;\n\n\t\t\tgl_Position = projectionMatrix * modelViewMatrix * vec4(newPosition, 1.0);\n\t\t\tvUv = uv;\n\t\t}\n\t`;\n\n// cnoise: classic 3D Perlin noise by Stefan Gustavson (MIT, github.com/stegu/webgl-noise)\nconst fragmentShader = `\n\t\tuniform sampler2D uTexture;\n\t\tuniform float uTime;\n\t\tuniform float uProgress;\n\t\tuniform vec2 uRes;\n\t\tuniform vec2 uImageRes;\n\t\tvarying vec2 vUv;\n\n\t\tvec4 permute(vec4 x) { return mod(((x * 34.0) + 1.0) * x, 289.0); }\n\t\tvec4 taylorInvSqrt(vec4 r) { return 1.79284291400159 - 0.85373472095314 * r; }\n\t\tvec3 fade(vec3 t) { return t * t * t * (t * (t * 6.0 - 15.0) + 10.0); }\n\n\t\tfloat cnoise(vec3 P) {\n\t\t\tvec3 Pi0 = floor(P);\n\t\t\tvec3 Pi1 = Pi0 + vec3(1.0);\n\t\t\tPi0 = mod(Pi0, 289.0);\n\t\t\tPi1 = mod(Pi1, 289.0);\n\t\t\tvec3 Pf0 = fract(P);\n\t\t\tvec3 Pf1 = Pf0 - vec3(1.0);\n\t\t\tvec4 ix = vec4(Pi0.x, Pi1.x, Pi0.x, Pi1.x);\n\t\t\tvec4 iy = vec4(Pi0.yy, Pi1.yy);\n\t\t\tvec4 iz0 = Pi0.zzzz;\n\t\t\tvec4 iz1 = Pi1.zzzz;\n\n\t\t\tvec4 ixy = permute(permute(ix) + iy);\n\t\t\tvec4 ixy0 = permute(ixy + iz0);\n\t\t\tvec4 ixy1 = permute(ixy + iz1);\n\n\t\t\tvec4 gx0 = ixy0 / 7.0;\n\t\t\tvec4 gy0 = fract(floor(gx0) / 7.0) - 0.5;\n\t\t\tgx0 = fract(gx0);\n\t\t\tvec4 gz0 = vec4(0.5) - abs(gx0) - abs(gy0);\n\t\t\tvec4 sz0 = step(gz0, vec4(0.0));\n\t\t\tgx0 -= sz0 * (step(0.0, gx0) - 0.5);\n\t\t\tgy0 -= sz0 * (step(0.0, gy0) - 0.5);\n\n\t\t\tvec4 gx1 = ixy1 / 7.0;\n\t\t\tvec4 gy1 = fract(floor(gx1) / 7.0) - 0.5;\n\t\t\tgx1 = fract(gx1);\n\t\t\tvec4 gz1 = vec4(0.5) - abs(gx1) - abs(gy1);\n\t\t\tvec4 sz1 = step(gz1, vec4(0.0));\n\t\t\tgx1 -= sz1 * (step(0.0, gx1) - 0.5);\n\t\t\tgy1 -= sz1 * (step(0.0, gy1) - 0.5);\n\n\t\t\tvec3 g000 = vec3(gx0.x, gy0.x, gz0.x);\n\t\t\tvec3 g100 = vec3(gx0.y, gy0.y, gz0.y);\n\t\t\tvec3 g010 = vec3(gx0.z, gy0.z, gz0.z);\n\t\t\tvec3 g110 = vec3(gx0.w, gy0.w, gz0.w);\n\t\t\tvec3 g001 = vec3(gx1.x, gy1.x, gz1.x);\n\t\t\tvec3 g101 = vec3(gx1.y, gy1.y, gz1.y);\n\t\t\tvec3 g011 = vec3(gx1.z, gy1.z, gz1.z);\n\t\t\tvec3 g111 = vec3(gx1.w, gy1.w, gz1.w);\n\n\t\t\tvec4 norm0 = taylorInvSqrt(vec4(dot(g000, g000), dot(g010, g010), dot(g100, g100), dot(g110, g110)));\n\t\t\tg000 *= norm0.x;\n\t\t\tg010 *= norm0.y;\n\t\t\tg100 *= norm0.z;\n\t\t\tg110 *= norm0.w;\n\t\t\tvec4 norm1 = taylorInvSqrt(vec4(dot(g001, g001), dot(g011, g011), dot(g101, g101), dot(g111, g111)));\n\t\t\tg001 *= norm1.x;\n\t\t\tg011 *= norm1.y;\n\t\t\tg101 *= norm1.z;\n\t\t\tg111 *= norm1.w;\n\n\t\t\tfloat n000 = dot(g000, Pf0);\n\t\t\tfloat n100 = dot(g100, vec3(Pf1.x, Pf0.yz));\n\t\t\tfloat n010 = dot(g010, vec3(Pf0.x, Pf1.y, Pf0.z));\n\t\t\tfloat n110 = dot(g110, vec3(Pf1.xy, Pf0.z));\n\t\t\tfloat n001 = dot(g001, vec3(Pf0.xy, Pf1.z));\n\t\t\tfloat n101 = dot(g101, vec3(Pf1.x, Pf0.y, Pf1.z));\n\t\t\tfloat n011 = dot(g011, vec3(Pf0.x, Pf1.yz));\n\t\t\tfloat n111 = dot(g111, Pf1);\n\n\t\t\tvec3 fade_xyz = fade(Pf0);\n\t\t\tvec4 n_z = mix(vec4(n000, n100, n010, n110), vec4(n001, n101, n011, n111), fade_xyz.z);\n\t\t\tvec2 n_yz = mix(n_z.xy, n_z.zw, fade_xyz.y);\n\t\t\tfloat n_xyz = mix(n_yz.x, n_yz.y, fade_xyz.x);\n\t\t\treturn 2.2 * n_xyz;\n\t\t}\n\n\t\tvec2 CoverUV(vec2 u, vec2 s, vec2 i) {\n\t\t\tfloat rs = s.x / s.y;\n\t\t\tfloat ri = i.x / i.y;\n\t\t\tvec2 st = rs < ri ? vec2(i.x * s.y / i.y, s.y) : vec2(s.x, i.y * s.x / i.x);\n\t\t\tvec2 o = (rs < ri ? vec2((st.x - s.x) / 2.0, 0.0) : vec2(0.0, (st.y - s.y) / 2.0)) / st;\n\t\t\treturn u * s / st + o;\n\t\t}\n\n\t\tvoid main() {\n\t\t\tvec2 newUv = CoverUV(vUv, uRes, uImageRes);\n\n\t\t\tvec2 displacedUv = vUv + cnoise(vec3(vUv * 5.0, uTime * 0.1));\n\t\t\tfloat strength = cnoise(vec3(displacedUv * 5.0, uTime * 0.2));\n\n\t\t\tfloat radialGradient = distance(vUv, vec2(0.5)) * 12.5 - 7.0 * uProgress;\n\t\t\tstrength += radialGradient;\n\n\t\t\tstrength = clamp(strength, 0.0, 1.0);\n\t\t\tstrength = 1.0 - strength;\n\n\t\t\tvec3 textureColor = texture2D(uTexture, newUv).rgb;\n\n\t\t\tfloat opacityProgress = smoothstep(0.0, 0.7, uProgress);\n\n\t\t\tgl_FragColor = vec4(textureColor, strength * opacityProgress);\n\t\t}\n\t`;\n\nexport function NoiseReveal({\n\tsrc,\n\talt,\n\ttrigger = \"view\",\n\trevealed = false,\n\tduration = 1.5,\n\tdelay = 0,\n\tclassName = \"\",\n}: NoiseRevealProps) {\n\tconst [container, containerRef] = useElementRef<HTMLDivElement>();\n\n\t// State, not a ref: the two trigger effects below must re-run the moment the\n\t// render loop publishes its setter, which is exactly what the source's\n\t// `$state.raw` buys there.\n\tconst [setTarget, setSetTarget] = useState<((value: number) => void) | null>(null);\n\n\t// The source reads `revealed` through `untrack` when it seeds `uProgress`, so\n\t// a change to it must never rebuild the scene. A live ref reproduces that;\n\t// `revealed` in the dependency array would dispose and re-create the renderer\n\t// on every toggle.\n\tconst revealedRef = useLiveRef(revealed);\n\n\t// The whole scene is built and torn down inside one effect, exactly as the\n\t// source's `$effect` does. The dependency array is the set that `$effect`\n\t// reads reactively: `src` and `duration` are captured into locals at setup,\n\t// and `trigger` decides the seed value — any of the three changing disposes\n\t// the old scene and builds a new one.\n\tuseEffect(() => {\n\t\tif (!container) return;\n\n\t\tconst _src = src;\n\t\tconst _duration = duration;\n\n\t\tconst rect = container.getBoundingClientRect();\n\t\tconst width = rect.width || 1;\n\t\tconst height = rect.height || 1;\n\t\t// Ported as-is: `|| 1` above already excludes 0, so this guard never\n\t\t// fires. Kept so the port and the source stay line-for-line comparable.\n\t\tif (height === 0) return;\n\n\t\tconst scene = new THREE.Scene();\n\t\tscene.background = null;\n\n\t\tconst cameraDistance = 5;\n\t\tconst fov = 50;\n\t\tconst camera = new THREE.PerspectiveCamera(fov, width / height, 0.01, 1000);\n\t\tcamera.position.set(0, 0, cameraDistance);\n\n\t\tconst renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });\n\t\trenderer.setClearColor(0x000000, 0);\n\t\trenderer.setPixelRatio(window.devicePixelRatio);\n\t\trenderer.setSize(width, height, false);\n\t\trenderer.domElement.style.width = \"100%\";\n\t\trenderer.domElement.style.height = \"100%\";\n\t\tcontainer.appendChild(renderer.domElement);\n\n\t\tconst geometry = new THREE.PlaneGeometry(1, 1, 32, 32);\n\n\t\tconst material = new THREE.ShaderMaterial({\n\t\t\tuniforms: {\n\t\t\t\tuTexture: { value: null },\n\t\t\t\tuTime: { value: 0 },\n\t\t\t\tuProgress: { value: trigger === \"manual\" && revealedRef.current ? 1 : 0 },\n\t\t\t\tuRes: { value: new THREE.Vector2(1, 1) },\n\t\t\t\tuImageRes: { value: new THREE.Vector2(1, 1) },\n\t\t\t},\n\t\t\tvertexShader,\n\t\t\tfragmentShader,\n\t\t\ttransparent: true,\n\t\t});\n\n\t\tconst plane = new THREE.Mesh(geometry, material);\n\t\tscene.add(plane);\n\n\t\t// Scale the unit plane to exactly fill the camera frustum at z=0.\n\t\t// Non-null assertions on `uniforms`: the type definitions declare it as a\n\t\t// string index signature, so `noUncheckedIndexedAccess` widens every read\n\t\t// to `| undefined`. Every key below is declared in the literal above.\n\t\tconst fitPlane = (w: number, h: number) => {\n\t\t\tconst visibleH = 2 * Math.tan((fov * Math.PI) / 360) * cameraDistance;\n\t\t\tconst visibleW = visibleH * (w / h);\n\t\t\tplane.scale.set(visibleW, visibleH, 1);\n\t\t\t(material.uniforms.uRes!.value as THREE.Vector2).set(visibleW, visibleH);\n\t\t};\n\t\tfitPlane(width, height);\n\n\t\tlet texture: THREE.Texture | null = null;\n\t\tnew THREE.TextureLoader().load(_src, (loaded) => {\n\t\t\tloaded.colorSpace = THREE.SRGBColorSpace;\n\t\t\ttexture = loaded;\n\t\t\tmaterial.uniforms.uTexture!.value = loaded;\n\t\t\tconst image = loaded.image as HTMLImageElement;\n\t\t\t(material.uniforms.uImageRes!.value as THREE.Vector2).set(\n\t\t\t\timage.naturalWidth || 1,\n\t\t\t\timage.naturalHeight || 1\n\t\t\t);\n\t\t});\n\n\t\tconst reducedMotion = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n\t\tlet progress = material.uniforms.uProgress!.value as number;\n\t\tconst tween = { from: progress, to: progress, start: 0, active: false };\n\n\t\tconst target = (value: number) => {\n\t\t\tif (value === tween.to) return;\n\t\t\ttween.to = value;\n\t\t\tif (reducedMotion) {\n\t\t\t\tprogress = value;\n\t\t\t\ttween.active = false;\n\t\t\t\treturn;\n\t\t\t}\n\t\t\ttween.from = progress;\n\t\t\ttween.start = performance.now();\n\t\t\ttween.active = true;\n\t\t};\n\t\tsetSetTarget(() => target);\n\n\t\tconst handleResize = () => {\n\t\t\tconst r = container.getBoundingClientRect();\n\t\t\tif (r.height === 0) return;\n\t\t\tcamera.aspect = r.width / r.height;\n\t\t\tcamera.updateProjectionMatrix();\n\t\t\trenderer.setSize(r.width, r.height, false);\n\t\t\tfitPlane(r.width, r.height);\n\t\t};\n\n\t\twindow.addEventListener(\"resize\", handleResize);\n\n\t\tconst clock = new THREE.Clock();\n\t\t// Clock time that elapsed while the loop was stopped. Subtracted from uTime\n\t\t// so a resumed loop carries on from the noise phase it paused at instead of\n\t\t// jumping forward by the length of the pause.\n\t\tlet pausedTime = 0;\n\t\tlet stoppedAt: number | null = null;\n\n\t\t// A queued frame is the loop's only \"is it running\" state, so `0` is the idle\n\t\t// sentinel — a real frame id is never 0. `start()` is therefore safe to call\n\t\t// again while the loop is already running, and it paints the frame it starts\n\t\t// with synchronously, exactly as the first `animate()` call did.\n\t\tlet animationId = 0;\n\t\tconst animate = () => {\n\t\t\tanimationId = requestAnimationFrame(animate);\n\t\t\tif (tween.active) {\n\t\t\t\tconst t = getProgress(performance.now() - tween.start, _duration * 1000);\n\t\t\t\tprogress = lerp(tween.from, tween.to, easeInOutCubic(t));\n\t\t\t\tif (t >= 1) tween.active = false;\n\t\t\t}\n\t\t\tmaterial.uniforms.uProgress!.value = progress;\n\t\t\tmaterial.uniforms.uTime!.value = clock.getElapsedTime() - pausedTime;\n\t\t\trenderer.render(scene, camera);\n\t\t};\n\n\t\tconst start = () => {\n\t\t\tif (animationId !== 0) return;\n\t\t\tif (stoppedAt !== null) {\n\t\t\t\tpausedTime += clock.getElapsedTime() - stoppedAt;\n\t\t\t\tstoppedAt = null;\n\t\t\t}\n\t\t\tanimate();\n\t\t};\n\n\t\tconst stop = () => {\n\t\t\tif (animationId === 0) return;\n\t\t\tcancelAnimationFrame(animationId);\n\t\t\tanimationId = 0;\n\t\t\tstoppedAt = clock.getElapsedTime();\n\t\t};\n\n\t\tstart();\n\n\t\t// Every frame evaluates the noise twice per fragment at the device pixel ratio,\n\t\t// so a canvas that is off screen or in a background tab is pure cost. The mask\n\t\t// is computed from scratch each frame and keeps no state to rebuild, so the\n\t\t// loop can stop there and pick up where it left off. `threshold: 0` — any\n\t\t// sliver on screen counts as visible — and `once: false`, because this has to\n\t\t// keep reporting both ways.\n\t\t//\n\t\t// The loop still starts immediately rather than waiting for the first observer\n\t\t// callback: that callback lands a task later, and a canvas that is already on\n\t\t// screen should not hold a blank frame until then.\n\t\tlet onScreen = true;\n\t\tconst handleVisibility = () => {\n\t\t\tif (document.hidden) stop();\n\t\t\telse if (onScreen) start();\n\t\t};\n\t\tdocument.addEventListener(\"visibilitychange\", handleVisibility);\n\n\t\tconst inViewHandle = observeInView(container, {\n\t\t\tonce: false,\n\t\t\tthreshold: 0,\n\t\t\tonChange: (intersecting) => {\n\t\t\t\tonScreen = intersecting;\n\t\t\t\tif (intersecting && !document.hidden) start();\n\t\t\t\telse stop();\n\t\t\t},\n\t\t});\n\n\t\treturn () => {\n\t\t\tsetSetTarget(null);\n\t\t\twindow.removeEventListener(\"resize\", handleResize);\n\t\t\tdocument.removeEventListener(\"visibilitychange\", handleVisibility);\n\t\t\tinViewHandle.destroy();\n\t\t\tstop();\n\t\t\tif (renderer.domElement.parentNode === container) container.removeChild(renderer.domElement);\n\t\t\trenderer.dispose();\n\t\t\tgeometry.dispose();\n\t\t\tmaterial.dispose();\n\t\t\ttexture?.dispose();\n\t\t};\n\t}, [container, src, duration, trigger, revealedRef]);\n\n\t// Viewport trigger: reveal once on first intersection\n\tuseEffect(() => {\n\t\tif (trigger !== \"view\" || !container || !setTarget) return;\n\n\t\tconst set = setTarget;\n\t\tlet timeoutId = 0;\n\t\tconst observer = new IntersectionObserver(\n\t\t\t(entries) => {\n\t\t\t\tfor (const entry of entries) {\n\t\t\t\t\tif (entry.isIntersecting) {\n\t\t\t\t\t\tobserver.disconnect();\n\t\t\t\t\t\ttimeoutId = window.setTimeout(() => set(1), delay * 1000);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t},\n\t\t\t{ threshold: 0.1 }\n\t\t);\n\n\t\tobserver.observe(container);\n\n\t\treturn () => {\n\t\t\tobserver.disconnect();\n\t\t\tclearTimeout(timeoutId);\n\t\t};\n\t}, [trigger, container, setTarget, delay]);\n\n\t// Manual trigger: follow the `revealed` prop both ways\n\tuseEffect(() => {\n\t\tif (trigger !== \"manual\" || !setTarget) return;\n\t\tsetTarget(revealed ? 1 : 0);\n\t}, [trigger, setTarget, revealed]);\n\n\treturn (\n\t\t<div\n\t\t\tref={containerRef}\n\t\t\tclassName={cn(\"relative h-[400px] w-full\", className)}\n\t\t\trole={alt ? \"img\" : undefined}\n\t\t\taria-label={alt}\n\t\t/>\n\t);\n}\n","type":"registry:ui","target":"@ui/fancy/components/noise-reveal/NoiseReveal.tsx"},{"path":"react/src/components/noise-reveal/index.ts","content":"\"use client\";\n\nexport { NoiseReveal } from \"./NoiseReveal\";\nexport type { NoiseRevealProps } from \"./NoiseReveal\";\n","type":"registry:ui","target":"@ui/fancy/components/noise-reveal/index.ts"}],"meta":{"license":"MIT","package":"fancy-ui-react","version":"0.7.0","source":"https://fancy-ui.rama.app/docs/components/noise-reveal?framework=react"}}