# Integrate `Strands` from Motion Garden

You are helping me add the **Strands** component to an existing project.
Everything you need is below — full source, dependencies, props and the
behavioural contract. Read it all before writing anything.

- **Component:** Strands
- **Category:** Backgrounds
- **Summary:** Glowing wavy light-strands with an optional glass refraction lens.
- **Reference:** https://motion-garden.netlify.app/components/strands
- **Full catalog (for you, the agent):** https://motion-garden.netlify.app/llms.txt

## Stack this source assumes

- React 19 with the Next.js App Router (never the `pages/` directory)
- TypeScript, strict
- Tailwind CSS v4, CSS-first config (utilities are inline; no `tailwind.config` needed)

If my project differs, adapt the integration — but do not rewrite the animation math to suit a different stack. Ask me first.

## Dependencies

```bash
bun add ogl
```

## Files to create

2 files — all of them are required. Paths are as shipped; the component may live anywhere in my project, but keep every peer file at the exact path shown so the import aliases resolve.

### 1. `components/registry/strands/strands.tsx`

```tsx
"use client";

import { Renderer, Program, Mesh, Color, Triangle, RenderTarget } from "ogl";
import { useEffect, useRef, CSSProperties } from "react";
import { useAnimationLoop, type Metrics } from "@/hooks/use-animation-loop";

const MAX_STRANDS = 12;
const MAX_COLORS = 8;

const VERT = `#version 300 es
in vec2 position;
void main() {
  gl_Position = vec4(position, 0.0, 1.0);
}
`;

const FRAG = `#version 300 es
precision highp float;

uniform float uTime;
uniform vec2 uResolution;
uniform vec3 uColors[${MAX_COLORS}];
uniform int uColorCount;
uniform int uStrandCount;
uniform float uSpeed;
uniform float uAmplitude;
uniform float uWaviness;
uniform float uThickness;
uniform float uGlow;
uniform float uTaper;
uniform float uSpread;
uniform float uHueShift;
uniform float uIntensity;
uniform float uOpacity;
uniform float uScale;
uniform float uSaturation;
uniform float uEnvWidth;
uniform float uBrightTaper;

out vec4 fragColor;

const float PI = 3.14159265;

vec3 spectrum(float t) {
  return 0.5 + 0.5 * cos(2.0 * PI * (t + vec3(0.00, 0.33, 0.67)));
}

vec3 samplePalette(float t) {
  t = fract(t);
  float scaled = t * float(uColorCount);
  int idx = int(floor(scaled));
  float blend = fract(scaled);
  int nextIdx = idx + 1;
  if (nextIdx >= uColorCount) nextIdx = 0;
  return mix(uColors[idx], uColors[nextIdx], blend);
}

vec3 strandColor(float t) {
  if (uColorCount > 0) return samplePalette(t);
  return spectrum(t);
}

void main() {
  vec2 uv = (gl_FragCoord.xy - 0.5 * uResolution) / uResolution.y;
  uv /= max(uScale, 0.0001);

  float e = 0.06 + uIntensity * 0.94;
  // Envelope half-width in strand uv-space — when the glass is on it equals the
  // lens rim, so strand tips land exactly on the glass edge. Amplitude keeps the
  // full taper (waves converge to the center line, which meets the circle at
  // x = ±r). Brightness falloff is mode-aware via uBrightTaper: a gentle power
  // under glass so tips stay visible until they touch the rim, the full taper
  // without glass so light dissolves in a soft gradient instead of hitting the
  // envelope clamp at near-full strength.
  float nx = clamp(abs(uv.x) / uEnvWidth, 0.0, 1.0);
  float envCos = cos(nx * PI * 0.5);
  float env = pow(envCos, uTaper);
  float bright = pow(envCos, uBrightTaper);

  vec3 col = vec3(0.0);

  for (int i = 0; i < ${MAX_STRANDS}; i++) {
    if (i >= uStrandCount) break;

    float fi = float(i);
    float ph = fi * 1.7 * uSpread;
    float freq = (2.0 + fi * 0.35) * uWaviness;
    float spd = 1.4 + fi * 1.2;

    float tt = uTime * uSpeed;
    float w = sin(uv.x * freq + tt * spd + ph) * 0.60
            + sin(uv.x * freq * 1.1 - tt * spd * 0.7 + ph * 1.7) * 0.40;

    float amp = (0.1 + 0.02 * e) * env * uAmplitude;
    float y = w * amp;

    float d = abs(uv.y - y);
    float thick = (0.001 + 0.05 * e) * (0.35 + env) * uThickness;
    float g = thick / (d + thick * 0.45);
    g = g * g;

    float h = fi / float(uStrandCount) + uv.x * 0.30 + uTime * 0.04 + uHueShift;
    col += strandColor(h) * g * bright;
  }

  col *= 0.45 + 0.7 * e;
  col = 1.0 - exp(-col * uGlow);

  float gray = dot(col, vec3(0.2126, 0.7152, 0.0722));
  col = max(mix(vec3(gray), col, uSaturation), 0.0);

  float lum = max(max(col.r, col.g), col.b);
  float alpha = clamp(lum, 0.0, 1.0) * uOpacity;

  fragColor = vec4(col * uOpacity, alpha);
}
`;

const GLASS_FRAG = `#version 300 es
precision highp float;

