{
  "$schema": "../../schema.json",
  "version": "1.0.0",
  "license": "MIT",
  "id": "grain-pulse",
  "type": "block",
  "title": "Grain pulse",
  "summary": "On each hit the grain swells, the colour channels split for a moment and the frame punches in, then it all settles until the next.",
  "tags": [
    "block",
    "effect",
    "grain",
    "chromatic",
    "beat",
    "texture"
  ],
  "params": [
    {
      "name": "background",
      "type": "string",
      "default": "",
      "description": "a background id from the registry; empty uses the brand's first",
      "group": "advanced",
      "control": "select"
    },
    {
      "name": "last",
      "type": "string",
      "default": "",
      "description": "the last word; empty takes the brand's name",
      "group": "main",
      "control": "text"
    },
    {
      "name": "grain",
      "type": "number",
      "default": 0.1,
      "description": "the quiet grain between hits",
      "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/grain-pulse/scene.html",
      "content": "<!doctype html>\n<html>\n<head>\n<meta charset=\"utf-8\">\n<title>grain-pulse</title>\n<!--\n  Effect · grain pulse (4.6 s). Texture on the beat: at each cut the film grain swells, the colour channels split for a\n  moment and the frame punches in a little, then all of it settles back to a quiet grain until the next hit. Here it\n  carries three hard cuts and the name.\n  Hits 0.3, 1.2, 2.1, 3.0 s · Hold to 4.6 s.\n  Square and vertical: each word fits the width with a margin; vertical sets them bigger and breaks a long name.\n-->\n<style>\n  #stage { overflow: hidden; }\n  .abs { position: absolute; box-sizing: border-box; }\n  .w { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; text-align: center; white-space: nowrap; }\n  #grain { position: absolute; inset: 0; width: 100%; height: 100%; mix-blend-mode: overlay; image-rendering: pixelated; pointer-events: none; }\n</style>\n</head>\n<body>\n<main id=\"stage\">\n  <div id=\"frame\" class=\"abs\" style=\"inset:0\"><div id=\"bg\" class=\"abs\" style=\"inset:0\"></div><div id=\"words\"></div></div>\n  <canvas id=\"grain\"></canvas>\n</main>\n<script type=\"module\">\nimport { scene, progress, ease } from 'paper-motion';\nimport { loadBrand } from 'paper-motion/brand';\n\nconst CONFIG = {\n  background: null,                      // a shader-set id, or null for the brand's first entry\n  words: ['Fast.', 'Simple.', 'Yours.'],\n  last: '',                              // the last word; empty takes the brand's name\n  grain: 0.1,                            // the quiet grain between hits\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, '#bg', bgId);\nconst $ = (id) => document.getElementById(id);\n$('bg').style.opacity = 0.5;\nconst all = [...CONFIG.words, CONFIG.last || B.name];\nconst els = all.map((w, i) => {\n  const d = document.createElement('div'); d.className = 'w';\n  const t = B.style(document.createElement('div'), 'display', { short: SHORT, color: 'figure' });\n  t.textContent = w; t.style.fontSize = `${SHORT * (i === all.length - 1 ? 0.17 : 0.24)}px`;\n  d.append(t); $('words').append(d); return { d, t };\n});\n// the grain: noise drawn small, a new field every frame, seeded by the frame so renders repeat\nconst gc = $('grain'), gw = 480, gh = 270; gc.width = gw; gc.height = gh; const gx = gc.getContext('2d');\nconst img = gx.createImageData(gw, gh);\nconst rand = (seed) => () => { seed = (seed * 1664525 + 1013904223) >>> 0; return seed / 4294967296; };\nawait document.fonts.ready;\n// outside 16:9 the type is fitted to the width with a margin (room for the punch): vertical sets it bigger and lets the\n// name break onto two lines; anything still too wide shrinks\nif (W <= H) {\n  const tall = H > W, room = W * 0.8;\n  els.forEach(({ t }, k) => {\n    const last = k === all.length - 1;\n    if (tall) { t.style.fontSize = `${W * (last ? 0.24 : 0.3)}px`; if (last) Object.assign(t.style, { whiteSpace: 'normal', maxWidth: `${room}px`, lineHeight: 0.95 }); }\n    for (let n = 0; n < 60 && t.scrollWidth > room + 1; n++) t.style.fontSize = `${parseFloat(t.style.fontSize) * 0.96}px`;\n  });\n  const words = els.slice(0, -1), same = Math.min(...words.map(({ t }) => parseFloat(t.style.fontSize)));   // the beat words share one size\n  words.forEach(({ t }) => { t.style.fontSize = `${same}px`; });\n}\n\nconst T = { hits: [0.3, 1.2, 2.1, 3.0] };\ns.update((t) => {\n  // which shot, and how fresh the last hit is\n  const tc = Math.round(t * 30) / 30;                               // cuts land on whole frames: hard, never blended\n  let i = -1; T.hits.forEach((h, k) => { if (tc >= h - 1e-6) i = k; });\n  const since = i >= 0 ? Math.max(0, t - T.hits[i]) : 9, hit = Math.exp(-since * 7);\n  els.forEach(({ d }, k) => { d.style.visibility = k === i ? '' : 'hidden'; });\n  const cur = els[i];\n  if (cur) {\n    const split = SHORT * 0.012 * hit;\n    cur.t.style.textShadow = split > 0.3 ? `${split}px 0 rgba(255,40,40,0.85), ${-split}px 0 rgba(40,170,255,0.85)` : 'none';\n    cur.t.style.transform = `scale(${1 + 0.06 * hit}) translateY(${(1 - Math.min(1, since / 0.12)) * SHORT * 0.01}px)`;\n    cur.t.style.filter = hit > 0.5 ? `blur(${(hit - 0.5) * 3}px)` : 'none';\n  }\n  // the whole frame: a punch and a brief lift in exposure on each hit, a slow drift between\n  $('frame').style.transform = `scale(${1.02 + 0.05 * hit + 0.02 * progress(t, 0, D)})`;\n  $('frame').style.filter = `brightness(${1 + 0.35 * hit}) contrast(${1 + 0.1 * hit})`;\n  $('bg').style.transform = `translateX(${SHORT * 0.02 * Math.sin(t * 0.9)}px)`;\n  // grain: quiet, swelling on the hit\n  const r = rand(Math.round(t * 30) + 1), a = CONFIG.grain + 0.45 * hit;\n  for (let p = 0; p < gw * gh; p++) { const v = r() * 255; img.data[p * 4] = img.data[p * 4 + 1] = img.data[p * 4 + 2] = v; img.data[p * 4 + 3] = 255; }\n  gx.putImageData(img, 0, 0);\n  gc.style.opacity = String(Math.min(1, a * 2));\n});\nT.hits.forEach((h) => B.cue(s, 'land', h));\nB.sound(s);\nawait s.start();\n</script>\n</body>\n</html>\n"
    },
    {
      "path": "pieces/grain-pulse/brief.md",
      "content": "# Grain pulse\n\nBlock: grain-pulse (4.6 s), from paper-motion.app. The look, fonts and sound come from `brand/`.\n\nOn each hit the grain swells, the colour channels split for a moment and the frame punches in, then it all settles until the next.\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 3.6 and 4.6 s; render.\n"
    }
  ],
  "requires": {
    "engine": ">=0.2.6"
  },
  "usage": "npx paper-motion add grain-pulse --name my-grain-pulse\n# edit CONFIG in pieces/my-grain-pulse/scene.html, then:\nnpx paper-motion render pieces/my-grain-pulse/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": [
    "beat-cuts",
    "rgb-split"
  ],
  "preview": {
    "video": "previews/grain-pulse.webm",
    "poster": "previews/grain-pulse.jpg",
    "at": 3.05,
    "values": {}
  },
  "mood": [
    "bold",
    "raw"
  ],
  "duration": 4.6,
  "formats": [
    "16:9",
    "9:16",
    "1:1"
  ],
  "beats": [
    {
      "name": "Hits",
      "from": 0.3,
      "to": 3.6,
      "what": "hits"
    },
    {
      "name": "Hold",
      "from": 3.6,
      "to": 4.6,
      "what": "hold"
    }
  ],
  "when": [
    "hard cuts on a beat",
    "a punchy social opener"
  ],
  "needs": [
    {
      "kind": "words",
      "fill": {
        "words": "$items"
      }
    }
  ]
}