{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "distorted-photo-card",
  "title": "Distorted Photo Card",
  "description": "A photo card with a liquid WebGL lens that follows the pointer: the image swells under it, ripples outward, smears and splits into RGB when you move fast, with a hairline ring tracking the lens, lazy WebGL setup and a plain-image fallback.",
  "dependencies": [
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/distorted-photo-card/distorted-photo-card.tsx",
      "content": "\"use client\";\n/* eslint-disable @next/next/no-img-element -- Registry components work in any React app. */\n\nimport * as React from \"react\";\nimport { clsx, type ClassValue } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nfunction cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\nexport type DistortedPhotoTheme = \"dark\" | \"light\";\n\nexport interface DistortedPhotoCardProps extends Omit<React.ComponentPropsWithoutRef<\"div\">, \"title\" | \"children\"> {\n  image?: string;\n  imageAlt?: string;\n  title?: string;\n  subtitle?: string;\n  /** Small mono label top-left. */\n  label?: string;\n  href?: string;\n  /** Lens radius as a fraction of the card height. */\n  radius?: number;\n  /** How strongly the lens magnifies, 0–1. */\n  strength?: number;\n  /** RGB split that follows fast pointer movement, 0–1. */\n  aberration?: number;\n  /** Rings rippling out from the lens. */\n  ripples?: boolean;\n  /** Show the hairline ring that follows the lens. */\n  showRing?: boolean;\n  /** Width / height of the card. */\n  aspectRatio?: number;\n  cornerRadius?: number;\n  theme?: DistortedPhotoTheme;\n}\n\nconst VERTEX = `\nattribute vec2 aPos;\nvarying vec2 vUv;\nvoid main() {\n  vUv = aPos * 0.5 + 0.5;\n  gl_Position = vec4(aPos, 0.0, 1.0);\n}`;\n\nconst FRAGMENT = `\nprecision mediump float;\nuniform sampler2D uTex;\nuniform vec2 uRes;\nuniform vec2 uImg;\nuniform vec2 uMouse;\nuniform vec2 uVel;\nuniform float uHover;\nuniform float uRadius;\nuniform float uStrength;\nuniform float uAberration;\nuniform float uRipple;\nuniform float uTime;\nvarying vec2 vUv;\n\n// object-fit: cover, in texture space.\nvec2 cover(vec2 uv) {\n  vec2 s = uRes / uImg;\n  float k = max(s.x, s.y);\n  vec2 size = uImg * k;\n  return (uv * uRes + (size - uRes) * 0.5) / size;\n}\n\nvoid main() {\n  vec2 uv = vUv;\n  vec2 aspect = vec2(uRes.x / uRes.y, 1.0);\n  vec2 toMouse = uv - uMouse;\n  float dist = length(toMouse * aspect);\n  float lens = smoothstep(uRadius, 0.0, dist) * uHover;\n\n  // Pull samples toward the lens centre (magnify), drag them against the pointer's motion, and add rings.\n  vec2 p = uv - toMouse * lens * uStrength;\n  p -= uVel * lens * 0.55;\n  p += toMouse * sin(dist * 46.0 - uTime * 3.2) * 0.012 * lens * uRipple;\n\n  float split = (length(uVel) * 0.9 + 0.0035) * lens * uAberration;\n  vec2 dir = length(uVel) > 0.0001 ? normalize(uVel) : vec2(1.0, 0.0);\n  float r = texture2D(uTex, cover(p + dir * split)).r;\n  float g = texture2D(uTex, cover(p)).g;\n  float b = texture2D(uTex, cover(p - dir * split)).b;\n  gl_FragColor = vec4(r, g, b, 1.0);\n}`;\n\nfunction compile(gl: WebGLRenderingContext, type: number, source: string) {\n  const shader = gl.createShader(type);\n  if (!shader) return null;\n  gl.shaderSource(shader, source);\n  gl.compileShader(shader);\n  if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {\n    gl.deleteShader(shader);\n    return null;\n  }\n  return shader;\n}\n\nfunction useReducedMotion() {\n  return React.useSyncExternalStore(\n    (callback) => {\n      const query = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n      query.addEventListener(\"change\", callback);\n      return () => query.removeEventListener(\"change\", callback);\n    },\n    () => window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches,\n    () => false,\n  );\n}\n\ntype Renderer = { draw: (time: number) => void; resize: () => void; dispose: () => void };\n\nexport function DistortedPhotoCard({\n  image = \"/demo/47.webp\",\n  imageAlt = \"\",\n  title = \"Afterimage\",\n  subtitle = \"Portrait study · 2026\",\n  label = \"No. 01\",\n  href,\n  radius = 0.32,\n  strength = 0.42,\n  aberration = 0.8,\n  ripples = true,\n  showRing = true,\n  aspectRatio = 4 / 5,\n  cornerRadius = 18,\n  theme = \"dark\",\n  className,\n  style,\n  ...props\n}: DistortedPhotoCardProps) {\n  const reduce = useReducedMotion();\n  const boxRef = React.useRef<HTMLDivElement>(null);\n  const canvasRef = React.useRef<HTMLCanvasElement>(null);\n  const ringRef = React.useRef<HTMLDivElement>(null);\n  const renderer = React.useRef<Renderer | null>(null);\n  const failed = React.useRef(false);\n  const raf = React.useRef(0);\n  // Pointer state, all in 0..1 card space: target and smoothed position, velocity, and hover amount.\n  const s = React.useRef({ tx: 0.5, ty: 0.5, x: 0.5, y: 0.5, vx: 0, vy: 0, hover: 0, target: 0, last: 0 });\n  const [live, setLive] = React.useState(false);\n  const settings = React.useRef({ radius, strength, aberration, ripples });\n  React.useEffect(() => {\n    settings.current = { radius, strength, aberration, ripples };\n  }, [radius, strength, aberration, ripples]);\n\n  // One frame of the smoothing + render loop. It stops itself once the lens has faded and the pointer is still.\n  const loop = React.useCallback(() => {\n    cancelAnimationFrame(raf.current);\n    const tick = (now: number) => {\n      const st = s.current;\n      const dt = Math.min(0.05, st.last ? (now - st.last) / 1000 : 1 / 60);\n      st.last = now;\n      const follow = 1 - Math.exp(-dt * 14);\n      const px = st.x;\n      const py = st.y;\n      st.x += (st.tx - st.x) * follow;\n      st.y += (st.ty - st.y) * follow;\n      // Velocity decays toward the latest movement, so a flick leaves a short, fading smear.\n      const decay = 1 - Math.exp(-dt * 9);\n      st.vx += ((st.x - px) * 4 - st.vx) * decay;\n      st.vy += ((st.y - py) * 4 - st.vy) * decay;\n      st.hover += (st.target - st.hover) * (1 - Math.exp(-dt * 7));\n      renderer.current?.draw(now);\n      if (ringRef.current) {\n        ringRef.current.style.transform = `translate(${st.x * 100}%, ${st.y * 100}%)`;\n        ringRef.current.style.opacity = String(st.hover);\n      }\n      const moving = Math.abs(st.tx - st.x) + Math.abs(st.ty - st.y) + Math.abs(st.vx) + Math.abs(st.vy) > 0.0005;\n      const fading = Math.abs(st.target - st.hover) > 0.002;\n      if (moving || fading || st.target > 0) raf.current = requestAnimationFrame(tick);\n      else {\n        st.hover = st.target;\n        st.last = 0;\n        renderer.current?.draw(now);\n      }\n    };\n    raf.current = requestAnimationFrame(tick);\n  }, []);\n\n  const setup = React.useCallback((): Renderer | null => {\n    const canvas = canvasRef.current;\n    const box = boxRef.current;\n    if (!canvas || !box) return null;\n    const gl = canvas.getContext(\"webgl\", { premultipliedAlpha: false, antialias: false, preserveDrawingBuffer: true });\n    if (!gl) return null;\n    const vs = compile(gl, gl.VERTEX_SHADER, VERTEX);\n    const fs = compile(gl, gl.FRAGMENT_SHADER, FRAGMENT);\n    const program = gl.createProgram();\n    if (!vs || !fs || !program) return null;\n    gl.attachShader(program, vs);\n    gl.attachShader(program, fs);\n    gl.linkProgram(program);\n    if (!gl.getProgramParameter(program, gl.LINK_STATUS)) return null;\n    gl.useProgram(program);\n\n    const buffer = gl.createBuffer();\n    gl.bindBuffer(gl.ARRAY_BUFFER, buffer);\n    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 1, -1, -1, 1, 1, 1]), gl.STATIC_DRAW);\n    const aPos = gl.getAttribLocation(program, \"aPos\");\n    gl.enableVertexAttribArray(aPos);\n    gl.vertexAttribPointer(aPos, 2, gl.FLOAT, false, 0, 0);\n\n    const u = (name: string) => gl.getUniformLocation(program, name);\n    const uni = { res: u(\"uRes\"), img: u(\"uImg\"), mouse: u(\"uMouse\"), vel: u(\"uVel\"), hover: u(\"uHover\"), radius: u(\"uRadius\"), strength: u(\"uStrength\"), aberration: u(\"uAberration\"), ripple: u(\"uRipple\"), time: u(\"uTime\") };\n\n    const texture = gl.createTexture();\n    gl.bindTexture(gl.TEXTURE_2D, texture);\n    gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);\n    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);\n    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);\n    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);\n    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);\n    let imgSize: [number, number] | null = null;\n\n    const img = new Image();\n    img.crossOrigin = \"anonymous\";\n    img.onload = () => {\n      try {\n        gl.bindTexture(gl.TEXTURE_2D, texture);\n        gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, img);\n        imgSize = [img.naturalWidth, img.naturalHeight];\n        setLive(true);\n        loop();\n      } catch {\n        // A cross-origin image without CORS headers can't be uploaded; the plain <img> stays.\n        failed.current = true;\n      }\n    };\n    img.onerror = () => {\n      failed.current = true;\n    };\n    img.src = image;\n\n    const resize = () => {\n      const dpr = Math.min(window.devicePixelRatio || 1, 2);\n      canvas.width = Math.max(1, Math.round(box.clientWidth * dpr));\n      canvas.height = Math.max(1, Math.round(box.clientHeight * dpr));\n      gl.viewport(0, 0, canvas.width, canvas.height);\n    };\n    resize();\n\n    const draw = (time: number) => {\n      if (!imgSize) return;\n      const st = s.current;\n      const cfg = settings.current;\n      gl.uniform2f(uni.res, canvas.width, canvas.height);\n      gl.uniform2f(uni.img, imgSize[0], imgSize[1]);\n      gl.uniform2f(uni.mouse, st.x, 1 - st.y);\n      gl.uniform2f(uni.vel, st.vx, -st.vy);\n      gl.uniform1f(uni.hover, st.hover);\n      gl.uniform1f(uni.radius, cfg.radius);\n      gl.uniform1f(uni.strength, cfg.strength);\n      gl.uniform1f(uni.aberration, cfg.aberration);\n      gl.uniform1f(uni.ripple, cfg.ripples ? 1 : 0);\n      gl.uniform1f(uni.time, time / 1000);\n      gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);\n    };\n\n    canvas.addEventListener(\"webglcontextlost\", () => {\n      failed.current = true;\n      setLive(false);\n    });\n\n    return {\n      draw,\n      resize,\n      dispose: () => {\n        gl.deleteTexture(texture);\n        gl.deleteBuffer(buffer);\n        gl.deleteProgram(program);\n        gl.getExtension(\"WEBGL_lose_context\")?.loseContext();\n      },\n    };\n  }, [image, loop]);\n\n\n  React.useEffect(() => {\n    const box = boxRef.current;\n    if (!box) return;\n    const observer = new ResizeObserver(() => {\n      renderer.current?.resize();\n      renderer.current?.draw(performance.now());\n    });\n    observer.observe(box);\n    return () => {\n      observer.disconnect();\n      cancelAnimationFrame(raf.current);\n      renderer.current?.dispose();\n      renderer.current = null;\n    };\n  }, []);\n\n  // The WebGL context is only created on first interaction, so a page full of cards doesn't use up contexts.\n  const activate = () => {\n    if (reduce || failed.current) return false;\n    if (!renderer.current) {\n      renderer.current = setup();\n      if (!renderer.current) {\n        failed.current = true;\n        return false;\n      }\n    }\n    return true;\n  };\n\n  const point = (event: React.PointerEvent) => {\n    const rect = event.currentTarget.getBoundingClientRect();\n    const st = s.current;\n    st.tx = (event.clientX - rect.left) / rect.width;\n    st.ty = (event.clientY - rect.top) / rect.height;\n  };\n\n  const enter = (event: React.PointerEvent) => {\n    if (!activate()) return;\n    point(event);\n    const st = s.current;\n    // Start the lens where the pointer came in, not in the middle.\n    if (st.hover < 0.05) {\n      st.x = st.tx;\n      st.y = st.ty;\n    }\n    st.target = 1;\n    loop();\n  };\n  const leave = () => {\n    s.current.target = 0;\n    loop();\n  };\n\n  const Root = href ? \"a\" : \"div\";\n  const light = theme === \"light\";\n\n  return (\n    <div className={cn(\"relative w-full\", className)} style={{ aspectRatio, ...style }} {...props}>\n      <Root\n        {...(href ? { href } : { role: \"group\", \"aria-label\": title })}\n        className=\"group/dp absolute inset-0 block overflow-hidden outline-none select-none focus-visible:ring-2 focus-visible:ring-offset-2\"\n        style={{\n          borderRadius: cornerRadius,\n          background: light ? \"#e6e6e3\" : \"#151515\",\n          touchAction: \"pan-y\",\n          [\"--tw-ring-color\" as string]: light ? \"#141414\" : \"#f4f4f2\",\n          [\"--tw-ring-offset-color\" as string]: light ? \"#f1f1ef\" : \"#0a0a0a\",\n        }}\n        onPointerEnter={enter}\n        onPointerDown={(event: React.PointerEvent) => event.pointerType !== \"mouse\" && enter(event)}\n        onPointerMove={(event: React.PointerEvent) => {\n          point(event);\n          if (s.current.target > 0) loop();\n        }}\n        onPointerLeave={leave}\n        onPointerUp={(event: React.PointerEvent) => event.pointerType !== \"mouse\" && leave()}\n        onPointerCancel={leave}\n      >\n        <div ref={boxRef} className=\"absolute inset-0\">\n          <img src={image} alt={imageAlt} draggable={false} className=\"absolute inset-0 h-full w-full object-cover\" />\n          <canvas ref={canvasRef} aria-hidden=\"true\" className=\"absolute inset-0 h-full w-full transition-opacity duration-300\" style={{ opacity: live ? 1 : 0 }} />\n        </div>\n\n        {showRing && !reduce && (\n          <div aria-hidden=\"true\" className=\"pointer-events-none absolute inset-0\">\n            {/* Positioned by percentage of the card, then centred on the lens; size follows the radius prop. */}\n            <div ref={ringRef} className=\"absolute top-0 left-0 h-full w-full opacity-0\">\n              <div\n                className=\"absolute rounded-full border border-white/50 mix-blend-difference\"\n                style={{ width: `${radius * 200 * (1 / aspectRatio)}%`, aspectRatio: 1, left: 0, top: 0, translate: \"-50% -50%\" }}\n              >\n                <span className=\"absolute top-1/2 left-1/2 size-1 -translate-x-1/2 -translate-y-1/2 rounded-full bg-white/80\" />\n              </div>\n            </div>\n          </div>\n        )}\n\n        <div aria-hidden=\"true\" className=\"pointer-events-none absolute inset-0 bg-gradient-to-t from-black/70 via-transparent to-black/20\" />\n        <div className=\"pointer-events-none absolute inset-x-0 top-0 flex items-center justify-between p-4 font-mono text-[10px] text-white\">\n          {label && <span className=\"rounded-full bg-black/35 px-2.5 py-1 backdrop-blur-md\">{label}</span>}\n        </div>\n        <div className=\"pointer-events-none absolute inset-x-0 bottom-0 p-5 text-white\">\n          <h3 className=\"text-[20px] leading-tight font-medium tracking-[-0.01em]\">{title}</h3>\n          {subtitle && <p className=\"mt-1 text-[12.5px] text-white/70\">{subtitle}</p>}\n        </div>\n      </Root>\n    </div>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}