# Dither Effect

Motionboard shot name: Dither Reveal

Two image cards sit beside a prompt box. When Turn these images into halftone dots is sent, both images dissolve into coarse dots in four colours, and a second prompt brings back the full-colour pictures. A dithered mark then resolves into the Halo lockup.

- Position in a launch film: Product reveal
- Slug: dither-effect
- Preview video: https://media.motionboard.app/dither-reveal.mp4
- Recipe page: https://motionboard.app/recipes/dither-effect/
- License: MIT. Free to use in your own projects, including commercial launch films. https://github.com/krisyangdev/motionboard/blob/main/LICENSE

## How to use this recipe

You are a coding agent. The user wants this shot for their own product. Follow these steps.

1. **Know the product.** If the product is clear from the user's repo or conversation, ask nothing and start. Otherwise ask once for: product name, logo file, brand colors.
2. **Pick the output.** If the user's message says a video or their website, do that. Otherwise ask once which one they want.
   - **Video (HyperFrames).** Create a folder named `dither-reveal`. Download every file under "Source files" below with the curl commands, keeping relative paths. Adapt `index.html` (see "What to adapt"). Run `npx --yes hyperframes@0.8.209 check` on the untouched copy first and note its result. Preview with `npx --yes hyperframes@0.8.209 preview`, render an MP4 with `npx --yes hyperframes@0.8.209 render` (into `renders/`, named with a timestamp). The same commands are in `package.json` as `dev`, `check`, `render`; pass flags after `--`, as in `npm run render -- --quality draft`. Rendering needs network access for web fonts and GSAP.
   - **Website motion.** Port the GSAP timeline in `index.html` into the user's site, using their stack (React, Vue, Svelte, plain HTML). Build the same DOM structure, keep the timeline's durations, delays and easings exactly, and start it when the element scrolls into view (IntersectionObserver or ScrollTrigger). Respect `prefers-reduced-motion: reduce`: show the final frame as a static state and skip the movement.
3. **What to adapt.**
   - **Brand layer** (product name, logo, colors, fonts, copy): replace directly with the user's.
   - **UI layer** (the product screens, cards, windows, controls shown in the shot): rebuild the user's real UI as clean DOM, from their codebase (components, design tokens) or from a screenshot used as reference only. Never paste a screenshot into the shot.
   - **Motion** (timing, easing, camera moves, layering, stagger, order of events): keep exactly as in the source. This is what the recipe is.
   - The timeline targets UI elements by id and class, and camera moves land on fixed coordinates. Keep the ids the timeline uses, keep row counts and text lengths close to the source, and when the layout moves, update the target coordinates without changing the times.
4. **Fonts.** HyperFrames fetches Google Fonts such as Inter on its own. Some sources use macOS local fonts (PingFang, Songti via `local()`). Use the user's brand font when it can be bundled; for a system font such as SF Pro, use Inter. Check that text still fits its boxes.
5. **Verify.** Compare the render with the preview video above at the beat times in the Timing row. The beats, timing and camera should match; only brand and UI content differ.

## Spec

| Item | Value |
| --- | --- |
| Timing | Film 11s. Prompt 1 types from 0.55s (about 1.6s) and is sent at 2.25s; the dissolve runs 2.45–3.8s (card B starts 0.12s after card A); hold 3.8–4.25s; prompt 2 types from 4.25s and is sent at 5.3s; the resolve runs 5.4–6.7s; full colour holds to 7.1s; the UI fades 7.1–7.6s; the mark appears at 7.3s and resolves 7.55–8.25s; the wordmark enters at 8.0s and the tagline at 8.12s, held to the end (2.3s) |
| Easing | Dissolve and resolve progress `power2.inOut`; cell size steps are discrete (3, 6, 10, 16px at 2.45, 2.8, 3.1 and 3.4s), then the resolve shrinks the cell as 16 × (1 − k)^1.6, rounded, down to 2px; the mark resolves with power3.inOut; cards and prompt box enter with expo.out (0.8s); the chat scrolls 0.65s power3.inOut; the send button presses with back.out(3); the UI fades with power2.in; wordmark and tagline power3.out (0.55s) |
| Camera | Locked flat frame with no camera move; the move is in the pixels. The UI scales to 0.985 as it fades at 7.1s (0.5s), and the mark and wordmark sit centred on the empty frame |
| Subject | Two image cards 560x672px, corner radius 18px, at x80 and x680, y186, each drawn on a canvas from a 500x600 source (a dusk ridge and a sunlit dune, both gradients and paths, no photos) at 2x. A cell is the average colour of a square of the source, read from an integral image. In dither state each cell gets (Bayer 8x8 value − 0.5) × 120 added to every channel and snaps to the nearest of 4 palette colours. A cell turns into dither when progress passes 0.4 × its Bayer value + 0.6 × its sweep position (0.75 × x + 0.25 × y), so the change sweeps left to right with a grain. Cells are squares up to 5px and circles from 13px, and from 3px up their size shrinks with luminance. The chat column is 540px wide at x1280 with a 540x200px prompt box at y700, a 62px send button and bubbles at 32px. The Halo mark is an orange ring (276px across, 48px stroke) with a cream bead, drawn on a 440px canvas with the same dither at 16px cells down to 2px |
| Sync | The dissolve starts 0.2s after the send press at 2.25s; the readout pill cell N px changes on the same frame as each cell-size step; the reply line changes from Applying halftone to Halftone applied to both at 3.8s, the frame where every cell is dotted; the resolve starts 0.1s after the second send at 5.3s; at 6.7s the pill reads cell 0 px and full colour and the reply reads Full colour restored.; the mark starts to resolve 0.25s after the UI has begun to fade |
| Style | Near-black `#09090b`, one accent orange `#ff7a45`; Inter, headline 76px 700, wordmark 136px 700, tagline 38px; dusk palette #080c24 #3a4ab8 #f2814f #ffe9cf, dune palette #1d1018 #3d7bd9 #ee8c44 #ffeccc, mark palette #09090b #7a2a12 #ff7a45 #ffe6cc; a static 7% film grain and a vignette |

