{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "dot-image-slider",
  "title": "Dot Image Slider",
  "description": "A before/after image reveal, a luminance-mapped halftone dot rendering on one side, the real photo on the other, dragged apart by a glowing lightsaber-style divider handle.",
  "dependencies": [
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/dot-image-slider/dot-image-slider.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 DotImageSliderProps extends Omit<React.ComponentPropsWithoutRef<\"div\">, \"children\"> {\n  image?: string;\n  dotSpacing?: number;\n  dotMaxSize?: number;\n  dotColor?: string;\n  backgroundColor?: string;\n  saberColor?: string;\n}\n\nfunction coverDims(nw: number, nh: number, cw: number, ch: number) {\n  const scale = Math.max(cw / nw, ch / nh);\n  const w = nw * scale;\n  const h = nh * scale;\n  return { w, h, x: (cw - w) / 2, y: (ch - h) / 2 };\n}\n\nconst DEFAULT_IMAGE = \"/demo/dot-image-slider.webp\";\n\nexport function DotImageSlider({\n  image = DEFAULT_IMAGE,\n  dotSpacing = 4,\n  dotMaxSize = 3,\n  dotColor = \"#1a1205\",\n  backgroundColor = \"#f0ece4\",\n  saberColor = \"#88aaff\",\n  className,\n  style,\n  ...props\n}: DotImageSliderProps) {\n  const containerRef = React.useRef<HTMLDivElement>(null);\n  const dotCanvasRef = React.useRef<HTMLCanvasElement>(null);\n\n  const [canvasSize, setCanvasSize] = React.useState({ w: 0, h: 0 });\n  const [sliderPos, setSliderPos] = React.useState(0.5);\n  const [isDragging, setIsDragging] = React.useState(false);\n  const [imageReady, setImageReady] = React.useState(false);\n  const [imgNatural, setImgNatural] = React.useState<{ w: number; h: number } | null>(null);\n\n  React.useEffect(() => {\n    const el = containerRef.current;\n    if (!el) return;\n    const measure = () => {\n      const w = el.offsetWidth;\n      const h = el.offsetHeight;\n      if (w > 0 && h > 0) setCanvasSize({ w, h });\n    };\n    measure();\n    const ro = new ResizeObserver(measure);\n    ro.observe(el);\n    return () => ro.disconnect();\n  }, []);\n\n  const { w: canvasW, h: canvasH } = canvasSize;\n  React.useEffect(() => {\n    const w = canvasW;\n    const h = canvasH;\n    if (!image || !dotCanvasRef.current || w === 0 || h === 0) return;\n    setImageReady(false);\n\n    const canvas = dotCanvasRef.current;\n    const ctx = canvas.getContext(\"2d\");\n    if (!ctx) return;\n\n    canvas.width = w;\n    canvas.height = h;\n    ctx.fillStyle = backgroundColor;\n    ctx.fillRect(0, 0, w, h);\n\n    const img = new Image();\n    img.crossOrigin = \"anonymous\";\n    img.onload = () => {\n      setImgNatural({ w: img.naturalWidth, h: img.naturalHeight });\n      const dims = coverDims(img.naturalWidth, img.naturalHeight, w, h);\n\n      const tmp = document.createElement(\"canvas\");\n      tmp.width = w;\n      tmp.height = h;\n      const tmpCtx = tmp.getContext(\"2d\");\n      if (!tmpCtx) return;\n\n      tmpCtx.fillStyle = backgroundColor;\n      tmpCtx.fillRect(0, 0, w, h);\n      tmpCtx.drawImage(img, dims.x, dims.y, dims.w, dims.h);\n\n      let pd: Uint8ClampedArray;\n      try {\n        pd = tmpCtx.getImageData(0, 0, w, h).data;\n      } catch {\n        ctx.drawImage(img, dims.x, dims.y, dims.w, dims.h);\n        setImageReady(true);\n        return;\n      }\n\n      ctx.fillStyle = dotColor;\n      const half = dotSpacing / 2;\n      for (let row = half; row < h; row += dotSpacing) {\n        for (let col = half; col < w; col += dotSpacing) {\n          const px = Math.round(col);\n          const py = Math.round(row);\n          if (px >= w || py >= h) continue;\n          const i = (py * w + px) * 4;\n          const lum = (0.299 * pd[i] + 0.587 * pd[i + 1] + 0.114 * pd[i + 2]) / 255;\n          const a = pd[i + 3] / 255;\n          if (a < 0.05) continue;\n          const r = dotMaxSize * Math.pow(1 - lum, 0.78) * a;\n          if (r < 0.22) continue;\n          ctx.beginPath();\n          ctx.arc(col, row, r, 0, Math.PI * 2);\n          ctx.fill();\n        }\n      }\n      setImageReady(true);\n    };\n    img.onerror = () => setImageReady(false);\n    img.src = image;\n  }, [image, canvasW, canvasH, dotSpacing, dotMaxSize, dotColor, backgroundColor]);\n\n  const updatePos = React.useCallback((clientX: number) => {\n    const el = containerRef.current;\n    if (!el) return;\n    const rect = el.getBoundingClientRect();\n    setSliderPos(Math.max(0.02, Math.min(0.98, (clientX - rect.left) / rect.width)));\n  }, []);\n\n  const onPointerDown = React.useCallback(\n    (e: React.PointerEvent) => {\n      (e.currentTarget as HTMLElement).setPointerCapture(e.pointerId);\n      setIsDragging(true);\n      updatePos(e.clientX);\n    },\n    [updatePos],\n  );\n\n  const onPointerMove = React.useCallback(\n    (e: React.PointerEvent) => {\n      if (!isDragging) return;\n      updatePos(e.clientX);\n    },\n    [isDragging, updatePos],\n  );\n\n  const onPointerUp = React.useCallback(() => setIsDragging(false), []);\n\n  const getImgStyle = (): React.CSSProperties => {\n    if (!imgNatural) return { display: \"none\" };\n    const { w, h, x, y } = coverDims(imgNatural.w, imgNatural.h, canvasSize.w, canvasSize.h);\n    return { position: \"absolute\", left: x, top: y, width: w, height: h, pointerEvents: \"none\" };\n  };\n\n  const pct = `${sliderPos * 100}%`;\n\n  return (\n    <div\n      ref={containerRef}\n      onPointerDown={onPointerDown}\n      onPointerMove={onPointerMove}\n      onPointerUp={onPointerUp}\n      className={cn(\"relative w-full h-full overflow-hidden rounded-xl select-none touch-none\", isDragging ? \"cursor-grabbing\" : \"cursor-ew-resize\", className)}\n      style={{ backgroundColor, ...style }}\n      {...props}\n    >\n      <style>{`\n        @keyframes dis-core { 0%,100% { opacity: 1; } 50% { opacity: .82; } }\n        @keyframes dis-aura { 0%,100% { opacity: .5; transform: translateX(-50%) scaleX(1); } 50% { opacity: .8; transform: translateX(-50%) scaleX(1.4); } }\n        @keyframes dis-knob { 0%,100% { box-shadow: 0 0 10px 3px ${saberColor}88, 0 2px 12px rgba(0,0,0,.18); } 50% { box-shadow: 0 0 18px 6px ${saberColor}bb, 0 2px 16px rgba(0,0,0,.22); } }\n      `}</style>\n\n      <canvas ref={dotCanvasRef} className=\"absolute inset-0 block h-full w-full\" />\n\n      {imageReady && (\n        <div className=\"pointer-events-none absolute inset-0\" style={{ clipPath: `inset(0 ${(1 - sliderPos) * 100}% 0 0)` }}>\n          {/* eslint-disable-next-line @next/next/no-img-element */}\n          <img src={image} crossOrigin=\"anonymous\" alt=\"\" style={getImgStyle()} />\n        </div>\n      )}\n\n      {imageReady && (\n        <div className=\"pointer-events-none absolute top-0 z-[4] h-full w-0 -translate-x-1/2\" style={{ left: pct }}>\n          <div\n            className=\"absolute top-0 left-1/2 h-full w-7 -translate-x-1/2\"\n            style={{ background: `radial-gradient(ellipse at center, ${saberColor}3a 0%, transparent 72%)`, animation: \"dis-aura 2.2s ease-in-out infinite\" }}\n          />\n          <div\n            className=\"absolute top-0 bottom-0 left-1/2 w-0.5 -translate-x-1/2 rounded-[2px]\"\n            style={{\n              background: \"#fff\",\n              boxShadow: `0 0 5px 2px #fff, 0 0 14px 5px ${saberColor}, 0 0 32px 10px ${saberColor}66`,\n              animation: \"dis-core 2.2s ease-in-out infinite\",\n            }}\n          />\n        </div>\n      )}\n\n      {imageReady && (\n        <div\n          className=\"pointer-events-none absolute top-1/2 z-[5] flex h-[42px] w-[42px] -translate-x-1/2 -translate-y-1/2 items-center justify-center rounded-full bg-white/93 backdrop-blur-[12px]\"\n          style={{ left: pct, animation: \"dis-knob 2.2s ease-in-out infinite\" }}\n        >\n          <svg width=\"22\" height=\"14\" viewBox=\"0 0 22 14\" fill=\"none\">\n            <path d=\"M1 7H21M1 7L5 3M1 7L5 11M21 7L17 3M21 7L17 11\" stroke=\"#3a3a3a\" strokeWidth=\"1.7\" strokeLinecap=\"round\" strokeLinejoin=\"round\" />\n          </svg>\n        </div>\n      )}\n\n      {!image && (\n        <div className=\"absolute inset-0 flex flex-col items-center justify-center gap-3 text-[#bbb]\">\n          <svg width=\"56\" height=\"56\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.1\">\n            <rect x=\"3\" y=\"3\" width=\"18\" height=\"18\" rx=\"3\" />\n            <circle cx=\"8.5\" cy=\"8.5\" r=\"1.5\" />\n            <polyline points=\"21,15 16,10 5,21\" />\n          </svg>\n          <span className=\"text-xs font-medium tracking-[0.04em]\" style={{ fontFamily: \"-apple-system, system-ui, sans-serif\" }}>\n            Connect an image\n          </span>\n        </div>\n      )}\n    </div>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}