uniform sampler2D uScene;
uniform vec2 uResolution;
uniform float uRadius;
uniform float uRefraction;
uniform float uDispersion;

out vec4 fragColor;

vec2 toUv(vec2 p) {
  return p * (uResolution.y / uResolution) + 0.5;
}

void main() {
  vec2 p = (gl_FragCoord.xy - 0.5 * uResolution) / uResolution.y;
  float d = length(p);
  float r = uRadius;

  float edge = fwidth(d) * 1.5;
  float mask = 1.0 - smoothstep(r - edge, r + edge, d);
  if (mask <= 0.0) {
    fragColor = vec4(0.0);
    return;
  }

  // sphere height: 0 at the rim, 1 at the center
  float z = sqrt(max(r * r - d * d, 0.0)) / r;
  float nd = d / r; // 0 at the center, 1 at the rim

  // refraction is confined to a narrow band near the rim; the rest stays undistorted
  vec2 dir = d > 0.0 ? p / d : vec2(0.0);
  float lens = smoothstep(0.85, 1.0, nd) * pow(nd, 6.0);
  vec2 offset = -dir * lens * uRefraction * 0.15;
  vec2 disp = -dir * lens * uDispersion * 0.012;

  vec3 light;
  light.r = texture(uScene, toUv(p + offset - disp)).r;
  light.g = texture(uScene, toUv(p + offset)).g;
  light.b = texture(uScene, toUv(p + offset + disp)).b;

  // neutral fresnel rim (no color tint so the glass stays clear)
  float fres = pow(1.0 - z, 3.0);
  vec3 rim = vec3(1.0) * fres * 0.18;

  // specular highlight from the upper-left
  vec2 lightDir = normalize(vec2(-0.55, 0.6));
  float spec = pow(max(dot(p / max(r, 1e-4), lightDir), 0.0), 6.0);
  spec *= smoothstep(r, r * 0.55, d);

  vec3 emissive = light + rim + vec3(spec) * 0.4;
  float emissiveA = clamp(max(max(emissive.r, emissive.g), emissive.b), 0.0, 1.0);

  // almost clear glass body: only a faint neutral darkening, mostly near the rim
  float bodyA = 0.05 + fres * 0.05;

  // composite emissive light over the clear body (premultiplied)
  float outA = emissiveA + bodyA * (1.0 - emissiveA);
  vec3 outRGB = emissive;

  outRGB *= mask;
  outA *= mask;

  fragColor = vec4(outRGB, outA);
}
`;

export interface StrandsProps {
	colors?: string[];
	count?: number;
	speed?: number;
	amplitude?: number;
	waviness?: number;
	thickness?: number;
	strandWidth?: number;
	glow?: number;
	taper?: number;
	spread?: number;
	hueShift?: number;
	intensity?: number;
	saturation?: number;
	opacity?: number;
	scale?: number;
	glass?: boolean;
	refraction?: number;
	dispersion?: number;
	glassSize?: number;
	paused?: boolean;
	className?: string;
	style?: CSSProperties;
}

const buildPalette = (colors: string[]): number[][] => {
	const filled = colors && colors.length ? colors : ["#ffffff"];
	const padded: number[][] = [];
	for (let i = 0; i < MAX_COLORS; i++) {
		const hex = filled[i] ?? filled[filled.length - 1];
		const c = new Color(hex);
		padded.push([c.r, c.g, c.b]);
	}
	return padded;
};

export default function Strands({
	colors = ["#FF4242", "#7C3AED", "#06B6D4", "#EAB308"],
	count = 13,
	speed = 0.8,
	amplitude = 1.25,
	waviness = 1,
	thickness = 1.55,
	strandWidth = 100,
	glow = 0.25,
	taper = 3,
	spread = 1.65,
	hueShift = 0,
	intensity = 1,
	saturation = 1,
	opacity = 0.9,
	scale = 0.9,
	glass = true,
	refraction = 1.3,
	dispersion = 0.5,
	glassSize = 1,
	paused = false,
	className = "",
	style,
}: StrandsProps) {
	const propsRef = useRef<
		Required<Omit<StrandsProps, "className" | "style" | "paused">>
	>({
		colors,
		count,
		speed,
		amplitude,
		waviness,
		thickness,
		strandWidth,
		glow,
		taper,
		spread,
		hueShift,
		intensity,
		saturation,
		opacity,
		scale,
		glass,
		refraction,
		dispersion,
		glassSize,
	});
	propsRef.current = {
		colors,
		count,
		speed,
		amplitude,
		waviness,
		thickness,
		strandWidth,
		glow,
		taper,
		spread,
		hueShift,
		intensity,
		saturation,
		opacity,
		scale,
		glass,
		refraction,
		dispersion,
		glassSize,
	};

	const ctnDom = useRef<HTMLDivElement>(null);
	const pausedRef = useRef(paused);
	pausedRef.current = paused;
	const drawRef = useRef<((t: number) => void | false) | null>(null);
	const measureRef = useRef<((m: Metrics) => void) | null>(null);
	const glRef = useRef<WebGLRenderingContext | WebGL2RenderingContext | null>(
		null,
	);

	const loop = useAnimationLoop({
		target: ctnDom,
		halted: paused,
		dpr: 2,
		onResize: (metrics) => measureRef.current?.(metrics),
		onFrame: ({ now }) => (drawRef.current ? drawRef.current(now) : false),
		gl: () => glRef.current,
	});

	useEffect(() => {
		const ctn = ctnDom.current;
		if (!ctn) return;

		const probe = document.createElement("canvas");
		if (!probe.getContext("webgl2") && !probe.getContext("webgl")) return;

		const renderer = new Renderer({
			alpha: true,
			premultipliedAlpha: true,
			antialias: true,
		});
		const gl = renderer.gl;
		gl.clearColor(0, 0, 0, 0);
		gl.enable(gl.BLEND);
		gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
		gl.canvas.style.backgroundColor = "transparent";

		const geometry = new Triangle(gl);
		if (geometry.attributes.uv) {
			delete geometry.attributes.uv;
		}

		const bw = gl.drawingBufferWidth || ctn.offsetWidth;
		const bh = gl.drawingBufferHeight || ctn.offsetHeight;

		const program = new Program(gl, {
			vertex: VERT,
			fragment: FRAG,
			uniforms: {
				uTime: { value: 0 },
				uResolution: { value: [bw, bh] },
				uColors: { value: buildPalette(propsRef.current.colors) },
				uColorCount: {
					value: Math.min(propsRef.current.colors.length, MAX_COLORS),
				},
				uStrandCount: { value: Math.min(propsRef.current.count, MAX_STRANDS) },
				uSpeed: { value: speed },
				uAmplitude: { value: amplitude },
				uWaviness: { value: waviness },
				uThickness: { value: thickness },
				uGlow: { value: glow },
				uTaper: { value: taper },
				uSpread: { value: spread },
				uHueShift: { value: hueShift },
				uIntensity: { value: intensity },
				uOpacity: { value: opacity },
				uScale: { value: scale },
				uSaturation: { value: saturation },
				uEnvWidth: {
					value:
						(glass ? 0.46 * glassSize : 0.3846) *
						Math.min(Math.max(strandWidth, 1), 100) *
						0.01,
				},
				uBrightTaper: { value: taper * (glass ? 0.3 : 1) },
			},
		});

		const mesh = new Mesh(gl, { geometry, program });

		const renderTarget = new RenderTarget(gl, {
			width: bw,
			height: bh,
		});

		const glassProgram = new Program(gl, {
			vertex: VERT,
			fragment: GLASS_FRAG,
			uniforms: {
				uScene: { value: renderTarget.texture },
				uResolution: { value: [bw, bh] },
				uRadius: { value: 0.46 * glassSize * scale },
				uRefraction: { value: refraction },
				uDispersion: { value: dispersion },
			},
		});
		const glassMesh = new Mesh(gl, { geometry, program: glassProgram });

		gl.canvas.style.position = "absolute";
		gl.canvas.style.top = "0";
		gl.canvas.style.left = "0";
		ctn.appendChild(gl.canvas);

		glRef.current = gl;
		measureRef.current = ({
			width,
			height,
			dpr,
			bufferWidth,
			bufferHeight,
		}) => {
			renderer.dpr = dpr;
			renderer.setSize(width, height);
			const targetBw = gl.drawingBufferWidth || bufferWidth;
			const targetBh = gl.drawingBufferHeight || bufferHeight;
			program.uniforms.uResolution.value = [targetBw, targetBh];
			renderTarget.setSize(targetBw, targetBh);
			glassProgram.uniforms.uResolution.value = [targetBw, targetBh];
		};

		let lastT = -1;
		let accum = 0;
		const update = (t: number) => {
			const dt = lastT >= 0 ? t - lastT : 0;
			lastT = t;
			const current = propsRef.current;
			if (!pausedRef.current) accum += dt;
			program.uniforms.uTime.value = accum * 0.001;
			program.uniforms.uColors.value = buildPalette(current.colors);
			program.uniforms.uColorCount.value = Math.min(
				current.colors.length,
				MAX_COLORS,
			);
			program.uniforms.uStrandCount.value = Math.min(
				Math.max(Math.round(current.count), 1),
				MAX_STRANDS,
			);
			program.uniforms.uSpeed.value = current.speed;
			program.uniforms.uAmplitude.value = current.amplitude;
			program.uniforms.uWaviness.value = current.waviness;
			program.uniforms.uThickness.value = current.thickness;
			program.uniforms.uGlow.value = current.glow;
			program.uniforms.uTaper.value = current.taper;
			program.uniforms.uSpread.value = current.spread;
			program.uniforms.uHueShift.value = current.hueShift;
			program.uniforms.uIntensity.value = current.intensity;
			program.uniforms.uOpacity.value = current.opacity;
			program.uniforms.uScale.value = current.scale;
			program.uniforms.uSaturation.value = current.saturation;
			program.uniforms.uEnvWidth.value =
				(current.glass ? 0.46 * current.glassSize : 0.3846) *
				Math.min(Math.max(current.strandWidth, 1), 100) *
				0.01;
			program.uniforms.uBrightTaper.value =
				current.taper * (current.glass ? 0.3 : 1);

			if (current.glass) {
				renderer.render({ scene: mesh, target: renderTarget });
				glassProgram.uniforms.uScene.value = renderTarget.texture;
				glassProgram.uniforms.uRefraction.value = current.refraction;
				glassProgram.uniforms.uDispersion.value = current.dispersion;
				glassProgram.uniforms.uRadius.value =
					0.46 * current.glassSize * current.scale;
				renderer.render({ scene: glassMesh });
			} else {
				renderer.render({ scene: mesh });
			}

			if (pausedRef.current) return false;
		};
		drawRef.current = update;

		loop.resize();
		loop.start();

		return () => {
			drawRef.current = null;
			measureRef.current = null;
			if (ctn && gl.canvas.parentNode === ctn) {
				ctn.removeChild(gl.canvas);
			}
		};
		// eslint-disable-next-line react-hooks/exhaustive-deps
	}, []);

	return (
		<div
			ref={ctnDom}
			className={`relative h-full w-full bg-transparent ${className}`}
			style={style}
		/>
	);
}
```

