{
  "$schema": "../../schema.json",
  "version": "1.0.0",
  "license": "MIT",
  "id": "beat-cuts",
  "type": "block",
  "title": "Beat cuts",
  "summary": "Every beat of the music is a hard cut to a new shot with a punch that decays; the first beat of a bar hits harder in the accent.",
  "tags": [
    "block",
    "edit",
    "music",
    "beat",
    "cuts",
    "rhythm"
  ],
  "params": [
    {
      "name": "background",
      "type": "string",
      "default": "",
      "description": "a background id from the registry; empty uses the brand's first",
      "group": "advanced",
      "control": "select"
    },
    {
      "name": "bpm",
      "type": "number",
      "default": 120,
      "min": 60,
      "max": 170,
      "step": 1,
      "description": "tempo: beats per minute (one word per beat; slower holds each word longer and makes the part longer)",
      "group": "main",
      "control": "slider"
    },
    {
      "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/beat-cuts/scene.html",
      "content": "<!doctype html>\n<html>\n<head>\n<meta charset=\"utf-8\">\n<title>beat-cuts</title>\n<!--\n  Edit · beat cuts (one beat per word: 6.5 s for twelve words at 120 bpm). The edit follows the music: every beat is a hard cut to a new shot, each landing with a\n  punch that decays before the next; the first beat of a bar hits harder and flips the frame to the accent. Give it\n  the track's tempo (`bpm`, `offset`) or its beat times (`beats`), and the shots. A strip at the bottom shows the bar\n  and beat, so the sync can be checked by eye.\n  Count-in 0–0.4 s · Cuts on every beat to 6.4 s.\n  Square and vertical: each word fits the width with a margin (and room for its punch); vertical breaks a hyphenated name onto two lines.\n-->\n<style>\n  #stage { overflow: hidden; }\n  .abs { position: absolute; box-sizing: border-box; }\n  .shot { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; text-align: center; overflow: hidden; }\n  .shot .w { white-space: nowrap; line-height: 1; position: relative; }\n  #strip { position: absolute; left: 50%; bottom: 6%; display: flex; gap: 0.6em; transform: translateX(-50%); }\n  #strip i { display: block; width: 0.7em; height: 0.7em; border-radius: 50%; }\n</style>\n</head>\n<body>\n<main id=\"stage\">\n  <div id=\"shots\"></div>\n  <div id=\"strip\"></div>\n</main>\n<script type=\"module\">\nimport { scene, progress } 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  bpm: 120,                              // the track's tempo; or `beats: [t, …]` from the track\n  offset: 0.4,                           // where its first beat falls, in seconds\n  beats: null,\n  shots: ['Make', 'it', 'move.', 'paper', 'motion', '↗', 'Every', 'frame', 'on', 'the', 'beat.', 'paper-motion'],\n  format: [1920, 1080],\n};\n\nconst B = await loadBrand();\nconst [W, H] = CONFIG.format, SHORT = Math.min(W, H);\n// one beat per word, never repeating; the last word holds a beat (at least 0.6 s) and the part ends there\nconst beat = 60 / CONFIG.bpm;\nconst beats = (CONFIG.beats ?? CONFIG.shots.map((_, i) => CONFIG.offset + i * beat)).slice(0, CONFIG.shots.length);\nconst D = +(beats[beats.length - 1] + Math.max(beat, 0.6)).toFixed(2);\nconst s = scene({ width: W, height: H, fps: 30, duration: D, background: B.color('ground') });\nconst $ = (id) => document.getElementById(id);\nconst accent = B.color('accent', '#E9E4D8'), ground = B.color('ground'), figure = B.color('figure'), muted = B.color('muted');\nconst bgId = CONFIG.background ?? B.set.entries.find((e) => e.shader && !e.image)?.id;\n// the shots: words, on black, on the accent (first beat of each bar), or over the brand's field\nconst shots = beats.map((b, i) => {\n  const el = document.createElement('div'); el.className = 'shot';\n  const down = i % 4 === 0, txt = CONFIG.shots[i], last = i === beats.length - 1;\n  const field = !down && i % 4 === 2;\n  el.style.background = down ? accent : ground;\n  if (field && bgId) { const f = document.createElement('div'); f.className = 'abs'; f.style.inset = 0; f.style.opacity = 0.8; el.append(f); B.shader(s, f, bgId); }\n  const w = B.style(document.createElement('div'), 'display', { short: SHORT, color: 'figure' }); w.className = 'w';\n  w.textContent = txt; w.style.color = down ? ground : figure;\n  w.style.fontSize = `${SHORT * (last ? 0.16 : txt.length > 6 ? 0.22 : 0.32)}px`;\n  if (i % 3 === 1 && !last) { w.style.fontFamily = B.font('mono'); w.style.fontSize = `${SHORT * 0.2}px`; w.style.letterSpacing = '-0.04em'; }\n  el.append(w); $('shots').append(el);\n  return { el, w, down };\n});\n(s.marks ??= []).push({ gesture: 'land', t: beats[beats.length - 1] });   // the last word: a part cut before it loses the ending\n$('strip').style.fontSize = `${SHORT * 0.018}px`;\n$('strip').innerHTML = '<i></i><i></i><i></i><i></i>';\nconst dots = [...$('strip').children];\n// the click track, so the sync can be heard: a thud on each beat, harder on the one\nbeats.forEach((b, i) => s.sfx(b, i % 4 === 0 ? 'thud' : 'knock', { gain: i % 4 === 0 ? 1 : 0.55 }));\nawait document.fonts.ready;\n// outside 16:9 every word fits the width with a margin, leaving room for its punch; vertical breaks a hyphenated name\n// at its hyphen and sets it bigger; anything still too wide shrinks\nif (W <= H) shots.forEach(({ w, down }) => {\n  const room = (W * 0.84) / (down ? 1.14 : 1.08);\n  if (H > W && /\\w-\\w/.test(w.textContent)) {\n    const parts = w.textContent.split(/(?<=-)/); w.textContent = ''; parts.forEach((p) => w.append(p, document.createElement('wbr')));   // a break after each hyphen\n    Object.assign(w.style, { whiteSpace: 'normal', maxWidth: `${room}px`, fontSize: `${W * 0.22}px`, lineHeight: 0.95 });\n  }\n  for (let n = 0; n < 60 && w.scrollWidth > room + 1; n++) w.style.fontSize = `${parseFloat(w.style.fontSize) * 0.96}px`;\n});\n\ns.update((t) => {\n  // cuts land on whole frames: hard, never blended by motion blur\n  const tc = Math.round(t * 30) / 30;\n  let i = 0; beats.forEach((b, k) => { if (tc >= b - 1e-6) i = k; });   // before the first beat, the first word is already up\n  shots.forEach(({ el }, k) => { el.style.visibility = k === i ? '' : 'hidden'; });\n  // no black count-in: the first word is on screen from the first frame and lands then (the beat before it is still\n  // in sync for a track: only what is shown before it changes)\n  const since = Math.max(0, t - (i === 0 ? 0 : beats[i])), hit = Math.exp(-since * (shots[i].down ? 6 : 9));\n  const { w, el, down } = shots[i];\n  w.style.transform = `scale(${1 + (down ? 0.14 : 0.08) * hit}) translateY(${(down ? -0.02 : 0.015) * SHORT * hit}px)`;\n  el.style.filter = `brightness(${1 + 0.25 * hit})`;\n  // the strip: which beat of the bar\n  dots.forEach((d, k) => { d.style.background = k === i % 4 ? (down ? ground : accent) : (down ? `${ground}44` : `${muted}55`); d.style.transform = `scale(${k === i % 4 ? 1 + 0.5 * hit : 1})`; });\n});\nawait s.start();\n</script>\n</body>\n</html>\n"
    },
    {
      "path": "pieces/beat-cuts/brief.md",
      "content": "# Beat cuts\n\nBlock: beat-cuts (6.4 s), from paper-motion.app. The look, fonts and sound come from `brand/`.\n\nEvery beat of the music is a hard cut to a new shot with a punch that decays; the first beat of a bar hits harder in the accent.\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 0.4 and 6.4 s; render.\n"
    }
  ],
  "requires": {
    "engine": ">=0.2.6"
  },
  "usage": "npx paper-motion add beat-cuts --name my-beat-cuts\n# edit CONFIG in pieces/my-beat-cuts/scene.html, then:\nnpx paper-motion render pieces/my-beat-cuts/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": [
    "grain-pulse",
    "cut"
  ],
  "preview": {
    "video": "previews/beat-cuts.webm",
    "poster": "previews/beat-cuts.jpg",
    "at": 2.45,
    "values": {}
  },
  "mood": [
    "bold",
    "playful"
  ],
  "duration": 6.5,
  "formats": [
    "16:9",
    "9:16",
    "1:1"
  ],
  "beats": [
    {
      "name": "Count-in",
      "from": 0,
      "to": 0.4,
      "what": "count-in"
    },
    {
      "name": "Cuts on every beat",
      "from": 0.4,
      "to": 6.4,
      "what": "cuts on every beat"
    }
  ],
  "when": [
    "an edit that follows a track",
    "a fast social teaser"
  ],
  "needs": [
    {
      "kind": "words",
      "fill": {
        "shots": "$items"
      }
    },
    {
      "kind": "music",
      "optional": true,
      "fill": {
        "bpm": "$bpm",
        "offset": "$offset",
        "beats": "$beats"
      }
    }
  ]
}