# Decode

> Random characters resolve into the text.

- Applies to the whole headline
- Live page with controls: https://smthngfun.com/a/decode
- This style as JSON: https://smthngfun.com/api/styles/decode
- 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 |
| --- | --- | --- | --- |
| Scramble before locking | `delay` | `350` | 0 to 2000 ms |
| Delay between letters | `stagger` | `35` | 5 to 150 ms |
| Scramble speed | `swap` | `50` | 20 to 250 ms |
| Accent colour | `accent` | empty | Six digit hex without the hash. Empty means the site's own accent colour. |

- `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 Decode. Start with every letter of the headline replaced by random characters picked from a set of capital letters, digits and symbols. Swap the random characters every 50ms. Lock the letters into their real value one at a time from left to right, starting after 350ms with 35ms between letters. Show letters that are still scrambling in my accent colour and switch them to the normal text colour once they lock. Give each letter a fixed width equal to its real width so the headline does not jitter while it scrambles. 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/decode 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
<!-- Decode animation from https://smthngfun.com/a/decode -->
<h1 class="hero-title">Make your landing page feel alive</h1>
```

```css
.hero-title .word { display: inline-block; white-space: nowrap; }
.hero-title .char { display: inline-block; }
.hero-title .char { text-align: center; }
.hero-title .char.pending { color: var(--accent, currentColor); }
```

```js
const title = document.querySelector('.hero-title');
const text = title.textContent.trim();
title.setAttribute('aria-label', text);
let n = 0;
title.innerHTML = text.split(/\s+/).map(function (word) {
  const chars = Array.from(word).map(function (ch) {
    return '<span class="char" style="--i:' + (n++) + '">' + ch + '</span>';
  }).join('');
  return '<span class="word" aria-hidden="true">' + chars + '</span>';
}).join(' ');
const chars = Array.from(title.querySelectorAll('.char'));
const glyphs = 'ABCDEFGHJKLMNPQRSTUVWXYZ0123456789#%&/<>*+=';
const size = parseFloat(getComputedStyle(title).fontSize);
if (!matchMedia('(prefers-reduced-motion: reduce)').matches) {
  chars.forEach(function (c) {
    c.dataset.ch = c.textContent;
    c.style.width = c.getBoundingClientRect().width / size + 'em';
    c.classList.add('pending');
  });
  const start = performance.now();
  let last = 0;
  requestAnimationFrame(function tick(now) {
    const swap = now - last > 50;
    if (swap) last = now;
    let done = true;
    chars.forEach(function (c, i) {
      if (!c.classList.contains('pending')) return;
      if (now - start >= 350 + i * 35) {
        c.textContent = c.dataset.ch;
        c.classList.remove('pending');
      } else {
        done = false;
        if (swap) c.textContent = glyphs[Math.floor(Math.random() * glyphs.length)];
      }
    });
    if (!done) requestAnimationFrame(tick);
  });
}
```

## Credit

Keep the comment at the top of the code, which reads "Decode animation from https://smthngfun.com/a/decode". 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.
