{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"ascii-text","type":"registry:ui","title":"ASCII Text","description":"Text rasterized into shimmering ASCII glyphs on a canvas, rippling in a slow wave and bulging under the pointer like a lens.","dependencies":[],"registryDependencies":[],"files":[{"path":"registry/ui/ascii-text.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useEffect, useRef } from \"react\";\n\nexport interface AsciiTextProps {\n  text?: string;\n  /** Height of one character cell, in px. Smaller is sharper. */\n  cellSize?: number;\n  /** Glyphs ordered from sparse to dense. */\n  charset?: string;\n  color?: string;\n  /** Second gradient stop (bottom right). */\n  secondaryColor?: string;\n  /** Horizontal wave distortion, in px. */\n  waveAmplitude?: number;\n  /** Wave speed multiplier. */\n  waveSpeed?: number;\n  /** Radius of the pointer lens, in px. 0 disables it. */\n  pointerRadius?: number;\n  className?: string;\n}\n\nexport function AsciiText({\n  text = \"ASCII\",\n  cellSize = 11,\n  charset = \" .:-=+*#%@\",\n  color = \"#bef264\",\n  secondaryColor = \"#22d3ee\",\n  waveAmplitude = 6,\n  waveSpeed = 1,\n  pointerRadius = 150,\n  className,\n}: AsciiTextProps) {\n  const wrap = useRef<HTMLDivElement>(null);\n  const ref = useRef<HTMLCanvasElement>(null);\n\n  useEffect(() => {\n    const root = wrap.current!;\n    const canvas = ref.current!;\n    const ctx = canvas.getContext(\"2d\")!;\n    const mask = document.createElement(\"canvas\");\n    const mctx = mask.getContext(\"2d\", { willReadFrequently: true })!;\n    const reduced = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const family = getComputedStyle(root).fontFamily || \"sans-serif\";\n    const glyphs = charset.length ? charset : \" #\";\n    const last = glyphs.length - 1;\n    const ptr = { x: -1e4, y: -1e4, s: 0, on: 0 };\n    let w = 0;\n    let h = 0;\n    let cw = cellSize * 0.6;\n    let cols = 0;\n    let rows = 0;\n    let px: Uint8ClampedArray = new Uint8ClampedArray(0);\n    let raf = 0;\n    let visible = true;\n\n    const sample = (x: number, y: number) => {\n      const ix = x | 0;\n      const iy = y | 0;\n      if (ix < 0 || iy < 0 || ix >= w || iy >= h) return 0;\n      return px[(iy * w + ix) * 4 + 3] / 255;\n    };\n\n    const draw = (t: number) => {\n      ctx.clearRect(0, 0, w, h);\n      ptr.s += (ptr.on - ptr.s) * 0.08;\n      const R = pointerRadius;\n      for (let r = 0; r < rows; r++) {\n        const y = r * cellSize + cellSize / 2;\n        const wave = Math.sin(t * waveSpeed * 2 + r * 0.28) * waveAmplitude;\n        let line = \"\";\n        for (let c = 0; c < cols; c++) {\n          let x = c * cw + cw / 2 + wave;\n          let sy = y;\n          const dx = x - ptr.x;\n          const dy = y - ptr.y;\n          const d = Math.sqrt(dx * dx + dy * dy);\n          const inLens = R > 0 && d < R && ptr.s > 0.01;\n          if (inLens) {\n            // Pull samples toward the pointer: the text bulges like under a lens.\n            const k = (1 - d / R) ** 2 * ptr.s * 0.55;\n            x -= dx * k;\n            sy -= dy * k;\n          }\n          const a = sample(x, sy);\n          if (a === 0) {\n            line += inLens && (c + r) % 3 === 0 && d > R * 0.35 ? glyphs[Math.min(1, last)] : \" \";\n            continue;\n          }\n          const shimmer = 0.7 + 0.3 * Math.sin(t * 3 + c * 0.21 - r * 0.37);\n          const boost = inLens ? (1 - d / R) * ptr.s * 0.6 : 0;\n          line += glyphs[Math.max(1, Math.min(last, Math.round((a * shimmer + boost) * last)))];\n        }\n        ctx.fillText(line, 0, r * cellSize);\n      }\n    };\n\n    const layout = () => {\n      w = canvas.offsetWidth;\n      h = canvas.offsetHeight;\n      if (!w || !h) return;\n      const dpr = Math.min(window.devicePixelRatio || 1, 2);\n      canvas.width = w * dpr;\n      canvas.height = h * dpr;\n      mask.width = w;\n      mask.height = h;\n      mctx.font = `900 100px ${family}`;\n      const m = mctx.measureText(text);\n      const size = Math.min((100 * w * 0.86) / Math.max(m.width, 1), h * 0.62);\n      mctx.font = `900 ${size}px ${family}`;\n      mctx.fillStyle = \"#fff\";\n      mctx.textAlign = \"center\";\n      mctx.textBaseline = \"middle\";\n      mctx.fillText(text, w / 2, h / 2);\n      px = mctx.getImageData(0, 0, w, h).data;\n\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n      ctx.font = `600 ${cellSize}px ui-monospace, SFMono-Regular, Menlo, Consolas, monospace`;\n      ctx.textBaseline = \"top\";\n      cw = ctx.measureText(\"M\").width || cellSize * 0.6;\n      cols = Math.ceil(w / cw);\n      rows = Math.ceil(h / cellSize);\n      const g = ctx.createLinearGradient(0, 0, w, h);\n      g.addColorStop(0.2, color);\n      g.addColorStop(0.8, secondaryColor);\n      ctx.fillStyle = g;\n      if (reduced || !raf) draw(0);\n    };\n\n    const loop = (now: number) => {\n      draw(now / 1000);\n      raf = visible && !reduced ? requestAnimationFrame(loop) : 0;\n    };\n\n    const onMove = (e: PointerEvent) => {\n      const r = canvas.getBoundingClientRect();\n      ptr.x = e.clientX - r.left;\n      ptr.y = e.clientY - r.top;\n      ptr.on = 1;\n    };\n    const onLeave = () => (ptr.on = 0);\n\n    const ro = new ResizeObserver(layout);\n    ro.observe(canvas);\n    document.fonts?.ready.then(layout);\n    const io = new IntersectionObserver(([entry]) => {\n      visible = entry.isIntersecting;\n      if (visible && !raf && !reduced) raf = requestAnimationFrame(loop);\n    });\n    io.observe(canvas);\n    root.addEventListener(\"pointermove\", onMove, { passive: true });\n    root.addEventListener(\"pointerleave\", onLeave);\n\n    return () => {\n      cancelAnimationFrame(raf);\n      raf = -1;\n      ro.disconnect();\n      io.disconnect();\n      root.removeEventListener(\"pointermove\", onMove);\n      root.removeEventListener(\"pointerleave\", onLeave);\n    };\n  }, [text, cellSize, charset, color, secondaryColor, waveAmplitude, waveSpeed, pointerRadius]);\n\n  return (\n    <div ref={wrap} className={className} style={{ position: \"relative\", width: \"100%\", height: \"100%\" }}>\n      <span className=\"sr-only\">{text}</span>\n      <canvas ref={ref} aria-hidden style={{ position: \"absolute\", inset: 0, width: \"100%\", height: \"100%\" }} />\n    </div>\n  );\n}\n"}]}