Back to the library

Snap guide — edge, with hysteresis

Variant: Edge

A card dragged toward a neighbour’s left edge catches inside 6px and does not let go until 12px. The gap between those two numbers is the piece: a snap that releases where it catches flickers on and off under a trembling hand, and the guide strobes with it. So once caught the card stops dead while the pointer overshoots to -6 and pulls back, and the two things that explain why are drawn only while it matters — a dashed ghost where the pointer would put the card, and a pale band showing how far it may wander. Every approach reaches the threshold still moving, so the 78ms snap continues the drag rather than firing after a stop, and on the break the card chases a pointer that never pauses for it. The guide arrives on the snap’s first frame and leaves on the break’s, never faded, since a guide that fades in reports the alignment after it happened; ticks on it name the four corners that agree. Reach for it in any canvas, layout or diagram editor with magnetic alignment, and steal the two-threshold rule even where you do not draw the band.

gesturelayoutdiagramloop

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>Snap guide — edge, with hysteresis</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;
        --round: 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 — 6s: drag in, catch at 6px, overshoot inside the band,
           pull out through 12px, come back and drop. */
        --cycle: 6s;
        --t: calc(var(--cycle) / var(--speed));

        --ease-out: cubic-bezier(0.23, 1, 0.32, 1);
        --ease-in-out: cubic-bezier(0.77, 0, 0.175, 1);
        /* The snap. 78ms, almost all of it in the first third: the object is
           answering the hand, so it must arrive before the eye asks why. */
        --ease-accent: cubic-bezier(0.32, 0.72, 0, 1);
        /* Approach: leaves rest, but arrives at the threshold still moving
           (end slope 1), so the snap continues the hand's motion instead of
           firing after a dead stop. */
        --_approach: cubic-bezier(0.55, 0, 0.75, 0.75);
        /* Coast: takes the 30px/s the break leaves with (start slope ~0.73)
           and settles. */
        --_coast: cubic-bezier(0.3, 0.22, 0.4, 1);

        --_muted: color-mix(in srgb, var(--slate) 52%, transparent);
        --_bar: color-mix(in srgb, var(--oat) 80%, transparent);
        --_ghost: color-mix(in srgb, var(--slate) 38%, transparent);
        --_band: color-mix(in srgb, var(--clay) 13%, transparent);
      }

      * {
        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);
      }

      .rig {
        display: grid;
        gap: 12px;
        justify-items: center;
        animation: rig var(--t) linear infinite;
      }

      .cap,
      .foot {
        font-size: 11px;
        letter-spacing: 0.03em;
        color: var(--_muted);
      }

      .stage {
        position: relative;
        width: 260px;
        height: 150px;
        background: var(--paper);
        border: 1px solid var(--oat);
        border-radius: calc(10px * var(--round));
        overflow: hidden;
      }

      .obj {
        position: absolute;
        height: 40px;
        padding: 10px;
        display: grid;
        align-content: center;
        gap: 6px;
        background: var(--paper);
        border-radius: calc(5px * var(--round));
      }

      .obj i {
        display: block;
        height: 5px;
        border-radius: 999px;
        background: var(--_bar);
      }

      .obj i + i {
        width: 55%;
      }

      .a {
        left: 40px;
        top: 18px;
        width: 112px;
        border: 1px solid var(--oat);
      }

      /* The selected object. Its resting place is the aligned one, so the
         keyframes speak in offsets from the guide: 0 means aligned. */
      .b {
        left: 40px;
        top: 92px;
        width: 76px;
        border: 1.5px solid var(--slate);
        animation: card var(--t) infinite;
      }

      /* The release band: ±12px around the guide. Drawn only while caught,
         because it is the answer to "how far can I wander". */
      .band {
        position: absolute;
        left: 28px;
        top: 10px;
        width: 24px;
        height: 130px;
        background: var(--_band);
        opacity: 0;
        animation: guide var(--t) linear infinite;
      }

      .guide {
        position: absolute;
        left: 39.5px;
        top: 10px;
        width: 1px;
        height: 130px;
        background: var(--clay);
        opacity: 0;
        animation: guide var(--t) linear infinite;
      }

      /* Ticks at the four corners the guide is reporting on — a guide that
         does not say which edges agree is just a line. */
      .guide b {
        position: absolute;
        left: -2.5px;
        width: 6px;
        height: 1px;
        background: var(--clay);
      }
      .guide .k1 {
        top: 8px;
      }
      .guide .k2 {
        top: 48px;
      }
      .guide .k3 {
        top: 82px;
      }
      .guide .k4 {
        top: 122px;
      }

      .hand {
        position: absolute;
        inset: 0;
        animation: hand var(--t) infinite;
      }

      /* Where the pointer would put the object if nothing were snapping.
         Shown only while the two disagree. */
      .ghost {
        position: absolute;
        left: 40px;
        top: 92px;
        width: 76px;
        height: 40px;
        border: 1px dashed var(--_ghost);
        border-radius: calc(5px * var(--round));
        opacity: 0;
        animation: ghost var(--t) linear infinite;
      }

      .ptr {
        position: absolute;
        left: 77px;
        top: 111px;
        width: 14px;
        height: 18px;
        opacity: 0;
        animation: ptr var(--t) linear infinite;
      }

      .arrow {
        display: block;
        width: 100%;
        height: 100%;
        fill: var(--slate);
        stroke: var(--paper);
        stroke-width: 1.4;
        stroke-linejoin: round;
      }

      .tip {
        position: absolute;
        left: -9px;
        top: -9px;
        width: 20px;
        height: 20px;
        border-radius: 50%;
        background: var(--clay-d);
        opacity: 0;
        transform: scale(0.5);
        animation: grip var(--t) infinite;
      }

      /* The pointer's own path. Every approach reaches the threshold with
         speed still on it. Between 24% and 44% it overshoots to -6 and pulls
         back out, and the object does not move at all; it then crosses 12
         at a steady 30px/s rather than pausing for the card. */
      @keyframes hand {
        0%,
        5% {
          transform: translate(120px, 0);
          animation-timing-function: var(--_approach);
        }
        24% {
          transform: translate(6px, 0);
          animation-timing-function: var(--ease-out);
        }
        34% {
          transform: translate(-6px, 0);
          animation-timing-function: var(--_approach);
        }
        44% {
          transform: translate(12px, 0);
          animation-timing-function: linear;
        }
        47.3% {
          transform: translate(18px, 0);
          animation-timing-function: var(--_coast);
        }
        58%,
        62% {
          transform: translate(44px, 0);
          animation-timing-function: var(--_approach);
        }
        76% {
          transform: translate(6px, 0);
          animation-timing-function: var(--ease-out);
        }
        80% {
          transform: translate(2px, 0);
          animation-timing-function: var(--ease-out);
        }
        88%,
        96% {
          transform: translate(24px, 18px);
        }
        96.1%,
        100% {
          transform: translate(120px, 0);
        }
      }

      /* The object rides the hand's keyframes on the hand's curves wherever
         it is free, so the two are pixel-identical; it only diverges inside
         the band. Catch at 6px, release at 12px — the gap between those two
         numbers is what stops a guide flickering under a trembling hand. On
         the break the card chases the moving pointer and meets it at 45.3%,
         where the hand's linear segment puts it at 14.4. */
      @keyframes card {
        0%,
        5% {
          transform: translateX(120px);
          animation-timing-function: var(--_approach);
        }
        24% {
          transform: translateX(6px);
          animation-timing-function: var(--ease-accent);
        }
        25.3%,
        44% {
          transform: translateX(0);
          animation-timing-function: var(--ease-accent);
        }
        45.3% {
          transform: translateX(14.4px);
          animation-timing-function: linear;
        }
        47.3% {
          transform: translateX(18px);
          animation-timing-function: var(--_coast);
        }
        58%,
        62% {
          transform: translateX(44px);
          animation-timing-function: var(--_approach);
        }
        76% {
          transform: translateX(6px);
          animation-timing-function: var(--ease-accent);
        }
        77.3%,
        96% {
          transform: translateX(0);
        }
        96.1%,
        100% {
          transform: translateX(120px);
        }
      }

      /* Guides arrive on the snap's first frame and leave on the break's —
         a guide that fades in reports the alignment after it happened. The
         last one goes on mouse-up, as guides do. */
      @keyframes guide {
        0%,
        24% {
          opacity: 0;
        }
        24.4%,
        44% {
          opacity: 1;
        }
        44.6%,
        76% {
          opacity: 0;
        }
        76.4%,
        80% {
          opacity: 1;
        }
        81%,
        100% {
          opacity: 0;
        }
      }

      @keyframes ghost {
        0%,
        24% {
          opacity: 0;
        }
        25.5%,
        44% {
          opacity: 1;
        }
        45.3%,
        76% {
          opacity: 0;
        }
        77%,
        80% {
          opacity: 1;
        }
        81%,
        100% {
          opacity: 0;
        }
      }

      @keyframes ptr {
        0% {
          opacity: 0;
        }
        4%,
        80% {
          opacity: 1;
        }
        88%,
        100% {
          opacity: 0;
        }
      }

      @keyframes grip {
        0%,
        4.5% {
          opacity: 0;
          transform: scale(0.5);
          animation-timing-function: var(--ease-out);
        }
        5%,
        79.5% {
          opacity: 0.4;
          transform: scale(1);
          animation-timing-function: var(--ease-out);
        }
        80.5%,
        100% {
          opacity: 0;
          transform: scale(0.5);
        }
      }

      @keyframes rig {
        0% {
          opacity: 0;
        }
        3%,
        90% {
          opacity: 1;
        }
        95%,
        100% {
          opacity: 0;
        }
      }

      @media (prefers-reduced-motion: reduce) {
        /* Nothing travels. The object sits aligned and the guide and its
           band breathe, so the card still says what is being reported. */
        .rig,
        .b,
        .hand {
          animation: none;
        }

        .hand {
          opacity: 0;
        }

        .band,
        .guide {
          animation: guide-rest calc(var(--t) * 1.5) ease-in-out infinite;
        }

        @keyframes guide-rest {
          0%,
          100% {
            opacity: 0.35;
          }
          50% {
            opacity: 1;
          }
        }
      }
    </style>
  </head>
  <body>
    <div
      class="rig"
      role="img"
      aria-label="A card is dragged toward another card's left edge. Within six pixels it snaps into alignment and a guide line appears; the pointer wanders inside a twelve-pixel band without the card moving, breaks free past twelve, then returns and drops the card aligned."
    >
      <p class="cap">drag · catch · break away</p>
      <div class="stage">
        <div class="band"></div>
        <div class="obj a"><i></i><i></i></div>
        <div class="obj b"><i></i><i></i></div>
        <div class="guide"><b class="k1"></b><b class="k2"></b><b class="k3"></b><b class="k4"></b></div>
        <div class="hand">
          <div class="ghost"></div>
          <span class="ptr">
            <span class="tip"></span>
            <svg class="arrow" viewBox="0 0 14 18">
              <path d="M1 1 L1 14.8 L4.5 11.6 L6.7 16.6 L9.3 15.4 L7.1 10.5 L11.8 10.3 Z" />
            </svg>
          </span>
        </div>
      </div>
      <p class="foot">catches inside 6px · lets go past 12px</p>
    </div>
  </body>
</html>

The variant family

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