← Text effects

Neon Flicker

Glowing text stutters on like a neon tube warming up, with one faulty letter that takes a beat longer to catch. Loop it and the sign hums, and every so often a letter buzzes.

Late night

How to use it

  1. Paste the snippet just before </head>.
  2. Add data-neon-flicker to a heading on a dark background.

Options

Add any of these attributes to the same element.

data-colorThe tube’s glow. Default #ff4fd8.
data-loopKeep humming once lit, with a letter buzzing every few seconds.
data-delayWait before switching on. Default 0 ms.

Neon needs the dark: on a light background the glow disappears. Rounded, heavy fonts look most like real tubes.

1 · Snippet
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/bundles/anime.umd.min.js"></script>
<script>
  (function () {
    // Neon Flicker: glowing text stutters on like a neon tube warming up, with
    // one faulty letter that takes a little longer to catch. Loop it and the
    // sign hums and buzzes now and then, as real ones do.
    // Usage: <h1 data-neon-flicker>Open late</h1> (on a dark background)
    // Optional: data-color (the tube's glow; default #ff4fd8), data-loop (hum,
    // with a letter buzzing every few seconds), data-delay (ms before switching
    // on).
    var DEFAULTS = { color: '#ff4fd8', delay: 0 };
    // When the whole sign switches on (true) and off (false), in ms.
    var STARTUP = [[0, true], [70, false], [260, true], [310, false], [380, true], [430, false], [700, true]];

    var css =
      '.nfl{--nfl-glow:1;color:#fff;text-shadow:0 0 0.03em #fff,0 0 calc(var(--nfl-glow)*0.1em) var(--nfl-c),' +
      '0 0 calc(var(--nfl-glow)*0.28em) var(--nfl-c),0 0 calc(var(--nfl-glow)*0.7em) var(--nfl-c)}' +
      '.nfl-word{white-space:nowrap}' +
      // An unlit tube: dim glass in the tube's colour, no glow.
      '.nfl-off{color:var(--nfl-c);opacity:0.2;text-shadow:none}';
    var style = document.createElement('style');
    style.textContent = css;
    document.head.appendChild(style);

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

    function light(chars, on) {
      chars.forEach(function (c) { c.classList.toggle('nfl-off', !on); });
    }

    // A few quick off/on blinks, starting at `at` ms into the timeline.
    function buzz(tl, chars, at, count) {
      for (var i = 0; i < count; i++) {
        tl.call(function () { light(chars, false); }, at + i * 110)
          .call(function () { light(chars, true); }, at + i * 110 + anime.utils.random(30, 70));
      }
      return tl;
    }

    function play(el, chars) {
      var d = el.dataset;
      var loop = d.loop !== undefined && d.loop !== 'false';
      var faulty = [chars[anime.utils.random(0, chars.length - 1)]];
      var rest = chars.filter(function (c) { return c !== faulty[0]; });

      var tl = anime.createTimeline({ delay: +d.delay || DEFAULTS.delay });
      STARTUP.forEach(function (step) {
        tl.call(function () { light(chars, step[1]); }, step[0]);
      });
      // The faulty letter misses the last switch-on and catches up after a beat.
      tl.call(function () { light(rest, true); light(faulty, false); }, 700);
      buzz(tl, faulty, 1050, 3).call(function () { light(faulty, true); }, 1500);
      // The glow blooms out as the gas warms up.
      tl.add(el, { '--nfl-glow': [0.35, 1], duration: 1400, ease: 'out(3)' }, 700);
      if (loop) tl.call(function () { hum(el, chars); }, 2100);
    }

    // Looping: the glow breathes slightly and, every few seconds, a letter (or
    // now and then the whole sign) buzzes.
    // Rests while off screen, to save battery.
    function hum(el, chars) {
      var visible = true;
      var glow = anime.animate(el, { '--nfl-glow': 0.8, duration: 1600, ease: 'inOutSine', loop: true, alternate: true });
      new IntersectionObserver(function (entries) {
        visible = entries[0].isIntersecting;
        if (visible) glow.play(); else glow.pause();
      }).observe(el);
      (function next() {
        setTimeout(function () {
          if (!visible) return next();
          var whole = Math.random() < 0.25;
          var who = whole ? chars : [chars[anime.utils.random(0, chars.length - 1)]];
          buzz(anime.createTimeline(), who, 0, anime.utils.random(2, 4))
            .call(function () { light(who, true); next(); }, 600);
        }, anime.utils.random(1800, 4200));
      })();
    }

    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._nfl);
        });
      }, { threshold: 0.4 });
      document.querySelectorAll('[data-neon-flicker]').forEach(function (el) {
        if (el._nfl) return;
        el.classList.add('nfl');
        el.style.setProperty('--nfl-c', el.dataset.color || DEFAULTS.color);
        var chars = (el._nfl = split(el));
        // Reduced motion: the sign is simply on.
        if (window.matchMedia('(prefers-reduced-motion: reduce)').matches) return;
        light(chars, false);
        io.observe(el);
      });
    }

    if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', init);
    else init();
  })();
</script>
2 · Your HTML
<section style="background: #06061a">
  <h1 data-neon-flicker data-loop>Open late</h1>
</section>