Back to the library

Transit — duration by distance

Variant: Distance-scaled

Three tokens leave together on moves of a quarter, a half and the full width, and take 200ms, 283ms and 400ms: duration proportional to the square root of distance. The two obvious rules are both wrong. A fixed duration makes the long move four times faster than the short one, fast enough to lose. A fixed speed makes it four times longer, 800ms of waiting where 200ms was right. The square root sits between them: twice the time and twice the speed for four times the distance. The rows land in order of distance, and that staggered arrival is the demo. 400ms is over the usual 300ms ceiling for UI motion on purpose. That ceiling is for responses near the cursor, and a move across the whole layout needs the extra time to stay trackable. Copy the rule, not the numbers: pick the time for your longest common move and scale every other move by √(distance ÷ that distance). Under reduced motion each row crossfades over the time its hop would have taken, so they still finish in distance order.

transitionspatialeasinglayoutloop

The source

A complete HTML document. Paste it into an empty .html file and it plays — no stylesheet, script, font, or image to fetch.

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Transit — duration by distance</title>
    <style>
      :root {
        /* Neutrals — fixed, never remixed. */
        --ivory: #faf9f5;
        --paper: #ffffff;
        --oat: #e3dacc;
        --slate: #141413;

        /* Knobs — the remix API. A knob exists iff declared here and used below. */
        --hue: 38.8;
        --lift: 0;
        --chroma: 1;
        --speed: 1;

        /* Accents — derived from the knobs with the family's offsets baked in,
           so one hue roll moves them together. Defaults reproduce the original hex. */
        --clay: oklch(calc(0.6724 + var(--lift)) calc(0.1308 * var(--chroma)) var(--hue));
        --clay-d: oklch(calc(0.5797 + var(--lift)) calc(0.127 * var(--chroma)) var(--hue));
        --olive: oklch(
          calc(0.6118 + var(--lift)) calc(0.0713 * var(--chroma)) calc(var(--hue) + 88.3)
        );
        --rust: oklch(
          calc(0.5408 + var(--lift)) calc(0.1357 * var(--chroma)) calc(var(--hue) - 10.3)
        );

        /* Clock — 3.2s. All three leave at 15% and come back at 65%. The full
           run takes 400ms (12.5%); a half run 283ms (8.84%); a quarter 200ms
           (6.25%). Duration = 400ms × √(distance ÷ full run). */
        --cycle: 3.2s;
        --t: calc(var(--cycle) / var(--speed));

        --ease-in-out: cubic-bezier(0.77, 0, 0.175, 1);

        --_d: 20px;
      }

      * {
        box-sizing: border-box;
        margin: 0;
        padding: 0;
      }

      body {
        min-height: 100vh;
        display: grid;
        place-items: center;
        background: var(--ivory);
        font-family:
          system-ui,
          -apple-system,
          'Segoe UI',
          Roboto,
          sans-serif;
        color: var(--slate);
      }

      .stage {
        display: grid;
        gap: clamp(12px, 4vh, 18px);
        width: min(300px, 84vw);
      }

      .row {
        display: grid;
        grid-template-columns: 3.2em 1fr;
        align-items: center;
        gap: 10px;
        font-size: 11.5px;
      }

      .ms {
        text-align: right;
        font-variant-numeric: tabular-nums;
        color: color-mix(in srgb, var(--slate) 60%, transparent);
      }

      .track {
        position: relative;
        overflow: clip;
        /* The carriers are track-sized, so in flight their empty boxes run
           past the edge; clipping here keeps the page from scrolling. The
           margin leaves room for the token's shadow. */
        overflow-clip-margin: 4px;
        height: var(--_d);
      }

      .socket {
        position: absolute;
        top: 0;
        width: var(--_d);
        height: var(--_d);
        border-radius: 50%;
        border: 1.5px dashed color-mix(in srgb, var(--slate) 22%, transparent);
      }

      /* Every row starts in the same place; the far socket sits at its row's
         fraction of the run. One expression places both the socket and the
         token's destination, so they cannot disagree at any stage width. */
      .socket.b {
        left: calc((100% - var(--_d)) * var(--_f));
      }

      .x {
        position: absolute;
        inset: 0;
      }

      .token {
        position: absolute;
        left: 0;
        top: 0;
        width: var(--_d);
        height: var(--_d);
        border-radius: 50%;
        background: var(--clay);
      }

      .row.quarter {
        --_f: 0.25;
      }

      .row.half {
        --_f: 0.5;
      }

      .row.full {
        --_f: 1;
      }

      .quarter .x {
        animation: hop-quarter var(--t) var(--ease-in-out) infinite;
      }

      .half .x {
        animation: hop-half var(--t) var(--ease-in-out) infinite;
      }

      .full .x {
        animation: hop-full var(--t) var(--ease-in-out) infinite;
      }

      .cap {
        padding-left: calc(3.2em + 10px);
        font-size: 11.5px;
        letter-spacing: 0.02em;
        color: color-mix(in srgb, var(--slate) 55%, transparent);
      }

      /* A fixed duration makes the long move four times faster than the
         short one, and fast enough to lose; a fixed speed makes it four times
         longer, slow enough to wait on. The square root sits between them: the
         long move takes twice as long and travels at twice the speed. Gravity
         uses the same law, which may be why it reads as natural. */
      @keyframes hop-quarter {
        0%,
        15% {
          transform: translateX(0);
        }
        21.25%,
        65% {
          transform: translateX(calc((100% - var(--_d)) * 0.25));
        }
        71.25%,
        100% {
          transform: translateX(0);
        }
      }

      @keyframes hop-half {
        0%,
        15% {
          transform: translateX(0);
        }
        23.84%,
        65% {
          transform: translateX(calc((100% - var(--_d)) * 0.5));
        }
        73.84%,
        100% {
          transform: translateX(0);
        }
      }

      @keyframes hop-full {
        0%,
        15% {
          transform: translateX(0);
        }
        27.5%,
        65% {
          transform: translateX(calc(100% - var(--_d)));
        }
        77.5%,
        100% {
          transform: translateX(0);
        }
      }

      .token.ghost {
        display: none;
      }

      @media (prefers-reduced-motion: reduce) {
        /* The rows still finish in order of distance — each crossfade lasts
           as long as its hop would have — so the law stays visible without
           anything moving. */
        .row .x {
          animation: none;
        }

        .token.ghost {
          display: block;
          left: calc((100% - var(--_d)) * var(--_f));
        }

        .quarter .token {
          animation: fade-quarter calc(var(--t) * 1.6) ease infinite;
        }

        .half .token {
          animation: fade-half calc(var(--t) * 1.6) ease infinite;
        }

        .full .token {
          animation: fade-full calc(var(--t) * 1.6) ease infinite;
        }

        .quarter .token.ghost {
          animation-name: land-quarter;
        }

        .half .token.ghost {
          animation-name: land-half;
        }

        .full .token.ghost {
          animation-name: land-full;
        }

        @keyframes fade-quarter {
          0%,
          15% {
            opacity: 1;
          }
          21.25%,
          65% {
            opacity: 0;
          }
          71.25%,
          100% {
            opacity: 1;
          }
        }

        @keyframes fade-half {
          0%,
          15% {
            opacity: 1;
          }
          23.84%,
          65% {
            opacity: 0;
          }
          73.84%,
          100% {
            opacity: 1;
          }
        }

        @keyframes fade-full {
          0%,
          15% {
            opacity: 1;
          }
          27.5%,
          65% {
            opacity: 0;
          }
          77.5%,
          100% {
            opacity: 1;
          }
        }
        @keyframes land-quarter {
          0%,
          15% {
            opacity: 0;
          }
          21.25%,
          65% {
            opacity: 1;
          }
          71.25%,
          100% {
            opacity: 0;
          }
        }

        @keyframes land-half {
          0%,
          15% {
            opacity: 0;
          }
          23.84%,
          65% {
            opacity: 1;
          }
          73.84%,
          100% {
            opacity: 0;
          }
        }

        @keyframes land-full {
          0%,
          15% {
            opacity: 0;
          }
          27.5%,
          65% {
            opacity: 1;
          }
          77.5%,
          100% {
            opacity: 0;
          }
        }
      }
    </style>
  </head>
  <body>
    <div
      class="stage"
      role="img"
      aria-label="Three dots leave together on moves of a quarter, half and the full width; the short move lands first, and the full move takes only twice as long as the quarter"
    >
      <div class="row quarter">
        <span class="ms">200ms</span>
        <div class="track">
          <span class="socket a"></span>
          <span class="socket b"></span>
          <div class="x"><span class="token"></span></div>
          <span class="token ghost"></span>
        </div>
      </div>
      <div class="row half">
        <span class="ms">283ms</span>
        <div class="track">
          <span class="socket a"></span>
          <span class="socket b"></span>
          <div class="x"><span class="token"></span></div>
          <span class="token ghost"></span>
        </div>
      </div>
      <div class="row full">
        <span class="ms">400ms</span>
        <div class="track">
          <span class="socket a"></span>
          <span class="socket b"></span>
          <div class="x"><span class="token"></span></div>
          <span class="token ghost"></span>
        </div>
      </div>
      <p class="cap">duration ∝ √distance</p>
    </div>
  </body>
</html>

The variant family

Same idea, one axis moved. Compare them side by side before you commit.