## Why it works

An ordered dither is a pattern with no randomness, so the same image always breaks into the same dots and the change can be seeked to any frame. Each cell switches when the progress passes a threshold made of its Bayer value and its position, so the dissolve has a grain and also a direction, left to right, and the eye reads it as a scan. Stepping the cell size from fine to coarse (3, 6, 10, 16px) keeps the image recognisable at first and turns it into a graphic at the end; stepping it back down is what makes the resolve read as the image returning. Colour is cut to 4 per image, so the two cards still look like a dusk and a noon. The change is a response to a typed sentence, the prompt sits next to the images and the reply line shows the current cell size, so the effect looks like something the product did on request. The frame stays locked, the cards are large and the only accent is the orange of the send button, the caret and the final mark, so the dots are the subject.

## Prompt

> Build an 11-second, 1920x1080, 30 fps product film called Dither Reveal for a fictional product, Halo (an AI image tool). Use one GSAP timeline and draw all pixels on canvas 2D from the timeline time (no WebGL, no random, no Date). The page is near-black #09090b with one accent orange #ff7a45, Inter for all text, a vignette and a static 7% film grain. The frame is locked: the move is in the pixels. Layout: a 76px headline, Restyle any image with one sentence., at the top left. Two image cards 560x672px with an 18px corner radius at x80 and x680, y186, each with a 30px title and a 26px caption under it (Dusk ridge, Alpine light, 4:5; Sunlit dune, Desert noon, 4:5). Draw each image yourself on a canvas of 1000x1200 (a 500x600 source at 2x): a dusk ridge made of a night-to-orange sky gradient, 70 seeded stars, a sun glow and three mountain layers with a rim light; a sunlit dune made of a blue-to-cream sky, a sun disc and three dune layers with shadow on the faces that turn away from the sun. On the right, a chat column 540px wide at x1280: a thread area above and a 540x200px prompt box at y700 with 32px text, a model chip Halo 2.1, and a 62px orange send button. Bubbles are 32px, replies carry a small orange ring avatar and two 26px pills (cell N px, 4 colours). Dither engine: sample the 500x600 source into an integral image. For a cell size s, each cell is the average colour of an s by s square. A cell is in dither state when progress p is greater than 0.4 × bayer8(ix, iy) + 0.6 × sweep, where sweep = 0.75 × x + 0.25 × y (0 to 1, left to right; use 1 − sweep for the resolve). In dither state add (bayer − 0.5) × 120 to every channel, snap to the nearest of 4 palette colours, clear the cell to the card background and draw a shape: a square up to 5px, a circle from 13px, a circle size that shrinks with luminance from 3px up. Palettes: dusk #080c24 #3a4ab8 #f2814f #ffe9cf, dune #1d1018 #3d7bd9 #ee8c44 #ffeccc. Redraw both cards in an onUpdate from the timeline time. Timeline: 0 to 0.8s headline, cards and prompt box enter with expo.out. Type Turn these images into halftone dots from 0.55s with an orange caret (about 1.6s) and press send at 2.25s: the button dips to 0.86 and returns with back.out(3), the text moves into a bubble and a reply appears reading Applying halftone with the pill cell 3 px. The dissolve runs 2.45–3.8s with power2.inOut progress and discrete cell sizes 3, 6, 10, 16px at 2.45, 2.8, 3.1 and 3.4s; card B starts 0.12s later. At 3.8s the reply changes to Halftone applied to both and the cards hold as coarse dots to 4.25s. Type Back to full colour from 4.25s and send at 5.3s; the thread scrolls up 300px in 0.65s. The resolve runs 5.4–6.7s: progress goes from 1 to 0 and the cell size falls as 16 × (1 − k)^1.6, rounded, down to 2px, the pill counts down and ends at cell 0 px and full colour, and the reply reads Full colour restored. Hold full colour to 7.1s. Ending: fade the whole UI out 7.1–7.6s (scale to 0.985). At 7.3s fade in a 440px canvas that draws the Halo mark (an orange ring (276px across, 48px stroke) with a cream bead in a gap, a soft orange glow) already dithered at 16px cells, and resolve it with the same engine over 7.55–8.25s (power3.inOut, cell size 16 down to 2px). Enter the word Halo (Inter 700, 136px) at 8.0s and the tagline Describe it. Halo makes the image. (38px) at 8.12s, both 0.55s power3.out, and hold the lockup still to 11s. Seed any random value with mulberry32.

## Often confused with

