{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "ascii-video",
  "title": "ASCII Video",
  "description": "Plays a video file or the viewer's webcam as live ASCII, with a pause button and an accessible label.",
  "dependencies": [],
  "registryDependencies": [
    "https://dotgrain.space/r/engine.json"
  ],
  "files": [
    {
      "path": "registry/ui/ascii-video.tsx",
      "content": "\"use client\";\n\nimport { useEffect, useRef, useState, type CSSProperties, type Ref } from \"react\";\nimport { useDitherTokens, useReducedMotion } from \"@/lib/dotgrain/provider\";\nimport { Quantize } from \"@/lib/dotgrain/quantize\";\nimport type { DitherTokens } from \"@/lib/dotgrain/types\";\n\nexport type AsciiVideoProps = Partial<Omit<DitherTokens, \"mode\">> & {\n  /** Video file or URL. Cross-origin videos need CORS headers and `crossOrigin`. */\n  src?: string;\n  /** Describes the video for screen readers; the ASCII canvas is announced as one image. */\n  label: string;\n  /** Offer a \"Start camera\" button; the webcam only starts when it is clicked. */\n  webcam?: boolean;\n  /** Intrinsic width; with `height` it reserves the aspect ratio. */\n  width?: number;\n  height?: number;\n  crossOrigin?: \"anonymous\";\n  className?: string;\n  style?: CSSProperties;\n  ref?: Ref<HTMLDivElement>;\n};\n\ntype Camera = \"off\" | \"starting\" | \"on\" | \"denied\" | \"unavailable\";\n\nconst hidden: CSSProperties = { position: \"absolute\", width: 1, height: 1, opacity: 0, pointerEvents: \"none\" };\nconst fill: CSSProperties = { position: \"absolute\", inset: 0, width: \"100%\", height: \"100%\", objectFit: \"cover\" };\nconst overlay: CSSProperties = { position: \"absolute\", bottom: 8, left: 8, display: \"flex\", gap: 8, alignItems: \"center\", font: \"12px/1.5 ui-monospace, monospace\" };\n// Readable over any palette without assuming the host's CSS.\nconst control: CSSProperties = { font: \"inherit\", color: \"#fff\", background: \"rgb(0 0 0 / 0.7)\", border: \"1px solid rgb(255 255 255 / 0.5)\", borderRadius: 4, padding: \"2px 8px\", cursor: \"pointer\" };\nconst message: CSSProperties = { ...control, cursor: undefined };\n\n\nexport function AsciiVideo({ src, label, webcam, width, height, crossOrigin, className, style, ref, ...tokens }: AsciiVideoProps) {\n  // State re-renders once the element mounts (Quantize needs it); playback is driven through the ref.\n  const [video, setVideo] = useState<HTMLVideoElement | null>(null);\n  const element = useRef<HTMLVideoElement>(null);\n  const [failed, setFailed] = useState(false);\n  // The viewer's play/pause choice; until they make one, play unless motion is reduced or speed is 0.\n  const [choice, setChoice] = useState<boolean | null>(null);\n  const [camera, setCamera] = useState<Camera>(\"off\");\n  const reduced = useReducedMotion();\n  // `speed` sets the playback rate.\n  const { speed } = useDitherTokens({ ...tokens, mode: \"ascii\" });\n  const playing = choice ?? !(reduced || speed <= 0);\n\n  useEffect(() => {\n    const v = element.current;\n    if (!v) return;\n    if (speed > 0) v.playbackRate = speed;\n    if (playing) v.play().catch(() => setChoice(false));\n    else v.pause();\n  }, [video, speed, playing, src, camera]);\n\n  useEffect(() => {\n    // srcObject may also be a Blob or MediaSource, which have no tracks.\n    const stream = element.current?.srcObject as Partial<MediaStream> | null | undefined;\n    return () => stream?.getTracks?.().forEach((t) => t.stop());\n  }, [video, camera]);\n\n  async function startCamera() {\n    const v = element.current;\n    if (!v || !navigator.mediaDevices?.getUserMedia) return setCamera(\"unavailable\");\n    setCamera(\"starting\");\n    try {\n      v.srcObject = await navigator.mediaDevices.getUserMedia({ video: true });\n      setChoice(true);\n      setCamera(\"on\");\n    } catch (error) {\n      setCamera(error instanceof DOMException && error.name === \"NotAllowedError\" ? \"denied\" : \"unavailable\");\n    }\n  }\n\n  const live = camera === \"on\";\n  const showing = !!(src || live);\n  return (\n    <div\n      ref={ref}\n      className={className}\n      style={{\n        position: \"relative\",\n        aspectRatio: width && height ? `${width} / ${height}` : \"16 / 9\",\n        maxWidth: width,\n        // An empty screen until the camera starts.\n        background: showing ? undefined : \"rgb(127 127 127 / 0.15)\",\n        ...style,\n      }}\n    >\n      {/* Rendered but invisible: browsers may not decode a display:none video's frames. */}\n      <video\n        ref={(v) => {\n          element.current = v;\n          setVideo(v);\n        }}\n        src={live ? undefined : src}\n        crossOrigin={crossOrigin}\n        muted\n        loop\n        playsInline\n        aria-hidden={failed ? undefined : true}\n        aria-label={failed ? label : undefined}\n        style={failed ? fill : hidden}\n      />\n      {video && !failed && showing && (\n        <Quantize\n          source={{ kind: \"video\", element: video }}\n          label={label}\n          onError={() => setFailed(true)}\n          {...tokens}\n          mode=\"ascii\"\n          style={{ position: \"absolute\", inset: 0, transform: live ? \"scaleX(-1)\" : undefined }}\n        />\n      )}\n      <div style={overlay}>\n        {showing && (\n          <button type=\"button\" style={control} onClick={() => setChoice(!playing)}>\n            {playing ? \"Pause\" : \"Play\"}\n          </button>\n        )}\n        {webcam && !live && (\n          <button type=\"button\" style={control} onClick={startCamera} disabled={camera === \"starting\"}>\n            Start camera\n          </button>\n        )}\n        <span role=\"status\" style={camera === \"denied\" || camera === \"unavailable\" ? message : undefined}>\n          {camera === \"denied\" ? \"Camera access was denied.\" : camera === \"unavailable\" ? \"No camera available.\" : \"\"}\n        </span>\n      </div>\n    </div>\n  );\n}\n",
      "type": "registry:ui"
    }
  ],
  "meta": {
    "modes": [
      "ascii"
    ]
  },
  "categories": [
    "media"
  ],
  "type": "registry:ui"
}