### 2. `hooks/use-animation-loop.ts` — peer hook, required

```tsx
"use client";

import { useCallback, useEffect, useLayoutEffect, useMemo, useRef } from "react";

export interface Metrics {
  width: number;
  height: number;
  dpr: number;
  bufferWidth: number;
  bufferHeight: number;
}

export interface FrameInfo {
  now: number;
  dt: number;
  elapsed: number;
  frame: number;
}

export interface AnimationLoopHandle {
  start(): void;
  stop(): void;
  paint(): void;
  resize(): void;
  readonly running: boolean;
}

export interface AnimationLoopOptions {
  target: React.RefObject<HTMLElement | null>;
  halted?: boolean;
  dpr?: number | "auto";
  onResize?(metrics: Metrics): void;
  onFrame?(info: FrameInfo): void | false;
  paintWhenHalted?: boolean;
  resizeDebounceMs?: number;
  gl?(): WebGLRenderingContext | WebGL2RenderingContext | null | undefined;
  onDispose?(): void;
  deps?: unknown[];
}

const MAX_DT = 1 / 15;

export function useAnimationLoop(
  options: AnimationLoopOptions,
): AnimationLoopHandle {
  const opts = useRef(options);
  useLayoutEffect(() => {
    opts.current = options;
  });

  const raf = useRef<number | null>(null);
  const timer = useRef<ReturnType<typeof setTimeout> | null>(null);
  const running = useRef(false);
  const disposed = useRef(false);
  const startedAt = useRef(0);
  const last = useRef(0);
  const frame = useRef(0);

  const measure = useCallback((): Metrics | null => {
    const el = opts.current.target.current;
    if (!el) return null;
    const rect = el.getBoundingClientRect();
    const cap = opts.current.dpr ?? "auto";
    const limit = cap === "auto" ? 2 : cap;
    const dpr = Math.min(
      typeof window === "undefined" ? 1 : window.devicePixelRatio || 1,
      limit,
    );
    return {
      width: rect.width,
      height: rect.height,
      dpr,
      bufferWidth: Math.max(1, Math.round(rect.width * dpr)),
      bufferHeight: Math.max(1, Math.round(rect.height * dpr)),
    };
  }, []);

  const tick = useCallback(function tick(now: number) {
    if (disposed.current) return;

    if (startedAt.current === 0) startedAt.current = now;
    const dt = last.current === 0 ? 0 : Math.min((now - last.current) / 1000, MAX_DT);
    last.current = now;

    const info: FrameInfo = {
      now,
      dt,
      elapsed: (now - startedAt.current) / 1000,
      frame: frame.current++,
    };

    const verdict = opts.current.onFrame?.(info);

    if (disposed.current) return;

    if (verdict === false || opts.current.halted) {
      running.current = false;
      raf.current = null;
      return;
    }
    raf.current = requestAnimationFrame(tick);
  }, []);

  const start = useCallback(() => {
    if (disposed.current || running.current) return;
    running.current = true;
    last.current = 0;
    raf.current = requestAnimationFrame(tick);
  }, [tick]);

  const stop = useCallback(() => {
    running.current = false;
    if (raf.current !== null) {
      cancelAnimationFrame(raf.current);
      raf.current = null;
    }
  }, []);

  const paint = useCallback(() => start(), [start]);

  const resize = useCallback(() => {
    const metrics = measure();
    if (!metrics) return;
    opts.current.onResize?.(metrics);
    if (opts.current.paintWhenHalted !== false) paint();
    else if (!opts.current.halted) start();
  }, [measure, paint, start]);

  const deps = options.deps ?? [];

  useEffect(() => {
    disposed.current = false;
    const el = opts.current.target.current;
    if (!el) return;

    const run = () => {
      if (disposed.current) return;
      resize();
    };

    const observer = new ResizeObserver(() => {
      const wait = opts.current.resizeDebounceMs ?? 0;
      if (wait <= 0) {
        run();
        return;
      }
      if (timer.current !== null) clearTimeout(timer.current);
      timer.current = setTimeout(() => {
        timer.current = null;
        run();
      }, wait);
    });
    observer.observe(el);

    resize();
    if (!opts.current.halted) start();

    return () => {
      disposed.current = true;
      running.current = false;
      if (raf.current !== null) {
        cancelAnimationFrame(raf.current);
        raf.current = null;
      }
      if (timer.current !== null) {
        clearTimeout(timer.current);
        timer.current = null;
      }
      observer.disconnect();
      opts.current.onDispose?.();
      const context = opts.current.gl?.();
      context?.getExtension("WEBGL_lose_context")?.loseContext();
      startedAt.current = 0;
      last.current = 0;
      frame.current = 0;
    };
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, deps);

  const halted = options.halted ?? false;
  useEffect(() => {
    if (!halted) start();
  }, [halted, start]);

  return useMemo<AnimationLoopHandle>(
    () => ({
      start,
      stop,
      paint,
      resize,
      get running() {
        return running.current;
      },
    }),
    [start, stop, paint, resize],
  );
}
```


## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `count` | `number` | `13` | Number of strands. Range 3–40. |
| `scale` | `number` | `0.9` | Field size — lower zooms out, higher magnifies the strands. Range 0.5–3. |
| `speed` | `number` | `0.8` | Animation speed. Range 0–3. |
| `amplitude` | `number` | `1.25` | Wave amplitude. Range 0–2. |
| `waviness` | `number` | `1` | Waviness of the strands. Range 0–3. |
| `thickness` | `number` | `1.55` | Strand thickness. Range 0.2–4. |
| `strandWidth` | `number` | `100` | Horizontal reach as a percentage — 100 spans to the glass rim. Range 10–100. |
| `glow` | `number` | `0.25` | Glow strength. Range 0–2. |
| `spread` | `number` | `1.65` | Vertical spread. Range 0–2. |
| `intensity` | `number` | `1` | Color intensity. Range 0–2. |
| `saturation` | `number` | `1` | Color saturation. Range 0–2. |
| `opacity` | `number` | `0.9` | Overall opacity. Range 0–1. |
| `glass` | `boolean` | `true` | Overlay a glass refraction lens. |
| `refraction` | `number` | `1.3` | Glass refraction strength. Range 0–2. |
| `dispersion` | `number` | `0.5` | Chromatic dispersion. Range 0–2. |

