{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"phosphor-wall","type":"registry:ui","title":"Phosphor Wall","description":"A scrolling wall of phosphor glyphs on a worn CRT, with scanlines, flicker, torn glitch bands and color fringing. WebGL.","dependencies":[],"registryDependencies":[],"files":[{"path":"registry/ui/phosphor-wall.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useEffect, useRef } from \"react\";\n\nexport interface PhosphorWallProps {\n  /** Phosphor color. */\n  color?: string;\n  /** Character cell height in px. */\n  scale?: number;\n  /** Scroll and scramble speed multiplier; 0 freezes the text. */\n  speed?: number;\n  /** Glitch tearing and color fringing (0 to 1). */\n  glitch?: number;\n  /** Brightness flicker (0 to 1). */\n  flicker?: number;\n  /** Tube curvature (0 is flat). */\n  curvature?: number;\n  className?: string;\n}\n\nconst VERT = \"attribute vec2 p;void main(){gl_Position=vec4(p,0.,1.);}\";\n\nconst FRAG = `\nprecision highp float;\nuniform vec2 uRes;\nuniform float uTime;\nuniform float uClock;\nuniform float uDpr;\nuniform vec2 uMouse;\nuniform float uActive;\nuniform vec3 uColor;\nuniform float uScale;\nuniform float uGlitch;\nuniform float uFlicker;\nuniform float uCurve;\n\nfloat hash(vec2 p) {\n  p = fract(p * vec2(123.34, 456.21));\n  p += dot(p, p + 45.32);\n  return fract(p.x * p.y);\n}\n\nfloat seg(float seed, float k) { return step(0.63, hash(vec2(seed, k * 1.37 + 0.5))); }\n\n// A pseudo-character on a 5x7 pixel grid, assembled from random strokes.\nfloat glyph(vec2 g, float seed) {\n  float lx = step(g.x, 0.5), rx = step(3.5, g.x), cx = step(abs(g.x - 2.0), 0.5);\n  float up = step(3.0, g.y), lo = step(g.y, 3.0);\n  float on = seg(seed, 0.0) * step(5.5, g.y);\n  on = max(on, seg(seed, 1.0) * step(abs(g.y - 3.0), 0.5) * step(0.5, g.x) * step(g.x, 3.5 + rx));\n  on = max(on, seg(seed, 2.0) * step(g.y, 0.5));\n  on = max(on, seg(seed, 3.0) * lx * up);\n  on = max(on, seg(seed, 4.0) * lx * lo);\n  on = max(on, seg(seed, 5.0) * rx * up);\n  on = max(on, seg(seed, 6.0) * rx * lo);\n  on = max(on, seg(seed, 7.0) * cx * up * 0.9);\n  on = max(on, seg(seed, 8.0) * cx * lo * 0.9);\n  on = max(on, seg(seed, 9.0) * step(abs(6.0 - g.y - g.x * 1.5), 0.6));\n  on = max(on, seg(seed, 10.0) * step(abs(g.y - g.x * 1.5), 0.6));\n  return on;\n}\n\n// x: lit phosphor at this pixel, y: soft glow of the character cell.\nvec2 textAt(vec2 frag) {\n  float pu = max(floor(uScale * uDpr / 9.0), 1.0);\n  vec2 cs = vec2(6.0, 9.0) * pu;\n  vec2 top = vec2(frag.x, uRes.y - frag.y);\n  vec2 cell = floor(top / cs);\n  vec2 local = top - cell * cs;\n  vec2 g = floor(local / pu);\n  g.y = 7.0 - g.y;\n  float inBox = step(g.x, 4.5) * step(0.0, g.y) * step(g.y, 6.5);\n\n  // The log scrolls up one line at a time.\n  float line = cell.y + floor(uTime * 1.3);\n  float lh = hash(vec2(line, 17.0));\n  float cols = floor(uRes.x / cs.x);\n  float len = floor(cols * mix(0.12, 0.92, lh * lh));\n  float indent = step(0.6, hash(vec2(line, 5.0))) * 2.0;\n  float inLine = step(indent, cell.x) * step(cell.x, len) * step(0.12, lh);\n  float space = step(hash(vec2(cell.x, line) + 0.37), 0.17);\n\n  // A few cells keep scrambling.\n  float fast = step(0.965, hash(cell + vec2(9.1, 2.3)));\n  float seed = hash(vec2(cell.x, line) + floor(uTime * 14.0) * fast * 0.13);\n  float bright = 0.5 + 0.5 * hash(vec2(line, 41.0));\n  float lit = glyph(g, seed * 97.0) * inBox * inLine * (1.0 - space);\n\n  vec2 sub = fract(local / pu) - 0.5;\n  float px = smoothstep(0.62, 0.32, max(abs(sub.x), abs(sub.y)));\n  return vec2(lit * px, inLine * (1.0 - space) * 0.07) * bright;\n}\n\nvoid main() {\n  vec2 uv = gl_FragCoord.xy / uRes;\n  vec2 cc = uv * 2.0 - 1.0;\n  cc *= 1.0 + uCurve * vec2(0.06, 0.09) * dot(cc, cc);\n  vec2 q = abs(cc) - 0.97;\n  float box = length(max(q, 0.0)) + min(max(q.x, q.y), 0.0) - 0.03;\n  float inside = 1.0 - smoothstep(-0.004, 0.004, box);\n  vec2 frag = (cc * 0.5 + 0.5) * uRes;\n\n  // Glitch: bursts of torn horizontal bands.\n  float band = floor(frag.y / (uRes.y / 28.0));\n  float gt = floor(uClock * 12.0);\n  float burst = step(1.0 - uGlitch * 0.35, hash(vec2(floor(uClock * 1.7), 7.0)));\n  float tear = step(1.0 - uGlitch * 0.25, hash(vec2(band, gt))) * burst;\n  frag.x += (hash(vec2(band, gt + 1.0)) - 0.5) * uRes.x * 0.08 * tear;\n\n  float ca = (0.3 + uGlitch * 0.8 + tear * 5.0) * uDpr;\n  vec2 tr = textAt(frag + vec2(ca, 0.0));\n  vec2 tg = textAt(frag);\n  vec2 tb = textAt(frag - vec2(ca, 0.0));\n\n  vec3 col = uColor * (tg.x + tg.y);\n  col += vec3(1.0, 0.2, 0.35) * max(tr.x - tg.x, 0.0) * 0.55;\n  col += vec3(0.25, 0.45, 1.0) * max(tb.x - tg.x, 0.0) * 0.55;\n  col += uColor * 0.035;\n\n  float md = length((uMouse - uv) * vec2(uRes.x / uRes.y, 1.0));\n  col *= 1.0 + uActive * 0.9 * exp(-md * md * 18.0);\n\n  col *= 0.8 + 0.2 * cos(frag.y / uDpr * 2.0944);\n  float roll = fract(uv.y * 0.7 - uClock * 0.07);\n  float rb = (roll - 0.5) / 0.05;\n  col *= 1.0 + 0.12 * exp(-rb * rb);\n  col *= 1.0 - uFlicker * (0.08 * hash(vec2(floor(uClock * 30.0), 1.0)) + 0.03 * sin(uClock * 95.0));\n  col += (hash(gl_FragCoord.xy + fract(uClock) * 97.0) - 0.5) * 0.04;\n  col *= 1.15 - 0.55 * dot(cc * 0.75, cc * 0.75);\n\n  gl_FragColor = vec4(max(col, 0.0) * inside, 1.0);\n}\n`;\n\nfunction rgb(hex: string) {\n  let h = hex.replace(\"#\", \"\");\n  if (h.length === 3) h = h.replace(/./g, \"$&$&\");\n  const n = parseInt(h.padEnd(6, \"0\").slice(0, 6), 16) || 0;\n  return [((n >> 16) & 255) / 255, ((n >> 8) & 255) / 255, (n & 255) / 255];\n}\n\n/** Compiles a full-screen-triangle program and returns a cached uniform lookup, or null. */\nfunction fullscreen(gl: WebGLRenderingContext, frag: string) {\n  const prog = gl.createProgram();\n  if (!prog) return null;\n  for (const [type, src] of [\n    [gl.VERTEX_SHADER, VERT],\n    [gl.FRAGMENT_SHADER, frag],\n  ] as const) {\n    const sh = gl.createShader(type);\n    if (!sh) return null;\n    gl.shaderSource(sh, src);\n    gl.compileShader(sh);\n    if (!gl.getShaderParameter(sh, gl.COMPILE_STATUS)) console.warn(gl.getShaderInfoLog(sh));\n    gl.attachShader(prog, sh);\n  }\n  gl.bindAttribLocation(prog, 0, \"p\");\n  gl.linkProgram(prog);\n  if (!gl.getProgramParameter(prog, gl.LINK_STATUS)) return null;\n  gl.useProgram(prog);\n  gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer());\n  gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 3, -1, -1, 3]), gl.STATIC_DRAW);\n  gl.enableVertexAttribArray(0);\n  gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);\n  const cache = new Map<string, WebGLUniformLocation | null>();\n  return (name: string) => {\n    if (!cache.has(name)) cache.set(name, gl.getUniformLocation(prog, name));\n    return cache.get(name) ?? null;\n  };\n}\n\nexport function PhosphorWall({\n  color = \"#33ff88\",\n  scale = 18,\n  speed = 1,\n  glitch = 0.5,\n  flicker = 0.5,\n  curvature = 0.4,\n  className,\n}: PhosphorWallProps) {\n  const host = useRef<HTMLDivElement>(null);\n  const opts = useRef({ color, scale, speed, glitch, flicker, curvature });\n  const redraw = useRef<(() => void) | null>(null);\n\n  useEffect(() => {\n    opts.current = { color, scale, speed, glitch, flicker, curvature };\n    redraw.current?.();\n  }, [color, scale, speed, glitch, flicker, curvature]);\n\n  useEffect(() => {\n    const el = host.current!;\n    const canvas = document.createElement(\"canvas\");\n    canvas.style.cssText = \"position:absolute;inset:0;width:100%;height:100%;display:block\";\n    el.appendChild(canvas);\n    const gl = canvas.getContext(\"webgl\", { antialias: false, powerPreference: \"low-power\" });\n    const u = gl && fullscreen(gl, FRAG);\n    if (!gl || !u) {\n      canvas.remove();\n      return;\n    }\n    const reduced = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const pointer = { x: 0.5, y: 0.5, active: 0, tx: 0.5, ty: 0.5, ta: 0 };\n    let raf = 0;\n    let visible = true;\n    let time = 0;\n    let clock = 0;\n    let last = 0;\n    let dpr = 1;\n\n    const draw = () => {\n      const o = opts.current;\n      gl.uniform2f(u(\"uRes\"), canvas.width, canvas.height);\n      gl.uniform1f(u(\"uTime\"), time);\n      gl.uniform1f(u(\"uClock\"), clock);\n      gl.uniform1f(u(\"uDpr\"), dpr);\n      gl.uniform2f(u(\"uMouse\"), pointer.x, pointer.y);\n      gl.uniform1f(u(\"uActive\"), pointer.active);\n      gl.uniform3fv(u(\"uColor\"), rgb(o.color));\n      gl.uniform1f(u(\"uScale\"), Math.max(o.scale, 6));\n      gl.uniform1f(u(\"uGlitch\"), reduced ? 0 : Math.min(Math.max(o.glitch, 0), 1));\n      gl.uniform1f(u(\"uFlicker\"), Math.min(Math.max(o.flicker, 0), 1));\n      gl.uniform1f(u(\"uCurve\"), Math.max(o.curvature, 0));\n      gl.drawArrays(gl.TRIANGLES, 0, 3);\n    };\n    const loop = (now: number) => {\n      const dt = Math.min(now - last, 50) / 1000;\n      last = now;\n      time += dt * opts.current.speed;\n      clock += dt;\n      pointer.x += (pointer.tx - pointer.x) * 0.15;\n      pointer.y += (pointer.ty - pointer.y) * 0.15;\n      pointer.active += (pointer.ta - pointer.active) * 0.06;\n      draw();\n      raf = requestAnimationFrame(loop);\n    };\n    const play = () => {\n      if (raf || !visible || reduced) return;\n      last = performance.now();\n      raf = requestAnimationFrame(loop);\n    };\n    const resize = () => {\n      dpr = Math.min(window.devicePixelRatio || 1, 2);\n      canvas.width = Math.max(1, Math.round(el.clientWidth * dpr));\n      canvas.height = Math.max(1, Math.round(el.clientHeight * dpr));\n      gl.viewport(0, 0, canvas.width, canvas.height);\n      draw();\n    };\n    const onMove = (e: PointerEvent) => {\n      const r = el.getBoundingClientRect();\n      pointer.tx = (e.clientX - r.left) / r.width;\n      pointer.ty = 1 - (e.clientY - r.top) / r.height;\n      if (!pointer.ta) {\n        pointer.x = pointer.tx;\n        pointer.y = pointer.ty;\n      }\n      pointer.ta = 1;\n    };\n    const onLeave = () => {\n      pointer.ta = 0;\n    };\n\n    const target = el.parentElement ?? el;\n    target.addEventListener(\"pointermove\", onMove, { passive: true });\n    target.addEventListener(\"pointerleave\", onLeave);\n    const ro = new ResizeObserver(resize);\n    ro.observe(el);\n    const io = new IntersectionObserver(([entry]) => {\n      visible = entry.isIntersecting;\n      if (visible) play();\n      else {\n        cancelAnimationFrame(raf);\n        raf = 0;\n      }\n    });\n    io.observe(el);\n    redraw.current = draw;\n\n    return () => {\n      cancelAnimationFrame(raf);\n      ro.disconnect();\n      io.disconnect();\n      target.removeEventListener(\"pointermove\", onMove);\n      target.removeEventListener(\"pointerleave\", onLeave);\n      redraw.current = null;\n      gl.getExtension(\"WEBGL_lose_context\")?.loseContext();\n      canvas.remove();\n    };\n  }, []);\n\n  return (\n    <div\n      ref={host}\n      aria-hidden\n      className={className}\n      style={{ position: \"absolute\", inset: 0, overflow: \"hidden\", pointerEvents: \"none\" }}\n    />\n  );\n}\n"}]}