All effectsClick & switch

SVG Icon Morph

图标路径变形

The path of one icon smoothly interpolates into the shape of another when switched.

Activate the button to morph the icon

Where to use it

Show state changes in playback controls, toolbars, or compact switches. Match coordinates and segment types between shapes, and update the control’s accessible label as its action changes.

Practical example

A podcast player transitions the play triangle into two pause bars while updating the control for screen readers.

Also called

  • Shape Morph
  • Icon Interpolation

AI prompt

Add SVG Icon Morph between [icon A] and [icon B] on toggle. Interpolate compatible path data over about 200ms. Use a crossfade for incompatible paths. Honor reduced motion with an instant swap.

Example code

Save the standalone HTML as a file and open it in your browser. It uses the same code as the preview.

View HTML
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>SVG Icon Morph</title>
  <style>
    :root {
      color-scheme: dark;
      --demo-bg: #171629;
      --demo-ink: #f8f5ff;
      --demo-accent: #b9a6ff;
      --demo-muted: #bfbbd3;
      font-family: system-ui, sans-serif;
      color: var(--demo-ink);
      background: var(--demo-bg);
    }

    *, *::before, *::after { box-sizing: border-box; }
    body { margin: 0; min-width: 0; }
    button, input { font: inherit; }

    .demo {
      display: grid;
      place-items: center;
      align-content: center;
      min-height: 100vh;
      padding: 14px;
      overflow: hidden;
      background: radial-gradient(ellipse at 50% 24%, #28233f, var(--demo-bg) 78%);
    }

    .demo-button {
      border: 1px solid #ffffff3d;
      border-radius: 7px;
      background: #ffffff12;
      color: var(--demo-ink);
      padding: 6px 11px;
      font-size: 12px;
      cursor: pointer;
    }

    :focus-visible {
      outline: 2px solid var(--demo-accent);
      outline-offset: 3px;
    }

    .demo {
      background: radial-gradient(ellipse at 50% 15%, #484354, #252333 65%, #1c1b28);
    }
    .morph-toolbar {
      display: grid; grid-template-columns: repeat(2, 1fr); gap: 2px; width: min(100%, 250px);
      padding: 5px; border: 1px solid #716a7c; border-radius: 20px;
      background: #24222e; box-shadow: 0 13px 22px #16132090, inset 0 1px #ffffff14;
    }
    .morph-toggle {
      display: grid; gap: 8px; place-items: center; padding: 16px 8px 12px;
      border: 0; border-radius: 15px; color: #efebfc; background: #393647;
      font-size: 11px; font-weight: 650; cursor: pointer;
    }
    .morph-toggle:first-child { color: #83dcff; }
    .morph-toggle:last-child { color: #ffd197; }
    .morph-toggle:hover, .morph-toggle[aria-pressed="true"] { background: #4b465d; }
    .morph-toggle svg { width: 32px; height: 32px; fill: currentColor; }
    .morph-toggle span { color: #efebfc; }
    @media (min-height: 250px) {
      .morph-toggle { padding-block: 24px; }
      .morph-toggle svg { width: 44px; height: 44px; }
    }
  </style>
</head>
<body>
  <main class="demo">
    <div class="morph-toolbar">
      <button class="morph-toggle" type="button" aria-pressed="false">
        <svg viewBox="0 0 32 32" aria-hidden="true"><path d="M8 5 L17 10 L17 22 L8 27 Z M17 10 L27 16 L27 16 L17 22 Z"></path></svg>
        <span>Play</span>
      </button>
      <button class="morph-toggle" type="button" aria-pressed="false">
        <svg viewBox="0 0 32 32" aria-hidden="true"><path d="M7 14 L14 14 L14 7 L18 7 L18 14 L25 14 L25 18 L18 18 L18 25 L14 25 L14 18 L7 18 Z"></path></svg>
        <span>Add</span>
      </button>
    </div>
  </main>
  <script>
    const pairs = [{"base":"M8 5 L17 10 L17 22 L8 27 Z M17 10 L27 16 L27 16 L17 22 Z","target":"M8 5 L14 5 L14 27 L8 27 Z M19 5 L25 5 L25 27 L19 27 Z","initial":"Play","toggled":"Pause"},{"base":"M7 14 L14 14 L14 7 L18 7 L18 14 L25 14 L25 18 L18 18 L18 25 L14 25 L14 18 L7 18 Z","target":"M5 16 L8 13 L12 17 L14 19 L17 16 L24 9 L27 12 L19 20 L14 25 L11 22 L8 19 L5 16 Z","initial":"Add","toggled":"Added"}];
    const reduced = matchMedia('(prefers-reduced-motion: reduce)');
    const numbers = /-?[0-9.]+/g;

    document.querySelectorAll('.morph-toggle').forEach((button, index) => {
      const { base, target, initial, toggled } = pairs[index];
      const path = button.querySelector('path');
      const label = button.querySelector('span');
      let frame;
      let fade;
      let pressed = false;

      function animateTo(destination) {
        cancelAnimationFrame(frame);
        fade?.cancel();
        if (reduced.matches) {
          path.setAttribute('d', destination);
          return;
        }

        const startingPath = path.getAttribute('d');
        if (startingPath.replace(numbers, '#') !== destination.replace(numbers, '#')) {
          const outgoing = path.cloneNode();
          path.after(outgoing);
          path.setAttribute('d', destination);
          const exit = outgoing.animate([{ opacity: 1 }, { opacity: 0 }], { duration: 200, fill: 'forwards' });
          exit.finished.then(() => outgoing.remove(), () => outgoing.remove());
          fade = path.animate([{ opacity: 0 }, { opacity: 1 }], { duration: 200 });
          return;
        }

        const start = startingPath.match(numbers).map(Number);
        const end = destination.match(numbers).map(Number);
        const began = performance.now();
        function draw(time) {
          const progress = Math.min((time - began) / 200, 1);
          const eased = progress * progress * (3 - 2 * progress);
          let coordinate = 0;
          path.setAttribute('d', destination.replace(numbers, () => {
            const value = start[coordinate] + (end[coordinate] - start[coordinate]) * eased;
            coordinate += 1;
            return value.toFixed(2);
          }));
          if (progress < 1) frame = requestAnimationFrame(draw);
          else path.setAttribute('d', destination);
        }
        frame = requestAnimationFrame(draw);
      }

      button.addEventListener('click', () => {
        pressed = !pressed;
        button.setAttribute('aria-pressed', String(pressed));
        label.textContent = pressed ? toggled : initial;
        animateTo(pressed ? target : base);
      });
      reduced.addEventListener('change', () => {
        if (reduced.matches) animateTo(pressed ? target : base);
      });
    });
  </script>
</body>
</html>

Original example by UI Skills