← Text reveals

Character Blur-in

Letters come into focus one after another, sharpening from a soft blur and settling to size as they fade in, like a lens pulling focus across the line.

Into focus

How to use it

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

Options

Add any of these attributes to the same element.

data-loopCome into focus, hold, blur away and come back.
data-holdHow long the text stays before blurring away, when looping. Default 1800 ms.
data-blurHow blurry the letters start, in px. Default 14.
data-durationHow long each letter takes to sharpen. Default 1200 ms.
data-staggerGap between letters. Default 45 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 () {
    // Character Blur-in: letters come into focus one after another, sharpening
    // from a soft blur and settling to size as they fade in, like a lens pulling
    // focus across the line.
    // Usage: <h1 data-character-blur-in>Into focus</h1>
    // Optional: data-loop (letters blur away and come back into focus),
    // data-hold (ms to stay before blurring away), data-blur (px, default 14),
    // data-duration (ms per letter), data-stagger (ms between letters),
    // data-delay (ms before starting).
    var DEFAULTS = { blur: 14, duration: 1200, stagger: 45, delay: 0, hold: 1800 };

    var css =
      '.cbi-word{display:inline-block;white-space:nowrap}' +
      '.cbi-char{display:inline-block;will-change:transform,filter,opacity}';
    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 = 'cbi-word';
        w.setAttribute('aria-hidden', 'true');
        Array.from(word).forEach(function (c) {
          var s = document.createElement('span');
          s.className = 'cbi-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 blur = 'blur(' + (+d.blur || DEFAULTS.blur) + 'px)';

      var tl = anime.createTimeline({ loop: loop, loopDelay: 300, delay: +d.delay || DEFAULTS.delay });
      tl.add(chars, {
        opacity: { from: 0, to: 1, duration: duration * 0.6 },
        filter: [blur, 'blur(0px)'],
        scale: [1.3, 1],
        duration: duration,
        ease: 'out(3)',
        delay: anime.stagger(stagger)
      });
      if (loop) {
        // Drift out of focus in the same direction, letter by letter.
        tl.add(chars, {
          opacity: 0,
          filter: blur,
          scale: 0.9,
          duration: duration * 0.6,
          ease: 'in(2)',
          delay: anime.stagger(stagger * 0.6)
        }, '+=' + (+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._cbiChars);
        });
      }, { threshold: 0.2 });
      document.querySelectorAll('[data-character-blur-in]').forEach(function (el) {
        if (el._cbiChars) return;
        var chars = split(el);
        el._cbiChars = chars;
        if (reduced()) return;
        anime.utils.set(chars, { opacity: 0 });
        io.observe(el);
      });
    }

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

<!-- or, looping: -->
<h1 data-character-blur-in data-loop>Into focus</h1>