{
  "$schema": "../../schema.json",
  "version": "1.0.0",
  "license": "MIT",
  "id": "word-swap",
  "type": "block",
  "title": "Word swap",
  "summary": "One word in a sentence keeps changing, rolling out and in, while the rest of the sentence makes room for each new width.",
  "tags": [
    "block",
    "text",
    "rotating",
    "headline",
    "kinetic"
  ],
  "params": [
    {
      "name": "background",
      "type": "string",
      "default": "",
      "description": "a background id from the registry; empty uses the brand's first",
      "group": "advanced",
      "control": "select"
    },
    {
      "name": "before",
      "type": "string",
      "default": "Make a ",
      "description": "before",
      "group": "main",
      "control": "text"
    },
    {
      "name": "after",
      "type": "string",
      "default": " video.",
      "description": "after",
      "group": "main",
      "control": "text"
    },
    {
      "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/word-swap/scene.html",
      "content": "<!doctype html>\n<html>\n<head>\n<meta charset=\"utf-8\">\n<title>word-swap</title>\n<!--\n  Text · word swap (5.6 s). One word in a sentence keeps changing: the old one rolls up and out, soft, the new one\n  rolls in from below, and the slot it sits in eases to the new word's width so the rest of the sentence slides to\n  make room. The camera breathes in a little on each change.\n  Vertical: three lines, the changing word on its own; square: two lines (the word stays on the first). Type fits the width.\n  Sentence 0.2 s · Swaps 1.4, 2.4, 3.4 s · Hold to 5.6 s.\n-->\n<style>\n  #stage { overflow: hidden; }\n  .abs { position: absolute; box-sizing: border-box; }\n  #p { display: flex; justify-content: center; }\n  #line { white-space: nowrap; display: flex; align-items: flex-start; }\n  #slot { position: relative; display: inline-block; overflow: hidden; vertical-align: bottom; }\n  #slot span { position: absolute; left: 0; top: 0; white-space: nowrap; }\n  #ghost { position: absolute; visibility: hidden; white-space: nowrap; }\n</style>\n</head>\n<body>\n<main id=\"stage\">\n  <div id=\"bg\" class=\"abs\" style=\"inset:0\"></div>\n  <div id=\"vig\" class=\"abs\" style=\"inset:0\"></div>\n  <div id=\"world\"><div id=\"p\"><div id=\"line\"><span id=\"pre\"></span><span id=\"slot\"></span><span id=\"post\"></span></div><span id=\"ghost\"></span></div></div>\n</main>\n<script type=\"module\">\nimport { scene, progress, ease, spring } from 'paper-motion';\nimport { camera } from 'paper-motion/kit';\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  before: 'Make a ',\n  words: ['launch', 'release', 'demo', 'brand'],\n  after: ' video.',\n  format: [1920, 1080],\n};\n\nconst B = await loadBrand();\nconst [W, H] = CONFIG.format, SHORT = Math.min(W, H), D = 5.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.4;\n$('vig').style.background = 'radial-gradient(ellipse at 50% 50%, rgba(0,0,0,0) 30%, rgba(0,0,0,0.6) 100%)';\n\nconst DIST = SHORT * 0.62, K = 1 + DIST / (SHORT * 1.7);\nconst cam = camera($('stage'), $('world'), { perspective: SHORT * 1.7 });\nconst accent = B.color('accent', '#E9E4D8');\nconst line = B.style($('line'), 'display', { short: SHORT * K, color: 'figure' });\nconst tall = H > W, square = W === H;\nlet fs = SHORT * K * 0.14; line.style.fontSize = `${fs}px`;\n$('pre').textContent = tall ? CONFIG.before.trim() : CONFIG.before; $('post').textContent = W > H ? CONFIG.after : CONFIG.after.trim();\nif (tall) Object.assign(line.style, { flexDirection: 'column', alignItems: 'center' });\nif (square) { Object.assign(line.style, { flexWrap: 'wrap', justifyContent: 'center', width: `${W}px` }); Object.assign($('post').style, { flexBasis: '100%', textAlign: 'center' }); }\n$('pre').style.whiteSpace = $('post').style.whiteSpace = 'pre';\nconst slot = $('slot'), ghost = $('ghost');\nObject.assign(ghost.style, { font: getComputedStyle(line).font, letterSpacing: getComputedStyle(line).letterSpacing });\nconst words = CONFIG.words.map((w) => { const el = document.createElement('span'); el.textContent = w; el.style.color = accent; slot.append(el); return el; });\n$('p').style.width = `${W}px`;\ncam.place($('p'), { x: 0, y: 0, z: 0 });\nawait document.fonts.ready;\nconst measure = (txt) => { ghost.textContent = txt; return ghost.offsetWidth; };\nif (W <= H) {   // narrow frames: the widest line fits the width at the closest the camera gets\n  const longest = CONFIG.words.reduce((a, w) => (measure(w) > measure(a) ? w : a));\n  const lines = tall ? [CONFIG.before.trim(), longest, CONFIG.after.trim()] : [CONFIG.before + longest, CONFIG.after.trim()];\n  const closest = (SHORT * 1.7) / (SHORT * 1.7 + DIST - SHORT * 0.29);\n  fs = Math.min(fs * 2, fs * (W * 0.84 / closest) / Math.max(...lines.map(measure)));\n  line.style.fontSize = `${fs}px`; ghost.style.fontSize = `${fs}px`;\n}\nconst widths = CONFIG.words.map(measure);\nconst lh = fs * 1.2; slot.style.height = `${lh}px`; line.style.lineHeight = `${lh}px`;   // one line height for all three parts: one baseline\nwords.forEach((el) => { el.style.lineHeight = `${lh}px`; });\n\nconst T = { in: 0.2, swaps: [1.4, 2.4, 3.4], dur: 0.55 };\ns.update((t) => {\n  // which word, and how far through the change to the next\n  let i = 0; while (i < T.swaps.length && t >= T.swaps[i]) i++;\n  const from = Math.max(0, i - 1), at = T.swaps[i - 1] ?? -1, p = i ? progress(t, at, at + T.dur, ease.inOutCubic) : 1;\n  words.forEach((el, k) => {\n    let y = 1.2, o = 0, b = 0;\n    if (i === 0 && k === 0) { const e = progress(t, T.in, T.in + 0.7, ease.outCubic); y = 1 - e; o = e; b = (1 - e) * 10; }\n    else if (k === i) { y = 1 - p; o = p; b = (1 - p) * 10; }                   // the new one rolls in from below\n    else if (k === from && i > 0) { y = -p; o = 1 - p; b = p * 10; }              // the old one rolls up and out\n    el.style.transform = `translateY(${y * lh}px)`; el.style.opacity = String(o); el.style.filter = b > 0.3 ? `blur(${b}px)` : 'none';\n  });\n  // the slot eases to the new word's width, on a spring, so the sentence slides to make room\n  const wOld = widths[from], wNew = widths[i] ?? widths[from];\n  const sp = i ? spring(Math.max(0, t - at), { stiffness: 170, damping: 17 }) : 1;\n  slot.style.width = `${wOld + (wNew - wOld) * (i ? sp : 1)}px`;\n  for (const id of ['pre', 'post']) $(id).style.opacity = String(progress(t, T.in, T.in + 0.6));\n  // the camera: a slow drift in, a small breath on each change\n  const arrive = progress(t, 0, 1.0, ease.outCubic), drift = progress(t, 0, D, ease.inOutSine);\n  const breath = T.swaps.reduce((k, a) => k + (t >= a ? Math.sin(Math.min(1, (t - a) / 0.6) * Math.PI) : 0), 0);\n  cam.at({ x: 0, y: 0, z: DIST + SHORT * 0.3 * (1 - arrive) - SHORT * 0.25 * drift - SHORT * 0.035 * breath, yaw: -5 * (1 - arrive) + 3 * Math.sin(t * 0.7) * arrive, pitch: 0, roll: 0 });\n  $('bg').style.transform = `scale(${1.08 + 0.08 * drift})`;\n});\nB.cue(s, 'text', T.in); T.swaps.forEach((a) => B.cue(s, 'transition', a));\nB.sound(s);\nawait s.start();\n</script>\n</body>\n</html>\n"
    },
    {
      "path": "pieces/word-swap/brief.md",
      "content": "# Word swap\n\nBlock: word-swap (5.6 s), from paper-motion.app. The look, fonts and sound come from `brand/`.\n\nOne word in a sentence keeps changing, rolling out and in, while the rest of the sentence makes room for each new width.\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.8 and 4 s; render.\n"
    }
  ],
  "requires": {
    "engine": ">=0.2.6"
  },
  "usage": "npx paper-motion add word-swap --name my-word-swap\n# edit CONFIG in pieces/my-word-swap/scene.html, then:\nnpx paper-motion render pieces/my-word-swap/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": [
    "marker-highlight",
    "kinetic-captions"
  ],
  "preview": {
    "video": "previews/word-swap.webm",
    "poster": "previews/word-swap.jpg",
    "at": 3,
    "values": {}
  },
  "mood": [
    "playful"
  ],
  "duration": 5.6,
  "formats": [
    "16:9",
    "9:16",
    "1:1"
  ],
  "beats": [
    {
      "name": "Sentence",
      "from": 0.2,
      "to": 0.8,
      "what": "sentence"
    },
    {
      "name": "Swaps",
      "from": 1.4,
      "to": 4,
      "what": "swaps"
    },
    {
      "name": "Hold",
      "from": 4,
      "to": 5.6,
      "what": "hold"
    }
  ],
  "when": [
    "many uses for one product",
    "a headline with options"
  ],
  "needs": [
    {
      "kind": "variants",
      "fill": {
        "before": "$before",
        "words": "$words",
        "after": "$after"
      }
    }
  ]
}