{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "scroll-parallax-layers",
  "type": "registry:component",
  "title": "Scroll Parallax Layers",
  "description": "A dusk scene built from three SVG layers (sky, hills, pines) that drift at different speeds as it crosses the viewport.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "motion",
    "scroll",
    "parallax",
    "layers",
    "svg",
    "scene",
    "depth"
  ],
  "docs": "https://hyperblur.dev/effects/scroll-parallax-layers",
  "files": [
    {
      "path": "registry/hyperblur/scroll-parallax-layers.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/scroll-parallax-layers.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface ScrollParallaxLayersProps extends React.HTMLAttributes<HTMLDivElement> {\n  scroller?: React.RefObject<HTMLElement | null> | HTMLElement | null;\n  height?: number | string;\n  autoplay?: boolean;\n  sceneHeight?: number | string;\n  strength?: number;\n  accent?: string;\n  before?: React.ReactNode;\n  after?: React.ReactNode;\n}\n\ntype Scroller = React.RefObject<HTMLElement | null> | HTMLElement | null;\n\ninterface Frame {\n  sc: HTMLElement | null;\n  top: number;\n  vh: number;\n}\n\nconst css = `\n.hb-qm2 {\n  --hb-qm2-p: 0;\n  --hb-qm2-k: 1;\n  --hb-qm2-a: #ffb26b;\n  --hb-qm2-h: 320px;\n  position: relative;\n  width: 100%;\n  font-family: inherit;\n}\n.hb-qm2, .hb-qm2 * { box-sizing: border-box; }\n.hb-qm2-vp {\n  position: relative;\n  overflow-x: hidden;\n  overflow-y: auto;\n  overflow-anchor: none;\n  scrollbar-width: thin;\n  scrollbar-color: #2e2e2e transparent;\n  outline: none;\n}\n.hb-qm2-vp:focus-visible { outline: 2px solid var(--hb-qm2-a); outline-offset: -2px; }\n.hb-qm2-scene {\n  position: relative;\n  height: var(--hb-qm2-h);\n  overflow: hidden;\n  isolation: isolate;\n  background: #0a0c26;\n}\n.hb-qm2-layer {\n  position: absolute;\n  left: 0;\n  right: 0;\n  top: -25%;\n  bottom: -25%;\n  will-change: transform;\n}\n.hb-qm2-layer svg { display: block; width: 100%; height: 100%; }\n.hb-qm2-sky { transform: translate3d(0, calc((var(--hb-qm2-p) - 0.5) * var(--hb-qm2-k) * 9%), 0); }\n.hb-qm2-hills { transform: translate3d(0, calc((var(--hb-qm2-p) - 0.5) * var(--hb-qm2-k) * -14%), 0); }\n.hb-qm2-fore { transform: translate3d(0, calc((var(--hb-qm2-p) - 0.5) * var(--hb-qm2-k) * -24%), 0); }\n.hb-qm2-sun { fill: var(--hb-qm2-a); }\n.hb-qm2-over {\n  position: absolute;\n  inset: 0;\n  z-index: 3;\n  display: flex;\n  align-items: center;\n  justify-content: center;\n  padding: 1em;\n  text-align: center;\n  color: #fff;\n  text-shadow: 0 2px 18px rgba(8, 6, 30, 0.6);\n  pointer-events: none;\n}\n@media (prefers-reduced-motion: reduce) {\n  .hb-qm2-layer { transform: none; will-change: auto; }\n}\n`;\n\nfunction hash(n: number) {\n  let x = Math.imul(n + 1, 0x9e3779b1) >>> 0;\n  x = Math.imul(x ^ (x >>> 15), 0x85ebca6b) >>> 0;\n  x = Math.imul(x ^ (x >>> 13), 0xc2b2ae35) >>> 0;\n  return (x ^ (x >>> 16)) >>> 0;\n}\n\nconst starField = Array.from({ length: 30 }, (_, i) => {\n  const a = hash(i * 3);\n  const b = hash(i * 3 + 1);\n  const c = hash(i * 3 + 2);\n  return {\n    x: ((a % 4000) / 10).toFixed(1),\n    y: ((b % 1300) / 10 + 6).toFixed(1),\n    r: (0.5 + (c % 9) / 10).toFixed(2),\n    o: (0.35 + (c % 6) / 10).toFixed(2),\n  };\n});\n\nfunction pine(cx: number, base: number, h: number, w: number) {\n  const y = (f: number) => (base - h * f).toFixed(1);\n  const x = (d: number) => (cx + w * d).toFixed(1);\n  return `M${x(0)} ${y(1)}L${x(0.32)} ${y(0.62)}L${x(0.18)} ${y(0.62)}L${x(0.46)} ${y(0.3)}L${x(0.28)} ${y(0.3)}L${x(0.5)} ${y(0)}L${x(-0.5)} ${y(0)}L${x(-0.28)} ${y(0.3)}L${x(-0.46)} ${y(0.3)}L${x(-0.18)} ${y(0.62)}L${x(-0.32)} ${y(0.62)}Z`;\n}\n\nconst pines = [\n  pine(24, 215, 130, 58),\n  pine(64, 212, 92, 42),\n  pine(104, 210, 52, 26),\n  pine(296, 210, 62, 30),\n  pine(338, 214, 124, 56),\n  pine(380, 212, 94, 44),\n].join(\"\");\n\nfunction clamp(n: number) {\n  return Math.min(1, Math.max(0, n));\n}\n\nfunction pick(scroller: Scroller | undefined): HTMLElement | null {\n  if (!scroller) return null;\n  return \"current\" in scroller ? scroller.current : scroller;\n}\n\nfunction useScrollFrame(\n  rootRef: React.RefObject<HTMLElement | null>,\n  viewportRef: React.RefObject<HTMLElement | null>,\n  scroller: Scroller | undefined,\n  own: boolean,\n  onFrame: (frame: Frame) => void,\n) {\n  const latest = React.useRef(onFrame);\n\n  React.useEffect(() => {\n    latest.current = onFrame;\n  });\n\n  React.useEffect(() => {\n    const root = rootRef.current;\n    if (!root) return;\n    const sc = own ? viewportRef.current : pick(scroller);\n    const host: HTMLElement | Window = sc ?? window;\n    const run = () => {\n      const top = sc ? sc.getBoundingClientRect().top : 0;\n      const vh = sc ? sc.clientHeight : window.innerHeight;\n      latest.current({ sc, top, vh });\n    };\n    host.addEventListener(\"scroll\", run, { passive: true });\n    window.addEventListener(\"resize\", run);\n    const ro = new ResizeObserver(run);\n    ro.observe(root);\n    if (sc) ro.observe(sc);\n    return () => {\n      host.removeEventListener(\"scroll\", run);\n      window.removeEventListener(\"resize\", run);\n      ro.disconnect();\n    };\n  }, [rootRef, viewportRef, scroller, own]);\n}\n\nfunction useAutoScroll(viewportRef: React.RefObject<HTMLElement | null>, active: boolean) {\n  React.useEffect(() => {\n    const el = viewportRef.current;\n    if (!el || !active) return;\n    if (window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches) {\n      el.scrollTop = (el.scrollHeight - el.clientHeight) * 0.45;\n      return;\n    }\n    let raf = 0;\n    let last = 0;\n    let pos = 0;\n    let dir = 1;\n    let hold = 700;\n    let idle = 0;\n    const touch = () => {\n      idle = performance.now() + 3200;\n    };\n    const tick = (now: number) => {\n      raf = requestAnimationFrame(tick);\n      const dt = Math.min(now - last, 50);\n      last = now;\n      const max = el.scrollHeight - el.clientHeight;\n      if (max <= 0) return;\n      if (now < idle) {\n        pos = el.scrollTop / max;\n        return;\n      }\n      if (hold > 0) {\n        hold -= dt;\n        return;\n      }\n      pos += (dir * dt) / 9000;\n      if (pos >= 1) {\n        pos = 1;\n        dir = -1;\n        hold = 900;\n      } else if (pos <= 0) {\n        pos = 0;\n        dir = 1;\n        hold = 900;\n      }\n      el.scrollTop = pos * max;\n    };\n    const io = new IntersectionObserver(([entry]) => {\n      if (entry.isIntersecting) {\n        if (!raf) {\n          last = performance.now();\n          raf = requestAnimationFrame(tick);\n        }\n      } else {\n        cancelAnimationFrame(raf);\n        raf = 0;\n      }\n    });\n    io.observe(el);\n    const events = [\"wheel\", \"touchstart\", \"pointerdown\", \"keydown\"] as const;\n    for (const name of events) el.addEventListener(name, touch, { passive: true });\n    return () => {\n      cancelAnimationFrame(raf);\n      io.disconnect();\n      for (const name of events) el.removeEventListener(name, touch);\n    };\n  }, [viewportRef, active]);\n}\n\nexport function ScrollParallaxLayers({\n  scroller,\n  height,\n  autoplay = false,\n  sceneHeight = 320,\n  strength = 1,\n  accent = \"#ffb26b\",\n  before,\n  after,\n  className = \"\",\n  style,\n  children,\n  ...rest\n}: ScrollParallaxLayersProps) {\n  const rootRef = React.useRef<HTMLDivElement>(null);\n  const viewportRef = React.useRef<HTMLDivElement>(null);\n  const sceneRef = React.useRef<HTMLDivElement>(null);\n  const uid = React.useId().replace(/:/g, \"\");\n  const own = height !== undefined;\n  const k = Math.min(1.3, Math.max(0, strength));\n\n  useAutoScroll(viewportRef, own && autoplay);\n  useScrollFrame(rootRef, viewportRef, scroller, own, ({ top, vh }) => {\n    const scene = sceneRef.current;\n    if (!scene) return;\n    const rect = scene.getBoundingClientRect();\n    const p = clamp((top + vh - rect.top) / (vh + rect.height));\n    rootRef.current?.style.setProperty(\"--hb-qm2-p\", p.toFixed(4));\n  });\n\n  const body = (\n    <>\n      {before}\n      <div ref={sceneRef} className=\"hb-qm2-scene\">\n        <div className=\"hb-qm2-layer hb-qm2-sky\" aria-hidden=\"true\">\n          <svg viewBox=\"0 0 400 300\" preserveAspectRatio=\"xMidYMid slice\">\n            <defs>\n              <linearGradient id={`${uid}s`} gradientUnits=\"userSpaceOnUse\" x1=\"0\" y1=\"0\" x2=\"0\" y2=\"300\">\n                <stop offset=\"0\" stopColor=\"#0a0c26\" />\n                <stop offset=\"0.3\" stopColor=\"#1c1b4b\" />\n                <stop offset=\"0.52\" stopColor=\"#4a2d7a\" />\n                <stop offset=\"0.66\" stopColor=\"#b5527f\" />\n                <stop offset=\"0.8\" style={{ stopColor: \"var(--hb-qm2-a)\" }} />\n              </linearGradient>\n              <radialGradient id={`${uid}g`}>\n                <stop offset=\"0\" style={{ stopColor: \"var(--hb-qm2-a)\", stopOpacity: 0.55 }} />\n                <stop offset=\"1\" style={{ stopColor: \"var(--hb-qm2-a)\", stopOpacity: 0 }} />\n              </radialGradient>\n            </defs>\n            <rect width=\"400\" height=\"300\" fill={`url(#${uid}s)`} />\n            {starField.map((s, i) => (\n              <circle key={i} cx={s.x} cy={s.y} r={s.r} fill=\"#fff\" opacity={s.o} />\n            ))}\n            <circle cx=\"282\" cy=\"182\" r=\"96\" fill={`url(#${uid}g)`} />\n            <circle className=\"hb-qm2-sun\" cx=\"282\" cy=\"182\" r=\"21\" />\n            <rect x=\"52\" y=\"118\" width=\"74\" height=\"2.5\" rx=\"1.25\" fill=\"#fff\" opacity=\"0.14\" />\n            <rect x=\"70\" y=\"127\" width=\"46\" height=\"2.5\" rx=\"1.25\" fill=\"#fff\" opacity=\"0.1\" />\n            <rect x=\"214\" y=\"104\" width=\"62\" height=\"2.5\" rx=\"1.25\" fill=\"#fff\" opacity=\"0.12\" />\n          </svg>\n        </div>\n        <div className=\"hb-qm2-layer hb-qm2-hills\" aria-hidden=\"true\">\n          <svg viewBox=\"0 0 400 300\" preserveAspectRatio=\"xMidYMid slice\">\n            <defs>\n              <linearGradient id={`${uid}h`} gradientUnits=\"userSpaceOnUse\" x1=\"0\" y1=\"170\" x2=\"0\" y2=\"250\">\n                <stop offset=\"0\" stopColor=\"#4b3a86\" />\n                <stop offset=\"1\" stopColor=\"#2a2260\" />\n              </linearGradient>\n            </defs>\n            <path\n              d=\"M0 192C38 168 78 172 118 184S198 148 250 158S342 184 400 164L400 300L0 300Z\"\n              fill={`url(#${uid}h)`}\n            />\n            <path\n              d=\"M0 220C50 198 108 206 160 218S258 192 308 202S380 216 400 206L400 300L0 300Z\"\n              fill=\"#241d55\"\n            />\n          </svg>\n        </div>\n        <div className=\"hb-qm2-layer hb-qm2-fore\" aria-hidden=\"true\">\n          <svg viewBox=\"0 0 400 300\" preserveAspectRatio=\"xMidYMid slice\">\n            <defs>\n              <linearGradient id={`${uid}f`} gradientUnits=\"userSpaceOnUse\" x1=\"0\" y1=\"196\" x2=\"0\" y2=\"300\">\n                <stop offset=\"0\" stopColor=\"#151231\" />\n                <stop offset=\"1\" stopColor=\"#090818\" />\n              </linearGradient>\n            </defs>\n            <path d={pines} fill=\"#100d29\" />\n            <path\n              d=\"M0 204C70 190 140 198 210 192S340 188 400 198L400 300L0 300Z\"\n              fill={`url(#${uid}f)`}\n            />\n          </svg>\n        </div>\n        {children ? <div className=\"hb-qm2-over\">{children}</div> : null}\n      </div>\n      {after}\n    </>\n  );\n\n  return (\n    <div\n      ref={rootRef}\n      className={`hb-qm2 ${className}`.trim()}\n      style={\n        {\n          \"--hb-qm2-a\": accent,\n          \"--hb-qm2-k\": k,\n          \"--hb-qm2-h\": typeof sceneHeight === \"number\" ? `${sceneHeight}px` : sceneHeight,\n          ...style,\n        } as React.CSSProperties\n      }\n      {...rest}\n    >\n      <style href=\"hb-scroll-parallax-layers\" precedence=\"default\">\n        {css}\n      </style>\n      {own ? (\n        <div\n          ref={viewportRef}\n          className=\"hb-qm2-vp\"\n          style={{ height }}\n          tabIndex={0}\n          role=\"region\"\n          aria-label=\"Scrollable content\"\n        >\n          {body}\n        </div>\n      ) : (\n        body\n      )}\n    </div>\n  );\n}\n"
    }
  ]
}