{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"bob-shapes","type":"registry:ui","title":"Bob Shapes","description":"Rows of small circles, squares and triangles bobbing and turning in rolling waves; they swell near the cursor. WebGL.","dependencies":[],"registryDependencies":[],"files":[{"path":"registry/ui/bob-shapes.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useEffect, useRef } from \"react\";\n\nexport type BobShapesShape = \"mixed\" | \"circle\" | \"square\" | \"triangle\";\n\nexport interface BobShapesProps {\n  /** Color on the left. */\n  colorA?: string;\n  /** Color on the right. */\n  colorB?: string;\n  /** Animation speed multiplier. */\n  speed?: number;\n  /** Which shape to draw; \"mixed\" alternates by row. */\n  shape?: BobShapesShape;\n  /** Grid spacing in CSS pixels. */\n  cellSize?: number;\n  /** How far shapes bob, as a share of the cell. */\n  amplitude?: number;\n  /** How much shapes grow near the cursor. */\n  mouseStrength?: number;\n  className?: string;\n}\n\nconst SHAPES: Record<BobShapesShape, number> = { mixed: 0, circle: 1, square: 2, triangle: 3 };\n\nconst FRAG = `\nprecision highp float;\nuniform vec2 uRes;\nuniform float uTime;\nuniform vec2 uMouse;\nuniform float uHover;\nuniform vec3 uA;\nuniform vec3 uB;\nuniform float uCell;\nuniform float uAmp;\nuniform float uShape;\nuniform float uMouseStrength;\n\nfloat sdTri(vec2 p, float r) {\n  const float k = 1.7320508;\n  p.x = abs(p.x) - r;\n  p.y = p.y + r / k;\n  if (p.x + k * p.y > 0.0) p = vec2(p.x - k * p.y, -k * p.x - p.y) / 2.0;\n  p.x -= clamp(p.x, -2.0 * r, 0.0);\n  return -length(p) * sign(p.y);\n}\n\nfloat sdShape(vec2 q, float r, float kind) {\n  if (kind < 0.5) return length(q) - r;\n  if (kind < 1.5) {\n    vec2 d = abs(q) - vec2(r * 0.72);\n    return length(max(d, 0.0)) + min(max(d.x, d.y), 0.0) - r * 0.14;\n  }\n  return sdTri(q + vec2(0.0, r * 0.2), r * 1.05) - r * 0.08;\n}\n\nvoid main() {\n  vec2 fc = gl_FragCoord.xy;\n  vec2 mouse = uMouse * uRes;\n  float t = uTime;\n  vec2 cell0 = floor(fc / uCell);\n  vec3 col = vec3(0.0);\n  float alpha = 0.0;\n\n  for (int j = -1; j <= 1; j++) {\n    for (int i = -1; i <= 1; i++) {\n      vec2 id = cell0 + vec2(float(i), float(j));\n      vec2 center = (id + 0.5) * uCell;\n      vec2 n = center / uRes.y;\n      float wave = sin(n.x * 7.0 - t * 2.0 + id.y * 0.45) * 0.6 + sin(n.x * 3.0 + n.y * 4.0 - t * 1.3) * 0.4;\n      center.y += wave * uCell * 0.45 * uAmp;\n      vec2 md = (center - mouse) / (uCell * 3.5);\n      float boost = exp(-dot(md, md)) * uHover * uMouseStrength;\n      float lift = 0.5 + 0.5 * wave;\n      float size = uCell * (0.14 + 0.1 * lift + 0.14 * boost);\n      float kind = uShape > 0.5 ? uShape - 1.0 : mod(id.y, 3.0);\n      float a = kind > 0.5 ? wave * 0.7 + t * 0.25 : 0.0;\n      vec2 q = mat2(cos(a), -sin(a), sin(a), cos(a)) * (fc - center);\n      float sd = sdShape(q, size, kind);\n      float cov = clamp(0.5 - sd, 0.0, 1.0);\n      float glow = exp(-max(sd, 0.0) / (size * 0.35)) * 0.22 * (0.4 + lift);\n      float g = smoothstep(0.0, 1.0, fc.x / uRes.x + wave * 0.12);\n      vec3 c = mix(uA, uB, g) * (0.45 + 0.55 * lift) + vec3(boost * 0.35);\n      float v = max(cov, glow);\n      col = max(col, c * v);\n      alpha = max(alpha, v);\n    }\n  }\n  gl_FragColor = vec4(min(col, vec3(alpha)), alpha);\n}\n`;\n\nconst VERT = \"attribute vec2 p;void main(){gl_Position=vec4(p,0.,1.);}\";\n\ntype Ptr = { x: number; y: number; hover: number };\ntype Uni = (name: string) => WebGLUniformLocation | null;\n\n/** Full-screen shader canvas inside `host`: DPR-capped resize, offscreen pause, reduced motion, eased pointer. */\nfunction runShader(host: HTMLElement, frag: string, speed: () => number, update: (gl: WebGLRenderingContext, u: Uni) => 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 });\n  if (!gl) return null;\n  const prog = gl.createProgram()!;\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    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)) {\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 u: Uni = (n) => {\n    if (!locs.has(n)) locs.set(n, gl.getUniformLocation(prog, n));\n    return locs.get(n) ?? null;\n  };\n  const zone = host.parentElement ?? host;\n  const reduced = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n  const ptr: Ptr = { x: 0.5, y: 0.5, hover: 0 };\n  const goal: Ptr = { ...ptr };\n  let raf = 0;\n  let last = 0;\n  let time = 0;\n\n  const draw = () => {\n    gl.uniform2f(u(\"uRes\"), canvas.width, canvas.height);\n    gl.uniform1f(u(\"uTime\"), time);\n    gl.uniform2f(u(\"uMouse\"), ptr.x, ptr.y);\n    gl.uniform1f(u(\"uHover\"), ptr.hover);\n    update(gl, u);\n    gl.drawArrays(gl.TRIANGLES, 0, 3);\n  };\n  const loop = (now: number) => {\n    const dt = Math.min((now - last) / 1000, 0.1);\n    last = now;\n    const k = 1 - Math.exp(-dt * 4);\n    for (const key of [\"x\", \"y\", \"hover\"] as const) ptr[key] += (goal[key] - ptr[key]) * k;\n    time += dt * speed();\n    draw();\n    raf = requestAnimationFrame(loop);\n  };\n  const run = (on: boolean) => {\n    if (on && !raf && !reduced) {\n      last = performance.now();\n      raf = requestAnimationFrame(loop);\n    } else if (!on && raf) {\n      cancelAnimationFrame(raf);\n      raf = 0;\n    }\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    draw();\n  };\n  const move = (e: PointerEvent) => {\n    const r = host.getBoundingClientRect();\n    goal.x = (e.clientX - r.left) / r.width;\n    goal.y = 1 - (e.clientY - r.top) / r.height;\n    goal.hover = 1;\n  };\n  const leave = () => {\n    goal.hover = 0;\n  };\n  const ro = new ResizeObserver(resize);\n  const io = new IntersectionObserver(([e]) => run(e.isIntersecting));\n  ro.observe(host);\n  io.observe(host);\n  zone.addEventListener(\"pointermove\", move);\n  zone.addEventListener(\"pointerleave\", leave);\n  return {\n    redraw: () => {\n      if (!raf) draw();\n    },\n    dispose: () => {\n      run(false);\n      ro.disconnect();\n      io.disconnect();\n      zone.removeEventListener(\"pointermove\", move);\n      zone.removeEventListener(\"pointerleave\", leave);\n      gl.getExtension(\"WEBGL_lose_context\")?.loseContext();\n      canvas.remove();\n    },\n  };\n}\n\nconst rgb = (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\nexport function BobShapes({\n  colorA = \"#f472b6\",\n  colorB = \"#60a5fa\",\n  speed = 1,\n  shape = \"mixed\",\n  cellSize = 32,\n  amplitude = 1,\n  mouseStrength = 1,\n  className,\n}: BobShapesProps) {\n  const ref = useRef<HTMLDivElement>(null);\n  const opts = useRef({ colorA, colorB, speed, shape, cellSize, amplitude, mouseStrength });\n  const redraw = useRef<(() => void) | undefined>(undefined);\n\n  useEffect(() => {\n    opts.current = { colorA, colorB, speed, shape, cellSize, amplitude, mouseStrength };\n    redraw.current?.();\n  }, [colorA, colorB, speed, shape, cellSize, amplitude, mouseStrength]);\n\n  useEffect(() => {\n    const r = runShader(\n      ref.current!,\n      FRAG,\n      () => opts.current.speed,\n      (gl, u) => {\n        const o = opts.current;\n        const dpr = Math.min(window.devicePixelRatio || 1, 2);\n        gl.uniform3fv(u(\"uA\"), rgb(o.colorA));\n        gl.uniform3fv(u(\"uB\"), rgb(o.colorB));\n        gl.uniform1f(u(\"uCell\"), Math.max(8, o.cellSize) * dpr);\n        gl.uniform1f(u(\"uAmp\"), o.amplitude);\n        gl.uniform1f(u(\"uShape\"), SHAPES[o.shape] ?? 0);\n        gl.uniform1f(u(\"uMouseStrength\"), o.mouseStrength);\n      },\n    );\n    redraw.current = r?.redraw;\n    return () => r?.dispose();\n  }, []);\n\n  return (\n    <div\n      ref={ref}\n      aria-hidden\n      className={className}\n      style={{ position: \"absolute\", inset: 0, overflow: \"hidden\", pointerEvents: \"none\" }}\n    />\n  );\n}\n"}]}