Skip to content

176 web animation techniques, each a live demo with plain settings and a ready-made prompt for your AI assistant. No dependencies, no build step.

27.2 KB
Raw Blame
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Animated Gradient Border — Animation Handbook</title>
  <meta name="description" content="A band of colors runs around the edge of a card or button. Best for featured offers.">
  <link rel="canonical" href="https://matinmonshizadeh.github.io/animation-handbook/animations/04-micro-interactions/gradient-border/">
  <link rel="icon" type="image/png" sizes="96x96" href="../../../favicon.png">
  <link rel="icon" type="image/svg+xml" href="../../../favicon.svg">
  <link rel="apple-touch-icon" href="../../../apple-touch-icon.png">
  <meta property="og:type" content="article">
  <meta property="og:site_name" content="Animation Handbook">
  <meta property="og:title" content="Animated Gradient Border — Animation Handbook">
  <meta property="og:description" content="A band of colors runs around the edge of a card or button. Best for featured offers.">
  <meta property="og:url" content="https://matinmonshizadeh.github.io/animation-handbook/animations/04-micro-interactions/gradient-border/">
  <meta property="og:image" content="https://matinmonshizadeh.github.io/animation-handbook/og-image.png">
  <meta name="twitter:card" content="summary_large_image">
  <meta name="twitter:title" content="Animated Gradient Border — Animation Handbook">
  <meta name="twitter:description" content="A band of colors runs around the edge of a card or button. Best for featured offers.">
  <meta name="twitter:image" content="https://matinmonshizadeh.github.io/animation-handbook/og-image.png">
  <script type="application/ld+json">{"@context": "https://schema.org", "@type": "TechArticle", "headline": "Animated Gradient Border", "name": "Animated Gradient Border", "description": "A band of colors runs around the edge of a card or button. Best for featured offers.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/04-micro-interactions/gradient-border/", "inLanguage": "en", "articleSection": "Micro-Interactions", "isPartOf": {"@type": "WebSite", "name": "Animation Handbook", "url": "https://matinmonshizadeh.github.io/animation-handbook/"}, "author": {"@type": "Person", "name": "matinmonshizadeh"}, "keywords": "web animation, CSS animation, Micro-Interactions, Animated Gradient Border"}</script>
  <style>
    :root{--ui-accent:#ff9d5c;--dur:4s;--bw:2px;--c1:#ff6f8b;--c2:#ffce5a;--c3:#5fd88a;--c4:#6ea8ff}
    *,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
    /* --band is the color stops of the turning layer: a band that fades in behind its bright front, or colors all around */
    .stage{position:relative;display:flex;align-items:center;justify-content:center;overflow:hidden;background:#0b0b0d;
      --band:transparent 0 40%,var(--c1) 58%,var(--c2) 72%,var(--c3) 85%,var(--c4) 96%,transparent}
    .stage.b-short{--band:transparent 0 68%,var(--c1) 78%,var(--c2) 85%,var(--c3) 91%,var(--c4) 97%,transparent}
    .stage.b-full{--band:var(--c1),var(--c2),var(--c3),var(--c4),var(--c1)}
    .scene{display:flex;align-items:center;justify-content:center;gap:clamp(28px,6vw,72px);width:100%;padding:clamp(16px,3vw,32px)}
    /* Each bordered element: a turning layer clipped to its rounded box (the ring), a blurred copy behind it (the glow),
       and an inner box that covers all but the edge */
    .gb{position:relative;isolation:isolate;display:block;border-radius:18px;color:#f4f4f2}
    .gb-ring,.gb-glow{position:absolute;inset:0;border-radius:inherit;overflow:hidden;pointer-events:none}
    .gb-ring{background:rgba(255,255,255,.12)}
    .gb-glow{z-index:-1;filter:blur(16px);opacity:.75}
    /* Hidden, not removed, so its turn keeps step with the ring's */
    .stage.no-glow .gb-glow{visibility:hidden}
    /* The turning layer: a square twice as wide as the element, so it covers every corner at every angle */
    .gb i{position:absolute;left:50%;top:50%;width:200%;aspect-ratio:1;background:conic-gradient(var(--band));
      transform:translate(-50%,-50%);animation:gb-turn var(--dur) linear infinite;will-change:transform}
    .gb-btn i{animation-delay:calc(var(--dur)*-.4)}
    @keyframes gb-turn{to{transform:translate(-50%,-50%) rotate(1turn)}}
    .gb-in{position:relative;display:block;margin:var(--bw);border-radius:calc(18px - var(--bw));background:#121216}
    .gb-card{flex:0 1 280px;min-width:0}
    .card-in{padding:clamp(16px,2.4vw,24px) clamp(18px,2.6vw,28px)}
    .eyebrow{font-size:12px;font-weight:700;letter-spacing:.08em;text-transform:uppercase;color:#ffce5a}
    .plan{margin-top:6px;font-size:clamp(24px,3vw,30px);font-weight:800;letter-spacing:-.02em}
    .price{margin-top:2px;font-size:14px;color:#adadb2}
    .price b{font-size:18px;color:#f4f4f2}
    .feats{margin-top:12px;list-style:none;display:flex;flex-direction:column;gap:6px;font-size:13px;color:#d6d6da}
    .feats li::before{content:"✓";margin-right:8px;color:#5fd88a}
    .gb-btn{flex:none;border:0;padding:0;background:none;font:inherit;cursor:pointer;border-radius:999px}
    .btn-in{display:flex;align-items:center;min-height:52px;padding:0 clamp(22px,2.8vw,32px);border-radius:999px;font-size:clamp(15px,1.6vw,17px);font-weight:700}
    .gb-btn:active .btn-in{background:#1a1a20}
    @media(max-width:600px){
      .scene{flex-direction:column;gap:22px}
      .gb-card{flex:none;width:min(260px,100%)}
      .feats li+li{display:none}
    }
    /* Blur costs the most on phones, and their stage is smaller, so less blur looks as soft */
    @media(max-width:600px),(max-height:500px){.gb-glow{filter:blur(10px)}}
    /* See inside and Compare. Their parts are made once, at load, and kept drawn but unseen, so the faint square and its dashed
       outline turn in step with the colors from the start (same turn, same delay) and Pause and Slow motion reach them. */
    .gb-sq,.gb-sqline{position:absolute;left:50%;top:50%;width:200%;aspect-ratio:1;z-index:-2;opacity:0;pointer-events:none;
      transform:translate(-50%,-50%);animation:gb-turn var(--dur) linear infinite;animation-delay:calc(var(--dur)*-.4);will-change:transform}
    .gb-sq{background:conic-gradient(var(--band))}
    .gb-sqline{border:2px dashed #f4f4f2}
    .sq-lbls,.sc-plain,.sc-name{display:none}
    .cap{width:fit-content;max-width:100%;padding:4px 9px;border-radius:6px;background:rgba(8,8,10,.92);color:#f4f4f2;font:600 13px/1.25 var(--hb-font)}
    /* See inside: the card steps aside (hidden, never removed, so its turn goes on); the button and its square are drawn smaller
       together by one factor (--k), so the square stays whole as it turns */
    .stage[data-hb-mode="inside"] .gb-card{position:absolute;visibility:hidden}
    .stage[data-hb-mode="inside"] .gb-btn{transform:scale(var(--k,1))}
    .stage[data-hb-mode="inside"] .gb-btn::after{content:"";position:absolute;left:0;right:0;top:50%;height:max(100%,calc(44px/var(--k,1)));transform:translateY(-50%)}
    .stage[data-hb-mode="inside"] .gb-sq{opacity:.28}
    .stage[data-hb-mode="inside"] .gb-sqline{opacity:.85}
    .stage[data-hb-mode="inside"] .gb-btn .gb-in{background:rgba(18,18,22,.78);outline:2px dashed #f4f4f2;outline-offset:-2px}
    .stage[data-hb-mode="inside"] .gb-btn:active .gb-in{background:rgba(26,26,32,.78)}
    .stage[data-hb-mode="inside"] .sq-lbls{display:grid;position:absolute;inset:0;padding:12px;pointer-events:none;grid-template-columns:var(--sqw) 1fr var(--sqw)}
    .stage[data-hb-mode="inside"].no-glow .sq-glow{display:none}
    .sq-lead{position:absolute;inset:0;width:100%;height:100%;overflow:visible}
    .sq-lead line{stroke:rgba(244,244,242,.55);stroke-width:1.25}
    .sq-lead circle{fill:#f4f4f2}
    .sq-col{display:flex;flex-direction:column;justify-content:center;gap:10px;grid-row:1}
    .sq-l{grid-column:1;align-items:flex-end}
    .sq-r{grid-column:3;align-items:flex-start}
    .stage[data-hb-mode="inside"] .sq-lbls.corners{grid-template-columns:1fr 1fr;grid-template-rows:auto 1fr auto;column-gap:6px;padding:6px}
    .sq-lbls.corners .sq-col{display:contents}
    .corners .cap{font:600 12px/14px var(--hb-font);padding:3px 6px}
    .corners .sq-glow{display:none}
    .corners .sq-1{grid-area:1/1}
    .corners .sq-2{grid-area:1/2;justify-self:end}
    .corners .sq-3{grid-area:3/1;align-self:end}
    .corners .sq-4{grid-area:3/2;justify-self:end;align-self:end}
    /* Compare: the scene twice, the plain one first; each half has its name on top and the card above the button when there is room */
    .stage[data-hb-mode="compare"]{display:grid;grid-template-columns:repeat(2,minmax(0,1fr))}
    .stage[data-hb-mode="compare"] .scene{position:relative;flex-direction:column;gap:22px;padding-top:calc(clamp(16px,3vw,32px) + 30px)}
    .stage[data-hb-mode="compare"] .sc-plain{display:flex;border-right:1px solid rgba(244,244,242,.14)}
    .stage[data-hb-mode="compare"] .gb-card{flex:none;width:min(260px,100%)}
    .stage[data-hb-mode="compare"].c-solo .gb-card{position:absolute;visibility:hidden}
    .stage[data-hb-mode="compare"] .sc-name{display:block;position:absolute;top:8px;left:50%;transform:translateX(-50%);z-index:3;white-space:nowrap}
    .sc-plain .gb-btn{cursor:default}
    .sc-plain .btn-in{white-space:nowrap}
    @media(max-width:600px){
      .stage[data-hb-mode="inside"] .sq-glow{display:none}
      .stage[data-hb-mode="compare"]{grid-template-columns:minmax(0,1fr);grid-template-rows:repeat(2,minmax(0,1fr))}
      .stage[data-hb-mode="compare"] .sc-plain{border-right:0;border-bottom:1px solid rgba(244,244,242,.14)}
    }
  </style>
  <link rel="stylesheet" href="../../../assets/css/demo-page.css?v=11">
  <script src="../../../assets/js/demo-page.js?v=11" defer></script>
</head>
<body class="hb" data-hb-kind="loop">
<nav class="hb-bar" aria-label="Animation Handbook">
  <a class="hb-home" href="../../../"><svg class="hb-mark" viewBox="0 0 32 32" aria-hidden="true"><g class="g"><circle cx="11.16" cy="20.84" r="6.1"/><circle cx="15.12" cy="16.88" r="6.1"/><circle cx="18.51" cy="13.49" r="6.1"/></g><circle class="b" cx="21.2" cy="10.8" r="6.6"/></svg><span>Animation Handbook</span></a>
  <span class="hb-pager"><a href="../pull-to-refresh/" rel="prev" aria-label="Previous: Pull to Refresh"><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="m15 18-6-6 6-6"/></svg><span><span class="hb-dir">Previous: </span>Pull to Refresh</span></a><a href="../spotlight-hover/" rel="next" aria-label="Next: Spotlight Hover Glow"><span><span class="hb-dir">Next: </span>Spotlight Hover Glow</span><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="m9 18 6-6-6-6"/></svg></a></span>
  <a class="hb-star" href="https://github.com/matinmonshizadeh/animation-handbook" target="_blank" rel="noopener" aria-label="Star on GitHub"><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M12 3l2.7 5.6 6.1.9-4.4 4.3 1 6.1L12 17l-5.4 2.9 1-6.1-4.4-4.3 6.1-.9z"/></svg><span>Star on GitHub</span></a>
</nav>
<main class="hb-page">
  <header class="hb-head">
    <p class="hb-cat">04.30 · Micro-Interactions</p>
    <h1>Animated Gradient Border</h1>
    <p class="hb-lede">A band of colors runs around the edge of a card or button. Best for featured offers.</p>
  </header>

  <section class="hb-step hb-watch" aria-labelledby="hb-watch-title">
    <div class="hb-step-head">
      <span class="hb-num" aria-hidden="true">1</span>
      <div><h2 id="hb-watch-title">Watch it</h2><p>It moves by itself. Pause it to look closely, or turn on slow motion to see each part of the movement.</p></div>
    </div>
    <div class="seg hb-modes" role="group" aria-label="What the box shows" data-hb-modes>
      <button type="button" data-hb-mode="normal" class="on" aria-pressed="true">Normal</button>
      <button type="button" data-hb-mode="inside" aria-pressed="false">See inside</button>
      <button type="button" data-hb-mode="compare" aria-pressed="false">Compare</button>
    </div>
    <div class="stage hb-dots">
      <div class="scene">
        <div class="gb gb-card">
          <span class="gb-glow" aria-hidden="true"><i></i></span>
          <span class="gb-ring" aria-hidden="true"><i></i></span>
          <div class="gb-in card-in">
            <p class="eyebrow">Most popular</p>
            <p class="plan">Pro</p>
            <p class="price"><b>$12</b> / month</p>
            <ul class="feats"><li>Unlimited projects</li><li>Priority support</li></ul>
          </div>
        </div>
        <button class="gb gb-btn" type="button">
          <span class="gb-glow" aria-hidden="true"><i></i></span>
          <span class="gb-ring" aria-hidden="true"><i></i></span>
          <span class="gb-in btn-in">Upgrade now</span>
        </button>
      </div>
    </div>
    <div class="hb-player">
      <button class="hb-play" type="button" id="btn-pause" data-hb-pause="css" data-state="playing"><svg class="hb-ic hb-i-pause" viewBox="0 0 24 24" aria-hidden="true"><rect x="6" y="4" width="4" height="16" rx="1"/><rect x="14" y="4" width="4" height="16" rx="1"/></svg><svg class="hb-ic hb-i-play" viewBox="0 0 24 24" aria-hidden="true"><path d="M6 3l14 9-14 9z"/></svg><span class="hb-pause-label">Pause</span></button>
      <label class="hb-toggle"><input class="hb-switch" type="checkbox" role="switch" id="slow-tog" data-hb-slowmo="css" autocomplete="off"><span>Slow motion</span></label>
    </div>
    <div class="hb-mode-notes" aria-live="polite">
      <p data-hb-for="inside" hidden>Inside, the colors are a big square turning behind the button: the button's shape cuts it out, and a dark box covers all but the edge.</p>
      <p data-hb-for="compare" hidden>Plain border: the edge is a still, faint line. Running colors: a band of colors keeps traveling around the same edge.</p>
    </div>
  </section>

  <section class="hb-step hb-try" aria-labelledby="hb-try-title">
    <div class="hb-step-head">
      <span class="hb-num" aria-hidden="true">2</span>
      <div><h2 id="hb-try-title">Try it</h2><p>Change a setting and see the difference as it moves.</p></div>
    </div>
    <div class="hb-settings">
      <div class="hb-setting">
        <p class="hb-setting-name" id="pal-lbl">Colors</p>
        <div class="seg" id="pal-seg" role="group" aria-labelledby="pal-lbl">
          <button type="button" data-pal="rainbow" class="on" aria-pressed="true">Rainbow</button>
          <button type="button" data-pal="sunset" aria-pressed="false">Sunset</button>
          <button type="button" data-pal="ocean" aria-pressed="false">Ocean</button>
        </div>
        <p class="hb-hint">The colors that travel around the edge.</p>
      </div>
      <div class="hb-setting">
        <p class="hb-setting-name" id="speed-lbl">Speed</p>
        <div class="seg" id="speed-seg" role="group" aria-labelledby="speed-lbl">
          <button type="button" data-secs="7" aria-pressed="false">Slow</button>
          <button type="button" data-secs="4" class="on" aria-pressed="true">Normal</button>
          <button type="button" data-secs="2" aria-pressed="false">Fast</button>
        </div>
        <p class="hb-hint">How long one trip around the edge takes.</p>
      </div>
      <div class="hb-setting">
        <label class="hb-switch-row"><span>Soft glow</span><input class="hb-switch" type="checkbox" role="switch" id="glow-tog" checked autocomplete="off"></label>
        <p class="hb-hint">A blurred halo of the same colors around it.</p>
      </div>
    </div>
    <details class="hb-options">
      <summary><span class="hb-more-label">More options</span><span class="hb-less">Fewer options</span><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="m6 9 6 6 6-6"/></svg></summary>
      <div class="hb-settings">
        <div class="hb-setting">
          <p class="hb-setting-name" id="band-lbl">Band length</p>
          <div class="seg" id="band-seg" role="group" aria-labelledby="band-lbl">
            <button type="button" data-band="short" aria-pressed="false">Short</button>
            <button type="button" data-band="long" class="on" aria-pressed="true">Long</button>
            <button type="button" data-band="full" aria-pressed="false">All around</button>
          </div>
          <p class="hb-hint">How much of the edge the colors cover at once.</p>
        </div>
        <div class="hb-setting">
          <p class="hb-setting-name" id="width-lbl">Border width</p>
          <div class="seg" id="width-seg" role="group" aria-labelledby="width-lbl">
            <button type="button" data-px="1" aria-pressed="false">Thin</button>
            <button type="button" data-px="2" class="on" aria-pressed="true">Medium</button>
            <button type="button" data-px="4" aria-pressed="false">Thick</button>
          </div>
          <p class="hb-hint">How thick the colored edge is.</p>
        </div>
      </div>
    </details>
  </section>

  <section class="hb-step hb-prompt-step" aria-labelledby="hb-prompt-title">
    <div class="hb-step-head">
      <span class="hb-num" aria-hidden="true">3</span>
      <div><h2 id="hb-prompt-title">Copy the prompt</h2><p>Paste it into an AI assistant such as ChatGPT, Claude or Cursor to build this in your own project.</p></div>
    </div>
    <div class="hb-card">
      <p class="hb-prompt">Add an animated gradient border to [the card or button you want to stand out]. Give it an edge where a band of colors keeps running around it; wherever the band does not reach, the edge stays a faint line. Build it from a square layer, twice as wide as the element, filled with a gradient that sweeps around its center, clipped to the element's rounded shape and covered by an inner box everywhere but the edge. Turn that layer and never animate the gradient itself, so the browser only rotates a finished picture. For a glow, put a blurred copy of the same layer behind it. If the visitor has reduced motion turned on, keep the border still. Match the settings listed below.</p>
      <div class="hb-chips-box" hidden>
        <p class="hb-chips-title">Your settings</p>
        <ul class="hb-chips"></ul>
      </div>
      <div class="hb-copy-row">
        <button class="hb-copy" type="button"><svg class="hb-ic hb-i-copy" viewBox="0 0 24 24" aria-hidden="true"><rect x="9" y="9" width="13" height="13" rx="2"/><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"/></svg><svg class="hb-ic hb-i-done" viewBox="0 0 24 24" aria-hidden="true"><path d="M20 6 9 17l-5-5"/></svg><span class="hb-copy-label" aria-live="polite">Copy prompt</span></button>
        <p class="hb-hint">Replace the words in brackets with your own element. Your settings are added at the end.</p>
      </div>
    </div>
    <div class="hb-star-row"><p>Free and open source. If this helped, a star on GitHub helps other people find it.</p><a class="hb-star-btn" href="https://github.com/matinmonshizadeh/animation-handbook" target="_blank" rel="noopener"><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M12 3l2.7 5.6 6.1.9-4.4 4.3 1 6.1L12 17l-5.4 2.9 1-6.1-4.4-4.3 6.1-.9z"/></svg>Star on GitHub</a></div>
  </section>

  <section class="hb-about" aria-labelledby="hb-what-title">
    <div class="hb-what">
      <h2 id="hb-what-title">What it is</h2>
      <div class="hb-what-text"><p>The full explanation is in <a href="README.md">README.md</a>.</p></div>
    </div>
    <div class="hb-fit">
      <h2>Good for</h2>
      <ul class="hb-tags hb-good"><li>Featured plans</li><li>Main buttons</li><li>New features</li><li>Promo cards</li></ul>
      <h3>Avoid on</h3>
      <ul class="hb-tags hb-avoid"><li>Every card on a page</li><li>Calm reading pages</li></ul>
    </div>
  </section>

  <section class="hb-related" aria-labelledby="hb-related-title" hidden>
    <h2 id="hb-related-title">Similar animations</h2>
    <div class="hb-rel-list"></div>
  </section>
</main>
<footer class="hb-foot"><span>Animation Handbook</span><a href="https://github.com/matinmonshizadeh/animation-handbook" target="_blank" rel="noopener">Source on GitHub</a><a href="#top">Back to top ↑</a></footer>
<script>
  const stage=document.querySelector('.stage');
  const vars=document.documentElement.style;
  // Each set of colors is the four stops of the band, from its faint tail to its bright front
  const PALETTES={
    rainbow:['#ff6f8b','#ffce5a','#5fd88a','#6ea8ff'],
    sunset:['#ffce5a','#ff9d5c','#ff6f8b','#b98cff'],
    ocean:['#3fd6c4','#4cc3ff','#6ea8ff','#8f7bff']
  };
  let secs=4;

  function choices(id,pick){
    const buttons=document.querySelectorAll('#'+id+' button');
    buttons.forEach(b=>b.addEventListener('click',()=>{
      buttons.forEach(x=>{x.classList.toggle('on',x===b);x.setAttribute('aria-pressed',String(x===b))});
      pick(b.dataset);
    }));
  }
  choices('pal-seg',d=>{PALETTES[d.pal].forEach((c,i)=>vars.setProperty('--c'+(i+1),c))});
  // A new --dur keeps the time already played, so every band would jump to another point of its turn.
  // Scaling each turn's time by the same factor as its length keeps the bands where they are, moving or paused,
  // and keeps each glow in step with its ring.
  choices('speed-seg',d=>{
    const next=+d.secs;
    stage.getAnimations({subtree:true}).forEach(a=>{if(a.currentTime!=null)a.currentTime=a.currentTime*next/secs});
    secs=next;
    vars.setProperty('--dur',next+'s');
  });
  document.getElementById('glow-tog').addEventListener('change',e=>{stage.classList.toggle('no-glow',!e.target.checked)});
  choices('band-seg',d=>{stage.classList.toggle('b-short',d.band==='short');stage.classList.toggle('b-full',d.band==='full')});
  choices('width-seg',d=>{vars.setProperty('--bw',d.px+'px')});

  // See inside and Compare
  const scene=stage.querySelector('.scene'),btn=scene.querySelector('.gb-btn'),card=scene.querySelector('.gb-card');
  const mk=(tag,cls,text)=>{const e=document.createElement(tag);e.className=cls;if(text!=null)e.textContent=text;return e};
  const cap=(cls,text)=>{const e=mk('span','cap '+cls,text);e.setAttribute('data-hb-caption','');return e};
  // Compare's first half: the same card and button with no turning layers and no glow, so only the faint line is left
  const plain=scene.cloneNode(true);
  plain.classList.add('sc-plain');plain.inert=true;plain.setAttribute('aria-hidden','true');
  plain.querySelectorAll('.gb-glow,.gb-ring i').forEach(n=>n.remove());
  const pb=plain.querySelector('.gb-btn'),ps=mk('span',pb.className);ps.append(...pb.childNodes);pb.replaceWith(ps);
  plain.append(cap('sc-name','Plain border'));scene.append(cap('sc-name','Running colors'));
  stage.prepend(plain);
  // See inside: the button's hidden square, drawn whole and faint with a dashed outline, turning in step with the colors
  btn.append(mk('span','gb-sq'),mk('span','gb-sqline'));
  const lbls=mk('div','sq-lbls'),colL=mk('div','sq-col sq-l'),colR=mk('div','sq-col sq-r');
  lbls.inert=true;lbls.setAttribute('aria-hidden','true');
  const caps=['A square of colors, twice as wide, turns behind it',"The button's shape cuts it out",'A dark box covers all but the edge','',
    'A blurred copy glows behind'].map((t,i)=>cap('sq-'+(i+1)+(i===4?' sq-glow':''),t));
  caps.forEach((c,i)=>(i===0||i===4?colL:colR).append(c));
  const lead=document.createElementNS('http://www.w3.org/2000/svg','svg');lead.setAttribute('class','sq-lead');
  lead.innerHTML='<line/><circle r="3"/>'.repeat(4);
  lbls.append(lead,colL,colR);stage.append(lbls);

  // One turn takes as long as Speed says, three times as long with Slow motion on
  const NUM={2:'two',4:'four',6:'six',7:'seven',12:'twelve',21:'twenty-one'},slow=document.getElementById('slow-tog');
  function words(){caps[3].textContent='One turn every '+NUM[secs*(slow.checked?3:1)]+' seconds';layout()}
  slow.addEventListener('change',words);
  document.querySelectorAll('#speed-seg button').forEach(b=>b.addEventListener('click',words));

  // The button and its square are drawn smaller together, by one factor, until the circle the square's corners sweep fits the box;
  // labels go in two columns beside the circle, or in the corners when there is no room, each with a line to the part it names.
  // In the corners the factor also keeps that whole circle a few pixels clear of every label, so no label covers the square at any angle
  const LONG=caps[0].textContent,SHORT='A square of colors turns behind it';
  function inside(){
    const w=stage.clientWidth,h=stage.clientHeight,R=btn.offsetWidth*Math.SQRT2,fit=Math.min(1,(Math.min(w,h)-16)/(2*R)),kw=(w-360)/(2*R);
    const s=stage.getBoundingClientRect(),c0=btn.getBoundingClientRect(),cx=c0.left+c0.width/2-s.left,cy=c0.top+c0.height/2-s.top;
    const place=on=>{lbls.classList.toggle('corners',on);caps[0].textContent=on?SHORT:LONG};
    let corners=kw<.6,k=Math.min(fit,Math.max(kw,0));
    if(corners){
      place(true);
      let d=1e9;
      caps.forEach(c=>{const r=c.getBoundingClientRect();
        if(r.width)d=Math.min(d,Math.hypot(Math.max(r.left-s.left-cx,0,cx-r.right+s.left),Math.max(r.top-s.top-cy,0,cy-r.bottom+s.top)))});
      const kc=Math.min(fit,(d-4)/R);
      if(kc>k)k=Math.max(kc,.3);else corners=false;
    }
    place(corners);
    btn.style.setProperty('--k',k);
    lbls.style.setProperty('--sqw',Math.min((w-2*R*k)/2-30,230)+'px');
    const b=btn.getBoundingClientRect(),u=b.width,v=b.height;
    [0,1,2,4].forEach((n,j)=>{
      const r=caps[n].getBoundingClientRect(),g=lead.children[2*j],dot=lead.children[2*j+1];
      g.style.display=dot.style.display=r.width?'':'none';
      if(!r.width)return;
      const ax=((r.left+r.right)/2-s.left<cx?r.right:r.left)-s.left,ay=corners?((r.top+r.bottom)/2-s.top<cy?r.bottom:r.top)-s.top:(r.top+r.bottom)/2-s.top;
      const dx=ax-cx,dy=ay-cy,d=Math.hypot(dx,dy)||1,sg=Math.sign(dx)||1,up=ay<cy;
      const [px,py]=[[cx+.75*u*dx/d,cy+.75*u*dy/d],up?[cx+.22*u*sg,cy-v/2]:[cx+sg*u/2,cy],[cx+sg*.43*u,cy],
        [cx+.22*u*sg,cy+(up?-1:1)*v/2]][j];
      g.setAttribute('x1',ax);g.setAttribute('y1',ay);g.setAttribute('x2',px);g.setAttribute('y2',py);
      dot.setAttribute('cx',px);dot.setAttribute('cy',py);
    });
  }
  // Compare keeps the card above the button in each half while the box has room for the name, the card and the button
  function compare(){
    const pad=Math.min(32,Math.max(16,innerWidth*.03));
    const solo=matchMedia('(max-width:600px)').matches||stage.clientHeight<30+2*pad+card.offsetHeight+22+btn.offsetHeight;
    stage.classList.toggle('c-solo',solo);
  }
  function layout(){const m=stage.dataset.hbMode;if(m==='inside')inside();else if(m==='compare')compare()}
  document.addEventListener('hb:mode',layout);
  // a setting can change the button's width (Border width) or hide a label (Soft glow), so the view works out its places again
  for(const type of ['click','change'])document.querySelector('.hb-try').addEventListener(type,layout);
  new ResizeObserver(layout).observe(stage);
  if(document.fonts)document.fonts.ready.then(layout);
  words();

  // Under reduced motion the loop starts paused, so both views are still pictures and their notes say so, until Play moves them
  const reduceMQ=matchMedia('(prefers-reduced-motion: reduce)'),notes=[...document.querySelectorAll('[data-hb-for]')];
  const moving=notes.map(n=>n.textContent),still=[
    "Inside, shown still: the colors come from a big square behind the button; the button's shape cuts it out, and a dark box covers all but the edge.",
    'Shown still: Plain border has only a faint line for an edge. Running colors has a band of colors on the same edge.'];
  let paused=reduceMQ.matches;
  function describe(){notes.forEach((n,i)=>{const t=reduceMQ.matches&&paused?still[i]:moving[i];if(n.textContent!==t)n.textContent=t})}
  document.addEventListener('hb:pause',e=>{paused=e.detail.paused;describe()});
  reduceMQ.addEventListener('change',describe);
  describe();
</script>
</body>
</html>