{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "text-ascii-render",
  "type": "registry:component",
  "title": "Text ASCII Render",
  "description": "Turns text into a field of monospace characters on a canvas. The letters ripple on their own and light up around the pointer.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "text",
    "ascii",
    "canvas",
    "monospace",
    "terminal",
    "text",
    "pointer"
  ],
  "docs": "https://hyperblur.dev/effects/text-ascii-render",
  "files": [
    {
      "path": "registry/hyperblur/text-ascii-render.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/text-ascii-render.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface TextAsciiRenderProps extends Omit<React.HTMLAttributes<HTMLDivElement>, \"color\"> {\n  text: string;\n  density?: number;\n  charset?: string;\n  height?: number | string;\n  fontFamily?: string;\n  textFont?: string;\n  fontWeight?: number;\n  wave?: number;\n  field?: boolean;\n  pointerRadius?: number;\n  color?: string;\n  accent?: string;\n}\n\nconst css = `\n:where(.hb-qt5) { font-family: ui-monospace, \"SF Mono\", Menlo, Consolas, monospace; }\n.hb-qt5 {\n  position: relative;\n  display: block;\n  box-sizing: border-box;\n  width: 100%;\n  height: var(--hb-qt5-h);\n  overflow: hidden;\n}\n.hb-qt5-cv {\n  position: absolute;\n  inset: 0;\n  display: block;\n  width: 100%;\n  height: 100%;\n  touch-action: pan-y;\n}\n.hb-qt5-sr {\n  position: absolute;\n  width: 1px;\n  height: 1px;\n  margin: -1px;\n  padding: 0;\n  overflow: hidden;\n  clip-path: inset(50%);\n  white-space: nowrap;\n  border: 0;\n}\n`;\n\nconst FRAME_MS = 26;\n\nfunction clamp(v: number) {\n  return v < 0 ? 0 : v > 1 ? 1 : v;\n}\n\nexport function TextAsciiRender({\n  text,\n  density = 1,\n  charset = \" .:-=+*#%@\",\n  height = 200,\n  fontFamily,\n  textFont = 'system-ui, \"Helvetica Neue\", Arial, sans-serif',\n  fontWeight = 800,\n  wave = 1,\n  field = true,\n  pointerRadius = 80,\n  color,\n  accent = \"#2f7bff\",\n  className = \"\",\n  style,\n  ...rest\n}: TextAsciiRenderProps) {\n  const rootRef = React.useRef<HTMLDivElement>(null);\n  const canvasRef = React.useRef<HTMLCanvasElement>(null);\n\n  React.useEffect(() => {\n    const root = rootRef.current;\n    const canvas = canvasRef.current;\n    if (!root || !canvas) return;\n    const ctx = canvas.getContext(\"2d\");\n    if (!ctx) return;\n\n    const chars = Array.from(charset.length > 1 ? charset : \" .:-=+*#%@\");\n    const last = chars.length - 1;\n    const reduce = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n\n    let W = 1;\n    let H = 1;\n    let dpr = 1;\n    let cols = 1;\n    let rows = 1;\n    let cw = 6;\n    let chh = 10;\n    let offX = 0;\n    let offY = 0;\n    let cell = 10;\n    let family = \"monospace\";\n    let ink = \"#ededed\";\n    let lum = new Float32Array(1);\n    let disposed = false;\n    let seen = true;\n    let raf = 0;\n    let lastFrame = 0;\n    let inside = false;\n    let blend = 0;\n    let realX = 0;\n    let realY = 0;\n    const origin = performance.now();\n\n    const rebuild = () => {\n      const rect = root.getBoundingClientRect();\n      W = Math.max(1, Math.floor(rect.width));\n      H = Math.max(1, Math.floor(rect.height));\n      dpr = Math.min(2, window.devicePixelRatio || 1);\n      canvas.width = Math.round(W * dpr);\n      canvas.height = Math.round(H * dpr);\n      const cs = getComputedStyle(root);\n      family = cs.fontFamily || \"monospace\";\n      ink = color ?? cs.color ?? \"#ededed\";\n      cell = Math.max(4, 10 / Math.max(0.2, density));\n      ctx.setTransform(1, 0, 0, 1, 0, 0);\n      ctx.font = `${cell}px ${family}`;\n      const m = ctx.measureText(\"M\").width;\n      cw = m > 0 ? m : cell * 0.6;\n      chh = cell;\n      cols = Math.max(1, Math.floor(W / cw));\n      rows = Math.max(1, Math.floor(H / chh));\n      offX = (W - cols * cw) / 2;\n      offY = (H - rows * chh) / 2;\n\n      const S = W * H > 400000 ? 2 : 3;\n      const bw = Math.max(1, Math.round(cols * cw * S));\n      const bh = Math.max(1, Math.round(rows * chh * S));\n      const big = document.createElement(\"canvas\");\n      big.width = bw;\n      big.height = bh;\n      const bctx = big.getContext(\"2d\", { willReadFrequently: true });\n      lum = new Float32Array(cols * rows);\n      if (!bctx) return;\n      const lines = text.split(\"\\n\");\n      bctx.font = `${fontWeight} 100px ${textFont}`;\n      let maxW = 1;\n      for (const line of lines) maxW = Math.max(maxW, bctx.measureText(line).width);\n      const fit = Math.min((bw * 0.94) / maxW, (bh * 0.8) / (lines.length * 1.05));\n      const px = Math.max(4, 100 * fit);\n      bctx.font = `${fontWeight} ${px.toFixed(1)}px ${textFont}`;\n      bctx.fillStyle = \"#fff\";\n      bctx.textAlign = \"center\";\n      bctx.textBaseline = \"middle\";\n      const lh = px * 1.02;\n      lines.forEach((line, i) => {\n        bctx.fillText(line, bw / 2, bh / 2 + (i - (lines.length - 1) / 2) * lh);\n      });\n      const data = bctx.getImageData(0, 0, bw, bh).data;\n      const bcw = bw / cols;\n      const bch = bh / rows;\n      for (let r = 0; r < rows; r++) {\n        const y0 = Math.floor(r * bch);\n        const y1 = Math.max(y0 + 1, Math.floor((r + 1) * bch));\n        for (let c = 0; c < cols; c++) {\n          const x0 = Math.floor(c * bcw);\n          const x1 = Math.max(x0 + 1, Math.floor((c + 1) * bcw));\n          let sum = 0;\n          let n = 0;\n          for (let y = y0; y < y1 && y < bh; y++) {\n            for (let x = x0; x < x1 && x < bw; x++) {\n              sum += data[(y * bw + x) * 4 + 3];\n              n++;\n            }\n          }\n          lum[r * cols + c] = n ? sum / n / 255 : 0;\n        }\n      }\n    };\n\n    const draw = (t: number, px: number, py: number, radius: number) => {\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n      ctx.clearRect(0, 0, W, H);\n      ctx.font = `${cell}px ${family}`;\n      ctx.textBaseline = \"top\";\n      ctx.textAlign = \"left\";\n      const amp = Math.max(0, wave);\n      let fillNow = \"\";\n      let alphaNow = -1;\n      for (let r = 0; r < rows; r++) {\n        const y = offY + r * chh;\n        for (let c = 0; c < cols; c++) {\n          const x = offX + c * cw;\n          const base = lum[r * cols + c];\n          const s = (Math.sin(c * 0.28 + r * 0.19 + t * 1.6) + Math.sin(c * 0.11 - r * 0.31 - t * 1.1)) * 0.5;\n          let v: number;\n          let a: number;\n          if (base > 0.04) {\n            v = 0.3 + 0.7 * base + s * 0.16 * amp;\n            a = 1;\n          } else if (field) {\n            v = 0.1 + s * 0.09 * amp;\n            a = 0.34;\n          } else {\n            v = 0;\n            a = 0;\n          }\n          let k = 0;\n          if (radius > 0) {\n            const d = Math.hypot(x + cw / 2 - px, y + chh / 2 - py);\n            if (d < radius) {\n              k = 1 - d / radius;\n              k *= k;\n              v += k * 0.7 * (0.5 + 0.5 * Math.sin(d * 0.13 - t * 7));\n              if (a === 0) a = 0.5 * k + 0.1;\n              else a = Math.max(a, 0.5 + 0.5 * k);\n            }\n          }\n          const idx = Math.round(clamp(v) * last);\n          if (idx <= 0 || a <= 0) continue;\n          const fill = k > 0.1 ? accent : ink;\n          if (fill !== fillNow) {\n            ctx.fillStyle = fill;\n            fillNow = fill;\n          }\n          if (a !== alphaNow) {\n            ctx.globalAlpha = a;\n            alphaNow = a;\n          }\n          ctx.fillText(chars[idx], x, y);\n        }\n      }\n      ctx.globalAlpha = 1;\n    };\n\n    const still = () => {\n      draw(0, -9999, -9999, 0);\n    };\n\n    const tick = (now: number) => {\n      raf = 0;\n      if (disposed || !seen) return;\n      raf = requestAnimationFrame(tick);\n      const dt = lastFrame ? now - lastFrame : FRAME_MS;\n      if (dt < FRAME_MS) return;\n      lastFrame = now;\n      const t = (now - origin) / 1000;\n      blend += ((inside ? 1 : 0) - blend) * (1 - Math.exp((-Math.min(dt, 100) / 1000) * 6));\n      const ix = W * (0.5 + 0.38 * Math.sin(t * 0.6));\n      const iy = H * (0.5 + 0.3 * Math.sin(t * 0.9 + 1));\n      const px = ix + (realX - ix) * blend;\n      const py = iy + (realY - iy) * blend;\n      draw(t, px, py, Math.max(0, pointerRadius) * (0.8 + 0.2 * blend));\n    };\n    const run = () => {\n      if (!raf && seen && !disposed && !reduce) raf = requestAnimationFrame(tick);\n    };\n\n    const setup = () => {\n      rebuild();\n      if (reduce) still();\n    };\n    setup();\n    if (document.fonts?.ready) {\n      document.fonts.ready.then(() => {\n        if (!disposed) setup();\n      });\n    }\n\n    const ro = new ResizeObserver(() => setup());\n    ro.observe(root);\n    const io = new IntersectionObserver((entries) => {\n      for (const entry of entries) {\n        seen = entry.isIntersecting;\n        if (seen) {\n          lastFrame = 0;\n          run();\n        }\n      }\n    });\n    io.observe(root);\n\n    const move = (e: PointerEvent) => {\n      const rect = root.getBoundingClientRect();\n      realX = e.clientX - rect.left;\n      realY = e.clientY - rect.top;\n      inside = true;\n    };\n    const leave = () => {\n      inside = false;\n    };\n    if (!reduce) {\n      root.addEventListener(\"pointerenter\", move);\n      root.addEventListener(\"pointermove\", move);\n      root.addEventListener(\"pointerleave\", leave);\n      root.addEventListener(\"pointercancel\", leave);\n    }\n    run();\n\n    return () => {\n      disposed = true;\n      if (raf) cancelAnimationFrame(raf);\n      ro.disconnect();\n      io.disconnect();\n      root.removeEventListener(\"pointerenter\", move);\n      root.removeEventListener(\"pointermove\", move);\n      root.removeEventListener(\"pointerleave\", leave);\n      root.removeEventListener(\"pointercancel\", leave);\n    };\n  }, [text, density, charset, wave, field, pointerRadius, color, accent, textFont, fontWeight, fontFamily]);\n\n  const vars = {\n    \"--hb-qt5-h\": typeof height === \"number\" ? `${height}px` : height,\n    ...(fontFamily ? { fontFamily } : null),\n    ...style,\n  } as React.CSSProperties;\n\n  return (\n    <div ref={rootRef} className={`hb-qt5 ${className}`} style={vars} {...rest}>\n      <style href=\"hb-text-ascii-render\" precedence=\"default\">{css}</style>\n      <span className=\"hb-qt5-sr\">{text}</span>\n      <canvas ref={canvasRef} className=\"hb-qt5-cv\" aria-hidden=\"true\" />\n    </div>\n  );\n}\n"
    }
  ]
}