{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "flow-lines-background",
  "title": "Flow Lines Background",
  "description": "Animated flowing gradient lines background driven by Perlin noise, powered by WebGPU.",
  "dependencies": ["vgpu", "@vgpu/wgsl"],
  "registryDependencies": ["utils"],
  "files": [
    {
      "path": "registry/primitives/shaders/flow-lines-background/index.tsx",
      "content": "\"use client\";\n\nimport { prepareShader } from \"@vgpu/wgsl/prepare\";\nimport { cn } from \"@/lib/utils\";\nimport type { ComponentPropsWithoutRef, Ref } from \"react\";\nimport { useEffect, useRef, useState } from \"react\";\nimport type { Effect, FrameLoopHandle, Gpu, Surface } from \"vgpu\";\nimport { effect, frameLoop, init, surface } from \"vgpu\";\n\nconst FLOW_LINES_SHADER = /* wgsl */ `\nstruct Params {\n  color1: vec3f,\n  color2: vec3f,\n  color3: vec3f,\n  colorAccent: vec3f,\n  planeRes: vec2f,\n  mouse2D: vec2f,\n  linesBlur: f32,\n  noise: f32,\n  offsetX: f32,\n  offsetY: f32,\n  linesAmount: f32,\n  backgroundScale: f32,\n  time: f32,\n}\n\n@group(0) @binding(0) var<uniform> params: Params;\n\nfn mod289_v4(x: vec4f) -> vec4f {\n  return x - 289.0 * floor(x / 289.0);\n}\n\nfn permute(x: vec4f) -> vec4f {\n  return mod289_v4(((x * 34.0) + 1.0) * x);\n}\n\nfn fade(t: vec2f) -> vec2f {\n  return t * t * t * (t * (t * 6.0 - 15.0) + 10.0);\n}\n\nfn cnoise2(P: vec2f) -> f32 {\n  var Pi = floor(vec4f(P.x, P.y, P.x, P.y)) + vec4f(0.0, 0.0, 1.0, 1.0);\n  let Pf = fract(vec4f(P.x, P.y, P.x, P.y)) - vec4f(0.0, 0.0, 1.0, 1.0);\n  Pi = mod289_v4(Pi);\n  let ix = vec4f(Pi.x, Pi.z, Pi.x, Pi.z);\n  let iy = vec4f(Pi.y, Pi.y, Pi.w, Pi.w);\n  let fx = vec4f(Pf.x, Pf.z, Pf.x, Pf.z);\n  let fy = vec4f(Pf.y, Pf.y, Pf.w, Pf.w);\n  let i = permute(permute(ix) + iy);\n  var gx = 2.0 * fract(i * 0.0243902439) - 1.0;\n  let gy = abs(gx) - 0.5;\n  let tx = floor(gx + 0.5);\n  gx = gx - tx;\n  let g00 = vec2f(gx.x, gy.x);\n  let g10 = vec2f(gx.y, gy.y);\n  let g01 = vec2f(gx.z, gy.z);\n  let g11 = vec2f(gx.w, gy.w);\n  let norm = 1.79284291400159 - 0.85373472095314 * vec4f(dot(g00, g00), dot(g01, g01), dot(g10, g10), dot(g11, g11));\n  let n00 = dot(g00 * norm.x, vec2f(fx.x, fy.x));\n  let n10 = dot(g10 * norm.z, vec2f(fx.y, fy.y));\n  let n01 = dot(g01 * norm.y, vec2f(fx.z, fy.z));\n  let n11 = dot(g11 * norm.w, vec2f(fx.w, fy.w));\n  let fade_xy = fade(vec2f(Pf.x, Pf.y));\n  let n_x = mix(vec2f(n00, n01), vec2f(n10, n11), fade_xy.x);\n  let n_xy = mix(n_x.x, n_x.y, fade_xy.y);\n  return 2.3 * n_xy;\n}\n\nfn lines(uv: vec2f, offset: f32) -> f32 {\n  let a = abs(0.5 * sin(uv.y * params.linesAmount) + offset * params.linesBlur);\n  return smoothstep(0.0, params.linesBlur + offset * params.linesBlur, a);\n}\n\nfn random(p: vec2f) -> f32 {\n  let k1 = vec2f(23.14069263277926, 2.665144142690225);\n  return fract(cos(dot(p, k1)) * 12345.6789);\n}\n\nfn fadeLine(uv: vec2f, mouse2D: vec2f, col1: vec3f, col2: vec3f, col3: vec3f) -> vec3f {\n  let m = (mouse2D + 1.0) * 0.5;\n  let n1 = cnoise2(uv);\n  let n2 = cnoise2(uv + params.offsetX * 20.0);\n  let n3 = cnoise2(uv * 0.3 + params.offsetY * 10.0);\n  let nFinal = mix(mix(n1, n2, m.x), n3, m.y);\n  let baseUv = vec2f(nFinal + 2.05) * params.backgroundScale;\n\n  let basePattern = lines(baseUv, 0.1);\n  let secondPattern = lines(baseUv, 0.2);\n\n  let baseColor = mix(col1, col2, basePattern);\n  return mix(baseColor, col3, secondPattern);\n}\n\n@fragment\nfn fs_main(@location(0) uvIn: vec2f) -> @location(0) vec4f {\n  // match Three.js plane UV (bottom-left origin)\n  let vUv = vec2f(uvIn.x, 1.0 - uvIn.y);\n  var uv = vUv;\n  uv.y += params.offsetY;\n  uv.x += params.offsetX;\n  uv.x *= params.planeRes.x / params.planeRes.y;\n\n  let col = fadeLine(uv, params.mouse2D, params.color1, params.color2, params.color3);\n\n  var uvRandom = vUv;\n  uvRandom.y *= random(vec2f(uvRandom.y, 0.5));\n  let finalCol = col + random(uvRandom) * params.noise;\n\n  return vec4f(finalCol, 1.0);\n}\n`;\n\nconst compiledFlowLinesShader = prepareShader(FLOW_LINES_SHADER);\n\n// 1:1 defaults from giats-portfolio background\nconst DEFAULT_LIGHT_COLOR: [number, number, number] = [\n  63 / 255,\n  63 / 255,\n  63 / 255,\n];\nconst DEFAULT_MID_COLOR: [number, number, number] = [\n  38 / 255,\n  38 / 255,\n  38 / 255,\n];\nconst DEFAULT_DARK_COLOR: [number, number, number] = [\n  9 / 255,\n  5 / 255,\n  12 / 255,\n];\nconst DEFAULT_COLOR_ACCENT: [number, number, number] = [20, 20, 20];\n\nconst OFFSET_STEP = 0.00005;\nconst OFFSET_MAX = 5;\nconst OFFSET_MIN = 0.1;\n\nconst parseColorToRgb = (\n  color?: string,\n  fallback: [number, number, number] = [0, 0, 0]\n): [number, number, number] => {\n  if (!color || typeof color !== \"string\") {\n    return fallback;\n  }\n  const trimmed = color.trim();\n  if (trimmed.startsWith(\"#\")) {\n    const hex = trimmed.slice(1);\n    if (hex.length === 3 || hex.length === 4) {\n      const r = Number.parseInt(hex[0] + hex[0], 16) / 255;\n      const g = Number.parseInt(hex[1] + hex[1], 16) / 255;\n      const b = Number.parseInt(hex[2] + hex[2], 16) / 255;\n      if (!Number.isNaN(r) && !Number.isNaN(g) && !Number.isNaN(b)) {\n        return [r, g, b];\n      }\n    } else if (hex.length >= 6) {\n      const r = Number.parseInt(hex.slice(0, 2), 16) / 255;\n      const g = Number.parseInt(hex.slice(2, 4), 16) / 255;\n      const b = Number.parseInt(hex.slice(4, 6), 16) / 255;\n      if (!Number.isNaN(r) && !Number.isNaN(g) && !Number.isNaN(b)) {\n        return [r, g, b];\n      }\n    }\n  }\n  if (typeof document !== \"undefined\") {\n    try {\n      const canvas = document.createElement(\"canvas\");\n      canvas.width = 1;\n      canvas.height = 1;\n      const ctx = canvas.getContext(\"2d\", { willReadFrequently: true });\n      if (ctx) {\n        ctx.fillStyle = trimmed;\n        ctx.fillRect(0, 0, 1, 1);\n        const data = ctx.getImageData(0, 0, 1, 1).data;\n        return [data[0] / 255, data[1] / 255, data[2] / 255];\n      }\n    } catch {\n      // Ignore and fallback\n    }\n  }\n  return fallback;\n};\n\nfunction stepOffset(\n  value: number,\n  increasing: { current: boolean },\n  step: number,\n  min: number,\n  max: number\n) {\n  if (value >= max && increasing.current) {\n    increasing.current = false;\n  } else if (value <= min && !increasing.current) {\n    increasing.current = true;\n  }\n  return value + (increasing.current ? step : -step);\n}\n\nexport interface FlowLinesBackgroundProps extends ComponentPropsWithoutRef<\"canvas\"> {\n  /** Deepest tone of the flowing lines. @default \"#09050c\" */\n  darkColor?: string;\n  /** Lightest tone of the flowing lines. @default \"#3f3f3f\" */\n  lightColor?: string;\n  /** Mid tone blended between light and dark. @default \"#262626\" */\n  midColor?: string;\n  /** Ref forwarded to the underlying canvas element. */\n  ref?: Ref<HTMLCanvasElement>;\n}\n\nexport const FlowLinesBackground = ({\n  ref,\n  lightColor = \"#3f3f3f\",\n  midColor = \"#262626\",\n  darkColor = \"#09050c\",\n  className,\n  style,\n  ...props\n}: FlowLinesBackgroundProps) => {\n  const localRef = useRef<HTMLCanvasElement>(null);\n  const [unsupported, setUnsupported] = useState(false);\n  const bgRef = useRef<Effect | null>(null);\n  const colorsRef = useRef({ darkColor, lightColor, midColor });\n  colorsRef.current = { darkColor, lightColor, midColor };\n\n  // reactive color updates (1:1 structure preserved, colors adjustable)\n  useEffect(() => {\n    if (!bgRef.current) {\n      return;\n    }\n    bgRef.current.set({\n      params: {\n        color1: parseColorToRgb(lightColor, DEFAULT_LIGHT_COLOR),\n        color2: parseColorToRgb(midColor, DEFAULT_MID_COLOR),\n        color3: parseColorToRgb(darkColor, DEFAULT_DARK_COLOR),\n      },\n    });\n  }, [lightColor, midColor, darkColor]);\n\n  useEffect(() => {\n    const canvas = localRef.current;\n    if (!canvas) {\n      return;\n    }\n\n    if (typeof navigator === \"undefined\" || !navigator.gpu) {\n      console.warn(\n        \"WebGPU is not supported in this browser. FlowLinesBackground fallback active.\"\n      );\n      setUnsupported(true);\n      return;\n    }\n    setUnsupported(false);\n\n    let disposed = false;\n    let gpu: Gpu | undefined;\n    let canvasSurface: Surface | undefined;\n    let bg: Effect | undefined;\n    let stopLoop: FrameLoopHandle | undefined;\n    let unsubResize: (() => void) | undefined;\n\n    (async () => {\n      try {\n        gpu = await init();\n        if (disposed) {\n          gpu.dispose();\n          return;\n        }\n\n        canvasSurface = surface(gpu, canvas, { dpr: [1, 2] });\n        let offsetX = 0.34;\n        let offsetY = 0;\n        const incX = { current: true };\n        const incY = { current: true };\n        let time = 0;\n\n        bg = effect(gpu, compiledFlowLinesShader, {\n          set: {\n            params: {\n              color1: parseColorToRgb(\n                colorsRef.current.lightColor,\n                DEFAULT_LIGHT_COLOR\n              ),\n              color2: parseColorToRgb(\n                colorsRef.current.midColor,\n                DEFAULT_MID_COLOR\n              ),\n              color3: parseColorToRgb(\n                colorsRef.current.darkColor,\n                DEFAULT_DARK_COLOR\n              ),\n              colorAccent: DEFAULT_COLOR_ACCENT,\n              planeRes: [canvasSurface.size[0], canvasSurface.size[1]],\n              mouse2D: [1, 1],\n              linesBlur: 0.49,\n              noise: 0.02,\n              offsetX,\n              offsetY,\n              linesAmount: 5,\n              backgroundScale: 3,\n              time,\n            },\n          },\n        });\n        bgRef.current = bg;\n\n        unsubResize = canvasSurface.onResize(({ width, height }) => {\n          bg?.set({ params: { planeRes: [width, height] } });\n        });\n\n        stopLoop = frameLoop(gpu, (frame) => {\n          if (!(canvasSurface && bg)) {\n            return;\n          }\n          time += 0.016 * 0.001;\n          offsetX = stepOffset(\n            offsetX,\n            incX,\n            OFFSET_STEP,\n            OFFSET_MIN,\n            OFFSET_MAX\n          );\n          offsetY = stepOffset(\n            offsetY,\n            incY,\n            OFFSET_STEP,\n            OFFSET_MIN,\n            OFFSET_MAX\n          );\n          bg.set({ params: { time, offsetX, offsetY } });\n          frame.pass(canvasSurface, bg);\n        });\n      } catch (error) {\n        console.error(\n          \"FlowLinesBackground WebGPU initialization failed:\",\n          error\n        );\n        if (!disposed) {\n          setUnsupported(true);\n        }\n      }\n    })();\n\n    return () => {\n      disposed = true;\n      bgRef.current = null;\n      unsubResize?.();\n      stopLoop?.stop();\n      bg?.dispose();\n      gpu?.dispose();\n    };\n  }, []);\n\n  const fallbackBackground = `radial-gradient(120% 120% at 50% 0%, ${lightColor} 0%, ${darkColor} 70%)`;\n\n  return (\n    <canvas\n      className={cn(\"h-full w-full\", className)}\n      ref={(element) => {\n        localRef.current = element;\n        if (typeof ref === \"function\") {\n          ref(element);\n        } else if (ref && \"current\" in ref) {\n          (ref as { current: HTMLCanvasElement | null }).current = element;\n        }\n      }}\n      style={unsupported ? { background: fallbackBackground, ...style } : style}\n      {...props}\n    />\n  );\n};\n",
      "type": "registry:ui",
      "target": "components/sora-ui/shaders/flow-lines-background.tsx"
    }
  ],
  "meta": {
    "demoProps": {
      "FlowLinesBackground": {
        "lightColor": {
          "value": "#3f3f3f"
        },
        "midColor": {
          "value": "#262626"
        },
        "darkColor": {
          "value": "#09050c"
        }
      }
    }
  },
  "type": "registry:ui"
}
