{
  "$schema": "https://www.cooki-ui.design/schema/registry-item.json",
  "name": "infinite-drift",
  "type": "registry:ui",
  "title": "Infinite Drift",
  "description": "Kinetic perspective conveyor gallery with parabolic curved bands, inertia gliding, and infinite loop simulation.",
  "category": "animations",
  "dependencies": [
    "clsx",
    "tailwind-merge"
  ],
  "tags": [
    "animations",
    "gallery",
    "canvas",
    "parallax",
    "drift",
    "kinetic",
    "3d"
  ],
  "files": [
    {
      "path": "registry/animations/infinite-drift/infinite-drift.tsx",
      "type": "registry:ui",
      "target": "components/ui/infinite-drift.tsx",
      "content": "\"use client\";\n\nimport React, { useRef, useEffect } from \"react\";\nimport { cn } from \"@/lib/utils\";\n\nexport interface InfiniteDriftBand {\n  images: string[];\n  speed?: number;\n  rotation?: number;\n  offsetY?: number;\n  curveAmount?: number;\n  curveDirection?: 1 | -1;\n  rotationType?: \"fromLeft\" | \"fromCenter\";\n}\n\nexport interface InfiniteDriftProps {\n  bands?: InfiniteDriftBand[];\n  height?: string | number;\n  gap?: number;\n  imageHeight?: number;\n  bandHeight?: number;\n  maxImageWidth?: number;\n  inertia?: number;\n  preserveOriginalRatios?: boolean;\n  className?: string;\n  children?: React.ReactNode;\n}\n\nconst DEFAULT_BANDS: InfiniteDriftBand[] = [\n  {\n    offsetY: -220,\n    speed: 1.0,\n    rotation: 7,\n    rotationType: \"fromLeft\",\n    curveAmount: 40.0,\n    curveDirection: 1,\n    images: [\n      \"https://images.unsplash.com/photo-1550684848-fac1c5b4e853?w=400&auto=format&fit=crop&q=80\",\n      \"https://images.unsplash.com/photo-1550684399-3f0f745771d1?w=400&auto=format&fit=crop&q=80\",\n      \"https://images.unsplash.com/photo-1550684847-75bdda21cc95?w=400&auto=format&fit=crop&q=80\",\n      \"https://images.unsplash.com/photo-1563089145-599997674d42?w=400&auto=format&fit=crop&q=80\",\n    ],\n  },\n  {\n    offsetY: -110,\n    speed: 1.3,\n    rotation: 7,\n    rotationType: \"fromCenter\",\n    curveAmount: 35.0,\n    curveDirection: 1,\n    images: [\n      \"https://images.unsplash.com/photo-1557683316-973673baf926?w=400&auto=format&fit=crop&q=80\",\n      \"https://images.unsplash.com/photo-1557683311-eac922347aa1?w=400&auto=format&fit=crop&q=80\",\n      \"https://images.unsplash.com/photo-1557683325-3ba8f0df79de?w=400&auto=format&fit=crop&q=80\",\n      \"https://images.unsplash.com/photo-1561070791-2526d30994b5?w=400&auto=format&fit=crop&q=80\",\n    ],\n  },\n  {\n    offsetY: 0,\n    speed: 0.7,\n    rotation: 7,\n    curveAmount: 40.0,\n    curveDirection: 1,\n    images: [\n      \"https://images.unsplash.com/photo-1511447333849-2b89ae13b002?w=400&auto=format&fit=crop&q=80\",\n      \"https://images.unsplash.com/photo-1536431311719-398b6704d4cc?w=400&auto=format&fit=crop&q=80\",\n      \"https://images.unsplash.com/photo-1543966888-7c1dc482a810?w=400&auto=format&fit=crop&q=80\",\n      \"https://images.unsplash.com/photo-1561070791-36c11767b26a?w=400&auto=format&fit=crop&q=80\",\n    ],\n  },\n  {\n    offsetY: 110,\n    speed: 1.2,\n    rotation: 7,\n    curveAmount: 40.0,\n    curveDirection: 1,\n    images: [\n      \"https://images.unsplash.com/photo-1493246507139-91e8bef99c02?w=400&auto=format&fit=crop&q=80\",\n      \"https://images.unsplash.com/photo-1477346611705-65d1883cee1e?w=400&auto=format&fit=crop&q=80\",\n      \"https://images.unsplash.com/photo-1501785888041-af3ef285b470?w=400&auto=format&fit=crop&q=80\",\n      \"https://images.unsplash.com/photo-1561070791-0626bcd0516e?w=400&auto=format&fit=crop&q=80\",\n    ],\n  },\n  {\n    offsetY: 220,\n    speed: 0.9,\n    rotation: 7,\n    curveAmount: 35.0,\n    curveDirection: 1,\n    images: [\n      \"https://images.unsplash.com/photo-1534067783941-51c9c23ecefd?w=400&auto=format&fit=crop&q=80\",\n      \"https://images.unsplash.com/photo-1441974231531-c6227db76b6e?w=400&auto=format&fit=crop&q=80\",\n      \"https://images.unsplash.com/photo-1550684848-fac1c5b4e853?w=400&auto=format&fit=crop&q=80\",\n      \"https://images.unsplash.com/photo-1557683316-973673baf926?w=400&auto=format&fit=crop&q=80\",\n    ],\n  },\n];\n\nconst glslMod = (a: number, b: number) => ((a % b) + b) % b;\n\nconst rotate2dPt = (\n  px: number,\n  py: number,\n  cx: number,\n  cy: number,\n  angle: number\n): [number, number] => {\n  const dx = px - cx;\n  const dy = py - cy;\n  const c = Math.cos(angle);\n  const s = Math.sin(angle);\n  return [cx + c * dx - s * dy, cy + s * dx + c * dy];\n};\n\ninterface BandState {\n  config: InfiniteDriftBand;\n  stripCanvas: HTMLCanvasElement;\n  sequenceWidth: number;\n  ready: boolean;\n}\n\nexport const InfiniteDrift: React.FC<InfiniteDriftProps> = ({\n  bands = DEFAULT_BANDS,\n  height = 500,\n  gap = 20,\n  imageHeight = 100,\n  bandHeight = 120,\n  maxImageWidth = 300,\n  inertia = 0.92,\n  preserveOriginalRatios = true,\n  className,\n  children,\n}) => {\n  const containerRef = useRef<HTMLDivElement>(null);\n  const canvasRef = useRef<HTMLCanvasElement>(null);\n  const scrollState = useRef({\n    scrollY: 0,\n    targetScrollY: 0,\n    scrollVelocity: 0,\n    isDragging: false,\n    lastMouseY: 0,\n  });\n\n  useEffect(() => {\n    if (!containerRef.current || !canvasRef.current) return;\n    const container = containerRef.current;\n    const canvas = canvasRef.current;\n    const dpr = Math.min(window.devicePixelRatio || 1, 2);\n\n    const resize = () => {\n      if (!container || !canvas) return;\n      canvas.width = container.clientWidth * dpr;\n      canvas.height = container.clientHeight * dpr;\n      const c = canvas.getContext(\"2d\");\n      if (c) c.scale(dpr, dpr);\n    };\n    resize();\n\n    const ctx = canvas.getContext(\"2d\");\n    if (!ctx) return;\n\n    let isDisposed = false;\n\n    const bandStates: BandState[] = bands.map((config) => ({\n      config,\n      stripCanvas: document.createElement(\"canvas\"),\n      sequenceWidth: 0,\n      ready: false,\n    }));\n\n    const loadBand = async (bs: BandState) => {\n      const { config } = bs;\n      const imgs = await Promise.all(\n        config.images.map(\n          (url) =>\n            new Promise<HTMLImageElement | HTMLCanvasElement>((resolve) => {\n              const img = new Image();\n              img.crossOrigin = \"anonymous\";\n              img.onload = () => resolve(img);\n              img.onerror = () => {\n                const fb = document.createElement(\"canvas\");\n                fb.width = 400;\n                fb.height = 300;\n                const fc = fb.getContext(\"2d\")!;\n                fc.fillStyle = `hsl(${Math.random() * 360}, 60%, 45%)`;\n                fc.fillRect(0, 0, 400, 300);\n                resolve(fb);\n              };\n              img.src = url;\n            })\n        )\n      );\n\n      if (isDisposed) return;\n\n      let seqW = 0;\n      const infos = imgs.map((src) => {\n        const nw =\n          (src as HTMLImageElement).naturalWidth ||\n          (src as HTMLCanvasElement).width ||\n          400;\n        const nh =\n          (src as HTMLImageElement).naturalHeight ||\n          (src as HTMLCanvasElement).height ||\n          300;\n        const ratio = nw / nh || 1.5;\n        let w: number;\n        let h: number;\n        if (preserveOriginalRatios) {\n          h = imageHeight;\n          w = Math.round(h * ratio);\n          if (w > maxImageWidth) {\n            w = maxImageWidth;\n            h = Math.round(w / ratio);\n          }\n        } else {\n          h = imageHeight;\n          w = Math.round(h * 1.5);\n        }\n        seqW += w + gap;\n        return { src, w, h };\n      });\n      seqW -= gap;\n\n      const CLONES = 3;\n      const strip = bs.stripCanvas;\n      strip.width = Math.max(1, seqW * CLONES);\n      strip.height = bandHeight;\n      const sc = strip.getContext(\"2d\");\n      if (!sc) return;\n\n      let cx2 = 0;\n      for (let c = 0; c < CLONES; c++) {\n        for (const info of infos) {\n          sc.drawImage(\n            info.src,\n            cx2,\n            (bandHeight - info.h) / 2,\n            info.w,\n            info.h\n          );\n          cx2 += info.w + gap;\n        }\n      }\n      bs.sequenceWidth = Math.max(1, seqW);\n      bs.ready = true;\n    };\n\n    void Promise.all(bandStates.map(loadBand));\n\n    const drawBand = (bs: BandState, W: number, H: number, scrollY: number) => {\n      if (!bs.ready || bs.sequenceWidth <= 0) return;\n      const { config, stripCanvas, sequenceWidth } = bs;\n\n      const rotRad = ((config.rotation || 0) * Math.PI) / 180;\n      const hasRot = Math.abs(rotRad) > 0.0001;\n      const curveAmount = config.curveAmount ?? 0;\n      const curveDir = config.curveDirection ?? 1;\n      const speed = config.speed ?? 1.0;\n      const offsetY = config.offsetY ?? 0;\n      const fromLeft = config.rotationType === \"fromLeft\";\n\n      const bandTopBase = (H - bandHeight) * 0.5 + offsetY;\n      const bandCenterY = bandTopBase + bandHeight * 0.5;\n      const pivotX = fromLeft ? 0 : W * 0.5;\n      const pivotY = bandCenterY;\n      const scrollPos = scrollY * speed;\n\n      ctx.save();\n\n      ctx.beginPath();\n      if (hasRot) {\n        const pad = curveAmount + 6;\n        const corners: [number, number][] = [\n          [0, bandTopBase - pad],\n          [W, bandTopBase - pad],\n          [W, bandTopBase + bandHeight + pad],\n          [0, bandTopBase + bandHeight + pad],\n        ];\n        corners.forEach(([cx_, cy_], i) => {\n          const [rx, ry] = rotate2dPt(cx_, cy_, pivotX, pivotY, -rotRad);\n          if (i === 0) ctx.moveTo(rx, ry);\n          else ctx.lineTo(rx, ry);\n        });\n        ctx.closePath();\n      } else {\n        const pad = curveAmount + 6;\n        ctx.rect(0, bandTopBase - pad, W, bandHeight + pad * 2);\n      }\n      ctx.clip();\n\n      const step = 2;\n      for (let x = 0; x < W; x += step) {\n        const nx = x / W;\n        const curveFactor = 4.0 * (nx - 0.5) * (nx - 0.5);\n        const curveOffset = (0.5 - curveFactor) * curveAmount * curveDir;\n        const bandTop = bandTopBase + curveOffset;\n\n        let srcX = x;\n        if (hasRot) {\n          const [rx] = rotate2dPt(x, bandCenterY, pivotX, pivotY, rotRad);\n          srcX = rx;\n        }\n\n        const wrappedX = glslMod(srcX + scrollPos, sequenceWidth);\n        const texSrcX = wrappedX + sequenceWidth;\n\n        if (texSrcX < 0 || texSrcX >= stripCanvas.width) continue;\n\n        ctx.drawImage(\n          stripCanvas,\n          texSrcX,\n          0,\n          step,\n          bandHeight,\n          x,\n          bandTop,\n          step,\n          bandHeight\n        );\n      }\n\n      ctx.restore();\n    };\n\n    let animId: number;\n    const animate = () => {\n      animId = requestAnimationFrame(animate);\n      if (!container || !canvas) return;\n      const W = container.clientWidth;\n      const H = container.clientHeight;\n      const state = scrollState.current;\n\n      if (!state.isDragging) {\n        state.targetScrollY += state.scrollVelocity;\n        state.scrollVelocity *= inertia;\n        if (Math.abs(state.scrollVelocity) < 0.1) state.scrollVelocity = 0;\n      }\n      state.scrollY += (state.targetScrollY - state.scrollY) * 0.1;\n\n      ctx.clearRect(0, 0, W, H);\n      for (const bs of bandStates) {\n        drawBand(bs, W, H, state.scrollY);\n      }\n    };\n    animate();\n\n    const handleWheel = (e: WheelEvent) => {\n      e.preventDefault();\n      scrollState.current.targetScrollY += e.deltaY;\n      scrollState.current.scrollVelocity = e.deltaY * 0.15;\n    };\n\n    const handleMouseDown = (e: MouseEvent) => {\n      scrollState.current.isDragging = true;\n      scrollState.current.lastMouseY = e.clientY;\n      scrollState.current.scrollVelocity = 0;\n    };\n\n    const handleMouseMove = (e: MouseEvent) => {\n      if (!scrollState.current.isDragging) return;\n      const d = e.clientY - scrollState.current.lastMouseY;\n      scrollState.current.targetScrollY += d * 2.0;\n      scrollState.current.lastMouseY = e.clientY;\n      scrollState.current.scrollVelocity = d * 0.25;\n    };\n\n    const handleMouseUp = () => {\n      scrollState.current.isDragging = false;\n    };\n\n    const handleResize = () => {\n      resize();\n    };\n\n    const handleTouchStart = (e: TouchEvent) => {\n      if (e.touches.length > 0) {\n        scrollState.current.lastMouseY = e.touches[0].clientY;\n        scrollState.current.isDragging = true;\n      }\n    };\n\n    const handleTouchMove = (e: TouchEvent) => {\n      if (!scrollState.current.isDragging || e.touches.length === 0) return;\n      const d = e.touches[0].clientY - scrollState.current.lastMouseY;\n      scrollState.current.targetScrollY += d * 2.5;\n      scrollState.current.lastMouseY = e.touches[0].clientY;\n      scrollState.current.scrollVelocity = d * 0.3;\n    };\n\n    container.addEventListener(\"wheel\", handleWheel, { passive: false });\n    container.addEventListener(\"mousedown\", handleMouseDown);\n    window.addEventListener(\"mousemove\", handleMouseMove);\n    window.addEventListener(\"mouseup\", handleMouseUp);\n    window.addEventListener(\"resize\", handleResize);\n    container.addEventListener(\"touchstart\", handleTouchStart, {\n      passive: true,\n    });\n    container.addEventListener(\"touchmove\", handleTouchMove, {\n      passive: true,\n    });\n    container.addEventListener(\"touchend\", handleMouseUp);\n\n    return () => {\n      isDisposed = true;\n      cancelAnimationFrame(animId);\n      container.removeEventListener(\"wheel\", handleWheel);\n      container.removeEventListener(\"mousedown\", handleMouseDown);\n      window.removeEventListener(\"mousemove\", handleMouseMove);\n      window.removeEventListener(\"mouseup\", handleMouseUp);\n      window.removeEventListener(\"resize\", handleResize);\n      container.removeEventListener(\"touchstart\", handleTouchStart);\n      container.removeEventListener(\"touchmove\", handleTouchMove);\n      container.removeEventListener(\"touchend\", handleMouseUp);\n    };\n  }, [\n    bands,\n    gap,\n    imageHeight,\n    bandHeight,\n    maxImageWidth,\n    inertia,\n    preserveOriginalRatios,\n  ]);\n\n  return (\n    <div\n      ref={containerRef}\n      className={cn(\n        \"relative w-full overflow-hidden bg-zinc-950/40 rounded-2xl cursor-grab active:cursor-grabbing border border-border/40 select-none\",\n        className\n      )}\n      style={{ height }}\n    >\n      <canvas ref={canvasRef} className=\"absolute inset-0 h-full w-full\" />\n      {children}\n    </div>\n  );\n};\n\nexport default InfiniteDrift;\n"
    }
  ]
}