{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"crt-warp","type":"registry:ui","title":"CRT Warp","description":"A glowing gradient seen through an old CRT: barrel curvature, scanlines, RGB mask, flicker and a rolling bar. WebGL.","dependencies":[],"registryDependencies":[],"files":[{"path":"registry/ui/crt-warp.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useEffect, useRef } from \"react\";\n\nexport interface CrtWarpProps {\n  /** Three gradient colors, bottom to top. */\n  colors?: string[];\n  /** Gradient drift speed multiplier; 0 freezes it. */\n  speed?: number;\n  /** Barrel curvature of the tube (0 is flat). */\n  curvature?: number;\n  /** Scanline darkness (0 to 1). */\n  scanlines?: number;\n  /** RGB aperture mask strength (0 to 1). */\n  mask?: number;\n  /** Brightness flicker and rolling bar strength (0 to 1). */\n  flicker?: 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 vec3 uC0;\nuniform vec3 uC1;\nuniform vec3 uC2;\nuniform float uCurve;\nuniform float uScan;\nuniform float uMask;\nuniform float uFlicker;\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}\nfloat noise(vec2 p) {\n  vec2 i = floor(p), f = fract(p);\n  vec2 u = f * f * (3.0 - 2.0 * f);\n  return mix(mix(hash(i), hash(i + vec2(1, 0)), u.x), mix(hash(i + vec2(0, 1)), hash(i + vec2(1, 1)), u.x), u.y);\n}\nfloat fbm(vec2 p) {\n  float v = 0.0, a = 0.5;\n  mat2 m = mat2(1.6, 1.2, -1.2, 1.6);\n  for (int i = 0; i < 4; i++) { v += a * noise(p); p = m * p; a *= 0.5; }\n  return v;\n}\n\n// The picture on the tube: a slow, glowing three-color gradient.\nvec3 scene(vec2 uv) {\n  float asp = uRes.x / uRes.y;\n  vec2 p = vec2((uv.x - 0.5) * asp, uv.y - 0.5);\n  float t = uTime * 0.2;\n  float n = fbm(p * 1.5 + vec2(t * 0.6, -t * 0.4));\n  float g = uv.y * 0.95 + (n - 0.5) * 0.75;\n  vec3 col = mix(uC0, uC1, smoothstep(0.02, 0.5, g));\n  col = mix(col, uC2, smoothstep(0.45, 0.95, g));\n  vec2 c1 = vec2(sin(t * 0.9) * 0.35 * asp, cos(t * 0.7) * 0.15 + 0.05);\n  vec2 c2 = vec2(cos(t * 0.6 + 2.0) * 0.4 * asp, sin(t * 0.8) * 0.2 - 0.1);\n  col += uC2 * 0.6 * exp(-dot(p - c1, p - c1) * 5.0);\n  col += uC1 * 0.45 * exp(-dot(p - c2, p - c2) * 4.0);\n  return col;\n}\n\nvoid main() {\n  vec2 uv = gl_FragCoord.xy / uRes;\n  vec2 cc = uv * 2.0 - 1.0;\n  // Barrel distortion: push the image outward the further it is from the center.\n  cc *= 1.0 + uCurve * vec2(0.08, 0.12) * dot(cc, cc);\n  cc *= 1.0 + uCurve * 0.03;\n  vec2 suv = cc * 0.5 + 0.5;\n\n  // Rounded screen edge.\n  float rad = 0.04 + 0.08 * uCurve;\n  vec2 q = abs(cc) - (1.0 - rad);\n  float box = length(max(q, 0.0)) + min(max(q.x, q.y), 0.0) - rad;\n  float inside = 1.0 - smoothstep(-0.004, 0.004, box);\n\n  // Rolling bar with a slight horizontal wobble.\n  float roll = fract(suv.y * 0.6 - uClock * 0.09);\n  float rb = (roll - 0.5) / 0.07;\n  float bar = exp(-rb * rb) * uFlicker;\n  suv.x += bar * 0.002 * sin(suv.y * 200.0 + uClock * 20.0);\n\n  // Chromatic fringing grows toward the edges.\n  vec2 off = cc * (0.0025 + 0.004 * uCurve);\n  vec3 col = vec3(scene(suv + off).r, scene(suv).g, scene(suv - off).b);\n\n  float sl = 0.5 + 0.5 * cos(suv.y * uRes.y / uDpr / 3.0 * 6.2832);\n  col *= 1.0 - uScan * 0.55 * sl;\n\n  float m = mod(floor(gl_FragCoord.x / uDpr), 3.0);\n  vec3 grille = vec3(step(m, 0.5), step(0.5, m) * step(m, 1.5), step(1.5, m));\n  col *= mix(vec3(1.0), 0.55 + grille * 0.9, uMask * 0.55);\n\n  col *= 1.0 + bar * 0.18;\n  float flick = sin(uClock * 110.0) * 0.5 + 0.5;\n  col *= 1.0 - uFlicker * (0.03 * flick + 0.05 * hash(vec2(floor(uClock * 24.0), 3.0)));\n  col += (hash(gl_FragCoord.xy + fract(uClock) * 311.0) - 0.5) * 0.05;\n  col *= 1.25 - 0.6 * dot(cc * 0.72, cc * 0.72);\n\n  // Dark bezel catching a faint reflection of the screen glow.\n  vec3 bezel = vec3(0.012, 0.012, 0.016) + scene(clamp(suv, 0.0, 1.0)) * 0.12 * exp(-max(box, 0.0) * 22.0);\n  gl_FragColor = vec4(mix(bezel, 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 CrtWarp({\n  colors = [\"#1a0b3d\", \"#ff2e88\", \"#ffb347\"],\n  speed = 1,\n  curvature = 0.5,\n  scanlines = 0.6,\n  mask = 0.5,\n  flicker = 0.5,\n  className,\n}: CrtWarpProps) {\n  const host = useRef<HTMLDivElement>(null);\n  const opts = useRef({ colors, speed, curvature, scanlines, mask, flicker });\n  const redraw = useRef<(() => void) | null>(null);\n\n  useEffect(() => {\n    opts.current = { colors, speed, curvature, scanlines, mask, flicker };\n    redraw.current?.();\n  }, [colors, speed, curvature, scanlines, mask, flicker]);\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    let raf = 0;\n    let visible = true;\n    let time = 6;\n    let clock = 3;\n    let last = 0;\n    let dpr = 1;\n\n    const draw = () => {\n      const o = opts.current;\n      const list = o.colors.length ? o.colors : [\"#ffffff\"];\n      for (let i = 0; i < 3; i++) gl.uniform3fv(u(`uC${i}`), rgb(list[i % list.length]));\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.uniform1f(u(\"uCurve\"), Math.max(o.curvature, 0));\n      gl.uniform1f(u(\"uScan\"), Math.min(Math.max(o.scanlines, 0), 1));\n      gl.uniform1f(u(\"uMask\"), Math.min(Math.max(o.mask, 0), 1));\n      gl.uniform1f(u(\"uFlicker\"), Math.min(Math.max(o.flicker, 0), 1));\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      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\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      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"}]}