{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "scratch-card",
  "title": "Scratch Card",
  "author": "VenusUI",
  "description": "A scratch-off card: scratch the silver coating with a mouse or finger to reveal what's underneath.",
  "files": [
    {
      "path": "registry/default/ui/scratch-card.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\n\nimport { cn } from \"@/lib/utils\"\n\ninterface ScratchCardProps extends Omit<React.ComponentProps<\"div\">, \"children\"> {\n  /** What's under the coating. */\n  children: React.ReactNode\n  /** Share of the coating (0–1) that must be scratched off before it clears completely. */\n  threshold?: number\n  /** Scratch radius in px. */\n  brush?: number\n  /** Text printed on the coating. */\n  label?: string\n  onReveal?: () => void\n}\n\n/**\n * A scratch-off card: scratch the silver coating with a mouse or finger to\n * reveal what's underneath; past `threshold` it clears completely. The\n * \"Reveal\" button does the same for keyboard and screen-reader users.\n */\nfunction ScratchCard({ children, threshold = 0.55, brush = 22, label = \"Scratch here\", onReveal, className, ...props }: ScratchCardProps) {\n  const canvasRef = React.useRef<HTMLCanvasElement>(null)\n  const [revealed, setRevealed] = React.useState(false)\n  const drawing = React.useRef(false)\n  const last = React.useRef<{ x: number; y: number } | null>(null)\n  const moves = React.useRef(0)\n\n  // Paint the coating at the canvas's real pixel size.\n  React.useEffect(() => {\n    const canvas = canvasRef.current\n    if (!canvas) return\n    const paint = () => {\n      const { width, height } = canvas.getBoundingClientRect()\n      const dpr = window.devicePixelRatio || 1\n      canvas.width = Math.round(width * dpr)\n      canvas.height = Math.round(height * dpr)\n      const ctx = canvas.getContext(\"2d\")\n      if (!ctx) return\n      ctx.scale(dpr, dpr)\n      const g = ctx.createLinearGradient(0, 0, width, height)\n      g.addColorStop(0, \"#d4d4d8\")\n      g.addColorStop(0.35, \"#f4f4f5\")\n      g.addColorStop(0.5, \"#a1a1aa\")\n      g.addColorStop(0.7, \"#e4e4e7\")\n      g.addColorStop(1, \"#a1a1aa\")\n      ctx.fillStyle = g\n      ctx.fillRect(0, 0, width, height)\n      // Speckles, like real scratch-off ink.\n      for (let i = 0; i < (width * height) / 60; i++) {\n        ctx.fillStyle = `rgba(${Math.random() > 0.5 ? \"255,255,255\" : \"82,82,91\"},${0.08 + Math.random() * 0.12})`\n        ctx.fillRect(Math.random() * width, Math.random() * height, 1.5, 1.5)\n      }\n      ctx.fillStyle = \"rgba(63,63,70,0.75)\"\n      ctx.font = \"600 14px system-ui, sans-serif\"\n      ctx.textAlign = \"center\"\n      ctx.textBaseline = \"middle\"\n      ctx.fillText(label.toUpperCase(), width / 2, height / 2)\n    }\n    paint()\n    const observer = new ResizeObserver(() => {\n      if (!revealed) paint()\n    })\n    observer.observe(canvas)\n    return () => observer.disconnect()\n  }, [label, revealed])\n\n  const reveal = () => {\n    if (revealed) return\n    setRevealed(true)\n    onReveal?.()\n  }\n\n  /** Share of the coating already scratched off, sampled on a coarse grid. */\n  const cleared = () => {\n    const canvas = canvasRef.current\n    const ctx = canvas?.getContext(\"2d\")\n    if (!canvas || !ctx) return 0\n    const { data } = ctx.getImageData(0, 0, canvas.width, canvas.height)\n    let clear = 0\n    let total = 0\n    for (let i = 3; i < data.length; i += 4 * 32) {\n      total++\n      if (data[i] === 0) clear++\n    }\n    return clear / total\n  }\n\n  const scratch = (event: React.PointerEvent<HTMLCanvasElement>) => {\n    const canvas = canvasRef.current\n    const ctx = canvas?.getContext(\"2d\")\n    if (!canvas || !ctx) return\n    const rect = canvas.getBoundingClientRect()\n    const point = { x: event.clientX - rect.left, y: event.clientY - rect.top }\n    ctx.globalCompositeOperation = \"destination-out\"\n    ctx.lineWidth = brush * 2\n    ctx.lineCap = \"round\"\n    ctx.beginPath()\n    ctx.moveTo(last.current?.x ?? point.x, last.current?.y ?? point.y)\n    ctx.lineTo(point.x, point.y)\n    ctx.stroke()\n    last.current = point\n    if (++moves.current % 8 === 0 && cleared() > threshold) reveal()\n  }\n\n  return (\n    <div data-slot=\"scratch-card\" className={cn(\"flex w-full max-w-sm flex-col items-center gap-3\", className)} {...props}>\n      <div className=\"relative w-full overflow-hidden rounded-2xl border bg-card shadow-lg\">\n        <div aria-hidden={!revealed} inert={!revealed}>\n          {children}\n        </div>\n        <canvas\n          ref={canvasRef}\n          aria-hidden=\"true\"\n          onPointerDown={(event) => {\n            event.currentTarget.setPointerCapture(event.pointerId)\n            drawing.current = true\n            last.current = null\n            scratch(event)\n          }}\n          onPointerMove={(event) => drawing.current && scratch(event)}\n          onPointerUp={() => {\n            drawing.current = false\n            if (cleared() > threshold) reveal()\n          }}\n          onPointerCancel={() => (drawing.current = false)}\n          className={cn(\n            \"absolute inset-0 size-full cursor-[grab] touch-none transition-opacity duration-700 motion-reduce:transition-none\",\n            revealed && \"pointer-events-none opacity-0\"\n          )}\n        />\n      </div>\n      {revealed ? (\n        <p role=\"status\" className=\"text-sm text-muted-foreground\">\n          Revealed!\n        </p>\n      ) : (\n        <button\n          type=\"button\"\n          onClick={reveal}\n          className=\"rounded-md px-2 py-1 text-sm text-muted-foreground underline-offset-4 outline-none hover:text-foreground hover:underline focus-visible:ring-3 focus-visible:ring-ring/50\"\n        >\n          Scratch with your finger or mouse, or reveal it\n        </button>\n      )}\n    </div>\n  )\n}\n\nexport { ScratchCard }\n",
      "type": "registry:ui"
    }
  ],
  "meta": {
    "version": "0.1.0"
  },
  "categories": [
    "interactive",
    "cards"
  ],
  "type": "registry:ui"
}