{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "fx-runtime",
  "title": "Effects runtime",
  "description": "Shared runtime of the Effects collection: gated frame loop, mouse and touch pointer with ghost hand, CSS colour conversion and capped device pixel ratio.",
  "dependencies": [],
  "devDependencies": [],
  "registryDependencies": [
    "https://reactmotionkit.com/r/use-prefers-reduced-motion.json",
    "https://reactmotionkit.com/r/use-should-animate.json"
  ],
  "files": [
    {
      "path": "lib/fx-runtime.ts",
      "content": "\"use client\"\n\n// Shared runtime of the Effects collection (ported from the lab). Reduced motion is read with\n// usePrefersReducedMotion by the effects themselves; here only the loop, viewport and pointer. Colours: `@/lib/fx-color`.\nimport { useEffect, useRef, useState, type RefObject } from \"react\"\n\nimport { usePrefersReducedMotion } from \"@/hooks/use-prefers-reduced-motion\"\nimport { useShouldAnimate } from \"@/hooks/use-should-animate\"\n\n/** True while the element is in the viewport AND the tab is visible (reduced motion is separate). */\nexport function useActive(ref: RefObject<Element | null>): boolean {\n  const { inView, pageVisible } = useShouldAnimate(ref)\n  return inView && pageVisible\n}\n\n/**\n * rAF loop that only runs while `active`. `cb(t, dt)` gets seconds since the loop first\n * started (paused time excluded) and the real frame delta (clamped to 1/20 s). `speed` scales\n * `t` only: the effect's clock follows the temperament, pointer easing on `dt` stays real time.\n */\nexport function useFrame(active: boolean, cb: (t: number, dt: number) => void, speed = 1) {\n  const cbRef = useRef(cb)\n  const speedRef = useRef(speed)\n  useEffect(() => {\n    cbRef.current = cb\n    speedRef.current = speed\n  })\n  const tRef = useRef(0)\n  useEffect(() => {\n    if (!active) return\n    let raf = 0\n    let last = performance.now()\n    const tick = (now: number) => {\n      // the first rAF timestamp can precede the performance.now() taken before scheduling it\n      const dt = Math.max(0, Math.min((now - last) / 1000, 0.05))\n      last = now\n      tRef.current += dt * speedRef.current\n      cbRef.current(tRef.current, dt)\n      raf = requestAnimationFrame(tick)\n    }\n    raf = requestAnimationFrame(tick)\n    return () => {\n      cancelAnimationFrame(raf)\n    }\n  }, [active])\n}\n\n/** Pointer position normalised to the element (0..1), eased toward the target. */\nexport function createSmoothedPointer(ease = 6) {\n  const target = { x: 0.5, y: 0.5, inside: 0 }\n  const value = { x: 0.5, y: 0.5, inside: 0 }\n  return {\n    target,\n    value,\n    step(dt: number) {\n      const k = 1 - Math.exp(-ease * dt)\n      value.x += (target.x - value.x) * k\n      value.y += (target.y - value.y) * k\n      value.inside += (target.inside - value.inside) * k\n    },\n  }\n}\n\n/** Normalised position (0..1) of a ghost hand at `s` seconds of the effect's clock. */\nexport type EffectPath = (s: number) => readonly [number, number]\nexport type EffectPointerSource = \"none\" | \"mouse\" | \"finger\" | \"ghost\"\n\n/** Read `.current` every frame; it is mutated in place (no React renders). */\nexport interface EffectPointer {\n  /** Position in the element, 0..1. Raw with a mouse (the effect eases it as it always has). */\n  x: number\n  y: number\n  /** The effect should react to (x, y). */\n  inside: boolean\n  source: EffectPointerSource\n  /** The latest real input is touch, or the device has no hover. */\n  touch: boolean\n  /** Touch, with the `progress` option: 0..1, advances over time, a horizontal drag moves it. */\n  progress: number\n}\n\nexport interface EffectPointerOptions {\n  /** `ghost` loops an automatic hand under touch; `enter` and `touch` only follow a finger. */\n  mobile?: \"ghost\" | \"enter\" | \"touch\"\n  /** Gesture of the ghost hand. Defaults to the cursor of the review videos. */\n  path?: EffectPath\n  /** Clock of the ghost hand and of `progress` (the temperament speed). */\n  speed?: number\n  /**\n   * Touch only: `pointer.progress` runs 0..1 over `duration` seconds, stops off screen, and a\n   * horizontal drag moves it (one element width = the whole range). `limit` caps how far it may\n   * advance by itself, e.g. up to the parts already seen. It never follows scrolling.\n   */\n  progress?: { duration: number; limit?: () => number }\n}\n\nexport const GHOST_PATH: EffectPath = (s) => [\n  0.5 + 0.32 * Math.sin(1.3 * s),\n  0.5 + 0.26 * Math.sin(2.1 * s + 0.6),\n]\n\n// Input timing, not effect motion: MISSION.md fixes the 1.5 s; the blend and the follow rate only\n// keep the hand from snapping when a finger lands or lifts, whatever the temperament.\nconst GHOST_RESUME = 1.5\nconst GHOST_BLEND = 1.2\nconst TOUCH_FOLLOW = 18\n// Movement (css px) before a gesture is read as horizontal or vertical; a vertical one is the page's.\nconst GESTURE_SLOP = 8\n\n/**\n * The only input path of an effect. Mouse and pen: raw position, as a hover effect expects. Touch:\n * an automatic hand (`mobile: \"ghost\"`) until a finger lands; the hand comes back 1.5 s after\n * release, easing from where the finger left it. Touch listeners are passive and never cancel, so\n * the page always scrolls; they keep tracking through a pan, when pointer events are cancelled.\n */\nexport function useEffectPointer(\n  ref: RefObject<Element | null>,\n  { mobile = \"ghost\", path = GHOST_PATH, speed = 1, progress }: EffectPointerOptions = {},\n): RefObject<EffectPointer> {\n  const pointerRef = useRef<EffectPointer>({\n    x: 0.5,\n    y: 0.5,\n    inside: false,\n    source: \"none\",\n    touch: false,\n    progress: 0,\n  })\n  const [touch, setTouch] = useState(false)\n  const reduced = usePrefersReducedMotion()\n  const active = useActive(ref)\n  const finger = useRef<readonly [number, number] | null>(null)\n  const released = useRef({ at: -Infinity, x: 0.5, y: 0.5 })\n  const gesture = useRef<{ cx: number; cy: number; base: number; axis: \"x\" | \"y\" | null } | null>(\n    null,\n  )\n  const options = useRef({ mobile, path, progress })\n  useEffect(() => {\n    options.current = { mobile, path, progress }\n  })\n\n  useEffect(() => {\n    const el = ref.current\n    if (!el) return\n    const pointer = pointerRef.current\n    const setMode = (on: boolean) => {\n      pointer.touch = on\n      setTouch(on)\n    }\n    if (window.matchMedia(\"(hover: none)\").matches) setMode(true)\n    const at = (cx: number, cy: number) => {\n      const r = el.getBoundingClientRect()\n      return [(cx - r.left) / (r.width || 1), (cy - r.top) / (r.height || 1)] as const\n    }\n    const place = (p: readonly [number, number], inside: boolean, source: EffectPointerSource) => {\n      pointer.x = p[0]\n      pointer.y = p[1]\n      pointer.inside = inside\n      pointer.source = source\n    }\n    const onPointer = (e: Event) => {\n      const { pointerType, clientX, clientY } = e as PointerEvent\n      if (pointerType === \"touch\") return\n      setMode(false)\n      finger.current = null\n      place(at(clientX, clientY), true, \"mouse\")\n    }\n    const onLeave = (e: Event) => {\n      if ((e as PointerEvent).pointerType === \"touch\") return\n      pointer.inside = false\n      pointer.source = \"none\"\n    }\n    const letGo = () => {\n      const f = finger.current\n      if (!f) return\n      released.current = { at: performance.now(), x: f[0], y: f[1] }\n      finger.current = null\n      if (reduced) place(f, false, \"none\")\n    }\n    const onStart = (e: Event) => {\n      const { touches } = e as TouchEvent\n      const t = touches[0]\n      if (!t) return\n      if (touches.length > 1) {\n        letGo() // a pinch belongs to the page\n        return\n      }\n      gesture.current = { cx: t.clientX, cy: t.clientY, base: pointer.progress, axis: null }\n      finger.current = at(t.clientX, t.clientY)\n      setMode(true)\n      if (reduced) place(finger.current, true, \"finger\")\n    }\n    const onMove = (e: Event) => {\n      const { touches } = e as TouchEvent\n      const t = touches[0]\n      const g = gesture.current\n      if (!t || !g) return\n      const dx = t.clientX - g.cx\n      const dy = t.clientY - g.cy\n      if (!g.axis && Math.hypot(dx, dy) >= GESTURE_SLOP)\n        g.axis = Math.abs(dx) > Math.abs(dy) ? \"x\" : \"y\"\n      if (g.axis === \"y\" || touches.length > 1) {\n        letGo()\n        return\n      }\n      finger.current = at(t.clientX, t.clientY)\n      if (g.axis === \"x\" && options.current.progress) {\n        const width = el.getBoundingClientRect().width || 1\n        pointer.progress = Math.min(1, Math.max(0, g.base + dx / width))\n      }\n      if (reduced) place(finger.current, true, \"finger\")\n    }\n    const onLift = (e: Event) => {\n      if ((e as TouchEvent).touches.length) return\n      gesture.current = null\n      letGo()\n    }\n    const passive = { passive: true }\n    // A drag steers the effect; vertical scroll and page zoom stay with the page. Never `none`.\n    const touchAction = (el as HTMLElement).style.touchAction\n    ;(el as HTMLElement).style.touchAction = \"pan-y pinch-zoom\"\n    el.addEventListener(\"pointermove\", onPointer)\n    el.addEventListener(\"pointerdown\", onPointer)\n    el.addEventListener(\"pointerleave\", onLeave)\n    el.addEventListener(\"touchstart\", onStart, passive)\n    el.addEventListener(\"touchmove\", onMove, passive)\n    el.addEventListener(\"touchend\", onLift, passive)\n    el.addEventListener(\"touchcancel\", onLift, passive)\n    return () => {\n      el.removeEventListener(\"pointermove\", onPointer)\n      el.removeEventListener(\"pointerdown\", onPointer)\n      el.removeEventListener(\"pointerleave\", onLeave)\n      el.removeEventListener(\"touchstart\", onStart)\n      el.removeEventListener(\"touchmove\", onMove)\n      el.removeEventListener(\"touchend\", onLift)\n      el.removeEventListener(\"touchcancel\", onLift)\n      ;(el as HTMLElement).style.touchAction = touchAction\n    }\n  }, [ref, reduced])\n\n  useFrame(\n    touch && active && !reduced,\n    (t, dt) => {\n      const pointer = pointerRef.current\n      const rest = released.current\n      const held = (performance.now() - rest.at) / 1000\n      const k = finger.current ? 0 : Math.min(1, Math.max(0, (held - GHOST_RESUME) / GHOST_BLEND))\n      const blend = k * k * (3 - 2 * k)\n      const run = options.current.progress\n      if (run && blend > 0) {\n        const next = pointer.progress + (dt * speed * blend) / run.duration\n        // never pulled back: a drag may have gone past what `limit` allows\n        pointer.progress = Math.max(pointer.progress, Math.min(run.limit?.() ?? 1, next))\n      }\n      let target: readonly [number, number] = finger.current ?? [rest.x, rest.y]\n      let source: EffectPointerSource = \"finger\"\n      if (!finger.current && held >= GHOST_RESUME) {\n        if (options.current.mobile !== \"ghost\") {\n          pointer.inside = false\n          pointer.source = \"none\"\n          return\n        }\n        const [gx, gy] = options.current.path(t)\n        target = [rest.x + (gx - rest.x) * blend, rest.y + (gy - rest.y) * blend]\n        source = \"ghost\"\n      }\n      const follow = 1 - Math.exp(-TOUCH_FOLLOW * dt)\n      pointer.x += (target[0] - pointer.x) * follow\n      pointer.y += (target[1] - pointer.y) * follow\n      pointer.inside = true\n      pointer.source = source\n    },\n    speed,\n  )\n  return pointerRef\n}\n\n/** Device pixel ratio capped (default 2). Call only in effects/handlers, never in render. */\nexport const cappedDpr = (cap = 2) => Math.min(window.devicePixelRatio || 1, cap)\n",
      "type": "registry:lib"
    },
    {
      "path": "lib/fx-color.ts",
      "content": "// The one colour conversion of the Effects collection. Colour props take any CSS colour; shaders\n// and canvas paint read these numbers. No React, no DOM at import time.\n\nexport type Rgb = [number, number, number]\nexport type Oklab = [number, number, number]\n\nconst clamp01 = (x: number) => Math.min(1, Math.max(0, x))\nconst srgbEncode = (x: number) => {\n  const c = clamp01(x)\n  return c <= 0.0031308 ? 12.92 * c : 1.055 * c ** (1 / 2.4) - 0.055\n}\nconst srgbDecode = (c: number) => (c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4)\n\n/** OKLab to sRGB in 0..1, clamped to the gamut. */\nexport function oklabToRgb([L, a, b]: Oklab): Rgb {\n  const l = (L + 0.3963377774 * a + 0.2158037573 * b) ** 3\n  const m = (L - 0.1055613458 * a - 0.0638541728 * b) ** 3\n  const s = (L - 0.0894841775 * a - 1.291485548 * b) ** 3\n  return [\n    srgbEncode(4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s),\n    srgbEncode(-1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s),\n    srgbEncode(-0.0041960863 * l - 0.7034186147 * m + 1.707614701 * s),\n  ]\n}\n\nfunction rgbToOklab([red, green, blue]: Rgb): Oklab {\n  const r = srgbDecode(red)\n  const g = srgbDecode(green)\n  const b = srgbDecode(blue)\n  const l = Math.cbrt(0.4122214708 * r + 0.5363325363 * g + 0.0514459929 * b)\n  const m = Math.cbrt(0.2119034982 * r + 0.6806995451 * g + 0.1073969566 * b)\n  const s = Math.cbrt(0.0883024619 * r + 0.2817188376 * g + 0.6299787005 * b)\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\n/** Numbers of a CSS function, `none` as 0, each with whether it was a percentage. */\nfunction args(body: string) {\n  return (body.split(\"/\")[0] ?? \"\")\n    .split(/[\\s,]+/)\n    .filter(Boolean)\n    .map((token) => ({\n      value: token === \"none\" ? 0 : Number.parseFloat(token),\n      percent: token.endsWith(\"%\"),\n    }))\n}\n\n/**\n * CIE Lab (D50) to sRGB: browsers resolve `var()` of shadcn tokens compiled by Tailwind v4 to\n * `lab()`. `xr, yr, zr` are relative to the D50 white; the matrix is CSS Color 4's Bradford\n * D50→D65 times XYZ→linear sRGB, already scaled by the D50 white point.\n */\nfunction labToRgb(L: number, a: number, b: number): Rgb {\n  const fy = (L + 16) / 116\n  const f = (t: number) => (t ** 3 > 216 / 24389 ? t ** 3 : (116 * t - 16) / (24389 / 27))\n  const x = f(fy + a / 500)\n  const y = L > 8 ? fy ** 3 : L / (24389 / 27) // 8 = κ·ε of CIE Lab\n  const z = f(fy - b / 200)\n  return [\n    srgbEncode(3.022234 * x - 1.617386 * y - 0.404848 * z),\n    srgbEncode(-0.943848 * x + 1.916254 * y + 0.027594 * z),\n    srgbEncode(0.069386 * x - 0.228977 * y + 1.15959 * z),\n  ]\n}\n\nfunction hslToRgb(h: number, s: number, l: number): Rgb {\n  const f = (n: number) => {\n    const k = (n + h / 30) % 12\n    return l - s * Math.min(l, 1 - l) * Math.max(-1, Math.min(k - 3, 9 - k, 1))\n  }\n  return [f(0), f(8), f(4)]\n}\n\ntype Parsed = { rgb: Rgb } | { lab: Oklab }\n\nfunction parse(input: string): Parsed | null {\n  const text = input.trim().toLowerCase()\n  const hex = /^#([0-9a-f]{3,8})$/.exec(text)?.[1]\n  if (hex) {\n    if (hex.length === 3 || hex.length === 4) {\n      const at = (i: number) => Number.parseInt(hex.charAt(i).repeat(2), 16) / 255\n      return { rgb: [at(0), at(1), at(2)] }\n    }\n    if (hex.length === 6 || hex.length === 8) {\n      const at = (i: number) => Number.parseInt(hex.slice(i, i + 2), 16) / 255\n      return { rgb: [at(0), at(2), at(4)] }\n    }\n    return null\n  }\n  const fn = /^([a-z]+)\\((.*)\\)$/.exec(text)\n  if (!fn?.[1] || fn[2] === undefined) return null\n  const name = fn[1]\n  if (name === \"color\") {\n    const [space, ...rest] = fn[2].trim().split(/\\s+/)\n    if (space !== \"srgb\") return null\n    const [r, g, b] = args(rest.join(\" \"))\n    if (!r || !g || !b) return null\n    return { rgb: [clamp01(r.value), clamp01(g.value), clamp01(b.value)] }\n  }\n  const [x, y, z] = args(fn[2])\n  if (!x || !y || !z || [x, y, z].some((v) => Number.isNaN(v.value))) return null\n  if (name === \"rgb\" || name === \"rgba\") {\n    const channel = (v: typeof x) => clamp01(v.value / (v.percent ? 100 : 255))\n    return { rgb: [channel(x), channel(y), channel(z)] }\n  }\n  if (name === \"hsl\" || name === \"hsla\") {\n    const hue = ((x.value % 360) + 360) % 360\n    return { rgb: hslToRgb(hue, clamp01(y.value / 100), clamp01(z.value / 100)) }\n  }\n  if (name === \"lab\")\n    return {\n      rgb: labToRgb(x.value, y.value * (y.percent ? 1.25 : 1), z.value * (z.percent ? 1.25 : 1)),\n    }\n  if (name === \"lch\") {\n    const C = y.value * (y.percent ? 1.5 : 1)\n    const h = (z.value * Math.PI) / 180\n    return { rgb: labToRgb(x.value, C * Math.cos(h), C * Math.sin(h)) }\n  }\n  if (name === \"oklch\" || name === \"oklab\") {\n    const L = x.value / (x.percent ? 100 : 1)\n    if (name === \"oklab\") return { lab: [L, y.value, z.value] }\n    const C = y.percent ? (y.value / 100) * 0.4 : y.value\n    const h = (z.value * Math.PI) / 180\n    return { lab: [L, C * Math.cos(h), C * Math.sin(h)] }\n  }\n  return null\n}\n\n/** Names, `var(--x)`, `color-mix()`: what only the browser can resolve. Null on the server. */\nfunction resolveInDom(input: string): Parsed | null {\n  if (typeof document === \"undefined\") return null\n  const probe = document.createElement(\"span\")\n  probe.style.color = input\n  if (!probe.style.color) return null\n  probe.style.display = \"none\"\n  document.body.append(probe)\n  const computed = getComputedStyle(probe).color\n  probe.remove()\n  return computed && computed !== input ? parse(computed) : null\n}\n\nconst FALLBACK: Parsed = { rgb: [0.5, 0.5, 0.5] }\n\nfunction read(input: string): Parsed {\n  return parse(input) ?? resolveInDom(input) ?? FALLBACK\n}\n\n/** Any CSS colour (hex, rgb, hsl, lab, lch, oklch, names, `var()`) as sRGB in 0..1. Alpha is ignored. */\nexport function colorToRgb(input: string): Rgb {\n  const parsed = read(input)\n  return \"rgb\" in parsed ? [...parsed.rgb] : oklabToRgb(parsed.lab)\n}\n\n/** Any CSS colour as OKLab `[L, a, b]`, for mixing in shaders and canvas paint. */\nexport function colorToOklab(input: string): Oklab {\n  const parsed = read(input)\n  return \"lab\" in parsed ? [...parsed.lab] : rgbToOklab(parsed.rgb)\n}\n\n/** `#rrggbb` of an sRGB triplet. */\nexport function rgbToHex(rgb: Rgb): string {\n  return `#${rgb\n    .map((c) =>\n      Math.round(clamp01(c) * 255)\n        .toString(16)\n        .padStart(2, \"0\"),\n    )\n    .join(\"\")}`\n}\n\n/** `#rrggbb` of any CSS colour: what `<input type=\"color\">` needs. */\nexport const colorToHex = (input: string) => rgbToHex(colorToRgb(input))\n",
      "type": "registry:lib"
    }
  ],
  "meta": {
    "tier": "free",
    "collection": "interface",
    "needsImage": false,
    "addedAt": "2026-11-08",
    "tags": [
      "effects",
      "runtime",
      "webgl"
    ],
    "tech": [],
    "presets": [],
    "keyboard": [],
    "heavy": false
  },
  "categories": [
    "internal"
  ],
  "type": "registry:lib"
}