← Entrances

TV Switch-off

Like an old tube TV turning off: the picture flares bright as it collapses to a thin glowing line, the line pulls in to a dot, and the dot fades away.

How to use it

  1. Paste the snippet just before </head>.
  2. Add data-tv-switch-off to any element. It looks best on a dark background, where the line and dot glow.

Options

Add any of these attributes to the same element.

data-lineHow thick the line and dot are, in px. Default 3.
data-flashHow bright it flares as it collapses. 1 for no flare. Default 4.
data-durationHow long it takes to switch off. Default 700 ms.
data-delayHow long it stays in view before it switches off. Default 1200 ms.
data-loopSwitch back on, hold and switch off again.
data-holdHow long it stays on between exits, when looping. Default 1400 ms.

It plays once it has been in view for data-delay, and ends hidden (visibility: hidden) but still taking up its space. To play it when you choose, such as when something is dismissed, leave the attribute off and call animacious.tvSwitchOff(el). The flare uses a CSS filter, which the script clears when it switches back on.

1 · Snippet
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/bundles/anime.umd.min.js"></script>
<script>
  (function () {
    // TV Switch-off: like an old tube TV turning off, the element flares
    // bright as it collapses to a thin glowing line, the line shrinks to a
    // dot, and the dot fades away.
    // Usage: <div data-tv-switch-off>…</div>
    // It plays once the element has been in view for data-delay ms, and ends
    // hidden (visibility: hidden) but still taking up its space. It looks best
    // on a dark background, where the line and dot glow.
    // Optional: data-line (thickness of the line, in px, default 3),
    // data-flash (how bright it flares, 1 for no flare, default 4),
    // data-duration (ms, default 700), data-delay (ms in view before it
    // switches off, default 1200), data-loop (switch back on, hold and switch
    // off again), data-hold (ms to stay on between exits when looping,
    // default 1400).
    // Script use: animacious.tvSwitchOff(el, done) plays it on any element
    // now, using its data-* options, and calls done afterwards.
    var DEFAULTS = { line: 3, flash: 4, duration: 700, delay: 1200, hold: 1400 };

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

    function leave(el, done) {
      var d = el.dataset;
      var t = +d.duration || DEFAULTS.duration;
      var line = +d.line || DEFAULTS.line;
      var flash = d.flash === undefined ? DEFAULTS.flash : +d.flash || 1;
      if (reduced()) {
        anime.utils.set(el, { opacity: 0 });
        if (done) done();
        return;
      }
      // Measure without any transform left over from a previous run.
      anime.utils.set(el, { scaleX: 1, scaleY: 1 });
      var w = el.offsetWidth || 1;
      var h = el.offsetHeight || 1;
      anime.utils.set(el, { filter: 'contrast(1) brightness(1) saturate(1)' });
      anime.createTimeline({
        onComplete: function () {
          if (done) done();
        }
      })
        // The picture squashes to a bright horizontal line…
        .add(el, {
          scaleY: { to: line / h, ease: 'in(3)' },
          // Pull every colour towards grey, then brighten: the line glows white.
          filter: { to: flash > 1 ? 'contrast(0.3) brightness(' + flash + ') saturate(0)' : 'contrast(1) brightness(1) saturate(1)', ease: 'in(2)' },
          duration: t * 0.4
        })
        // …the line pulls in to a dot…
        .add(el, { scaleX: line / w, duration: t * 0.35, ease: 'inOut(3)' }, '+=' + Math.round(t * 0.05))
        // …and the dot fades.
        .add(el, { opacity: 0, scaleX: line / w * 0.4, scaleY: line / h * 0.4, duration: t * 0.25, ease: 'in(2)' }, '+=' + Math.round(t * 0.08));
    }

    function cycle(el) {
      var d = el.dataset;
      var s = el._tvo;
      leave(el, function () {
        if (d.loop === undefined || d.loop === 'false') {
          el.style.visibility = 'hidden';
          return;
        }
        // Switch back on quietly, then go again once it's on screen.
        setTimeout(function () {
          anime.utils.set(el, { scaleX: 1, scaleY: 1, opacity: 0 });
          el.style.filter = '';
          anime.animate(el, {
            opacity: 1,
            duration: 500,
            ease: 'out(2)',
            onComplete: function () {
              setTimeout(function () {
                if (s.visible) cycle(el);
                else s.waiting = true;
              }, +d.hold || DEFAULTS.hold);
            }
          });
        }, 600);
      });
    }

    window.animacious = window.animacious || {};
    window.animacious.tvSwitchOff = function (el, done) {
      leave(el, function () {
        el.style.visibility = 'hidden';
        if (done) done(el);
      });
    };

    function init() {
      var io = new IntersectionObserver(function (entries) {
        entries.forEach(function (e) {
          var s = e.target._tvo;
          s.visible = e.isIntersecting;
          if (!e.isIntersecting) return;
          if (!s.started) {
            s.started = true;
            var wait = e.target.dataset.delay;
            setTimeout(function () { cycle(e.target); }, wait === undefined ? DEFAULTS.delay : +wait || 0);
          } else if (s.waiting) {
            s.waiting = false;
            cycle(e.target);
          }
        });
      }, { threshold: 0.2 });
      document.querySelectorAll('[data-tv-switch-off]').forEach(function (el) {
        if (el._tvo) return;
        el._tvo = {};
        if (!reduced()) io.observe(el);
      });
    }

    if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', init);
    else init();
  })();
</script>
2 · Your HTML
<div class="screen" data-tv-switch-off>…</div>

<!-- or a thinner line with no flare: -->
<div class="screen" data-tv-switch-off data-line="2" data-flash="1">…</div>