{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"print-dots","type":"registry:ui","title":"Print Dots","description":"Renders an image as breathing halftone dots that swell and melt into the full photo around the cursor.","dependencies":[],"registryDependencies":["utils"],"files":[{"path":"registry/ui/print-dots.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useEffect, useRef } from \"react\";\nimport { cn } from \"@/lib/utils\";\n\nexport interface PrintDotsProps {\n  /** Image URL (must allow CORS so its pixels can be sampled). */\n  src: string;\n  alt?: string;\n  /** Halftone cell size in px. */\n  cellSize?: number;\n  /** Px radius of the full-image reveal around the cursor. */\n  radius?: number;\n  /** Dots take the image colors; off paints them with dotColor. */\n  colored?: boolean;\n  dotColor?: string;\n  className?: string;\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 PrintDots({\n  src,\n  alt = \"\",\n  cellSize = 11,\n  radius = 170,\n  colored = true,\n  dotColor = \"#c6ff3d\",\n  className,\n}: PrintDotsProps) {\n  const ref = useRef<HTMLCanvasElement>(null);\n\n  useEffect(() => {\n    const canvas = ref.current!;\n    const ctx = canvas.getContext(\"2d\")!;\n    const full = document.createElement(\"canvas\");\n    const fctx = full.getContext(\"2d\")!;\n    const reveal = document.createElement(\"canvas\");\n    const rctx = reveal.getContext(\"2d\")!;\n    const reduced = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const img = new Image();\n    img.crossOrigin = \"anonymous\";\n    let w = 0;\n    let h = 0;\n    let dpr = 1;\n    let cols = 0;\n    let rows = 0;\n    let rgb = new Uint8ClampedArray(0);\n    let ready = false;\n    let raf = 0;\n    let visible = true;\n    const m = { x: 0, y: 0, tx: 0, ty: 0, on: 0, ton: 0 };\n\n    // Cover-fit the image into a w x h box.\n    const cover = (c: CanvasRenderingContext2D, bw: number, bh: number) => {\n      const s = Math.max(bw / img.naturalWidth, bh / img.naturalHeight);\n      const iw = img.naturalWidth * s;\n      const ih = img.naturalHeight * s;\n      c.drawImage(img, (bw - iw) / 2, (bh - ih) / 2, iw, ih);\n    };\n\n    const build = () => {\n      if (!img.complete || !img.naturalWidth || !w) return;\n      full.width = reveal.width = canvas.width;\n      full.height = reveal.height = canvas.height;\n      cover(fctx, full.width, full.height);\n      // Average each cell by letting the browser downsample the image to one pixel per cell.\n      cols = Math.ceil(w / cellSize);\n      rows = Math.ceil(h / cellSize);\n      const small = document.createElement(\"canvas\");\n      small.width = cols;\n      small.height = rows;\n      const sctx = small.getContext(\"2d\", { willReadFrequently: true })!;\n      sctx.save();\n      sctx.scale(1 / cellSize, 1 / cellSize);\n      cover(sctx, cols * cellSize, rows * cellSize);\n      sctx.restore();\n      rgb = sctx.getImageData(0, 0, cols, rows).data;\n      ready = true;\n    };\n\n    const draw = (time: number) => {\n      m.x += (m.tx - m.x) * 0.14;\n      m.y += (m.ty - m.y) * 0.14;\n      m.on += (m.ton - m.on) * 0.08;\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n      ctx.clearRect(0, 0, w, h);\n      if (!ready) return;\n      const half = cellSize / 2;\n      const t = time / 1000;\n      if (!colored) {\n        ctx.fillStyle = dotColor;\n        ctx.beginPath();\n      }\n      for (let j = 0; j < rows; j++) {\n        for (let i = 0; i < cols; i++) {\n          const k = (j * cols + i) * 4;\n          const lum = (0.299 * rgb[k] + 0.587 * rgb[k + 1] + 0.114 * rgb[k + 2]) / 255;\n          const x = i * cellSize + half;\n          const y = j * cellSize + half;\n          const near = m.on * smooth(radius, radius * 0.3, Math.hypot(x - m.x, y - m.y));\n          const wave = reduced ? 1 : 0.86 + 0.14 * Math.sin(t * 1.6 - (x + y) * 0.012);\n          let r = half * 0.95 * Math.sqrt(lum) * wave;\n          r += (cellSize * 0.72 - r) * near;\n          if (r < 0.4) continue;\n          if (colored) {\n            ctx.fillStyle = `rgb(${rgb[k]},${rgb[k + 1]},${rgb[k + 2]})`;\n            ctx.beginPath();\n            ctx.arc(x, y, r, 0, Math.PI * 2);\n            ctx.fill();\n          } else {\n            ctx.moveTo(x + r, y);\n            ctx.arc(x, y, r, 0, Math.PI * 2);\n          }\n        }\n      }\n      if (!colored) ctx.fill();\n\n      if (m.on > 0.01) {\n        // Punch a soft circle of the real image in around the cursor.\n        rctx.globalCompositeOperation = \"source-over\";\n        rctx.clearRect(0, 0, reveal.width, reveal.height);\n        rctx.drawImage(full, 0, 0);\n        rctx.globalCompositeOperation = \"destination-in\";\n        const g = rctx.createRadialGradient(m.x * dpr, m.y * dpr, 0, m.x * dpr, m.y * dpr, radius * dpr);\n        g.addColorStop(0, `rgba(0,0,0,${m.on})`);\n        g.addColorStop(0.45, `rgba(0,0,0,${m.on * 0.9})`);\n        g.addColorStop(1, \"rgba(0,0,0,0)\");\n        rctx.fillStyle = g;\n        rctx.fillRect(0, 0, reveal.width, reveal.height);\n        ctx.setTransform(1, 0, 0, 1, 0, 0);\n        ctx.drawImage(reveal, 0, 0);\n      }\n    };\n\n    const loop = (time: number) => {\n      draw(time);\n      const busy = !reduced || Math.abs(m.ton - m.on) > 0.002 || Math.abs(m.tx - m.x) + Math.abs(m.ty - m.y) > 0.3;\n      raf = visible && busy ? requestAnimationFrame(loop) : 0;\n    };\n    const kick = () => {\n      if (!raf && visible) raf = requestAnimationFrame(loop);\n    };\n\n    const resize = () => {\n      dpr = Math.min(window.devicePixelRatio || 1, 2);\n      w = canvas.offsetWidth;\n      h = canvas.offsetHeight;\n      canvas.width = Math.round(w * dpr);\n      canvas.height = Math.round(h * dpr);\n      build();\n      kick();\n    };\n\n    const onMove = (e: PointerEvent) => {\n      const r = canvas.getBoundingClientRect();\n      m.tx = e.clientX - r.left;\n      m.ty = e.clientY - r.top;\n      if (m.on < 0.02) {\n        m.x = m.tx;\n        m.y = m.ty;\n      }\n      m.ton = 1;\n      kick();\n    };\n    const onLeave = () => {\n      m.ton = 0;\n      kick();\n    };\n\n    img.onload = () => {\n      build();\n      kick();\n    };\n    img.src = src;\n\n    const ro = new ResizeObserver(resize);\n    ro.observe(canvas);\n    const io = new IntersectionObserver(([entry]) => {\n      visible = entry.isIntersecting;\n      kick();\n    });\n    io.observe(canvas);\n    canvas.addEventListener(\"pointermove\", onMove, { passive: true });\n    canvas.addEventListener(\"pointerleave\", onLeave);\n    return () => {\n      img.onload = null;\n      cancelAnimationFrame(raf);\n      ro.disconnect();\n      io.disconnect();\n      canvas.removeEventListener(\"pointermove\", onMove);\n      canvas.removeEventListener(\"pointerleave\", onLeave);\n    };\n  }, [src, cellSize, radius, colored, dotColor]);\n\n  return (\n    <div className={cn(\"relative overflow-hidden\", className)}>\n      <canvas ref={ref} role=\"img\" aria-label={alt} style={{ position: \"absolute\", inset: 0, width: \"100%\", height: \"100%\" }} />\n    </div>\n  );\n}\n"}]}