# Signal glitch

> The headline tears, warps and splits into colours, like a bad transmission.

- Applies to the whole headline
- Live page with controls: https://smthngfun.com/a/glitch
- This style as JSON: https://smthngfun.com/api/styles/glitch
- Every style: https://smthngfun.com/llms.txt

## Settings

The values below are the ones this page was asked for. Change any of them by adding it as a query parameter to the page, to this file or to the JSON address.

| Setting | Parameter | Value | Allowed |
| --- | --- | --- | --- |
| Average time between bursts | `gap` | `1800` | 300 to 8000 ms |
| Length of a burst | `burst` | `380` | 80 to 1500 ms |
| Warp | `warp` | `0.3` | 0 to 1 em |
| How far it tears | `shift` | `0.12` | 0.02 to 0.5 em |
| Colour split | `split` | `0.04` | 0 to 0.15 em |
| Split colours | `palette` | empty | Up to eight six digit hex colours joined with dashes. Empty means off. |

- `t` The headline text to show and to use in the example code.
- `f` A Google font name for the headline, for example Playfair Display.

## Prompt

Written for a coding assistant. It describes the animation fully, so it works without the code below.

```text
Animate the main headline in the hero section of my landing page with an effect called Signal glitch. Make the headline glitch like a transmission breaking up in a science fiction film. It sits perfectly still and readable most of the time, then suffers short bursts of interference at random moments. Bursts must not come on a fixed beat. Wait a random time between them that averages 1800ms, and make each burst last a random time that averages 380ms. During a burst, change the damage every 45 to 90 milliseconds so no two frames match. Build the visible headline from seven stacked copies of the text, each clipped to its own horizontal band, so together they look like one normal headline. Keep the real text in the page underneath, transparent, for layout and screen readers. In each frame of a burst, pick new random heights for the bands and slide roughly half of them sideways by a random amount of up to 0.12em in either direction, so the text looks torn along horizontal lines. Give the bands that move a small random slant and sideways stretch as well. Warp the letters themselves during a burst. Apply an SVG filter to the heading that makes noise which changes from row to row but not along a row, by giving feTurbulence a base frequency close to zero across and a random value down, and use it in feDisplacementMap to push each row of pixels sideways by up to 0.3em. Displace sideways only, never up or down, so the letters bend and smear like a picture losing horizontal hold. Pick a new noise seed, frequency and strength every frame, and take the filter off completely between bursts. Put two more full copies of the text behind the headline, one in each of my two accent colours, hidden at rest, and during a burst show them and push them apart left and right by up to 0.04em so coloured fringes show at the edges of the letters. Now and then let the whole headline flicker dimmer for a single frame. When a burst ends, snap everything back exactly to rest. Do not add static, grain or noise specks over the text. If the visitor has prefers-reduced-motion turned on, show the plain headline and do none of this. Keep my existing font, colours and layout exactly as they are. Screen readers and search engines must still read the headline as one normal sentence, so put the full text in an aria-label on the heading and mark any decorative spans as aria-hidden. If the visitor has prefers-reduced-motion turned on, show the finished headline straight away with no animation. Make sure nothing shifts the layout while it plays. Use plain CSS and a little vanilla JavaScript, unless my project already has an animation library, in which case use that. A live demo with reference code for these exact settings is at https://smthngfun.com/a/glitch and you can use that code as your starting point, adapted to my codebase and styling. If I am happy to credit the source, add a small "Animation by smthngfun" link to https://smthngfun.com in the footer of the page. Ask me before adding it.
```

## Reference code

A working version with the settings above. It assumes the headline has the class `hero-title`. Adapt it to the project's own framework and styling.

```html
<!-- Signal glitch animation from https://smthngfun.com/a/glitch -->
<h1 class="hero-title">Make your landing page feel alive</h1>
```

```css
.hero-title { position: relative; z-index: 0; }
.hero-title .glitch-base { display: block; color: transparent; } /* holds the layout */
.hero-title .glitch-strip,
.hero-title .glitch-ghost {
  position: absolute;
  inset: 0;
  display: block;
  pointer-events: none;
}
.hero-title .glitch-ghost { z-index: -1; opacity: 0; }
.hero-title .glitch-ghost.one { color: #7458c9; }
.hero-title .glitch-ghost.two { color: #c3b3f3; }
```

