← Text reveals

Stretch-in

Letters start squashed thin, like slivers standing on end, and stretch out to full width with a springy wobble, one after another.

Stretch out

How to use it

  1. Paste the snippet just before </head>.
  2. Add data-stretch-in to any heading or text element.

Options

Add any of these attributes to the same element.

data-loopStretch out, hold, squeeze back and stretch out again.
data-holdHow long the text stays before squeezing, when looping. Default 1600 ms.
data-fromWhere the stretching starts: first (default), center or last.
data-durationHow long each letter takes to stretch. Default 1100 ms.
data-staggerGap between letters. Default 55 ms.
data-delayWait before the first letter. Default 0 ms.
1 · Snippet
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/bundles/anime.umd.min.js"></script>
<script>
  (function () {
    // Stretch-in: letters start squashed thin, like slivers standing on end,
    // and stretch out to full width with a springy wobble, one after another.
    // Usage: <h1 data-stretch-in>Stretch out</h1>
    // Optional: data-loop (letters squeeze back to slivers and stretch out
    // again), data-hold (ms to stay before squeezing), data-from (where the
    // stretching starts: "first", the default, "center" or "last"),
    // data-duration (ms per letter), data-stagger (ms between letters),
    // data-delay (ms before starting).
    var DEFAULTS = { duration: 1100, stagger: 55, delay: 0, hold: 1600, from: 'first' };

    var css =
      '.sti-word{display:inline-block;white-space:nowrap}' +
      '.sti-char{display:inline-block;transform-origin:50% 100%;will-change:transform}';
    var style = document.createElement('style');
    style.textContent = css;
    document.head.appendChild(style);

    function reduced() {
      return window.matchMedia('(prefers-reduced-motion: reduce)').matches;
    }

    function split(el) {
      var text = el.textContent.replace(/\s+/g, ' ').trim();
      el.setAttribute('aria-label', text);
      el.textContent = '';
      var chars = [];
      text.split(' ').forEach(function (word, i) {
        if (i) el.appendChild(document.createTextNode(' '));
        var w = document.createElement('span');
        w.className = 'sti-word';
        w.setAttribute('aria-hidden', 'true');
        Array.from(word).forEach(function (c) {
          var s = document.createElement('span');
          s.className = 'sti-char';
          s.textContent = c;
          w.appendChild(s);
          chars.push(s);
        });
        el.appendChild(w);
      });
      return chars;
    }

    function play(el, chars) {
      var d = el.dataset;
      var loop = d.loop !== undefined && d.loop !== 'false';
      var duration = +d.duration || DEFAULTS.duration;
      var stagger = +d.stagger || DEFAULTS.stagger;
      var from = d.from || DEFAULTS.from;

      var tl = anime.createTimeline({ loop: loop, loopDelay: 300, delay: +d.delay || DEFAULTS.delay });
      tl.add(chars, {
        // Thin and a little tall to start, then springing out to full width.
        scaleX: { from: 0.04, to: 1, ease: 'outElastic(1, .55)' },
        scaleY: { from: 1.35, to: 1, duration: duration * 0.5, ease: 'out(3)' },
        opacity: { from: 0, to: 1, duration: duration * 0.2, ease: 'linear' },
        duration: duration,
        delay: anime.stagger(stagger, { from: from })
      });
      if (loop) {
        tl.add(chars, {
          scaleX: 0.04,
          scaleY: 1.35,
          opacity: 0,
          duration: duration * 0.4,
          ease: 'in(3)',
          delay: anime.stagger(stagger * 0.5, { from: from })
        }, '+=' + (+d.hold || DEFAULTS.hold));
      }
    }

    function init() {
      var io = new IntersectionObserver(function (entries) {
        entries.forEach(function (e) {
          if (!e.isIntersecting) return;
          io.unobserve(e.target);
          play(e.target, e.target._stiChars);
        });
      }, { threshold: 0.2 });
      document.querySelectorAll('[data-stretch-in]').forEach(function (el) {
        if (el._stiChars) return;
        var chars = split(el);
        el._stiChars = chars;
        if (reduced()) return;
        anime.utils.set(chars, { opacity: 0, scaleX: 0.04 });
        io.observe(el);
      });
    }

    if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', init);
    else init();
  })();
</script>
2 · Your HTML
<h1 data-stretch-in>Stretch out</h1>

<!-- or, from the middle, looping: -->
<h1 data-stretch-in data-from="center" data-loop>Stretch out</h1>