# Scramble decode

Subtle web animation sketch from [https://mighil.com/animations](https://mighil.com/animations).

- **Live demo:** https://mighil.com/animations/scramble-decode
- **Standalone HTML:** https://mighil.com/animations/scramble-decode?download=1
- **This file:** https://mighil.com/animations/scramble-decode/scramble-decode.md
- **Technique:** `glyphs`

## What it does

Noise settles left to right into a real line.

## Instructions for AI / coding agents

Implement this micro-interaction in the user's stack (vanilla HTML, React, Vue, Svelte, etc.).

1. Use the **HTML** as the DOM structure (adapt JSX/templates as needed; keep class names and ids).
2. Include the **Theme** CSS (or map the same variables into the host design system). It switches light/dark with `prefers-color-scheme`.
3. Apply the **CSS** as-is when possible. It uses `var(--text-color)`, `var(--heading-color)`, `var(--background-color)`, etc. from the theme so both modes work.
4. Run the **JavaScript** after the markup mounts. Prefer `DOMContentLoaded` or a framework `useEffect` / `onMount`.
5. Keep motion respectful of `prefers-reduced-motion` if the snippet already checks it.
6. Do not depend on mighil.com runtime or fonts. Theme variables are enough for color.
7. If porting to a component, expose the interactive root via a ref and preserve accessibility attributes (`aria-*`, `role`, `button` types).

## Theme (auto light / dark)

```css
/* Auto light / dark — follows prefers-color-scheme */
:root{
  color-scheme: light dark;
  --text-color: hsl(0, 0%, 18%);
  --heading-color: hsl(0, 0%, 18%);
  --background-color: hsl(0, 0%, 99%);
  --bg-color: hsl(0, 0%, 99%);
  --link-color: hsl(0, 0%, 18%);
  --visited-color: hsl(0, 0%, 18%);
  --blockquote-color: hsl(0, 0%, 18%);
  --code-bg-color: hsl(0, 0%, 95%);
  --code-color: hsl(0, 0%, 18%);
  --sub-color: hsl(0, 0%, 29%);
}
@media (prefers-color-scheme: dark){
:root{
  --text-color: hsl(35, 25%, 85%);
  --heading-color: hsl(35.2, 32.6%, 82.5%);
  --background-color: hsl(60, 2.2%, 8.8%);
  --bg-color: hsl(60, 2.2%, 8.8%);
  --link-color: hsl(34.7, 55.3%, 79.8%);
  --visited-color: hsla(34.7, 55.3%, 79.8%, 0.85);
  --blockquote-color: hsl(35, 30%, 75%);
  --code-bg-color: hsl(30, 5%, 7.8%);
  --code-color: limegreen;
  --sub-color: hsla(34.7, 24.7%, 84.9%, 0.78);
}
}
```

## HTML

```html
<div class="scramble" id="scramble" aria-live="polite">keep it quiet</div>
```

## CSS

```css
.scramble{
  font-family:ui-monospace,SFMono-Regular,Menlo,monospace;
  font-size:1rem;letter-spacing:.04em;min-height:1.5em;min-width:12ch;text-align:center;
}
```

## JavaScript

```js
// Scramble
  const scrambleEl = document.getElementById('scramble');
  const scrambleTarget = 'keep it quiet';
  const glyphs = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789·_+';
  let scrambleTimer = null;
  function runScramble() {
    if (!scrambleEl) return;
    clearInterval(scrambleTimer);
    if (reduce) {
      scrambleEl.textContent = scrambleTarget;
      return;
    }
    let frame = 0;
    scrambleTimer = setInterval(() => {
      frame += 1;
      const revealed = Math.floor(frame / 3);
      let out = '';
      for (let i = 0; i < scrambleTarget.length; i++) {
        if (scrambleTarget[i] === ' ') out += ' ';
        else if (i < revealed) out += scrambleTarget[i];
        else out += glyphs[(Math.random() * glyphs.length) | 0];
      }
      scrambleEl.textContent = out;
      if (revealed >= scrambleTarget.length) {
        clearInterval(scrambleTimer);
        scrambleEl.textContent = scrambleTarget;
      }
    }, 30);
  }

document.querySelectorAll('[data-act]').forEach((btn) => {
    btn.addEventListener('click', (e) => {
      e.stopPropagation();
      const act = btn.getAttribute('data-act');
      if (act === 'scramble') runScramble();
    });
  });
```

---

_Source: https://mighil.com/animations/scramble-decode_