```js
const title = document.querySelector('.hero-title');
const text = title.textContent.trim();
title.setAttribute('aria-label', text);
if (!matchMedia('(prefers-reduced-motion: reduce)').matches) {
  const STRIPS = 7;
  const size = parseFloat(getComputedStyle(title).fontSize);
  const copy = function (cls) {
    const el = document.createElement('span');
    el.className = cls;
    el.setAttribute('aria-hidden', 'true');
    el.textContent = text;
    return el;
  };
  title.textContent = '';
  const ghosts = [copy('glitch-ghost one'), copy('glitch-ghost two')];
  const strips = Array.from({ length: STRIPS }, function () { return copy('glitch-strip'); });
  title.append(copy('glitch-base'), ghosts[0], ghosts[1]);
  strips.forEach(function (s) { title.append(s); });

  // The warp. Noise that changes from row to row pushes each row of pixels sideways.
  // The colour matrix pins the green channel at the middle, so nothing moves up or down.
  const holder = document.createElement('div');
  holder.innerHTML = '<svg width="0" height="0" style="position:absolute" aria-hidden="true">' +
    '<filter id="glitch-warp" x="-20%" y="-10%" width="140%" height="120%" color-interpolation-filters="sRGB">' +
    '<feTurbulence type="fractalNoise" numOctaves="1" baseFrequency="0.0001 0.02" seed="1"/>' +
    '<feColorMatrix type="matrix" values="1 0 0 0 0  0 0 0 0 0.5  0 0 1 0 0  0 0 0 1 0"/>' +
    '<feDisplacementMap in="SourceGraphic" scale="0" xChannelSelector="R" yChannelSelector="G"/>' +
    '</filter></svg>';
  title.append(holder.firstChild);
  const rows = title.querySelector('feTurbulence');
  const bend = title.querySelector('feDisplacementMap');

  // Each strip shows one horizontal band of the text. Together they make the whole headline.
  function cut(edges) {
    strips.forEach(function (s, i) {
      s.style.clipPath = 'inset(calc(' + edges[i] + '% - 0.6px) -2em calc(' + (100 - edges[i + 1]) + '% - 0.6px) -2em)';
    });
  }
  function rest() {
    cut(Array.from({ length: STRIPS + 1 }, function (_, i) { return (i * 100) / STRIPS; }));
    strips.forEach(function (s) { s.style.transform = ''; });
    ghosts.forEach(function (g) { g.style.transform = ''; g.style.opacity = 0; });
    title.style.filter = '';
    title.style.opacity = '';
  }
  function damage() {
    const cuts = Array.from({ length: STRIPS - 1 }, function () { return Math.random() * 100; }).sort(function (a, b) { return a - b; });
    cut([0].concat(cuts, [100]));
    strips.forEach(function (s) {
      if (Math.random() < 0.5) { s.style.transform = ''; return; }
      const dx = (Math.random() * 2 - 1) * 0.12 * size;
      s.style.transform = 'translateX(' + dx + 'px) skewX(' + (Math.random() * 2 - 1) * 14 + 'deg) scaleX(' + (0.92 + Math.random() * 0.2) + ')';
    });
    const split = 0.04 * size * (0.4 + Math.random() * 0.6);
    ghosts[0].style.transform = 'translateX(' + -split + 'px)';
    ghosts[1].style.transform = 'translateX(' + split + 'px)';
    ghosts.forEach(function (g) { g.style.opacity = 1; });
    title.style.opacity = Math.random() < 0.15 ? 0.55 : '';

    // New warp every frame. Rows about a third of a letter to a whole letter tall.
    rows.setAttribute('seed', Math.floor(Math.random() * 999));
    rows.setAttribute('baseFrequency', '0.0001 ' + (0.6 + Math.random() * 2.4) / size);
    bend.setAttribute('scale', 0.3 * size * (0.3 + Math.random() * 0.7) * 2);
    title.style.filter = 'url(#glitch-warp)';
  }

  function burst() {
    const ends = performance.now() + 380 * (0.6 + Math.random() * 0.8);
    (function frame() {
      if (performance.now() >= ends) {
        rest();
        setTimeout(burst, 1800 * (0.4 + Math.random() * 1.2));
        return;
      }
      damage();
      setTimeout(frame, 45 + Math.random() * 45);
    })();
  }

  rest();
  setTimeout(burst, 600);
}
```

## Credit

Keep the comment at the top of the code, which reads "Signal glitch animation from https://smthngfun.com/a/glitch". If the person you are building for is happy to credit the source, add a small visible "Animation by smthngfun" link to https://smthngfun.com in the footer of their page. Ask them first, and do not add it without their say.
