{
  "$schema": "../../schema.json",
  "version": "1.0.0",
  "license": "MIT",
  "id": "dither-dissolve",
  "type": "block",
  "title": "Dither dissolve",
  "summary": "One shot breaks into an ordered dither, dot by dot, in a sweep across the frame; the edge lights in the accent and the next shot is under it.",
  "tags": [
    "block",
    "transition",
    "dither",
    "dissolve",
    "shader"
  ],
  "params": [
    {
      "name": "background",
      "type": "string",
      "default": "",
      "description": "a background id from the registry; empty uses the brand's first",
      "group": "advanced",
      "control": "select"
    },
    {
      "name": "a",
      "type": "string",
      "default": "Tools were made for hands.",
      "description": "a",
      "group": "main",
      "control": "text"
    },
    {
      "name": "b",
      "type": "string",
      "default": "",
      "description": "the name it dissolves into (empty: the brand's name)",
      "group": "main",
      "control": "text"
    },
    {
      "name": "bLine",
      "type": "string",
      "default": "Brand videos, made by your coding agent.",
      "description": "bLine",
      "group": "main",
      "control": "text"
    },
    {
      "name": "cell",
      "type": "number",
      "default": 14,
      "description": "the dither's dot size, in pixels at 1080p",
      "group": "main"
    },
    {
      "name": "format",
      "type": "enum",
      "default": "1920x1080",
      "options": [
        "1920x1080",
        "1080x1920",
        "1080x1080"
      ],
      "description": "output size; the layout recomposes for each",
      "group": "main",
      "control": "segmented"
    }
  ],
  "install": {
    "kind": "scene"
  },
  "files": [
    {
      "path": "pieces/dither-dissolve/scene.html",
      "content": "<!doctype html>\n<html>\n<head>\n<meta charset=\"utf-8\">\n<title>dither-dissolve</title>\n<!--\n  Transition · dither dissolve (4.6 s). The first shot breaks into an ordered dither, dot by dot, in a sweep across the\n  frame; the dissolving edge lights up in the accent as it passes, and the second shot is underneath. Both shots keep\n  a slow push so the cut never feels still.\n  Shot A 0–1.4 s · Dissolve 1.4–2.9 s · Shot B to 4.6 s.\n  Square and vertical: type fits the width with a margin; vertical sets it bigger and breaks the name at its hyphen.\n-->\n<style>\n  #stage { overflow: hidden; }\n  .abs { position: absolute; box-sizing: border-box; }\n  .shot { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; }\n  #edge { position: absolute; left: 0; top: 0; }\n</style>\n</head>\n<body>\n<main id=\"stage\">\n  <div id=\"b\" class=\"shot\"><div id=\"bbg\" class=\"abs\" style=\"inset:0\"></div><div id=\"bt\" style=\"position:relative\"></div><div id=\"bs\" style=\"position:relative\"></div></div>\n  <div id=\"a\" class=\"shot\"><div id=\"at\"></div></div>\n  <canvas id=\"edge\"></canvas>\n</main>\n<script type=\"module\">\nimport { scene, progress, ease } from 'paper-motion';\nimport { wordRise, blurIn } from 'paper-motion/moves';\nimport { loadBrand } from 'paper-motion/brand';\n\nconst CONFIG = {\n  background: null,                      // the shader behind shot B, or null for the brand's first entry\n  a: 'Tools were made for hands.',\n  b: null,                               // the name it dissolves into; null for the brand's name\n  bLine: 'Brand videos, made by your coding agent.',\n  cell: 14,                              // the dither's dot size, in pixels at 1080p\n  format: [1920, 1080],\n};\n\nconst B = await loadBrand();\nconst [W, H] = CONFIG.format, SHORT = Math.min(W, H), D = 4.6;\nconst s = scene({ width: W, height: H, fps: 30, duration: D, background: B.color('ground') });\nconst bgId = CONFIG.background ?? B.set.entries.find((e) => e.shader && !e.image)?.id;\nif (bgId) B.shader(s, '#bbg', bgId);\nconst $ = (id) => document.getElementById(id);\nconst ground = B.color('ground'), accent = B.color('accent', '#E9E4D8');\n$('a').style.background = ground;\nconst at = B.style($('at'), 'display', { short: SHORT, color: 'figure' }); at.textContent = CONFIG.a; at.style.fontSize = `${SHORT * 0.11}px`; at.style.maxWidth = '12ch';\nconst bt = B.style($('bt'), 'display', { short: SHORT, color: 'figure' }); bt.textContent = CONFIG.b || B.name; bt.style.fontSize = `${SHORT * 0.15}px`; bt.style.textShadow = '0 4px 40px rgba(0,0,0,0.5)';\nconst bs = B.style($('bs'), 'title', { short: SHORT, color: 'figure' }); bs.textContent = CONFIG.bLine; bs.style.marginTop = `${SHORT * 0.02}px`; bs.style.opacity = 0.85;\n$('b').style.background = ground;\nawait document.fonts.ready;\n// outside 16:9 the type is set from the width: vertical sets it bigger (the name may break at its hyphen), and anything\n// wider than the frame minus a margin (and the push) shrinks until it fits\nif (W <= H) {\n  const tall = H > W, room = W * 0.78;\n  if (tall) {\n    at.style.fontSize = `${W * 0.15}px`; at.style.maxWidth = `${room}px`;\n    Object.assign(bt.style, { fontSize: `${W * 0.21}px`, maxWidth: `${room}px`, lineHeight: 0.95 });\n    Object.assign(bs.style, { fontSize: `${W * 0.08}px`, maxWidth: `${room}px`, marginTop: `${W * 0.05}px` });\n  }\n  if (!tall) bs.style.fontSize = `${W * 0.052}px`;\n  Object.assign(bs.style, { whiteSpace: 'normal', textWrap: 'balance', maxWidth: `${room}px`, marginLeft: 'auto', marginRight: 'auto' });   // the line wraps rather than shrinks\n  for (const el of [at, bt, bs]) for (let n = 0; n < 60 && el.scrollWidth > room + 1; n++) el.style.fontSize = `${parseFloat(el.style.fontSize) * 0.96}px`;\n}\n\n// the dither: an 8×8 Bayer matrix biased by a diagonal sweep; a cell is gone once the progress passes its threshold\nconst cell = CONFIG.cell * SHORT / 1080, cols = Math.ceil(W / cell), rows = Math.ceil(H / cell);\nconst bayer = [[0, 32, 8, 40, 2, 34, 10, 42], [48, 16, 56, 24, 50, 18, 58, 26], [12, 44, 4, 36, 14, 46, 6, 38], [60, 28, 52, 20, 62, 30, 54, 22], [3, 35, 11, 43, 1, 33, 9, 41], [51, 19, 59, 27, 49, 17, 57, 25], [15, 47, 7, 39, 13, 45, 5, 37], [63, 31, 55, 23, 61, 29, 53, 21]];\nconst thr = new Float32Array(cols * rows);\nfor (let y = 0; y < rows; y++) for (let x = 0; x < cols; x++) {\n  const sweep = (x / cols) * 0.7 + (1 - y / rows) * 0.3;             // left-to-right, a little from the bottom\n  thr[y * cols + x] = sweep * 0.62 + (bayer[y % 8][x % 8] / 64) * 0.38;\n}\n// the mask for shot A, drawn small and scaled up; and the edge canvas, drawn at full size\nconst mc = document.createElement('canvas'); mc.width = cols; mc.height = rows; const mx = mc.getContext('2d');\nconst edge = $('edge'); edge.width = W; edge.height = H; edge.style.width = `${W}px`; edge.style.height = `${H}px`; const ex = edge.getContext('2d');\nconst a = $('a');\nObject.assign(a.style, { webkitMaskSize: '100% 100%', maskSize: '100% 100%', imageRendering: 'pixelated' });\nconst T = { dis: [1.4, 2.9] };\nconst band = 0.06;\n\ns.update((t) => {\n  // the shots: words in, a slow push on each\n  wordRise(at, t, { start: 0.15, each: B.pace.stagger, duration: 0.8 });\n  a.style.transform = `scale(${1 + 0.05 * progress(t, 0, T.dis[1])})`;\n  $('b').style.transform = `scale(${1.08 - 0.08 * progress(t, T.dis[0], D, ease.outCubic)})`;\n  wordRise(bt, t, { start: T.dis[0] + 0.7, each: B.pace.stagger, duration: 0.9 });\n  blurIn(bs, t, { start: T.dis[0] + 1.3, duration: 0.8 });\n  // the dissolve\n  const p = progress(t, ...T.dis, ease.inOutSine) * (1 + band * 2) - band;\n  ex.clearRect(0, 0, W, H);\n  if (t < T.dis[0]) { a.style.webkitMaskImage = a.style.maskImage = 'none'; a.style.visibility = ''; return; }\n  if (t >= T.dis[1]) { a.style.visibility = 'hidden'; return; }\n  a.style.visibility = '';\n  const img = mx.createImageData(cols, rows);\n  ex.fillStyle = accent;\n  for (let i = 0; i < thr.length; i++) {\n    const v = thr[i] - p;                                           // > 0: still there\n    img.data[i * 4 + 3] = v > 0 ? 255 : 0;\n    if (v > -band && v < band) {                                    // the edge: dots that light up as they go\n      const k = 1 - Math.abs(v) / band, x = (i % cols) * cell + cell / 2, y = Math.floor(i / cols) * cell + cell / 2;\n      ex.globalAlpha = k * 0.9; ex.beginPath(); ex.arc(x, y, cell * 0.36 * k + cell * 0.08, 0, Math.PI * 2); ex.fill();\n    }\n  }\n  ex.globalAlpha = 1;\n  mx.putImageData(img, 0, 0);\n  const url = `url(${mc.toDataURL()})`;\n  a.style.webkitMaskImage = a.style.maskImage = url;\n});\nB.cue(s, 'text', 0.15); B.cue(s, 'transition', T.dis[0]); B.cue(s, 'land', T.dis[0] + 0.7);\nB.sound(s);\nawait s.start();\n</script>\n</body>\n</html>\n"
    },
    {
      "path": "pieces/dither-dissolve/brief.md",
      "content": "# Dither dissolve\n\nBlock: dither-dissolve (4.6 s), from paper-motion.app. The look, fonts and sound come from `brand/`.\n\nOne shot breaks into an ordered dither, dot by dot, in a sweep across the frame; the edge lights in the accent and the next shot is under it.\n\nA block is one ready-made moment. Edit `CONFIG` (its words, numbers and format: 16:9, 9:16 or 1:1, the layout recomposes for each); check `npx paper-motion still` at 1.4 and 2.9 s; render.\n"
    }
  ],
  "requires": {
    "engine": ">=0.2.6"
  },
  "usage": "npx paper-motion add dither-dissolve --name my-dither-dissolve\n# edit CONFIG in pieces/my-dither-dissolve/scene.html, then:\nnpx paper-motion render pieces/my-dither-dissolve/scene.html --motion-blur 5",
  "notes": [
    "Wired to the brand folder: needs brand/tokens.json (a palette and a font pair work) and, for the field, a shader-set entry.",
    "Recomposes for 16:9, 9:16 and 1:1: set `format` and the layout and camera follow."
  ],
  "related": [
    "cells",
    "crossfade"
  ],
  "preview": {
    "video": "previews/dither-dissolve.webm",
    "poster": "previews/dither-dissolve.jpg",
    "at": 2.2,
    "values": {}
  },
  "mood": [
    "luxury",
    "calm"
  ],
  "duration": 4.6,
  "formats": [
    "16:9",
    "9:16",
    "1:1"
  ],
  "beats": [
    {
      "name": "Shot A",
      "from": 0,
      "to": 1.4,
      "what": "shot a"
    },
    {
      "name": "Dissolve",
      "from": 1.4,
      "to": 2.9,
      "what": "dissolve"
    },
    {
      "name": "Shot B",
      "from": 2.9,
      "to": 4.6,
      "what": "shot b"
    }
  ],
  "when": [
    "a transition that only this brand has",
    "between a statement and the name"
  ],
  "needs": [
    {
      "kind": "line",
      "fill": {
        "a": "$text",
        "bLine": "$brief.what"
      }
    }
  ]
}