{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "grainient",
  "title": "Grainient",
  "description": "A flowing, film-grained gradient in up to eight colours that stirs under the pointer and swirls on click. Raw WebGL 2, no dependencies.",
  "files": [
    {
      "path": "registry/mewo/ui/grainient.tsx",
      "content": "\"use client\"\n\nimport { useEffect, useRef, type CSSProperties, type HTMLAttributes } from \"react\"\nimport { cn } from \"@/lib/utils\"\n\nexport type GrainientType = \"linear\" | \"radial\"\n\nexport interface GrainientProps extends Omit<HTMLAttributes<HTMLDivElement>, \"style\"> {\n  /** Two to eight colours, darkest first. Blended in Oklab so the ramp never muddies. */\n  colors?: string[]\n  type?: GrainientType\n  /** Direction of a linear gradient, in degrees. */\n  angle?: number\n  /** How many times the ramp repeats across the surface. */\n  bands?: number\n  speed?: number\n  /** How far the surface warps. */\n  flow?: number\n  scale?: number\n  /** Edge softness between colours. */\n  softness?: number\n  /** A lit, cloth-like sheen along the bands. */\n  silk?: number\n  grainAmount?: number\n  grainSize?: number\n  /** Re-seed the grain a few times a second, like film. */\n  grainAnimated?: boolean\n  contrast?: number\n  saturation?: number\n  /** Picks a different arrangement of the same colours. */\n  variation?: number\n  /** Let the pointer stir the gradient. */\n  interactive?: boolean\n  stir?: number\n  /** Swirl around the pointer on click. */\n  clickSwirl?: boolean\n  /** Settle in on mount. */\n  intro?: boolean\n  /** Lift the ramp for a light page. */\n  lightMode?: boolean\n  /** Override the device pixel ratio (capped at 2 by default). */\n  dpr?: number\n  style?: CSSProperties\n}\n\ntype Settings = {\n  colors: string[]\n  type: number\n  angle: number\n  bands: number\n  speed: number\n  flow: number\n  scale: number\n  softness: number\n  silk: number\n  grainAmount: number\n  grainSize: number\n  grainAnimated: boolean\n  contrast: number\n  saturation: number\n  variation: number\n  interactive: boolean\n  stir: number\n  clickSwirl: boolean\n  intro: boolean\n  lightMode: boolean\n  dpr?: number\n}\n\ntype Sample = { x: number; y: number; vx: number; vy: number; age: number; grow: number; done: boolean }\n\ntype Spin = { x: number; y: number; age: number; direction: number }\n\ntype Program = { program: WebGLProgram; uniforms: Record<string, WebGLUniformLocation | null> }\n\nconst MAX_COLORS = 8\nconst MAX_STIRS = 24\nconst MAX_TWIRLS = 4\nconst STIR_LIFE = 1.3\nconst STIR_STEP = 0.06\nconst TWIRL_LIFE = 2.6\nconst INTRO_SECONDS = 1.6\nconst DEFAULT_COLORS = [\"#120f17\", \"#2a0f0a\", \"#9a3412\", \"#f97316\", \"#fed7aa\"]\nconst TYPES: Record<string, number> = { linear: 0, radial: 1 }\nconst LIFT = 0.2\n\nconst VERTEX = `#version 300 es\nout vec2 vUv;\nvoid main() {\n  vec2 p = vec2(float((gl_VertexID << 1) & 2), float(gl_VertexID & 2));\n  vUv = p;\n  gl_Position = vec4(p * 2.0 - 1.0, 0.0, 1.0);\n}`\n\nconst FIELD = `#version 300 es\nprecision highp float;\nprecision highp int;\nuniform float uAspect;\nuniform float uTime;\nuniform vec3 uRamp[8];\nuniform int uCount;\nuniform int uType;\nuniform vec2 uDir;\nuniform float uExtent;\nuniform float uBands;\nuniform float uSoftness;\nuniform float uFlow;\nuniform float uScale;\nuniform float uSilk;\nuniform vec4 uStir[24];\nuniform float uStirWeight[24];\nuniform vec4 uTwirl[4];\nuniform vec2 uSeed;\nuniform float uContrast;\nuniform float uSaturation;\nuniform float uSettle;\nuniform float uReveal;\nin vec2 vUv;\nout vec4 outColor;\n\nvec2 hash2(vec2 p) {\n  vec3 q = fract(vec3(p.xyx) * vec3(0.1031, 0.103, 0.0973));\n  q += dot(q, q.yzx + 33.33);\n  return fract((q.xx + q.yz) * q.zy) * 2.0 - 1.0;\n}\n\nfloat noise(vec2 p) {\n  vec2 i = floor(p);\n  vec2 f = fract(p);\n  vec2 u = f * f * f * (f * (f * 6.0 - 15.0) + 10.0);\n  float a = dot(hash2(i), f);\n  float b = dot(hash2(i + vec2(1.0, 0.0)), f - vec2(1.0, 0.0));\n  float c = dot(hash2(i + vec2(0.0, 1.0)), f - vec2(0.0, 1.0));\n  float d = dot(hash2(i + vec2(1.0, 1.0)), f - vec2(1.0, 1.0));\n  return 0.5 + 0.5 * mix(mix(a, b, u.x), mix(c, d, u.x), u.y);\n}\n\nvec2 drift(vec2 p, float t) {\n  vec2 w1 = vec2(\n    noise(p * 0.85 + vec2(t * 0.11, t * 0.07)),\n    noise(p * 0.85 + vec2(4.1, 1.7) - vec2(t * 0.08, t * 0.12))\n  );\n  vec2 w2 = vec2(\n    noise(p * 1.55 + w1 * 1.8 + vec2(1.3, 7.1) + t * 0.05),\n    noise(p * 1.55 + w1 * 1.8 + vec2(6.4, 2.2) - t * 0.06)\n  );\n  return (w1 - 0.5) * 1.1 + (w2 - 0.5) * 0.55;\n}\n\nvec2 warp(vec2 p) {\n  vec2 q = p / uScale;\n  if (uType == 1) {\n    float turn = uTime * 0.35;\n    q = mat2(cos(turn), sin(turn), -sin(turn), cos(turn)) * q;\n  } else {\n    q += vec2(-uDir.y, uDir.x) * uTime * 0.45;\n  }\n  return p + drift(q + uSeed, uTime) * uFlow * uScale * uSettle;\n}\n\nfloat coord(vec2 p) {\n  vec2 w = warp(p);\n  return uType == 1 ? 1.0 - length(w) * 1.1 : dot(w, uDir) / uExtent + 0.5;\n}\n\nvec3 labToRgb(vec3 lab) {\n  float l = lab.x + 0.3963377774 * lab.y + 0.2158037573 * lab.z;\n  float m = lab.x - 0.1055613458 * lab.y - 0.0638541728 * lab.z;\n  float s = lab.x - 0.0894841775 * lab.y - 1.2914855480 * lab.z;\n  l = l * l * l;\n  m = m * m * m;\n  s = s * s * s;\n  return vec3(\n    4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s,\n    -1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s,\n    -0.0041960863 * l - 0.7034186147 * m + 1.7076147010 * s\n  );\n}\n\nvec3 encode(vec3 c) {\n  c = clamp(c, 0.0, 1.0);\n  return mix(c * 12.92, 1.055 * pow(c, vec3(1.0 / 2.4)) - 0.055, step(0.0031308, c));\n}\n\nvoid main() {\n  vec2 p = (vUv - 0.5) * vec2(uAspect, 1.0);\n  for (int i = 0; i < 24; i++) {\n    float w = uStirWeight[i];\n    if (w <= 0.0) continue;\n    vec2 d = p - uStir[i].xy;\n    p -= uStir[i].zw * w * exp(-dot(d, d) / 0.045);\n  }\n  for (int i = 0; i < 4; i++) {\n    float angle = uTwirl[i].z;\n    if (angle == 0.0) continue;\n    vec2 d = p - uTwirl[i].xy;\n    float a = angle * exp(-dot(d, d) / (uTwirl[i].w * uTwirl[i].w));\n    float c = cos(a);\n    float s = sin(a);\n    p = uTwirl[i].xy + mat2(c, -s, s, c) * d;\n  }\n  float raw = coord(p);\n  float s = raw;\n  if (uBands > 1.0) {\n    s *= uBands;\n    float cycle = floor(s);\n    s = fract(s);\n    if (mod(cycle, 2.0) > 0.5) s = 1.0 - s;\n  }\n  s = clamp(s, 0.0, 1.0);\n  float scaled = s * float(uCount - 1);\n  int index = int(min(floor(scaled), float(uCount - 2)));\n  float f = scaled - float(index);\n  float edge = max(uSoftness * 0.5, fwidth(scaled) * 0.75);\n  float shaped = smoothstep(0.5 - edge, 0.5 + edge, f);\n  vec3 from = uRamp[0];\n  vec3 to = uRamp[1];\n  for (int i = 0; i < 7; i++) {\n    if (i == index) {\n      from = uRamp[i];\n      to = uRamp[i + 1];\n    }\n  }\n  vec3 lab = mix(from, to, shaped);\n  if (uSilk > 0.0) {\n    vec2 grad = vec2(coord(p + vec2(0.004, 0.0)) - raw, coord(p + vec2(0.0, 0.004)) - raw) / 0.004;\n    vec2 slope = grad * sin(3.14159265 * clamp(raw, 0.0, 1.0) * uBands) * uSilk * 4.0;\n    vec3 normal = normalize(vec3(-slope, 1.0));\n    vec3 light = normalize(vec3(-uDir.y, uDir.x, 1.2));\n    float diffuse = dot(normal, light) - light.z;\n    lab = uRamp[0] + (lab - uRamp[0]) * (1.0 + (diffuse * 0.35 + max(diffuse, 0.0) * diffuse * 0.6) * uSilk);\n  }\n  lab = mix(uRamp[0], lab, smoothstep(0.0, 1.0, uReveal * 1.5 - length(vUv - 0.5) * 0.6));\n  lab.x = (lab.x - 0.55) * uContrast + 0.55;\n  lab.yz *= uSaturation;\n  outColor = vec4(encode(labToRgb(lab)), 1.0);\n}`\n\nconst COMPOSITE = `#version 300 es\nprecision highp float;\nuniform sampler2D uField;\nuniform float uGrain;\nuniform float uGrainSize;\nuniform float uFrame;\nin vec2 vUv;\nout vec4 outColor;\n\nfloat hash(vec2 p) {\n  vec3 q = fract(vec3(p.xyx) * 0.1031);\n  q += dot(q, q.yzx + 33.33);\n  return fract((q.x + q.y) * q.z);\n}\n\nfloat clump(vec2 p) {\n  vec2 i = floor(p);\n  vec2 f = fract(p);\n  vec2 u = f * f * (3.0 - 2.0 * f);\n  return mix(mix(hash(i), hash(i + vec2(1.0, 0.0)), u.x), mix(hash(i + vec2(0.0, 1.0)), hash(i + vec2(1.0, 1.0)), u.x), u.y);\n}\n\nvoid main() {\n  vec3 color = texture(uField, vUv).rgb;\n  vec2 pixel = gl_FragCoord.xy + uFrame * vec2(17.0, 59.0);\n  float fine = hash(pixel) + hash(pixel + 7.13) - 1.0;\n  float coarse = (clump(pixel / max(uGrainSize, 1.0)) - 0.5) * 2.0;\n  float grain = mix(fine, coarse, clamp(uGrainSize - 1.0, 0.0, 1.0) * 0.8);\n  float luma = dot(color, vec3(0.2126, 0.7152, 0.0722));\n  color += grain * uGrain * (0.45 + 2.2 * luma * (1.0 - luma));\n  outColor = vec4(clamp(color, 0.0, 1.0), 1.0);\n}`\n\nconst clamp = (value: number, min: number, max: number) => Math.min(max, Math.max(min, value))\n\nconst toLinear = (value: number) => (value <= 0.04045 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4)\n\nconst toLab = ([r, g, b]: number[]): number[] => {\n  const lr = toLinear(r)\n  const lg = toLinear(g)\n  const lb = toLinear(b)\n  const l = Math.cbrt(0.4122214708 * lr + 0.5363325363 * lg + 0.0514459929 * lb)\n  const m = Math.cbrt(0.2119034982 * lr + 0.6806995451 * lg + 0.1073969566 * lb)\n  const s = Math.cbrt(0.0883024619 * lr + 0.2817188376 * lg + 0.6299787005 * lb)\n  return [\n    0.2104542553 * l + 0.793617785 * m - 0.0040720468 * s,\n    1.9779984951 * l - 2.428592205 * m + 0.4505937099 * s,\n    0.0259040371 * l + 0.7827717662 * m - 0.808675766 * s,\n  ]\n}\n\nconst compile = (gl: WebGL2RenderingContext, type: number, source: string) => {\n  const shader = gl.createShader(type)\n  if (!shader) return null\n  gl.shaderSource(shader, source)\n  gl.compileShader(shader)\n  if (gl.getShaderParameter(shader, gl.COMPILE_STATUS)) return shader\n  gl.deleteShader(shader)\n  return null\n}\n\nconst link = (gl: WebGL2RenderingContext, fragmentSource: string): Program | null => {\n  const vertex = compile(gl, gl.VERTEX_SHADER, VERTEX)\n  const fragment = compile(gl, gl.FRAGMENT_SHADER, fragmentSource)\n  const program = gl.createProgram()\n  if (!vertex || !fragment || !program) return null\n  gl.attachShader(program, vertex)\n  gl.attachShader(program, fragment)\n  gl.linkProgram(program)\n  gl.deleteShader(vertex)\n  gl.deleteShader(fragment)\n  if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {\n    gl.deleteProgram(program)\n    return null\n  }\n  const uniforms: Record<string, WebGLUniformLocation | null> = {}\n  const count = gl.getProgramParameter(program, gl.ACTIVE_UNIFORMS)\n  for (let i = 0; i < count; i++) {\n    const info = gl.getActiveUniform(program, i)\n    if (!info) continue\n    const name = info.name.replace(/\\[0\\]$/, \"\")\n    uniforms[name] = gl.getUniformLocation(program, name)\n  }\n  return { program, uniforms }\n}\n\nexport function Grainient({\n  colors = DEFAULT_COLORS,\n  type = \"linear\",\n  angle = 55,\n  bands = 1,\n  speed = 1,\n  flow = 0.8,\n  scale = 1,\n  softness = 1,\n  silk = 0.35,\n  grainAmount = 0.08,\n  grainSize = 1.2,\n  grainAnimated = true,\n  contrast = 1,\n  saturation = 1,\n  variation = 0,\n  interactive = true,\n  stir = 1,\n  clickSwirl = true,\n  intro = true,\n  lightMode = false,\n  dpr,\n  className = \"\",\n  style,\n  ...rest\n}: GrainientProps) {\n  const containerRef = useRef<HTMLDivElement>(null)\n  const wakeRef = useRef<(() => void) | null>(null)\n  const palette = (Array.isArray(colors) && colors.length ? colors : DEFAULT_COLORS).slice(0, MAX_COLORS)\n  const settings: Settings = {\n    colors: palette.length > 1 ? palette : [palette[0], palette[0]],\n    type: TYPES[type] ?? 0,\n    angle,\n    bands: clamp(bands, 1, 8),\n    speed: Math.max(0, speed),\n    flow: clamp(flow, 0, 3),\n    scale: clamp(scale, 0.25, 3),\n    softness: clamp(softness, 0, 1),\n    silk: clamp(silk, 0, 1),\n    grainAmount: clamp(grainAmount, 0, 0.5),\n    grainSize: clamp(grainSize, 0.5, 4),\n    grainAnimated,\n    contrast: clamp(contrast, 0.2, 2),\n    saturation: clamp(saturation, 0, 2),\n    variation: Math.max(0, variation),\n    interactive,\n    stir: clamp(stir, 0, 2),\n    clickSwirl,\n    intro,\n    lightMode,\n    dpr,\n  }\n  const settingsRef = useRef(settings)\n\n  useEffect(() => {\n    const container = containerRef.current\n    if (!container) return undefined\n    // The canvas is made here, not in JSX: React Strict Mode runs this effect twice in development, and the\n    // context the first cleanup loses would come straight back from getContext() on the same element.\n    const canvas = document.createElement(\"canvas\")\n    canvas.className = \"absolute inset-0 block h-full w-full\"\n    canvas.setAttribute(\"aria-hidden\", \"true\")\n    const gl = canvas.getContext(\"webgl2\", {\n      alpha: false,\n      antialias: false,\n      depth: false,\n      stencil: false,\n    })\n    if (!gl) return undefined\n\n    const field = link(gl, FIELD)\n    const composite = link(gl, COMPOSITE)\n    if (!field || !composite) return undefined\n    container.appendChild(canvas)\n    const fu = field.uniforms\n    const cu = composite.uniforms\n    const vao = gl.createVertexArray()\n    const target = gl.createTexture()\n    gl.bindTexture(gl.TEXTURE_2D, target)\n    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)\n    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)\n    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)\n    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)\n    const buffer = gl.createFramebuffer()\n    const fieldSize = { width: 0, height: 0 }\n\n    const probe = document.createElement(\"canvas\").getContext(\"2d\", { willReadFrequently: true })\n    const colorCache = new Map<string, number[]>()\n    const parse = (value: string): number[] => {\n      const key = String(value)\n      const cached = colorCache.get(key)\n      if (cached) return cached\n      let rgb = [0, 0, 0]\n      if (probe) {\n        probe.clearRect(0, 0, 1, 1)\n        probe.fillStyle = \"#000000\"\n        probe.fillStyle = key\n        probe.fillRect(0, 0, 1, 1)\n        const [r, g, b] = probe.getImageData(0, 0, 1, 1).data\n        rgb = [r / 255, g / 255, b / 255]\n      }\n      colorCache.set(key, rgb)\n      return rgb\n    }\n\n    const reduce = window.matchMedia?.(\"(prefers-reduced-motion: reduce)\").matches ?? false\n    const view = { width: 1, height: 1, ratio: 1 }\n    const ramp = new Float32Array(MAX_COLORS * 3)\n    const stirs = new Float32Array(MAX_STIRS * 4)\n    const stirWeights = new Float32Array(MAX_STIRS)\n    const twirls = new Float32Array(MAX_TWIRLS * 4)\n    const trail: Sample[] = []\n    const spins: Spin[] = []\n    const pointer = { x: 0.5, y: 0.5, sx: 0.5, sy: 0.5, vx: 0, inside: false, fresh: true }\n    let paletteKey = \"\"\n    let time = 0\n    let introClock = settingsRef.current.intro && !reduce ? 0 : 1\n    let frameSeed = 0\n    let grainClock = 0\n    let raf = 0\n    let last = 0\n    let visible = true\n\n    const syncPalette = () => {\n      const s = settingsRef.current\n      const key = `${s.colors.join(\",\")}|${s.lightMode}`\n      if (key === paletteKey) return\n      paletteKey = key\n      const labs = s.colors.map((color) => toLab(parse(color)))\n      if (!s.lightMode) {\n        labs.forEach((lab, i) => ramp.set(lab, i * 3))\n      } else {\n        const blank = labs.map(([L]) => Math.abs(L - labs[0][0]) < 0.1)\n        const tints = labs.filter((_, i) => !blank[i]).reverse()\n        labs.forEach((_, i) => {\n          const [L, a, b] = blank[i] ? [1, 0, 0] : (tints.shift() as number[])\n          ramp.set([L + (1 - L) * LIFT, a * (1 - LIFT), b * (1 - LIFT)], i * 3)\n        })\n      }\n    }\n\n    const draw = () => {\n      const s = settingsRef.current\n      const aspect = view.width / view.height\n      syncPalette()\n      gl.bindVertexArray(vao)\n      gl.disable(gl.BLEND)\n      gl.bindFramebuffer(gl.FRAMEBUFFER, buffer)\n      gl.viewport(0, 0, fieldSize.width, fieldSize.height)\n      gl.useProgram(field.program)\n      gl.uniform1f(fu.uAspect, aspect)\n      gl.uniform1f(fu.uTime, time)\n      gl.uniform3fv(fu.uRamp, ramp)\n      gl.uniform1i(fu.uCount, s.colors.length)\n      const radians = (s.angle * Math.PI) / 180\n      gl.uniform1i(fu.uType, s.type)\n      gl.uniform2f(fu.uDir, Math.sin(radians), Math.cos(radians))\n      gl.uniform1f(fu.uExtent, aspect * Math.abs(Math.sin(radians)) + Math.abs(Math.cos(radians)))\n      gl.uniform1f(fu.uBands, s.bands)\n      gl.uniform1f(fu.uSoftness, s.softness)\n      gl.uniform1f(fu.uFlow, s.flow)\n      gl.uniform1f(fu.uScale, s.scale)\n      gl.uniform1f(fu.uSilk, s.silk)\n      gl.uniform4fv(fu.uStir, stirs)\n      gl.uniform1fv(fu.uStirWeight, stirWeights)\n      gl.uniform4fv(fu.uTwirl, twirls)\n      gl.uniform2f(fu.uSeed, s.variation * 3.17, s.variation * 1.91)\n      gl.uniform1f(fu.uContrast, s.contrast)\n      gl.uniform1f(fu.uSaturation, s.saturation)\n      gl.uniform1f(fu.uSettle, introClock * introClock * (3 - 2 * introClock))\n      gl.uniform1f(fu.uReveal, introClock)\n      gl.drawArrays(gl.TRIANGLES, 0, 3)\n      gl.bindFramebuffer(gl.FRAMEBUFFER, null)\n      gl.viewport(0, 0, canvas.width, canvas.height)\n      gl.useProgram(composite.program)\n      gl.activeTexture(gl.TEXTURE0)\n      gl.bindTexture(gl.TEXTURE_2D, target)\n      gl.uniform1i(cu.uField, 0)\n      gl.uniform1f(cu.uGrain, s.grainAmount)\n      gl.uniform1f(cu.uGrainSize, s.grainSize * view.ratio)\n      gl.uniform1f(cu.uFrame, frameSeed)\n      gl.drawArrays(gl.TRIANGLES, 0, 3)\n    }\n\n    const resize = () => {\n      const s = settingsRef.current\n      view.width = Math.max(1, container.clientWidth)\n      view.height = Math.max(1, container.clientHeight)\n      view.ratio = Math.min(s.dpr || window.devicePixelRatio || 1, 2)\n      canvas.width = Math.max(1, Math.round(view.width * view.ratio))\n      canvas.height = Math.max(1, Math.round(view.height * view.ratio))\n      const width = Math.max(1, Math.round(canvas.width / 2))\n      const height = Math.max(1, Math.round(canvas.height / 2))\n      if (width !== fieldSize.width || height !== fieldSize.height) {\n        fieldSize.width = width\n        fieldSize.height = height\n        gl.bindTexture(gl.TEXTURE_2D, target)\n        gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA8, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null)\n        gl.bindFramebuffer(gl.FRAMEBUFFER, buffer)\n        gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, target, 0)\n        gl.bindFramebuffer(gl.FRAMEBUFFER, null)\n      }\n      draw()\n      wake()\n    }\n\n    const updateTouches = (dt: number, aspect: number) => {\n      const s = settingsRef.current\n      const follow = 1 - Math.exp(-dt / 0.06)\n      const px = pointer.sx\n      const py = pointer.sy\n      pointer.sx += (pointer.x - pointer.sx) * follow\n      pointer.sy += (pointer.y - pointer.sy) * follow\n      const vx = ((pointer.sx - px) / dt) * aspect\n      const vy = (pointer.sy - py) / dt\n      pointer.vx = vx\n      const head = trail[0]\n      trail.forEach((sample) => {\n        if (sample.done) sample.age += dt\n      })\n      if (s.interactive && pointer.inside && Math.hypot(vx, vy) > 0.02 && !reduce) {\n        if (!head || head.done) {\n          trail.unshift({ x: 0, y: 0, vx: 0, vy: 0, age: 0, grow: 0, done: false })\n          if (trail.length > MAX_STIRS) trail.pop()\n        }\n        const live = trail[0]\n        live.x = (pointer.sx - 0.5) * aspect\n        live.y = pointer.sy - 0.5\n        live.vx = vx\n        live.vy = vy\n        live.grow = Math.min(1, live.grow + dt / STIR_STEP)\n        live.done = live.grow >= 1\n      } else if (head) {\n        head.done = true\n      }\n      while (trail.length && trail[trail.length - 1].age > STIR_LIFE) trail.pop()\n      for (let i = 0; i < MAX_STIRS; i++) {\n        const sample = trail[i]\n        if (!sample) {\n          stirWeights[i] = 0\n          continue\n        }\n        const fade = 1 - sample.age / STIR_LIFE\n        const cap = Math.min(1, 2.2 / Math.max(Math.hypot(sample.vx, sample.vy), 0.001))\n        stirs.set([sample.x, sample.y, sample.vx * cap, sample.vy * cap], i * 4)\n        stirWeights[i] = fade * fade * sample.grow * s.stir * 0.05\n      }\n      spins.forEach((spin) => {\n        spin.age += dt\n      })\n      while (spins.length && spins[spins.length - 1].age > TWIRL_LIFE) spins.pop()\n      for (let i = 0; i < MAX_TWIRLS; i++) {\n        const spin = spins[i]\n        if (!spin) {\n          twirls[i * 4 + 2] = 0\n          continue\n        }\n        const k = spin.age / TWIRL_LIFE\n        const envelope = Math.min(1, spin.age / 0.18) * (1 - k) * (1 - k)\n        twirls.set([spin.x, spin.y, spin.direction * 2.4 * envelope, 0.16 + 0.1 * Math.min(1, spin.age / 0.5)], i * 4)\n      }\n    }\n\n    const frame = (now: number) => {\n      raf = 0\n      if (!visible || document.hidden) return\n      const s = settingsRef.current\n      const dt = last ? clamp((now - last) / 1000, 0.001, 0.05) : 1 / 60\n      last = now\n      if (!reduce) time += dt * s.speed * 0.16\n      if (introClock < 1) introClock = Math.min(1, introClock + dt / INTRO_SECONDS)\n      grainClock += dt\n      if (s.grainAnimated && !reduce && grainClock > 1 / 24) {\n        grainClock = 0\n        frameSeed = (frameSeed + 1) % 97\n      }\n      updateTouches(dt, view.width / view.height)\n      draw()\n      const busy =\n        (!reduce && (s.speed > 0 || s.grainAnimated)) || introClock < 1 || trail.length > 0 || spins.length > 0\n      if (busy) raf = requestAnimationFrame(frame)\n      else last = 0\n    }\n\n    function wake() {\n      if (!raf && visible && !document.hidden) raf = requestAnimationFrame(frame)\n    }\n\n    const locate = (event: PointerEvent) => {\n      const rect = container.getBoundingClientRect()\n      const x = (event.clientX - rect.left) / Math.max(1, rect.width)\n      const y = 1 - (event.clientY - rect.top) / Math.max(1, rect.height)\n      pointer.inside = x >= 0 && x <= 1 && y >= 0 && y <= 1\n      pointer.x = x\n      pointer.y = y\n      if (pointer.fresh || !pointer.inside) {\n        pointer.sx = x\n        pointer.sy = y\n        pointer.fresh = false\n      }\n    }\n\n    const onPointerMove = (event: PointerEvent) => {\n      locate(event)\n      if (pointer.inside || trail.length) wake()\n    }\n\n    const onPointerDown = (event: PointerEvent) => {\n      locate(event)\n      const s = settingsRef.current\n      if (!pointer.inside || !s.clickSwirl || reduce) return\n      spins.unshift({\n        x: (pointer.x - 0.5) * (view.width / view.height),\n        y: pointer.y - 0.5,\n        age: 0,\n        direction: pointer.vx < 0 ? 1 : -1,\n      })\n      if (spins.length > MAX_TWIRLS) spins.pop()\n      wake()\n    }\n\n    const onPointerLeave = () => {\n      pointer.inside = false\n    }\n\n    const onVisibility = () => {\n      last = 0\n      wake()\n    }\n\n    const resizeObserver = new ResizeObserver(resize)\n    resizeObserver.observe(container)\n    const intersection = new IntersectionObserver((entries) => {\n      visible = entries.some((entry) => entry.isIntersecting)\n      if (visible) {\n        last = 0\n        wake()\n      }\n    })\n    intersection.observe(container)\n    window.addEventListener(\"pointermove\", onPointerMove, { passive: true })\n    window.addEventListener(\"pointerdown\", onPointerDown, { passive: true })\n    document.documentElement.addEventListener(\"pointerleave\", onPointerLeave)\n    document.addEventListener(\"visibilitychange\", onVisibility)\n    resize()\n    wakeRef.current = wake\n\n    return () => {\n      cancelAnimationFrame(raf)\n      wakeRef.current = null\n      resizeObserver.disconnect()\n      intersection.disconnect()\n      window.removeEventListener(\"pointermove\", onPointerMove)\n      window.removeEventListener(\"pointerdown\", onPointerDown)\n      document.documentElement.removeEventListener(\"pointerleave\", onPointerLeave)\n      document.removeEventListener(\"visibilitychange\", onVisibility)\n      gl.deleteTexture(target)\n      gl.deleteFramebuffer(buffer)\n      gl.deleteVertexArray(vao)\n      gl.deleteProgram(field.program)\n      gl.deleteProgram(composite.program)\n      gl.getExtension(\"WEBGL_lose_context\")?.loseContext()\n      canvas.remove()\n    }\n  }, [])\n\n  // Props flow through a ref, so the WebGL setup effect above runs exactly once: a prop change only\n  // updates the ref and wakes the loop. It never tears the context down.\n  useEffect(() => {\n    settingsRef.current = settings\n    wakeRef.current?.()\n  })\n\n  return (\n    <div\n      ref={containerRef}\n      data-slot=\"grainient\"\n      className={cn(\"relative h-full w-full overflow-hidden\", className)}\n      style={style}\n      {...rest}\n    />\n  )\n}\n",
      "type": "registry:ui"
    }
  ],
  "meta": {
    "added": "2026-10-21"
  },
  "categories": [
    "background"
  ],
  "type": "registry:ui"
}