{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "bg-crt-warp",
  "type": "registry:component",
  "title": "CRT Warp",
  "description": "An old tube screen: curved frame, scanlines, color fringing, flicker and a rolling bar. The pointer pushes a lens of distortion across the glass. Shows an animated test pattern when it has no children.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "overlay",
    "background",
    "crt",
    "retro",
    "scanlines",
    "pointer",
    "canvas"
  ],
  "docs": "https://hyperblur.dev/effects/bg-crt-warp",
  "files": [
    {
      "path": "registry/hyperblur/bg-crt-warp.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/bg-crt-warp.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface BgCrtWarpProps extends React.HTMLAttributes<HTMLDivElement> {\n  accent?: string;\n  frameColor?: string;\n  curvature?: number;\n  scanlines?: number;\n  fringe?: number;\n  flicker?: number;\n  bulge?: number;\n  radius?: number;\n  speed?: number;\n}\n\nconst css = `\n.hb-qo4 { position: absolute; inset: 0; overflow: hidden; pointer-events: none; box-sizing: border-box; background: #050505; }\n.hb-qo4-c { position: absolute; inset: 0; z-index: 2; display: block; width: 100%; height: 100%; pointer-events: none; }\n.hb-qo4-in { position: relative; z-index: 1; width: 100%; height: 100%; pointer-events: auto; font-family: inherit; }\n`;\n\nconst BARS: [number, number, number][] = [\n  [196, 196, 196],\n  [196, 190, 24],\n  [24, 186, 196],\n  [24, 186, 60],\n  [190, 40, 190],\n  [196, 36, 44],\n  [36, 52, 196],\n];\n\nconst LOW: [number, number, number][] = [\n  [36, 52, 196],\n  [10, 10, 14],\n  [190, 40, 190],\n  [10, 10, 14],\n  [24, 186, 196],\n  [10, 10, 14],\n  [196, 196, 196],\n];\n\nfunction toRgb(value: string): [number, number, number] {\n  const c = document.createElement(\"canvas\");\n  c.width = 1;\n  c.height = 1;\n  const x = c.getContext(\"2d\");\n  if (!x) return [47, 123, 255];\n  x.fillStyle = \"#2f7bff\";\n  x.fillStyle = value;\n  x.fillRect(0, 0, 1, 1);\n  const d = x.getImageData(0, 0, 1, 1).data;\n  return [d[0], d[1], d[2]];\n}\n\nexport function BgCrtWarp({\n  accent = \"#2f7bff\",\n  frameColor = \"#050505\",\n  curvature = 0.12,\n  scanlines = 0.24,\n  fringe = 1,\n  flicker = 0.05,\n  bulge = 0.35,\n  radius = 90,\n  speed = 1,\n  className = \"\",\n  style,\n  children,\n  ...rest\n}: BgCrtWarpProps) {\n  const wrapRef = React.useRef<HTMLDivElement>(null);\n  const canvasRef = React.useRef<HTMLCanvasElement>(null);\n  const hasChildren = children != null && children !== false;\n\n  React.useEffect(() => {\n    const wrap = wrapRef.current;\n    const host = wrap?.parentElement;\n    const canvas = canvasRef.current;\n    const context = canvas?.getContext(\"2d\");\n    if (!wrap || !host || !canvas || !context) return;\n    const g = context;\n    const motion = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n\n    const [ar, ag, ab] = toRgb(accent);\n    const curv = Math.max(0, curvature);\n    const buffer = document.createElement(\"canvas\");\n    const bctx = buffer.getContext(\"2d\");\n    let img: ImageData | null = null;\n    let w = 0;\n    let h = 0;\n    let bw = 0;\n    let bh = 0;\n    let t = 0;\n    let inside = false;\n    let tx = 0;\n    let ty = 0;\n    let px = 0;\n    let py = 0;\n    let hover = 0;\n    let placed = false;\n    let frameX = new Float32Array(0);\n    let frameY = new Float32Array(0);\n    const out: [number, number, number] = [0, 0, 0];\n\n    const idleX = (tt: number) => w * (0.5 + 0.3 * Math.sin(tt * 0.41 + 1.1));\n    const idleY = (tt: number) => h * (0.5 + 0.26 * Math.sin(tt * 0.67 + 0.2));\n\n    const track = (dt: number) => {\n      const gx = inside ? tx : idleX(t);\n      const gy = inside ? ty : idleY(t);\n      if (!placed) {\n        px = gx;\n        py = gy;\n        placed = true;\n      } else {\n        const e = 1 - Math.exp(-dt * (inside ? 9 : 2));\n        px += (gx - px) * e;\n        py += (gy - py) * e;\n      }\n      hover += ((inside ? 1 : 0) - hover) * Math.min(1, dt * 4);\n    };\n\n    const pattern = (x: number, y: number, asp: number) => {\n      const u = (x + 1) / 2;\n      const v = (y + 1) / 2;\n      let r: number;\n      let gg: number;\n      let b: number;\n      if (v < 0.6) {\n        const bar = BARS[Math.min(6, Math.max(0, Math.floor(u * 7)))];\n        const m = 0.82 + 0.18 * Math.sin(t * 1.6 + u * 9);\n        const s = 0.5 + 0.5 * Math.sin((u * 3 + v * 2) * Math.PI - t * 1.4);\n        r = bar[0] * m + ar * 0.32 * s;\n        gg = bar[1] * m + ag * 0.32 * s;\n        b = bar[2] * m + ab * 0.32 * s;\n      } else if (v < 0.68) {\n        const bar = LOW[Math.min(6, Math.max(0, Math.floor(u * 7)))];\n        r = bar[0] * 0.7;\n        gg = bar[1] * 0.7;\n        b = bar[2] * 0.7;\n      } else {\n        const band = 0.5 + 0.5 * Math.sin(u * 12 - t * 2.6);\n        const k = Math.min(1, Math.max(0, u * 1.15 - 0.05));\n        const lum = k * (0.55 + 0.45 * band);\n        const mix = Math.max(0, k - 0.6) / 0.4;\n        r = (ar * (1 - mix) + 235 * mix) * lum;\n        gg = (ag * (1 - mix) + 235 * mix) * lum;\n        b = (ab * (1 - mix) + 235 * mix) * lum;\n      }\n      const d = Math.sqrt(x * asp * x * asp + y * y);\n      const ring = Math.abs(d - 0.62);\n      if (ring < 0.014 || (d < 0.62 && (Math.abs(x * asp) < 0.006 || Math.abs(y) < 0.008))) {\n        r = r * 0.3 + 230;\n        gg = gg * 0.3 + 230;\n        b = b * 0.3 + 230;\n      }\n      out[0] = r;\n      out[1] = gg;\n      out[2] = b;\n    };\n\n    const renderPattern = () => {\n      if (!bctx || !img || !bw || !bh) return;\n      const data = img.data;\n      const asp = w / h;\n      const rn = Math.max(0.05, radius / (h / 2));\n      const inv = 1 / (rn * rn);\n      const pnx = (px / w) * 2 - 1;\n      const pny = (py / h) * 2 - 1;\n      const bul = bulge * (0.6 + 0.4 * hover);\n      const fr = 0.035 * fringe;\n      let o = 0;\n      for (let j = 0; j < bh; j++) {\n        const ny = ((j + 0.5) / bh) * 2 - 1;\n        for (let i = 0; i < bw; i++) {\n          const nx = ((i + 0.5) / bw) * 2 - 1;\n          const dx = (nx - pnx) * asp;\n          const dy = ny - pny;\n          const q = 1 - bul * Math.exp(-(dx * dx + dy * dy) * inv);\n          const ax = pnx + (nx - pnx) * q;\n          const ay = pny + (ny - pny) * q;\n          const r2 = ax * ax + ay * ay;\n          const f = 1 + curv * r2;\n          const sx = ax * f;\n          const sy = ay * f;\n          if (sx < -1 || sx > 1 || sy < -1 || sy > 1) {\n            data[o] = 0;\n            data[o + 1] = 0;\n            data[o + 2] = 0;\n            data[o + 3] = 255;\n            o += 4;\n            continue;\n          }\n          const cr = 1 + fr * r2;\n          pattern(sx * cr, sy, asp);\n          data[o] = out[0];\n          pattern(sx, sy, asp);\n          data[o + 1] = out[1];\n          pattern(sx / cr, sy, asp);\n          data[o + 2] = out[2];\n          data[o + 3] = 255;\n          o += 4;\n        }\n      }\n      bctx.putImageData(img, 0, 0);\n      g.imageSmoothingEnabled = true;\n      g.drawImage(buffer, 0, 0, w, h);\n    };\n\n    const unwarp = (sx: number, sy: number) => {\n      let nx = sx;\n      let ny = sy;\n      for (let k = 0; k < 4; k++) {\n        const f = 1 + curv * (nx * nx + ny * ny);\n        nx = sx / f;\n        ny = sy / f;\n      }\n      return [nx, ny];\n    };\n\n    const layout = () => {\n      const scale = Math.min(0.5, Math.sqrt(60000 / (w * h)));\n      bw = Math.max(8, Math.round(w * scale));\n      bh = Math.max(8, Math.round(h * scale));\n      buffer.width = bw;\n      buffer.height = bh;\n      img = bctx ? bctx.createImageData(bw, bh) : null;\n      const S = 32;\n      frameX = new Float32Array(S * 4);\n      frameY = new Float32Array(S * 4);\n      for (let e = 0; e < 4; e++) {\n        for (let k = 0; k < S; k++) {\n          const p = (k / S) * 2 - 1;\n          const sx = e === 0 ? p : e === 1 ? 1 : e === 2 ? -p : -1;\n          const sy = e === 0 ? -1 : e === 1 ? p : e === 2 ? 1 : -p;\n          const [nx, ny] = unwarp(sx, sy);\n          frameX[e * S + k] = ((nx + 1) / 2) * w;\n          frameY[e * S + k] = ((ny + 1) / 2) * h;\n        }\n      }\n    };\n\n    const screenPath = () => {\n      g.moveTo(frameX[0], frameY[0]);\n      for (let k = 1; k < frameX.length; k++) g.lineTo(frameX[k], frameY[k]);\n      g.closePath();\n    };\n\n    const overlay = () => {\n      const asp = w / h;\n      const rn = Math.max(0.05, radius / (h / 2));\n      const inv = 1 / (rn * rn);\n      const pnx = (px / w) * 2 - 1;\n      const pny = (py / h) * 2 - 1;\n      const bul = bulge * (0.6 + 0.4 * hover);\n      const stepX = Math.max(8, w / 60);\n      g.beginPath();\n      for (let y = 1.5; y < h; y += 3) {\n        const ny0 = (y / h) * 2 - 1;\n        for (let x = 0; x <= w + stepX; x += stepX) {\n          const nx = (Math.min(x, w) / w) * 2 - 1;\n          let ny = ny0 / (1 + curv * (nx * nx + ny0 * ny0));\n          const dx = (nx - pnx) * asp;\n          const dy = ny - pny;\n          ny += dy * bul * 0.9 * Math.exp(-(dx * dx + dy * dy) * inv);\n          const yy = ((ny + 1) / 2) * h;\n          if (x === 0) g.moveTo(0, yy);\n          else g.lineTo(Math.min(x, w), yy);\n        }\n      }\n      g.strokeStyle = `rgba(0,0,0,${Math.min(1, scanlines).toFixed(3)})`;\n      g.lineWidth = 1.2;\n      g.stroke();\n\n      const lens = g.createRadialGradient(px, py, 0, px, py, radius * 1.1);\n      lens.addColorStop(0, `rgba(255,255,255,${(0.07 * (0.5 + 0.5 * hover)).toFixed(3)})`);\n      lens.addColorStop(1, \"rgba(255,255,255,0)\");\n      g.fillStyle = lens;\n      g.fillRect(0, 0, w, h);\n\n      const roll = ((t * 0.14) % 1.4) - 0.2;\n      const ry = roll * h;\n      const bar = g.createLinearGradient(0, ry - h * 0.12, 0, ry + h * 0.12);\n      bar.addColorStop(0, \"rgba(255,255,255,0)\");\n      bar.addColorStop(0.5, \"rgba(255,255,255,0.06)\");\n      bar.addColorStop(1, \"rgba(255,255,255,0)\");\n      g.fillStyle = bar;\n      g.fillRect(0, ry - h * 0.12, w, h * 0.24);\n\n      const tick = Math.floor(t * 14);\n      const hz = ((Math.imul(tick, 2654435761) >>> 0) % 1000) / 1000;\n      const dim = flicker * (0.45 + 0.35 * Math.sin(t * 43) + (hz > 0.93 ? 1.2 : 0));\n      if (dim > 0) {\n        g.fillStyle = `rgba(0,0,0,${Math.min(0.6, dim).toFixed(3)})`;\n        g.fillRect(0, 0, w, h);\n      }\n\n      const vig = g.createRadialGradient(w / 2, h / 2, Math.min(w, h) * 0.3, w / 2, h / 2, Math.hypot(w, h) * 0.55);\n      vig.addColorStop(0, \"rgba(0,0,0,0)\");\n      vig.addColorStop(1, \"rgba(0,0,0,0.62)\");\n      g.fillStyle = vig;\n      g.fillRect(0, 0, w, h);\n\n      if (fringe > 0) {\n        g.globalCompositeOperation = \"lighter\";\n        g.lineWidth = 2;\n        g.save();\n        g.translate(-1.5 * fringe, 0);\n        g.beginPath();\n        screenPath();\n        g.strokeStyle = \"rgba(255,40,90,0.35)\";\n        g.stroke();\n        g.restore();\n        g.save();\n        g.translate(1.5 * fringe, 0);\n        g.beginPath();\n        screenPath();\n        g.strokeStyle = \"rgba(40,200,255,0.35)\";\n        g.stroke();\n        g.restore();\n        g.globalCompositeOperation = \"source-over\";\n      }\n\n      g.beginPath();\n      screenPath();\n      g.lineWidth = 16;\n      g.strokeStyle = \"rgba(0,0,0,0.55)\";\n      g.stroke();\n      g.beginPath();\n      g.rect(-2, -2, w + 4, h + 4);\n      screenPath();\n      g.fillStyle = frameColor;\n      g.fill(\"evenodd\");\n      g.beginPath();\n      screenPath();\n      g.lineWidth = 1;\n      g.strokeStyle = \"rgba(255,255,255,0.07)\";\n      g.stroke();\n    };\n\n    const draw = () => {\n      if (!w || !h) return;\n      g.clearRect(0, 0, w, h);\n      if (!hasChildren) renderPattern();\n      overlay();\n    };\n\n    const resize = () => {\n      const nw = wrap.clientWidth;\n      const nh = wrap.clientHeight;\n      if (!nw || !nh) return;\n      const dpr = Math.min(2, window.devicePixelRatio || 1);\n      w = nw;\n      h = nh;\n      canvas.width = Math.max(1, Math.round(w * dpr));\n      canvas.height = Math.max(1, Math.round(h * dpr));\n      g.setTransform(dpr, 0, 0, dpr, 0, 0);\n      layout();\n      if (motion.matches) still();\n      else {\n        track(0);\n        draw();\n      }\n    };\n\n    let visible = true;\n    let raf = 0;\n    let prev = 0;\n\n    const still = () => {\n      t = 0.9;\n      inside = false;\n      placed = false;\n      hover = 0;\n      track(0);\n      draw();\n    };\n\n    const frame = (now: number) => {\n      const dt = Math.min(0.033, (now - prev) / 1000);\n      prev = now;\n      t += dt * speed;\n      track(dt);\n      draw();\n      raf = requestAnimationFrame(frame);\n    };\n\n    const sync = () => {\n      const run = visible && !document.hidden && !motion.matches;\n      if (run && !raf) {\n        prev = performance.now();\n        raf = requestAnimationFrame(frame);\n      } else if (!run && raf) {\n        cancelAnimationFrame(raf);\n        raf = 0;\n      }\n      if (motion.matches) still();\n    };\n\n    const move = (e: PointerEvent) => {\n      if (motion.matches) return;\n      const rect = host.getBoundingClientRect();\n      const sx = rect.width ? host.clientWidth / rect.width : 1;\n      const sy = rect.height ? host.clientHeight / rect.height : 1;\n      tx = (e.clientX - rect.left) * sx;\n      ty = (e.clientY - rect.top) * sy;\n      inside = true;\n    };\n\n    const leave = (e: PointerEvent) => {\n      if (e.type === \"pointerup\" && e.pointerType === \"mouse\") return;\n      inside = false;\n    };\n\n    const ro = new ResizeObserver(resize);\n    const io = new IntersectionObserver((entries) => {\n      visible = entries[entries.length - 1].isIntersecting;\n      sync();\n    });\n    ro.observe(wrap);\n    io.observe(wrap);\n    document.addEventListener(\"visibilitychange\", sync);\n    motion.addEventListener(\"change\", sync);\n    host.addEventListener(\"pointermove\", move, { passive: true });\n    host.addEventListener(\"pointerdown\", move, { passive: true });\n    host.addEventListener(\"pointerleave\", leave);\n    host.addEventListener(\"pointerup\", leave);\n    host.addEventListener(\"pointercancel\", leave);\n    resize();\n    sync();\n\n    return () => {\n      if (raf) cancelAnimationFrame(raf);\n      ro.disconnect();\n      io.disconnect();\n      document.removeEventListener(\"visibilitychange\", sync);\n      motion.removeEventListener(\"change\", sync);\n      host.removeEventListener(\"pointermove\", move);\n      host.removeEventListener(\"pointerdown\", move);\n      host.removeEventListener(\"pointerleave\", leave);\n      host.removeEventListener(\"pointerup\", leave);\n      host.removeEventListener(\"pointercancel\", leave);\n    };\n  }, [accent, frameColor, curvature, scanlines, fringe, flicker, bulge, radius, speed, hasChildren]);\n\n  return (\n    <div ref={wrapRef} className={`hb-qo4 ${className}`} style={style} {...rest}>\n      <style href=\"hb-bg-crt-warp\" precedence=\"default\">{css}</style>\n      {hasChildren && <div className=\"hb-qo4-in\">{children}</div>}\n      <canvas ref={canvasRef} className=\"hb-qo4-c\" aria-hidden=\"true\" />\n    </div>\n  );\n}\n"
    }
  ]
}