- [Dot Matrix Animation](https://motionboard.app/r/dot-matrix-animation.md): Dot Matrix Animation quantizes one graphic onto a circular dot display and every frame snaps to that grid; in Dither Effect two full images dissolve into ordered-dither dots at changing cell sizes and resolve back.
- [X-Ray Effect](https://motionboard.app/r/x-ray-effect.md): X-Ray Effect cross-dissolves two renders of one device from the same camera position; here one image is rebuilt from cells whose size and colour count change, then returns to the original.

## Source files

Runnable HyperFrames project, 3 files.

- https://motionboard.app/source/dither-reveal/hyperframes.json (353 B)
- https://motionboard.app/source/dither-reveal/index.html (28.8 KB)
- https://motionboard.app/source/dither-reveal/package.json (302 B)

Download all of them:

```sh
curl -fsSL --create-dirs -o "dither-reveal/hyperframes.json" "https://motionboard.app/source/dither-reveal/hyperframes.json"
curl -fsSL --create-dirs -o "dither-reveal/index.html" "https://motionboard.app/source/dither-reveal/index.html"
curl -fsSL --create-dirs -o "dither-reveal/package.json" "https://motionboard.app/source/dither-reveal/package.json"
```

## index.html

```html
<!doctype html>
<html lang="en" data-resolution="landscape">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=1920, height=1080" />
    <script src="https://cdn.jsdelivr.net/npm/gsap@3.14.2/dist/gsap.min.js"></script>
    <style>
      :root {
        --bg: #09090b;
        --ink: #f4f4f6;
        --muted: #a7aab6;
        --dim: #9296a2;
        --accent: #ff7a45;
        --line: #262932;
        --panel: #14151a;
        --font-ui: "Inter", -apple-system, "Helvetica Neue", sans-serif;
        --font-mono: "JetBrains Mono", "SF Mono", Menlo, monospace;
      }
      * { margin: 0; padding: 0; box-sizing: border-box; }
      html, body { width: 1920px; height: 1080px; overflow: hidden; background: var(--bg); }
      #root {
        position: relative; width: 1920px; height: 1080px; overflow: hidden;
        background: radial-gradient(1400px 800px at 32% 38%, #101117 0%, #09090b 70%);
        font-family: var(--font-ui); color: var(--ink); -webkit-font-smoothing: antialiased;
      }
      .full { position: absolute; left: 0; top: 0; width: 1920px; height: 1080px; }

      /* ---------- headline ---------- */
      #head {
        position: absolute; left: 100px; top: 54px; width: 1720px;
        font-size: 76px; line-height: 92px; font-weight: 700; letter-spacing: -0.03em; color: var(--ink);
        white-space: nowrap; opacity: 0;
      }

      /* ---------- image cards ---------- */
      .card { position: absolute; top: 186px; width: 560px; opacity: 0; }
      #cardA { left: 80px; }
      #cardB { left: 680px; }
      .frame {
        position: relative; width: 560px; height: 672px; border-radius: 18px; overflow: hidden;
        background: #0b0c10; border: 1px solid rgba(255, 255, 255, 0.09);
        box-shadow: 0 30px 60px rgba(0, 0, 0, 0.45);
      }
      .frame canvas { position: absolute; left: 0; top: 0; width: 560px; height: 672px; display: block; }
      .cap { position: relative; margin-top: 18px; }
      .cap b { display: block; font-size: 30px; line-height: 38px; font-weight: 600; letter-spacing: -0.01em; color: var(--ink); }
      .cap span { display: block; margin-top: 2px; font-size: 26px; line-height: 34px; font-weight: 400; color: var(--muted); }

      /* ---------- chat column ---------- */
      #col { position: absolute; left: 1280px; top: 0; width: 540px; height: 1080px; }
      #threadWrap {
        position: absolute; left: 0; top: 196px; width: 540px; height: 464px; overflow: hidden;
        -webkit-mask-image: linear-gradient(to bottom, transparent 0, #000 56px, #000 100%);
        mask-image: linear-gradient(to bottom, transparent 0, #000 56px, #000 100%);
      }
      #thread { position: absolute; left: 0; top: 0; width: 540px; height: 800px; }
      .bubble {
        position: absolute; right: 0; max-width: 520px; padding: 22px 28px;
        background: #1b1d24; border: 1px solid #2b2e38; border-radius: 26px 26px 8px 26px;
        font-size: 32px; line-height: 42px; font-weight: 400; color: var(--ink); opacity: 0;
        transform-origin: 100% 100%;
      }
      .reply { position: absolute; left: 0; width: 540px; opacity: 0; display: flex; gap: 18px; align-items: flex-start; }
      .av {
        flex: 0 0 52px; width: 52px; height: 52px; border-radius: 50%; background: #1b1d24; border: 1px solid #2b2e38;
        display: flex; align-items: center; justify-content: center;
      }
      .rb { flex: 1 1 auto; }
      .rt { position: relative; height: 42px; font-size: 32px; line-height: 42px; font-weight: 500; letter-spacing: -0.01em; color: var(--ink); white-space: nowrap; }
      .rt span { position: absolute; left: 0; top: 0; }
      .rt .b { opacity: 0; }
      .pills { margin-top: 14px; display: flex; gap: 12px; }
      .pill {
        font-family: var(--font-mono); font-size: 26px; line-height: 34px; padding: 6px 16px; border-radius: 999px;
        background: rgba(255, 122, 69, 0.12); border: 1px solid rgba(255, 122, 69, 0.38); color: #ffb08c; white-space: nowrap;
      }
      .pill.plain { background: #17181e; border-color: #2b2e38; color: var(--muted); }

      /* ---------- prompt box ---------- */
      #pbox {
        position: absolute; left: 0; top: 700px; width: 540px; height: 200px; border-radius: 30px; opacity: 0;
        background: var(--panel); border: 1px solid #2a2d36; box-shadow: 0 24px 50px rgba(0, 0, 0, 0.4);
      }
      .ptxt { position: absolute; left: 30px; top: 26px; width: 480px; font-size: 32px; line-height: 44px; font-weight: 400; color: var(--ink); }
      #ph { color: var(--dim); }
      .w { display: inline-block; white-space: nowrap; }
      .ch { position: relative; opacity: 0; }
      .ch i, .c0 i { position: absolute; top: 5px; width: 3px; height: 36px; background: var(--accent); opacity: 0; }
      .ch i { right: -4px; }
      .c0 { position: relative; display: inline-block; width: 0; height: 40px; vertical-align: top; }
      .c0 i { left: 0; }
      #chip {
        position: absolute; left: 26px; bottom: 22px; height: 46px; padding: 0 16px; border-radius: 999px;
        font-size: 26px; line-height: 46px; font-weight: 500; color: var(--muted); background: #1a1c22; border: 1px solid #2b2e38;
        display: flex; align-items: center; gap: 10px;
      }
      #send {
        position: absolute; right: 24px; bottom: 20px; width: 62px; height: 62px; border-radius: 50%; background: var(--accent);
        display: flex; align-items: center; justify-content: center; box-shadow: 0 0 0 0 rgba(255, 122, 69, 0.0);
      }

      /* ---------- lockup ---------- */
      #markc { position: absolute; left: 740px; top: 150px; width: 440px; height: 440px; opacity: 0; }
      #markc canvas { display: block; width: 440px; height: 440px; }
      #word {
        position: absolute; left: 0; top: 612px; width: 1920px; text-align: center; opacity: 0;
        font-size: 136px; line-height: 150px; font-weight: 700; letter-spacing: -0.035em; color: var(--ink);
      }
      #tag {
        position: absolute; left: 0; top: 794px; width: 1920px; text-align: center; opacity: 0;
        font-size: 38px; line-height: 48px; font-weight: 400; letter-spacing: -0.005em; color: var(--muted);
      }

      #vig {
        pointer-events: none;
        background: radial-gradient(ellipse at 50% 48%, rgba(0, 0, 0, 0) 58%, rgba(0, 0, 0, 0.5) 100%);
      }
      #grain {
        pointer-events: none; opacity: 0.07; mix-blend-mode: screen;
        background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='300' height='300'><filter id='n'><feTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2' seed='4' stitchTiles='stitch'/><feColorMatrix values='0 0 0 0 1  0 0 0 0 1  0 0 0 0 1  0 0 0 0.9 0'/></filter><rect width='300' height='300' filter='url(%23n)'/></svg>");
      }
    </style>
  </head>
  <body>
    <div id="root" data-composition-id="main" data-start="0" data-duration="11" data-width="1920" data-height="1080">
      <div id="ui" class="clip full" data-start="0" data-duration="11" data-track-index="0" data-layout-allow-overlap>
        <div id="head">Restyle any image with one sentence.</div>

        <div class="card" id="cardA" data-layout-allow-overlap>
          <div class="frame"><canvas id="cvA" width="1000" height="1200"></canvas></div>
          <div class="cap"><b>Dusk ridge</b><span>Alpine light, 4:5</span></div>
        </div>
        <div class="card" id="cardB" data-layout-allow-overlap>
          <div class="frame"><canvas id="cvB" width="1000" height="1200"></canvas></div>
          <div class="cap"><b>Sunlit dune</b><span>Desert noon, 4:5</span></div>
        </div>

        <div id="col" data-layout-allow-overlap>
          <div id="threadWrap">
            <div id="thread">
              <div class="bubble" id="b1" style="top: 0px">Turn these images into halftone dots</div>
              <div class="reply" id="r1" style="top: 150px">
                <div class="av">
                  <svg width="30" height="30" viewBox="0 0 30 30"><circle cx="15" cy="15" r="10" fill="none" stroke="#ff7a45" stroke-width="4.5"/></svg>
                </div>
                <div class="rb">
                  <div class="rt"><span class="a" id="r1a">Applying halftone</span><span class="b" id="r1b">Halftone applied to both.</span></div>
                  <div class="pills"><span class="pill" id="p1">cell 3 px</span><span class="pill plain">4 colours</span></div>
                </div>
              </div>
              <div class="bubble" id="b2" style="top: 336px">Back to full colour</div>
              <div class="reply" id="r2" style="top: 446px">
                <div class="av">
                  <svg width="30" height="30" viewBox="0 0 30 30"><circle cx="15" cy="15" r="10" fill="none" stroke="#ff7a45" stroke-width="4.5"/></svg>
                </div>
                <div class="rb">
                  <div class="rt"><span class="a" id="r2a">Restoring full colour</span><span class="b" id="r2b">Full colour restored.</span></div>
                  <div class="pills"><span class="pill" id="p3">cell 16 px</span><span class="pill plain" id="p4">4 colours</span></div>
                </div>
              </div>
            </div>
          </div>

          <div id="pbox">
            <div class="ptxt" id="ph">Ask Halo to restyle your images</div>
            <div class="ptxt" id="txt1"></div>
            <div class="ptxt" id="txt2"></div>
            <div id="chip"><span>Halo 2.1</span>
              <svg width="16" height="16" viewBox="0 0 16 16"><path d="M3 6l5 5 5-5" fill="none" stroke="#a7aab6" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>
            </div>
            <div id="send">
              <svg width="30" height="30" viewBox="0 0 30 30"><path d="M15 24V7M7.5 14.5L15 7l7.5 7.5" fill="none" stroke="#1a0d06" stroke-width="3.4" stroke-linecap="round" stroke-linejoin="round"/></svg>
            </div>
          </div>
        </div>
      </div>

      <div id="endl" class="clip full" data-start="0" data-duration="11" data-track-index="1" data-layout-allow-overlap>
        <div id="markc"><canvas id="cvM" width="880" height="880"></canvas></div>
        <div id="word">Halo</div>
        <div id="tag">Describe it. Halo makes the image.</div>
      </div>

      <div id="vig" class="clip full" data-start="0" data-duration="11" data-track-index="2" data-layout-allow-overlap></div>
      <div id="grain" class="clip full" data-start="0" data-duration="11" data-track-index="3" data-layout-allow-overlap></div>
    </div>

    <script>
      const DUR = 11;
      const tl = gsap.timeline({ paused: true });

      /* ---------- helpers ---------- */
      const clamp01 = (x) => Math.min(1, Math.max(0, x));
      const seg = (t, a, b) => clamp01((t - a) / (b - a));
      const pow2io = (k) => (k < 0.5 ? 2 * k * k : 1 - Math.pow(-2 * k + 2, 2) / 2);
      const pow3io = (k) => (k < 0.5 ? 4 * k * k * k : 1 - Math.pow(-2 * k + 2, 3) / 2);
      function mulberry32(a) {
        return function () {
          a |= 0; a = (a + 0x6d2b79f5) | 0;
          let t = Math.imul(a ^ (a >>> 15), 1 | a);
          t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
          return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
        };
      }
      const mk = (w, h) => { const c = document.createElement("canvas"); c.width = w; c.height = h; return c; };

      /* ---------- key times ---------- */
      const T_TYPE1 = 0.55, SEND1 = 2.25;
      const DIS0 = 2.45, DIS1 = 3.8;
      const T_TYPE2 = 4.25, SEND2 = 5.3;
      const RES0 = 5.4, RES1 = 6.7;
      const UI_OUT = 7.1;
      const MK_IN = 7.3, MK0 = 7.55, MK1 = 8.25;

      /* ================= source images (drawn at 2x on canvas) ================= */
      const IW = 500, IH = 600;
      const ridgeFn = (base, amp, f, ph, a2, f2, p2) => (x) => base - amp * (1 - Math.abs(Math.sin(x * f + ph))) + a2 * Math.sin(x * f2 + p2);

      function drawDusk(g) {
        g.save(); g.scale(2, 2);
        let gr = g.createLinearGradient(0, 0, 0, 420);
        gr.addColorStop(0, "#0a1034"); gr.addColorStop(0.32, "#232a78"); gr.addColorStop(0.58, "#7a4a9e");
        gr.addColorStop(0.82, "#ff9566"); gr.addColorStop(1, "#ffd0a0");
        g.fillStyle = gr; g.fillRect(0, 0, IW, IH);
        // stars
        const r = mulberry32(21);
        for (let i = 0; i < 70; i++) {
          const x = r() * IW, y = r() * 210, a = 0.25 + r() * 0.65, rad = 0.7 + r() * 1.0;
          g.fillStyle = "rgba(255,245,230," + a.toFixed(2) + ")";
          g.beginPath(); g.arc(x, y, rad, 0, Math.PI * 2); g.fill();
        }
        // sun glow + disc
        gr = g.createRadialGradient(345, 330, 0, 345, 330, 230);
        gr.addColorStop(0, "rgba(255,236,205,1)"); gr.addColorStop(0.2, "rgba(255,170,115,0.85)"); gr.addColorStop(1, "rgba(255,120,90,0)");
        g.fillStyle = gr; g.fillRect(0, 100, IW, 460);
        g.fillStyle = "#fff1d8"; g.beginPath(); g.arc(345, 330, 36, 0, Math.PI * 2); g.fill();
        const layer = (fn, c0, c1, y0, y1) => {
          g.beginPath(); g.moveTo(0, IH);
          for (let x = 0; x <= IW; x += 3) g.lineTo(x, fn(x));
          g.lineTo(IW, IH); g.closePath();
          const lg = g.createLinearGradient(0, y0, 0, y1); lg.addColorStop(0, c0); lg.addColorStop(1, c1);
          g.fillStyle = lg; g.fill();
        };
        const rim = (fn, w, a) => {
          g.beginPath();
          for (let x = 0; x <= IW; x += 3) { if (x === 0) g.moveTo(x, fn(x)); else g.lineTo(x, fn(x)); }
          const lg = g.createLinearGradient(0, 0, IW, 0);
          lg.addColorStop(0, "rgba(255,170,110,0.05)"); lg.addColorStop(0.68, "rgba(255,190,130," + a + ")"); lg.addColorStop(1, "rgba(255,170,110,0.1)");
          g.strokeStyle = lg; g.lineWidth = w; g.stroke();
        };
        const yb = ridgeFn(392, 132, 0.0105, 0.7, 10, 0.045, 1.2);
        const ym = ridgeFn(474, 108, 0.0143, 2.1, 8, 0.05, 0.4);
        const yf = ridgeFn(548, 62, 0.0185, 4.0, 6, 0.06, 2.3);
        layer(yb, "#6a509f", "#2a2c74", 260, 500); rim(yb, 2.4, 0.9);
        // mist between ridges
        gr = g.createLinearGradient(0, 410, 0, 500);
        gr.addColorStop(0, "rgba(255,160,130,0)"); gr.addColorStop(0.55, "rgba(255,160,130,0.28)"); gr.addColorStop(1, "rgba(255,160,130,0)");
        g.fillStyle = gr; g.fillRect(0, 410, IW, 90);
        layer(ym, "#262a6e", "#10154a", 360, 560); rim(ym, 2.2, 0.55);
        layer(yf, "#10143c", "#070a24", 490, 600);
        g.restore();
      }

      function drawDune(g) {
        g.save(); g.scale(2, 2);
        let gr = g.createLinearGradient(0, 0, 0, 380);
        gr.addColorStop(0, "#2f6fd0"); gr.addColorStop(0.4, "#7fb2ec"); gr.addColorStop(0.78, "#ffdcae"); gr.addColorStop(1, "#ffc88c");
        g.fillStyle = gr; g.fillRect(0, 0, IW, IH);
        gr = g.createRadialGradient(150, 175, 0, 150, 175, 260);
        gr.addColorStop(0, "rgba(255,246,220,1)"); gr.addColorStop(0.25, "rgba(255,236,190,0.7)"); gr.addColorStop(1, "rgba(255,220,160,0)");
        g.fillStyle = gr; g.fillRect(0, 0, IW, 460);
        g.fillStyle = "#fff7e0"; g.beginPath(); g.arc(150, 175, 46, 0, Math.PI * 2); g.fill();
        const dune = (fn, c0, c1, y0, y1, shadeDepth, shadeAlpha) => {
          g.beginPath(); g.moveTo(0, IH);
          for (let x = 0; x <= IW; x += 2) g.lineTo(x, fn(x));
          g.lineTo(IW, IH); g.closePath();
          const lg = g.createLinearGradient(0, y0, 0, y1); lg.addColorStop(0, c0); lg.addColorStop(1, c1);
          g.fillStyle = lg; g.fill();
          // shadow on the faces that turn away from the sun (left light)
          for (let x = 0; x < IW; x += 2) {
            const sl = fn(x + 2) - fn(x);
            if (sl > 0.25) {
              const a = Math.min(shadeAlpha, sl * 0.34), y = fn(x), h = shadeDepth * Math.min(1, sl / 2.2);
              const sg = g.createLinearGradient(0, y, 0, y + h);
              sg.addColorStop(0, "rgba(96,30,34," + a.toFixed(2) + ")"); sg.addColorStop(1, "rgba(96,30,34,0)");
              g.fillStyle = sg; g.fillRect(x, y, 2.2, h);
            }
          }
        };
        const y1 = (x) => 392 + 34 * Math.sin(x * 0.0085 + 0.5) + 16 * Math.sin(x * 0.021 + 2.0) - 36 * (x / IW);
        const y2 = (x) => 462 + 40 * Math.sin(x * 0.0072 + 2.6) + 14 * Math.sin(x * 0.019 + 0.3) - 14 * (x / IW);
        const y3 = (x) => 538 + 30 * Math.sin(x * 0.0093 + 4.4) + 10 * Math.sin(x * 0.024 + 1.1);
        dune(y1, "#ffc782", "#e88d4a", 360, 520, 130, 0.78);
        dune(y2, "#f6a45c", "#d06a30", 430, 600, 150, 0.82);
        dune(y3, "#e58140", "#8f3b22", 510, 600, 90, 0.7);
        // ripples on the front dune
        g.strokeStyle = "rgba(110,40,24,0.22)"; g.lineWidth = 1.4;
        for (let k = 0; k < 6; k++) {
          g.beginPath();
          for (let x = 0; x <= IW; x += 4) { const y = y3(x) + 14 + k * 9; if (x === 0) g.moveTo(x, y); else g.lineTo(x, y); }
          g.stroke();
        }
        g.restore();
      }

      function drawMark(g) {
        g.save(); g.scale(2, 2);
        const gr = g.createRadialGradient(220, 220, 0, 220, 220, 215);
        gr.addColorStop(0, "rgba(255,120,60,0.34)"); gr.addColorStop(0.6, "rgba(255,110,50,0.14)"); gr.addColorStop(1, "rgba(255,110,50,0)");
        g.fillStyle = gr; g.fillRect(0, 0, 440, 440);
        const lg = g.createLinearGradient(70, 70, 370, 370);
        lg.addColorStop(0, "#ffe3c6"); lg.addColorStop(0.45, "#ff8a4c"); lg.addColorStop(1, "#e24e1a");
        g.strokeStyle = lg; g.lineWidth = 48; g.beginPath(); g.arc(220, 220, 138, 0, Math.PI * 2); g.stroke();
        const bx = 220 + 138 * Math.cos(-Math.PI / 4), by = 220 + 138 * Math.sin(-Math.PI / 4);
        g.fillStyle = "#09090b"; g.beginPath(); g.arc(bx, by, 42, 0, Math.PI * 2); g.fill();
        g.fillStyle = "#fff0dc"; g.beginPath(); g.arc(bx, by, 25, 0, Math.PI * 2); g.fill();
        g.restore();
      }

      /* ================= ordered-dither / halftone renderer ================= */
      const BAYER = (() => {
        let m = [[0]];
        while (m.length < 8) {
          const s = m.length, r = [], q = [[0, 2], [3, 1]];
          for (let y = 0; y < 2 * s; y++) { r[y] = []; for (let x = 0; x < 2 * s; x++) r[y][x] = 4 * m[y % s][x % s] + q[Math.floor(y / s)][Math.floor(x / s)]; }
          m = r;
        }
        return m.map((row) => row.map((v) => (v + 0.5) / 64));
      })();
      const hex = (h) => [parseInt(h.slice(1, 3), 16), parseInt(h.slice(3, 5), 16), parseInt(h.slice(5, 7), 16)];

      function makeLayer(canvas, drawFn, w, h, palette, opts) {
        const src = mk(w * 2, h * 2);
        const sg = src.getContext("2d");
        drawFn(sg);
        // flattened 1x copy for sampling (composited on the layer background)
        const flat = mk(w, h);
        const fg = flat.getContext("2d");
        fg.fillStyle = opts.bg; fg.fillRect(0, 0, w, h);
        fg.drawImage(src, 0, 0, w, h);
        const data = fg.getImageData(0, 0, w, h).data;
        const W1 = w + 1;
        const I = [new Float64Array(W1 * (h + 1)), new Float64Array(W1 * (h + 1)), new Float64Array(W1 * (h + 1))];
        for (let y = 0; y < h; y++) {
          let r0 = 0, r1 = 0, r2 = 0;
          for (let x = 0; x < w; x++) {
            const k = (y * w + x) * 4;
            r0 += data[k]; r1 += data[k + 1]; r2 += data[k + 2];
            const i = (y + 1) * W1 + (x + 1), up = y * W1 + (x + 1);
            I[0][i] = I[0][up] + r0; I[1][i] = I[1][up] + r1; I[2][i] = I[2][up] + r2;
          }
        }
        const pal = palette.map(hex);
        const bgc = hex(opts.bg);
        const ctx = canvas.getContext("2d");
        const cols = palette.slice();
        const Wc = canvas.width, Hc = canvas.height;
        const sc = Wc / w;
        const rects = [];
        const shapes = palette.map(() => []);

        function avg(x0, y0, x1, y1, out) {
          const a = (x1 - x0) * (y1 - y0);
          for (let c = 0; c < 3; c++) {
            const A = I[c];
            out[c] = (A[y1 * W1 + x1] - A[y0 * W1 + x1] - A[y1 * W1 + x0] + A[y0 * W1 + x0]) / a;
          }
        }
        const rgb = [0, 0, 0];
        return function render(p, s, dir) {
          ctx.clearRect(0, 0, Wc, Hc);
          ctx.drawImage(src, 0, 0);
          if (p <= 0.0005) return;
          const nx = Math.ceil(w / s), ny = Math.ceil(h / s);
          const k = clamp01((s - 5) / 8);           // 0 square -> 1 circle
          const sizeMix = clamp01((s - 3) / 6);     // how much luminance drives dot size
          rects.length = 0;
          for (let i = 0; i < shapes.length; i++) shapes[i].length = 0;
          for (let iy = 0; iy < ny; iy++) {
            const y0 = Math.min(h - 1, Math.floor(iy * s)), y1 = Math.min(h, Math.max(y0 + 1, Math.floor((iy + 1) * s)));
            for (let ix = 0; ix < nx; ix++) {
              const bay = BAYER[iy & 7][ix & 7];
              const xw = clamp01(0.75 * ((ix + 0.5) * s / w) + 0.25 * ((iy + 0.5) * s / h)) * 0.999;
              const thr = 0.4 * bay + 0.6 * (dir > 0 ? xw : 1 - xw);
              if (!(p > thr)) continue;
              const x0 = Math.min(w - 1, Math.floor(ix * s)), x1 = Math.min(w, Math.max(x0 + 1, Math.floor((ix + 1) * s)));
              avg(x0, y0, x1, y1, rgb);
              const lum = (0.2126 * rgb[0] + 0.7152 * rgb[1] + 0.0722 * rgb[2]) / 255;
              const off = (bay - 0.5) * opts.spread;
              let best = 0, bd = 1e12;
              for (let c = 0; c < pal.length; c++) {
                const dr = rgb[0] + off - pal[c][0], dg = rgb[1] + off - pal[c][1], db = rgb[2] + off - pal[c][2];
                const d = dr * dr + dg * dg + db * db;
                if (d < bd) { bd = d; best = c; }
              }
              const X0 = x0 * sc, Y0 = y0 * sc, X1 = x1 * sc, Y1 = y1 * sc;
              rects.push(X0, Y0, X1 - X0, Y1 - Y0);
              if (opts.skip0 && best === 0) continue;
              const f = 1 - sizeMix + sizeMix * (0.52 + 0.48 * Math.pow(lum, 0.7));
              shapes[best].push(X0, Y0, X1 - X0, Y1 - Y0, f);
            }
          }
          ctx.fillStyle = opts.bg;
          ctx.beginPath();
          for (let i = 0; i < rects.length; i += 4) ctx.rect(rects[i], rects[i + 1], rects[i + 2], rects[i + 3]);
          ctx.fill();
          for (let c = 0; c < shapes.length; c++) {
            const L = shapes[c];
            if (!L.length) continue;
            ctx.fillStyle = cols[c];
            ctx.beginPath();
            for (let i = 0; i < L.length; i += 5) {
              const cw = L[i + 2], ch = L[i + 3], f = L[i + 4];
              const dw = cw * f, dh = ch * f, dx = L[i] + (cw - dw) / 2, dy = L[i + 1] + (ch - dh) / 2;
              if (k < 0.03) { ctx.rect(dx, dy, dw, dh); continue; }
              const rr = Math.min(dw, dh) / 2 * k;
              ctx.moveTo(dx + rr, dy);
              ctx.arcTo(dx + dw, dy, dx + dw, dy + dh, rr);
              ctx.arcTo(dx + dw, dy + dh, dx, dy + dh, rr);
              ctx.arcTo(dx, dy + dh, dx, dy, rr);
              ctx.arcTo(dx, dy, dx + dw, dy, rr);
              ctx.closePath();
            }
            ctx.fill();
          }
        };
      }

      const renderA = makeLayer(document.getElementById("cvA"), drawDusk, IW, IH,
        ["#080c24", "#3a4ab8", "#f2814f", "#ffe9cf"], { bg: "#0b0c10", spread: 120 });
      const renderB = makeLayer(document.getElementById("cvB"), drawDune, IW, IH,
        ["#1d1018", "#3d7bd9", "#ee8c44", "#ffeccc"], { bg: "#0b0c10", spread: 120 });
      const renderM = makeLayer(document.getElementById("cvM"), drawMark, 440, 440,
        ["#09090b", "#7a2a12", "#ff7a45", "#ffe6cc"], { bg: "#09090b", spread: 120, skip0: true });

      /* ================= state from timeline time ================= */
      function cardState(t) {
        if (t < RES0) {
          const k = seg(t, DIS0, DIS1);
          const s = t < DIS0 + 0.35 ? 3 : t < DIS0 + 0.65 ? 6 : t < DIS0 + 0.95 ? 10 : 16;
          return { p: pow2io(k), s, dir: 1 };
        }
        const k = seg(t, RES0, RES1);
        return { p: 1 - pow2io(k), s: Math.max(2, Math.round(16 * Math.pow(1 - k, 1.6))), dir: -1 };
      }
      const el = (id) => document.getElementById(id);
      const txt = {};
      function setText(id, s) { if (txt[id] !== s) { txt[id] = s; el(id).textContent = s; } }

      function render(t) {
        const a = cardState(t), b = cardState(t - 0.12);
        if (t < UI_OUT + 0.6) {
          renderA(a.p, a.s, a.dir);
          renderB(b.p, b.s, b.dir);
        }
        setText("p1", "cell " + (t < DIS0 ? 3 : a.s) + " px");
        if (t < RES0) { setText("p3", "cell 16 px"); setText("p4", "4 colours"); }
        else if (t >= RES1) { setText("p3", "cell 0 px"); setText("p4", "full colour"); }
        else { setText("p3", "cell " + a.s + " px"); setText("p4", "4 colours"); }
        if (t >= MK_IN - 0.05) {
          const k = seg(t, MK0, MK1);
          const p = 1 - pow3io(k);
          renderM(p, Math.max(2, Math.round(16 * Math.pow(1 - k, 1.5))), -1);
        }
      }

      /* ================= typing ================= */
      const rng = mulberry32(7);
      function typeInto(hostId, text, t0) {
        const host = el(hostId);
        host.innerHTML = "";
        const c0 = document.createElement("span"); c0.className = "c0";
        const c0i = document.createElement("i"); c0.appendChild(c0i); host.appendChild(c0);
        let prev = c0i, t = t0;
        tl.set(c0i, { opacity: 1 }, t0 - 0.2);
        text.split(" ").forEach((word, wi, arr) => {
          const w = document.createElement("span"); w.className = "w";
          [...word].forEach((chr) => {
            const s = document.createElement("span"); s.className = "ch"; s.textContent = chr;
            const i = document.createElement("i"); s.appendChild(i);
            w.appendChild(s);
            t += 0.034 + rng() * 0.026;
            tl.set(s, { opacity: 1 }, t);
            tl.set(prev, { opacity: 0 }, t);
            tl.set(i, { opacity: 1 }, t);
            prev = i;
          });
          host.appendChild(w);
          if (wi < arr.length - 1) { host.appendChild(document.createTextNode(" ")); t += 0.03; }
        });
        return t;
      }

      /* ================= timeline ================= */
      // intro
      tl.set("#head", { opacity: 0, y: 16 }, 0);
      tl.set("#cardA", { opacity: 0, y: 30 }, 0);
      tl.set("#cardB", { opacity: 0, y: 30 }, 0);
      tl.set("#pbox", { opacity: 0, y: 30 }, 0);
      tl.to("#head", { opacity: 1, y: 0, duration: 0.7, ease: "power3.out" }, 0.05);
      tl.to("#cardA", { opacity: 1, y: 0, duration: 0.8, ease: "expo.out" }, 0.15);
      tl.to("#cardB", { opacity: 1, y: 0, duration: 0.8, ease: "expo.out" }, 0.27);
      tl.to("#pbox", { opacity: 1, y: 0, duration: 0.8, ease: "expo.out" }, 0.35);

      // prompt 1
      tl.set("#ph", { opacity: 0 }, T_TYPE1 - 0.2);
      const end1 = typeInto("txt1", "Turn these images into halftone dots", T_TYPE1);
      tl.to("#send", { scale: 0.86, duration: 0.08, ease: "power2.out" }, SEND1);
      tl.to("#send", { scale: 1, duration: 0.35, ease: "back.out(3)" }, SEND1 + 0.08);
      tl.set("#txt1", { opacity: 0 }, SEND1 + 0.1);
      tl.set("#ph", { opacity: 1 }, SEND1 + 0.12);
      tl.set("#b1", { opacity: 0, y: 40, scale: 0.96 }, 0);
      tl.to("#b1", { opacity: 1, y: 0, scale: 1, duration: 0.55, ease: "expo.out" }, SEND1 + 0.12);
      tl.set("#r1", { opacity: 0, y: 24 }, 0);
      tl.to("#r1", { opacity: 1, y: 0, duration: 0.55, ease: "expo.out" }, SEND1 + 0.55);
      tl.to("#r1a", { opacity: 0, duration: 0.01 }, DIS1);
      tl.to("#r1b", { opacity: 1, duration: 0.25, ease: "power2.out" }, DIS1);

      // prompt 2
      tl.set("#ph", { opacity: 0 }, T_TYPE2 - 0.2);
      const end2 = typeInto("txt2", "Back to full colour", T_TYPE2);
      tl.to("#send", { scale: 0.86, duration: 0.08, ease: "power2.out" }, SEND2);
      tl.to("#send", { scale: 1, duration: 0.35, ease: "back.out(3)" }, SEND2 + 0.08);
      tl.set("#txt2", { opacity: 0 }, SEND2 + 0.1);
      tl.set("#ph", { opacity: 1 }, SEND2 + 0.12);
      tl.to("#thread", { y: -300, duration: 0.65, ease: "power3.inOut" }, SEND2 + 0.1);
      tl.set("#b2", { opacity: 0, y: 36, scale: 0.96 }, 0);
      tl.to("#b2", { opacity: 1, y: 0, scale: 1, duration: 0.55, ease: "expo.out" }, SEND2 + 0.22);
      tl.set("#r2", { opacity: 0, y: 24 }, 0);
      tl.to("#r2", { opacity: 1, y: 0, duration: 0.55, ease: "expo.out" }, SEND2 + 0.65);
      tl.to("#r2a", { opacity: 0, duration: 0.01 }, RES1);
      tl.to("#r2b", { opacity: 1, duration: 0.25, ease: "power2.out" }, RES1);

      // hand-off to the lockup
      tl.to("#ui", { opacity: 0, scale: 0.985, duration: 0.5, ease: "power2.in", transformOrigin: "50% 50%" }, UI_OUT);
      tl.set("#markc", { opacity: 0 }, 0);
      tl.to("#markc", { opacity: 1, duration: 0.3, ease: "power2.out" }, MK_IN);
      tl.set("#word", { opacity: 0, y: 16, filter: "blur(6px)" }, 0);
      tl.to("#word", { opacity: 1, y: 0, filter: "blur(0px)", duration: 0.55, ease: "power3.out" }, 8.0);
      tl.set("#tag", { opacity: 0, y: 12 }, 0);
      tl.to("#tag", { opacity: 1, y: 0, duration: 0.55, ease: "power3.out" }, 8.12);

      // canvases are redrawn from timeline time
      const proxy = { v: 0 };
      tl.to(proxy, { v: 1, duration: DUR, ease: "none", onUpdate: () => render(tl.time()) }, 0);
      render(0);

      tl.set({}, {}, DUR);
      window.__timelines = window.__timelines || {};
      window.__timelines["main"] = tl;
    </script>
  </body>
</html>
```
