{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"ring-tunnel","type":"registry:ui","title":"Ring Tunnel","description":"An endless flight through a bending tunnel of glowing rings and spark-lit lines. WebGL.","dependencies":[],"registryDependencies":[],"files":[{"path":"registry/ui/ring-tunnel.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useEffect, useRef } from \"react\";\n\nexport interface RingTunnelProps {\n  colorA?: string;\n  colorB?: string;\n  /** Flight speed multiplier. */\n  speed?: number;\n  /** Rings per unit of depth. */\n  density?: number;\n  /** Number of lengthwise lines; 0 hides them. */\n  lines?: number;\n  /** Spiral of the lengthwise lines. */\n  twist?: number;\n  /** Line glow strength. */\n  glow?: number;\n  /** The vanishing point drifts toward the cursor. */\n  interactive?: boolean;\n  className?: string;\n}\n\nconst FRAG = `precision highp float;\nuniform vec2 uRes;\nuniform float uTime;\nuniform vec3 uColorA;\nuniform vec3 uColorB;\nuniform float uDensity;\nuniform float uLines;\nuniform float uTwist;\nuniform float uGlow;\nuniform vec2 uCenter;\n\nfloat hash(float n){ return fract(sin(n * 12.9898) * 43758.5453); }\n\nvoid main(){\n  vec2 p = (gl_FragCoord.xy - 0.5 * uRes) / uRes.y - uCenter;\n  float t = uTime;\n\n  // Bend the tunnel: offset each pixel by a path sampled at its own depth.\n  float z0 = 0.35 / max(length(p), 1e-3);\n  p -= vec2(sin(z0 * 0.21 + t * 0.35), cos(z0 * 0.17 + t * 0.28)) * 0.035 * min(z0, 12.0) / 12.0;\n\n  float r = max(length(p), 1e-3);\n  float a = atan(p.y, p.x);\n  float z = 0.35 / r;\n  float w = 0.003 * uGlow;\n\n  // Rings: distance to the nearest ring in depth, projected back to screen space.\n  float rc = z * uDensity + t * 1.6;\n  float fr = fract(rc);\n  float sd = min(fr, 1.0 - fr) / uDensity * r * r / 0.35;\n  float id = floor(rc + 0.5);\n  float pulse = 0.45 + 0.55 * pow(0.5 + 0.5 * sin(t * 2.0 + id * 1.7), 3.0);\n  float ring = (exp(-sd / w) + 0.5 * w * 3.0 / (sd + w * 3.0)) * pulse * (0.5 + 0.5 * hash(id));\n\n  // Lengthwise lines with sparks racing along them.\n  float line = 0.0;\n  if (uLines > 0.5) {\n    float la = a / 6.2831853 * uLines + z * uTwist * 0.15;\n    float fa = fract(la);\n    float da = min(fa, 1.0 - fa) * 6.2831853 / uLines * r;\n    float lid = mod(floor(la + 0.5), uLines);\n    float spark = pow(fract(-z * 0.12 - t * 0.9 + hash(lid) * 7.0), 12.0);\n    line = exp(-da / (w * 0.7)) * (0.15 + spark * 1.6) + 0.07 * w / (da + w);\n  }\n\n  float fog = smoothstep(0.0, 0.22, r) * exp(-z * 0.03);\n  float mixv = 0.5 + 0.5 * sin(z * 0.4 - t * 0.6);\n  vec3 tint = mix(uColorA, uColorB, mixv);\n  vec3 col = tint * (ring + line + 0.03) * fog;\n  col += mix(uColorB, vec3(1.0), 0.3) * 0.018 / (r + 0.025); // light at the far end\n  col += vec3(1.0) * pow(ring * fog, 3.0) * 0.25;\n  col += (fract(sin(dot(gl_FragCoord.xy, vec2(12.9898, 78.233))) * 43758.5453) - 0.5) / 255.0;\n  col = clamp(col, 0.0, 1.0);\n  gl_FragColor = vec4(col, max(col.r, max(col.g, col.b)));\n}`;\n\nfunction hexToRgb(hex: string) {\n  const n = parseInt(hex.replace(\"#\", \"\").padEnd(6, \"0\").slice(0, 6), 16);\n  return [((n >> 16) & 255) / 255, ((n >> 8) & 255) / 255, (n & 255) / 255];\n}\n\ntype SetUniform = (name: string, v: number | number[], size?: number) => void;\n\n/** Full-screen-triangle WebGL runner: sizes to its host, pauses offscreen, one static frame under reduced motion. */\nfunction mountShader(host: HTMLElement, frag: string, frame: (set: SetUniform, dt: number) => void) {\n  const canvas = document.createElement(\"canvas\");\n  canvas.style.cssText = \"position:absolute;inset:0;width:100%;height:100%;display:block\";\n  const gl = canvas.getContext(\"webgl\", { antialias: false, premultipliedAlpha: true });\n  if (!gl) return null;\n  const prog = gl.createProgram()!;\n  const stages: [number, string][] = [\n    [gl.VERTEX_SHADER, \"attribute vec2 p;void main(){gl_Position=vec4(p,0.,1.);}\"],\n    [gl.FRAGMENT_SHADER, frag],\n  ];\n  for (const [type, src] of stages) {\n    const s = gl.createShader(type)!;\n    gl.shaderSource(s, src);\n    gl.compileShader(s);\n    if (!gl.getShaderParameter(s, gl.COMPILE_STATUS)) console.warn(gl.getShaderInfoLog(s));\n    gl.attachShader(prog, s);\n  }\n  gl.bindAttribLocation(prog, 0, \"p\");\n  gl.linkProgram(prog);\n  if (!gl.getProgramParameter(prog, gl.LINK_STATUS)) {\n    gl.getExtension(\"WEBGL_lose_context\")?.loseContext();\n    return null;\n  }\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  host.appendChild(canvas);\n\n  const locs = new Map<string, WebGLUniformLocation | null>();\n  const setters = [gl.uniform1fv, gl.uniform2fv, gl.uniform3fv, gl.uniform4fv];\n  const set: SetUniform = (name, v, size) => {\n    if (!locs.has(name)) locs.set(name, gl.getUniformLocation(prog, name));\n    const arr = typeof v === \"number\" ? [v] : v;\n    setters[(size ?? arr.length) - 1].call(gl, locs.get(name)!, arr);\n  };\n\n  const reduced = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n  let raf = 0;\n  let last = 0;\n  const render = (dt: number) => {\n    set(\"uRes\", [canvas.width, canvas.height]);\n    frame(set, dt);\n    gl.drawArrays(gl.TRIANGLES, 0, 3);\n  };\n  const loop = (now: number) => {\n    render(last ? Math.min((now - last) / 1000, 0.1) : 0);\n    last = now;\n    raf = requestAnimationFrame(loop);\n  };\n  const resize = () => {\n    const dpr = Math.min(window.devicePixelRatio || 1, 2);\n    canvas.width = Math.max(1, Math.round(host.clientWidth * dpr));\n    canvas.height = Math.max(1, Math.round(host.clientHeight * dpr));\n    gl.viewport(0, 0, canvas.width, canvas.height);\n    render(0);\n  };\n  const ro = new ResizeObserver(resize);\n  ro.observe(host);\n  const io = new IntersectionObserver(([e]) => {\n    cancelAnimationFrame(raf);\n    raf = 0;\n    last = 0;\n    if (e.isIntersecting && !reduced) raf = requestAnimationFrame(loop);\n  });\n  io.observe(host);\n\n  return {\n    redraw: () => {\n      if (!raf) render(0);\n    },\n    destroy: () => {\n      cancelAnimationFrame(raf);\n      ro.disconnect();\n      io.disconnect();\n      gl.getExtension(\"WEBGL_lose_context\")?.loseContext();\n      canvas.remove();\n    },\n  };\n}\n\nexport function RingTunnel({\n  colorA = \"#38bdf8\",\n  colorB = \"#f472b6\",\n  speed = 1,\n  density = 2,\n  lines = 16,\n  twist = 1,\n  glow = 1,\n  interactive = true,\n  className,\n}: RingTunnelProps) {\n  const host = useRef<HTMLDivElement>(null);\n  const opts = useRef({ a: hexToRgb(colorA), b: hexToRgb(colorB), speed, density, lines, twist, glow, interactive });\n  const redraw = useRef<() => void>(undefined);\n\n  useEffect(() => {\n    opts.current = { a: hexToRgb(colorA), b: hexToRgb(colorB), speed, density, lines, twist, glow, interactive };\n    redraw.current?.();\n  }, [colorA, colorB, speed, density, lines, twist, glow, interactive]);\n\n  useEffect(() => {\n    const el = host.current!;\n    const area = el.parentElement ?? el;\n    const c = { x: 0, y: 0, tx: 0, ty: 0 };\n    let t = 5;\n    const shader = mountShader(el, FRAG, (set, dt) => {\n      const o = opts.current;\n      t += dt * o.speed;\n      const k = 1 - Math.exp(-dt * 2.5);\n      c.x += ((o.interactive ? c.tx : 0) - c.x) * k;\n      c.y += ((o.interactive ? c.ty : 0) - c.y) * k;\n      set(\"uTime\", t);\n      set(\"uColorA\", o.a);\n      set(\"uColorB\", o.b);\n      set(\"uDensity\", Math.max(o.density, 0.1));\n      set(\"uLines\", Math.round(o.lines));\n      set(\"uTwist\", o.twist);\n      set(\"uGlow\", Math.max(o.glow, 0.05));\n      set(\"uCenter\", [c.x, c.y]);\n    });\n    if (!shader) return;\n    redraw.current = shader.redraw;\n\n    const onMove = (e: PointerEvent) => {\n      const r = el.getBoundingClientRect();\n      c.tx = ((e.clientX - r.left - r.width / 2) / r.height) * 0.25;\n      c.ty = (-(e.clientY - r.top - r.height / 2) / r.height) * 0.25;\n    };\n    const onLeave = () => {\n      c.tx = 0;\n      c.ty = 0;\n    };\n    area.addEventListener(\"pointermove\", onMove, { passive: true });\n    area.addEventListener(\"pointerleave\", onLeave);\n    return () => {\n      redraw.current = undefined;\n      area.removeEventListener(\"pointermove\", onMove);\n      area.removeEventListener(\"pointerleave\", onLeave);\n      shader.destroy();\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"}]}