## Usage

```tsx
import Strands from "@/components/registry/strands/strands";

<Strands
  count={13}
  scale={0.9}
  speed={0.8}
  amplitude={1.25}
  waviness={1}
  thickness={1.55}
  strandWidth={100}
  glow={0.25}
  spread={1.65}
  intensity={1}
  saturation={1}
  opacity={0.9}
  glass={true}
  refraction={1.3}
  dispersion={0.5}
/>
```

The import path matches the file path above; change it if you move the component. Every value shown is the documented default — pass only what you actually want to change. The snippet lists the tunable props only: if the component's props type in the source requires others (content, handlers, data), pass those too.

## Integration rules

- Keep the `"use client"` directive. This is a client component and will not render as a Server Component.
- It fills its parent. Give the wrapper an explicit height (or a definite grid area) — against a height-auto parent it collapses to nothing.
- It runs a free-running animation loop. Halt it when off-screen or backgrounded if you care about battery.
- The peer hook is required, not optional. Copy it to `hooks/use-animation-loop.ts` so the component's `@/hooks/use-animation-loop` import resolves.
- It owns a GPU/canvas context and releases it on unmount. Preserve the cleanup return — leaking contexts kills the tab after a few navigations.

## Your task

1. Tell me where in my project this should live and why.
2. Create the files above verbatim.
3. Wire the component into the place I point you at, with sensible props for that context.
4. Flag anything in my setup that conflicts with the stack assumptions above.

Do not restyle the component or alter the animation math unless I ask.
