labs

arc reel

2026-08-28

a curve fixed to the viewport

the reel on the home page rides a parabola recomputed every frame, and it used to jump at every wrap because each card's pose came from its index: the pattern travelled with the cards instead of the cards travelling through it. keying on position makes the arc a fixed shape in the viewport and the loop seamless by construction.

the bank angle is the real tangent, atan(dPerp/dAlong), not a decorative sine, so a card turns exactly as much as the curve turns under it.

take it with you

three steps, and the stage above runs in your own project.

  1. install

    nothing to add. it runs on react alone, 18 or newer.

  2. copy

    5 files, kept together in one folder, arc-reel/. the styles travel with them.

    "use client";
    
    import { useCallback, useEffect, useLayoutEffect, useRef } from "react";
    import { arcPose, fitAmp } from "./arc";
    import { createLoop, type LoopHandle } from "./loop";
    import { useReducedMotion } from "./motion";
    import "./arc-reel.css";
    
    interface Props {
      images: string[];
      // share of the curve's slope a card banks by: 1 follows the tangent, 0 keeps cards level
      tilt?: number;
      // degrees a card can bank at the steepest part of the curve
      maxRot?: number;
      // px per second; hover or press to ease it to a stop
      speed?: number;
    }
    
    const GAP = 0.14;
    
    interface Metrics {
      run: number;
      pitch: number;
      total: number;
      cardW: number;
      amp: number;
      base: number;
    }
    
    export default function ArcReel({ images, tilt = 0.45, maxRot = 9, speed = 34 }: Props) {
      const cards = images.length * 2;
      const reduced = useReducedMotion();
      const boxRef = useRef<HTMLDivElement>(null);
      const cardsRef = useRef<(HTMLElement | null)[]>([]);
      const metricsRef = useRef<Metrics | null>(null);
      const loopRef = useRef<LoopHandle | null>(null);
    
      const paramsRef = useRef({ tilt, maxRot, speed });
      paramsRef.current = { tilt, maxRot, speed };
    
      const phaseRef = useRef(0);
    
      const heldRef = useRef(false);
      const hold = (on: boolean) => {
        heldRef.current = on;
        if (on) loopRef.current?.pause();
        else loopRef.current?.resume();
      };
    
      const measure = useCallback((): Metrics | null => {
        const box = boxRef.current;
        const card = cardsRef.current[0];
        if (!box || !card) return null;
    
        const boxW = box.clientWidth;
        const boxH = box.clientHeight;
        const cardW = card.offsetWidth;
        const cardH = card.offsetHeight;
        if (!boxW || !boxH || !cardW || !cardH) return null;
    
        const run = boxW + cardW;
        const slack = Math.max(0, boxH - cardH);
        const amp = fitAmp(cardH * 0.95, slack, { span: run }, cardH, cardW);
        const pitch = Math.max(cardW * (1 + GAP), run / cards);
    
        return { run, pitch, total: pitch * cards, cardW, amp, base: (slack - amp) / 2 };
      }, [cards]);
    
      const place = useCallback((position: number) => {
        const m = metricsRef.current;
        if (!m) return;
        const { run, pitch, total, cardW, amp, base } = m;
        const { tilt, maxRot } = paramsRef.current;
        const els = cardsRef.current;
    
        for (let i = 0; i < els.length; i++) {
          const el = els[i];
          if (!el) continue;
    
          let raw = (i * pitch + position + phaseRef.current) % total;
          if (raw < 0) raw += total;
    
          const pose = arcPose(raw / run, { amp, span: run, tilt, maxRot });
    
          const x = raw - cardW;
          const y = base + (amp - pose.perp);
          el.style.transform =
            `translate3d(${x.toFixed(2)}px, ${y.toFixed(2)}px, 0) ` +
            `rotate(${(-pose.rot).toFixed(3)}deg) scale(${pose.scale.toFixed(4)})`;
        }
      }, []);
    
      useLayoutEffect(() => {
        const box = boxRef.current;
        if (!box) return;
    
        const sync = () => {
          metricsRef.current = measure();
          place(loopRef.current?.position ?? 0);
          if (metricsRef.current) box.dataset.placed = "on";
        };
    
        sync();
        const ro = new ResizeObserver(sync);
        ro.observe(box);
        return () => ro.disconnect();
      }, [measure, place]);
    
      useEffect(() => {
        place(loopRef.current?.position ?? 0);
      }, [place, tilt, maxRot]);
    
      useEffect(() => {
        if (reduced) return;
    
        let loop: LoopHandle | null = null;
        let onScreen = true;
    
        const start = () => {
          if (loop) return;
          loop = createLoop({
            getDistance: () => metricsRef.current?.total ?? 0,
            speed: () => paramsRef.current.speed,
            onFrame: place,
          });
          loopRef.current = loop;
          if (heldRef.current) loop.pause();
          loop.start();
        };
    
        const stop = () => {
          if (!loop) return;
          const total = metricsRef.current?.total ?? 0;
          if (total > 0) phaseRef.current = (phaseRef.current + loop.position) % total;
          loop.kill();
          loop = null;
          loopRef.current = null;
        };
    
        const sync = () => {
          if (onScreen && document.visibilityState === "visible") start();
          else stop();
        };
    
        const io = new IntersectionObserver(([entry]) => {
          onScreen = entry?.isIntersecting ?? false;
          sync();
        });
        if (boxRef.current) io.observe(boxRef.current);
        document.addEventListener("visibilitychange", sync);
        sync();
    
        return () => {
          stop();
          io.disconnect();
          document.removeEventListener("visibilitychange", sync);
        };
      }, [reduced, place]);
    
      return (
        <div
          className="arc-lab"
          ref={boxRef}
          data-still={String(reduced)}
          onPointerEnter={() => hold(true)}
          onPointerLeave={() => hold(false)}
          onTouchStart={() => hold(true)}
          onTouchEnd={() => hold(false)}
        >
          {Array.from({ length: cards }, (_, i) => (
            <figure
              key={i}
              className="arc-lab-card"
              ref={(el) => {
                cardsRef.current[i] = el;
              }}
            >
              <img src={images[i % images.length]} alt="" decoding="async" draggable={false} />
            </figure>
          ))}
        </div>
      );
    }
    
  3. use

    the stage above, exactly as it runs here. the comments mark what to change.

    example.tsx
    "use client";
    
    import ArcReel from "./arc-reel/ArcReel";
    
    // swap these for your own images; the reel doubles them so the loop never runs short
    const IMAGES = ["movievault", "quizzme", "redline", "pasteup", "tenzies", "fandeck"].map(
      (seed) => `https://picsum.photos/seed/${seed}/640/400`,
    );
    
    export default function ArcReelExample({ images = IMAGES }: { images?: string[] }) {
      return <ArcReel images={images} tilt={0.45} maxRot={9} speed={34} />;
    }