{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "dot-screen-reveal",
  "title": "Dot Screen Reveal",
  "description": "A photo printed as a halftone dot screen that develops into the real image under your pointer: dots swell into each other and melt away in a soft window that follows the cursor, then the screen slowly grows back behind it.",
  "dependencies": [
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/dot-screen-reveal/dot-screen-reveal.tsx",
      "content": "\"use client\";\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 interface DotScreenRevealProps extends Omit<React.ComponentPropsWithoutRef<\"div\">, \"children\"> {\n  /** The image under the dot screen. Cross-origin images need CORS headers for colour dots. */\n  src: string;\n  alt?: string;\n  /** Distance between dots, in px. */\n  spacing?: number;\n  /** Radius of the window the pointer opens, in px. */\n  radius?: number;\n  /** \"color\" dots take the image's colours; \"mono\" dots are all one ink colour. */\n  variant?: \"color\" | \"mono\";\n  /** Colour of the paper behind the dots. */\n  paper?: string;\n  /** Colour of the dots in the mono variant. */\n  ink?: string;\n  /** How long the screen takes to grow back over a revealed area, in ms (0 = right away). */\n  heal?: number;\n  children?: React.ReactNode;\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\nfunction rgb(hex: string): [number, number, number] {\n  const h = hex.replace(\"#\", \"\");\n  const full = h.length === 3 ? h.split(\"\").map((c) => c + c).join(\"\") : h.slice(0, 6);\n  const n = parseInt(full, 16) || 0;\n  return [(n >> 16) & 255, (n >> 8) & 255, n & 255];\n}\n\nconst smooth = (a: number, b: number, x: number) => {\n  const t = Math.min(1, Math.max(0, (x - a) / (b - a)));\n  return t * t * (3 - 2 * t);\n};\n\nexport function DotScreenReveal({ src, alt = \"\", spacing = 10, radius = 150, variant = \"color\", paper = \"#0b0b0d\", ink = \"#f2efe6\", heal = 1600, className, style, children, ...props }: DotScreenRevealProps) {\n  const reduce = useReducedMotion();\n  const rootRef = React.useRef<HTMLDivElement>(null);\n  const canvasRef = React.useRef<HTMLCanvasElement>(null);\n  const live = React.useRef({ spacing, radius, variant, paper, ink, heal });\n  const kick = React.useRef<() => void>(() => {});\n  React.useEffect(() => {\n    live.current = { spacing, radius, variant, paper, ink, heal };\n    kick.current();\n  }, [spacing, radius, variant, paper, ink, heal]);\n\n  React.useEffect(() => {\n    const root = rootRef.current;\n    const canvas = canvasRef.current;\n    const ctx = canvas?.getContext(\"2d\");\n    if (!root || !canvas || !ctx) return;\n    const img = new Image();\n    img.crossOrigin = \"anonymous\";\n    img.decoding = \"async\";\n    // The image sampled down to one pixel per dot (RGBA), and how revealed each dot is (0–1).\n    let samples: Uint8ClampedArray | null = null;\n    let tainted = false;\n    let reveal = new Float32Array(0);\n    let cols = 0;\n    let rows = 0;\n    let w = 0;\n    let h = 0;\n    let builtFor = \"\";\n    // A tiny canvas, one pixel per dot, holding the paper's opacity; drawn scaled up with smoothing,\n    // it fades the paper softly instead of cell by cell.\n    const mask = document.createElement(\"canvas\");\n    const mctx = mask.getContext(\"2d\");\n    let mdata: ImageData | null = null;\n    const pointer = { x: 0, y: 0, over: false };\n    let raf = 0;\n    let visible = true;\n    let last = 0;\n\n    const sample = () => {\n      const L = live.current;\n      const key = `${w}x${h}@${L.spacing}:${img.complete && img.naturalWidth}`;\n      if (key === builtFor) return;\n      builtFor = key;\n      cols = Math.max(1, Math.ceil(w / L.spacing));\n      rows = Math.max(1, Math.ceil(h / L.spacing));\n      reveal = new Float32Array(cols * rows);\n      mask.width = cols;\n      mask.height = rows;\n      mdata = mctx ? mctx.createImageData(cols, rows) : null;\n      samples = null;\n      if (!img.naturalWidth) return;\n      // Sample with the same crop as object-fit: cover, so each dot sits over the part of the photo it stands for.\n      const off = document.createElement(\"canvas\");\n      off.width = cols;\n      off.height = rows;\n      const octx = off.getContext(\"2d\", { willReadFrequently: true });\n      if (!octx) return;\n      const scale = Math.max(w / img.naturalWidth, h / img.naturalHeight);\n      const sw = w / scale;\n      const sh = h / scale;\n      octx.drawImage(img, (img.naturalWidth - sw) / 2, (img.naturalHeight - sh) / 2, sw, sh, 0, 0, w / L.spacing, h / L.spacing);\n      try {\n        samples = octx.getImageData(0, 0, cols, rows).data;\n      } catch {\n        // A cross-origin image without CORS can't be read: fall back to even ink dots.\n        tainted = true;\n      }\n    };\n\n    const resize = () => {\n      const dpr = Math.min(window.devicePixelRatio || 1, 2);\n      w = root.clientWidth;\n      h = root.clientHeight;\n      canvas.width = Math.round(w * dpr);\n      canvas.height = Math.round(h * dpr);\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n    };\n\n    // Returns true while anything is still changing.\n    const step = (dt: number) => {\n      const L = live.current;\n      sample();\n      let busy = pointer.over;\n      const healRate = reduce || L.heal <= 0 ? Infinity : 1000 / L.heal;\n      const r = L.radius;\n      for (let j = 0; j < rows; j++)\n        for (let i = 0; i < cols; i++) {\n          const k = j * cols + i;\n          let target = 0;\n          if (pointer.over) {\n            const dx = (i + 0.5) * L.spacing - pointer.x;\n            const dy = (j + 0.5) * L.spacing - pointer.y;\n            target = 1 - smooth(r * 0.45, r, Math.sqrt(dx * dx + dy * dy));\n          }\n          const v = reveal[k];\n          // Opens quickly toward the pointer, then grows back slowly once it has moved on.\n          let next = target > v ? (reduce ? target : v + (target - v) * (1 - Math.exp(-dt * 14))) : healRate === Infinity ? target : Math.max(target, v - dt * healRate);\n          if (next < 0.002) next = 0;\n          reveal[k] = next;\n          if (next > 0) busy = true;\n        }\n      return busy;\n    };\n\n    const draw = () => {\n      const L = live.current;\n      ctx.clearRect(0, 0, w, h);\n      if (!cols) return;\n      const [pr, pg, pb] = rgb(L.paper);\n      const paperLum = (0.299 * pr + 0.587 * pg + 0.114 * pb) / 255;\n      // Paper: opaque except where revealed.\n      if (mdata && mctx) {\n        const d = mdata.data;\n        for (let k = 0; k < reveal.length; k++) {\n          d[k * 4] = pr;\n          d[k * 4 + 1] = pg;\n          d[k * 4 + 2] = pb;\n          d[k * 4 + 3] = 255 * (1 - smooth(0.25, 0.95, reveal[k]));\n        }\n        mctx.putImageData(mdata, 0, 0);\n        ctx.imageSmoothingEnabled = true;\n        ctx.drawImage(mask, 0, 0, cols * L.spacing, rows * L.spacing);\n      } else {\n        ctx.fillStyle = L.paper;\n        ctx.fillRect(0, 0, w, h);\n      }\n      const max = L.spacing * 0.5;\n      const mono = L.variant === \"mono\" || !samples || tainted;\n      if (mono) ctx.fillStyle = L.ink;\n      for (let j = 0; j < rows; j++)\n        for (let i = 0; i < cols; i++) {\n          const k = j * cols + i;\n          const rv = reveal[k];\n          let r = 0;\n          let g = 0;\n          let b = 0;\n          let lum = 0.5;\n          if (samples) {\n            r = samples[k * 4];\n            g = samples[k * 4 + 1];\n            b = samples[k * 4 + 2];\n            lum = (0.299 * r + 0.587 * g + 0.114 * b) / 255;\n          }\n          // Classic halftone: on dark paper bright areas get big dots, on light paper dark areas do.\n          const tone = paperLum < 0.5 ? lum : 1 - lum;\n          // Near the window the dots swell into each other, then fade, so the photo seems to develop out of the screen.\n          const swell = 1 + smooth(0, 0.6, rv) * 0.9;\n          const rad = Math.min(max * 1.42, max * Math.sqrt(Math.max(0, tone)) * 0.98 * swell);\n          const alpha = 1 - smooth(0.5, 0.95, rv);\n          if (rad < 0.35 || alpha <= 0) continue;\n          ctx.globalAlpha = alpha;\n          if (!mono) ctx.fillStyle = `rgb(${r},${g},${b})`;\n          ctx.beginPath();\n          ctx.arc((i + 0.5) * L.spacing, (j + 0.5) * L.spacing, rad, 0, Math.PI * 2);\n          ctx.fill();\n        }\n      ctx.globalAlpha = 1;\n    };\n\n    const frame = (now: number) => {\n      const dt = Math.min(0.05, last ? (now - last) / 1000 : 1 / 60);\n      last = now;\n      const busy = step(dt);\n      draw();\n      // Idle and fully healed: stop until the pointer comes back.\n      if (busy && visible && !document.hidden) raf = requestAnimationFrame(frame);\n      else {\n        raf = 0;\n        last = 0;\n      }\n    };\n    const start = () => {\n      if (raf) return;\n      raf = requestAnimationFrame(frame);\n    };\n    const restart = () => {\n      cancelAnimationFrame(raf);\n      raf = 0;\n      builtFor = \"\";\n      start();\n    };\n    kick.current = restart;\n\n    const local = (event: PointerEvent) => {\n      const r = root.getBoundingClientRect();\n      const s = r.width / root.offsetWidth || 1;\n      return { x: (event.clientX - r.left) / s, y: (event.clientY - r.top) / s };\n    };\n    const onMove = (event: PointerEvent) => {\n      const p = local(event);\n      pointer.x = p.x;\n      pointer.y = p.y;\n      pointer.over = true;\n      start();\n    };\n    const onLeave = () => {\n      pointer.over = false;\n      start();\n    };\n    img.onload = restart;\n    img.src = src;\n    if (img.complete) restart();\n    const ro = new ResizeObserver(() => {\n      resize();\n      restart();\n    });\n    ro.observe(root);\n    const io = new IntersectionObserver(([entry]) => {\n      visible = entry.isIntersecting;\n      if (visible) start();\n    });\n    io.observe(root);\n    root.addEventListener(\"pointermove\", onMove);\n    root.addEventListener(\"pointerdown\", onMove);\n    root.addEventListener(\"pointerleave\", onLeave);\n    root.addEventListener(\"pointercancel\", onLeave);\n    resize();\n    return () => {\n      cancelAnimationFrame(raf);\n      kick.current = () => {};\n      img.onload = null;\n      ro.disconnect();\n      io.disconnect();\n      root.removeEventListener(\"pointermove\", onMove);\n      root.removeEventListener(\"pointerdown\", onMove);\n      root.removeEventListener(\"pointerleave\", onLeave);\n      root.removeEventListener(\"pointercancel\", onLeave);\n    };\n  }, [src, reduce]);\n\n  return (\n    <div ref={rootRef} className={cn(\"relative isolate overflow-hidden\", className)} style={{ background: paper, ...style }} {...props}>\n      {/* eslint-disable-next-line @next/next/no-img-element */}\n      <img src={src} alt={alt} draggable={false} className=\"pointer-events-none absolute inset-0 -z-20 h-full w-full object-cover select-none\" />\n      <canvas ref={canvasRef} aria-hidden=\"true\" className=\"pointer-events-none absolute inset-0 -z-10 h-full w-full\" />\n      {children}\n    </div>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}