{"format":1,"package":"volito","version":"0.5.0","packs":[{"format":1,"id":"essentials","name":"MONARC Essentials","version":"1.0.0","description":"The graphics every video needs — name plates, titles, chapter cards, calls to action, stats, lists, agendas, quotes, logo walls, progress bars, before/after splits and end screens — in any brand, any aspect.","author":"MONARC STUDIOS","license":"MONARC Packs License 1.0 (free to use in any video; not for resale as templates)","homepage":"https://volito.dev/docs/packs/essentials","style":"studio","shared":"shared.js","items":["lower-third","title-card","headline","subscribe","social","callout","stat","bar-chart","steps","quote","banner","end-card","logo-sting","wipe","chapter","logo-wall","progress-bar","before-after","agenda"],"entries":[{"id":"lower-third","name":"Lower third","description":"A name plate over footage: an accent spine grows, the plate wipes in, the name rises, the role slides in.","category":"lower-third","tags":["name","speaker","intro","interview","plate"],"placement":"overlay","duration":{"default":5,"min":2.5,"max":30},"aspects":["16:9","9:16","1:1","4:5"],"props":{"name":{"type":"text","label":"Name","default":"Priya Sharma","maxLength":32},"role":{"type":"text","label":"Role or title","default":"Product designer","maxLength":44},"side":{"type":"choice","label":"Side","options":["left","right"],"default":"left"},"look":{"type":"choice","label":"Look","description":"plate: a card behind the text · minimal: a spine and the text on a soft scrim","options":["plate","minimal"],"default":"plate"}},"beats":{"in":0.15},"parts":{"scrim":"the soft dark wash behind it","spine":"the accent bar","plate":"the card","name":"the name","role":"the role line and its dot"},"style":"studio","pack":"essentials","ref":"essentials/lower-third","code":"// MONARC Essentials — helpers this pack's items share (prepended to each item's code). The layout\n// helpers (safeArea, fitFont, fitLines, landOn, widest) live in the motion kit.\n\n/** Words of `text` (with *marks*) laid into `lines` (the same words without marks), for $.sync / $.headline. */\nfunction markedRows(text, lines) {\n  const words = String(text).split(/\\s+/).filter(Boolean);\n  const rows = [];\n  let i = 0;\n  for (let l = 0; l < lines.length; l++) {\n    const n = String(lines[l]).split(/\\s+/).filter(Boolean).length;\n    rows.push(words.slice(i, i + n).join(\" \"));\n    i += n;\n  }\n  if (i < words.length && rows.length) rows[rows.length - 1] += \" \" + words.slice(i).join(\" \");\n  return rows;\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  // A 9:16 frame is seen on a phone: the plate reads bigger there.\n  const k = $.k * (A.tall ? 1.2 : 1);\n  const tin = $.beat(\"in\", 0.15);\n  const right = p.side === \"right\";\n  const side = right ? \"right\" : \"left\";\n  const name = $.case(p.name), role = String(p.role).toUpperCase();\n  const tName = landOn($, p.name, tin);\n  const tRole = landOn($, p.role, tName + 0.45, tName);\n  // Sizes: the name in the display face, the role as a tracked-out mono label — both cut to fit the safe width.\n  const maxW = A.w * (A.tall ? 0.86 : 0.62);\n  const nf = fitFont($, name, \"display\", 88 * k, maxW - 100 * k, { ls: -0.02 });\n  const rf = fitFont($, role, \"mono\", 30 * k, maxW - 100 * k, { ls: 0.14 });\n  const pad = 46 * k;\n  const w = Math.max($.measure(name, nf), $.measure(role, rf) + 38 * k) + pad * 2;\n  const h = nf.size * 1.06 + rf.size * 1.25 + pad * 1.35;\n  const y = A.tall ? H * 0.6 : A.square ? H * 0.66 : H * 0.7;\n  const x = right ? W - A.x - w : A.x;\n  const plate = p.look !== \"minimal\";\n  const tx = right ? x + w - pad - 10 * k : x + pad + 10 * k;\n  const anchor = right ? \"end\" : \"start\";\n  // Everything leaves the way it came: back to its side.\n  const leave = { out: \"slide\", outFrom: side, distance: 30 };\n\n  let s = M.part(\"scrim\", { at: tin - 0.1, in: \"fade\", dur: 0.45, out: \"fade\", vary: false },\n    $.scrim(0, y - 260 * k, W, H - y + 260 * k, { to: \"bottom\", a: plate ? 0.45 : 0.62 }));\n  s += M.part(\"spine\", Object.assign({ at: tin, in: \"grow-y\", origin: \"top\", ease: \"expo\", dur: 0.45, box: { x: right ? x + w - 10 * k : x, y: y, w: 10 * k, h: h } }, leave),\n    $.rect(right ? x + w - 10 * k : x, y, 10 * k, h, { r: 5 * k, fill: c.accent }));\n  if (plate) s += M.part(\"plate\", Object.assign({ at: tin + 0.06, in: \"wipe\", from: side, dur: 0.5, box: { x: right ? x : x + 10 * k, y: y, w: w - 10 * k, h: h } }, leave),\n    $.card(right ? x : x + 10 * k, y, w - 10 * k, h, { r: $.style.radius * 0.4, shadow: \"md\" }));\n  s += M.text(\"name\", Object.assign({ at: tName + 0.16, in: \"rise\", distance: 26 }, leave, nf, { x: tx, y: y + pad * 0.62, anchor: anchor, fill: plate ? c.fg : \"#FFFFFF\" }), name);\n  const ry = y + pad * 0.62 + nf.size * 1.06;\n  const rw = $.measure(role, rf) + 26 * k;\n  s += M.part(\"role\", Object.assign({ at: tRole, in: \"slide\", from: side, distance: 10, box: { x: right ? tx - rw : tx, y: ry, w: rw, h: rf.size } }, leave),\n    $.circle(right ? tx - 7 * k : tx + 7 * k, ry + rf.size * 0.62, 7 * k, { fill: c.accent }),\n    $.text(role, Object.assign({}, rf, { x: right ? tx - 26 * k : tx + 26 * k, y: ry, anchor: anchor, fill: plate ? c.mute : \"rgba(255,255,255,0.78)\" })));\n  return s;\n}\n","libs":[]},{"id":"title-card","name":"Title card","description":"A full-frame chapter or opening title: a kicker chip, the title built letter by letter, a brush underline, a subtitle — over the brand's ground with an outlined number drifting behind.","category":"title","tags":["chapter","section","intro","opening","title"],"placement":"full","duration":{"default":4,"min":2,"max":20},"aspects":["16:9","9:16","1:1","4:5"],"props":{"kicker":{"type":"text","label":"Kicker","description":"a small label above the title (empty for none)","default":"Chapter two","maxLength":28},"title":{"type":"text","label":"Title","default":"Depth over reach","maxLength":48},"subtitle":{"type":"text","label":"Subtitle","default":"Why the best channels say less","maxLength":80},"number":{"type":"text","label":"Number behind","description":"a big outlined number behind the title (\"02\"; empty for none)","default":"02","maxLength":3},"align":{"type":"choice","label":"Align","options":["center","left"],"default":"center"}},"beats":{"in":0.05},"parts":{"ground":"the brand's ground behind it all","number":"the big outlined number","kicker":"the small label above","title":"the title, line by line","underline":"the brush underline","subtitle":"the line under the title"},"style":"studio","pack":"essentials","ref":"essentials/title-card","code":"// MONARC Essentials — helpers this pack's items share (prepended to each item's code). The layout\n// helpers (safeArea, fitFont, fitLines, landOn, widest) live in the motion kit.\n\n/** Words of `text` (with *marks*) laid into `lines` (the same words without marks), for $.sync / $.headline. */\nfunction markedRows(text, lines) {\n  const words = String(text).split(/\\s+/).filter(Boolean);\n  const rows = [];\n  let i = 0;\n  for (let l = 0; l < lines.length; l++) {\n    const n = String(lines[l]).split(/\\s+/).filter(Boolean).length;\n    rows.push(words.slice(i, i + n).join(\" \"));\n    i += n;\n  }\n  if (i < words.length && rows.length) rows[rows.length - 1] += \" \" + words.slice(i).join(\" \");\n  return rows;\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const tin = $.beat(\"in\", 0.05);\n  const left = p.align === \"left\";\n  const x = left ? A.x : W / 2;\n  const anchor = left ? \"start\" : \"middle\";\n  const title = $.case(p.title);\n  const tTitle = landOn($, p.title, tin + 0.3);\n  // The title as big as fits: one line when it can, up to three.\n  const fit = fitLines($, title, Object.assign({}, $.font(\"display\"), { ls: -0.035 }), A.w * (left ? 0.86 : 0.94), Math.min(230 * k, A.h * 0.34), 70 * k, 3);\n  const lh = fit.size * 1.02;\n  const sub = p.subtitle ? $.case(p.subtitle) : \"\";\n  const subF = Object.assign({}, $.font(\"text\"), { size: Math.max(34 * k, Math.min(52 * k, fit.size * 0.26)) });\n  const subLines = sub ? $.wrap(sub, A.w * 0.8, subF) : [];\n  const kick = p.kicker ? $.case(p.kicker) : \"\";\n  const chip = kick ? $.chip.size(kick, { size: 30 * k }) : { w: 0, h: 0 };\n  const chipH = kick ? chip.h + 40 * k : 0;\n  const total = chipH + fit.lines.length * lh + (subLines.length ? 50 * k + subLines.length * subF.size * 1.25 : 0);\n  let y = H / 2 - total / 2;\n\n  let s = M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: W, h: H }, glow: [W * 0.72, H * 0.3] }));\n  if (p.number) {\n    const nf = Object.assign({}, $.font(\"display\"), { size: Math.min(H * 0.62, W * 0.5) });\n    const nx = (left ? W * 0.78 : W / 2) + ($.wave(t, 6) - 0.5) * 40 * k;\n    const nw = $.measure(String(p.number), nf);\n    s += M.part(\"number\", { at: tin, in: \"drop\", distance: 90, box: { x: nx - nw / 2, y: H / 2 - nf.size * 0.55, w: nw, h: nf.size } },\n      $.text(String(p.number), Object.assign({}, nf, { x: nx, y: H / 2 - nf.size * 0.55, anchor: \"middle\", fill: \"none\", stroke: $.rgba(c.fg, 0.13), sw: 3 * k })));\n  }\n  if (kick) {\n    s += M.part(\"kicker\", { at: tin + 0.14, in: \"rise\", distance: 18, box: { x: left ? x : x - chip.w / 2, y: y, w: chip.w, h: chip.h } }, $.chip(kick, x, y, { size: 30 * k, anchor: anchor, dot: true }));\n    y += chipH;\n  }\n  // Each line built letter by letter (word by word in scripts that join), rising on a spring.\n  fit.lines.forEach(function (line, i) {\n    s += M.text(\"title\", Object.assign({ at: tTitle + i * 0.16, in: \"rise\", split: \"letters\", distance: fit.size * 0.6 / k, ease: \"spring(2.4, 7)\", stagger: Math.min(0.04, 0.45 / Math.max(1, line.length)) }, fit.font, { x: x, y: y + i * lh, anchor: anchor }), line);\n  });\n  const lastLine = fit.lines[fit.lines.length - 1];\n  const lastW = $.measure(lastLine, fit.font);\n  const uy = y + (fit.lines.length - 1) * lh + fit.size * 1.0;\n  const ux = left ? x : x - lastW / 2;\n  // The underline draws on once the last letter has landed.\n  const tUnder = tTitle + (fit.lines.length - 1) * 0.16 + Math.min(0.04, 0.45 / Math.max(1, lastLine.length)) * lastLine.length + 0.15;\n  const sw = Math.max(8 * k, fit.size * 0.07);\n  s += M.part(\"underline\", { at: tUnder, in: \"wipe\", from: \"left\", dur: 0.4, box: { x: ux - sw, y: uy - 14 * k, w: lastW + sw * 2, h: 28 * k } }, $.underline(1, 0, ux, uy, lastW, { sw: sw, dur: 0.01 }));\n  y += fit.lines.length * lh + 50 * k;\n  if (subLines.length) s += M.text(\"subtitle\", Object.assign({ at: tUnder + 0.1, in: \"rise\", distance: 20, split: \"lines\", stagger: 0.08 }, subF, { x: x, y: y, anchor: anchor, fill: c.mute, lh: 1.25, maxW: A.w * 0.8 }), sub);\n  return s;\n}\n","libs":[]},{"id":"headline","name":"Kinetic headline","description":"Big words that pop in one by one — on the voice when you give it a transcript — with a marker swiped under the *starred* words.","category":"text","tags":["kinetic typography","words","captions","hook","headline","text","emphasis"],"placement":"overlay","duration":{"default":3.5,"min":1.5,"max":20},"aspects":["16:9","9:16","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"wrap words in *stars* to mark them","default":"Stop editing. *Start directing.*","maxLength":90},"ground":{"type":"choice","label":"Behind the words","description":"none: straight on the footage (with an outline) · scrim: a soft dark wash · panel: the brand's ground, full frame","options":["scrim","none","panel"],"default":"scrim"},"position":{"type":"choice","label":"Position","options":["center","top","bottom"],"default":"center"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.16,"min":0.04,"max":1,"step":0.01}},"beats":{"in":0.1},"parts":{"ground":"the scrim or panel behind the words","words":"the words","marker":"the marker swiped under starred words"},"style":"studio","pack":"essentials","ref":"essentials/headline","code":"// MONARC Essentials — helpers this pack's items share (prepended to each item's code). The layout\n// helpers (safeArea, fitFont, fitLines, landOn, widest) live in the motion kit.\n\n/** Words of `text` (with *marks*) laid into `lines` (the same words without marks), for $.sync / $.headline. */\nfunction markedRows(text, lines) {\n  const words = String(text).split(/\\s+/).filter(Boolean);\n  const rows = [];\n  let i = 0;\n  for (let l = 0; l < lines.length; l++) {\n    const n = String(lines[l]).split(/\\s+/).filter(Boolean).length;\n    rows.push(words.slice(i, i + n).join(\" \"));\n    i += n;\n  }\n  if (i < words.length && rows.length) rows[rows.length - 1] += \" \" + words.slice(i).join(\" \");\n  return rows;\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const tin = $.beat(\"in\", 0.1);\n  const raw = $.case(String(p.text || \"\"));\n  const plain = raw.replace(/\\*/g, \"\");\n  const face = $.font(\"display\");\n  const fit = fitLines($, plain, Object.assign({}, face, { ls: -0.03 }), A.w * 0.94, Math.min(150 * k, A.h * 0.16), 52 * k, A.tall ? 5 : 3);\n  const lh = 1.12;\n  const blockH = fit.size * (1 + (fit.lines.length - 1) * lh);\n  const y = p.position === \"top\" ? A.y : p.position === \"bottom\" ? A.y + A.h - blockH : H / 2 - blockH / 2;\n  // Words, each with its time (on the voice when there is a transcript, else one every `pace`) and whether it is starred.\n  const timed = $.sync(markedRows(raw, fit.lines), tin).reduce(function (a, row) { return a.concat(row); }, []);\n  const at = timed.map(function (w, i) { return $.words.length ? w.at : tin + i * p.pace; });\n  const hl = timed.map(function (w) { return w.hl; });\n  const font = Object.assign({}, fit.font, { x: W / 2, y: y, anchor: \"middle\", lh: lh, maxW: A.w * 0.94 });\n  const lay = M.layout(plain, font, \"words\");\n  const onFootage = p.ground !== \"panel\";\n  let s = \"\";\n  if (p.ground === \"panel\") s += M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: W, h: H } }));\n  else if (p.ground === \"scrim\") {\n    const to = p.position === \"top\" ? \"top\" : \"bottom\";\n    s += M.part(\"ground\", { at: tin - 0.1, in: \"fade\", dur: 0.4, out: \"fade\", vary: false }, p.position === \"center\" ? $.rect(0, 0, W, H, { fill: \"#000000\", op: 0.38 }) : $.scrim(0, to === \"top\" ? 0 : y - 200 * k, W, to === \"top\" ? y + blockH + 200 * k : H - y + 200 * k, { to: to, a: 0.62 }));\n  }\n  // The marker: one swipe behind each run of starred words, once the run's last word is said.\n  let i = 0;\n  while (i < lay.units.length) {\n    if (!hl[i]) { i++; continue; }\n    let j = i;\n    while (j + 1 < lay.units.length && hl[j + 1] && lay.units[j + 1].line === lay.units[i].line) j++;\n    const a = lay.units[i], b = lay.units[j];\n    const box = { x: a.x - fit.size * 0.1, y: a.y + fit.size * 0.18, w: b.x + b.w - a.x + fit.size * 0.2, h: fit.size * 0.86 };\n    s += M.part(\"marker\", { at: (at[j] || tin) + 0.08, in: \"grow-x\", origin: \"left\", ease: \"out\", dur: 0.32, box: box }, $.rect(box.x, box.y, box.w, box.h, { r: fit.size * 0.14, fill: c.accent, transform: $.tf({ r: -1.2, ox: box.x, oy: box.y + box.h / 2 }) }));\n    i = j + 1;\n  }\n  s += M.text(\"words\", Object.assign({ split: \"words\", in: \"rise\", distance: fit.size * 0.4 / k, ease: \"spring(2.4, 7.5)\", unitAt: function (u) { return at[u.index] != null ? at[u.index] : tin; }, fillFor: function () { return onFootage ? \"#FFFFFF\" : c.fg; } }, font), plain);\n  if (p.ground === \"none\") s = $.g({ extra: 'filter=\"url(#mg-md)\"' }, s);\n  return s;\n}\n","libs":[]},{"id":"subscribe","name":"Subscribe","description":"A call to action over footage: the channel card springs in, a cursor glides onto the button and presses it, the bell rings.","category":"cta","tags":["subscribe","follow","youtube","cta","button","bell","channel"],"placement":"overlay","duration":{"default":4.5,"min":3,"max":15},"aspects":["16:9","9:16","1:1","4:5"],"props":{"channel":{"type":"text","label":"Channel","default":"MONARC Studio","maxLength":26},"detail":{"type":"text","label":"Line under it","default":"1.2M subscribers","maxLength":30},"button":{"type":"text","label":"Button","default":"Subscribe","maxLength":14},"done":{"type":"text","label":"After the press","default":"Subscribed","maxLength":14},"avatar":{"type":"image","label":"Avatar","description":"a square picture for the channel (a play mark when empty)","default":""},"position":{"type":"choice","label":"Position","options":["auto","bottom-left","bottom-right","center"],"default":"auto"},"cursor":{"type":"boolean","label":"Cursor","default":true},"bell":{"type":"boolean","label":"Bell","default":true}},"beats":{"in":0.1},"parts":{"card":"the channel card","cursor":"the cursor that presses the button"},"style":"studio","pack":"essentials","ref":"essentials/subscribe","code":"// MONARC Essentials — helpers this pack's items share (prepended to each item's code). The layout\n// helpers (safeArea, fitFont, fitLines, landOn, widest) live in the motion kit.\n\n/** Words of `text` (with *marks*) laid into `lines` (the same words without marks), for $.sync / $.headline. */\nfunction markedRows(text, lines) {\n  const words = String(text).split(/\\s+/).filter(Boolean);\n  const rows = [];\n  let i = 0;\n  for (let l = 0; l < lines.length; l++) {\n    const n = String(lines[l]).split(/\\s+/).filter(Boolean).length;\n    rows.push(words.slice(i, i + n).join(\" \"));\n    i += n;\n  }\n  if (i < words.length && rows.length) rows[rows.length - 1] += \" \" + words.slice(i).join(\" \");\n  return rows;\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const tin = $.beat(\"in\", 0.1);\n  // A 16:9 frame has room for a bigger card; a 9:16 one is seen on a phone.\n  const k = $.k * (A.wide ? 1.2 : A.tall ? 1.3 : 1.1);\n  const press = landOn($, p.button, Math.min(tin + 1.1, M.outAt - 0.8), tin);\n  const name = $.case(p.channel);\n  const nf = fitFont($, name, \"display\", 42 * k, 430 * k);\n  const cw = Math.max(560 * k, 160 * k + $.measure(name, nf) + 60 * k), ch = 300 * k;\n  const pos = p.position === \"auto\" ? (A.wide ? \"bottom-right\" : \"center\") : p.position;\n  const x = pos === \"center\" ? (W - cw) / 2 : pos === \"bottom-left\" ? A.x : W - A.x - cw;\n  const y = pos === \"center\" ? (A.tall ? H * 0.58 : (H - ch) / 2) : A.y + A.h - ch;\n\n  let card = $.card(x, y, cw, ch, { shadow: \"lg\" });\n  if (p.avatar) card += $.image(\"avatar\", x + 38 * k, y + 40 * k, 80 * k, 80 * k, { r: 40 * k });\n  else {\n    card += $.circle(x + 78 * k, y + 80 * k, 40 * k, { fill: c.accent });\n    card += $.icon(\"play\", x + 60 * k, y + 62 * k, 36 * k, { color: $.on(c.accent), fill: true });\n  }\n  card += $.text(name, Object.assign({}, nf, { x: x + 140 * k, y: y + 46 * k }));\n  if (p.detail) card += $.text(p.detail, Object.assign({}, $.font(\"mono\"), { size: 24 * k, x: x + 140 * k, y: y + 98 * k, fill: c.mute }));\n  const bx = x + 44 * k, by = y + 168 * k;\n  card += $.button(t, $.case(p.button), bx, by, { size: 36 * k, icon: p.bell ? \"bell\" : undefined, pressAt: press, done: $.case(p.done) });\n  if (p.bell) {\n    const ring = $.bump(t, press + 0.15, 0.05, 0.6);\n    const bellX = x + cw - 92 * k, bellY = y + 176 * k;\n    card += $.g({ transform: $.tf({ r: Math.sin((t - press) * 40) * 18 * ring, ox: bellX + 28 * k, oy: bellY }) }, $.icon(\"bell\", bellX, bellY, 56 * k, { color: ring > 0.05 ? c.accent : c.mute }));\n  }\n  let s = M.part(\"card\", { at: tin, in: \"rise\", distance: 70, out: \"rise\", box: { x: x, y: y, w: cw, h: ch } }, card);\n  // The cursor glides in from below and presses the button.\n  if (p.cursor && t > press - 0.8 && t < press + 0.9) {\n    const glide = $.E.inOut3($.seg(t, press - 0.75, press - 0.04));\n    const px = $.lerp(x + cw + 180 * k, bx + 150 * k, glide), py = $.lerp(Math.min(H, y + ch + 220 * k), by + 52 * k, glide);\n    s += M.part(\"cursor\", { at: press - 0.8, in: \"fade\", dur: 0.2, outAt: press + 0.6, out: \"fade\", outDur: 0.3 }, $.cursor(px, py, { press: $.bump(t, press, 0.05, 0.2), scale: 2 * k }));\n  }\n  return s;\n}\n","libs":[]},{"id":"social","name":"Social handle","description":"Your handle in a corner: a pill with the platform's icon slides in, a follow button presses, it tucks away.","category":"social","tags":["handle","follow","instagram","tiktok","youtube","x","linkedin","username","social"],"placement":"overlay","duration":{"default":4,"min":2.5,"max":30},"aspects":["16:9","9:16","1:1","4:5"],"props":{"handle":{"type":"text","label":"Handle","default":"@monarcstudio","maxLength":30},"platform":{"type":"choice","label":"Platform","description":"picks the icon and the line under the handle","options":["youtube","instagram","tiktok","x","linkedin","threads","website"],"default":"instagram"},"action":{"type":"text","label":"Button","description":"empty for no button","default":"Follow","maxLength":12},"done":{"type":"text","label":"After the press","default":"Following","maxLength":12},"corner":{"type":"choice","label":"Corner","options":["bottom-left","bottom-right","top-left","top-right","bottom-center"],"default":"bottom-left"}},"beats":{"in":0.15},"parts":{"pill":"the pill behind","icon":"the platform icon","handle":"the handle and platform line","button":"the follow button"},"style":"studio","pack":"essentials","ref":"essentials/social","code":"// MONARC Essentials — helpers this pack's items share (prepended to each item's code). The layout\n// helpers (safeArea, fitFont, fitLines, landOn, widest) live in the motion kit.\n\n/** Words of `text` (with *marks*) laid into `lines` (the same words without marks), for $.sync / $.headline. */\nfunction markedRows(text, lines) {\n  const words = String(text).split(/\\s+/).filter(Boolean);\n  const rows = [];\n  let i = 0;\n  for (let l = 0; l < lines.length; l++) {\n    const n = String(lines[l]).split(/\\s+/).filter(Boolean).length;\n    rows.push(words.slice(i, i + n).join(\" \"));\n    i += n;\n  }\n  if (i < words.length && rows.length) rows[rows.length - 1] += \" \" + words.slice(i).join(\" \");\n  return rows;\n}\n\nvar PLATFORMS = {\n  youtube: { icon: \"play\", line: \"YouTube\" },\n  instagram: { icon: \"camera\", line: \"Instagram\" },\n  tiktok: { icon: \"reel\", line: \"TikTok\" },\n  x: { icon: \"x\", line: \"X\" },\n  linkedin: { icon: \"users\", line: \"LinkedIn\" },\n  threads: { icon: \"chat\", line: \"Threads\" },\n  website: { icon: \"globe\", line: \"Website\" },\n};\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k * 1.05, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const tin = $.beat(\"in\", 0.15);\n  const pl = PLATFORMS[p.platform] || PLATFORMS.instagram;\n  const hf = fitFont($, p.handle, \"display\", 40 * k, A.w * 0.5);\n  const lf = Object.assign({}, $.font(\"mono\"), { size: 20 * k, ls: 0.14 });\n  const hasBtn = !!String(p.action || \"\").trim();\n  const b1 = hasBtn ? $.button.size($.case(p.action), { size: 26 * k }) : { w: 0, h: 0 };\n  const b2 = hasBtn ? $.button.size($.case(p.done || p.action), { size: 26 * k, icon: \"check\" }) : { w: 0, h: 0 };\n  const bw = Math.max(b1.w, b2.w);\n  const ph = 112 * k;\n  const pw = ph + Math.max($.measure(p.handle, hf), $.measure(pl.line.toUpperCase(), lf)) + 34 * k + (hasBtn ? bw + 30 * k : 10 * k);\n  const corner = p.corner;\n  const x = corner === \"bottom-center\" ? (W - pw) / 2 : corner.indexOf(\"left\") >= 0 ? A.x : W - A.x - pw;\n  const y = corner.indexOf(\"top\") === 0 ? A.y : A.y + A.h - ph;\n  const side = corner.indexOf(\"right\") >= 0 ? \"right\" : corner === \"bottom-center\" ? \"down\" : \"left\";\n  const press = landOn($, p.action, tin + 0.9, tin);\n  // Every part leaves the way the pill came in, off its own side.\n  const leave = { out: \"slide\", outFrom: side, distance: 50 };\n\n  let s = M.part(\"pill\", Object.assign({ at: tin, in: \"slide\", from: side, box: { x: x, y: y, w: pw, h: ph } }, leave), $.rect(x, y, pw, ph, { r: ph / 2, fill: c.surface, stroke: c.line, sw: 2 * k, shadow: \"lg\" }));\n  s += M.part(\"icon\", Object.assign({ at: tin + 0.1, in: \"spin\", intensity: 0.12, box: { x: x + ph * 0.14, y: y + ph * 0.14, w: ph * 0.72, h: ph * 0.72 } }, leave),\n    $.circle(x + ph / 2, y + ph / 2, ph * 0.36, { fill: c.accent }) +\n    $.icon(pl.icon, x + ph / 2 - ph * 0.17, y + ph / 2 - ph * 0.17, ph * 0.34, { color: $.on(c.accent), sw: 2.4 }));\n  s += M.part(\"handle\", Object.assign({ at: tin + 0.2, in: \"slide\", from: \"right\", distance: 8, box: { x: x + ph, y: y + ph * 0.2, w: pw - ph, h: ph * 0.6 } }, leave),\n    $.text(p.handle, Object.assign({}, hf, { x: x + ph + 4 * k, y: y + ph * 0.2 })) +\n    $.text(pl.line.toUpperCase(), Object.assign({}, lf, { x: x + ph + 6 * k, y: y + ph * 0.2 + hf.size * 1.08, fill: c.mute })));\n  if (hasBtn) s += M.part(\"button\", Object.assign({ at: tin + 0.35, in: \"pop\", box: { x: x + pw - 22 * k - bw, y: y + (ph - b1.h) / 2, w: bw, h: b1.h } }, leave),\n    $.button(t, $.case(p.action), x + pw - 22 * k - bw, y + (ph - b1.h) / 2, { size: 26 * k, pressAt: press, done: $.case(p.done || p.action) }));\n  return s;\n}\n","libs":[]},{"id":"callout","name":"Callout","description":"Points at something in the shot: a ring pulses on the spot, a line draws out to a label card that pops in beside it.","category":"callout","tags":["annotation","pointer","arrow","label","highlight","tutorial","product"],"placement":"overlay","duration":{"default":4,"min":2,"max":20},"aspects":["16:9","9:16","1:1","4:5"],"props":{"label":{"type":"text","label":"Label","default":"Export in one click","maxLength":40},"detail":{"type":"text","label":"Second line","default":"ProRes, WebM or MP4","maxLength":48},"x":{"type":"number","label":"Spot across","description":"where the spot is, 0 (left edge) to 1 (right edge)","default":0.62,"min":0,"max":1,"step":0.01},"y":{"type":"number","label":"Spot down","description":"where the spot is, 0 (top) to 1 (bottom)","default":0.44,"min":0,"max":1,"step":0.01},"side":{"type":"choice","label":"Label side","options":["auto","left","right","above","below"],"default":"auto"},"icon":{"type":"icon","label":"Icon","description":"an icon name (volito api lists them; empty for none)","default":"sparkle"}},"beats":{"in":0.1},"parts":{"pulse":"the ring pulsing on the spot","dot":"the spot","line":"the line out to the card","card":"the label card"},"style":"studio","pack":"essentials","ref":"essentials/callout","code":"// MONARC Essentials — helpers this pack's items share (prepended to each item's code). The layout\n// helpers (safeArea, fitFont, fitLines, landOn, widest) live in the motion kit.\n\n/** Words of `text` (with *marks*) laid into `lines` (the same words without marks), for $.sync / $.headline. */\nfunction markedRows(text, lines) {\n  const words = String(text).split(/\\s+/).filter(Boolean);\n  const rows = [];\n  let i = 0;\n  for (let l = 0; l < lines.length; l++) {\n    const n = String(lines[l]).split(/\\s+/).filter(Boolean).length;\n    rows.push(words.slice(i, i + n).join(\" \"));\n    i += n;\n  }\n  if (i < words.length && rows.length) rows[rows.length - 1] += \" \" + words.slice(i).join(\" \");\n  return rows;\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const tin = $.beat(\"in\", 0.1);\n  const sx = W * p.x, sy = H * p.y;\n  const label = $.case(p.label);\n  const lf = fitFont($, label, \"display\", 46 * k, A.w * 0.5);\n  const df = Object.assign({}, $.font(\"text\"), { size: 30 * k });\n  const hasIcon = !!p.icon && $.icons.indexOf(p.icon) >= 0;\n  const iw = hasIcon ? 64 * k : 0;\n  const cw = Math.max($.measure(label, lf), p.detail ? $.measure(p.detail, df) : 0) + iw + 72 * k;\n  const ch = lf.size * 1.15 + (p.detail ? df.size * 1.35 : 0) + 56 * k;\n  // The label sits where there is room: away from the nearer edge.\n  let side = p.side;\n  if (side === \"auto\") side = A.wide ? (p.x > 0.5 ? \"left\" : \"right\") : p.y > 0.5 ? \"above\" : \"below\";\n  const gap = 150 * k;\n  let cx = side === \"left\" ? sx - gap - cw : side === \"right\" ? sx + gap : sx - cw / 2;\n  let cy = side === \"above\" ? sy - gap - ch : side === \"below\" ? sy + gap : sy - ch / 2;\n  cx = Math.max(A.x, Math.min(W - A.x - cw, cx));\n  cy = Math.max(A.y, Math.min(A.y + A.h - ch, cy));\n  // The line runs from the spot to the card's nearest edge.\n  const ex = side === \"left\" ? cx + cw : side === \"right\" ? cx : Math.max(cx + 30 * k, Math.min(cx + cw - 30 * k, sx));\n  const ey = side === \"above\" ? cy + ch : side === \"below\" ? cy : Math.max(cy + 20 * k, Math.min(cy + ch - 20 * k, sy));\n  const dl = Math.hypot(ex - sx, ey - sy) || 1;\n  const r0 = 26 * k;\n  const lx = sx + ((ex - sx) / dl) * r0, ly = sy + ((ey - sy) / dl) * r0;\n  // The line draws out from the spot: a wipe from the spot's side.\n  const lineFrom = Math.abs(ex - sx) > Math.abs(ey - sy) ? (ex > sx ? \"left\" : \"right\") : ey > sy ? \"up\" : \"down\";\n\n  const pulse = ((t - tin) % 1.4) / 1.4;\n  let s = M.part(\"pulse\", { at: tin, in: \"fade\", dur: 0.2, box: { x: sx - 70 * k, y: sy - 70 * k, w: 140 * k, h: 140 * k } },\n    t > tin ? $.circle(sx, sy, (18 + 50 * $.E.out3(pulse)) * k, { stroke: c.accent, sw: 4 * k * (1 - pulse), op: 1 - pulse }) : \"\");\n  s += M.part(\"dot\", { at: tin, in: \"pop\", box: { x: sx - 21 * k, y: sy - 21 * k, w: 42 * k, h: 42 * k } }, $.circle(sx, sy, 16 * k, { fill: c.accent, stroke: \"#FFFFFF\", sw: 5 * k, shadow: \"sm\" }));\n  s += M.part(\"line\", { at: tin + 0.25, in: \"wipe\", from: lineFrom, ease: \"inOut\", dur: 0.35, box: { x: Math.min(lx, ex) - 4 * k, y: Math.min(ly, ey) - 4 * k, w: Math.abs(ex - lx) + 8 * k, h: Math.abs(ey - ly) + 8 * k } },\n    $.line(lx, ly, ex, ey, { stroke: \"#FFFFFF\", sw: 4 * k }));\n  let card = $.card(cx, cy, cw, ch, { shadow: \"lg\" });\n  if (hasIcon) card += $.rect(cx + 28 * k, cy + 28 * k, 48 * k, 48 * k, { r: 14 * k, fill: c.tint }) + $.icon(p.icon, cx + 36 * k, cy + 36 * k, 32 * k, { color: c.accent });\n  card += $.text(label, Object.assign({}, lf, { x: cx + 36 * k + iw, y: cy + 28 * k }));\n  if (p.detail) card += $.text(p.detail, Object.assign({}, df, { x: cx + 36 * k + iw, y: cy + 28 * k + lf.size * 1.2, fill: c.mute }));\n  s += M.part(\"card\", { at: tin + 0.55, in: \"pop\", box: { x: cx, y: cy, w: cw, h: ch }, ox: ex, oy: ey }, card);\n  return s;\n}\n","libs":[]},{"id":"stat","name":"Stat","description":"One number that matters, full frame: it counts up as it lands, the label rises under it, a ring fills to the value or bars grow to it.","category":"stat","tags":["number","statistic","count","percent","growth","kpi","data"],"placement":"full","duration":{"default":4,"min":2,"max":20},"aspects":["16:9","9:16","1:1","4:5"],"props":{"value":{"type":"number","label":"Value","default":3.2},"decimals":{"type":"number","label":"Decimals","default":1,"min":0,"max":3,"step":1},"prefix":{"type":"text","label":"Before the number","default":"","maxLength":4},"suffix":{"type":"text","label":"After the number","default":"×","maxLength":6},"label":{"type":"text","label":"Label","default":"more reach in 90 days","maxLength":48},"context":{"type":"text","label":"Small print","description":"a source or a comparison, in the mono face","default":"Across 1,200 creator channels","maxLength":60},"chart":{"type":"choice","label":"Chart","options":["bars","ring","none"],"default":"bars"},"max":{"type":"number","label":"Ring full at","description":"the value a full ring stands for (100 for a percent)","default":100,"when":{"prop":"chart","is":["ring"]}},"series":{"type":"list","label":"Bars","description":"the bars' values, the last one is the stat (empty: four steps up to it)","of":"number","default":[],"maxItems":8,"when":{"prop":"chart","is":["bars"]}}},"beats":{"in":0.1},"parts":{"ground":"the brand's ground","number":"the big number","label":"the label under it","context":"the small print","chart":"the bars or ring"},"style":"studio","pack":"essentials","ref":"essentials/stat","code":"// MONARC Essentials — helpers this pack's items share (prepended to each item's code). The layout\n// helpers (safeArea, fitFont, fitLines, landOn, widest) live in the motion kit.\n\n/** Words of `text` (with *marks*) laid into `lines` (the same words without marks), for $.sync / $.headline. */\nfunction markedRows(text, lines) {\n  const words = String(text).split(/\\s+/).filter(Boolean);\n  const rows = [];\n  let i = 0;\n  for (let l = 0; l < lines.length; l++) {\n    const n = String(lines[l]).split(/\\s+/).filter(Boolean).length;\n    rows.push(words.slice(i, i + n).join(\" \"));\n    i += n;\n  }\n  if (i < words.length && rows.length) rows[rows.length - 1] += \" \" + words.slice(i).join(\" \");\n  return rows;\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const tin = $.beat(\"in\", 0.1);\n  const tNum = landOn($, String(p.value), tin + 0.15);\n  const tLabel = landOn($, p.label, tNum + 0.55, tNum);\n  const dec = Math.max(0, Math.min(3, Math.round(p.decimals)));\n  // The widest the counter gets (what $.counter draws at the end), so the size never jumps.\n  const final = (p.prefix || \"\") + (dec ? Number(p.value).toFixed(dec) : String(Math.round(Number(p.value))).replace(/\\B(?=(\\d{3})+(?!\\d))/g, \",\")) + (p.suffix || \"\");\n  const chart = p.chart;\n  const side = chart !== \"none\" && A.wide;\n  // The number takes the room it owns: the left half beside a chart on 16:9, the width otherwise.\n  const room = side ? A.w * 0.5 : A.w * 0.9;\n  const big = fitFont($, final, \"display\", Math.min(340 * k, A.h * (chart !== \"none\" && !side ? 0.26 : 0.42)), room, { ls: -0.05 });\n  const label = $.case(p.label);\n  const lf = fitFont($, label, \"text\", Math.max(40 * k, big.size * 0.2), room);\n  const cf = Object.assign({}, $.font(\"mono\"), { size: 26 * k, ls: 0.06 });\n  const blockH = big.size * 1.02 + lf.size * 1.3 + (p.context ? cf.size * 2.2 : 0);\n  const chartH = chart === \"none\" || side ? 0 : A.h * 0.34;\n  const top = A.y + (A.h - blockH - chartH - (chartH ? 60 * k : 0)) / 2;\n  const x = side ? A.x + A.w * 0.02 : W / 2;\n  const anchor = side ? \"start\" : \"middle\";\n\n  let s = M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: W, h: H } }));\n  // The number counts up over the time its entrance takes (at least 0.9 s).\n  const np = M.progress(\"number\", { at: tNum, in: \"rise\", distance: 40, dur: 0.9 });\n  const counting = $.counter(t, np.at, np.at + Math.max(0.9, np.end - np.at), Number(p.value), { dec: dec, prefix: p.prefix || \"\", suffix: p.suffix || \"\" });\n  s += M.text(\"number\", Object.assign({ at: tNum, in: \"rise\", distance: 40 }, big, { x: x, y: top, anchor: anchor, fill: c.accent }), counting);\n  s += M.text(\"label\", Object.assign({ at: tLabel, in: \"rise\", distance: 20 }, lf, { x: x, y: top + big.size * 1.04, anchor: anchor }), label);\n  if (p.context) s += M.text(\"context\", Object.assign({ at: tLabel + 0.25, in: \"fade\", dur: 0.4 }, cf, { x: x, y: top + big.size * 1.04 + lf.size * 1.45, anchor: anchor, fill: c.mute, maxW: room }), p.context);\n\n  // The chart: beside the number on 16:9, under it elsewhere.\n  const box = side ? { x: A.x + A.w * 0.58, y: A.y + A.h * 0.12, w: A.w * 0.4, h: A.h * 0.76 } : { x: W / 2 - Math.min(A.w * 0.8, chartH * 1.6) / 2, y: top + blockH + 60 * k, w: Math.min(A.w * 0.8, chartH * 1.6), h: chartH };\n  if (chart === \"ring\") {\n    const r = Math.min(box.w, box.h) * 0.4, cx = box.x + box.w / 2, cy = box.y + box.h / 2;\n    const fill = Math.max(0, Math.min(1, Number(p.value) / (Number(p.max) || 100)));\n    s += M.part(\"chart\", { at: tNum - 0.05, in: \"pop\", box: { x: cx - r * 1.1, y: cy - r * 1.1, w: r * 2.2, h: r * 2.2 } },\n      $.ring(cx, cy, r, fill * $.E.out3($.seg(t, tNum, tNum + 0.9)), { sw: r * 0.16 }),\n      $.text(Math.round(fill * 100) + \"%\", Object.assign({}, $.font(\"mono\"), { size: r * 0.34, x: cx, y: cy - r * 0.2, anchor: \"middle\", fill: c.fg, op: $.fadeIn(t, tNum + 0.6, 0.3) })));\n  } else if (chart === \"bars\") {\n    const v = Number(p.value) || 1;\n    const series = Array.isArray(p.series) && p.series.length >= 2 ? p.series.map(Number) : [v * 0.34, v * 0.52, v * 0.71, v];\n    s += M.part(\"chart\", { at: tNum - 0.1, in: \"fade\", dur: 0.2, box: box },\n      $.bars(t, tNum + 0.05, box.x, box.y, box.w, box.h, series, { highlight: series.length - 1, stagger: 0.1 }),\n      $.line(box.x - 10 * k, box.y + box.h, box.x + box.w + 10 * k, box.y + box.h, { stroke: c.line, sw: 3 * k, p: $.E.out3($.seg(t, tNum - 0.1, tNum + 0.4)) }));\n  }\n  return s;\n}\n","libs":[]},{"id":"bar-chart","name":"Bar chart","description":"A ranked bar chart, full frame: each bar grows in turn with its value counting up beside it; one row can stand out in the accent.","category":"chart","tags":["chart","data","ranking","comparison","bars","graph","statistics"],"placement":"full","duration":{"default":6,"min":3,"max":30},"aspects":["16:9","9:16","1:1","4:5"],"props":{"title":{"type":"text","label":"Heading","default":"Where viewers drop off","maxLength":48},"rows":{"type":"list","label":"Rows","description":"label and value per row (\"Intro: 92\")","of":{"label":"text","value":"number"},"default":[{"label":"First 3 seconds","value":92},{"label":"First 30 seconds","value":64},{"label":"Halfway","value":41},{"label":"The end","value":23}],"minItems":2,"maxItems":7},"prefix":{"type":"text","label":"Before each value","default":"","maxLength":3},"suffix":{"type":"text","label":"After each value","default":"%","maxLength":6},"decimals":{"type":"number","label":"Decimals","default":0,"min":0,"max":2,"step":1},"highlight":{"type":"number","label":"Row in the accent","description":"1 for the first row; 0 for the largest value; -1 for none","default":0,"min":-1,"max":7,"step":1},"source":{"type":"text","label":"Source","default":"Retention across 300 uploads","maxLength":60}},"beats":{"in":0.1},"parts":{"ground":"the brand's ground","title":"the heading","rows":"each row (label, bar, value)","source":"the source line"},"style":"studio","pack":"essentials","ref":"essentials/bar-chart","code":"// MONARC Essentials — helpers this pack's items share (prepended to each item's code). The layout\n// helpers (safeArea, fitFont, fitLines, landOn, widest) live in the motion kit.\n\n/** Words of `text` (with *marks*) laid into `lines` (the same words without marks), for $.sync / $.headline. */\nfunction markedRows(text, lines) {\n  const words = String(text).split(/\\s+/).filter(Boolean);\n  const rows = [];\n  let i = 0;\n  for (let l = 0; l < lines.length; l++) {\n    const n = String(lines[l]).split(/\\s+/).filter(Boolean).length;\n    rows.push(words.slice(i, i + n).join(\" \"));\n    i += n;\n  }\n  if (i < words.length && rows.length) rows[rows.length - 1] += \" \" + words.slice(i).join(\" \");\n  return rows;\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const tin = $.beat(\"in\", 0.1);\n  const rows = (p.rows || []).slice(0, 7);\n  const n = Math.max(1, rows.length);\n  const max = Math.max.apply(null, rows.map(function (r) { return Number(r.value) || 0; }).concat([1e-9]));\n  let hi = Math.round(p.highlight);\n  if (hi === 0) hi = rows.reduce(function (b, r, i) { return (Number(r.value) || 0) > (Number(rows[b].value) || 0) ? i : b; }, 0);\n  else hi = hi - 1;\n  const title = $.case(p.title);\n  const tf = fitLines($, title, Object.assign({}, $.font(\"display\"), { ls: -0.03 }), A.w, Math.min((A.tall ? 120 : 100) * k, A.h * 0.1), 44 * k, 2);\n  const headH = tf.lines.length * tf.size * 1.05 + 60 * k;\n  const footH = p.source ? 70 * k : 0;\n  const gapY = 26 * k;\n  const rh = Math.min((A.tall ? 210 : A.wide ? 150 : 185) * k, (A.h - headH - footH - gapY * (n - 1)) / n);\n  const top = A.y + (A.h - headH - footH - n * rh - (n - 1) * gapY) / 2;\n  const w = A.wide ? Math.min(A.w, 1500 * k) : A.w;\n  const x = (W - w) / 2;\n  const fmt = function (v) { return (p.prefix || \"\") + (p.decimals ? v.toFixed(p.decimals) : String(Math.round(v)).replace(/\\B(?=(\\d{3})+(?!\\d))/g, \",\")) + (p.suffix || \"\"); };\n  // Labels sit above their bar on tall frames, beside it on wide ones.\n  const lf = Object.assign({}, $.font(\"text\"), { size: Math.min((A.tall ? 46 : 40) * k, rh * (A.wide ? 0.4 : 0.3)) });\n  const labels = rows.map(function (r) { return $.case(r.label); });\n  const labelW = A.wide ? Math.min(w * 0.34, widest($, labels, lf) + 44 * k) : 0;\n  const vf = Object.assign({}, $.font(\"mono\"), { size: Math.min(44 * k, rh * 0.36) });\n  const valueW = $.measure(fmt(max), vf) + 30 * k;\n  const barX = x + labelW;\n  const barW = w - labelW - valueW;\n\n  let s = M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: W, h: H } }));\n  tf.lines.forEach(function (line, i) {\n    s += M.text(\"title\", Object.assign({ at: tin + i * 0.1, in: \"rise\", distance: 26 }, tf.font, { x: x, y: top + i * tf.size * 1.05 }), line);\n  });\n  const start = tin + 0.4;\n  rows.forEach(function (r, i) {\n    const at = landOn($, r.label, start + i * 0.18, start);\n    const ry = top + headH + i * (rh + gapY);\n    const v = Number(r.value) || 0;\n    // The bar grows (and its value counts) over the row's entrance, at least 0.8 s.\n    const rp = M.progress(\"rows\", { at: at - 0.05, in: \"fade\", dur: 0.25 });\n    const grow = $.E.out3($.seg(t, rp.at, rp.at + 0.8));\n    const bh = A.wide ? rh * 0.66 : rh * 0.48;\n    const by = A.wide ? ry + (rh - bh) / 2 : ry + rh - bh;\n    const col = hi === i ? c.accent : $.rgba(c.fg, 0.28);\n    let row = \"\";\n    if (A.wide) row += $.text(labels[i], Object.assign({}, lf, { x: x, y: ry + (rh - lf.size) / 2, fill: hi === i ? c.fg : c.mute, maxW: labelW - 20 * k }));\n    else row += $.text(labels[i], Object.assign({}, lf, { x: x, y: ry, fill: hi === i ? c.fg : c.mute }));\n    row += $.rect(barX, by, barW, bh, { r: bh * 0.22, fill: $.rgba(c.fg, 0.06) });\n    row += $.rect(barX, by, Math.max(bh * 0.44, barW * (v / max) * grow), bh, { r: bh * 0.22, fill: col, shadow: hi === i ? \"glow\" : null });\n    row += $.text(fmt(v * grow), Object.assign({}, vf, { x: barX + barW + 24 * k, y: by + (bh - vf.size) / 2 - vf.size * 0.05, fill: hi === i ? c.accent : c.fg }));\n    s += M.part(\"rows\", { at: at - 0.05, in: \"fade\", dur: 0.25, box: { x: x, y: ry, w: w, h: rh } }, row);\n  });\n  if (p.source) s += M.text(\"source\", Object.assign({ at: start + n * 0.18 + 0.4, in: \"fade\", dur: 0.4 }, $.font(\"mono\"), { size: 24 * k, x: x, y: top + headH + n * rh + (n - 1) * gapY + 34 * k, fill: c.mute, ls: 0.06 }), p.source);\n  return s;\n}\n","libs":[]},{"id":"steps","name":"Steps","description":"A numbered list or checklist, full frame: the heading lands, then each step slides in on its own card — numbered, or ticked off one by one.","category":"list","tags":["list","steps","how to","process","checklist","agenda","tips","framework"],"placement":"full","duration":{"default":6,"min":3,"max":30},"aspects":["16:9","9:16","1:1","4:5"],"props":{"kicker":{"type":"text","label":"Kicker","default":"The framework","maxLength":28},"title":{"type":"text","label":"Heading","default":"Three steps to a better hook","maxLength":48},"steps":{"type":"list","label":"Steps","of":"text","default":["Open on the payoff","Cut every pause","Land one idea per shot"],"minItems":1,"maxItems":6},"marker":{"type":"choice","label":"Marker","options":["numbers","checks"],"default":"numbers"},"gap":{"type":"number","label":"Seconds between steps","default":0.35,"min":0.1,"max":5,"step":0.05}},"beats":{"in":0.1},"parts":{"ground":"the brand's ground","kicker":"the small label","title":"the heading","steps":"each step's card"},"style":"studio","pack":"essentials","ref":"essentials/steps","code":"// MONARC Essentials — helpers this pack's items share (prepended to each item's code). The layout\n// helpers (safeArea, fitFont, fitLines, landOn, widest) live in the motion kit.\n\n/** Words of `text` (with *marks*) laid into `lines` (the same words without marks), for $.sync / $.headline. */\nfunction markedRows(text, lines) {\n  const words = String(text).split(/\\s+/).filter(Boolean);\n  const rows = [];\n  let i = 0;\n  for (let l = 0; l < lines.length; l++) {\n    const n = String(lines[l]).split(/\\s+/).filter(Boolean).length;\n    rows.push(words.slice(i, i + n).join(\" \"));\n    i += n;\n  }\n  if (i < words.length && rows.length) rows[rows.length - 1] += \" \" + words.slice(i).join(\" \");\n  return rows;\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const tin = $.beat(\"in\", 0.1);\n  const rows = (p.steps || []).slice(0, 6);\n  const n = Math.max(1, rows.length);\n  const kick = p.kicker ? $.case(p.kicker) : \"\";\n  const title = $.case(p.title);\n  const tf = fitLines($, title, Object.assign({}, $.font(\"display\"), { ls: -0.03 }), A.w, Math.min((A.tall ? 140 : 110) * k, A.h * 0.11), 48 * k, 2);\n  const chip = kick ? $.chip.size(kick, { size: 28 * k }) : { w: 0, h: 0 };\n  const chipH = kick ? chip.h + 28 * k : 0;\n  const headH = chipH + tf.lines.length * tf.size * 1.05 + 56 * k;\n  // Rows share the room under the heading, with a gap.\n  const room = A.h - headH;\n  const gapY = 22 * k;\n  const rh = Math.min((A.tall ? 200 : A.wide ? 150 : 180) * k, (room - gapY * (n - 1)) / n);\n  const total = headH + n * rh + (n - 1) * gapY;\n  let y = A.y + (A.h - total) / 2;\n  const w = A.tall ? A.w : Math.min(A.w, 1500 * k);\n  const ox = A.wide ? (W - w) / 2 : A.x;\n\n  let s = M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: W, h: H }, glow: [W * 0.8, H * 0.2] }));\n  if (kick) {\n    s += M.part(\"kicker\", { at: tin, in: \"rise\", distance: 16, box: { x: ox, y: y, w: chip.w, h: chip.h } }, $.chip(kick, ox, y, { size: 28 * k, dot: true }));\n    y += chipH;\n  }\n  tf.lines.forEach(function (line, i) {\n    s += M.text(\"title\", Object.assign({ at: tin + 0.12 + i * 0.1, in: \"rise\", distance: 28 }, tf.font, { x: ox, y: y + i * tf.size * 1.05 }), line);\n  });\n  y += tf.lines.length * tf.size * 1.05 + 56 * k;\n  const first = landOn($, rows[0], tin + 0.55);\n  rows.forEach(function (row, i) {\n    const at = landOn($, row, first + i * p.gap, first + (i - 1) * p.gap);\n    const ry = y + i * (rh + gapY);\n    const badge = rh * 0.62;\n    const label = $.case(row);\n    const lf = fitFont($, label, \"display\", Math.min(60 * k, rh * 0.42), w - badge - rh * 0.9);\n    let card = $.card(ox, ry, w, rh, { r: Math.min($.style.radius, rh * 0.3), shadow: \"md\" });\n    const bx = ox + (rh - badge) / 2, by = ry + (rh - badge) / 2;\n    if (p.marker === \"checks\") {\n      const done = $.E.out3($.seg(t, at + 0.25, at + 0.55));\n      card += $.rect(bx, by, badge, badge, { r: badge * 0.28, fill: done > 0.5 ? c.accent : \"none\", stroke: c.accent, sw: 4 * k });\n      card += $.path(\"M\" + (bx + badge * 0.24) + \" \" + (by + badge * 0.52) + \"L\" + (bx + badge * 0.43) + \" \" + (by + badge * 0.7) + \"L\" + (bx + badge * 0.78) + \" \" + (by + badge * 0.32), { stroke: $.on(c.accent), sw: badge * 0.11, draw: done });\n    } else {\n      card += $.rect(bx, by, badge, badge, { r: badge * 0.28, fill: c.accent });\n      card += $.text(String(i + 1).padStart(2, \"0\"), Object.assign({}, $.font(\"mono\"), { size: badge * 0.42, x: bx + badge / 2, y: by + badge * 0.27, anchor: \"middle\", fill: $.on(c.accent) }));\n    }\n    card += $.text(label, Object.assign({}, lf, { x: ox + rh * 0.95, y: ry + (rh - lf.size) / 2 - lf.size * 0.04 }));\n    s += M.part(\"steps\", { at: at, in: \"slide\", from: \"left\", distance: 33, box: { x: ox, y: ry, w: w, h: rh } }, card);\n  });\n  return s;\n}\n","libs":[]},{"id":"quote","name":"Quote","description":"A pull quote, full frame: a big quotation mark pops, the words set in line by line, the speaker signs it off under an accent rule.","category":"quote","tags":["quote","testimonial","review","citation","words"],"placement":"full","duration":{"default":6,"min":3,"max":30},"aspects":["16:9","9:16","1:1","4:5"],"props":{"quote":{"type":"longtext","label":"Quote","default":"The best tools disappear. You stop thinking about the editor and start thinking about the story.","maxLength":220},"author":{"type":"text","label":"Who said it","default":"Maya Chen","maxLength":32},"role":{"type":"text","label":"Their role","default":"Documentary editor","maxLength":44},"face":{"type":"choice","label":"Typeface","description":"serif: the brand's accent face (an italic serif) · display: its headline face","options":["serif","display"],"default":"serif"}},"beats":{"in":0.1},"parts":{"ground":"the brand's ground","mark":"the quotation mark","quote":"the quote, line by line","rule":"the short accent rule","author":"who said it","role":"their role"},"style":"studio","pack":"essentials","ref":"essentials/quote","code":"// MONARC Essentials — helpers this pack's items share (prepended to each item's code). The layout\n// helpers (safeArea, fitFont, fitLines, landOn, widest) live in the motion kit.\n\n/** Words of `text` (with *marks*) laid into `lines` (the same words without marks), for $.sync / $.headline. */\nfunction markedRows(text, lines) {\n  const words = String(text).split(/\\s+/).filter(Boolean);\n  const rows = [];\n  let i = 0;\n  for (let l = 0; l < lines.length; l++) {\n    const n = String(lines[l]).split(/\\s+/).filter(Boolean).length;\n    rows.push(words.slice(i, i + n).join(\" \"));\n    i += n;\n  }\n  if (i < words.length && rows.length) rows[rows.length - 1] += \" \" + words.slice(i).join(\" \");\n  return rows;\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const tin = $.beat(\"in\", 0.1);\n  const face = p.face === \"display\" ? $.font(\"display\") : $.font(\"accent\");\n  const text = String(p.quote).replace(/^[\"“”]+|[\"“”]+$/g, \"\").trim();\n  const maxW = A.w * (A.wide ? 0.78 : 0.88);\n  const fit = fitLines($, text, Object.assign({}, face, { ls: p.face === \"display\" ? -0.02 : 0 }), maxW, Math.min(96 * k, A.h * 0.12), 38 * k, A.tall ? 8 : 5);\n  const lh = 1.22;\n  const markSize = Math.max(160 * k, fit.size * 2.4);\n  const af = Object.assign({}, $.font(\"display\"), { size: Math.max(34 * k, fit.size * 0.5) });\n  const rf = Object.assign({}, $.font(\"mono\"), { size: Math.max(22 * k, fit.size * 0.3), ls: 0.12 });\n  const blockH = markSize * 0.55 + fit.lines.length * fit.size * lh + 70 * k + af.size * 1.2 + (p.role ? rf.size * 1.6 : 0);\n  const x = A.wide ? (W - maxW) / 2 : A.x;\n  let y = A.y + (A.h - blockH) / 2;\n  const tWords = landOn($, text, tin + 0.35);\n  const leave = { out: \"drop\", distance: 24, outDur: 0.4 };\n\n  let s = M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: W, h: H }, glow: [W * 0.2, H * 0.25] }));\n  // The quotation mark, in the accent, springs in first.\n  s += M.part(\"mark\", Object.assign({ at: tin, in: \"spin\", intensity: 0.05, box: { x: x - markSize * 0.04, y: y - markSize * 0.18, w: markSize * 0.6, h: markSize * 0.9 } }, leave),\n    $.text(\"“\", Object.assign({}, $.font(\"accent\"), { size: markSize, x: x - markSize * 0.04, y: y - markSize * 0.18, fill: c.accent })));\n  y += markSize * 0.55;\n  s += M.text(\"quote\", Object.assign({ at: tWords, in: \"rise\", distance: 24, split: \"lines\", stagger: 0.14 }, leave, fit.font, { x: x, y: y, lh: lh, maxW: maxW }), text);\n  y += fit.lines.length * fit.size * lh + 40 * k;\n  const tSign = tWords + fit.lines.length * 0.14 + 0.25;\n  s += M.part(\"rule\", Object.assign({ at: tSign, in: \"grow-x\", origin: \"left\", dur: 0.4, box: { x: x, y: y, w: 90 * k, h: 6 * k } }, leave), $.rect(x, y, 90 * k, 6 * k, { r: 3 * k, fill: c.accent }));\n  y += 30 * k;\n  s += M.text(\"author\", Object.assign({ at: tSign + 0.1, in: \"slide\", from: \"left\", distance: 8 }, leave, af, { x: x, y: y }), $.case(p.author));\n  if (p.role) s += M.text(\"role\", Object.assign({ at: tSign + 0.22, in: \"slide\", from: \"left\", distance: 8 }, leave, rf, { x: x, y: y + af.size * 1.2, fill: c.mute }), String(p.role).toUpperCase());\n  return s;\n}\n","libs":[]},{"id":"banner","name":"News banner","description":"A broadcast strap over footage: the label block slams in, the headline bar wipes across, a ticker scrolls under it.","category":"banner","tags":["breaking news","live","strap","ticker","chyron","broadcast","headline"],"placement":"overlay","duration":{"default":8,"min":3,"max":60},"aspects":["16:9","9:16","1:1","4:5"],"props":{"label":{"type":"text","label":"Label","default":"Breaking","maxLength":14},"headline":{"type":"text","label":"Headline","default":"AI editors now cut a reel in under a minute","maxLength":70},"ticker":{"type":"longtext","label":"Ticker","description":"items separated by \" · \" scroll under the headline (empty for none)","default":"Creators report 3× more output · Open motion packs land on npm · Agents can now render graphics on their own","maxLength":280},"live":{"type":"boolean","label":"Live dot","default":true},"speed":{"type":"number","label":"Ticker speed","description":"px a second at 1080 px","default":140,"min":30,"max":600}},"beats":{"in":0.1},"parts":{"label":"the label block","bar":"the headline bar and ticker strip","headline":"the headline text","ticker":"the scrolling ticker text"},"style":"studio","pack":"essentials","ref":"essentials/banner","code":"// MONARC Essentials — helpers this pack's items share (prepended to each item's code). The layout\n// helpers (safeArea, fitFont, fitLines, landOn, widest) live in the motion kit.\n\n/** Words of `text` (with *marks*) laid into `lines` (the same words without marks), for $.sync / $.headline. */\nfunction markedRows(text, lines) {\n  const words = String(text).split(/\\s+/).filter(Boolean);\n  const rows = [];\n  let i = 0;\n  for (let l = 0; l < lines.length; l++) {\n    const n = String(lines[l]).split(/\\s+/).filter(Boolean).length;\n    rows.push(words.slice(i, i + n).join(\" \"));\n    i += n;\n  }\n  if (i < words.length && rows.length) rows[rows.length - 1] += \" \" + words.slice(i).join(\" \");\n  return rows;\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const k = $.k * (A.tall ? 1.15 : 1);\n  const tin = $.beat(\"in\", 0.1);\n  const label = String(p.label || \"\").toUpperCase();\n  const lf = Object.assign({}, $.font(\"display\"), { size: 40 * k, ls: 0.06 });\n  const hf = fitFont($, $.case(p.headline), \"display\", 48 * k, A.w * (A.wide ? 0.74 : 0.92) - 60 * k);\n  const tf = Object.assign({}, $.font(\"mono\"), { size: 26 * k, ls: 0.04 });\n  const hasTicker = !!String(p.ticker || \"\").trim();\n  const bh = 96 * k, th = hasTicker ? 52 * k : 0;\n  // On 9:16 the label sits on its own row above the headline (the strap is too narrow for both).\n  const stack = !A.wide;\n  const lw = $.measure(label, lf) + (p.live ? 70 * k : 46 * k);\n  const x = stack ? A.x : A.x * 0.6;\n  const w = W - 2 * x;\n  const yH = (stack ? H * 0.64 : H * 0.78) + (stack ? 70 * k : 0);\n  const yL = stack ? yH - 70 * k : yH;\n  const hx = stack ? x : x + lw;\n  const bw = w - (hx - x);\n  const leave = { out: \"rise\", distance: 30, outDur: 0.35 };\n\n  // The headline bar wipes out from behind the label; the ticker strip under it.\n  let s = M.part(\"bar\", Object.assign({ at: tin + 0.18, in: \"wipe\", from: \"left\", dur: 0.45, box: { x: hx, y: yH, w: bw, h: bh + th } }, leave),\n    $.rect(hx, yH, bw, bh, { fill: c.surface, shadow: \"md\" }),\n    hasTicker ? $.rect(hx, yH + bh, bw, th, { fill: c.ink }) : \"\");\n  s += $.clip(hx, yH, bw, bh, 0, M.text(\"headline\", Object.assign({ at: tin + 0.35, in: \"rise\", distance: 24 }, leave, hf, { x: hx + 30 * k, y: yH + (bh - hf.size) / 2 - hf.size * 0.04, within: { x: hx, y: yH, w: bw, h: bh } }), $.case(p.headline)));\n  if (hasTicker) {\n    const items = String(p.ticker).replace(/\\s+/g, \" \").trim();\n    const loop = items + \"   ·   \";\n    const lw2 = $.measure(loop, tf);\n    const off = (Math.max(0, t - tin - 0.35) * p.speed * k) % lw2;\n    let run = \"\";\n    for (let i = 0; i < Math.ceil((w + lw2) / lw2) + 1; i++) run += loop;\n    s += $.clip(hx, yH + bh, bw, th, 0, M.part(\"ticker\", Object.assign({ at: tin + 0.55, in: \"fade\", dur: 0.3, box: { x: hx, y: yH + bh, w: bw, h: th } }, leave),\n      $.text(run, Object.assign({}, tf, { x: hx + 30 * k - off, y: yH + bh + (th - tf.size) / 2 - tf.size * 0.06, fill: \"#FFFFFF\", within: { x: -1e6, y: 0, w: 2e6, h: H } }))));\n  }\n  // The label block slams in with a jolt.\n  const lh = stack ? 70 * k : bh;\n  let lb = $.rect(x, yL, lw, lh, { fill: c.accent, shadow: \"md\" });\n  if (p.live) lb += $.circle(x + 30 * k, yL + lh / 2, 9 * k, { fill: $.on(c.accent), op: 0.55 + 0.45 * $.wave(t, 1.1) });\n  lb += $.text(label, Object.assign({}, lf, { x: x + (p.live ? 52 * k : 23 * k), y: yL + (lh - lf.size) / 2 - lf.size * 0.03, fill: $.on(c.accent) }));\n  s += M.part(\"label\", Object.assign({ at: tin, in: \"slam\", intensity: 0.12, emphasis: [{ fx: \"shake\", at: tin + 0.08 }], box: { x: x, y: yL, w: lw, h: lh }, origin: \"left\" }, leave), lb);\n  return s;\n}\n","libs":[]},{"id":"end-card","name":"End card","description":"An end screen, full frame: \"watch next\" slots pop in (for YouTube's end-screen elements or your own thumbnails), the subscribe button presses, the handle signs off.","category":"end-card","tags":["end screen","outro","watch next","youtube","subscribe","thumbnail"],"placement":"full","duration":{"default":10,"min":4,"max":20},"aspects":["16:9","9:16","1:1","4:5"],"props":{"title":{"type":"text","label":"Heading","default":"Watch next","maxLength":28},"first":{"type":"text","label":"First slot","default":"How we edit a reel in 10 minutes","maxLength":48},"second":{"type":"text","label":"Second slot","default":"The hook formula","maxLength":48},"firstImage":{"type":"image","label":"First thumbnail","description":"a 16:9 picture (an empty slot when none)","default":""},"secondImage":{"type":"image","label":"Second thumbnail","default":""},"handle":{"type":"text","label":"Handle","default":"@monarcstudio","maxLength":30},"button":{"type":"text","label":"Button","default":"Subscribe","maxLength":14},"done":{"type":"text","label":"After the press","default":"Subscribed","maxLength":14}},"beats":{"in":0.1},"holdFromEnd":0.6,"parts":{"ground":"the brand's ground","title":"the heading","slots":"each watch-next slot","footer":"the button and handle"},"style":"studio","pack":"essentials","ref":"essentials/end-card","code":"// MONARC Essentials — helpers this pack's items share (prepended to each item's code). The layout\n// helpers (safeArea, fitFont, fitLines, landOn, widest) live in the motion kit.\n\n/** Words of `text` (with *marks*) laid into `lines` (the same words without marks), for $.sync / $.headline. */\nfunction markedRows(text, lines) {\n  const words = String(text).split(/\\s+/).filter(Boolean);\n  const rows = [];\n  let i = 0;\n  for (let l = 0; l < lines.length; l++) {\n    const n = String(lines[l]).split(/\\s+/).filter(Boolean).length;\n    rows.push(words.slice(i, i + n).join(\" \"));\n    i += n;\n  }\n  if (i < words.length && rows.length) rows[rows.length - 1] += \" \" + words.slice(i).join(\" \");\n  return rows;\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const tin = $.beat(\"in\", 0.1);\n  const title = $.case(p.title);\n  const hf = fitFont($, title, \"display\", Math.min(110 * k, A.h * 0.1), A.w, { ls: -0.03 });\n  const slotF = Object.assign({}, $.font(\"text\"), { size: 32 * k });\n  // Two 16:9 slots side by side on wide frames, stacked on tall ones.\n  const stack = !A.wide;\n  const sw = stack ? Math.min(A.w, (A.h * (A.tall ? 0.62 : 0.72)) / 2 * (16 / 9) - 40 * k) : (A.w - 60 * k) / 2;\n  const sh = (sw * 9) / 16;\n  const labelH = slotF.size * 2.6;\n  const headH = hf.size * 1.3;\n  const footH = 130 * k;\n  const gridH = stack ? 2 * (sh + labelH) + 30 * k : sh + labelH;\n  const top = A.y + (A.h - headH - gridH - footH) / 2;\n  const left = stack ? (W - sw) / 2 : A.x;\n\n  let s = M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: W, h: H } }));\n  s += M.text(\"title\", Object.assign({ at: tin, in: \"rise\", distance: 26 }, hf, { x: W / 2, y: top, anchor: \"middle\" }), title);\n  [[p.first, p.firstImage ? \"firstImage\" : \"\"], [p.second, p.secondImage ? \"secondImage\" : \"\"]].forEach(function (slot, i) {\n    const sx = stack ? left : left + i * (sw + 60 * k);\n    const sy = top + headH + (stack ? i * (sh + labelH + 30 * k) : 0);\n    const ccx = sx + sw / 2, ccy = sy + sh / 2;\n    const rr = $.style.radius * 0.6;\n    let el = \"\";\n    if (slot[1]) el += $.image(slot[1], sx, sy, sw, sh, { r: rr });\n    else {\n      // No picture: a tile in the brand's accents, a faint pattern, its number and a play button.\n      const a = i ? c.accent2 : c.accent, b = i ? c.accent : c.accent2;\n      el += $.rect(sx, sy, sw, sh, { r: rr, fill: $.gradient([a, $.mix(a, b, 0.55)], { angle: 135 }), shadow: \"md\" });\n      el += $.clip(sx, sy, sw, sh, rr, $.pattern(t, \"dots\", { box: { x: sx, y: sy, w: sw, h: sh }, color: $.on(a), a: 0.12, scale: 1.2 }));\n      el += $.text(\"0\" + (i + 1), Object.assign({}, $.font(\"display\"), { size: sh * 0.42, x: sx + 24 * k, y: sy + sh - sh * 0.42 - 14 * k, fill: $.on(a), op: 0.22 }));\n      const pr = Math.min(sw, sh) * 0.14 * (1 + 0.06 * $.wave(t, 1.6, i * 0.3));\n      el += $.circle(ccx, ccy, pr, { fill: \"#FFFFFF\", shadow: \"md\" }) + $.icon(\"play\", ccx - pr * 0.42, ccy - pr * 0.5, pr, { color: a, fill: true });\n    }\n    $.wrap($.case(slot[0] || \"\"), sw, slotF).slice(0, 2).forEach(function (line, j) { el += $.text(line, Object.assign({}, slotF, { x: sx, y: sy + sh + 18 * k + j * slotF.size * 1.2 })); });\n    s += M.part(\"slots\", { at: tin + 0.25 + i * 0.15, in: \"rise\", distance: 40, box: { x: sx, y: sy, w: sw, h: sh + labelH } }, el);\n  });\n  // The footer: the button presses, the handle beside it.\n  const fy = top + headH + gridH + 40 * k;\n  const press = tin + 1.3;\n  const b1 = $.button.size($.case(p.button), { size: 34 * k, icon: \"bell\" }), b2 = $.button.size($.case(p.done), { size: 34 * k, icon: \"check\" });\n  const btn = { w: Math.max(b1.w, b2.w), h: b1.h };\n  const hfnt = Object.assign({}, $.font(\"mono\"), { size: 30 * k });\n  const hw = p.handle ? $.measure(p.handle, hfnt) + 36 * k : 0;\n  const fx = W / 2 - (btn.w + hw) / 2;\n  s += M.part(\"footer\", { at: tin + 0.6, in: \"rise\", distance: 24, box: { x: fx, y: fy, w: btn.w + hw, h: btn.h } },\n    $.button(t, $.case(p.button), fx, fy, { size: 34 * k, icon: \"bell\", pressAt: press, done: $.case(p.done) }),\n    p.handle ? $.text(p.handle, Object.assign({}, hfnt, { x: fx + btn.w + 36 * k, y: fy + btn.h / 2 - hfnt.size * 0.55, fill: c.mute })) : \"\");\n  return s;\n}\n","libs":[]},{"id":"logo-sting","name":"Logo sting","description":"A brand reveal, full frame: rings burst from the centre, the logo pops, the wordmark lands letter by letter with a light sweep, the tagline spaces out under it.","category":"logo","tags":["logo","intro","outro","brand","reveal","wordmark","sting","bumper"],"placement":"full","duration":{"default":3.5,"min":2,"max":10},"aspects":["16:9","9:16","1:1","4:5"],"props":{"wordmark":{"type":"text","label":"Wordmark","default":"MONARC","maxLength":20},"tagline":{"type":"text","label":"Tagline","default":"Made with motion","maxLength":40},"logo":{"type":"image","label":"Logo","description":"a square mark (PNG with alpha or SVG) above the wordmark; none for the wordmark alone","default":""},"burst":{"type":"boolean","label":"Burst","default":true}},"beats":{"in":0.1},"holdFromEnd":0.5,"parts":{"ground":"the brand's ground","rings":"the rings that burst out","logo":"the logo picture","wordmark":"the wordmark","tagline":"the tagline"},"style":"studio","pack":"essentials","ref":"essentials/logo-sting","code":"// MONARC Essentials — helpers this pack's items share (prepended to each item's code). The layout\n// helpers (safeArea, fitFont, fitLines, landOn, widest) live in the motion kit.\n\n/** Words of `text` (with *marks*) laid into `lines` (the same words without marks), for $.sync / $.headline. */\nfunction markedRows(text, lines) {\n  const words = String(text).split(/\\s+/).filter(Boolean);\n  const rows = [];\n  let i = 0;\n  for (let l = 0; l < lines.length; l++) {\n    const n = String(lines[l]).split(/\\s+/).filter(Boolean).length;\n    rows.push(words.slice(i, i + n).join(\" \"));\n    i += n;\n  }\n  if (i < words.length && rows.length) rows[rows.length - 1] += \" \" + words.slice(i).join(\" \");\n  return rows;\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const tin = $.beat(\"in\", 0.1);\n  const word = $.case(p.wordmark);\n  const wf = fitFont($, word, \"display\", Math.min(220 * k, A.h * 0.26), A.w * 0.86, { ls: 0.02 });\n  const tag = p.tagline ? String(p.tagline).toUpperCase() : \"\";\n  const tfnt = fitFont($, tag, \"mono\", 30 * k, A.w * 0.8, { ls: 0.32 });\n  const logo = p.logo ? Math.min(220 * k, A.h * 0.22) : 0;\n  const total = logo + (logo ? 50 * k : 0) + wf.size + (tag ? 40 * k + tfnt.size : 0);\n  const cx = W / 2, cy = H / 2;\n  let y = cy - total / 2;\n  const tWord = tin + (logo ? 0.55 : 0.3);\n  const leave = { out: \"push\", intensity: 0.06 };\n\n  let s = M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: W, h: H }, glow: [cx, cy] }));\n  if (p.burst) {\n    let rings = \"\";\n    for (let i = 0; i < 3; i++) {\n      const a = tin + i * 0.12;\n      const q = $.E.out3($.seg(t, a, a + 0.9));\n      if (q > 0 && q < 1) rings += $.circle(cx, cy, (60 + 700 * q) * k, { stroke: i === 1 ? c.accent2 : c.accent, sw: (14 - i * 3) * k * (1 - q), op: 1 - q });\n    }\n    rings += $.burst(t, tWord, cx, cy, { n: 18, r: Math.max(W, H) * 0.32, colors: [c.accent, c.accent2, c.fg], size: 10 * k });\n    s += M.part(\"rings\", { at: tin, in: \"none\", out: \"fade\", vary: false }, rings);\n  }\n  if (logo) {\n    s += M.part(\"logo\", Object.assign({ at: tin + 0.15, in: \"pop\", box: { x: cx - logo / 2, y: y, w: logo, h: logo } }, leave), $.image(\"logo\", cx - logo / 2, y, logo, logo, { fit: \"contain\" }));\n    y += logo + 50 * k;\n  }\n  const ww = $.measure(word, wf);\n  // The wordmark lands letter by letter (word by word in scripts that join), then a light sweeps across it.\n  s += M.text(\"wordmark\", Object.assign({ at: tWord, in: \"rise\", split: \"letters\", distance: wf.size * 0.6 / k, ease: \"spring(2.4, 7)\", stagger: Math.min(0.06, 0.5 / Math.max(1, word.length)) }, leave, wf, { x: cx, y: y, anchor: \"middle\" }), word);\n  const sweep = $.seg(t, tWord + 0.5, tWord + 1.2);\n  if (sweep > 0 && sweep < 1) {\n    const sx = cx - ww / 2 - 200 * k + (ww + 400 * k) * $.E.inOut2(sweep);\n    s += $.g({ extra: 'style=\"mix-blend-mode:screen\"' }, $.clip(cx - ww / 2 - 20 * k, y - 10 * k, ww + 40 * k, wf.size * 1.1, 0,\n      $.rect(sx - 60 * k, y - 20 * k, 120 * k, wf.size * 1.3, { fill: $.gradient([[0, \"#FFFFFF\", 0], [0.5, \"#FFFFFF\", 0.55], [1, \"#FFFFFF\", 0]], { angle: 90 }), transform: \"translate(\" + $.fx(sx) + \" \" + $.fx(y + wf.size / 2) + \") skewX(-18) translate(\" + $.fx(-sx) + \" \" + $.fx(-(y + wf.size / 2)) + \")\" })));\n  }\n  y += wf.size + 40 * k;\n  if (tag) {\n    // The tagline spaces out as it fades in.\n    const tp = M.progress(\"tagline\", { at: tWord + 0.45, in: \"fade\", dur: 0.65, ease: \"out\" });\n    s += M.text(\"tagline\", Object.assign({ at: tWord + 0.45, in: \"fade\", dur: 0.65, ease: \"out\" }, leave, tfnt, { x: cx, y: y, anchor: \"middle\", fill: c.mute, ls: 0.14 + 0.18 * Math.min(1, tp.in) }), tag);\n  }\n  return s;\n}\n","libs":[]},{"id":"wipe","name":"Brand wipe","description":"A transition in the brand's colours: panels sweep in to cover the frame at the midpoint — cut your clips there — and sweep out to reveal the next shot.","category":"transition","tags":["transition","wipe","swipe","cut","bars","circle","reveal"],"placement":"overlay","duration":{"default":1.2,"min":0.6,"max":4},"aspects":["16:9","9:16","1:1","4:5"],"props":{"shape":{"type":"choice","label":"Shape","options":["bars","diagonal","circle"],"default":"bars"},"bars":{"type":"number","label":"Bars","default":5,"min":2,"max":12,"step":1,"when":{"prop":"shape","is":["bars"]}},"label":{"type":"text","label":"Word at the cut","description":"shown while the frame is covered (empty for none)","default":"","maxLength":24},"direction":{"type":"choice","label":"Direction","options":["right","left","up","down"],"default":"right"}},"parts":{"panels":"the panels that cover the frame","label":"the word at the cut"},"style":"studio","pack":"essentials","ref":"essentials/wipe","code":"// MONARC Essentials — helpers this pack's items share (prepended to each item's code). The layout\n// helpers (safeArea, fitFont, fitLines, landOn, widest) live in the motion kit.\n\n/** Words of `text` (with *marks*) laid into `lines` (the same words without marks), for $.sync / $.headline. */\nfunction markedRows(text, lines) {\n  const words = String(text).split(/\\s+/).filter(Boolean);\n  const rows = [];\n  let i = 0;\n  for (let l = 0; l < lines.length; l++) {\n    const n = String(lines[l]).split(/\\s+/).filter(Boolean).length;\n    rows.push(words.slice(i, i + n).join(\" \"));\n    i += n;\n  }\n  if (i < words.length && rows.length) rows[rows.length - 1] += \" \" + words.slice(i).join(\" \");\n  return rows;\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const M = motion($, t);\n  const mid = $.dur / 2;\n  // 0 → 1 covering until the midpoint, 1 → 2 uncovering after it.\n  const cover = $.E.inOut3($.seg(t, 0, mid));\n  const uncover = $.E.inOut3($.seg(t, mid, $.dur));\n  const colors = [c.accent, c.accent2, c.ink];\n  const dir = p.direction;\n  const horiz = dir === \"right\" || dir === \"left\";\n  const sign = dir === \"right\" || dir === \"down\" ? 1 : -1;\n  const L = horiz ? W : H;\n  let s = \"\";\n  if (p.shape === \"circle\") {\n    const R = Math.hypot(W, H) * 0.56;\n    const cx = W / 2, cy = H / 2;\n    if (t < mid) s += $.circle(cx, cy, R * cover, { fill: c.accent }) + $.circle(cx, cy, R * Math.max(0, cover * 1.12 - 0.12), { fill: c.ink });\n    else s += $.path(\"M0 0H\" + W + \"V\" + H + \"H0Z M\" + (cx + R * uncover) + \" \" + cy + \"A\" + R * uncover + \" \" + R * uncover + \" 0 1 0 \" + (cx - R * uncover) + \" \" + cy + \"A\" + R * uncover + \" \" + R * uncover + \" 0 1 0 \" + (cx + R * uncover) + \" \" + cy + \"Z\", { fill: c.ink, extra: 'fill-rule=\"evenodd\"' });\n  } else if (p.shape === \"diagonal\") {\n    const skew = 0.35 * (horiz ? H : W);\n    const pos = t < mid ? -L - skew + (L + skew) * cover : (L + skew) * uncover;\n    [[c.accent, -0.12], [c.accent2, -0.06], [c.ink, 0]].forEach(function (lay) {\n      const a = pos + (t < mid ? lay[1] : -lay[1] * 0.5) * L;\n      const d = horiz\n        ? \"M\" + a + \" 0H\" + (a + L + skew) + \"L\" + (a + L) + \" \" + H + \"H\" + (a - skew) + \"Z\"\n        : \"M0 \" + a + \"V\" + (a + L + skew) + \"L\" + W + \" \" + (a + L) + \"V\" + (a - skew) + \"Z\";\n      s += $.path(d, { fill: lay[0], transform: sign < 0 ? (horiz ? \"translate(\" + W + \" 0) scale(-1 1)\" : \"translate(0 \" + H + \") scale(1 -1)\") : undefined });\n    });\n  } else {\n    const n = Math.max(2, Math.round(p.bars));\n    const band = (horiz ? H : W) / n;\n    for (let i = 0; i < n; i++) {\n      const lag = (i / (n - 1)) * 0.35;\n      const ci = $.E.inOut3($.clamp(($.seg(t, 0, mid) - lag) / (1 - lag)));\n      const ui = $.E.inOut3($.clamp(($.seg(t, mid, $.dur) - lag) / (1 - lag)));\n      const len = L * (t < mid ? ci : 1 - ui);\n      const start = t < mid ? (sign > 0 ? 0 : L - len) : sign > 0 ? L - len : 0;\n      s += horiz ? $.rect(start, i * band - 1, len, band + 2, { fill: colors[i % colors.length] }) : $.rect(i * band - 1, start, band + 2, len, { fill: colors[i % colors.length] });\n    }\n  }\n  // The panels are the transition; they move only when the creator says so.\n  s = M.part(\"panels\", { in: \"none\", out: \"none\", vary: false }, s);\n  if (p.label) {\n    const lf = fitFont($, $.case(p.label), \"display\", 150 * k, W * 0.8, { ls: -0.03 });\n    s += M.text(\"label\", Object.assign({ at: mid - mid * 0.5, in: \"pop\", outAt: mid + mid * 0.25, out: \"fade\", outDur: mid * 0.3 }, lf, { x: W / 2, y: H / 2 - lf.size * 0.55, anchor: \"middle\", fill: c.paper }), $.case(p.label));\n  }\n  return s;\n}\n\n/** Air as the wipe covers the frame, a soft drop at the cut (the midpoint), air going as it uncovers. */\nfunction sound($) {\n  const mid = $.dur / 2;\n  return {\n    cues: [\n      { at: Math.max(0, mid - 0.4), kind: \"whoosh\", dur: Math.min(0.45, mid), level: 0.9 },\n      { at: mid, kind: \"sub\", level: 0.5 },\n      { at: mid + 0.04, kind: \"whoosh\", dur: Math.min(0.45, $.dur - mid), level: 0.55, pitch: -3 },\n    ],\n    auto: true,\n  };\n}\n","libs":[]},{"id":"chapter","name":"Chapter card","description":"A chapter card: a huge outlined number that fills from the bottom, the chapter's name under a kicker, and a row of segments showing where it sits among all the chapters.","category":"title","tags":["chapter","section","title","part","episode"],"placement":"full","duration":{"default":4,"min":2,"max":20},"props":{"number":{"type":"number","label":"Chapter","default":2,"min":1,"max":12},"total":{"type":"number","label":"Chapters","default":5,"min":1,"max":12},"kicker":{"type":"text","label":"Kicker","default":"Chapter","maxLength":20},"title":{"type":"text","label":"Title","default":"Finding the hook","maxLength":50}},"parts":{"ground":"the ground","number":"the big number","kicker":"the kicker","title":"the chapter name","progress":"the chapter segments"},"beats":{"in":0.15,"out":-0.45},"style":"studio","pack":"essentials","ref":"essentials/chapter","code":"// MONARC Essentials — helpers this pack's items share (prepended to each item's code). The layout\n// helpers (safeArea, fitFont, fitLines, landOn, widest) live in the motion kit.\n\n/** Words of `text` (with *marks*) laid into `lines` (the same words without marks), for $.sync / $.headline. */\nfunction markedRows(text, lines) {\n  const words = String(text).split(/\\s+/).filter(Boolean);\n  const rows = [];\n  let i = 0;\n  for (let l = 0; l < lines.length; l++) {\n    const n = String(lines[l]).split(/\\s+/).filter(Boolean).length;\n    rows.push(words.slice(i, i + n).join(\" \"));\n    i += n;\n  }\n  if (i < words.length && rows.length) rows[rows.length - 1] += \" \" + words.slice(i).join(\" \");\n  return rows;\n}\n\n// A chapter card: a huge outlined number, the chapter's name under a small kicker, and a row of segments that\n// shows where this chapter sits among all of them, the current one filling in.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const tin = $.beat(\"in\", 0.1);\n  let s = M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: W, h: H } }));\n  const total = Math.max(1, Math.min(12, Math.round(p.total))), cur = Math.max(1, Math.min(total, Math.round(p.number)));\n  const num = (cur < 10 ? \"0\" : \"\") + cur;\n  const side = A.wide;\n  const nf = fitFont($, num, \"display\", Math.min(side ? A.h * 0.7 : A.h * 0.32, 520 * k), side ? A.w * 0.42 : A.w * 0.8, { ls: -0.04 });\n  const title = $.case(String(p.title || \"\"));\n  const tf = fitLines($, title, Object.assign({}, $.font(\"display\"), { ls: -0.03 }), side ? A.w * 0.5 : A.w, (side ? 110 : 120) * k, 50 * k, 2);\n  const kf = Object.assign({}, $.font(\"mono\"), { size: 28 * k, ls: 0.12, weight: 700 });\n  const kicker = String(p.kicker || \"Chapter\").toUpperCase() + \"  \" + cur + \" / \" + total;\n  // Wide: number left, words right. Otherwise: number above, words under it.\n  const blockH = kf.size * 2 + tf.lines.length * tf.size * 1.05 + 90 * k;\n  // Stacked: the number and the words as one block, centred.\n  const nx = side ? A.x : W / 2, ny = side ? A.y + (A.h - nf.size) / 2 - nf.size * 0.05 : A.y + Math.max(0, (A.h - nf.size * 1.02 - 40 * k - blockH) / 2);\n  const wx = side ? A.x + A.w * 0.5 : W / 2, anchor = side ? \"start\" : \"middle\";\n  const wy = side ? A.y + (A.h - blockH) / 2 : ny + nf.size * 1.02 + 40 * k;\n  // The number: drawn as an outline that fills from the bottom once it lands.\n  const fillP = $.E.inOut2($.seg(t, tin + 0.5, tin + 1.3));\n  const clipId = $.uid();\n  $.def('<clipPath id=\"' + clipId + '\"><rect x=\"' + (nx - (side ? 0 : nf.size * 2)) + '\" y=\"' + (ny + nf.size * (1 - fillP)) + '\" width=\"' + nf.size * 4 + '\" height=\"' + nf.size * 1.2 + '\"/></clipPath>');\n  const numM = $.text(num, Object.assign({}, nf, { x: nx, y: ny, anchor: anchor, fill: \"none\", stroke: c.accent, sw: 4 * k })) +\n    '<g clip-path=\"url(#' + clipId + ')\">' + $.text(num, Object.assign({}, nf, { x: nx, y: ny, anchor: anchor, fill: c.accent })) + \"</g>\";\n  s += M.part(\"number\", { at: tin, in: \"rise\", distance: 60, out: \"fade\", box: { x: side ? nx : nx - nf.size, y: ny, w: nf.size * 2, h: nf.size } }, numM);\n  s += M.text(\"kicker\", Object.assign({ at: tin + 0.25, in: \"fade\", dur: 0.4 }, kf, { x: wx, y: wy, anchor: anchor, fill: c.mute }), kicker);\n  tf.lines.forEach(function (line, i) { s += M.text(\"title\", Object.assign({ at: tin + 0.35 + i * 0.1, in: \"rise\", distance: 30 }, tf.font, { x: wx, y: wy + kf.size * 2 + i * tf.size * 1.05, anchor: anchor, fill: c.fg }), line); });\n  // The segments: one per chapter, done ones solid, this one filling, the rest faint.\n  const segW = Math.min((side ? A.w * 0.46 : A.w * 0.8) / total, 90 * k), segH = 10 * k, gap = 10 * k;\n  const rowW = total * segW + (total - 1) * gap;\n  const sx = side ? wx : W / 2 - rowW / 2, sy = wy + kf.size * 2 + tf.lines.length * tf.size * 1.05 + 50 * k;\n  let segs = \"\";\n  for (let i = 0; i < total; i++) {\n    const x = sx + i * (segW + gap);\n    segs += $.rect(x, sy, segW, segH, { r: segH / 2, fill: $.rgba(c.fg, 0.14) });\n    const f = i + 1 < cur ? 1 : i + 1 === cur ? $.E.inOut2($.seg(t, tin + 0.7, tin + 1.6)) : 0;\n    if (f > 0) segs += $.rect(x, sy, segW * f, segH, { r: segH / 2, fill: i + 1 === cur ? c.accent : c.fg });\n  }\n  s += M.part(\"progress\", { at: tin + 0.5, in: \"fade\", dur: 0.4, out: \"fade\" }, segs);\n  return s;\n}\n","libs":[]},{"id":"logo-wall","name":"Logo wall","description":"Social proof: a line like \"Trusted by teams at\" over a wall of wordmarks — each name set as a small logo with its own mark and face — popping in one after another.","category":"social","tags":["logos","trusted by","clients","partners","social proof"],"placement":"full","duration":{"default":5,"min":2,"max":20},"props":{"title":{"type":"text","label":"Title","default":"Trusted by teams at","maxLength":50},"names":{"type":"list","label":"Names","default":["Velora","Quintic","Brightfold","Marrow & Co","Atlasyn","Kindlewood","Nimbleform","Tessel"],"maxItems":12,"minItems":1,"of":"text"},"mono":{"type":"boolean","label":"One colour","default":false}},"parts":{"ground":"the ground","title":"the line above","logos":"each logo cell"},"beats":{"in":0.15,"out":-0.45},"style":"studio","pack":"essentials","ref":"essentials/logo-wall","code":"// MONARC Essentials — helpers this pack's items share (prepended to each item's code). The layout\n// helpers (safeArea, fitFont, fitLines, landOn, widest) live in the motion kit.\n\n/** Words of `text` (with *marks*) laid into `lines` (the same words without marks), for $.sync / $.headline. */\nfunction markedRows(text, lines) {\n  const words = String(text).split(/\\s+/).filter(Boolean);\n  const rows = [];\n  let i = 0;\n  for (let l = 0; l < lines.length; l++) {\n    const n = String(lines[l]).split(/\\s+/).filter(Boolean).length;\n    rows.push(words.slice(i, i + n).join(\" \"));\n    i += n;\n  }\n  if (i < words.length && rows.length) rows[rows.length - 1] += \" \" + words.slice(i).join(\" \");\n  return rows;\n}\n\n// \"Trusted by\" with a wall of wordmarks: each name set as a small logo (a mark and the name in its own face)\n// in a grid of cells that pop in one after another.\nvar FACES = [[\"Archivo Black\", 400], [\"Playfair Display\", 700, true], [\"Space Mono\", 700], [\"Bricolage Grotesque\", 800], [\"Syne\", 800], [\"Fraunces\", 900], [\"Unbounded\", 800], [\"Righteous\", 400]];\nfunction mark($, i, x, y, s, col) {\n  // A simple shape per logo: circle, square, triangle, ring, half disc, diamond, bars, plus.\n  const h = s / 2, cx = x + h, cy = y + h;\n  switch (i % 8) {\n    case 0: return $.circle(cx, cy, h, { fill: col });\n    case 1: return $.rect(x, y, s, s, { r: s * 0.22, fill: col });\n    case 2: return $.path(\"M\" + cx + \" \" + y + \"L\" + (x + s) + \" \" + (y + s) + \"L\" + x + \" \" + (y + s) + \"Z\", { fill: col });\n    case 3: return $.circle(cx, cy, h * 0.78, { stroke: col, sw: s * 0.2 });\n    case 4: return $.path(\"M\" + x + \" \" + cy + \"A\" + h + \" \" + h + \" 0 0 1 \" + (x + s) + \" \" + cy + \"Z\", { fill: col });\n    case 5: return $.path(\"M\" + cx + \" \" + y + \"L\" + (x + s) + \" \" + cy + \"L\" + cx + \" \" + (y + s) + \"L\" + x + \" \" + cy + \"Z\", { fill: col });\n    case 6: return $.rect(x, y, s * 0.26, s, { fill: col }) + $.rect(x + s * 0.37, y + s * 0.3, s * 0.26, s * 0.7, { fill: col }) + $.rect(x + s * 0.74, y + s * 0.55, s * 0.26, s * 0.45, { fill: col });\n    default: return $.rect(x + s * 0.36, y, s * 0.28, s, { r: 2, fill: col }) + $.rect(x, y + s * 0.36, s, s * 0.28, { r: 2, fill: col });\n  }\n}\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const tin = $.beat(\"in\", 0.1);\n  let s = M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: W, h: H } }));\n  const names = (p.names || []).map(String).filter(Boolean).slice(0, 12);\n  const n = Math.max(1, names.length);\n  const cols = A.wide ? Math.min(4, n) : A.tall ? 2 : Math.min(3, n);\n  const rows = Math.ceil(n / cols);\n  const title = $.case(String(p.title || \"\"));\n  const tf = fitFont($, title, \"text\", (A.tall ? 44 : 38) * k, A.w, { weight: 600 });\n  const gap = 18 * k;\n  const cw = (A.w - gap * (cols - 1)) / cols, ch = Math.min(cw * (A.tall ? 0.6 : 0.5), (A.h - tf.size * 2.6) / rows - gap);\n  const gridH = rows * ch + (rows - 1) * gap;\n  const top = A.y + (A.h - tf.size * 2.4 - gridH) / 2;\n  s += M.text(\"title\", Object.assign({ at: tin, in: \"rise\", distance: 20 }, tf, { x: W / 2, y: top, anchor: \"middle\", fill: c.mute }), title);\n  const gy = top + tf.size * 2.4;\n  let cells = \"\";\n  names.forEach(function (name, i) {\n    const col = i % cols, row = Math.floor(i / cols);\n    // The last row is centred when it is short.\n    const inRow = row === rows - 1 ? n - row * cols : cols;\n    const x = A.x + (A.w - (inRow * cw + (inRow - 1) * gap)) / 2 + col * (cw + gap), y = gy + row * (ch + gap);\n    const fc = FACES[i % FACES.length];\n    const ms = ch * 0.3;\n    const lf = fitFont($, name, \"text\", ch * 0.3, cw - ms - 60 * k, { family: fc[0], weight: fc[1], italic: !!fc[2], ls: -0.01 });\n    const lw = ms + 14 * k + $.measure(name, lf);\n    const lx = x + (cw - lw) / 2, mid = y + ch / 2;\n    let m = $.rect(x, y, cw, ch, { r: 18 * k, fill: $.rgba(c.fg, 0.04), stroke: c.line, sw: 1.5 * k });\n    const ink = p.mono ? c.fg : seriesInk($, i);\n    m += mark($, i, lx, mid - ms / 2, ms, ink);\n    m += $.text(name, Object.assign({}, lf, { x: lx + ms + 14 * k, y: mid - lf.size * 0.56, fill: c.fg }));\n    cells += M.part(\"logos\", { at: tin + 0.25 + i * 0.08, in: \"pop\", out: \"fade\", box: { x: x, y: y, w: cw, h: ch } }, m);\n  });\n  return s + cells;\n}\n/** A mark colour from the brand: the accents, then the text colour. */\nfunction seriesInk($, i) { return [$.c.accent, $.c.accent2, $.c.fg][i % 3]; }\n","libs":[]},{"id":"progress-bar","name":"Progress bar","description":"A progress card for footage: an icon, what is happening, a striped bar filling with the percentage counting up, then a check and the done line when it reaches the end.","category":"timer","tags":["progress","loading","export","upload","status"],"placement":"overlay","duration":{"default":5,"min":2,"max":30},"props":{"label":{"type":"text","label":"Label","default":"Exporting your video","maxLength":40},"value":{"type":"number","label":"Reaches (%)","default":100,"min":0,"max":100},"done":{"type":"text","label":"Done line","default":"Ready to post","maxLength":40},"icon":{"type":"icon","label":"Icon","default":"download"},"position":{"type":"choice","label":"Position","options":["bottom","center","top"],"default":"bottom"}},"parts":{"card":"the card"},"beats":{"in":0.15,"out":-0.45},"style":"studio","pack":"essentials","ref":"essentials/progress-bar","code":"// MONARC Essentials — helpers this pack's items share (prepended to each item's code). The layout\n// helpers (safeArea, fitFont, fitLines, landOn, widest) live in the motion kit.\n\n/** Words of `text` (with *marks*) laid into `lines` (the same words without marks), for $.sync / $.headline. */\nfunction markedRows(text, lines) {\n  const words = String(text).split(/\\s+/).filter(Boolean);\n  const rows = [];\n  let i = 0;\n  for (let l = 0; l < lines.length; l++) {\n    const n = String(lines[l]).split(/\\s+/).filter(Boolean).length;\n    rows.push(words.slice(i, i + n).join(\" \"));\n    i += n;\n  }\n  if (i < words.length && rows.length) rows[rows.length - 1] += \" \" + words.slice(i).join(\" \");\n  return rows;\n}\n\n// A progress card for footage: an icon, what is happening, a striped bar filling with the percentage counting\n// up beside it, then a check and the done line when it reaches the end.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const tin = $.beat(\"in\", 0.15);\n  const w = Math.min(A.w, (A.wide ? 760 : 900) * k), pad = 30 * k, ic = 64 * k;\n  const lf = Object.assign({}, $.font(\"display\"), { size: 36 * k });\n  const mf = Object.assign({}, $.font(\"mono\"), { size: 26 * k, weight: 700 });\n  const barH = 18 * k;\n  const h = pad * 2 + Math.max(ic, lf.size * 1.3) + 30 * k + barH;\n  const x = W / 2 - w / 2;\n  const y = p.position === \"top\" ? A.y : p.position === \"center\" ? H / 2 - h / 2 : A.y + A.h - h;\n  // The fill runs from 0 to the target over the time the card holds, easing out near the end.\n  const end = $.beat(\"out\", $.dur - 0.45) - 0.6, target = Math.max(0, Math.min(100, Number(p.value) || 0));\n  const f = target / 100 * $.E.inOut2($.seg(t, tin + 0.4, end));\n  const done = target >= 100 && t >= end;\n  let m = $.rect(x, y, w, h, { r: 26 * k, fill: c.surface, stroke: c.line, sw: 1.5 * k, shadow: \"lg\" });\n  const icx = x + pad, icy = y + pad;\n  m += $.rect(icx, icy, ic, ic, { r: ic * 0.28, fill: done ? c.green : c.accent });\n  if (done) m += $.path(\"M\" + $.fx(icx + ic * 0.27) + \" \" + $.fx(icy + ic * 0.52) + \"L\" + $.fx(icx + ic * 0.44) + \" \" + $.fx(icy + ic * 0.68) + \"L\" + $.fx(icx + ic * 0.74) + \" \" + $.fx(icy + ic * 0.34), { stroke: $.on(c.green), sw: 6 * k, draw: $.E.out3($.seg(t, end, end + 0.3)) });\n  else m += $.icon(p.icon || \"download\", icx + ic * 0.22, icy + ic * 0.22, ic * 0.56, { color: $.on(c.accent), sw: 2.6 });\n  const tx = icx + ic + 24 * k;\n  const label = done && p.done ? String(p.done) : String(p.label || \"\");\n  m += $.text(label, Object.assign({}, lf, { x: tx, y: icy + (ic - lf.size) / 2 - 2 * k, fill: c.fg, maxW: w - (tx - x) - pad - 120 * k }));\n  m += $.text(Math.round(f * 100) + \"%\", Object.assign({}, mf, { x: x + w - pad, y: icy + (ic - mf.size) / 2, anchor: \"end\", fill: done ? c.green : c.accent }));\n  const bx = x + pad, by = y + h - pad - barH, bw = w - pad * 2;\n  m += $.rect(bx, by, bw, barH, { r: barH / 2, fill: $.rgba(c.fg, 0.1) });\n  if (f > 0) {\n    // Moving stripes inside the fill while it works.\n    const fw = Math.max(barH, bw * f), id = $.uid();\n    $.def('<pattern id=\"' + id + '\" width=\"' + 24 * k + '\" height=\"' + 24 * k + '\" patternUnits=\"userSpaceOnUse\" patternTransform=\"rotate(45) translate(' + $.fx((t * 40 * k) % (24 * k)) + ' 0)\"><rect width=\"' + 12 * k + '\" height=\"' + 24 * k + '\" fill=\"#FFFFFF\" fill-opacity=\"0.22\"/></pattern>');\n    m += $.rect(bx, by, fw, barH, { r: barH / 2, fill: done ? c.green : c.accent });\n    if (!done) m += $.rect(bx, by, fw, barH, { r: barH / 2, fill: \"url(#\" + id + \")\" });\n  }\n  return M.part(\"card\", { at: tin, in: \"rise\", distance: 50, ease: \"bouncy\", out: \"fade\", box: { x: x, y: y, w: w, h: h } }, m);\n}\n","libs":[]},{"id":"before-after","name":"Before and after","description":"Two pictures (or two tiles when none are given) split by a handle that sweeps across to reveal the after side and settles in the middle, each side labelled.","category":"callout","tags":["before after","compare","split","reveal","transformation"],"placement":"full","duration":{"default":5,"min":3,"max":20},"props":{"caption":{"type":"text","label":"Caption","default":"Same footage, one pass of the agent","maxLength":60},"before":{"type":"text","label":"Before label","default":"Before","maxLength":16},"after":{"type":"text","label":"After label","default":"After","maxLength":16},"beforeImage":{"type":"image","label":"Before picture","default":""},"afterImage":{"type":"image","label":"After picture","default":""}},"parts":{"ground":"the ground","frame":"the pictures, handle and labels","caption":"the caption"},"beats":{"in":0.15,"out":-0.45},"style":"studio","pack":"essentials","ref":"essentials/before-after","code":"// MONARC Essentials — helpers this pack's items share (prepended to each item's code). The layout\n// helpers (safeArea, fitFont, fitLines, landOn, widest) live in the motion kit.\n\n/** Words of `text` (with *marks*) laid into `lines` (the same words without marks), for $.sync / $.headline. */\nfunction markedRows(text, lines) {\n  const words = String(text).split(/\\s+/).filter(Boolean);\n  const rows = [];\n  let i = 0;\n  for (let l = 0; l < lines.length; l++) {\n    const n = String(lines[l]).split(/\\s+/).filter(Boolean).length;\n    rows.push(words.slice(i, i + n).join(\" \"));\n    i += n;\n  }\n  if (i < words.length && rows.length) rows[rows.length - 1] += \" \" + words.slice(i).join(\" \");\n  return rows;\n}\n\n// Before and after: two pictures (or two tiles when none are given) split by a handle that sweeps across to\n// reveal the after side, overshoots a little and settles; each side carries its label.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const tin = $.beat(\"in\", 0.1);\n  let s = M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: W, h: H } }));\n  const cap = String(p.caption || \"\");\n  const cf = fitFont($, cap, \"display\", (A.tall ? 64 : 56) * k, A.w, { ls: -0.02 });\n  const capH = cap ? cf.size * 1.6 : 0;\n  // The frame: as big as fits, 16:9 inside wide and square frames, 4:5 inside tall ones.\n  const ar = A.tall ? 4 / 5 : 16 / 9;\n  let fw = A.w, fh = fw / ar;\n  if (fh > A.h - capH) { fh = A.h - capH; fw = fh * ar; }\n  const fx = W / 2 - fw / 2, fy = A.y + (A.h - capH - fh) / 2 + capH;\n  const r = $.style.radius * 0.8;\n  const side = function (key, img, tone, word) {\n    let m = \"\";\n    if (img) m += $.image(img, fx, fy, fw, fh, { r: r });\n    else {\n      // No picture: a flat grey \"before\" and the brand's colours \"after\".\n      m += $.rect(fx, fy, fw, fh, { r: r, fill: tone === \"before\" ? $.mix(c.bg, c.fg, 0.18) : $.gradient([c.accent, c.accent2], { angle: 135 }) });\n      m += $.clip(fx, fy, fw, fh, r, $.pattern(t, tone === \"before\" ? \"hatch\" : \"dots\", { box: { x: fx, y: fy, w: fw, h: fh }, color: tone === \"before\" ? c.fg : $.on(c.accent), a: 0.1 }));\n      const wf = fitFont($, word, \"display\", fh * 0.2, fw * 0.4, { ls: -0.03 });\n      m += $.text(word, Object.assign({}, wf, { x: tone === \"before\" ? fx + fw * 0.25 : fx + fw * 0.75, y: fy + fh / 2 - wf.size * 0.55, anchor: \"middle\", fill: tone === \"before\" ? $.rgba(c.fg, 0.35) : $.on(c.accent), op: 0.9 }));\n    }\n    return m;\n  };\n  const before = side(\"before\", p.beforeImage ? \"beforeImage\" : \"\", \"before\", String(p.before || \"Before\"));\n  const after = side(\"after\", p.afterImage ? \"afterImage\" : \"\", \"after\", String(p.after || \"After\"));\n  // Before on the left, after on the right. The handle starts at the right edge (all before), sweeps left to\n  // show the after side, then springs back to the middle.\n  const go = $.E.inOut3($.seg(t, tin + 0.6, tin + 1.8));\n  const back = $.spring(Math.max(0, t - tin - 1.8), 2.2, 6);\n  const pos = t < tin + 1.8 ? 0.98 - 0.9 * go : 0.08 + 0.42 * back;\n  const hx = fx + fw * pos;\n  const clip = $.uid();\n  $.def('<clipPath id=\"' + clip + '\"><rect x=\"' + hx + '\" y=\"' + fy + '\" width=\"' + Math.max(0, fx + fw - hx) + '\" height=\"' + fh + '\"/></clipPath>');\n  let m = before + '<g clip-path=\"url(#' + clip + ')\">' + after + \"</g>\";\n  m += $.rect(hx - 3 * k, fy, 6 * k, fh, { fill: \"#FFFFFF\" });\n  m += $.circle(hx, fy + fh / 2, 34 * k, { fill: \"#FFFFFF\", shadow: \"md\" });\n  m += $.icon(\"arrowLeft\", hx - 30 * k, fy + fh / 2 - 13 * k, 26 * k, { color: c.ink, sw: 3 });\n  m += $.icon(\"arrowRight\", hx + 4 * k, fy + fh / 2 - 13 * k, 26 * k, { color: c.ink, sw: 3 });\n  // The labels ride in the top corners.\n  const chip = function (txt, x, anchor, fill) { return $.chip($.case(txt), x, fy + 24 * k, { size: 28 * k, fill: fill, color: $.on(fill), anchor: anchor }); };\n  m += chip(String(p.before || \"Before\"), fx + 24 * k, \"start\", c.ink);\n  m += chip(String(p.after || \"After\"), fx + fw - 24 * k, \"end\", c.accent);\n  s += M.part(\"frame\", { at: tin, in: \"pop\", out: \"fade\", box: { x: fx, y: fy, w: fw, h: fh } }, m);\n  if (cap) s += M.text(\"caption\", Object.assign({ at: tin + 0.3, in: \"rise\", distance: 20 }, cf, { x: W / 2, y: fy - capH, anchor: \"middle\", fill: c.fg }), $.case(cap));\n  return s;\n}\n","libs":[]},{"id":"agenda","name":"Agenda","description":"\"In this video\": the topics as a numbered list with their timestamps, a marker moving down to light the current one and the ones before it ticked off.","category":"list","tags":["agenda","contents","chapters","timestamps","list"],"placement":"full","duration":{"default":5,"min":3,"max":20},"props":{"title":{"type":"text","label":"Title","default":"In this video","maxLength":40},"rows":{"type":"list","label":"Topics (Topic: time)","default":[{"topic":"Why the first frame matters","time":"0:00"},{"topic":"Cutting on the beat","time":"1:42"},{"topic":"Captions that keep people","time":"3:10"},{"topic":"The export settings we use","time":"5:05"}],"maxItems":8,"minItems":1,"of":{"topic":"text","time":"text"}},"current":{"type":"number","label":"Current topic","default":3,"min":1,"max":8}},"parts":{"ground":"the ground","title":"the heading","rows":"each topic"},"beats":{"in":0.15,"out":-0.45},"style":"studio","pack":"essentials","ref":"essentials/agenda","code":"// MONARC Essentials — helpers this pack's items share (prepended to each item's code). The layout\n// helpers (safeArea, fitFont, fitLines, landOn, widest) live in the motion kit.\n\n/** Words of `text` (with *marks*) laid into `lines` (the same words without marks), for $.sync / $.headline. */\nfunction markedRows(text, lines) {\n  const words = String(text).split(/\\s+/).filter(Boolean);\n  const rows = [];\n  let i = 0;\n  for (let l = 0; l < lines.length; l++) {\n    const n = String(lines[l]).split(/\\s+/).filter(Boolean).length;\n    rows.push(words.slice(i, i + n).join(\" \"));\n    i += n;\n  }\n  if (i < words.length && rows.length) rows[rows.length - 1] += \" \" + words.slice(i).join(\" \");\n  return rows;\n}\n\n// \"In this video\": the topics as a numbered list with their timestamps, a marker moving down to light the\n// current one, the ones before it ticked off.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const tin = $.beat(\"in\", 0.1);\n  let s = M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: W, h: H } }));\n  const rows = (p.rows || []).slice(0, 8);\n  const n = Math.max(1, rows.length);\n  const title = $.case(String(p.title || \"\"));\n  const tf = fitFont($, title, \"display\", (A.tall ? 96 : 84) * k, A.w, { ls: -0.03 });\n  const listW = Math.min(A.w, 1100 * k);\n  const rh = Math.min((A.h - tf.size * 1.6) / n, (A.tall ? 150 : 120) * k);\n  const top = A.y + (A.h - tf.size * 1.6 - rh * n) / 2;\n  const lx = W / 2 - listW / 2;\n  s += M.text(\"title\", Object.assign({ at: tin, in: \"rise\", distance: 26 }, tf, { x: lx, y: top, fill: c.fg }), title);\n  const ly = top + tf.size * 1.6;\n  // The marker steps to the current topic once the list is in.\n  const cur = Math.max(0, Math.min(n - 1, Math.round(p.current) - 1));\n  const stepT = tin + 0.6 + n * 0.08;\n  const glide = $.E.inOut3($.seg(t, stepT, stepT + 0.6));\n  const my = ly + rh * cur * glide;\n  const nf = Object.assign({}, $.font(\"mono\"), { size: Math.min(28 * k, rh * 0.26), weight: 700 });\n  const lf = Object.assign({}, $.font(\"display\"), { size: Math.min(46 * k, rh * 0.38) });\n  let list = $.rect(lx - 16 * k, my + rh * 0.08, listW + 32 * k, rh * 0.84, { r: rh * 0.2, fill: $.rgba(c.accent, 0.12), op: $.fadeIn(t, stepT, 0.3) });\n  rows.forEach(function (r, i) {\n    const y = ly + i * rh, mid = y + rh / 2, at = tin + 0.3 + i * 0.08;\n    const past = i < cur && glide >= (i + 1) / Math.max(1, cur), lit = i === cur && glide >= 1;\n    let m = \"\";\n    const bx = lx + 18 * k, br = rh * 0.22;\n    m += $.circle(bx, mid, br, { fill: past ? c.accent : lit ? c.accent : \"none\", stroke: past || lit ? c.accent : c.line, sw: 3 * k });\n    if (past) m += $.path(\"M\" + $.fx(bx - br * 0.45) + \" \" + $.fx(mid) + \"L\" + $.fx(bx - br * 0.1) + \" \" + $.fx(mid + br * 0.35) + \"L\" + $.fx(bx + br * 0.5) + \" \" + $.fx(mid - br * 0.35), { stroke: $.on(c.accent), sw: 4 * k });\n    else m += $.text(String(i + 1), Object.assign({}, nf, { size: br * 1.1, x: bx, y: mid - br * 0.6, anchor: \"middle\", fill: lit ? $.on(c.accent) : c.mute }));\n    const tsW = $.measure(String(r.time || \"\"), nf);\n    m += $.text(String(r.topic || \"\"), Object.assign({}, lf, { x: bx + br + 28 * k, y: mid - lf.size * 0.56, fill: past ? c.mute : c.fg, maxW: listW - (br * 2 + 60 * k) - tsW - 40 * k }));\n    m += $.text(String(r.time || \"\"), Object.assign({}, nf, { x: lx + listW, y: mid - nf.size * 0.55, anchor: \"end\", fill: lit ? c.accent : c.mute }));\n    if (i < n - 1) m += $.line(lx, y + rh, lx + listW, y + rh, { stroke: c.line, sw: 1.5 * k });\n    list += M.part(\"rows\", { at: at, in: \"rise\", distance: 20, out: \"fade\", box: { x: lx, y: y, w: listW, h: rh } }, m);\n  });\n  return s + list;\n}\n","libs":[]}]},{"format":1,"id":"broadcast","name":"MONARC Broadcast","version":"1.0.0","description":"Lower thirds in five looks, a news ticker, a live badge, a score bug, a location pin and a countdown — broadcast graphics in any brand, any aspect.","author":"MONARC STUDIOS","license":"MONARC Packs License 1.0 (free to use in any video; not for resale as templates)","homepage":"https://volito.dev/docs/packs/broadcast","style":"ember","items":["lt-bars","lt-reveal","lt-kicker","lt-pill","lt-news","ticker","live-badge","score-bug","location","countdown"],"entries":[{"id":"lt-bars","name":"Lower third — two bars","description":"A name bar wipes in from one side and a role bar in the accent from the other, the words rising inside their bars.","category":"lower-third","tags":["lower-third","overlay","bold","interview"],"placement":"overlay","duration":{"default":5,"min":2.5,"max":30},"props":{"name":{"type":"text","label":"Name","default":"Priya Sharma","maxLength":32},"role":{"type":"text","label":"Role","default":"Product designer","maxLength":40},"side":{"type":"choice","label":"Side","options":["left","right"],"default":"left"}},"parts":{"scrim":"the soft wash behind it","nameBar":"the bar behind the name","name":"the name","roleBar":"the accent bar behind the role","role":"the role"},"beats":{"in":0.15,"out":-0.45},"style":"ember","pack":"broadcast","ref":"broadcast/lt-bars","code":"function frame(t, $) {\n  const p = $.data, c = $.c, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const k = $.k * (A.tall ? 1.2 : 1);\n  const tin = $.beat(\"in\", 0.15);\n  const right = p.side === \"right\";\n  const side = right ? \"right\" : \"left\", other = right ? \"left\" : \"right\";\n  const name = $.case(p.name), role = String(p.role || \"\").toUpperCase();\n  const tName = landOn($, p.name, tin);\n  const tRole = landOn($, p.role, tName + 0.3, tName);\n  const maxW = A.w * (A.tall ? 0.9 : 0.6);\n  const nf = fitFont($, name, \"display\", 78 * k, maxW - 80 * k, { ls: -0.01 });\n  const rf = fitFont($, role, \"mono\", 30 * k, maxW - 80 * k, { ls: 0.12 });\n  const pad = 34 * k;\n  const nw = $.measure(name, nf) + pad * 2, nh = nf.size * 1.34;\n  const rw = role ? $.measure(role, rf) + pad * 1.6 : 0, rh = rf.size * 1.95;\n  const y = A.tall ? H * 0.6 : A.square ? H * 0.66 : H * 0.7;\n  const x = right ? W - A.x - nw : A.x;\n  const inset = 36 * k;\n  const rx = right ? W - A.x - inset - rw : A.x + inset;\n  const leave = { out: \"wipe\", outFrom: side, outDur: 0.35 };\n  let s = M.part(\"scrim\", { at: tin - 0.1, in: \"fade\", dur: 0.4, out: \"fade\", vary: false }, $.scrim(0, y - 240 * k, W, H - y + 240 * k, { to: \"bottom\", a: 0.4 }));\n  s += M.part(\"nameBar\", Object.assign({ at: tin, in: \"wipe\", from: side, dur: 0.45, ease: \"expo\", box: { x: x, y: y, w: nw, h: nh } }, leave), $.rect(x, y, nw, nh, { fill: c.surface, shadow: \"md\" }));\n  s += $.clip(x, y, nw, nh, 0, M.text(\"name\", Object.assign({ at: tName + 0.18, in: \"rise\", distance: 40 }, leave, nf, { x: right ? x + nw - pad : x + pad, y: y + (nh - nf.size) / 2 - nf.size * 0.02, anchor: right ? \"end\" : \"start\", fill: c.fg }), name));\n  if (role) {\n    s += M.part(\"roleBar\", Object.assign({ at: tRole, in: \"wipe\", from: other, dur: 0.4, ease: \"expo\", box: { x: rx, y: y + nh, w: rw, h: rh } }, leave), $.rect(rx, y + nh, rw, rh, { fill: c.accent }));\n    s += $.clip(rx, y + nh, rw, rh, 0, M.text(\"role\", Object.assign({ at: tRole + 0.15, in: \"rise\", distance: 24 }, leave, rf, { x: rx + rw / 2, y: y + nh + (rh - rf.size) / 2 - rf.size * 0.05, anchor: \"middle\", fill: $.on(c.accent) }), role));\n  }\n  return s;\n}\n","libs":[]},{"id":"lt-reveal","name":"Lower third — mask reveal","description":"Cardless: an accent sweep crosses the frame and leaves the name behind it, then the role fades up. Made for footage.","category":"lower-third","tags":["lower-third","overlay","cardless","minimal"],"placement":"overlay","duration":{"default":5,"min":2.5,"max":30},"props":{"name":{"type":"text","label":"Name","default":"Maya Chen","maxLength":32},"role":{"type":"text","label":"Role","default":"Documentary editor","maxLength":44},"side":{"type":"choice","label":"Side","options":["left","right"],"default":"left"}},"parts":{"scrim":"the soft wash behind it","sweep":"the accent bar that crosses","name":"the name","role":"the role"},"beats":{"in":0.15,"out":-0.45},"style":"ember","pack":"broadcast","ref":"broadcast/lt-reveal","code":"function frame(t, $) {\n  const p = $.data, c = $.c, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const k = $.k * (A.tall ? 1.2 : 1);\n  const tin = landOn($, p.name, $.beat(\"in\", 0.15));\n  const right = p.side === \"right\";\n  const name = $.case(p.name), role = String(p.role || \"\");\n  const maxW = A.w * (A.tall ? 0.92 : 0.66);\n  const nf = fitFont($, name, \"display\", 104 * k, maxW, { ls: -0.02 });\n  const rf = fitFont($, role, \"text\", 36 * k, maxW);\n  const nw = $.measure(name, nf);\n  const y = A.tall ? H * 0.6 : A.square ? H * 0.64 : H * 0.68;\n  const x = right ? W - A.x - nw : A.x;\n  // The sweep runs across the name; the name shows behind its trailing edge.\n  const sw = Math.max(26 * k, nf.size * 0.32), sh = nf.size * 1.16;\n  const run = $.E.inOut3($.seg(t, tin, tin + 0.7));\n  const lead = right ? x + nw + sw - (nw + sw * 2) * run : x - sw + (nw + sw * 2) * run;\n  const shown = right ? { x: lead, w: x + nw + 20 * k - lead } : { x: x - 20 * k, w: lead - x + 20 * k };\n  let s = M.part(\"scrim\", { at: tin - 0.1, in: \"fade\", dur: 0.45, out: \"fade\", vary: false }, $.scrim(0, y - 280 * k, W, H - y + 280 * k, { to: \"bottom\", a: 0.55 }));\n  if (t >= tin && shown.w > 0) s += $.clip(shown.x, y - sh, Math.max(0, shown.w), sh * 2.2, 0, M.text(\"name\", { at: tin, in: \"none\", out: \"fade\", ...nf, x: x, y: y, fill: \"#FFFFFF\", shadow: \"md\" }, name));\n  if (run > 0 && run < 1) s += M.part(\"sweep\", { at: tin, in: \"none\", out: \"none\", vary: false }, $.rect(right ? lead : lead - sw, y - sh * 0.08, sw, sh, { fill: c.accent }));\n  if (role) s += M.text(\"role\", { at: tin + 0.55, in: \"rise\", distance: 18, ...rf, x: right ? x + nw : x, y: y + nf.size * 1.12, anchor: right ? \"end\" : \"start\", fill: \"rgba(255,255,255,0.82)\", shadow: \"sm\" }, role);\n  return s;\n}\n","libs":[]},{"id":"lt-kicker","name":"Lower third — kicker","description":"A kicker tag in the accent, a heavy name and a baseline that draws under it, the role beneath. Cardless, for footage.","category":"lower-third","tags":["lower-third","overlay","podcast","bold"],"placement":"overlay","duration":{"default":5,"min":2.5,"max":30},"props":{"kicker":{"type":"text","label":"Kicker","default":"Guest","maxLength":18},"name":{"type":"text","label":"Name","default":"Jordan Okafor","maxLength":30},"role":{"type":"text","label":"Role","default":"Founder, Northwind Studio","maxLength":44},"side":{"type":"choice","label":"Side","options":["left","right"],"default":"left"}},"parts":{"scrim":"the soft wash behind it","kicker":"the tag above the name","name":"the name","rule":"the baseline","role":"the role"},"beats":{"in":0.15,"out":-0.45},"style":"ember","pack":"broadcast","ref":"broadcast/lt-kicker","code":"function frame(t, $) {\n  const p = $.data, c = $.c, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const k = $.k * (A.tall ? 1.2 : 1);\n  const tin = $.beat(\"in\", 0.15);\n  const right = p.side === \"right\";\n  const side = right ? \"right\" : \"left\";\n  const anchor = right ? \"end\" : \"start\";\n  const name = $.case(p.name), role = String(p.role || \"\"), kick = String(p.kicker || \"\").toUpperCase();\n  const tName = landOn($, p.name, tin + 0.15);\n  const maxW = A.w * (A.tall ? 0.92 : 0.62);\n  const nf = fitFont($, name, \"display\", 92 * k, maxW, { ls: -0.02 });\n  const rf = fitFont($, role, \"text\", 34 * k, maxW);\n  const nw = $.measure(name, nf);\n  const kf = { ...$.font(\"mono\"), size: 26 * k, ls: 0.14 };\n  const chip = kick ? { w: $.measure(kick, kf) + 30 * k, h: kf.size * 1.75 } : { w: 0, h: 0 };\n  const total = chip.h + 18 * k + nf.size * 1.05 + 22 * k + rf.size * 1.3;\n  const y = (A.tall ? H * 0.62 : A.square ? H * 0.68 : H * 0.72) - total / 2 + 40 * k;\n  const x = right ? W - A.x : A.x;\n  const leave = { out: \"slide\", outFrom: side, distance: 24 };\n  let s = M.part(\"scrim\", { at: tin - 0.1, in: \"fade\", dur: 0.45, out: \"fade\", vary: false }, $.scrim(0, y - 220 * k, W, H - y + 220 * k, { to: \"bottom\", a: 0.55 }));\n  let yy = y;\n  if (kick) {\n    const cx = right ? x - chip.w : x;\n    s += M.part(\"kicker\", Object.assign({ at: tin, in: \"pop\", origin: side, box: { x: cx, y: yy, w: chip.w, h: chip.h } }, leave), $.rect(cx, yy, chip.w, chip.h, { r: chip.h * 0.18, fill: c.accent }) + $.text(kick, { ...kf, x: cx + chip.w / 2, y: yy + (chip.h - kf.size) / 2 - kf.size * 0.06, anchor: \"middle\", fill: $.on(c.accent) }));\n    yy += chip.h + 18 * k;\n  }\n  s += M.text(\"name\", Object.assign({ at: tName, in: \"rise\", distance: 30, split: \"words\", stagger: 0.07 }, leave, nf, { x: x, y: yy, anchor: anchor, fill: \"#FFFFFF\", shadow: \"md\" }), name);\n  yy += nf.size * 1.05;\n  const rx = right ? x - nw : x;\n  s += M.part(\"rule\", Object.assign({ at: tName + 0.3, in: \"grow-x\", origin: side, dur: 0.45, ease: \"expo\", box: { x: rx, y: yy, w: nw, h: 6 * k } }, leave), $.rect(rx, yy, nw, 6 * k, { r: 3 * k, fill: c.accent }));\n  yy += 22 * k;\n  if (role) s += M.text(\"role\", Object.assign({ at: tName + 0.5, in: \"fade\", dur: 0.4 }, leave, rf, { x: x, y: yy, anchor: anchor, fill: \"rgba(255,255,255,0.85)\", shadow: \"sm\" }), role);\n  return s;\n}\n","libs":[]},{"id":"lt-pill","name":"Lower third — pill","description":"A rounded pill with a picture or initials, a status dot, the name and the role. Pops in; good for podcasts and calls.","category":"lower-third","tags":["lower-third","overlay","podcast","minimal","avatar"],"placement":"overlay","duration":{"default":5,"min":2.5,"max":30},"props":{"name":{"type":"text","label":"Name","default":"Alex Rivera","maxLength":28},"role":{"type":"text","label":"Role","default":"Host · The Long Cut","maxLength":40},"photo":{"type":"image","label":"Photo","description":"A square picture of the person; initials when empty"},"status":{"type":"choice","label":"Status dot","options":["live","online","none"],"default":"live"},"side":{"type":"choice","label":"Side","options":["left","right"],"default":"left"}},"parts":{"pill":"the pill","avatar":"the picture or initials","dot":"the status dot","name":"the name","role":"the role"},"beats":{"in":0.15,"out":-0.45},"style":"ember","pack":"broadcast","ref":"broadcast/lt-pill","code":"function frame(t, $) {\n  const p = $.data, c = $.c, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const k = $.k * (A.tall ? 1.3 : 1.15);\n  const tin = landOn($, p.name, $.beat(\"in\", 0.15));\n  const right = p.side === \"right\";\n  const side = right ? \"right\" : \"left\";\n  const name = $.case(p.name), role = String(p.role || \"\");\n  const maxW = A.w * (A.tall ? 0.92 : 0.5);\n  const h = 128 * k, r = h / 2, av = h * 0.38;\n  const nf = fitFont($, name, \"display\", 48 * k, maxW - h - 60 * k);\n  const rf = fitFont($, role, \"text\", 30 * k, maxW - h - 60 * k);\n  const w = h + Math.max($.measure(name, nf), role ? $.measure(role, rf) : 0) + 56 * k;\n  const y = A.tall ? H * 0.62 : A.square ? H * 0.7 : H * 0.74;\n  const x = right ? W - A.x - w : A.x;\n  const ax = x + r, ay = y + r;\n  const dotCol = p.status === \"live\" ? c.red : p.status === \"online\" ? c.green : null;\n  const leave = { out: \"pop\" };\n  let s = M.part(\"pill\", Object.assign({ at: tin, in: \"pop\", origin: side, box: { x: x, y: y, w: w, h: h } }, leave), $.rect(x, y, w, h, { r: r, fill: c.surface, stroke: c.line, sw: 2 * k, shadow: \"lg\" }));\n  s += M.part(\"avatar\", Object.assign({ at: tin + 0.12, in: \"pop\", box: { x: ax - av, y: ay - av, w: av * 2, h: av * 2 } }, leave), avatar($, \"photo\", ax, ay, av, { name: p.name, fill: c.accent }));\n  if (dotCol) s += M.part(\"dot\", Object.assign({ at: tin + 0.35, in: \"pop\", idle: p.status === \"live\" ? \"pulse\" : \"none\", box: { x: ax + av * 0.5, y: ay + av * 0.5, w: av * 0.6, h: av * 0.6 } }, leave), $.circle(ax + av * 0.72, ay + av * 0.72, av * 0.24, { fill: dotCol, stroke: c.surface, sw: 4 * k }));\n  const tx = x + h + 4 * k;\n  const ty = role ? y + h / 2 - (nf.size * 1.08 + rf.size) / 2 : y + (h - nf.size) / 2;\n  s += M.text(\"name\", Object.assign({ at: tin + 0.18, in: \"rise\", distance: 14 }, leave, nf, { x: tx, y: ty, fill: c.fg }), name);\n  if (role) s += M.text(\"role\", Object.assign({ at: tin + 0.3, in: \"fade\", dur: 0.35 }, leave, rf, { x: tx, y: ty + nf.size * 1.08, fill: c.mute }), role);\n  return s;\n}\n","libs":[]},{"id":"lt-news","name":"Lower third — news boxes","description":"Broadcast style: a tight headline box with a hard accent shadow, a sub-line box beneath and an optional tag above.","category":"lower-third","tags":["lower-third","overlay","news","broadcast"],"placement":"overlay","duration":{"default":6,"min":3,"max":30},"props":{"tag":{"type":"text","label":"Tag","default":"Exclusive","maxLength":16},"headline":{"type":"text","label":"Headline","default":"Creators switch to AI editing","maxLength":48},"sub":{"type":"text","label":"Sub-line","default":"Studios report three times the output","maxLength":56}},"parts":{"tag":"the tag above","headline":"the headline box and its words","sub":"the sub-line box and its words"},"beats":{"in":0.15,"out":-0.45},"style":"ember","pack":"broadcast","ref":"broadcast/lt-news","code":"function frame(t, $) {\n  const p = $.data, c = $.c, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const k = $.k * (A.tall ? 1.2 : 1);\n  const tin = landOn($, p.headline, $.beat(\"in\", 0.15));\n  const head = $.case(p.headline), sub = String(p.sub || \"\"), tag = String(p.tag || \"\").toUpperCase();\n  const maxW = A.w * (A.tall ? 0.94 : 0.72);\n  const hf = fitFont($, head, \"display\", 64 * k, maxW - 50 * k, { ls: -0.01 });\n  const sf = fitFont($, sub, \"text\", 38 * k, maxW - 40 * k);\n  const tf = { ...$.font(\"mono\"), size: 28 * k, ls: 0.16 };\n  const pad = 24 * k, sh = 9 * k;\n  const hw = $.measure(head, hf) + pad * 2, hh = hf.size * 1.36;\n  const sw = sub ? $.measure(sub, sf) + pad * 1.6 : 0, shh = sf.size * 1.6;\n  const tw = tag ? $.measure(tag, tf) + pad * 1.4 : 0, th = tf.size * 1.8;\n  const blockH = (tag ? th : 0) + hh + (sub ? shh : 0);\n  const y0 = (A.tall ? H * 0.64 : A.square ? H * 0.7 : H * 0.74) - blockH / 2;\n  const x = A.x;\n  let y = y0;\n  let s = \"\";\n  if (tag) {\n    s += M.part(\"tag\", { at: tin, in: \"pop\", origin: \"bottom-left\", out: \"fade\", box: { x: x, y: y, w: tw, h: th } }, $.rect(x, y, tw, th, { fill: c.ink }) + $.text(tag, { ...tf, x: x + tw / 2, y: y + (th - tf.size) / 2 - tf.size * 0.05, anchor: \"middle\", fill: \"#FFFFFF\" }));\n    y += th;\n  }\n  s += M.part(\"headline\", { at: tin + 0.1, in: \"wipe\", from: \"left\", dur: 0.45, ease: \"expo\", out: \"wipe\", outFrom: \"right\", box: { x: x, y: y, w: hw + sh, h: hh + sh } },\n    $.rect(x + sh, y + sh, hw, hh, { fill: c.accent }), $.rect(x, y, hw, hh, { fill: \"#FFFFFF\" }), $.text(head, { ...hf, x: x + pad, y: y + (hh - hf.size) / 2 - hf.size * 0.03, fill: \"#111318\" }));\n  if (sub) s += M.part(\"sub\", { at: tin + 0.4, in: \"wipe\", from: \"left\", dur: 0.4, ease: \"expo\", out: \"wipe\", outFrom: \"right\", box: { x: x, y: y + hh, w: sw, h: shh } },\n    $.rect(x, y + hh, sw, shh, { fill: c.accent }), $.text(sub, { ...sf, x: x + pad * 0.8, y: y + hh + (shh - sf.size) / 2 - sf.size * 0.06, fill: $.on(c.accent) }));\n  return s;\n}\n","libs":[]},{"id":"ticker","name":"News ticker","description":"A crawl along the bottom: a label block in the accent and a line of items scrolling left without a break.","category":"banner","tags":["ticker","crawl","news","overlay","broadcast"],"placement":"overlay","duration":{"default":10,"min":3,"max":120},"props":{"label":{"type":"text","label":"Label","default":"Latest","maxLength":14},"items":{"type":"list","label":"Items","default":["Volito ships 96 graphics","3D text renders without a GPU","Agents preview a graphic in 0.2 s","Every pack works in any brand"],"maxItems":12,"of":"text"},"speed":{"type":"number","label":"Speed (px/s at 1080p)","default":170,"min":40,"max":600},"position":{"type":"choice","label":"Position","options":["bottom","top"],"default":"bottom"}},"parts":{"bar":"the strip","label":"the label block","items":"the scrolling words"},"beats":{"in":0.1,"out":-0.4},"style":"ember","pack":"broadcast","ref":"broadcast/ticker","code":"function frame(t, $) {\n  const p = $.data, c = $.c, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const k = $.k * (A.tall ? 1.15 : 1);\n  const tin = $.beat(\"in\", 0.1);\n  const bh = 74 * k;\n  const top = p.position === \"top\";\n  const y = top ? A.y * 0.6 : A.tall ? A.y + A.h - bh : H - bh - H * 0.05;\n  const label = String(p.label || \"\").toUpperCase();\n  const lf = { ...$.font(\"display\"), size: 32 * k, ls: 0.06 };\n  const lw = label ? $.measure(label, lf) + 56 * k : 0;\n  const tf = { ...$.font(\"text\"), size: 32 * k };\n  const items = (p.items || []).map(function (s) { return String(s); }).filter(Boolean);\n  const loop = items.length ? items.join(\"   •   \") + \"   •   \" : \"\";\n  const loopW = loop ? $.measure(loop, tf) : 1;\n  const leave = { out: \"slide\", outFrom: top ? \"up\" : \"down\", distance: 40 };\n  let s = M.part(\"bar\", Object.assign({ at: tin, in: \"slide\", from: top ? \"up\" : \"down\", distance: 40, box: { x: 0, y: y, w: W, h: bh } }, leave), $.rect(0, y, W, bh, { fill: c.ink }), $.rect(0, top ? y + bh - 4 * k : y, W, 4 * k, { fill: c.accent }));\n  // The crawl: copies of the loop, moving left at `speed` from when the bar lands.\n  const run = Math.max(0, t - tin - 0.3) * p.speed * k;\n  const off = run % loopW;\n  let row = \"\";\n  const x0 = lw + 30 * k - off;\n  for (let i = 0; x0 + i * loopW < W + loopW && i < 40; i++) row += $.text(loop, { ...tf, x: x0 + i * loopW, y: y + (bh - tf.size) / 2 - tf.size * 0.04, fill: \"#FFFFFF\", within: { x: -1e6, y: 0, w: 2e6, h: H } });\n  s += $.clip(lw, y, W - lw, bh, 0, M.part(\"items\", Object.assign({ at: tin + 0.3, in: \"fade\", dur: 0.4 }, leave), row));\n  if (label) s += M.part(\"label\", Object.assign({ at: tin + 0.1, in: \"wipe\", from: \"left\", dur: 0.4, ease: \"expo\", box: { x: 0, y: y, w: lw, h: bh } }, leave), $.rect(0, y, lw, bh, { fill: c.accent }), $.text(label, { ...lf, x: lw / 2, y: y + (bh - lf.size) / 2 - lf.size * 0.03, anchor: \"middle\", fill: $.on(c.accent) }));\n  return s;\n}\n","libs":[]},{"id":"live-badge","name":"Live badge","description":"A LIVE pill with a pulsing dot, a viewer count that counts up and keeps ticking, and an optional running clock.","category":"social","tags":["live","badge","stream","overlay","hud"],"placement":"overlay","duration":{"default":6,"min":2,"max":600},"props":{"label":{"type":"text","label":"Label","default":"Live","maxLength":10},"viewers":{"type":"number","label":"Viewers","default":12480,"min":0,"max":99999999},"clock":{"type":"boolean","label":"Running clock","default":true},"elapsed":{"type":"number","label":"Clock starts at (s)","default":2534,"min":0,"max":359999},"corner":{"type":"choice","label":"Corner","options":["top-left","top-right"],"default":"top-left"}},"parts":{"live":"the LIVE pill","viewers":"the viewer count","clock":"the running clock"},"beats":{"in":0.1,"out":-0.35},"style":"ember","pack":"broadcast","ref":"broadcast/live-badge","code":"function frame(t, $) {\n  const p = $.data, c = $.c, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const k = $.k * (A.tall ? 1.3 : 1.2);\n  const tin = $.beat(\"in\", 0.1);\n  const right = p.corner === \"top-right\";\n  const h = 64 * k;\n  const lf = { ...$.font(\"display\"), size: 30 * k, ls: 0.08 };\n  const nf = { ...$.font(\"mono\"), size: 28 * k, weight: 700 };\n  const label = String(p.label || \"LIVE\").toUpperCase();\n  const liveW = h * 0.62 + $.measure(label, lf) + 30 * k;\n  // Viewers: up to the number in 1.2 s, then a slow, fixed drift so the count keeps moving.\n  const up = $.E.out3($.seg(t, tin + 0.3, tin + 1.5));\n  const drift = Math.floor(Math.max(0, t - tin - 1.5) * 1.7);\n  let v = Math.round(p.viewers * up);\n  for (let i = 0; i < drift; i++) v += Math.floor($.hash(i * 7.31) * 9) - 2;\n  const vText = $.count(Math.max(0, v));\n  const vw = h * 0.95 + Math.max($.measure(\"88.8K\", nf), $.measure(vText, nf)) + 24 * k;\n  const secs = Math.floor(p.elapsed + Math.max(0, t - tin));\n  const two = function (n) { return (n < 10 ? \"0\" : \"\") + n; };\n  const cText = Math.floor(secs / 3600) + \":\" + two(Math.floor(secs / 60) % 60) + \":\" + two(secs % 60);\n  const cw = p.clock ? $.measure(\"00:00:00\", nf) + 40 * k : 0;\n  const gap = 12 * k;\n  const total = liveW + gap + vw + (p.clock ? gap + cw : 0);\n  let x = right ? W - A.x - total : A.x;\n  const y = A.y;\n  const leave = { out: \"fade\", outDur: 0.3 };\n  let s = M.part(\"live\", Object.assign({ at: tin, in: \"pop\", origin: right ? \"right\" : \"left\", box: { x: x, y: y, w: liveW, h: h } }, leave),\n    $.rect(x, y, liveW, h, { r: 10 * k, fill: c.red, shadow: \"md\" }),\n    $.circle(x + h * 0.42, y + h / 2, 9 * k, { fill: \"#FFFFFF\", op: 0.45 + 0.55 * $.wave(t, 1.1) }),\n    $.text(label, { ...lf, x: x + h * 0.66, y: y + (h - lf.size) / 2 - lf.size * 0.03, fill: \"#FFFFFF\" }));\n  x += liveW + gap;\n  s += M.part(\"viewers\", Object.assign({ at: tin + 0.15, in: \"slide\", from: right ? \"right\" : \"left\", distance: 16, box: { x: x, y: y, w: vw, h: h } }, leave),\n    $.rect(x, y, vw, h, { r: 10 * k, fill: \"rgba(10,12,16,0.72)\" }),\n    $.icon(\"users\", x + 18 * k, y + (h - 30 * k) / 2, 30 * k, { color: \"#FFFFFF\", sw: 2.2 }),\n    $.text(vText, { ...nf, x: x + h * 0.85, y: y + (h - nf.size) / 2 - nf.size * 0.05, fill: \"#FFFFFF\" }));\n  x += vw + gap;\n  if (p.clock) s += M.part(\"clock\", Object.assign({ at: tin + 0.3, in: \"slide\", from: right ? \"right\" : \"left\", distance: 16, box: { x: x, y: y, w: cw, h: h } }, leave),\n    $.rect(x, y, cw, h, { r: 10 * k, fill: \"rgba(10,12,16,0.72)\" }),\n    $.text(cText, { ...nf, x: x + cw / 2, y: y + (h - nf.size) / 2 - nf.size * 0.05, anchor: \"middle\", fill: \"#FFFFFF\" }));\n  return s;\n}\n","libs":[]},{"id":"score-bug","name":"Score bug","description":"A sports score bug: two teams with their colours and scores, the period and a running clock. A goal adds a point with a flash.","category":"banner","tags":["sports","score","scoreboard","overlay","broadcast"],"placement":"overlay","duration":{"default":8,"min":2,"max":600},"props":{"home":{"type":"text","label":"Home team","default":"MON","maxLength":4},"away":{"type":"text","label":"Away team","default":"NWD","maxLength":4},"homeScore":{"type":"number","label":"Home score","default":2,"min":0,"max":999},"awayScore":{"type":"number","label":"Away score","default":1,"min":0,"max":999},"homeColor":{"type":"color","label":"Home colour","default":"#FF6A3D"},"awayColor":{"type":"color","label":"Away colour","default":"#3DD6FF"},"period":{"type":"text","label":"Period","default":"2nd","maxLength":6},"clock":{"type":"text","label":"Clock (mm:ss)","default":"67:12","maxLength":6},"clockRuns":{"type":"choice","label":"Clock","options":["up","down","stopped"],"default":"up"},"goal":{"type":"choice","label":"Goal","options":["none","home","away"],"default":"home"},"goalAt":{"type":"number","label":"Goal at (s)","default":3.5,"min":0,"max":600}},"parts":{"bug":"the board","home":"the home row","away":"the away row","clock":"the period and clock"},"beats":{"in":0.1,"out":-0.35},"style":"ember","pack":"broadcast","ref":"broadcast/score-bug","code":"function frame(t, $) {\n  const p = $.data, c = $.c, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const k = $.k * (A.tall ? 1.3 : 1.15);\n  const tin = $.beat(\"in\", 0.1);\n  const rowH = 56 * k, sw = 12 * k;\n  const nf = { ...$.font(\"display\"), size: 30 * k, ls: 0.04 };\n  const sf = { ...$.font(\"mono\"), size: 32 * k, weight: 700 };\n  const teamW = Math.max($.measure(\"WWW\", nf), $.measure(String(p.home).toUpperCase(), nf), $.measure(String(p.away).toUpperCase(), nf)) + 30 * k;\n  const scoreW = $.measure(\"888\", sf) + 24 * k;\n  const w = sw + teamW + scoreW;\n  const clockW = $.measure(\"888:88\", sf) + 30 * k;\n  const x = A.x, y = A.y;\n  const scored = p.goal !== \"none\" && t >= p.goalAt;\n  const hs = p.homeScore + (scored && p.goal === \"home\" ? 1 : 0), as = p.awayScore + (scored && p.goal === \"away\" ? 1 : 0);\n  const flash = p.goal !== \"none\" ? $.bump(t, p.goalAt, 0.05, 0.6) : 0;\n  // The clock: mm:ss running up or down from its start, or held.\n  const m = /^(\\d+):(\\d\\d)$/.exec(String(p.clock || \"0:00\"));\n  let secs = m ? Number(m[1]) * 60 + Number(m[2]) : 0;\n  const ran = Math.floor(Math.max(0, t - tin));\n  secs = p.clockRuns === \"up\" ? secs + ran : p.clockRuns === \"down\" ? Math.max(0, secs - ran) : secs;\n  const clock = Math.floor(secs / 60) + \":\" + (secs % 60 < 10 ? \"0\" : \"\") + (secs % 60);\n  const leave = { out: \"slide\", outFrom: \"left\", distance: 30 };\n  let s = M.part(\"bug\", Object.assign({ at: tin, in: \"wipe\", from: \"left\", dur: 0.4, ease: \"expo\", box: { x: x, y: y, w: w + clockW, h: rowH * 2 } }, leave),\n    $.rect(x, y, w, rowH * 2, { fill: c.ink, shadow: \"md\" }), $.rect(x + w, y, clockW, rowH * 2, { fill: c.surface }));\n  const row = function (name, team, score, col, ry, hit) {\n    return M.part(name, Object.assign({ at: tin + (name === \"home\" ? 0.2 : 0.3), in: \"slide\", from: \"left\", distance: 20, box: { x: x, y: ry, w: w, h: rowH } }, leave),\n      $.rect(x, ry, sw, rowH, { fill: col }),\n      $.text(String(team).toUpperCase(), { ...nf, x: x + sw + 16 * k, y: ry + (rowH - nf.size) / 2 - nf.size * 0.03, fill: \"#FFFFFF\" }),\n      hit > 0.01 ? $.rect(x + sw + teamW, ry, scoreW, rowH, { fill: col, op: hit * 0.9 }) : \"\",\n      $.g({ transform: $.tf({ s: 1 + 0.35 * hit, ox: x + sw + teamW + scoreW / 2, oy: ry + rowH / 2 }) }, $.text(String(score), { ...sf, x: x + sw + teamW + scoreW / 2, y: ry + (rowH - sf.size) / 2 - sf.size * 0.05, anchor: \"middle\", fill: \"#FFFFFF\" })));\n  };\n  s += row(\"home\", p.home, hs, p.homeColor, y, p.goal === \"home\" ? flash : 0);\n  s += row(\"away\", p.away, as, p.awayColor, y + rowH, p.goal === \"away\" ? flash : 0);\n  s += M.part(\"clock\", Object.assign({ at: tin + 0.4, in: \"fade\", dur: 0.3, box: { x: x + w, y: y, w: clockW, h: rowH * 2 } }, leave),\n    $.text(String(p.period || \"\").toUpperCase(), { ...$.font(\"mono\"), size: 22 * k, ls: 0.12, x: x + w + clockW / 2, y: y + rowH * 0.42, anchor: \"middle\", fill: c.mute }),\n    $.text(clock, { ...sf, x: x + w + clockW / 2, y: y + rowH * 1.02, anchor: \"middle\", fill: c.fg }));\n  return s;\n}\n","libs":[]},{"id":"location","name":"Location pin","description":"A map pin drops and bounces with a ripple, and a card beside it names the place, its region and its coordinates.","category":"callout","tags":["location","map","pin","place","overlay"],"placement":"overlay","duration":{"default":5,"min":2.5,"max":30},"props":{"place":{"type":"text","label":"Place","default":"Lisbon","maxLength":26},"region":{"type":"text","label":"Region","default":"Portugal","maxLength":32},"coords":{"type":"text","label":"Coordinates","default":"38.72° N, 9.14° W","maxLength":28},"x":{"type":"number","label":"Pin across (0–1)","default":0.3,"min":0.05,"max":0.95},"y":{"type":"number","label":"Pin down (0–1)","default":0.62,"min":0.1,"max":0.9}},"parts":{"pin":"the pin","ripple":"the ring under it","card":"the place card"},"beats":{"in":0.1,"out":-0.4},"style":"ember","pack":"broadcast","ref":"broadcast/location","code":"function frame(t, $) {\n  const p = $.data, c = $.c, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const k = $.k * (A.tall ? 1.2 : 1);\n  const tin = landOn($, p.place, $.beat(\"in\", 0.1));\n  const px = W * p.x, py = Math.max(A.y + 140 * k, Math.min(A.y + A.h, H * p.y));\n  const pw = 64 * k, ph = 88 * k;\n  // A teardrop pin whose point sits on (px, py).\n  const pin = \"M\" + $.fx(px) + \" \" + $.fx(py) + \" C\" + $.fx(px - pw * 0.2) + \" \" + $.fx(py - ph * 0.35) + \" \" + $.fx(px - pw / 2) + \" \" + $.fx(py - ph * 0.5) + \" \" + $.fx(px - pw / 2) + \" \" + $.fx(py - ph * 0.68) + \" A\" + $.fx(pw / 2) + \" \" + $.fx(pw / 2) + \" 0 1 1 \" + $.fx(px + pw / 2) + \" \" + $.fx(py - ph * 0.68) + \" C\" + $.fx(px + pw / 2) + \" \" + $.fx(py - ph * 0.5) + \" \" + $.fx(px + pw * 0.2) + \" \" + $.fx(py - ph * 0.35) + \" \" + $.fx(px) + \" \" + $.fx(py) + \"Z\";\n  const ring = $.seg(t, tin + 0.45, tin + 1.6);\n  let s = \"\";\n  s += M.part(\"ripple\", { at: tin + 0.4, in: \"fade\", dur: 0.1, out: \"fade\", box: { x: px - 90 * k, y: py - 30 * k, w: 180 * k, h: 60 * k } },\n    $.path(\"M\" + $.fx(px - 60 * k) + \" \" + $.fx(py) + \"a\" + $.fx(60 * k) + \" \" + $.fx(18 * k) + \" 0 1 0 \" + $.fx(120 * k) + \" 0a\" + $.fx(60 * k) + \" \" + $.fx(18 * k) + \" 0 1 0 \" + $.fx(-120 * k) + \" 0\", { fill: \"rgba(0,0,0,0.35)\" }),\n    ring > 0 && ring < 1 ? $.path(\"M\" + $.fx(px - 60 * k * (0.4 + ring)) + \" \" + $.fx(py) + \"a\" + $.fx(60 * k * (0.4 + ring)) + \" \" + $.fx(18 * k * (0.4 + ring)) + \" 0 1 0 \" + $.fx(120 * k * (0.4 + ring)) + \" 0a\" + $.fx(60 * k * (0.4 + ring)) + \" \" + $.fx(18 * k * (0.4 + ring)) + \" 0 1 0 \" + $.fx(-120 * k * (0.4 + ring)) + \" 0\", { fill: \"none\", stroke: c.accent, sw: 4 * k * (1 - ring) }) : \"\");\n  s += M.part(\"pin\", { at: tin, in: \"bounce\", origin: \"bottom\", out: \"rise\", box: { x: px - pw / 2, y: py - ph, w: pw, h: ph } },\n    $.path(pin, { fill: c.accent, stroke: \"rgba(0,0,0,0.25)\", sw: 2 * k }), $.circle(px, py - ph * 0.68, pw * 0.2, { fill: \"#FFFFFF\" }));\n  // The card sits on the side with more room.\n  const left = p.x > 0.55;\n  const nf = fitFont($, $.case(p.place), \"display\", 54 * k, A.w * 0.5);\n  const rf = { ...$.font(\"text\"), size: 30 * k };\n  const cf = { ...$.font(\"mono\"), size: 22 * k, ls: 0.06 };\n  const cw = Math.max($.measure($.case(p.place), nf), $.measure(String(p.region || \"\"), rf), $.measure(String(p.coords || \"\"), cf)) + 64 * k;\n  const ch = nf.size * 1.2 + (p.region ? rf.size * 1.35 : 0) + (p.coords ? cf.size * 1.6 : 0) + 44 * k;\n  const cx = left ? Math.max(A.x, px - pw - 30 * k - cw) : Math.min(W - A.x - cw, px + pw + 30 * k);\n  const cy = Math.max(A.y, py - ph - ch * 0.3);\n  let yy = cy + 22 * k;\n  let card = $.card(cx, cy, cw, ch, { shadow: \"lg\" });\n  card += $.text($.case(p.place), { ...nf, x: cx + 32 * k, y: yy, fill: c.fg });\n  yy += nf.size * 1.2;\n  if (p.region) { card += $.text(String(p.region), { ...rf, x: cx + 32 * k, y: yy, fill: c.mute }); yy += rf.size * 1.35; }\n  if (p.coords) card += $.text(String(p.coords), { ...cf, x: cx + 32 * k, y: yy + 4 * k, fill: c.accent });\n  s += M.part(\"card\", { at: tin + 0.55, in: \"pop\", origin: left ? \"right\" : \"left\", out: \"fade\", box: { x: cx, y: cy, w: cw, h: ch } }, card);\n  return s;\n}\n","libs":[]},{"id":"countdown","name":"Countdown","description":"A big number counting down second by second inside a ring that empties, then the closing words pop.","category":"timer","tags":["countdown","timer","start","overlay"],"placement":"overlay","duration":{"default":6,"min":2,"max":120},"props":{"label":{"type":"text","label":"Label","default":"Starting in","maxLength":24},"from":{"type":"number","label":"Count from","default":5,"min":1,"max":99},"end":{"type":"text","label":"Closing words","default":"Let's go","maxLength":16}},"parts":{"ring":"the ring","number":"the number","label":"the label","end":"the closing words"},"beats":{"in":0.1,"out":-0.35},"style":"ember","pack":"broadcast","ref":"broadcast/countdown","code":"function frame(t, $) {\n  const p = $.data, c = $.c, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const k = $.k;\n  const tin = $.beat(\"in\", 0.1);\n  const n = Math.max(1, Math.round(p.from));\n  // The count fills the time before the closing words (the last ~0.9 s).\n  const span = Math.max(0.5, $.dur - tin - 1.2);\n  const step = span / n;\n  const el = t - tin;\n  const left = Math.max(0, n - Math.floor(el / step));\n  const done = el >= span;\n  const cx = W / 2, cy = A.y + A.h * (A.tall ? 0.45 : 0.5);\n  const r = Math.min(A.w, A.h) * 0.24;\n  const tick = el >= 0 && !done ? (el % step) / step : 0;\n  let s = M.part(\"ring\", { at: tin, in: \"grow\", dur: 0.5, out: \"fade\", box: { x: cx - r, y: cy - r, w: 2 * r, h: 2 * r } },\n    $.circle(cx, cy, r, { fill: \"rgba(10,12,16,0.55)\" }),\n    $.ring(cx, cy, r * 0.92, done ? 0 : 1 - (el + 0) / span, { sw: r * 0.09, color: c.accent }));\n  const label = $.case(String(p.label || \"\"));\n  if (label) s += M.text(\"label\", { at: tin + 0.15, in: \"rise\", distance: 16, out: \"fade\", ...fitFont($, label, \"text\", 40 * k, A.w), x: cx, y: cy - r - 76 * k, anchor: \"middle\", fill: \"#FFFFFF\", shadow: \"md\" }, label);\n  if (!done) {\n    // Each number pops in on its second.\n    const pop = 1 + 0.25 * Math.exp(-9 * tick * step) * Math.cos(tick * step * 14);\n    const nf = { ...$.font(\"display\"), size: r * 0.95 };\n    s += M.part(\"number\", { at: tin + 0.1, in: \"fade\", dur: 0.2, out: \"none\", box: { x: cx - r, y: cy - r, w: 2 * r, h: 2 * r } },\n      $.g({ transform: $.tf({ s: pop, ox: cx, oy: cy }) }, $.text(String(left), { ...nf, x: cx, y: cy - nf.size * 0.52, anchor: \"middle\", fill: \"#FFFFFF\" })));\n  } else {\n    const end = $.case(String(p.end || \"\"));\n    const ef = fitFont($, end, \"display\", r * 0.46, r * 1.45);\n    s += M.text(\"end\", { at: tin + span, in: \"slam\", intensity: 0.4, out: \"fade\", ...ef, x: cx, y: cy - ef.size * 0.55, anchor: \"middle\", fill: \"#FFFFFF\" }, end);\n  }\n  return s;\n}\n","libs":[]}]},{"format":1,"id":"social","name":"MONARC Social","version":"1.0.0","description":"Follow cards, notifications, comments, chat threads, posts, polls, milestones, story frames, live chat, avatar groups, now playing and ratings — the social and app UI that videos borrow, in any brand, any aspect.","author":"MONARC STUDIOS","license":"MONARC Packs License 1.0 (free to use in any video; not for resale as templates)","homepage":"https://volito.dev/docs/packs/social","style":"pastel","shared":"shared.js","items":["follow-card","notification","notifications","comments","chat","post","now-playing","rating","poll","milestone","story","live-chat","joined"],"entries":[{"id":"follow-card","name":"Follow card","description":"A profile card: picture or initials, name with a verified check, handle, a follower count that counts up, a line of bio and a Follow button a cursor presses.","category":"social","tags":["social","follow","profile","cta","overlay"],"placement":"overlay","duration":{"default":5,"min":3,"max":30},"props":{"name":{"type":"text","label":"Name","default":"MONARC Studio","maxLength":26},"handle":{"type":"text","label":"Handle","default":"@monarcstudio","maxLength":28},"followers":{"type":"number","label":"Followers","default":248000,"min":0,"max":999999999},"bio":{"type":"text","label":"Bio","default":"Edits that feel directed, not assembled.","maxLength":60},"platform":{"type":"choice","label":"Platform","options":["instagram","tiktok","youtube","x","linkedin","threads","website"],"default":"instagram"},"photo":{"type":"image","label":"Photo"},"verified":{"type":"boolean","label":"Verified","default":true},"action":{"type":"text","label":"Button","default":"Follow","maxLength":14},"done":{"type":"text","label":"Button after","default":"Following","maxLength":14},"position":{"type":"choice","label":"Position","options":["auto","center","bottom-left","bottom-right"],"default":"auto"}},"parts":{"card":"the card","avatar":"the picture","name":"the name and handle","stats":"the follower count","bio":"the bio","button":"the button","cursor":"the cursor"},"beats":{"in":0.15,"out":-0.45},"style":"pastel","pack":"social","ref":"social/follow-card","code":"// MONARC Social — helpers this pack's items share.\n\n/** A platform's icon and how it names its numbers (the platforms' own marks are theirs; these are plain icons). */\nvar PLATFORM = {\n  instagram: { icon: \"camera\", line: \"Instagram\", count: \"followers\" },\n  tiktok: { icon: \"reel\", line: \"TikTok\", count: \"followers\" },\n  youtube: { icon: \"play\", line: \"YouTube\", count: \"subscribers\" },\n  x: { icon: \"x\", line: \"X\", count: \"followers\" },\n  linkedin: { icon: \"users\", line: \"LinkedIn\", count: \"followers\" },\n  threads: { icon: \"chat\", line: \"Threads\", count: \"followers\" },\n  website: { icon: \"globe\", line: \"Website\", count: \"readers\" },\n};\n\n/** A small round check in the accent: verified. */\nfunction verified($, cx, cy, r) {\n  return $.circle(cx, cy, r, { fill: $.c.accent }) + $.path(\"M\" + $.fx(cx - r * 0.45) + \" \" + $.fx(cy + r * 0.02) + \"L\" + $.fx(cx - r * 0.1) + \" \" + $.fx(cy + r * 0.36) + \"L\" + $.fx(cx + r * 0.48) + \" \" + $.fx(cy - r * 0.32), { stroke: $.on($.c.accent), sw: r * 0.28 });\n}\n\n/** A colour for a name, from the brand's two accents and fixed hues — the same name, the same colour. */\nfunction nameColor($, name) {\n  var list = [$.c.accent, $.c.accent2, \"#3DD6FF\", \"#7C5CFF\", \"#22C55E\", \"#F59E0B\"];\n  var h = 0, s = String(name || \"\");\n  for (var i = 0; i < s.length; i++) h = (h * 31 + s.charCodeAt(i)) % 9973;\n  return list[h % list.length];\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const k = $.k * (A.wide ? 1.15 : A.tall ? 1.35 : 1.15);\n  const tin = $.beat(\"in\", 0.15);\n  const pl = PLATFORM[p.platform] || PLATFORM.instagram;\n  const cw = Math.min(A.w, 620 * k), pad = 40 * k, av = 62 * k;\n  const nf = fitFont($, p.name, \"display\", 42 * k, cw - pad * 2 - av * 2 - 70 * k);\n  const hf = { ...$.font(\"mono\"), size: 24 * k };\n  const bf = fitFont($, p.bio, \"text\", 28 * k, cw - pad * 2);\n  const btn = { size: 30 * k };\n  const bh = $.button.size($.case(p.action), btn).h;\n  const ch = pad + av * 2 + 28 * k + 36 * k * 1.4 + (p.bio ? bf.size * 1.5 : 0) + 24 * k + bh + pad;\n  const pos = p.position === \"auto\" ? (A.wide ? \"bottom-right\" : \"center\") : p.position;\n  const x = pos === \"center\" ? (W - cw) / 2 : pos === \"bottom-left\" ? A.x : W - A.x - cw;\n  const y = pos === \"center\" ? A.y + (A.h - ch) * (A.tall ? 0.62 : 0.5) : A.y + A.h - ch;\n  const press = landOn($, p.action, Math.min(tin + 1.5, $.beat(\"out\", $.dur - 0.45) - 0.9), tin);\n  const leave = { out: \"rise\", distance: 40 };\n  let s = M.part(\"card\", Object.assign({ at: tin, in: \"rise\", distance: 70, box: { x: x, y: y, w: cw, h: ch } }, leave), $.card(x, y, cw, ch, { shadow: \"lg\" }));\n  const ax = x + pad + av, ay = y + pad + av;\n  s += M.part(\"avatar\", Object.assign({ at: tin + 0.1, in: \"pop\", box: { x: ax - av, y: ay - av, w: av * 2, h: av * 2 } }, leave), avatar($, \"photo\", ax, ay, av, { name: p.name, fill: c.accent, ring: c.surface }));\n  // The platform's icon sits on the picture's edge.\n  s += M.part(\"avatar\", Object.assign({ at: tin + 0.25, in: \"pop\", box: { x: ax + av * 0.4, y: ay + av * 0.4, w: av * 0.8, h: av * 0.8 } }, leave), $.circle(ax + av * 0.72, ay + av * 0.72, av * 0.34, { fill: c.surface }), $.icon(pl.icon, ax + av * 0.72 - av * 0.2, ay + av * 0.72 - av * 0.2, av * 0.4, { color: c.fg, sw: 2.2 }));\n  const tx = ax + av + 26 * k;\n  const nw = $.measure(p.name, nf);\n  s += M.part(\"name\", Object.assign({ at: tin + 0.2, in: \"rise\", distance: 16, box: { x: tx, y: ay - av * 0.75, w: cw - (tx - x) - pad, h: av * 1.4 } }, leave),\n    $.text(p.name, { ...nf, x: tx, y: ay - nf.size * 0.95, fill: c.fg }),\n    p.verified ? verified($, tx + nw + nf.size * 0.5, ay - nf.size * 0.42, nf.size * 0.36) : \"\",\n    $.text(String(p.handle || \"\"), { ...hf, x: tx, y: ay + 8 * k, fill: c.mute }));\n  let yy = y + pad + av * 2 + 28 * k;\n  // The count on its own row: a big number and what it counts.\n  const count = $.counter(t, tin + 0.4, tin + 1.6, p.followers, { short: true });\n  const cf = { ...$.font(\"display\"), size: 36 * k };\n  s += M.part(\"stats\", Object.assign({ at: tin + 0.35, in: \"fade\", dur: 0.3, box: { x: x + pad, y: yy, w: cw - pad * 2, h: cf.size * 1.2 } }, leave),\n    $.text(count, { ...cf, x: x + pad, y: yy, fill: c.fg }),\n    $.text(pl.count, { ...$.font(\"text\"), size: 26 * k, x: x + pad + $.measure($.count(p.followers), cf) + 14 * k, y: yy + cf.size * 0.22, fill: c.mute }));\n  yy += cf.size * 1.4;\n  if (p.bio) {\n    s += M.text(\"bio\", Object.assign({ at: tin + 0.45, in: \"fade\", dur: 0.35 }, leave, bf, { x: x + pad, y: yy, fill: c.fg }), String(p.bio));\n    yy += bf.size * 1.5;\n  }\n  yy += 24 * k;\n  s += M.part(\"button\", Object.assign({ at: tin + 0.55, in: \"rise\", distance: 14, box: { x: x + pad, y: yy, w: cw - pad * 2, h: bh } }, leave),\n    $.button(t, $.case(p.action), x + cw / 2, yy, { size: 30 * k, anchor: \"middle\", pressAt: press, done: $.case(p.done) }));\n  if (t > press - 0.8 && t < press + 0.9) {\n    const g = $.E.inOut3($.seg(t, press - 0.75, press - 0.04));\n    const px = $.lerp(x + cw + 160 * k, x + cw * 0.55, g), py = $.lerp(Math.min(H, y + ch + 200 * k), yy + bh * 0.6, g);\n    s += M.part(\"cursor\", { at: press - 0.8, in: \"fade\", dur: 0.2, outAt: press + 0.6, out: \"fade\", outDur: 0.3 }, $.cursor(px, py, { press: $.bump(t, press, 0.05, 0.2), scale: 2 * k }));\n  }\n  return s;\n}\n","libs":[]},{"id":"notification","name":"Notification","description":"A phone or desktop notification slides in: the app's icon and name, the time, a bold title and the message.","category":"social","tags":["notification","phone","ui","overlay","alert"],"placement":"overlay","duration":{"default":4.5,"min":2,"max":30},"props":{"app":{"type":"text","label":"App","default":"MONARC","maxLength":18},"icon":{"type":"icon","label":"Icon","default":"sparkle"},"title":{"type":"text","label":"Title","default":"Your cut is ready","maxLength":40},"message":{"type":"text","label":"Message","default":"Three versions exported in 4K. Tap to review before you post.","maxLength":110},"time":{"type":"text","label":"Time","default":"now","maxLength":10},"look":{"type":"choice","label":"Look","options":["phone","desktop"],"default":"phone"}},"parts":{"banner":"the notification"},"beats":{"in":0.15,"out":-0.45},"style":"pastel","pack":"social","ref":"social/notification","code":"// MONARC Social — helpers this pack's items share.\n\n/** A platform's icon and how it names its numbers (the platforms' own marks are theirs; these are plain icons). */\nvar PLATFORM = {\n  instagram: { icon: \"camera\", line: \"Instagram\", count: \"followers\" },\n  tiktok: { icon: \"reel\", line: \"TikTok\", count: \"followers\" },\n  youtube: { icon: \"play\", line: \"YouTube\", count: \"subscribers\" },\n  x: { icon: \"x\", line: \"X\", count: \"followers\" },\n  linkedin: { icon: \"users\", line: \"LinkedIn\", count: \"followers\" },\n  threads: { icon: \"chat\", line: \"Threads\", count: \"followers\" },\n  website: { icon: \"globe\", line: \"Website\", count: \"readers\" },\n};\n\n/** A small round check in the accent: verified. */\nfunction verified($, cx, cy, r) {\n  return $.circle(cx, cy, r, { fill: $.c.accent }) + $.path(\"M\" + $.fx(cx - r * 0.45) + \" \" + $.fx(cy + r * 0.02) + \"L\" + $.fx(cx - r * 0.1) + \" \" + $.fx(cy + r * 0.36) + \"L\" + $.fx(cx + r * 0.48) + \" \" + $.fx(cy - r * 0.32), { stroke: $.on($.c.accent), sw: r * 0.28 });\n}\n\n/** A colour for a name, from the brand's two accents and fixed hues — the same name, the same colour. */\nfunction nameColor($, name) {\n  var list = [$.c.accent, $.c.accent2, \"#3DD6FF\", \"#7C5CFF\", \"#22C55E\", \"#F59E0B\"];\n  var h = 0, s = String(name || \"\");\n  for (var i = 0; i < s.length; i++) h = (h * 31 + s.charCodeAt(i)) % 9973;\n  return list[h % list.length];\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const k = $.k * (A.tall ? 1.3 : 1.15);\n  const tin = landOn($, p.title, $.beat(\"in\", 0.15));\n  const desk = p.look === \"desktop\";\n  const w = desk ? Math.min(A.w * 0.42, 720 * k) : Math.min(A.w, A.wide ? 760 * k : A.w);\n  const pad = 26 * k, ic = 64 * k;\n  const af = { ...$.font(\"text\"), size: 22 * k, ls: 0.06 };\n  const tf = fitFont($, p.title, \"display\", 32 * k, w - pad * 2 - ic - 20 * k);\n  const mf = { ...$.font(\"text\"), size: 27 * k, lh: 1.3 };\n  const lines = $.wrap(String(p.message || \"\"), w - pad * 2 - ic - 20 * k, mf).slice(0, 3);\n  const h = pad * 2 + af.size * 1.6 + tf.size * 1.25 + lines.length * mf.size * 1.3;\n  const x = desk ? W - A.x - w : (W - w) / 2;\n  const y = desk ? A.y : A.y * (A.tall ? 0.55 : 0.8);\n  let b = $.rect(x, y, w, h, { r: (desk ? 18 : 34) * k, fill: c.surface, stroke: c.line, sw: 1.5 * k, shadow: \"lg\" });\n  b += appIcon($, p.icon, x + pad, y + pad, ic, { fill: c.accent });\n  const tx = x + pad + ic + 20 * k;\n  b += $.text(String(p.app || \"\").toUpperCase(), { ...af, x: tx, y: y + pad - 2 * k, fill: c.mute });\n  b += $.text(String(p.time || \"\"), { ...af, x: x + w - pad, y: y + pad - 2 * k, anchor: \"end\", fill: c.mute });\n  let yy = y + pad + af.size * 1.6;\n  b += $.text(String(p.title), { ...tf, x: tx, y: yy, fill: c.fg });\n  yy += tf.size * 1.25;\n  lines.forEach(function (l, i) { b += $.text(l, { ...mf, x: tx, y: yy + i * mf.size * 1.3, fill: c.fg, op: 0.82 }); });\n  return M.part(\"banner\", { at: tin, in: desk ? \"slide\" : \"drop\", from: desk ? \"right\" : \"up\", distance: desk ? 60 : 90, ease: \"bouncy\", out: desk ? \"slide\" : \"drop\", outFrom: desk ? \"right\" : \"up\", box: { x: x, y: y, w: w, h: h } }, b);\n}\n","libs":[]},{"id":"notifications","name":"Notification stack","description":"Notifications land one after another and stack up, each new one pushing the pile down.","category":"social","tags":["notification","stack","cascade","ui","overlay"],"placement":"overlay","duration":{"default":6,"min":3,"max":30},"props":{"items":{"type":"list","label":"Notifications (App: message)","default":[{"app":"Sales","text":"New order — $1,240"},{"app":"Mail","text":"The client signed off on the cut"},{"app":"Calendar","text":"Launch call moved to 3 pm"},{"app":"Bank","text":"Payment received from Northwind"}],"maxItems":6,"minItems":1,"of":{"app":"text","text":"text"}},"gap":{"type":"number","label":"Seconds between","default":0.75,"min":0.2,"max":5},"icon":{"type":"icon","label":"Icon","default":"bell"}},"parts":{"items":"each notification"},"beats":{"in":0.2,"out":-0.45},"style":"pastel","pack":"social","ref":"social/notifications","code":"// MONARC Social — helpers this pack's items share.\n\n/** A platform's icon and how it names its numbers (the platforms' own marks are theirs; these are plain icons). */\nvar PLATFORM = {\n  instagram: { icon: \"camera\", line: \"Instagram\", count: \"followers\" },\n  tiktok: { icon: \"reel\", line: \"TikTok\", count: \"followers\" },\n  youtube: { icon: \"play\", line: \"YouTube\", count: \"subscribers\" },\n  x: { icon: \"x\", line: \"X\", count: \"followers\" },\n  linkedin: { icon: \"users\", line: \"LinkedIn\", count: \"followers\" },\n  threads: { icon: \"chat\", line: \"Threads\", count: \"followers\" },\n  website: { icon: \"globe\", line: \"Website\", count: \"readers\" },\n};\n\n/** A small round check in the accent: verified. */\nfunction verified($, cx, cy, r) {\n  return $.circle(cx, cy, r, { fill: $.c.accent }) + $.path(\"M\" + $.fx(cx - r * 0.45) + \" \" + $.fx(cy + r * 0.02) + \"L\" + $.fx(cx - r * 0.1) + \" \" + $.fx(cy + r * 0.36) + \"L\" + $.fx(cx + r * 0.48) + \" \" + $.fx(cy - r * 0.32), { stroke: $.on($.c.accent), sw: r * 0.28 });\n}\n\n/** A colour for a name, from the brand's two accents and fixed hues — the same name, the same colour. */\nfunction nameColor($, name) {\n  var list = [$.c.accent, $.c.accent2, \"#3DD6FF\", \"#7C5CFF\", \"#22C55E\", \"#F59E0B\"];\n  var h = 0, s = String(name || \"\");\n  for (var i = 0; i < s.length; i++) h = (h * 31 + s.charCodeAt(i)) % 9973;\n  return list[h % list.length];\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const k = $.k * (A.tall ? 1.25 : 1.1);\n  const tin = $.beat(\"in\", 0.2);\n  const rows = p.items || [];\n  const w = Math.min(A.w, A.wide ? 720 * k : A.w);\n  const x = A.wide ? W - A.x - w : (W - w) / 2;\n  const pad = 22 * k, ic = 52 * k, h = 112 * k, gap = 16 * k;\n  const af = { ...$.font(\"text\"), size: 21 * k, ls: 0.06 };\n  let s = \"\";\n  rows.forEach(function (r, i) {\n    const at = tin + i * p.gap;\n    // Later arrivals push this one down by a row each, on a spring.\n    let push = 0;\n    for (let j = i + 1; j < rows.length; j++) push += $.spring(Math.max(0, t - (tin + j * p.gap)), 2.4, 8);\n    const y = A.y + push * (h + gap);\n    const tf = fitFont($, String(r.text), \"display\", 30 * k, w - pad * 2 - ic - 20 * k);\n    let b = $.rect(x, y, w, h, { r: 26 * k, fill: c.surface, stroke: c.line, sw: 1.5 * k, shadow: \"md\" });\n    b += appIcon($, p.icon, x + pad, y + (h - ic) / 2, ic, { fill: nameColor($, r.app) });\n    b += $.text(String(r.app).toUpperCase(), { ...af, x: x + pad + ic + 18 * k, y: y + pad - 2 * k, fill: c.mute });\n    b += $.text(\"now\", { ...af, x: x + w - pad, y: y + pad - 2 * k, anchor: \"end\", fill: c.mute });\n    b += $.text(String(r.text), { ...tf, x: x + pad + ic + 18 * k, y: y + pad + af.size * 1.45, fill: c.fg });\n    s = M.part(\"items\", { at: at, in: \"drop\", distance: 70, ease: \"bouncy\", out: \"fade\", box: { x: x, y: y, w: w, h: h } }, b) + s;\n  });\n  return s;\n}\n","libs":[]},{"id":"comments","name":"Comments","description":"Comment cards appear one by one: a picture or initials, the name, the time, the words typing on and a like count.","category":"social","tags":["comments","social","replies","typing","overlay"],"placement":"overlay","duration":{"default":7,"min":3,"max":30},"props":{"items":{"type":"list","label":"Comments (Name: words)","default":[{"name":"maya.edits","text":"This transition is unreal. How long did it take?"},{"name":"jordan_k","text":"Saving this for my next edit"},{"name":"alex.rivera","text":"The pacing in the first 3 seconds 🔥"}],"maxItems":4,"minItems":1,"of":{"name":"text","text":"text"}},"likes":{"type":"number","label":"Likes on the first","default":1240,"min":0,"max":9999999},"gap":{"type":"number","label":"Seconds between","default":1.6,"min":0.4,"max":6}},"parts":{"items":"each comment"},"beats":{"in":0.2,"out":-0.45},"style":"pastel","pack":"social","ref":"social/comments","code":"// MONARC Social — helpers this pack's items share.\n\n/** A platform's icon and how it names its numbers (the platforms' own marks are theirs; these are plain icons). */\nvar PLATFORM = {\n  instagram: { icon: \"camera\", line: \"Instagram\", count: \"followers\" },\n  tiktok: { icon: \"reel\", line: \"TikTok\", count: \"followers\" },\n  youtube: { icon: \"play\", line: \"YouTube\", count: \"subscribers\" },\n  x: { icon: \"x\", line: \"X\", count: \"followers\" },\n  linkedin: { icon: \"users\", line: \"LinkedIn\", count: \"followers\" },\n  threads: { icon: \"chat\", line: \"Threads\", count: \"followers\" },\n  website: { icon: \"globe\", line: \"Website\", count: \"readers\" },\n};\n\n/** A small round check in the accent: verified. */\nfunction verified($, cx, cy, r) {\n  return $.circle(cx, cy, r, { fill: $.c.accent }) + $.path(\"M\" + $.fx(cx - r * 0.45) + \" \" + $.fx(cy + r * 0.02) + \"L\" + $.fx(cx - r * 0.1) + \" \" + $.fx(cy + r * 0.36) + \"L\" + $.fx(cx + r * 0.48) + \" \" + $.fx(cy - r * 0.32), { stroke: $.on($.c.accent), sw: r * 0.28 });\n}\n\n/** A colour for a name, from the brand's two accents and fixed hues — the same name, the same colour. */\nfunction nameColor($, name) {\n  var list = [$.c.accent, $.c.accent2, \"#3DD6FF\", \"#7C5CFF\", \"#22C55E\", \"#F59E0B\"];\n  var h = 0, s = String(name || \"\");\n  for (var i = 0; i < s.length; i++) h = (h * 31 + s.charCodeAt(i)) % 9973;\n  return list[h % list.length];\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const k = $.k * (A.tall ? 1.3 : 1.12);\n  const tin = $.beat(\"in\", 0.2);\n  const rows = p.items || [];\n  const w = Math.min(A.w, A.wide ? 700 * k : A.w);\n  const pad = 24 * k, av = 30 * k;\n  const nf = { ...$.font(\"display\"), size: 26 * k };\n  const tf = { ...$.font(\"text\"), size: 28 * k, lh: 1.3 };\n  const meta = { ...$.font(\"text\"), size: 22 * k };\n  // Heights first, so the stack can sit at the bottom of the safe area.\n  const cards = rows.map(function (r, i) {\n    const lines = $.wrap(String(r.text), w - pad * 2 - av * 2 - 20 * k, tf).slice(0, 3);\n    return { r: r, lines: lines, h: pad * 2 + nf.size * 1.3 + lines.length * tf.size * 1.3 + meta.size * 1.4, at: tin + i * p.gap };\n  });\n  const gap = 16 * k;\n  const total = cards.reduce(function (a, c2) { return a + c2.h; }, 0) + gap * Math.max(0, cards.length - 1);\n  const x = A.wide ? A.x : (W - w) / 2;\n  let y = A.wide ? A.y + A.h - total : A.y + A.h * 0.55 - total / 2;\n  let s = \"\";\n  cards.forEach(function (cd, i) {\n    const r = cd.r;\n    let b = $.rect(x, y, w, cd.h, { r: 24 * k, fill: c.surface, stroke: c.line, sw: 1.5 * k, shadow: \"md\" });\n    b += avatar($, null, x + pad + av, y + pad + av, av, { name: r.name, fill: nameColor($, r.name) });\n    const tx = x + pad + av * 2 + 18 * k;\n    b += $.text(String(r.name), { ...nf, x: tx, y: y + pad, fill: c.fg });\n    b += $.text((i + 1) * 2 + \"m\", { ...meta, x: tx + $.measure(String(r.name), nf) + 14 * k, y: y + pad + 3 * k, fill: c.mute });\n    // The words type on, line by line.\n    let left = Math.max(0, Math.floor((t - cd.at - 0.25) * 42));\n    cd.lines.forEach(function (l, j) {\n      const n = Math.min(Array.from(l).length, left);\n      left -= n;\n      if (n > 0) b += $.text(Array.from(l).slice(0, n).join(\"\"), { ...tf, x: tx, y: y + pad + nf.size * 1.35 + j * tf.size * 1.3, fill: c.fg });\n    });\n    const ly = y + cd.h - pad - meta.size * 0.9;\n    const likes = i === 0 ? $.count(Math.round(p.likes * $.E.out3($.seg(t, cd.at + 0.6, cd.at + 1.6)))) : String(Math.round(p.likes / (6 + i * 5)));\n    b += $.icon(\"heart\", tx, ly - 2 * k, meta.size * 1.05, { color: i === 0 && t > cd.at + 0.6 ? c.red : c.mute, fill: i === 0 && t > cd.at + 0.6 });\n    b += $.text(likes, { ...meta, x: tx + meta.size * 1.5, y: ly, fill: c.mute });\n    b += $.text(\"Reply\", { ...meta, x: tx + meta.size * 5.5, y: ly, fill: c.mute });\n    s += M.part(\"items\", { at: cd.at, in: \"rise\", distance: 40, ease: \"bouncy\", out: \"fade\", box: { x: x, y: y, w: w, h: cd.h } }, b);\n    y += cd.h + gap;\n  });\n  return s;\n}\n","libs":[]},{"id":"chat","name":"Chat thread","description":"A message thread plays out: their bubbles arrive after a typing indicator, yours on the right in the accent.","category":"social","tags":["chat","messages","dm","texting","ui"],"placement":"overlay","duration":{"default":8,"min":3,"max":60},"props":{"contact":{"type":"text","label":"Contact","default":"Sam · Producer","maxLength":28},"messages":{"type":"list","label":"Messages (me: … / them: …)","default":[{"who":"them","text":"Did the new cut land?"},{"who":"me","text":"Rendering now — 3 versions"},{"who":"them","text":"Send the vertical one first"},{"who":"me","text":"On its way 🚀"}],"maxItems":8,"minItems":1,"of":{"who":"text","text":"text"}},"gap":{"type":"number","label":"Seconds between","default":1.4,"min":0.4,"max":6}},"parts":{"header":"the contact bar","messages":"each bubble","typing":"the typing dots"},"beats":{"in":0.2,"out":-0.45},"style":"pastel","pack":"social","ref":"social/chat","code":"// MONARC Social — helpers this pack's items share.\n\n/** A platform's icon and how it names its numbers (the platforms' own marks are theirs; these are plain icons). */\nvar PLATFORM = {\n  instagram: { icon: \"camera\", line: \"Instagram\", count: \"followers\" },\n  tiktok: { icon: \"reel\", line: \"TikTok\", count: \"followers\" },\n  youtube: { icon: \"play\", line: \"YouTube\", count: \"subscribers\" },\n  x: { icon: \"x\", line: \"X\", count: \"followers\" },\n  linkedin: { icon: \"users\", line: \"LinkedIn\", count: \"followers\" },\n  threads: { icon: \"chat\", line: \"Threads\", count: \"followers\" },\n  website: { icon: \"globe\", line: \"Website\", count: \"readers\" },\n};\n\n/** A small round check in the accent: verified. */\nfunction verified($, cx, cy, r) {\n  return $.circle(cx, cy, r, { fill: $.c.accent }) + $.path(\"M\" + $.fx(cx - r * 0.45) + \" \" + $.fx(cy + r * 0.02) + \"L\" + $.fx(cx - r * 0.1) + \" \" + $.fx(cy + r * 0.36) + \"L\" + $.fx(cx + r * 0.48) + \" \" + $.fx(cy - r * 0.32), { stroke: $.on($.c.accent), sw: r * 0.28 });\n}\n\n/** A colour for a name, from the brand's two accents and fixed hues — the same name, the same colour. */\nfunction nameColor($, name) {\n  var list = [$.c.accent, $.c.accent2, \"#3DD6FF\", \"#7C5CFF\", \"#22C55E\", \"#F59E0B\"];\n  var h = 0, s = String(name || \"\");\n  for (var i = 0; i < s.length; i++) h = (h * 31 + s.charCodeAt(i)) % 9973;\n  return list[h % list.length];\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const k = $.k * (A.tall ? 1.3 : 1.1);\n  const tin = $.beat(\"in\", 0.2);\n  const msgs = p.messages || [];\n  const w = Math.min(A.w, A.wide ? 760 * k : A.w);\n  const x = (W - w) / 2;\n  const tf = { ...$.font(\"text\"), size: 30 * k, lh: 1.28 };\n  const bp = 22 * k, maxB = w * 0.74, gap = 14 * k;\n  // Each bubble's lines and time: theirs wait 0.7 s behind typing dots.\n  let at = tin + 0.5;\n  const bubbles = msgs.map(function (m) {\n    const mine = /^me$/i.test(String(m.who).trim());\n    const lines = $.wrap(String(m.text), maxB - bp * 2, tf).slice(0, 4);\n    const bw = Math.max.apply(null, lines.map(function (l) { return $.measure(l, tf); })) + bp * 2;\n    const bh = lines.length * tf.size * 1.28 + bp * 1.4;\n    const b = { mine: mine, lines: lines, w: bw, h: bh, typing: mine ? null : at, at: mine ? at : at + 0.7 };\n    at = b.at + p.gap;\n    return b;\n  });\n  const hh = 96 * k;\n  // The thread scrolls up when it outgrows the space under the header.\n  const room = A.h - hh - 20 * k;\n  let used = 0;\n  const shown = bubbles.filter(function (b) { return t >= b.at; });\n  shown.forEach(function (b) { used += b.h + gap; });\n  const typingNow = bubbles.some(function (b) { return b.typing !== null && t >= b.typing && t < b.at; });\n  if (typingNow) used += 70 * k + gap;\n  const scroll = Math.max(0, used - room);\n  const top = A.y + hh + 20 * k;\n  let s = M.part(\"header\", { at: tin, in: \"drop\", distance: 30, out: \"fade\", vary: false, box: { x: x, y: A.y, w: w, h: hh } },\n    $.rect(x, A.y, w, hh, { r: 24 * k, fill: c.surface, stroke: c.line, sw: 1.5 * k, shadow: \"md\" }),\n    avatar($, null, x + 30 * k + 28 * k, A.y + hh / 2, 28 * k, { name: p.contact, fill: c.accent }),\n    $.text(String(p.contact), { ...$.font(\"display\"), size: 30 * k, x: x + 30 * k + 72 * k, y: A.y + hh / 2 - 17 * k, fill: c.fg }));\n  let y = top - scroll;\n  let body = \"\";\n  bubbles.forEach(function (b, i) {\n    if (b.typing !== null && t >= b.typing && t < b.at) {\n      const dx = x, dw = 120 * k, dh = 66 * k;\n      let dots = $.rect(dx, y, dw, dh, { r: dh / 2, fill: c.surface, stroke: c.line, sw: 1.5 * k });\n      for (let d = 0; d < 3; d++) dots += $.circle(dx + dw * (0.3 + d * 0.2), y + dh / 2 - 6 * k * Math.max(0, Math.sin((t - b.typing) * 9 - d * 0.9)), 8 * k, { fill: c.mute });\n      body += M.part(\"typing\", { at: b.typing, in: \"pop\", origin: \"left\", out: \"none\", box: { x: dx, y: y, w: dw, h: dh } }, dots);\n    }\n    if (t < b.at) return;\n    const bx = b.mine ? x + w - b.w : x;\n    let bub = $.rect(bx, y, b.w, b.h, { r: 28 * k, fill: b.mine ? c.accent : c.surface, stroke: b.mine ? \"none\" : c.line, sw: 1.5 * k });\n    b.lines.forEach(function (l, j) { bub += $.text(l, { ...tf, x: bx + bp, y: y + bp * 0.7 + j * tf.size * 1.28, fill: b.mine ? $.on(c.accent) : c.fg }); });\n    body += M.part(\"messages\", { at: b.at, in: \"pop\", origin: b.mine ? \"bottom-right\" : \"bottom-left\", ease: \"bouncy\", out: \"fade\", box: { x: bx, y: y, w: b.w, h: b.h } }, bub);\n    y += b.h + gap;\n  });\n  return s + $.clip(x - 20 * k, top, w + 40 * k, A.y + A.h - top, 0, body);\n}\n","libs":[]},{"id":"post","name":"Social post","description":"A post card: picture or initials, name, handle and check, the words, and likes, reposts and replies counting up as a heart pops.","category":"social","tags":["post","tweet","social","quote","card"],"placement":"overlay","duration":{"default":6,"min":3,"max":30},"props":{"name":{"type":"text","label":"Name","default":"Maya Chen","maxLength":26},"handle":{"type":"text","label":"Handle","default":"@mayaedits","maxLength":26},"text":{"type":"longtext","label":"Post","default":"Stopped editing timelines by hand this year. I describe the cut, the agent builds it, I direct. Shipping three times as much.","maxLength":280},"photo":{"type":"image","label":"Photo"},"verified":{"type":"boolean","label":"Verified","default":true},"likes":{"type":"number","label":"Likes","default":18400,"min":0,"max":999999999},"reposts":{"type":"number","label":"Reposts","default":2310,"min":0,"max":999999999},"replies":{"type":"number","label":"Replies","default":412,"min":0,"max":999999999},"time":{"type":"text","label":"Time","default":"2h","maxLength":12}},"parts":{"card":"the card","author":"the name line","text":"the words","stats":"the counts","heart":"the heart"},"beats":{"in":0.15,"out":-0.45},"style":"pastel","pack":"social","ref":"social/post","code":"// MONARC Social — helpers this pack's items share.\n\n/** A platform's icon and how it names its numbers (the platforms' own marks are theirs; these are plain icons). */\nvar PLATFORM = {\n  instagram: { icon: \"camera\", line: \"Instagram\", count: \"followers\" },\n  tiktok: { icon: \"reel\", line: \"TikTok\", count: \"followers\" },\n  youtube: { icon: \"play\", line: \"YouTube\", count: \"subscribers\" },\n  x: { icon: \"x\", line: \"X\", count: \"followers\" },\n  linkedin: { icon: \"users\", line: \"LinkedIn\", count: \"followers\" },\n  threads: { icon: \"chat\", line: \"Threads\", count: \"followers\" },\n  website: { icon: \"globe\", line: \"Website\", count: \"readers\" },\n};\n\n/** A small round check in the accent: verified. */\nfunction verified($, cx, cy, r) {\n  return $.circle(cx, cy, r, { fill: $.c.accent }) + $.path(\"M\" + $.fx(cx - r * 0.45) + \" \" + $.fx(cy + r * 0.02) + \"L\" + $.fx(cx - r * 0.1) + \" \" + $.fx(cy + r * 0.36) + \"L\" + $.fx(cx + r * 0.48) + \" \" + $.fx(cy - r * 0.32), { stroke: $.on($.c.accent), sw: r * 0.28 });\n}\n\n/** A colour for a name, from the brand's two accents and fixed hues — the same name, the same colour. */\nfunction nameColor($, name) {\n  var list = [$.c.accent, $.c.accent2, \"#3DD6FF\", \"#7C5CFF\", \"#22C55E\", \"#F59E0B\"];\n  var h = 0, s = String(name || \"\");\n  for (var i = 0; i < s.length; i++) h = (h * 31 + s.charCodeAt(i)) % 9973;\n  return list[h % list.length];\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const k = $.k * (A.tall ? 1.3 : 1.1);\n  const tin = $.beat(\"in\", 0.15);\n  const cw = Math.min(A.w, A.wide ? 900 * k : A.w), pad = 40 * k, av = 40 * k;\n  const nf = fitFont($, p.name, \"display\", 32 * k, cw * 0.5);\n  const mf = { ...$.font(\"text\"), size: 26 * k };\n  const tf = { ...$.font(\"text\"), size: 36 * k, lh: 1.32 };\n  const lines = $.wrap(String(p.text || \"\"), cw - pad * 2, tf).slice(0, 7);\n  const ch = pad + av * 2 + 24 * k + lines.length * tf.size * 1.32 + 30 * k + 40 * k + pad;\n  const x = (W - cw) / 2, y = A.y + (A.h - ch) * (A.tall ? 0.48 : 0.5);\n  const leave = { out: \"rise\", distance: 40 };\n  let s = M.part(\"card\", Object.assign({ at: tin, in: \"rise\", distance: 60, box: { x: x, y: y, w: cw, h: ch } }, leave), $.card(x, y, cw, ch, { shadow: \"lg\" }));\n  const ay = y + pad + av;\n  const tx = x + pad + av * 2 + 20 * k;\n  const nw = $.measure(String(p.name), nf);\n  s += M.part(\"author\", Object.assign({ at: tin + 0.12, in: \"fade\", dur: 0.3, box: { x: x + pad, y: y + pad, w: cw - pad * 2, h: av * 2 } }, leave),\n    avatar($, \"photo\", x + pad + av, ay, av, { name: p.name, fill: c.accent }),\n    $.text(String(p.name), { ...nf, x: tx, y: ay - nf.size * 1.0, fill: c.fg }),\n    p.verified ? verified($, tx + nw + nf.size * 0.55, ay - nf.size * 0.45, nf.size * 0.38) : \"\",\n    $.text(String(p.handle || \"\") + (p.time ? \" · \" + p.time : \"\"), { ...mf, x: tx, y: ay + 6 * k, fill: c.mute }));\n  s += M.text(\"text\", Object.assign({ at: tin + 0.3, in: \"rise\", distance: 16, split: \"lines\", stagger: 0.08 }, leave, tf, { x: x + pad, y: y + pad + av * 2 + 24 * k, fill: c.fg, maxW: cw - pad * 2 }), String(p.text || \"\"));\n  // Counts climb after the words land; the heart pops red as it starts.\n  const sy = y + ch - pad - 30 * k, ts = tin + 0.6 + lines.length * 0.08;\n  const cnt = function (n) { return $.count(Math.round(n * $.E.out3($.seg(t, ts, ts + 1.6)))); };\n  const col = (cw - pad * 2) / 3;\n  const statsRow = [[\"chat\", cnt(p.replies)], [\"loop\", cnt(p.reposts)], [\"heart\", cnt(p.likes)]];\n  let row = \"\";\n  statsRow.forEach(function (st, i) {\n    const sx = x + pad + i * col;\n    if (st[0] !== \"heart\") row += $.icon(st[0], sx, sy - 2 * k, 32 * k, { color: c.mute });\n    row += $.text(st[1], { ...mf, x: sx + 46 * k, y: sy, fill: st[0] === \"heart\" && t > ts ? c.red : c.mute });\n  });\n  s += M.part(\"stats\", Object.assign({ at: ts - 0.2, in: \"fade\", dur: 0.3, box: { x: x + pad, y: sy - 10 * k, w: cw - pad * 2, h: 50 * k } }, leave), row);\n  const hx = x + pad + 2 * col;\n  const liked = t > ts;\n  s += M.part(\"heart\", Object.assign({ at: ts - 0.2, in: \"fade\", dur: 0.2, emphasis: [{ fx: \"pop\", at: ts }], box: { x: hx, y: sy - 4 * k, w: 34 * k, h: 34 * k } }, leave), $.icon(\"heart\", hx, sy - 2 * k, 32 * k, { color: liked ? c.red : c.mute, fill: liked }));\n  return s;\n}\n","libs":[]},{"id":"now-playing","name":"Now playing","description":"A music card: the cover (or a generated one), the title and artist, a progress bar moving with the clip and an equaliser.","category":"social","tags":["music","now playing","song","audio","card"],"placement":"overlay","duration":{"default":6,"min":2,"max":60},"props":{"title":{"type":"text","label":"Song","default":"Midnight Edit","maxLength":32},"artist":{"type":"text","label":"Artist","default":"The Long Cut","maxLength":32},"cover":{"type":"image","label":"Cover"},"length":{"type":"text","label":"Length (m:ss)","default":"3:42","maxLength":6},"start":{"type":"number","label":"Starts at (0–1)","default":0.34,"min":0,"max":1},"position":{"type":"choice","label":"Position","options":["auto","center","bottom-left","bottom-right"],"default":"auto"}},"parts":{"card":"the card","cover":"the cover","title":"the song and artist","progress":"the progress bar","eq":"the equaliser"},"beats":{"in":0.15,"out":-0.45},"style":"pastel","pack":"social","ref":"social/now-playing","code":"// MONARC Social — helpers this pack's items share.\n\n/** A platform's icon and how it names its numbers (the platforms' own marks are theirs; these are plain icons). */\nvar PLATFORM = {\n  instagram: { icon: \"camera\", line: \"Instagram\", count: \"followers\" },\n  tiktok: { icon: \"reel\", line: \"TikTok\", count: \"followers\" },\n  youtube: { icon: \"play\", line: \"YouTube\", count: \"subscribers\" },\n  x: { icon: \"x\", line: \"X\", count: \"followers\" },\n  linkedin: { icon: \"users\", line: \"LinkedIn\", count: \"followers\" },\n  threads: { icon: \"chat\", line: \"Threads\", count: \"followers\" },\n  website: { icon: \"globe\", line: \"Website\", count: \"readers\" },\n};\n\n/** A small round check in the accent: verified. */\nfunction verified($, cx, cy, r) {\n  return $.circle(cx, cy, r, { fill: $.c.accent }) + $.path(\"M\" + $.fx(cx - r * 0.45) + \" \" + $.fx(cy + r * 0.02) + \"L\" + $.fx(cx - r * 0.1) + \" \" + $.fx(cy + r * 0.36) + \"L\" + $.fx(cx + r * 0.48) + \" \" + $.fx(cy - r * 0.32), { stroke: $.on($.c.accent), sw: r * 0.28 });\n}\n\n/** A colour for a name, from the brand's two accents and fixed hues — the same name, the same colour. */\nfunction nameColor($, name) {\n  var list = [$.c.accent, $.c.accent2, \"#3DD6FF\", \"#7C5CFF\", \"#22C55E\", \"#F59E0B\"];\n  var h = 0, s = String(name || \"\");\n  for (var i = 0; i < s.length; i++) h = (h * 31 + s.charCodeAt(i)) % 9973;\n  return list[h % list.length];\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const k = $.k * (A.tall ? 1.3 : 1.1);\n  const tin = $.beat(\"in\", 0.15);\n  const cw = Math.min(A.w, 680 * k), pad = 28 * k, art = 150 * k;\n  const ch = art + pad * 2 + 70 * k;\n  const pos = p.position === \"auto\" ? (A.wide ? \"bottom-left\" : \"center\") : p.position;\n  const x = pos === \"center\" ? (W - cw) / 2 : pos === \"bottom-left\" ? A.x : W - A.x - cw;\n  const y = pos === \"center\" ? A.y + (A.h - ch) * 0.6 : A.y + A.h - ch;\n  const leave = { out: \"slide\", outFrom: pos === \"bottom-right\" ? \"right\" : \"left\", distance: 40 };\n  let s = M.part(\"card\", Object.assign({ at: tin, in: \"slide\", from: pos === \"bottom-right\" ? \"right\" : \"left\", distance: 60, box: { x: x, y: y, w: cw, h: ch } }, leave), $.card(x, y, cw, ch, { shadow: \"lg\" }));\n  // The cover: the picture, or a gradient in the brand's accents with a disc on it.\n  let cover;\n  if ($.media.cover) cover = $.image(\"cover\", x + pad, y + pad, art, art, { r: 18 * k });\n  else {\n    const gid = $.uid(\"np\");\n    $.def('<linearGradient id=\"' + gid + '\" x1=\"0\" y1=\"0\" x2=\"1\" y2=\"1\"><stop offset=\"0\" stop-color=\"' + c.accent + '\"/><stop offset=\"1\" stop-color=\"' + c.accent2 + '\"/></linearGradient>');\n    cover = $.rect(x + pad, y + pad, art, art, { r: 18 * k, fill: \"url(#\" + gid + \")\" }) + $.circle(x + pad + art / 2, y + pad + art / 2, art * 0.3, { fill: \"rgba(0,0,0,0.25)\" }) + $.circle(x + pad + art / 2, y + pad + art / 2, art * 0.06, { fill: \"#FFFFFF\", op: 0.8 });\n  }\n  s += M.part(\"cover\", Object.assign({ at: tin + 0.1, in: \"pop\", box: { x: x + pad, y: y + pad, w: art, h: art } }, leave), cover);\n  const tx = x + pad + art + 26 * k;\n  const tw = cw - (tx - x) - pad;\n  const nf = fitFont($, p.title, \"display\", 38 * k, tw - 80 * k);\n  const af = fitFont($, p.artist, \"text\", 28 * k, tw);\n  s += M.part(\"title\", Object.assign({ at: tin + 0.2, in: \"rise\", distance: 14, box: { x: tx, y: y + pad, w: tw, h: art * 0.7 } }, leave),\n    $.text(String(p.title), { ...nf, x: tx, y: y + pad + 10 * k, fill: c.fg }),\n    $.text(String(p.artist), { ...af, x: tx, y: y + pad + 10 * k + nf.size * 1.25, fill: c.mute }));\n  // An equaliser beside the title: bars bouncing at fixed, different rates.\n  let eq = \"\";\n  for (let i = 0; i < 4; i++) {\n    const hgt = (0.3 + 0.7 * Math.abs(Math.sin(t * (5 + i * 1.7) + i * 1.3))) * 34 * k;\n    eq += $.rect(x + cw - pad - (4 - i) * 12 * k, y + pad + 44 * k - hgt, 7 * k, hgt, { r: 3 * k, fill: c.accent });\n  }\n  s += M.part(\"eq\", Object.assign({ at: tin + 0.3, in: \"fade\", dur: 0.3, box: { x: x + cw - pad - 50 * k, y: y + pad, w: 50 * k, h: 48 * k } }, leave), eq);\n  // Progress: from `start` of the song, moving in real time.\n  const m = /^(\\d+):(\\d\\d)$/.exec(String(p.length || \"3:00\"));\n  const len = m ? Number(m[1]) * 60 + Number(m[2]) : 180;\n  const now = Math.min(len, p.start * len + Math.max(0, t - tin));\n  const fmt = function (v) { v = Math.floor(v); return Math.floor(v / 60) + \":\" + (v % 60 < 10 ? \"0\" : \"\") + (v % 60); };\n  const py = y + pad + art + 30 * k, pw = cw - pad * 2;\n  const mf = { ...$.font(\"mono\"), size: 20 * k };\n  s += M.part(\"progress\", Object.assign({ at: tin + 0.35, in: \"grow-x\", origin: \"left\", dur: 0.4, box: { x: x + pad, y: py - 6 * k, w: pw, h: 40 * k } }, leave),\n    $.rect(x + pad, py, pw, 8 * k, { r: 4 * k, fill: $.rgba(c.fg, 0.15) }),\n    $.rect(x + pad, py, pw * now / len, 8 * k, { r: 4 * k, fill: c.accent }),\n    $.circle(x + pad + pw * now / len, py + 4 * k, 10 * k, { fill: c.fg }),\n    $.text(fmt(now), { ...mf, x: x + pad, y: py + 18 * k, fill: c.mute }),\n    $.text(\"-\" + fmt(len - now), { ...mf, x: x + pad + pw, y: py + 18 * k, anchor: \"end\", fill: c.mute }));\n  return s;\n}\n","libs":[]},{"id":"rating","name":"Rating","description":"Stars fill one by one to the score, the score counts up, then a review and its author fade in.","category":"social","tags":["rating","review","stars","testimonial","proof"],"placement":"full","duration":{"default":6,"min":3,"max":30},"props":{"score":{"type":"number","label":"Score (out of 5)","default":4.8,"min":0,"max":5,"decimals":1},"count":{"type":"text","label":"Count line","default":"from 2,480 reviews","maxLength":40},"quote":{"type":"longtext","label":"Review","default":"The fastest way we've found to get from raw footage to something we're proud to post.","maxLength":200},"author":{"type":"text","label":"Author","default":"Priya S., creative director","maxLength":40}},"parts":{"ground":"the ground","stars":"the stars","score":"the score and count","quote":"the review","author":"who wrote it"},"beats":{"in":0.15,"out":-0.45},"style":"pastel","pack":"social","ref":"social/rating","code":"// MONARC Social — helpers this pack's items share.\n\n/** A platform's icon and how it names its numbers (the platforms' own marks are theirs; these are plain icons). */\nvar PLATFORM = {\n  instagram: { icon: \"camera\", line: \"Instagram\", count: \"followers\" },\n  tiktok: { icon: \"reel\", line: \"TikTok\", count: \"followers\" },\n  youtube: { icon: \"play\", line: \"YouTube\", count: \"subscribers\" },\n  x: { icon: \"x\", line: \"X\", count: \"followers\" },\n  linkedin: { icon: \"users\", line: \"LinkedIn\", count: \"followers\" },\n  threads: { icon: \"chat\", line: \"Threads\", count: \"followers\" },\n  website: { icon: \"globe\", line: \"Website\", count: \"readers\" },\n};\n\n/** A small round check in the accent: verified. */\nfunction verified($, cx, cy, r) {\n  return $.circle(cx, cy, r, { fill: $.c.accent }) + $.path(\"M\" + $.fx(cx - r * 0.45) + \" \" + $.fx(cy + r * 0.02) + \"L\" + $.fx(cx - r * 0.1) + \" \" + $.fx(cy + r * 0.36) + \"L\" + $.fx(cx + r * 0.48) + \" \" + $.fx(cy - r * 0.32), { stroke: $.on($.c.accent), sw: r * 0.28 });\n}\n\n/** A colour for a name, from the brand's two accents and fixed hues — the same name, the same colour. */\nfunction nameColor($, name) {\n  var list = [$.c.accent, $.c.accent2, \"#3DD6FF\", \"#7C5CFF\", \"#22C55E\", \"#F59E0B\"];\n  var h = 0, s = String(name || \"\");\n  for (var i = 0; i < s.length; i++) h = (h * 31 + s.charCodeAt(i)) % 9973;\n  return list[h % list.length];\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const k = $.k;\n  const tin = $.beat(\"in\", 0.15);\n  const star = function (cx, cy, r, fill) {\n    let d = \"\";\n    for (let i = 0; i < 10; i++) {\n      const a = -Math.PI / 2 + i * Math.PI / 5, rr = i % 2 ? r * 0.45 : r;\n      d += (i ? \"L\" : \"M\") + $.fx(cx + rr * Math.cos(a)) + \" \" + $.fx(cy + rr * Math.sin(a));\n    }\n    return d + \"Z\";\n  };\n  const sr = Math.min(A.w / 13, 64 * k);\n  const sg = sr * 2.4;\n  const sx0 = W / 2 - sg * 2;\n  const total = sr * 2 + 40 * k + 150 * k + 60 * k + 200 * k;\n  let y = A.y + (A.h - total) / 2 + sr;\n  let s = M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: W, h: H } }));\n  // Each star fills in turn up to the score; the last one partly.\n  let stars = \"\";\n  for (let i = 0; i < 5; i++) {\n    const cx = sx0 + i * sg;\n    const at = tin + 0.2 + i * 0.16;\n    const f = Math.max(0, Math.min(1, p.score - i)) * $.E.out3($.seg(t, at, at + 0.3));\n    const pop = 1 + 0.25 * $.bump(t, at + 0.2, 0.06, 0.25);\n    const id = $.uid(\"rs\");\n    $.def('<clipPath id=\"' + id + '\"><rect x=\"' + $.fx(cx - sr) + '\" y=\"' + $.fx(y - sr) + '\" width=\"' + $.fx(2 * sr * f) + '\" height=\"' + $.fx(2 * sr) + '\"/></clipPath>');\n    stars += $.g({ transform: $.tf({ s: pop, ox: cx, oy: y }) }, $.path(star(cx, y, sr), { fill: $.rgba(c.fg, 0.12) }), '<g clip-path=\"url(#' + id + ')\">' + $.path(star(cx, y, sr), { fill: c.accent2 }) + \"</g>\");\n  }\n  s += M.part(\"stars\", { at: tin, in: \"fade\", dur: 0.2, box: { x: sx0 - sr, y: y - sr, w: sg * 4 + 2 * sr, h: 2 * sr } }, stars);\n  y += sr + 40 * k;\n  const nf = { ...$.font(\"display\"), size: 120 * k };\n  const score = $.counter(t, tin + 0.2, tin + 1.1, p.score, { dec: 1 });\n  s += M.part(\"score\", { at: tin + 0.2, in: \"rise\", distance: 20, box: { x: A.x, y: y, w: A.w, h: nf.size * 1.6 } },\n    $.text(score, { ...nf, x: W / 2, y: y, anchor: \"middle\", fill: c.fg }),\n    $.text(String(p.count || \"\"), { ...$.font(\"text\"), size: 30 * k, x: W / 2, y: y + nf.size * 1.05, anchor: \"middle\", fill: c.mute }));\n  y += nf.size * 1.05 + 30 * k + 60 * k;\n  const qf = fitLines($, \"“\" + String(p.quote || \"\") + \"”\", { ...$.font(\"accent\"), ls: 0 }, A.w * (A.wide ? 0.7 : 0.92), 64 * k, 30 * k, 4);\n  s += M.text(\"quote\", { at: tin + 1.2, in: \"rise\", distance: 16, split: \"lines\", stagger: 0.1, ...qf.font, x: W / 2, y: y, anchor: \"middle\", fill: c.fg, lh: 1.25, maxW: A.w * (A.wide ? 0.7 : 0.92) }, qf.lines.join(\"\\n\"));\n  y += qf.lines.length * qf.size * 1.25 + 30 * k;\n  s += M.text(\"author\", { at: tin + 1.6, in: \"fade\", dur: 0.4, ...$.font(\"mono\"), size: 24 * k, ls: 0.1, x: W / 2, y: y, anchor: \"middle\", fill: c.mute }, String(p.author || \"\").toUpperCase());\n  return s;\n}\n","libs":[]},{"id":"poll","name":"Poll","description":"A poll card: the question, then each option's bar filling to its share with the percentage counting up; the winning option is ticked and lit, the vote count under it.","category":"social","tags":["poll","vote","question","results","community"],"placement":"overlay","duration":{"default":6,"min":3,"max":30},"props":{"question":{"type":"text","label":"Question","default":"What should the next video be about?","maxLength":90},"options":{"type":"list","label":"Options (Label: votes)","default":[{"label":"Colour grading","votes":4210},{"label":"Sound design","votes":2980},{"label":"Cutting dialogue","votes":1870}],"maxItems":5,"minItems":1,"of":{"label":"text","votes":"number"}},"total":{"type":"text","label":"Votes line","default":"9,060 votes · Final results","maxLength":40}},"parts":{"card":"the card"},"beats":{"in":0.15,"out":-0.45},"style":"pastel","pack":"social","ref":"social/poll","code":"// MONARC Social — helpers this pack's items share.\n\n/** A platform's icon and how it names its numbers (the platforms' own marks are theirs; these are plain icons). */\nvar PLATFORM = {\n  instagram: { icon: \"camera\", line: \"Instagram\", count: \"followers\" },\n  tiktok: { icon: \"reel\", line: \"TikTok\", count: \"followers\" },\n  youtube: { icon: \"play\", line: \"YouTube\", count: \"subscribers\" },\n  x: { icon: \"x\", line: \"X\", count: \"followers\" },\n  linkedin: { icon: \"users\", line: \"LinkedIn\", count: \"followers\" },\n  threads: { icon: \"chat\", line: \"Threads\", count: \"followers\" },\n  website: { icon: \"globe\", line: \"Website\", count: \"readers\" },\n};\n\n/** A small round check in the accent: verified. */\nfunction verified($, cx, cy, r) {\n  return $.circle(cx, cy, r, { fill: $.c.accent }) + $.path(\"M\" + $.fx(cx - r * 0.45) + \" \" + $.fx(cy + r * 0.02) + \"L\" + $.fx(cx - r * 0.1) + \" \" + $.fx(cy + r * 0.36) + \"L\" + $.fx(cx + r * 0.48) + \" \" + $.fx(cy - r * 0.32), { stroke: $.on($.c.accent), sw: r * 0.28 });\n}\n\n/** A colour for a name, from the brand's two accents and fixed hues — the same name, the same colour. */\nfunction nameColor($, name) {\n  var list = [$.c.accent, $.c.accent2, \"#3DD6FF\", \"#7C5CFF\", \"#22C55E\", \"#F59E0B\"];\n  var h = 0, s = String(name || \"\");\n  for (var i = 0; i < s.length; i++) h = (h * 31 + s.charCodeAt(i)) % 9973;\n  return list[h % list.length];\n}\n\n// A poll card: the question, then each option's bar filling to its share with the percentage counting up; the\n// winning option is ticked and lit, the vote count under it all.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const k = $.k * (A.tall ? 1.25 : 1.1);\n  const tin = $.beat(\"in\", 0.15);\n  const opts = (p.options || []).slice(0, 5);\n  const sum = opts.reduce(function (a, o) { return a + Math.max(0, Number(o.votes) || 0); }, 0) || 1;\n  const win = opts.reduce(function (b, o, i) { return (Number(o.votes) || 0) > (Number(opts[b].votes) || 0) ? i : b; }, 0);\n  const w = Math.min(A.w, (A.wide ? 760 : 900) * $.k);\n  const pad = 30 * k;\n  const qf = Object.assign({}, $.font(\"display\"), { size: 36 * k, lh: 1.2 });\n  const q = $.wrap(String(p.question || \"\"), w - pad * 2, qf).slice(0, 3);\n  const of = Object.assign({}, $.font(\"text\"), { size: 28 * k, weight: 600 }), pf = Object.assign({}, $.font(\"mono\"), { size: 26 * k, weight: 700 });\n  const oh = 64 * k, og = 14 * k;\n  const h = pad * 2 + q.length * qf.size * 1.2 + 24 * k + opts.length * (oh + og) + 34 * k;\n  const x = W / 2 - w / 2, y = A.wide ? A.y + (A.h - h) / 2 : A.y + A.h * 0.12;\n  let m = $.rect(x, y, w, h, { r: 30 * k, fill: c.surface, stroke: c.line, sw: 1.5 * k, shadow: \"lg\" });\n  q.forEach(function (l, i) { m += $.text(l, Object.assign({}, qf, { x: x + pad, y: y + pad + i * qf.size * 1.2, fill: c.fg })); });\n  const reveal = tin + 0.9;\n  opts.forEach(function (o, i) {\n    const oy = y + pad + q.length * qf.size * 1.2 + 24 * k + i * (oh + og);\n    const share = Math.max(0, Number(o.votes) || 0) / sum, f = share * $.E.out3($.seg(t, reveal + i * 0.08, reveal + 1.1 + i * 0.08));\n    const won = i === win && t > reveal + 1.2;\n    m += $.rect(x + pad, oy, w - pad * 2, oh, { r: oh * 0.3, fill: $.rgba(c.fg, 0.05), stroke: won ? c.accent : c.line, sw: (won ? 3 : 1.5) * k });\n    if (f > 0) m += $.rect(x + pad, oy, Math.max(oh * 0.6, (w - pad * 2) * f), oh, { r: oh * 0.3, fill: won ? $.rgba(c.accent, 0.3) : $.rgba(c.fg, 0.1) });\n    m += $.text(String(o.label || \"\"), Object.assign({}, of, { x: x + pad + 22 * k, y: oy + (oh - of.size) / 2 - 2 * k, fill: c.fg, maxW: w - pad * 2 - 160 * k }));\n    m += $.text(Math.round(f * 100) + \"%\", Object.assign({}, pf, { x: x + w - pad - 22 * k, y: oy + (oh - pf.size) / 2, anchor: \"end\", fill: won ? c.accent : c.mute }));\n    if (won) m += verified($, x + w - pad - 22 * k - $.measure(\"100%\", pf) - 24 * k, oy + oh / 2, 13 * k);\n  });\n  const vf = Object.assign({}, $.font(\"text\"), { size: 24 * k, weight: 500 });\n  m += $.text(String(p.total || \"\"), Object.assign({}, vf, { x: x + pad, y: y + h - pad - vf.size * 0.4, fill: c.mute }));\n  return M.part(\"card\", { at: tin, in: \"pop\", ease: \"bouncy\", out: \"fade\", box: { x: x, y: y, w: w, h: h } }, m);\n}\n","libs":[]},{"id":"milestone","name":"Milestone","description":"A milestone badge: the platform's icon and the count rolling up to the goal; at the goal it pops, confetti bursts and the thank-you line rises under it.","category":"social","tags":["milestone","followers","subscribers","celebration","thank you"],"placement":"overlay","duration":{"default":5,"min":3,"max":20},"props":{"value":{"type":"number","label":"Goal","default":100000,"min":1,"max":10000000000},"label":{"type":"text","label":"Counts","default":"","maxLength":20},"platform":{"type":"choice","label":"Platform","options":["youtube","instagram","tiktok","x","linkedin","threads","website"],"default":"youtube"},"line":{"type":"text","label":"Line","default":"Thank you for 100K!","maxLength":40}},"parts":{"badge":"the badge","confetti":"the confetti","line":"the line under it"},"beats":{"in":0.15,"out":-0.45},"style":"pastel","pack":"social","ref":"social/milestone","code":"// MONARC Social — helpers this pack's items share.\n\n/** A platform's icon and how it names its numbers (the platforms' own marks are theirs; these are plain icons). */\nvar PLATFORM = {\n  instagram: { icon: \"camera\", line: \"Instagram\", count: \"followers\" },\n  tiktok: { icon: \"reel\", line: \"TikTok\", count: \"followers\" },\n  youtube: { icon: \"play\", line: \"YouTube\", count: \"subscribers\" },\n  x: { icon: \"x\", line: \"X\", count: \"followers\" },\n  linkedin: { icon: \"users\", line: \"LinkedIn\", count: \"followers\" },\n  threads: { icon: \"chat\", line: \"Threads\", count: \"followers\" },\n  website: { icon: \"globe\", line: \"Website\", count: \"readers\" },\n};\n\n/** A small round check in the accent: verified. */\nfunction verified($, cx, cy, r) {\n  return $.circle(cx, cy, r, { fill: $.c.accent }) + $.path(\"M\" + $.fx(cx - r * 0.45) + \" \" + $.fx(cy + r * 0.02) + \"L\" + $.fx(cx - r * 0.1) + \" \" + $.fx(cy + r * 0.36) + \"L\" + $.fx(cx + r * 0.48) + \" \" + $.fx(cy - r * 0.32), { stroke: $.on($.c.accent), sw: r * 0.28 });\n}\n\n/** A colour for a name, from the brand's two accents and fixed hues — the same name, the same colour. */\nfunction nameColor($, name) {\n  var list = [$.c.accent, $.c.accent2, \"#3DD6FF\", \"#7C5CFF\", \"#22C55E\", \"#F59E0B\"];\n  var h = 0, s = String(name || \"\");\n  for (var i = 0; i < s.length; i++) h = (h * 31 + s.charCodeAt(i)) % 9973;\n  return list[h % list.length];\n}\n\n// A milestone: a badge with the platform's icon and the count rolling up to the goal; when it gets there it\n// pops, confetti bursts and the thank-you line rises under it.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const k = $.k;\n  const tin = $.beat(\"in\", 0.15);\n  const pl = PLATFORM[p.platform] || PLATFORM.youtube;\n  const goal = Number(p.value) || 0;\n  const hit = tin + 1.6;\n  const cx = W / 2, cy = A.y + A.h * (A.tall ? 0.38 : 0.42);\n  const R = Math.min(A.w * 0.3, A.h * (A.tall ? 0.2 : 0.3));\n  // The ring fills as the count climbs; at the goal it pulses once.\n  const f = $.E.inOut2($.seg(t, tin + 0.3, hit));\n  const thump = 1 + 0.12 * $.bump(t, hit, 0.08, 0.35);\n  let b = $.circle(cx, cy, R, { fill: c.surface, shadow: \"lg\" });\n  b += $.ring(cx, cy, R * 0.9, f, { color: c.accent, sw: R * 0.08 });\n  b += appIcon($, pl.icon, cx - R * 0.17, cy - R * 0.62, R * 0.34, { fill: c.accent });\n  const nf = fitFont($, $.count(goal), \"display\", R * 0.5, R * 1.5, { ls: -0.04 });\n  b += $.text($.count(Math.round(goal * f)), Object.assign({}, nf, { x: cx, y: cy - nf.size * 0.3, anchor: \"middle\", fill: c.fg }));\n  const lf = Object.assign({}, $.font(\"text\"), { size: R * 0.14, weight: 600 });\n  b += $.text(String(p.label || pl.count), Object.assign({}, lf, { x: cx, y: cy + nf.size * 0.75, anchor: \"middle\", fill: c.mute }));\n  let s = M.part(\"badge\", { at: tin, in: \"pop\", ease: \"bouncy\", out: \"fade\", box: { x: cx - R, y: cy - R, w: 2 * R, h: 2 * R } }, $.g({ transform: $.tf({ s: thump, ox: cx, oy: cy }) }, b));\n  if (t >= hit) s += M.part(\"confetti\", { at: hit, in: \"none\", out: \"fade\" }, $.burst(t, hit, cx, cy, { n: 28, r: R * 2.4, colors: [c.accent, c.accent2, \"#FFD23F\", \"#3DD6FF\"], size: 14 * k, dur: 1.2 }));\n  if (p.line) {\n    const tf = fitFont($, $.case(String(p.line)), \"display\", (A.tall ? 84 : 72) * k, A.w, { ls: -0.02 });\n    s += M.text(\"line\", Object.assign({ at: hit + 0.15, in: \"rise\", distance: 30, out: \"fade\" }, tf, { x: cx, y: cy + R + 50 * k, anchor: \"middle\", fill: c.fg, shadow: \"sm\" }), $.case(String(p.line)));\n  }\n  return s;\n}\n","libs":[]},{"id":"story","name":"Story frame","description":"A story's frame over your footage: progress segments along the top with the current one filling, the poster's badge, name and time, a tilted caption sticker and the reply bar.","category":"social","tags":["story","stories","reels","overlay","vertical"],"placement":"overlay","duration":{"default":6,"min":2,"max":30},"props":{"name":{"type":"text","label":"Name","default":"Monarc Studio","maxLength":30},"time":{"type":"text","label":"Time","default":"2h","maxLength":8},"segments":{"type":"number","label":"Segments","default":4,"min":1,"max":8},"current":{"type":"number","label":"Current","default":2,"min":1,"max":8},"caption":{"type":"text","label":"Sticker","default":"New cut drops Friday","maxLength":50},"reply":{"type":"boolean","label":"Reply bar","default":true}},"parts":{"segments":"the progress segments","poster":"the badge, name and time","sticker":"the caption sticker","reply":"the reply bar"},"beats":{"in":0.15,"out":-0.45},"style":"pastel","pack":"social","ref":"social/story","code":"// MONARC Social — helpers this pack's items share.\n\n/** A platform's icon and how it names its numbers (the platforms' own marks are theirs; these are plain icons). */\nvar PLATFORM = {\n  instagram: { icon: \"camera\", line: \"Instagram\", count: \"followers\" },\n  tiktok: { icon: \"reel\", line: \"TikTok\", count: \"followers\" },\n  youtube: { icon: \"play\", line: \"YouTube\", count: \"subscribers\" },\n  x: { icon: \"x\", line: \"X\", count: \"followers\" },\n  linkedin: { icon: \"users\", line: \"LinkedIn\", count: \"followers\" },\n  threads: { icon: \"chat\", line: \"Threads\", count: \"followers\" },\n  website: { icon: \"globe\", line: \"Website\", count: \"readers\" },\n};\n\n/** A small round check in the accent: verified. */\nfunction verified($, cx, cy, r) {\n  return $.circle(cx, cy, r, { fill: $.c.accent }) + $.path(\"M\" + $.fx(cx - r * 0.45) + \" \" + $.fx(cy + r * 0.02) + \"L\" + $.fx(cx - r * 0.1) + \" \" + $.fx(cy + r * 0.36) + \"L\" + $.fx(cx + r * 0.48) + \" \" + $.fx(cy - r * 0.32), { stroke: $.on($.c.accent), sw: r * 0.28 });\n}\n\n/** A colour for a name, from the brand's two accents and fixed hues — the same name, the same colour. */\nfunction nameColor($, name) {\n  var list = [$.c.accent, $.c.accent2, \"#3DD6FF\", \"#7C5CFF\", \"#22C55E\", \"#F59E0B\"];\n  var h = 0, s = String(name || \"\");\n  for (var i = 0; i < s.length; i++) h = (h * 31 + s.charCodeAt(i)) % 9973;\n  return list[h % list.length];\n}\n\n// A story's frame: progress segments along the top (the current one filling), the poster's badge, name and time,\n// a caption sticker and the reply bar — laid out in a 9:16 column, over your footage.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, W = $.W, H = $.H;\n  const M = motion($, t);\n  const A = safeArea($);\n  const tin = $.beat(\"in\", 0.1);\n  // A 9:16 column: the whole frame on 9:16, centred on wider ones.\n  const cw = Math.min(W, H * 9 / 16), cx0 = (W - cw) / 2;\n  const k = cw / 1080;\n  const m0 = 36 * k, top = Math.max(A.tall ? A.y * 0.4 : 30 * $.k, 36 * k);\n  const n = Math.max(1, Math.min(8, Math.round(p.segments))), cur = Math.max(1, Math.min(n, Math.round(p.current)));\n  const segG = 10 * k, segW = (cw - m0 * 2 - segG * (n - 1)) / n, segH = 8 * k;\n  let s = \"\";\n  if (cw < W - 2) s += $.rect(cx0, 0, cw, H, { stroke: $.rgba(\"#FFFFFF\", 0.35), sw: 3 * $.k, r: 24 * $.k });\n  let bar = \"\";\n  for (let i = 0; i < n; i++) {\n    const x = cx0 + m0 + i * (segW + segG);\n    const f = i + 1 < cur ? 1 : i + 1 === cur ? $.seg(t, tin + 0.2, $.dur - 0.3) : 0;\n    bar += $.rect(x, top, segW, segH, { r: segH / 2, fill: $.rgba(\"#FFFFFF\", 0.4) });\n    if (f > 0) bar += $.rect(x, top, segW * f, segH, { r: segH / 2, fill: \"#FFFFFF\" });\n  }\n  s += M.part(\"segments\", { at: tin, in: \"fade\", dur: 0.3, out: \"fade\" }, bar);\n  // The poster: a ring badge with initials, name and time.\n  const ar = 52 * k, ax = cx0 + m0 + ar, ay = top + segH + 34 * k + ar;\n  const nf = Object.assign({}, $.font(\"display\"), { size: 46 * k }), tf = Object.assign({}, $.font(\"text\"), { size: 38 * k, weight: 500 });\n  let who = $.circle(ax, ay, ar + 5 * k, { stroke: c.accent, sw: 5 * k }) + $.circle(ax, ay, ar, { fill: nameColor($, p.name) });\n  who += $.text(initials(String(p.name || \"\")), Object.assign({}, $.font(\"text\"), { size: ar * 0.8, weight: 700, x: ax, y: ay - ar * 0.45, anchor: \"middle\", fill: $.on(nameColor($, p.name)) }));\n  who += $.text(String(p.name || \"\"), Object.assign({}, nf, { x: ax + ar + 22 * k, y: ay - nf.size * 0.55, fill: \"#FFFFFF\", shadow: \"sm\" }));\n  who += $.text(String(p.time || \"\"), Object.assign({}, tf, { x: ax + ar + 34 * k + $.measure(String(p.name || \"\"), nf), y: ay - tf.size * 0.5, fill: $.rgba(\"#FFFFFF\", 0.75), shadow: \"sm\" }));\n  s += M.part(\"poster\", { at: tin + 0.1, in: \"slide\", from: \"left\", distance: 30, out: \"fade\", box: { x: ax - ar, y: ay - ar, w: cw * 0.7, h: ar * 2 } }, who);\n  // The caption sticker: a tilted label in the middle third.\n  if (p.caption) {\n    const sf = Object.assign({}, $.font(\"display\"), { size: 64 * k });\n    const lines = $.wrap($.case(String(p.caption)), cw * 0.72, sf).slice(0, 3);\n    const lw = Math.max.apply(null, lines.map(function (l) { return $.measure(l, sf); })) + 60 * k, lh = lines.length * sf.size * 1.15 + 40 * k;\n    const sx = cx0 + cw / 2 - lw / 2, sy = H * 0.5 - lh / 2;\n    let st = $.rect(sx, sy, lw, lh, { r: 22 * k, fill: \"#FFFFFF\", shadow: \"md\" });\n    lines.forEach(function (l, i) { st += $.text(l, Object.assign({}, sf, { x: cx0 + cw / 2, y: sy + 20 * k + i * sf.size * 1.15, anchor: \"middle\", fill: c.accent })); });\n    s += M.part(\"sticker\", { at: tin + 0.4, in: \"pop\", ease: \"bouncy\", out: \"fade\", box: { x: sx, y: sy, w: lw, h: lh } }, $.g({ transform: $.tf({ r: -4, ox: cx0 + cw / 2, oy: H * 0.5 }) }, st));\n  }\n  // The reply bar along the bottom.\n  if (p.reply !== false) {\n    const rh = 92 * k, ry = H - (A.tall ? H - (A.y + A.h) : 40 * $.k) - rh;\n    const rf = Object.assign({}, $.font(\"text\"), { size: 30 * k, weight: 500 });\n    let rb = $.rect(cx0 + m0, ry, cw - m0 * 2 - rh - 20 * k, rh, { r: rh / 2, stroke: $.rgba(\"#FFFFFF\", 0.8), sw: 3 * k, fill: $.rgba(\"#000000\", 0.15) });\n    rb += $.text(\"Send message\", Object.assign({}, rf, { x: cx0 + m0 + 36 * k, y: ry + (rh - rf.size) / 2 - 2 * k, fill: $.rgba(\"#FFFFFF\", 0.85) }));\n    rb += $.icon(\"heart\", cx0 + cw - m0 - rh + 10 * k, ry + rh * 0.2, rh * 0.6, { color: \"#FFFFFF\", sw: 2.4 });\n    s += M.part(\"reply\", { at: tin + 0.3, in: \"rise\", distance: 30, out: \"fade\", box: { x: cx0 + m0, y: ry, w: cw - m0 * 2, h: rh } }, rb);\n  }\n  return s;\n}\n","libs":[]},{"id":"live-chat","name":"Live chat","description":"A live chat over the stream: messages arrive at the bottom and push the earlier ones up, each name in its own colour, under a LIVE header with the viewer count.","category":"social","tags":["live","chat","stream","comments","twitch"],"placement":"overlay","duration":{"default":7,"min":3,"max":60},"props":{"viewers":{"type":"text","label":"Viewers","default":"2.4K","maxLength":10},"messages":{"type":"list","label":"Messages (Name: text)","default":[{"name":"ana_r","text":"the colour on this is unreal"},{"name":"kenji","text":"how long did the edit take?"},{"name":"leanovak","text":"cutting on the beat >>>"},{"name":"sam.o","text":"first time catching it live!"},{"name":"priya","text":"can you share the LUT?"},{"name":"tomasz","text":"this is the best one yet"}],"maxItems":12,"minItems":1,"of":{"name":"text","text":"text"}}},"parts":{"panel":"the panel and header","messages":"the messages"},"beats":{"in":0.15,"out":-0.45},"style":"pastel","pack":"social","ref":"social/live-chat","code":"// MONARC Social — helpers this pack's items share.\n\n/** A platform's icon and how it names its numbers (the platforms' own marks are theirs; these are plain icons). */\nvar PLATFORM = {\n  instagram: { icon: \"camera\", line: \"Instagram\", count: \"followers\" },\n  tiktok: { icon: \"reel\", line: \"TikTok\", count: \"followers\" },\n  youtube: { icon: \"play\", line: \"YouTube\", count: \"subscribers\" },\n  x: { icon: \"x\", line: \"X\", count: \"followers\" },\n  linkedin: { icon: \"users\", line: \"LinkedIn\", count: \"followers\" },\n  threads: { icon: \"chat\", line: \"Threads\", count: \"followers\" },\n  website: { icon: \"globe\", line: \"Website\", count: \"readers\" },\n};\n\n/** A small round check in the accent: verified. */\nfunction verified($, cx, cy, r) {\n  return $.circle(cx, cy, r, { fill: $.c.accent }) + $.path(\"M\" + $.fx(cx - r * 0.45) + \" \" + $.fx(cy + r * 0.02) + \"L\" + $.fx(cx - r * 0.1) + \" \" + $.fx(cy + r * 0.36) + \"L\" + $.fx(cx + r * 0.48) + \" \" + $.fx(cy - r * 0.32), { stroke: $.on($.c.accent), sw: r * 0.28 });\n}\n\n/** A colour for a name, from the brand's two accents and fixed hues — the same name, the same colour. */\nfunction nameColor($, name) {\n  var list = [$.c.accent, $.c.accent2, \"#3DD6FF\", \"#7C5CFF\", \"#22C55E\", \"#F59E0B\"];\n  var h = 0, s = String(name || \"\");\n  for (var i = 0; i < s.length; i++) h = (h * 31 + s.charCodeAt(i)) % 9973;\n  return list[h % list.length];\n}\n\n// A live chat over the stream: messages arrive one after another at the bottom and push the earlier ones up,\n// each name in its own colour; a LIVE header with the viewer count tops the panel.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const k = $.k * (A.tall ? 1.3 : 1.1);\n  const tin = $.beat(\"in\", 0.15);\n  const msgs = (p.messages || []).slice(0, 12);\n  const pw = A.wide ? Math.min(A.w * 0.36, 620 * $.k) : A.w;\n  const px = A.wide ? A.x + A.w - pw : A.x;\n  const ph = A.wide ? A.h : A.h * 0.5;\n  const py = A.wide ? A.y : A.y + A.h - ph;\n  const pad = 24 * k;\n  const nf = Object.assign({}, $.font(\"text\"), { size: 26 * k, weight: 700 }), mf = Object.assign({}, $.font(\"text\"), { size: 26 * k, weight: 500, lh: 1.3 });\n  // The header: a red LIVE chip and the viewer count.\n  const hh = 60 * k;\n  let s = $.rect(px, py, pw, ph, { r: 24 * k, fill: $.rgba(c.ink, 0.55) });\n  s += $.chip(\"LIVE\", px + pad, py + (hh - 36 * k) / 2 + 6 * k, { size: 22 * k, fill: c.red, color: \"#FFFFFF\", dot: true });\n  s += $.icon(\"eye\", px + pad + 110 * k, py + hh / 2 - 12 * k, 26 * k, { color: \"#FFFFFF\", sw: 2.2 });\n  s += $.text(String(p.viewers || \"\"), Object.assign({}, nf, { x: px + pad + 146 * k, y: py + hh / 2 - nf.size * 0.5, fill: \"#FFFFFF\" }));\n  s += $.line(px + pad, py + hh + 6 * k, px + pw - pad, py + hh + 6 * k, { stroke: $.rgba(\"#FFFFFF\", 0.15), sw: 2 * k });\n  let out = M.part(\"panel\", { at: tin, in: \"fade\", dur: 0.4, out: \"fade\" }, s);\n  // The messages: one every `gap` seconds; the newest at the bottom, the stack scrolling up as they come.\n  const gap = Math.max(0.3, (($.beat(\"out\", $.dur - 0.45) - tin - 0.6) / Math.max(1, msgs.length)));\n  const lay = msgs.map(function (mm) {\n    const name = String(mm.name || \"\"), text = String(mm.text || \"\");\n    const lines = $.wrap(name + \"  \" + text, pw - pad * 2, mf).slice(0, 3);\n    return { name: name, text: text, lines: lines, h: lines.length * mf.size * 1.3 + 18 * k };\n  });\n  const bottom = py + ph - pad;\n  const clip = $.uid();\n  $.def('<clipPath id=\"' + clip + '\"><rect x=\"' + px + '\" y=\"' + (py + hh + 14 * k) + '\" width=\"' + pw + '\" height=\"' + (ph - hh - 14 * k) + '\"/></clipPath>');\n  let list = \"\";\n  let shown = 0;\n  lay.forEach(function (_, i) { if (t >= tin + 0.6 + i * gap) shown = i + 1; });\n  // Total height of what is shown, with the newest easing up from below.\n  let y = bottom;\n  for (let i = shown - 1; i >= 0; i--) {\n    const at = tin + 0.6 + i * gap, e = $.E.out3($.seg(t, at, at + 0.25));\n    const L = lay[i];\n    y -= L.h * (i === shown - 1 ? e : 1);\n    const col = nameColor($, L.name);\n    let m = \"\";\n    L.lines.forEach(function (line, j) {\n      if (j === 0 && line.indexOf(L.name) === 0) {\n        m += $.text(L.name, Object.assign({}, nf, { x: px + pad, y: y + j * mf.size * 1.3, fill: col }));\n        m += $.text(line.slice(L.name.length), Object.assign({}, mf, { x: px + pad + $.measure(L.name, nf), y: y + j * mf.size * 1.3, fill: \"#FFFFFF\" }));\n      } else m += $.text(line, Object.assign({}, mf, { x: px + pad, y: y + j * mf.size * 1.3, fill: \"#FFFFFF\" }));\n    });\n    list += $.g({ op: i === shown - 1 ? e : 1 }, m);\n  }\n  out += M.part(\"messages\", { at: tin + 0.5, in: \"none\", out: \"fade\" }, '<g clip-path=\"url(#' + clip + ')\">' + list + \"</g>\");\n  return out;\n}\n","libs":[]},{"id":"joined","name":"Joined","description":"Who joined: overlapping avatar badges pop in one after another, a +N badge closes the row and the line (\"Ana, Kenji and 12 others joined\") types in.","category":"social","tags":["joined","avatars","community","members","social proof"],"placement":"overlay","duration":{"default":5,"min":2,"max":20},"props":{"names":{"type":"list","label":"Names","default":["Ana Ruiz","Kenji Mori","Lea Novak","Sam Okafor"],"maxItems":6,"minItems":1,"of":"text"},"more":{"type":"number","label":"Others","default":12,"min":0,"max":1000000000},"line":{"type":"text","label":"Line","default":"joined the stream","maxLength":40}},"parts":{"pill":"the pill","avatars":"the avatars","line":"the line"},"beats":{"in":0.15,"out":-0.45},"style":"pastel","pack":"social","ref":"social/joined","code":"// MONARC Social — helpers this pack's items share.\n\n/** A platform's icon and how it names its numbers (the platforms' own marks are theirs; these are plain icons). */\nvar PLATFORM = {\n  instagram: { icon: \"camera\", line: \"Instagram\", count: \"followers\" },\n  tiktok: { icon: \"reel\", line: \"TikTok\", count: \"followers\" },\n  youtube: { icon: \"play\", line: \"YouTube\", count: \"subscribers\" },\n  x: { icon: \"x\", line: \"X\", count: \"followers\" },\n  linkedin: { icon: \"users\", line: \"LinkedIn\", count: \"followers\" },\n  threads: { icon: \"chat\", line: \"Threads\", count: \"followers\" },\n  website: { icon: \"globe\", line: \"Website\", count: \"readers\" },\n};\n\n/** A small round check in the accent: verified. */\nfunction verified($, cx, cy, r) {\n  return $.circle(cx, cy, r, { fill: $.c.accent }) + $.path(\"M\" + $.fx(cx - r * 0.45) + \" \" + $.fx(cy + r * 0.02) + \"L\" + $.fx(cx - r * 0.1) + \" \" + $.fx(cy + r * 0.36) + \"L\" + $.fx(cx + r * 0.48) + \" \" + $.fx(cy - r * 0.32), { stroke: $.on($.c.accent), sw: r * 0.28 });\n}\n\n/** A colour for a name, from the brand's two accents and fixed hues — the same name, the same colour. */\nfunction nameColor($, name) {\n  var list = [$.c.accent, $.c.accent2, \"#3DD6FF\", \"#7C5CFF\", \"#22C55E\", \"#F59E0B\"];\n  var h = 0, s = String(name || \"\");\n  for (var i = 0; i < s.length; i++) h = (h * 31 + s.charCodeAt(i)) % 9973;\n  return list[h % list.length];\n}\n\n// Who joined: overlapping avatar badges pop in one after another, a \"+12\" badge closes the row, and the line\n// (\"Ana, Kenji and 12 others joined\") types in beside or under it.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const k = $.k * (A.tall ? 1.3 : 1.15);\n  const tin = $.beat(\"in\", 0.15);\n  const names = (p.names || []).map(String).filter(Boolean).slice(0, 6);\n  const more = Math.max(0, Math.round(Number(p.more) || 0));\n  const r = 46 * k, step = r * 1.7;\n  const count = names.length + (more ? 1 : 0);\n  const rowW = (count - 1) * step + 2 * r;\n  const first = names.slice(0, 2).join(\", \");\n  const line = (first ? first + (more ? \" and \" + more + \" others \" : \" \") : \"\") + String(p.line || \"\");\n  // The line shrinks to fit the frame when it is set under the badges.\n  const lf = fitFont($, line, \"text\", 34 * k, A.w - 60 * k, { weight: 600 });\n  const lineW = $.measure(line, lf);\n  const beside = A.wide && rowW + 30 * k + lineW < A.w * 0.9;\n  const pillW = beside ? rowW + 30 * k + lineW + 50 * k : Math.max(rowW, lineW) + 60 * k, pillH = beside ? 2 * r + 30 * k : 2 * r + lf.size * 1.6 + 50 * k;\n  const x = W / 2 - pillW / 2, y = A.y + A.h - pillH;\n  let s = M.part(\"pill\", { at: tin, in: \"rise\", distance: 40, out: \"fade\", box: { x: x, y: y, w: pillW, h: pillH } }, $.rect(x, y, pillW, pillH, { r: beside ? pillH / 2 : 34 * k, fill: c.surface, stroke: c.line, sw: 1.5 * k, shadow: \"lg\" }));\n  const ax0 = beside ? x + 25 * k + r : W / 2 - rowW / 2 + r, ay = y + 15 * k + r + (beside ? 0 : 10 * k);\n  let row = \"\";\n  names.forEach(function (nm, i) {\n    const at = tin + 0.3 + i * 0.12, sc = Math.max(0, $.pop(t, at, 2.6, 7));\n    if (sc <= 0) return;\n    const ax = ax0 + i * step, col = nameColor($, nm);\n    row += $.g({ transform: $.tf({ s: sc, ox: ax, oy: ay }) }, $.circle(ax, ay, r + 4 * k, { fill: c.surface }) + $.circle(ax, ay, r, { fill: col }) +\n      $.text(initials(nm), Object.assign({}, $.font(\"text\"), { size: r * 0.72, weight: 700, x: ax, y: ay - r * 0.42, anchor: \"middle\", fill: $.on(col) })));\n  });\n  if (more) {\n    const at = tin + 0.3 + names.length * 0.12, sc = Math.max(0, $.pop(t, at, 2.6, 7));\n    const ax = ax0 + names.length * step;\n    if (sc > 0) row += $.g({ transform: $.tf({ s: sc, ox: ax, oy: ay }) }, $.circle(ax, ay, r + 4 * k, { fill: c.surface }) + $.circle(ax, ay, r, { fill: c.accent }) +\n      $.text(\"+\" + more, Object.assign({}, $.font(\"text\"), { size: r * 0.62, weight: 800, x: ax, y: ay - r * 0.36, anchor: \"middle\", fill: $.on(c.accent) })));\n  }\n  s += M.part(\"avatars\", { at: tin + 0.3, in: \"none\", out: \"fade\" }, row);\n  const lx = beside ? ax0 + rowW - r + 30 * k : W / 2, ly = beside ? ay - lf.size * 0.55 : ay + r + 24 * k;\n  s += M.text(\"line\", Object.assign({ at: tin + 0.4 + count * 0.12, in: \"fade\", dur: 0.4, out: \"fade\" }, lf, { x: lx, y: ly, anchor: beside ? \"start\" : \"middle\", fill: c.fg }), $.typed(t, tin + 0.4 + count * 0.12, line, 40));\n  return s;\n}\n","libs":[]}]},{"format":1,"id":"data","name":"MONARC Data","version":"1.0.0","description":"Line charts, donuts, bar races, goal meters, gauges, KPI tiles, waffles, heatmaps, leaderboards, versus splits, timelines, funnels, a world-map choropleth and flight routes — data that moves, in any brand, any aspect.","author":"MONARC STUDIOS","license":"MONARC Packs License 1.0 (free to use in any video; not for resale as templates)","homepage":"https://volito.dev/docs/packs/data","style":"cream","shared":"shared.js","items":["line-chart","donut","bar-race","goals","versus","timeline","funnel","world-map","flight","heatmap","gauge","kpi-grid","waffle","leaderboard"],"entries":[{"id":"line-chart","name":"Line chart","description":"A line draws on through its points over a soft area, each point popping as the line reaches it, the last value called out, against an optional target.","category":"chart","tags":["chart","line","trend","growth","data"],"placement":"full","duration":{"default":6,"min":3,"max":30},"props":{"title":{"type":"text","label":"Title","default":"Weekly views since the switch","maxLength":56},"rows":{"type":"list","label":"Rows (Label: value)","default":[{"label":"W1","value":12},{"label":"W2","value":18},{"label":"W3","value":16},{"label":"W4","value":27},{"label":"W5","value":34},{"label":"W6","value":31},{"label":"W7","value":46},{"label":"W8","value":58}],"maxItems":12,"minItems":2,"of":{"label":"text","value":"number"}},"prefix":{"type":"text","label":"Before numbers","default":"","maxLength":3},"suffix":{"type":"text","label":"After numbers","default":"K","maxLength":6},"decimals":{"type":"number","label":"Decimals","default":0,"min":0,"max":3},"target":{"type":"number","label":"Target line (0 = none)","default":40,"min":0,"max":1000000000000},"targetLabel":{"type":"text","label":"Target label","default":"Goal","maxLength":16}},"parts":{"ground":"the ground","title":"the heading","grid":"the gridlines and axis labels","area":"the area under the line","line":"the line","dots":"the points","label":"the last value's callout","target":"the target line"},"beats":{"in":0.15,"out":-0.45},"style":"cream","pack":"data","ref":"data/line-chart","code":"// MONARC Data — helpers this pack's charts share.\n\n/** Series colours: the brand's two accents first, then fixed hues that sit well beside them. */\nfunction seriesColor($, i) {\n  var list = [$.c.accent, $.c.accent2, \"#3DD6FF\", \"#7C5CFF\", \"#22C55E\", \"#F59E0B\", \"#EC4899\", \"#94A3B8\"];\n  return list[i % list.length];\n}\n\n/** A number as a reader expects it: thousands separators, fixed decimals, a prefix and a suffix. */\nfunction fmtNum(v, dec, prefix, suffix) {\n  var n = Number(v) || 0, d = Math.max(0, Math.min(3, Math.round(dec || 0)));\n  var s = n.toFixed(d).replace(/\\B(?=(\\d{3})+(?!\\d))/g, \",\");\n  if (d) { var parts = n.toFixed(d).split(\".\"); s = parts[0].replace(/\\B(?=(\\d{3})+(?!\\d))/g, \",\") + \".\" + parts[1]; }\n  return (prefix || \"\") + s + (suffix || \"\");\n}\n\n/** Round \"nice\" steps for an axis that reaches max: 1, 2 or 5 × a power of ten. */\nfunction niceStep(max, count) {\n  var raw = (max || 1) / Math.max(1, count || 4), pow = Math.pow(10, Math.floor(Math.log(raw) / Math.LN10)), m = raw / pow;\n  return (m <= 1 ? 1 : m <= 2 ? 2 : m <= 5 ? 5 : 10) * pow;\n}\n\n/** A heading (and kicker) at the top of the safe area; returns the y below it. */\nfunction chartHead($, M, t, at, title, A, k) {\n  if (!title) return { s: \"\", y: A.y };\n  var tf = fitLines($, $.case(title), Object.assign({}, $.font(\"display\"), { ls: -0.02 }), A.w, Math.min((A.tall ? 96 : 80) * k, A.h * 0.1), 40 * k, 2);\n  var s = \"\";\n  tf.lines.forEach(function (line, i) { s += M.text(\"title\", Object.assign({ at: at + i * 0.1, in: \"rise\", distance: 22 }, tf.font, { x: A.x, y: A.y + i * tf.size * 1.05, fill: $.c.fg }), line); });\n  return { s: s, y: A.y + tf.lines.length * tf.size * 1.05 + 40 * k };\n}\n\n/** A donut or ring segment from angle a0 to a1 (radians, 0 = up, clockwise). */\nfunction arcPath(cx, cy, r0, r1, a0, a1) {\n  var big = a1 - a0 > Math.PI ? 1 : 0;\n  var p = function (r, a) { return (cx + r * Math.sin(a)).toFixed(2) + \" \" + (cy - r * Math.cos(a)).toFixed(2); };\n  if (a1 - a0 >= Math.PI * 2 - 1e-4) a1 = a0 + Math.PI * 2 - 1e-4;\n  return \"M\" + p(r1, a0) + \"A\" + r1 + \" \" + r1 + \" 0 \" + big + \" 1 \" + p(r1, a1) + \"L\" + p(r0, a1) + \"A\" + r0 + \" \" + r0 + \" 0 \" + big + \" 0 \" + p(r0, a0) + \"Z\";\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const k = $.k;\n  const tin = $.beat(\"in\", 0.15);\n  let s = M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: W, h: H } }));\n  const head = chartHead($, M, t, tin, p.title, A, k);\n  s += head.s;\n  const rows = p.rows || [];\n  const n = rows.length;\n  const lf = { ...$.font(\"text\"), size: Math.min(28 * k, (A.w / n) * 0.3) };\n  const vf = { ...$.font(\"mono\"), size: 22 * k };\n  const maxV = Math.max.apply(null, rows.map(function (r) { return r.value; }).concat([p.target || 0, 1e-9]));\n  const step = niceStep(maxV * 1.1, 4);\n  const top = step * Math.ceil((maxV * 1.08) / step);\n  const axisW = $.measure(fmtNum(top, 0, p.prefix, p.suffix), vf) + 18 * k;\n  const box = { x: A.x + axisW, y: head.y + 30 * k, w: A.w - axisW - 70 * k, h: A.y + A.h - head.y - 30 * k - lf.size * 2.2 };\n  const X = function (i) { return box.x + (n > 1 ? (i / (n - 1)) * box.w : box.w / 2); };\n  const Y = function (v) { return box.y + box.h - (v / top) * box.h; };\n  let grid = \"\";\n  for (let v = 0; v <= top + 1e-9; v += step) {\n    grid += $.line(box.x, Y(v), box.x + box.w, Y(v), { stroke: $.rgba(c.fg, v === 0 ? 0.3 : 0.1), sw: 2 * k });\n    grid += $.text(fmtNum(v, 0, p.prefix, p.suffix), { ...vf, x: box.x - 16 * k, y: Y(v) - vf.size * 0.55, anchor: \"end\", fill: c.mute });\n  }\n  rows.forEach(function (r, i) { if (n <= 8 || i % Math.ceil(n / 8) === 0 || i === n - 1) grid += $.text(String(r.label), { ...lf, x: X(i), y: box.y + box.h + lf.size * 0.6, anchor: \"middle\", fill: c.mute }); });\n  s += M.part(\"grid\", { at: tin + 0.1, in: \"fade\", dur: 0.4, out: \"fade\", vary: false }, grid);\n  // The line draws on from the left; each point pops when the line reaches it.\n  const t0 = tin + 0.5, t1 = t0 + 1.8;\n  const run = $.E.inOut2($.seg(t, t0, t1));\n  const pts = rows.map(function (r, i) { return [X(i), Y(r.value)]; });\n  const d = pts.map(function (q, i) { return (i ? \"L\" : \"M\") + $.fx(q[0]) + \" \" + $.fx(q[1]); }).join(\"\");\n  const reach = box.x + box.w * run;\n  if (p.target > 0) {\n    const ty = Y(p.target);\n    s += M.part(\"target\", { at: tin + 0.3, in: \"grow-x\", origin: \"left\", dur: 0.6, out: \"fade\", box: { x: box.x, y: ty - 4 * k, w: box.w, h: 8 * k } },\n      $.line(box.x, ty, box.x + box.w, ty, { stroke: c.accent2, sw: 3 * k, dash: 12 * k + \" \" + 10 * k }),\n      $.text(String(p.targetLabel || \"\") + \" \" + fmtNum(p.target, p.decimals, p.prefix, p.suffix), { ...vf, x: box.x + box.w + 12 * k, y: ty - vf.size * 0.55, fill: c.accent2 }));\n  }\n  const gid = $.uid(\"lg\");\n  $.def('<linearGradient id=\"' + gid + '\" x1=\"0\" y1=\"0\" x2=\"0\" y2=\"1\"><stop offset=\"0\" stop-color=\"' + c.accent + '\" stop-opacity=\"0.32\"/><stop offset=\"1\" stop-color=\"' + c.accent + '\" stop-opacity=\"0\"/></linearGradient>');\n  s += $.clip(box.x - 10 * k, box.y - 40 * k, reach - box.x + 10 * k, box.h + 50 * k, 0,\n    M.part(\"area\", { at: t0, in: \"fade\", dur: 0.4, out: \"fade\", vary: false }, $.path(d + \"L\" + $.fx(pts[n - 1][0]) + \" \" + $.fx(box.y + box.h) + \"L\" + $.fx(pts[0][0]) + \" \" + $.fx(box.y + box.h) + \"Z\", { fill: \"url(#\" + gid + \")\" })) +\n    M.part(\"line\", { at: t0, in: \"none\", out: \"fade\" }, $.path(d, { stroke: c.accent, sw: 6 * k, fill: \"none\" })));\n  let dots = \"\";\n  pts.forEach(function (q, i) {\n    if (q[0] > reach + 1) return;\n    const at = t0 + (q[0] - box.x) / box.w * (t1 - t0);\n    const pop = $.pop(t, at);\n    dots += $.circle(q[0], q[1], 9 * k * Math.max(0, pop), { fill: c.bg, stroke: c.accent, sw: 4 * k });\n  });\n  s += M.part(\"dots\", { at: t0, in: \"none\", out: \"fade\" }, dots);\n  const last = rows[n - 1], lp = pts[n - 1];\n  const callout = fmtNum(last.value * $.E.out3($.seg(t, t1 - 0.2, t1 + 0.4)), p.decimals, p.prefix, p.suffix);\n  const cfnt = { ...$.font(\"display\"), size: 40 * k };\n  const cw2 = $.measure(fmtNum(last.value, p.decimals, p.prefix, p.suffix), cfnt) + 36 * k, chh = cfnt.size * 1.5;\n  const cx = Math.min(lp[0] - cw2 / 2, A.x + A.w - cw2), cy = lp[1] - chh - 22 * k;\n  s += M.part(\"label\", { at: t1 - 0.1, in: \"pop\", origin: \"bottom\", out: \"fade\", box: { x: cx, y: cy, w: cw2, h: chh } },\n    $.rect(cx, cy, cw2, chh, { r: chh / 2, fill: c.accent }), $.text(callout, { ...cfnt, x: cx + cw2 / 2, y: cy + (chh - cfnt.size) / 2 - cfnt.size * 0.04, anchor: \"middle\", fill: $.on(c.accent) }));\n  return s;\n}\n","libs":[]},{"id":"donut","name":"Donut chart","description":"Segments sweep round a ring one after another, the centre counts up to the total (or the first share), and a legend lists each part.","category":"chart","tags":["chart","donut","pie","share","data"],"placement":"full","duration":{"default":6,"min":3,"max":30},"props":{"title":{"type":"text","label":"Title","default":"Where the hours went","maxLength":50},"rows":{"type":"list","label":"Rows (Label: value)","default":[{"label":"Editing","value":46},{"label":"Rendering","value":12},{"label":"Review","value":18},{"label":"Publishing","value":9}],"maxItems":12,"minItems":2,"of":{"label":"text","value":"number"}},"center":{"type":"choice","label":"Centre shows","options":["total","first share"],"default":"first share"},"centerLabel":{"type":"text","label":"Centre label","default":"on editing","maxLength":20},"prefix":{"type":"text","label":"Before numbers","default":"","maxLength":3},"suffix":{"type":"text","label":"After numbers","default":" h","maxLength":6},"decimals":{"type":"number","label":"Decimals","default":0,"min":0,"max":3}},"parts":{"ground":"the ground","title":"the heading","ring":"the segments","center":"the number in the middle","legend":"the legend"},"beats":{"in":0.15,"out":-0.45},"style":"cream","pack":"data","ref":"data/donut","code":"// MONARC Data — helpers this pack's charts share.\n\n/** Series colours: the brand's two accents first, then fixed hues that sit well beside them. */\nfunction seriesColor($, i) {\n  var list = [$.c.accent, $.c.accent2, \"#3DD6FF\", \"#7C5CFF\", \"#22C55E\", \"#F59E0B\", \"#EC4899\", \"#94A3B8\"];\n  return list[i % list.length];\n}\n\n/** A number as a reader expects it: thousands separators, fixed decimals, a prefix and a suffix. */\nfunction fmtNum(v, dec, prefix, suffix) {\n  var n = Number(v) || 0, d = Math.max(0, Math.min(3, Math.round(dec || 0)));\n  var s = n.toFixed(d).replace(/\\B(?=(\\d{3})+(?!\\d))/g, \",\");\n  if (d) { var parts = n.toFixed(d).split(\".\"); s = parts[0].replace(/\\B(?=(\\d{3})+(?!\\d))/g, \",\") + \".\" + parts[1]; }\n  return (prefix || \"\") + s + (suffix || \"\");\n}\n\n/** Round \"nice\" steps for an axis that reaches max: 1, 2 or 5 × a power of ten. */\nfunction niceStep(max, count) {\n  var raw = (max || 1) / Math.max(1, count || 4), pow = Math.pow(10, Math.floor(Math.log(raw) / Math.LN10)), m = raw / pow;\n  return (m <= 1 ? 1 : m <= 2 ? 2 : m <= 5 ? 5 : 10) * pow;\n}\n\n/** A heading (and kicker) at the top of the safe area; returns the y below it. */\nfunction chartHead($, M, t, at, title, A, k) {\n  if (!title) return { s: \"\", y: A.y };\n  var tf = fitLines($, $.case(title), Object.assign({}, $.font(\"display\"), { ls: -0.02 }), A.w, Math.min((A.tall ? 96 : 80) * k, A.h * 0.1), 40 * k, 2);\n  var s = \"\";\n  tf.lines.forEach(function (line, i) { s += M.text(\"title\", Object.assign({ at: at + i * 0.1, in: \"rise\", distance: 22 }, tf.font, { x: A.x, y: A.y + i * tf.size * 1.05, fill: $.c.fg }), line); });\n  return { s: s, y: A.y + tf.lines.length * tf.size * 1.05 + 40 * k };\n}\n\n/** A donut or ring segment from angle a0 to a1 (radians, 0 = up, clockwise). */\nfunction arcPath(cx, cy, r0, r1, a0, a1) {\n  var big = a1 - a0 > Math.PI ? 1 : 0;\n  var p = function (r, a) { return (cx + r * Math.sin(a)).toFixed(2) + \" \" + (cy - r * Math.cos(a)).toFixed(2); };\n  if (a1 - a0 >= Math.PI * 2 - 1e-4) a1 = a0 + Math.PI * 2 - 1e-4;\n  return \"M\" + p(r1, a0) + \"A\" + r1 + \" \" + r1 + \" 0 \" + big + \" 1 \" + p(r1, a1) + \"L\" + p(r0, a1) + \"A\" + r0 + \" \" + r0 + \" 0 \" + big + \" 0 \" + p(r0, a0) + \"Z\";\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const k = $.k;\n  const tin = $.beat(\"in\", 0.15);\n  let s = M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: W, h: H } }));\n  const head = chartHead($, M, t, tin, p.title, A, k);\n  s += head.s;\n  const rows = p.rows || [];\n  const total = rows.reduce(function (a, r) { return a + Math.max(0, r.value); }, 0) || 1;\n  const room = { x: A.x, y: head.y, w: A.w, h: A.y + A.h - head.y };\n  const side = A.wide;\n  const R = side ? Math.min(room.h * 0.46, room.w * 0.24) : Math.min(room.w * 0.36, room.h * 0.26);\n  const cx = side ? room.x + R + 20 * k : W / 2, cy = side ? room.y + room.h / 2 : room.y + R + 10 * k;\n  const r0 = R * 0.62;\n  // Segments sweep in turn, together taking 1.4 s.\n  const t0 = tin + 0.4;\n  let a = 0, ring = \"\";\n  rows.forEach(function (r, i) {\n    const share = Math.max(0, r.value) / total;\n    const st = t0 + (a / (Math.PI * 2)) * 1.4;\n    const grow = $.E.out3($.seg(t, st, st + share * 1.4 + 0.15));\n    if (grow > 0) ring += $.path(arcPath(cx, cy, r0, R, a + 0.012, a + Math.max(0.0001, share * Math.PI * 2 * grow) - 0.012), { fill: seriesColor($, i) });\n    a += share * Math.PI * 2;\n  });\n  s += M.part(\"ring\", { at: t0 - 0.1, in: \"fade\", dur: 0.2, out: \"fade\", box: { x: cx - R, y: cy - R, w: 2 * R, h: 2 * R } }, $.circle(cx, cy, (R + r0) / 2, { stroke: $.rgba(c.fg, 0.07), sw: R - r0, fill: \"none\" }), ring);\n  const first = rows[0] || { value: 0 };\n  const big = p.center === \"total\" ? fmtNum(total * $.E.out3($.seg(t, t0, t0 + 1.4)), p.decimals, p.prefix, p.suffix) : Math.round((first.value / total) * 100 * $.E.out3($.seg(t, t0, t0 + 1.4))) + \"%\";\n  const bf = fitFont($, p.center === \"total\" ? fmtNum(total, p.decimals, p.prefix, p.suffix) : \"100%\", \"display\", r0 * 0.62, r0 * 1.5);\n  s += M.part(\"center\", { at: t0 + 0.2, in: \"pop\", out: \"fade\", box: { x: cx - r0, y: cy - r0, w: 2 * r0, h: 2 * r0 } },\n    $.text(big, { ...bf, x: cx, y: cy - bf.size * 0.62, anchor: \"middle\", fill: c.fg }),\n    $.text(String(p.centerLabel || \"\"), { ...$.font(\"text\"), size: Math.min(28 * k, r0 * 0.2), x: cx, y: cy + bf.size * 0.45, anchor: \"middle\", fill: c.mute }));\n  // The legend beside the ring on wide frames, under it elsewhere.\n  const lx = side ? cx + R + 80 * k : A.x, ly0 = side ? cy - (rows.length * 70 * k) / 2 : cy + R + 50 * k;\n  const lw = side ? Math.min(A.x + A.w - lx, 680 * k) : A.w;\n  const rowH = Math.min(70 * k, (A.y + A.h - ly0) / Math.max(1, rows.length));\n  const lf = { ...$.font(\"text\"), size: Math.min(34 * k, rowH * 0.5) };\n  rows.forEach(function (r, i) {\n    const y = ly0 + i * rowH;\n    s += M.part(\"legend\", { at: t0 + 0.3 + i * 0.12, in: \"slide\", from: \"left\", distance: 16, out: \"fade\", box: { x: lx, y: y, w: lw, h: rowH } },\n      $.rect(lx, y + rowH * 0.25, rowH * 0.5, rowH * 0.5, { r: rowH * 0.12, fill: seriesColor($, i) }),\n      $.text(String(r.label), { ...lf, x: lx + rowH * 0.8, y: y + (rowH - lf.size) / 2 - lf.size * 0.05, fill: c.fg }),\n      $.text(fmtNum(r.value, p.decimals, p.prefix, p.suffix) + \"  \" + Math.round((r.value / total) * 100) + \"%\", { ...$.font(\"mono\"), size: lf.size * 0.82, x: lx + lw, y: y + (rowH - lf.size * 0.82) / 2 - lf.size * 0.05, anchor: \"end\", fill: c.mute }));\n  });\n  return s;\n}\n","libs":[]},{"id":"bar-race","name":"Bar chart race","description":"Ranked bars grow and overtake each other as the periods advance; the axis rescales and the period counts up in the corner.","category":"chart","tags":["chart","bar race","ranking","time series","data"],"placement":"full","duration":{"default":8,"min":4,"max":60},"props":{"title":{"type":"text","label":"Title","default":"Most-used editing tools","maxLength":50},"periods":{"type":"list","label":"Periods","default":["2022","2023","2024","2025","2026"],"maxItems":12,"minItems":2,"of":"text"},"rows":{"type":"list","label":"Rows (Name: v1, v2, …)","default":[{"label":"MONARC","values":"4, 14, 38, 72, 118"},{"label":"Studio A","values":"60, 66, 70, 71, 72"},{"label":"Cutter","values":"40, 48, 52, 50, 46"},{"label":"Frame.io","values":"22, 30, 41, 47, 51"},{"label":"Clipmate","values":"35, 30, 24, 20, 18"},{"label":"Reelsy","values":"8, 20, 33, 44, 49"}],"maxItems":12,"minItems":2,"of":{"label":"text","values":"text"}},"show":{"type":"number","label":"Bars shown","default":6,"min":3,"max":10},"prefix":{"type":"text","label":"Before numbers","default":"","maxLength":3},"suffix":{"type":"text","label":"After numbers","default":"K","maxLength":6},"decimals":{"type":"number","label":"Decimals","default":0,"min":0,"max":3}},"parts":{"ground":"the ground","title":"the heading","bars":"the bars","period":"the period in the corner"},"beats":{"in":0.15,"out":-0.45},"style":"cream","pack":"data","ref":"data/bar-race","code":"// MONARC Data — helpers this pack's charts share.\n\n/** Series colours: the brand's two accents first, then fixed hues that sit well beside them. */\nfunction seriesColor($, i) {\n  var list = [$.c.accent, $.c.accent2, \"#3DD6FF\", \"#7C5CFF\", \"#22C55E\", \"#F59E0B\", \"#EC4899\", \"#94A3B8\"];\n  return list[i % list.length];\n}\n\n/** A number as a reader expects it: thousands separators, fixed decimals, a prefix and a suffix. */\nfunction fmtNum(v, dec, prefix, suffix) {\n  var n = Number(v) || 0, d = Math.max(0, Math.min(3, Math.round(dec || 0)));\n  var s = n.toFixed(d).replace(/\\B(?=(\\d{3})+(?!\\d))/g, \",\");\n  if (d) { var parts = n.toFixed(d).split(\".\"); s = parts[0].replace(/\\B(?=(\\d{3})+(?!\\d))/g, \",\") + \".\" + parts[1]; }\n  return (prefix || \"\") + s + (suffix || \"\");\n}\n\n/** Round \"nice\" steps for an axis that reaches max: 1, 2 or 5 × a power of ten. */\nfunction niceStep(max, count) {\n  var raw = (max || 1) / Math.max(1, count || 4), pow = Math.pow(10, Math.floor(Math.log(raw) / Math.LN10)), m = raw / pow;\n  return (m <= 1 ? 1 : m <= 2 ? 2 : m <= 5 ? 5 : 10) * pow;\n}\n\n/** A heading (and kicker) at the top of the safe area; returns the y below it. */\nfunction chartHead($, M, t, at, title, A, k) {\n  if (!title) return { s: \"\", y: A.y };\n  var tf = fitLines($, $.case(title), Object.assign({}, $.font(\"display\"), { ls: -0.02 }), A.w, Math.min((A.tall ? 96 : 80) * k, A.h * 0.1), 40 * k, 2);\n  var s = \"\";\n  tf.lines.forEach(function (line, i) { s += M.text(\"title\", Object.assign({ at: at + i * 0.1, in: \"rise\", distance: 22 }, tf.font, { x: A.x, y: A.y + i * tf.size * 1.05, fill: $.c.fg }), line); });\n  return { s: s, y: A.y + tf.lines.length * tf.size * 1.05 + 40 * k };\n}\n\n/** A donut or ring segment from angle a0 to a1 (radians, 0 = up, clockwise). */\nfunction arcPath(cx, cy, r0, r1, a0, a1) {\n  var big = a1 - a0 > Math.PI ? 1 : 0;\n  var p = function (r, a) { return (cx + r * Math.sin(a)).toFixed(2) + \" \" + (cy - r * Math.cos(a)).toFixed(2); };\n  if (a1 - a0 >= Math.PI * 2 - 1e-4) a1 = a0 + Math.PI * 2 - 1e-4;\n  return \"M\" + p(r1, a0) + \"A\" + r1 + \" \" + r1 + \" 0 \" + big + \" 1 \" + p(r1, a1) + \"L\" + p(r0, a1) + \"A\" + r0 + \" \" + r0 + \" 0 \" + big + \" 0 \" + p(r0, a0) + \"Z\";\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const k = $.k;\n  const tin = $.beat(\"in\", 0.15);\n  let s = M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: W, h: H } }));\n  const head = chartHead($, M, t, tin, p.title, A, k);\n  s += head.s;\n  const periods = p.periods || [];\n  const np = periods.length;\n  const rows = (p.rows || []).map(function (r, i) { return { label: String(r.label), i: i, v: String(r.values).split(/[,\\s]+/).filter(Boolean).map(Number).concat(new Array(np).fill(0)).slice(0, np) }; });\n  // The race runs from 0.6 s to 0.8 s before the end; each period gets an equal share.\n  const t0 = tin + 0.6, t1 = Math.max(t0 + 1, $.beat(\"out\", $.dur - 0.45) - 0.4);\n  const pos = Math.max(0, Math.min(np - 1, ((t - t0) / (t1 - t0)) * (np - 1)));\n  const i0 = Math.min(np - 2, Math.floor(pos)), f = pos - i0, fe = $.E.inOut2(f);\n  const valAt = function (r) { return r.v[i0] + (r.v[i0 + 1] - r.v[i0]) * f; };\n  const rankAt = function (j) { const sorted = rows.slice().sort(function (a, b) { return b.v[j] - a.v[j] || a.i - b.i; }); const m = {}; sorted.forEach(function (r, ix) { m[r.i] = ix; }); return m; };\n  const ra = rankAt(i0), rb = rankAt(i0 + 1);\n  const show = Math.min(rows.length, Math.round(p.show));\n  const room = { y: head.y, h: A.y + A.h - head.y - 20 * k };\n  // On wide frames the names sit left of the bars; elsewhere each bar gets a name row above it.\n  const slot = room.h / show, bh = slot * (A.wide ? 0.72 : 0.44);\n  const maxNow = Math.max.apply(null, rows.map(valAt).concat([1e-9]));\n  const lf = { ...$.font(\"display\"), size: Math.min(34 * k, A.wide ? bh * 0.5 : slot * 0.3) };\n  const vf = { ...$.font(\"mono\"), size: Math.min(28 * k, bh * 0.42), weight: 700 };\n  const labelW = A.wide ? Math.min(A.w * 0.22, Math.max.apply(null, rows.map(function (r) { return $.measure(r.label, lf); })) + 24 * k) : 0;\n  const bx = A.x + labelW, bw = A.w - labelW - $.measure(fmtNum(maxNow, p.decimals, p.prefix, p.suffix), vf) - 30 * k;\n  let bars = \"\";\n  rows.forEach(function (r) {\n    const rank = ra[r.i] + (rb[r.i] - ra[r.i]) * fe;\n    if (rank > show - 0.05) return;\n    const y = A.wide ? room.y + rank * slot + (slot - bh) / 2 : room.y + rank * slot + slot * 0.42;\n    const v = valAt(r), w = Math.max(bh * 0.3, (v / maxNow) * bw);\n    const col = seriesColor($, r.i);\n    bars += $.rect(bx, y, w, bh, { r: bh * 0.18, fill: col });\n    if (A.wide) bars += $.text(r.label, { ...lf, x: bx - 18 * k, y: y + (bh - lf.size) / 2 - lf.size * 0.04, anchor: \"end\", fill: c.fg });\n    else bars += $.text(r.label, { ...lf, x: bx, y: y - lf.size * 1.25, fill: c.fg });\n    bars += $.text(fmtNum(v, p.decimals, p.prefix, p.suffix), { ...vf, x: bx + w + 14 * k, y: y + (bh - vf.size) / 2 - vf.size * 0.05, fill: c.fg });\n  });\n  s += M.part(\"bars\", { at: tin + 0.3, in: \"fade\", dur: 0.4, out: \"fade\" }, bars);\n  const pf = { ...$.font(\"display\"), size: Math.min(A.h * 0.18, 200 * k) };\n  const label = String(periods[Math.round(pos)] || \"\");\n  s += M.part(\"period\", { at: tin + 0.4, in: \"fade\", dur: 0.4, out: \"fade\", vary: false }, $.text(label, { ...pf, x: A.x + A.w, y: A.y + A.h - pf.size * 1.05, anchor: \"end\", fill: $.rgba(c.fg, 0.16) }));\n  return s;\n}\n","libs":[]},{"id":"goals","name":"Goal meters","description":"Rows of capsule meters fill to their share of a goal, each with its label and a percentage counting up.","category":"chart","tags":["chart","progress","goal","percent","data"],"placement":"full","duration":{"default":5,"min":3,"max":30},"props":{"title":{"type":"text","label":"Title","default":"Launch checklist","maxLength":50},"rows":{"type":"list","label":"Rows (Label: percent)","default":[{"label":"Footage cut","value":100},{"label":"Captions","value":85},{"label":"Colour grade","value":60},{"label":"Sound mix","value":35}],"maxItems":8,"minItems":1,"of":{"label":"text","value":"number"}}},"parts":{"ground":"the ground","title":"the heading","rows":"each meter"},"beats":{"in":0.15,"out":-0.45},"style":"cream","pack":"data","ref":"data/goals","code":"// MONARC Data — helpers this pack's charts share.\n\n/** Series colours: the brand's two accents first, then fixed hues that sit well beside them. */\nfunction seriesColor($, i) {\n  var list = [$.c.accent, $.c.accent2, \"#3DD6FF\", \"#7C5CFF\", \"#22C55E\", \"#F59E0B\", \"#EC4899\", \"#94A3B8\"];\n  return list[i % list.length];\n}\n\n/** A number as a reader expects it: thousands separators, fixed decimals, a prefix and a suffix. */\nfunction fmtNum(v, dec, prefix, suffix) {\n  var n = Number(v) || 0, d = Math.max(0, Math.min(3, Math.round(dec || 0)));\n  var s = n.toFixed(d).replace(/\\B(?=(\\d{3})+(?!\\d))/g, \",\");\n  if (d) { var parts = n.toFixed(d).split(\".\"); s = parts[0].replace(/\\B(?=(\\d{3})+(?!\\d))/g, \",\") + \".\" + parts[1]; }\n  return (prefix || \"\") + s + (suffix || \"\");\n}\n\n/** Round \"nice\" steps for an axis that reaches max: 1, 2 or 5 × a power of ten. */\nfunction niceStep(max, count) {\n  var raw = (max || 1) / Math.max(1, count || 4), pow = Math.pow(10, Math.floor(Math.log(raw) / Math.LN10)), m = raw / pow;\n  return (m <= 1 ? 1 : m <= 2 ? 2 : m <= 5 ? 5 : 10) * pow;\n}\n\n/** A heading (and kicker) at the top of the safe area; returns the y below it. */\nfunction chartHead($, M, t, at, title, A, k) {\n  if (!title) return { s: \"\", y: A.y };\n  var tf = fitLines($, $.case(title), Object.assign({}, $.font(\"display\"), { ls: -0.02 }), A.w, Math.min((A.tall ? 96 : 80) * k, A.h * 0.1), 40 * k, 2);\n  var s = \"\";\n  tf.lines.forEach(function (line, i) { s += M.text(\"title\", Object.assign({ at: at + i * 0.1, in: \"rise\", distance: 22 }, tf.font, { x: A.x, y: A.y + i * tf.size * 1.05, fill: $.c.fg }), line); });\n  return { s: s, y: A.y + tf.lines.length * tf.size * 1.05 + 40 * k };\n}\n\n/** A donut or ring segment from angle a0 to a1 (radians, 0 = up, clockwise). */\nfunction arcPath(cx, cy, r0, r1, a0, a1) {\n  var big = a1 - a0 > Math.PI ? 1 : 0;\n  var p = function (r, a) { return (cx + r * Math.sin(a)).toFixed(2) + \" \" + (cy - r * Math.cos(a)).toFixed(2); };\n  if (a1 - a0 >= Math.PI * 2 - 1e-4) a1 = a0 + Math.PI * 2 - 1e-4;\n  return \"M\" + p(r1, a0) + \"A\" + r1 + \" \" + r1 + \" 0 \" + big + \" 1 \" + p(r1, a1) + \"L\" + p(r0, a1) + \"A\" + r0 + \" \" + r0 + \" 0 \" + big + \" 0 \" + p(r0, a0) + \"Z\";\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const k = $.k;\n  const tin = $.beat(\"in\", 0.15);\n  let s = M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: W, h: H } }));\n  const head = chartHead($, M, t, tin, p.title, A, k);\n  s += head.s;\n  const rows = p.rows || [];\n  const room = A.y + A.h - head.y;\n  const rowH = Math.min((A.tall ? 190 : 150) * k, room / Math.max(1, rows.length));\n  const total = rows.length * rowH;\n  const w = A.wide ? Math.min(A.w, 1500 * k) : A.w;\n  const x = A.wide ? (W - w) / 2 : A.x;\n  let y = head.y + Math.max(0, (room - total) / 2);\n  const lf = { ...$.font(\"text\"), size: Math.min(36 * k, rowH * 0.26) };\n  const vf = { ...$.font(\"display\"), size: Math.min(40 * k, rowH * 0.28) };\n  rows.forEach(function (r, i) {\n    const at = tin + 0.35 + i * 0.18;\n    const pct = Math.max(0, Math.min(100, r.value));\n    const g = $.E.out3($.seg(t, at, at + 1));\n    const th = rowH * 0.3, ty = y + rowH * 0.42;\n    const done = pct >= 100 && g >= 1;\n    let row = $.text(String(r.label), { ...lf, x: x, y: y + rowH * 0.06, fill: c.fg });\n    row += $.text(Math.round(pct * g) + \"%\", { ...vf, x: x + w, y: y + rowH * 0.04, anchor: \"end\", fill: done ? c.green : c.fg });\n    row += $.rect(x, ty, w, th, { r: th / 2, fill: $.rgba(c.fg, 0.1) });\n    row += $.rect(x, ty, Math.max(th, w * (pct / 100) * g), th, { r: th / 2, fill: done ? c.green : seriesColor($, 0) });\n    if (done) row += $.icon(\"check\", x + w - th * 1.05, ty + th * 0.12, th * 0.76, { color: $.on(c.green), sw: 3 });\n    s += M.part(\"rows\", { at: at - 0.1, in: \"rise\", distance: 20, out: \"fade\", box: { x: x, y: y, w: w, h: rowH } }, row);\n    y += rowH;\n  });\n  return s;\n}\n","libs":[]},{"id":"versus","name":"Versus","description":"Two sides face off: a label and a big number counting up on each, a VS badge popping between them and a split bar settling to their ratio.","category":"chart","tags":["comparison","versus","vs","split","data"],"placement":"full","duration":{"default":5,"min":3,"max":30},"props":{"title":{"type":"text","label":"Title","default":"Hours to publish one video","maxLength":50},"left":{"type":"text","label":"Left","default":"Before","maxLength":20},"leftValue":{"type":"number","label":"Left value","default":9.5,"min":0,"max":1000000000000},"right":{"type":"text","label":"Right","default":"With MONARC","maxLength":20},"rightValue":{"type":"number","label":"Right value","default":1.5,"min":0,"max":1000000000000},"prefix":{"type":"text","label":"Before numbers","default":"","maxLength":3},"suffix":{"type":"text","label":"After numbers","default":" h","maxLength":6},"decimals":{"type":"number","label":"Decimals","default":1,"min":0,"max":3}},"parts":{"ground":"the ground","title":"the heading","left":"the left side","right":"the right side","badge":"the VS badge","split":"the split bar"},"beats":{"in":0.15,"out":-0.45},"style":"cream","pack":"data","ref":"data/versus","code":"// MONARC Data — helpers this pack's charts share.\n\n/** Series colours: the brand's two accents first, then fixed hues that sit well beside them. */\nfunction seriesColor($, i) {\n  var list = [$.c.accent, $.c.accent2, \"#3DD6FF\", \"#7C5CFF\", \"#22C55E\", \"#F59E0B\", \"#EC4899\", \"#94A3B8\"];\n  return list[i % list.length];\n}\n\n/** A number as a reader expects it: thousands separators, fixed decimals, a prefix and a suffix. */\nfunction fmtNum(v, dec, prefix, suffix) {\n  var n = Number(v) || 0, d = Math.max(0, Math.min(3, Math.round(dec || 0)));\n  var s = n.toFixed(d).replace(/\\B(?=(\\d{3})+(?!\\d))/g, \",\");\n  if (d) { var parts = n.toFixed(d).split(\".\"); s = parts[0].replace(/\\B(?=(\\d{3})+(?!\\d))/g, \",\") + \".\" + parts[1]; }\n  return (prefix || \"\") + s + (suffix || \"\");\n}\n\n/** Round \"nice\" steps for an axis that reaches max: 1, 2 or 5 × a power of ten. */\nfunction niceStep(max, count) {\n  var raw = (max || 1) / Math.max(1, count || 4), pow = Math.pow(10, Math.floor(Math.log(raw) / Math.LN10)), m = raw / pow;\n  return (m <= 1 ? 1 : m <= 2 ? 2 : m <= 5 ? 5 : 10) * pow;\n}\n\n/** A heading (and kicker) at the top of the safe area; returns the y below it. */\nfunction chartHead($, M, t, at, title, A, k) {\n  if (!title) return { s: \"\", y: A.y };\n  var tf = fitLines($, $.case(title), Object.assign({}, $.font(\"display\"), { ls: -0.02 }), A.w, Math.min((A.tall ? 96 : 80) * k, A.h * 0.1), 40 * k, 2);\n  var s = \"\";\n  tf.lines.forEach(function (line, i) { s += M.text(\"title\", Object.assign({ at: at + i * 0.1, in: \"rise\", distance: 22 }, tf.font, { x: A.x, y: A.y + i * tf.size * 1.05, fill: $.c.fg }), line); });\n  return { s: s, y: A.y + tf.lines.length * tf.size * 1.05 + 40 * k };\n}\n\n/** A donut or ring segment from angle a0 to a1 (radians, 0 = up, clockwise). */\nfunction arcPath(cx, cy, r0, r1, a0, a1) {\n  var big = a1 - a0 > Math.PI ? 1 : 0;\n  var p = function (r, a) { return (cx + r * Math.sin(a)).toFixed(2) + \" \" + (cy - r * Math.cos(a)).toFixed(2); };\n  if (a1 - a0 >= Math.PI * 2 - 1e-4) a1 = a0 + Math.PI * 2 - 1e-4;\n  return \"M\" + p(r1, a0) + \"A\" + r1 + \" \" + r1 + \" 0 \" + big + \" 1 \" + p(r1, a1) + \"L\" + p(r0, a1) + \"A\" + r0 + \" \" + r0 + \" 0 \" + big + \" 0 \" + p(r0, a0) + \"Z\";\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const k = $.k;\n  const tin = $.beat(\"in\", 0.15);\n  let s = M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: W, h: H } }));\n  const head = chartHead($, M, t, tin, p.title, A, k);\n  s += head.s;\n  // Side by side on wide and square frames, one above the other on tall ones.\n  const stack = A.tall;\n  const room = { x: A.x, y: head.y, w: A.w, h: A.y + A.h - head.y - 110 * k };\n  const half = stack ? { w: room.w, h: room.h / 2 } : { w: room.w / 2, h: room.h };\n  const vfmt = function (v) { return fmtNum(v, p.decimals, p.prefix, p.suffix); };\n  const side = function (name, label, value, col, i) {\n    const bx = stack ? room.x : room.x + i * half.w, by = stack ? room.y + i * half.h : room.y;\n    const nf = fitFont($, vfmt(value), \"display\", Math.min(half.h * 0.42, 220 * k), half.w * 0.86, { ls: -0.03 });\n    const lf = fitFont($, $.case(label), \"text\", 40 * k, half.w * 0.86);\n    const cx = bx + half.w / 2, cy = by + half.h / 2;\n    const num = vfmt(value * $.E.out3($.seg(t, tin + 0.5, tin + 1.6)));\n    return M.part(name, { at: tin + 0.25 + i * 0.15, in: \"rise\", distance: 30, out: \"fade\", box: { x: bx, y: by, w: half.w, h: half.h } },\n      $.text($.case(label), { ...lf, x: cx, y: cy - nf.size * 0.62 - lf.size * 1.4, anchor: \"middle\", fill: c.mute }),\n      $.text(num, { ...nf, x: cx, y: cy - nf.size * 0.55, anchor: \"middle\", fill: col }));\n  };\n  s += side(\"left\", p.left, p.leftValue, c.fg, 0);\n  s += side(\"right\", p.right, p.rightValue, c.accent, 1);\n  const bcx = stack ? W / 2 : room.x + room.w / 2, bcy = stack ? room.y + room.h / 2 : room.y + room.h / 2;\n  const br = 54 * k;\n  s += M.part(\"badge\", { at: tin + 0.7, in: \"slam\", intensity: 0.5, out: \"fade\", box: { x: bcx - br, y: bcy - br, w: 2 * br, h: 2 * br } },\n    stack ? \"\" : $.line(bcx, room.y + 40 * k, bcx, room.y + room.h - 40 * k, { stroke: $.rgba(c.fg, 0.14), sw: 3 * k }),\n    $.circle(bcx, bcy, br, { fill: c.accent2 }), $.text(\"VS\", { ...$.font(\"display\"), size: br * 0.78, x: bcx, y: bcy - br * 0.45, anchor: \"middle\", fill: $.on(c.accent2) }));\n  // The split bar: 50/50, then the true ratio.\n  const tot = (p.leftValue + p.rightValue) || 1;\n  const f = 0.5 + (p.leftValue / tot - 0.5) * $.E.inOut3($.seg(t, tin + 1.1, tin + 2.1));\n  const sy = A.y + A.h - 60 * k, sh = 26 * k;\n  s += M.part(\"split\", { at: tin + 0.9, in: \"grow-x\", origin: \"center\", dur: 0.4, out: \"fade\", box: { x: A.x, y: sy, w: A.w, h: sh } },\n    $.rect(A.x, sy, A.w * f - 4 * k, sh, { r: sh / 2, fill: c.fg, op: 0.85 }), $.rect(A.x + A.w * f + 4 * k, sy, A.w * (1 - f) - 4 * k, sh, { r: sh / 2, fill: c.accent }));\n  return s;\n}\n","libs":[]},{"id":"timeline","name":"Timeline","description":"Milestones along a line that draws itself: each date's dot pops as the line reaches it and its words rise beside it.","category":"list","tags":["timeline","history","milestones","roadmap","data"],"placement":"full","duration":{"default":7,"min":3,"max":40},"props":{"title":{"type":"text","label":"Title","default":"How we got here","maxLength":50},"rows":{"type":"list","label":"Milestones (When: what)","default":[{"when":"2019","what":"First edit in a garage"},{"when":"2021","what":"Studio opens in Lisbon"},{"when":"2023","what":"100 creators on board"},{"when":"2025","what":"The AI editor ships"},{"when":"2026","what":"Volito goes open"}],"maxItems":8,"minItems":2,"of":{"when":"text","what":"text"}}},"parts":{"ground":"the ground","title":"the heading","line":"the line","dots":"the dots","labels":"the dates and words"},"beats":{"in":0.15,"out":-0.45},"style":"cream","pack":"data","ref":"data/timeline","code":"// MONARC Data — helpers this pack's charts share.\n\n/** Series colours: the brand's two accents first, then fixed hues that sit well beside them. */\nfunction seriesColor($, i) {\n  var list = [$.c.accent, $.c.accent2, \"#3DD6FF\", \"#7C5CFF\", \"#22C55E\", \"#F59E0B\", \"#EC4899\", \"#94A3B8\"];\n  return list[i % list.length];\n}\n\n/** A number as a reader expects it: thousands separators, fixed decimals, a prefix and a suffix. */\nfunction fmtNum(v, dec, prefix, suffix) {\n  var n = Number(v) || 0, d = Math.max(0, Math.min(3, Math.round(dec || 0)));\n  var s = n.toFixed(d).replace(/\\B(?=(\\d{3})+(?!\\d))/g, \",\");\n  if (d) { var parts = n.toFixed(d).split(\".\"); s = parts[0].replace(/\\B(?=(\\d{3})+(?!\\d))/g, \",\") + \".\" + parts[1]; }\n  return (prefix || \"\") + s + (suffix || \"\");\n}\n\n/** Round \"nice\" steps for an axis that reaches max: 1, 2 or 5 × a power of ten. */\nfunction niceStep(max, count) {\n  var raw = (max || 1) / Math.max(1, count || 4), pow = Math.pow(10, Math.floor(Math.log(raw) / Math.LN10)), m = raw / pow;\n  return (m <= 1 ? 1 : m <= 2 ? 2 : m <= 5 ? 5 : 10) * pow;\n}\n\n/** A heading (and kicker) at the top of the safe area; returns the y below it. */\nfunction chartHead($, M, t, at, title, A, k) {\n  if (!title) return { s: \"\", y: A.y };\n  var tf = fitLines($, $.case(title), Object.assign({}, $.font(\"display\"), { ls: -0.02 }), A.w, Math.min((A.tall ? 96 : 80) * k, A.h * 0.1), 40 * k, 2);\n  var s = \"\";\n  tf.lines.forEach(function (line, i) { s += M.text(\"title\", Object.assign({ at: at + i * 0.1, in: \"rise\", distance: 22 }, tf.font, { x: A.x, y: A.y + i * tf.size * 1.05, fill: $.c.fg }), line); });\n  return { s: s, y: A.y + tf.lines.length * tf.size * 1.05 + 40 * k };\n}\n\n/** A donut or ring segment from angle a0 to a1 (radians, 0 = up, clockwise). */\nfunction arcPath(cx, cy, r0, r1, a0, a1) {\n  var big = a1 - a0 > Math.PI ? 1 : 0;\n  var p = function (r, a) { return (cx + r * Math.sin(a)).toFixed(2) + \" \" + (cy - r * Math.cos(a)).toFixed(2); };\n  if (a1 - a0 >= Math.PI * 2 - 1e-4) a1 = a0 + Math.PI * 2 - 1e-4;\n  return \"M\" + p(r1, a0) + \"A\" + r1 + \" \" + r1 + \" 0 \" + big + \" 1 \" + p(r1, a1) + \"L\" + p(r0, a1) + \"A\" + r0 + \" \" + r0 + \" 0 \" + big + \" 0 \" + p(r0, a0) + \"Z\";\n}\n\n// Milestones on a line that draws on: each gets a card (its year on an accent tab, what happened under it)\n// joined to its dot; the newest one reached is lit. Cards alternate above and below on wide frames.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const k = $.k;\n  const tin = $.beat(\"in\", 0.15);\n  let s = M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: W, h: H } }));\n  const head = chartHead($, M, t, tin, p.title, A, k);\n  s += head.s;\n  const rows = p.rows || [];\n  const n = rows.length;\n  const horiz = A.wide;\n  const t0 = tin + 0.5, t1 = Math.min(t0 + 0.6 * n + 0.8, $.beat(\"out\", $.dur - 0.45) - 0.5);\n  const run = $.E.inOut2($.seg(t, t0, t1));\n  const yf = Object.assign({}, $.font(\"display\"), { size: (horiz ? 34 : 40) * k });\n  const tf = Object.assign({}, $.font(\"text\"), { size: (horiz ? 27 : 32) * k, lh: 1.25, weight: 600 });\n  const at = function (i) { return t0 + (n > 1 ? i / (n - 1) : 0) * (t1 - t0) * (horiz ? 1 : (n - 1) / n); };\n  // The newest milestone the line has reached is lit; the rest settle back.\n  let cur = -1;\n  rows.forEach(function (r, i) { if (t >= at(i)) cur = i; });\n  let line = \"\", dots = \"\", cards = \"\";\n  const card = function (r, i, x, y, w, align) {\n    const lines = $.wrap(String(r.what), w - 44 * k, tf).slice(0, 3);\n    const tabW = $.measure(String(r.when), yf) + 32 * k, tabH = yf.size * 1.35;\n    const h = tabH + 22 * k + lines.length * tf.size * 1.25 + 22 * k;\n    const lit = i === cur;\n    let m = $.rect(x, y, w, h, { r: 16 * k, fill: lit ? c.surface : $.rgba(c.surface, 0.85), stroke: lit ? c.accent : c.line, sw: (lit ? 3 : 1.5) * k, shadow: lit ? \"md\" : \"sm\" });\n    const tx = align === \"middle\" ? x + w / 2 - tabW / 2 : x + 22 * k;\n    m += $.rect(tx, y + 18 * k, tabW, tabH, { r: tabH / 2, fill: lit ? c.accent : $.rgba(c.accent, 0.14) });\n    m += $.text(String(r.when), Object.assign({}, yf, { x: tx + tabW / 2, y: y + 18 * k + (tabH - yf.size) / 2 - 1 * k, anchor: \"middle\", fill: lit ? $.on(c.accent) : c.accent }));\n    lines.forEach(function (l, j) { m += $.text(l, Object.assign({}, tf, { x: align === \"middle\" ? x + w / 2 : x + 22 * k, y: y + 18 * k + tabH + 18 * k + j * tf.size * 1.25, anchor: align === \"middle\" ? \"middle\" : \"start\", fill: c.fg })); });\n    return { m: m, h: h };\n  };\n  if (horiz) {\n    const ly = head.y + (A.y + A.h - head.y) / 2;\n    const x0 = A.x + 40 * k, x1 = A.x + A.w - 40 * k;\n    line = $.line(x0, ly, x1, ly, { stroke: $.rgba(c.fg, 0.12), sw: 8 * k }) + $.line(x0, ly, x0 + (x1 - x0) * run, ly, { stroke: c.accent, sw: 8 * k });\n    const colW = (x1 - x0) / Math.max(1, n - 1);\n    const cw = Math.min(colW * 1.75, 380 * k);\n    rows.forEach(function (r, i) {\n      const x = n > 1 ? x0 + i * colW : (x0 + x1) / 2;\n      const up = i % 2 === 0;\n      const reached = t >= at(i);\n      dots += $.circle(x, ly, 18 * k * Math.max(0, $.pop(t, at(i))), { fill: i === cur ? c.accent : c.bg, stroke: c.accent, sw: 6 * k });\n      if (i === cur) dots += $.circle(x, ly, 30 * k * (1 + 0.15 * $.wave(t, 1.4)), { stroke: c.accent, sw: 2.5 * k, op: 0.45 });\n      const cd = card(r, i, 0, 0, cw, \"middle\");\n      const cx = Math.max(A.x, Math.min(A.x + A.w - cw, x - cw / 2));\n      const cy = up ? ly - 46 * k - cd.h : ly + 46 * k;\n      const stem = $.line(x, up ? ly - 20 * k : ly + 20 * k, x, up ? cy + cd.h : cy, { stroke: c.accent, sw: 3 * k, p: $.E.out3($.seg(t, at(i), at(i) + 0.25)) });\n      if (reached) cards += stem;\n      cards += M.part(\"labels\", { at: at(i) + 0.05, in: \"rise\", distance: up ? -24 : 24, out: \"fade\", box: { x: cx, y: cy, w: cw, h: cd.h } }, card(r, i, cx, cy, cw, \"middle\").m);\n    });\n  } else {\n    const lx = A.x + 34 * k;\n    const y0 = head.y + 10 * k, y1 = A.y + A.h - 10 * k;\n    const rowH = (y1 - y0) / Math.max(1, n);\n    line = $.line(lx, y0, lx, y1, { stroke: $.rgba(c.fg, 0.12), sw: 8 * k }) + $.line(lx, y0, lx, y0 + (y1 - y0) * run, { stroke: c.accent, sw: 8 * k });\n    const cw = A.w - 90 * k;\n    rows.forEach(function (r, i) {\n      const cd = card(r, i, 0, 0, cw, \"start\");\n      const y = y0 + i * rowH + Math.max(0, (rowH - cd.h) / 2 - 10 * k);\n      dots += $.circle(lx, y + 18 * k + yf.size * 0.67, 18 * k * Math.max(0, $.pop(t, at(i))), { fill: i === cur ? c.accent : c.bg, stroke: c.accent, sw: 6 * k });\n      cards += M.part(\"labels\", { at: at(i) + 0.05, in: \"slide\", from: \"left\", distance: 24, out: \"fade\", box: { x: lx + 56 * k, y: y, w: cw, h: cd.h } }, card(r, i, lx + 56 * k, y, cw, \"start\").m);\n    });\n  }\n  s += M.part(\"line\", { at: t0, in: \"none\", out: \"fade\", vary: false }, line);\n  s += M.part(\"dots\", { at: t0, in: \"none\", out: \"fade\" }, dots);\n  return s + cards;\n}\n","libs":[]},{"id":"funnel","name":"Funnel","description":"Stages stack from widest to narrowest, each dropping in with its count, and the share that carries on to the next stage between them.","category":"chart","tags":["funnel","conversion","pipeline","data"],"placement":"full","duration":{"default":6,"min":3,"max":30},"props":{"title":{"type":"text","label":"Title","default":"From view to sign-up","maxLength":50},"rows":{"type":"list","label":"Stages (Label: count)","default":[{"label":"Watched","value":120000},{"label":"Clicked","value":18400},{"label":"Tried it","value":6200},{"label":"Signed up","value":2480}],"maxItems":7,"minItems":2,"of":{"label":"text","value":"number"}},"prefix":{"type":"text","label":"Before numbers","default":"","maxLength":3},"suffix":{"type":"text","label":"After numbers","default":"","maxLength":6},"decimals":{"type":"number","label":"Decimals","default":0,"min":0,"max":3}},"parts":{"ground":"the ground","title":"the heading","stages":"each stage","rates":"the share between stages"},"beats":{"in":0.15,"out":-0.45},"style":"cream","pack":"data","ref":"data/funnel","code":"// MONARC Data — helpers this pack's charts share.\n\n/** Series colours: the brand's two accents first, then fixed hues that sit well beside them. */\nfunction seriesColor($, i) {\n  var list = [$.c.accent, $.c.accent2, \"#3DD6FF\", \"#7C5CFF\", \"#22C55E\", \"#F59E0B\", \"#EC4899\", \"#94A3B8\"];\n  return list[i % list.length];\n}\n\n/** A number as a reader expects it: thousands separators, fixed decimals, a prefix and a suffix. */\nfunction fmtNum(v, dec, prefix, suffix) {\n  var n = Number(v) || 0, d = Math.max(0, Math.min(3, Math.round(dec || 0)));\n  var s = n.toFixed(d).replace(/\\B(?=(\\d{3})+(?!\\d))/g, \",\");\n  if (d) { var parts = n.toFixed(d).split(\".\"); s = parts[0].replace(/\\B(?=(\\d{3})+(?!\\d))/g, \",\") + \".\" + parts[1]; }\n  return (prefix || \"\") + s + (suffix || \"\");\n}\n\n/** Round \"nice\" steps for an axis that reaches max: 1, 2 or 5 × a power of ten. */\nfunction niceStep(max, count) {\n  var raw = (max || 1) / Math.max(1, count || 4), pow = Math.pow(10, Math.floor(Math.log(raw) / Math.LN10)), m = raw / pow;\n  return (m <= 1 ? 1 : m <= 2 ? 2 : m <= 5 ? 5 : 10) * pow;\n}\n\n/** A heading (and kicker) at the top of the safe area; returns the y below it. */\nfunction chartHead($, M, t, at, title, A, k) {\n  if (!title) return { s: \"\", y: A.y };\n  var tf = fitLines($, $.case(title), Object.assign({}, $.font(\"display\"), { ls: -0.02 }), A.w, Math.min((A.tall ? 96 : 80) * k, A.h * 0.1), 40 * k, 2);\n  var s = \"\";\n  tf.lines.forEach(function (line, i) { s += M.text(\"title\", Object.assign({ at: at + i * 0.1, in: \"rise\", distance: 22 }, tf.font, { x: A.x, y: A.y + i * tf.size * 1.05, fill: $.c.fg }), line); });\n  return { s: s, y: A.y + tf.lines.length * tf.size * 1.05 + 40 * k };\n}\n\n/** A donut or ring segment from angle a0 to a1 (radians, 0 = up, clockwise). */\nfunction arcPath(cx, cy, r0, r1, a0, a1) {\n  var big = a1 - a0 > Math.PI ? 1 : 0;\n  var p = function (r, a) { return (cx + r * Math.sin(a)).toFixed(2) + \" \" + (cy - r * Math.cos(a)).toFixed(2); };\n  if (a1 - a0 >= Math.PI * 2 - 1e-4) a1 = a0 + Math.PI * 2 - 1e-4;\n  return \"M\" + p(r1, a0) + \"A\" + r1 + \" \" + r1 + \" 0 \" + big + \" 1 \" + p(r1, a1) + \"L\" + p(r0, a1) + \"A\" + r0 + \" \" + r0 + \" 0 \" + big + \" 0 \" + p(r0, a0) + \"Z\";\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const k = $.k;\n  const tin = $.beat(\"in\", 0.15);\n  let s = M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: W, h: H } }));\n  const head = chartHead($, M, t, tin, p.title, A, k);\n  s += head.s;\n  const rows = p.rows || [];\n  const n = rows.length;\n  const room = A.y + A.h - head.y;\n  const gap = 40 * k;\n  const sh = Math.min((A.tall ? 200 : 130) * k, (room - gap * (n - 1)) / n);\n  const maxW = A.wide ? A.w * 0.6 : A.w * 0.9, minW = maxW * 0.22;\n  const top = rows[0] ? rows[0].value || 1 : 1;\n  const cx = A.wide ? A.x + maxW / 2 + 20 * k : W / 2;\n  let y = head.y + Math.max(0, (room - (n * sh + (n - 1) * gap)) / 2);\n  const lf = { ...$.font(\"display\"), size: Math.min(38 * k, sh * 0.3) };\n  const vf = { ...$.font(\"mono\"), size: Math.min(32 * k, sh * 0.26), weight: 700 };\n  rows.forEach(function (r, i) {\n    const at = tin + 0.4 + i * 0.25;\n    const w1 = minW + (maxW - minW) * Math.sqrt(r.value / top);\n    const next = rows[i + 1];\n    const w2 = next ? minW + (maxW - minW) * Math.sqrt(next.value / top) : w1 * 0.92;\n    const d = \"M\" + $.fx(cx - w1 / 2) + \" \" + $.fx(y) + \"L\" + $.fx(cx + w1 / 2) + \" \" + $.fx(y) + \"L\" + $.fx(cx + w2 / 2) + \" \" + $.fx(y + sh) + \"L\" + $.fx(cx - w2 / 2) + \" \" + $.fx(y + sh) + \"Z\";\n    // Deeper stages mix toward the ground; their words take whichever ink reads on them.\n    const col = $.mix(seriesColor($, 0), c.bg, i * (0.4 / Math.max(1, n - 1)));\n    let st = $.path(d, { fill: col });\n    const labelInside = A.wide;\n    const val = fmtNum(r.value * $.E.out3($.seg(t, at, at + 0.8)), p.decimals, p.prefix, p.suffix);\n    if (labelInside) {\n      st += $.text(String(r.label), { ...lf, x: cx, y: y + sh / 2 - lf.size * 0.85, anchor: \"middle\", fill: $.on(col) });\n      st += $.text(val, { ...vf, x: cx, y: y + sh / 2 + lf.size * 0.25, anchor: \"middle\", fill: $.on(col) });\n    } else {\n      // The label and its final count, sized to fit the stage's narrow end.\n      const line = String(r.label) + \"  \" + fmtNum(r.value, p.decimals, p.prefix, p.suffix);\n      const ff = fitFont($, line, \"display\", Math.min(lf.size, sh * 0.28), w2 * 0.86);\n      st += $.text(String(r.label) + \"  \" + val, { ...ff, x: cx, y: y + sh / 2 - ff.size * 0.55, anchor: \"middle\", fill: $.on(col) });\n    }\n    s += M.part(\"stages\", { at: at, in: \"drop\", distance: 30, ease: \"bouncy\", out: \"fade\", box: { x: cx - w1 / 2, y: y, w: w1, h: sh } }, st);\n    if (next) {\n      const rate = Math.round((next.value / (r.value || 1)) * 1000) / 10;\n      const rx = A.wide ? cx + maxW / 2 + 60 * k : cx;\n      const ry = y + sh + gap / 2;\n      s += M.part(\"rates\", { at: at + 0.3, in: \"fade\", dur: 0.3, out: \"fade\", box: { x: rx - 80 * k, y: ry - 20 * k, w: 160 * k, h: 40 * k } },\n        A.wide ? $.text(\"↓ \" + rate + \"%\", { ...vf, x: rx, y: ry - vf.size * 0.55, fill: c.mute }) : $.text(rate + \"% carry on\", { ...$.font(\"mono\"), size: 22 * k, x: rx, y: ry - 11 * k, anchor: \"middle\", fill: c.mute }));\n    }\n    y += sh + gap;\n  });\n  return s;\n}\n","libs":[]},{"id":"world-map","name":"World map","description":"A choropleth in the brand's colours: the countries given fill in west to east, shaded on one scale from a pale tint to the accent, the top ones labelled, with a gradient legend and the data's source. Zooms to a region.","category":"chart","tags":["map","world","countries","choropleth","geo"],"placement":"full","libs":["world"],"duration":{"default":6,"min":3,"max":30},"props":{"title":{"type":"text","label":"Title","default":"Where our viewers are","maxLength":50},"subtitle":{"type":"text","label":"Subtitle","default":"Share of watch time, last 90 days","maxLength":80},"countries":{"type":"list","label":"Countries (Name: value)","default":[{"name":"India","value":42},{"name":"United States","value":31},{"name":"Brazil","value":18},{"name":"United Kingdom","value":12},{"name":"Indonesia","value":11},{"name":"Philippines","value":9},{"name":"Germany","value":9},{"name":"Mexico","value":8},{"name":"Nigeria","value":7},{"name":"Canada","value":7},{"name":"Japan","value":6},{"name":"Vietnam","value":6},{"name":"France","value":6},{"name":"Australia","value":5},{"name":"Turkey","value":5},{"name":"South Africa","value":5},{"name":"Spain","value":4},{"name":"Egypt","value":4},{"name":"Argentina","value":4},{"name":"Kenya","value":3}],"maxItems":40,"minItems":1,"of":{"name":"text","value":"number"}},"region":{"type":"choice","label":"Region","options":["world","americas","europe","africa","asia","middle east","oceania","india"],"default":"world"},"labels":{"type":"number","label":"Countries labelled","default":4,"min":0,"max":12},"prefix":{"type":"text","label":"Before numbers","default":"","maxLength":3},"suffix":{"type":"text","label":"After numbers","default":"%","maxLength":6},"decimals":{"type":"number","label":"Decimals","default":0,"min":0,"max":3},"source":{"type":"text","label":"Source","default":"Source: channel analytics","maxLength":90},"legend":{"type":"boolean","label":"Legend","default":true}},"parts":{"ground":"the ground","title":"the heading","map":"the map","highlights":"the chosen countries","labels":"the labels","legend":"the legend and the source"},"beats":{"in":0.15,"out":-0.45},"style":"cream","pack":"data","ref":"data/world-map","code":"// MONARC Data — helpers this pack's charts share.\n\n/** Series colours: the brand's two accents first, then fixed hues that sit well beside them. */\nfunction seriesColor($, i) {\n  var list = [$.c.accent, $.c.accent2, \"#3DD6FF\", \"#7C5CFF\", \"#22C55E\", \"#F59E0B\", \"#EC4899\", \"#94A3B8\"];\n  return list[i % list.length];\n}\n\n/** A number as a reader expects it: thousands separators, fixed decimals, a prefix and a suffix. */\nfunction fmtNum(v, dec, prefix, suffix) {\n  var n = Number(v) || 0, d = Math.max(0, Math.min(3, Math.round(dec || 0)));\n  var s = n.toFixed(d).replace(/\\B(?=(\\d{3})+(?!\\d))/g, \",\");\n  if (d) { var parts = n.toFixed(d).split(\".\"); s = parts[0].replace(/\\B(?=(\\d{3})+(?!\\d))/g, \",\") + \".\" + parts[1]; }\n  return (prefix || \"\") + s + (suffix || \"\");\n}\n\n/** Round \"nice\" steps for an axis that reaches max: 1, 2 or 5 × a power of ten. */\nfunction niceStep(max, count) {\n  var raw = (max || 1) / Math.max(1, count || 4), pow = Math.pow(10, Math.floor(Math.log(raw) / Math.LN10)), m = raw / pow;\n  return (m <= 1 ? 1 : m <= 2 ? 2 : m <= 5 ? 5 : 10) * pow;\n}\n\n/** A heading (and kicker) at the top of the safe area; returns the y below it. */\nfunction chartHead($, M, t, at, title, A, k) {\n  if (!title) return { s: \"\", y: A.y };\n  var tf = fitLines($, $.case(title), Object.assign({}, $.font(\"display\"), { ls: -0.02 }), A.w, Math.min((A.tall ? 96 : 80) * k, A.h * 0.1), 40 * k, 2);\n  var s = \"\";\n  tf.lines.forEach(function (line, i) { s += M.text(\"title\", Object.assign({ at: at + i * 0.1, in: \"rise\", distance: 22 }, tf.font, { x: A.x, y: A.y + i * tf.size * 1.05, fill: $.c.fg }), line); });\n  return { s: s, y: A.y + tf.lines.length * tf.size * 1.05 + 40 * k };\n}\n\n/** A donut or ring segment from angle a0 to a1 (radians, 0 = up, clockwise). */\nfunction arcPath(cx, cy, r0, r1, a0, a1) {\n  var big = a1 - a0 > Math.PI ? 1 : 0;\n  var p = function (r, a) { return (cx + r * Math.sin(a)).toFixed(2) + \" \" + (cy - r * Math.cos(a)).toFixed(2); };\n  if (a1 - a0 >= Math.PI * 2 - 1e-4) a1 = a0 + Math.PI * 2 - 1e-4;\n  return \"M\" + p(r1, a0) + \"A\" + r1 + \" \" + r1 + \" 0 \" + big + \" 1 \" + p(r1, a1) + \"L\" + p(r0, a1) + \"A\" + r0 + \" \" + r0 + \" 0 \" + big + \" 0 \" + p(r0, a0) + \"Z\";\n}\n\nvar REGIONS = { world: [10, 12, 1], americas: [-82, 8, 1.75], europe: [14, 53, 4.2], africa: [18, 2, 2.1], asia: [92, 32, 1.9], \"middle east\": [44, 28, 4.4], oceania: [142, -26, 2.8], india: [80, 22, 5] };\n\n// A choropleth: every country given is shaded on one scale from a pale tint (lowest) to the accent (highest),\n// filling in west to east; the top ones are labelled, with a gradient legend and the data's source beneath.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const k = $.k;\n  const tin = $.beat(\"in\", 0.15);\n  let s = M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: W, h: H } }));\n  const head = chartHead($, M, t, tin, p.title, A, k);\n  s += head.s;\n  let top = head.y;\n  if (p.subtitle) {\n    const sf = Object.assign({}, $.font(\"text\"), { size: (A.tall ? 34 : 30) * k, weight: 500 });\n    s += M.text(\"title\", Object.assign({ at: tin + 0.15, in: \"fade\", dur: 0.4 }, sf, { x: A.x, y: top - 26 * k, fill: c.mute, maxW: A.w }), String(p.subtitle));\n    top += sf.size * 1.1;\n  }\n  // The foot: the source on the left, the legend on the right (stacked on narrow frames).\n  const ff = Object.assign({}, $.font(\"mono\"), { size: 20 * k, ls: 0.02 });\n  const legendW = Math.min(A.w * (A.tall ? 0.7 : 0.32), 380 * k), legendH = 14 * k;\n  const footH = (p.legend !== false ? legendH + ff.size * 1.8 : 0) + (p.source && A.tall ? ff.size * 1.8 : 0);\n  const bottom = A.y + A.h - footH - (footH ? 18 * k : 0);\n  let rg = REGIONS[p.region] || REGIONS.world;\n  // On a tall frame the whole world fits across the width, and a ranked list fills the room below it.\n  const ranked = A.tall && (p.region || \"world\") === \"world\";\n  // On a square frame the whole world is a thin strip: frame the chosen countries instead.\n  if (p.region === \"world\" && !A.wide && !ranked) {\n    const pts = (p.countries || []).map(function (r) { return worldFind(r.name); }).filter(Boolean);\n    if (pts.length) {\n      let sx = 0, sy = 0, lat = 0;\n      pts.forEach(function (cn) { sx += Math.cos(cn.c[0] * Math.PI / 180); sy += Math.sin(cn.c[0] * Math.PI / 180); lat += cn.c[1]; });\n      rg = [Math.atan2(sy, sx) * 180 / Math.PI, lat / pts.length, A.tall ? 2.3 : 1.35];\n    }\n  }\n  // A slow push in while the countries fill.\n  const zoom = rg[2] * (1 + 0.04 * $.E.inOut2($.seg(t, tin, $.dur)));\n  const box = { x: A.x, y: top, w: A.w, h: ranked ? Math.min(bottom - top, A.w * 0.62) : Math.max(80 * k, bottom - top) };\n  const m = worldMap($, { x: box.x, y: box.y, w: box.w, h: box.h, center: [rg[0], rg[1]], zoom: zoom });\n  const picked = (p.countries || []).map(function (r) { return { r: r, c: worldFind(r.name) }; }).filter(function (q) { return q.c; });\n  const vals = picked.map(function (q) { return Number(q.r.value) || 0; });\n  const lo = vals.length ? Math.min.apply(null, vals) : 0, hi = vals.length ? Math.max.apply(null, vals) : 1;\n  const pale = $.mix(c.bg, c.accent, 0.22);\n  const shade = function (v) { return $.mix(pale, c.accent, hi > lo ? (v - lo) / (hi - lo) : 1); };\n  let land = \"\";\n  m.countries.forEach(function (cn) { land += $.path(m.path(cn), { fill: $.rgba(c.fg, 0.08), stroke: c.bg, sw: 0.8 * k }); });\n  const clipId = m.clipId();\n  const boxId = $.uid();\n  $.def('<clipPath id=\"' + boxId + '\"><rect x=\"' + box.x + '\" y=\"' + box.y + '\" width=\"' + box.w + '\" height=\"' + box.h + '\"/></clipPath>');\n  s += '<g clip-path=\"url(#' + boxId + ')\">' + M.part(\"map\", { at: tin + 0.1, in: \"fade\", dur: 0.6, out: \"fade\", vary: false }, '<g clip-path=\"url(#' + clipId + ')\">' + land + \"</g>\");\n  // Countries fill west to east, each easing from the empty land colour to its shade.\n  const sweep = picked.slice().sort(function (a, b) { return a.c.c[0] - b.c.c[0]; });\n  let fills = \"\";\n  sweep.forEach(function (q, i) {\n    const at = tin + 0.6 + i * Math.min(0.12, 1.5 / Math.max(1, sweep.length));\n    const f = $.E.out3($.seg(t, at, at + 0.45));\n    if (f <= 0) return;\n    fills += $.path(m.path(q.c), { fill: shade(Number(q.r.value) || 0), op: f, stroke: c.bg, sw: 0.8 * k });\n  });\n  s += M.part(\"highlights\", { at: tin + 0.5, in: \"none\", out: \"fade\" }, '<g clip-path=\"url(#' + clipId + ')\">' + fills + \"</g>\");\n  // Labels for the top countries: a dot at the country, a card beside it, kept inside the map.\n  const order = picked.slice().sort(function (a, b) { return b.r.value - a.r.value; });\n  let labs = \"\";\n  const lf = Object.assign({}, $.font(\"display\"), { size: 24 * k }), vf = Object.assign({}, $.font(\"mono\"), { size: 21 * k, weight: 700 });\n  const placed = [];\n  order.slice(0, ranked ? 0 : Math.round(p.labels)).forEach(function (q, i) {\n    const at = tin + 1.4 + i * 0.18;\n    const pt = m.project(q.c.c[0], q.c.c[1]);\n    if (pt[0] < box.x || pt[0] > box.x + box.w || pt[1] < box.y || pt[1] > box.y + box.h) return;\n    const name = String(q.r.name), val = fmtNum(q.r.value, p.decimals, p.prefix, p.suffix);\n    const w = $.measure(name, lf) + $.measure(val, vf) + 46 * k, h = lf.size * 1.7;\n    let lx = Math.max(box.x, Math.min(box.x + box.w - w, pt[0] + 18 * k)), ly = Math.max(box.y, Math.min(box.y + box.h - h, pt[1] - h - 16 * k));\n    // A card that would cover one already placed moves below its dot instead.\n    if (placed.some(function (b) { return lx < b.x + b.w && lx + w > b.x && ly < b.y + b.h && ly + h > b.y; })) ly = Math.min(box.y + box.h - h, pt[1] + 16 * k);\n    if (placed.some(function (b) { return lx < b.x + b.w && lx + w > b.x && ly < b.y + b.h && ly + h > b.y; })) return;\n    placed.push({ x: lx, y: ly, w: w, h: h });\n    labs += M.part(\"labels\", { at: at, in: \"pop\", origin: \"bottom-left\", out: \"fade\", box: { x: lx, y: ly, w: w, h: h } },\n      $.circle(pt[0], pt[1], 7 * k, { fill: c.fg, stroke: c.bg, sw: 3 * k }),\n      $.rect(lx, ly, w, h, { r: h / 2, fill: c.surface, stroke: c.line, sw: 1.5 * k }),\n      $.text(name, Object.assign({}, lf, { x: lx + 18 * k, y: ly + (h - lf.size) / 2 - 1 * k, fill: c.fg })),\n      $.text(val, Object.assign({}, vf, { x: lx + w - 18 * k, y: ly + (h - vf.size) / 2, anchor: \"end\", fill: c.accent })));\n  });\n  s += labs + \"</g>\";\n  if (ranked) {\n    // The ranking: the top countries as bars on the same scale, under the map.\n    const rows = order.slice(0, Math.max(3, Math.min(8, Math.round(p.labels) + 2)));\n    const y0 = box.y + box.h + 50 * k, avail = bottom - y0;\n    const rh = Math.min(96 * k, avail / Math.max(1, rows.length));\n    const nf = Object.assign({}, $.font(\"display\"), { size: Math.min(40 * k, rh * 0.42) }), rf = Object.assign({}, $.font(\"mono\"), { size: Math.min(30 * k, rh * 0.34), weight: 700 });\n    const nameW = Math.max.apply(null, rows.map(function (q) { return $.measure(String(q.r.name), nf); })) + 24 * k;\n    const valW = Math.max.apply(null, rows.map(function (q) { return $.measure(fmtNum(q.r.value, p.decimals, p.prefix, p.suffix), rf); })) + 20 * k;\n    const barX = A.x + Math.min(nameW, A.w * 0.42), barW = A.x + A.w - valW - barX;\n    let list = \"\";\n    rows.forEach(function (q, i) {\n      const at = tin + 1.2 + i * 0.12, y = y0 + i * rh;\n      const f = $.E.out3($.seg(t, at, at + 0.6)), v = Number(q.r.value) || 0;\n      list += $.text(String(q.r.name), Object.assign({}, nf, { x: A.x, y: y + (rh * 0.62 - nf.size) / 2, fill: c.fg, op: $.fadeIn(t, at, 0.3) }));\n      list += $.rect(barX, y + rh * 0.62 / 2 - rh * 0.14, Math.max(2 * k, barW * (v / hi) * f), rh * 0.28, { r: rh * 0.14, fill: shade(v) });\n      list += $.text(fmtNum(v, p.decimals, p.prefix, p.suffix), Object.assign({}, rf, { x: A.x + A.w, y: y + (rh * 0.62 - rf.size) / 2, anchor: \"end\", fill: c.accent, op: $.fadeIn(t, at + 0.3, 0.3) }));\n    });\n    s += M.part(\"labels\", { at: tin + 1.1, in: \"none\", out: \"fade\" }, list);\n  }\n  // The foot: where the numbers come from, and the scale they are shaded on.\n  let foot = \"\";\n  const fy = A.y + A.h - (p.legend !== false ? legendH + ff.size * 1.8 : 0);\n  if (p.legend !== false) {\n    const lx = A.tall ? A.x : A.x + A.w - legendW;\n    foot += $.rect(lx, fy, legendW, legendH, { r: legendH / 2, fill: $.gradient([pale, c.accent], { angle: 90 }) });\n    foot += $.text(fmtNum(lo, p.decimals, p.prefix, p.suffix), Object.assign({}, ff, { x: lx, y: fy + legendH + 8 * k, fill: c.mute }));\n    foot += $.text(fmtNum(hi, p.decimals, p.prefix, p.suffix), Object.assign({}, ff, { x: lx + legendW, y: fy + legendH + 8 * k, anchor: \"end\", fill: c.mute }));\n  }\n  if (p.source) {\n    const sy = A.tall ? fy - ff.size * 1.8 : A.y + A.h - ff.size * 1.2;\n    foot += $.text(String(p.source), Object.assign({}, ff, { x: A.x, y: sy, fill: c.mute, maxW: A.tall ? A.w : A.w - legendW - 40 * k }));\n  }\n  return s + M.part(\"legend\", { at: tin + 0.9, in: \"fade\", dur: 0.5, out: \"fade\" }, foot);\n}\n"},{"id":"flight","name":"Flight route","description":"A map frames two places; a route arcs from one to the other along the great circle while a plane flies it, and both ends are pinned and named.","category":"chart","tags":["map","travel","route","flight","geo"],"placement":"full","libs":["world"],"duration":{"default":6,"min":3,"max":30},"props":{"from":{"type":"text","label":"From (name)","default":"Lisbon","maxLength":24},"fromAt":{"type":"text","label":"From (lat, lon or country)","default":"38.72, -9.14","maxLength":32},"to":{"type":"text","label":"To (name)","default":"Mumbai","maxLength":24},"toAt":{"type":"text","label":"To (lat, lon or country)","default":"19.08, 72.88","maxLength":32},"note":{"type":"text","label":"Note","default":"8,154 km · 9 h 40 m","maxLength":32}},"parts":{"ground":"the ground","map":"the map","route":"the route","plane":"the plane","pins":"the two pins and names","note":"the note"},"beats":{"in":0.15,"out":-0.45},"style":"cream","pack":"data","ref":"data/flight","code":"// MONARC Data — helpers this pack's charts share.\n\n/** Series colours: the brand's two accents first, then fixed hues that sit well beside them. */\nfunction seriesColor($, i) {\n  var list = [$.c.accent, $.c.accent2, \"#3DD6FF\", \"#7C5CFF\", \"#22C55E\", \"#F59E0B\", \"#EC4899\", \"#94A3B8\"];\n  return list[i % list.length];\n}\n\n/** A number as a reader expects it: thousands separators, fixed decimals, a prefix and a suffix. */\nfunction fmtNum(v, dec, prefix, suffix) {\n  var n = Number(v) || 0, d = Math.max(0, Math.min(3, Math.round(dec || 0)));\n  var s = n.toFixed(d).replace(/\\B(?=(\\d{3})+(?!\\d))/g, \",\");\n  if (d) { var parts = n.toFixed(d).split(\".\"); s = parts[0].replace(/\\B(?=(\\d{3})+(?!\\d))/g, \",\") + \".\" + parts[1]; }\n  return (prefix || \"\") + s + (suffix || \"\");\n}\n\n/** Round \"nice\" steps for an axis that reaches max: 1, 2 or 5 × a power of ten. */\nfunction niceStep(max, count) {\n  var raw = (max || 1) / Math.max(1, count || 4), pow = Math.pow(10, Math.floor(Math.log(raw) / Math.LN10)), m = raw / pow;\n  return (m <= 1 ? 1 : m <= 2 ? 2 : m <= 5 ? 5 : 10) * pow;\n}\n\n/** A heading (and kicker) at the top of the safe area; returns the y below it. */\nfunction chartHead($, M, t, at, title, A, k) {\n  if (!title) return { s: \"\", y: A.y };\n  var tf = fitLines($, $.case(title), Object.assign({}, $.font(\"display\"), { ls: -0.02 }), A.w, Math.min((A.tall ? 96 : 80) * k, A.h * 0.1), 40 * k, 2);\n  var s = \"\";\n  tf.lines.forEach(function (line, i) { s += M.text(\"title\", Object.assign({ at: at + i * 0.1, in: \"rise\", distance: 22 }, tf.font, { x: A.x, y: A.y + i * tf.size * 1.05, fill: $.c.fg }), line); });\n  return { s: s, y: A.y + tf.lines.length * tf.size * 1.05 + 40 * k };\n}\n\n/** A donut or ring segment from angle a0 to a1 (radians, 0 = up, clockwise). */\nfunction arcPath(cx, cy, r0, r1, a0, a1) {\n  var big = a1 - a0 > Math.PI ? 1 : 0;\n  var p = function (r, a) { return (cx + r * Math.sin(a)).toFixed(2) + \" \" + (cy - r * Math.cos(a)).toFixed(2); };\n  if (a1 - a0 >= Math.PI * 2 - 1e-4) a1 = a0 + Math.PI * 2 - 1e-4;\n  return \"M\" + p(r1, a0) + \"A\" + r1 + \" \" + r1 + \" 0 \" + big + \" 1 \" + p(r1, a1) + \"L\" + p(r0, a1) + \"A\" + r0 + \" \" + r0 + \" 0 \" + big + \" 0 \" + p(r0, a0) + \"Z\";\n}\n\n/** \"38.7, -9.1\" → [lon, lat]; a country name → its label point. */\nfunction placeAt(s) {\n  const m = /^\\s*(-?\\d+(?:\\.\\d+)?)\\s*[, ]\\s*(-?\\d+(?:\\.\\d+)?)\\s*$/.exec(String(s || \"\"));\n  if (m) return [Number(m[2]), Number(m[1])];\n  const cn = worldFind(s);\n  return cn ? cn.c : [0, 0];\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const k = $.k;\n  const tin = $.beat(\"in\", 0.15);\n  let s = M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: W, h: H } }));\n  const a = placeAt(p.fromAt), b = placeAt(p.toAt);\n  // The great circle between them, as points (slerp on the unit sphere).\n  const v = function (q) { const l = q[0] * Math.PI / 180, f = q[1] * Math.PI / 180; return [Math.cos(f) * Math.cos(l), Math.cos(f) * Math.sin(l), Math.sin(f)]; };\n  const va = v(a), vb = v(b);\n  const om = Math.acos(Math.max(-1, Math.min(1, va[0] * vb[0] + va[1] * vb[1] + va[2] * vb[2]))) || 1e-6;\n  const gc = [];\n  for (let i = 0; i <= 64; i++) {\n    const f = i / 64, s1 = Math.sin((1 - f) * om) / Math.sin(om), s2 = Math.sin(f * om) / Math.sin(om);\n    const x = s1 * va[0] + s2 * vb[0], y = s1 * va[1] + s2 * vb[1], z = s1 * va[2] + s2 * vb[2];\n    gc.push([Math.atan2(y, x) * 180 / Math.PI, Math.asin(Math.max(-1, Math.min(1, z))) * 180 / Math.PI]);\n  }\n  // Frame both ends: centre between them (longitudes unwrapped), zoom to their spread.\n  let lb = b[0];\n  while (lb - a[0] > 180) lb -= 360;\n  while (a[0] - lb > 180) lb += 360;\n  const cen = [(a[0] + lb) / 2, (a[1] + b[1]) / 2 + 6];\n  const box = { x: 0, y: 0, w: W, h: H };\n  // Fit: project both ends at zoom 1, then zoom until they span 56% of the width or 45% of the height.\n  const m1 = worldMap($, { x: 0, y: 0, w: W, h: H, center: cen, zoom: 1 });\n  const pa = m1.project(a[0], a[1]), pb = m1.project(b[0], b[1]);\n  const fit = Math.min((W * 0.56) / Math.max(1, Math.abs(pa[0] - pb[0])), (H * 0.45) / Math.max(1, Math.abs(pa[1] - pb[1])));\n  const zoom = Math.min(8, Math.max(1, fit)) * (1 + 0.04 * $.E.inOut2($.seg(t, tin, $.dur)));\n  const m = worldMap($, { x: box.x, y: box.y, w: box.w, h: box.h, center: cen, zoom: zoom });\n  let land = \"\";\n  m.countries.forEach(function (cn) { land += $.path(m.path(cn), { fill: $.rgba(c.fg, 0.14), stroke: c.bg, sw: 0.8 * k }); });\n  s += M.part(\"map\", { at: tin, in: \"fade\", dur: 0.6, out: \"fade\", vary: false }, '<g clip-path=\"url(#' + m.clipId() + ')\">' + land + \"</g>\");\n  // The route: projected points, lifted into an arc, drawn on as the plane flies.\n  const pts = m.ring(gc);\n  const P = pts.map(function (q, i) { const f = i / (pts.length - 1); return [q[0], q[1] - Math.sin(f * Math.PI) * Math.min(220 * k, Math.hypot(pts[pts.length - 1][0] - pts[0][0], pts[pts.length - 1][1] - pts[0][1]) * 0.22)]; });\n  const t0 = tin + 0.9, t1 = $.beat(\"out\", $.dur - 0.45) - 0.9;\n  const run = $.E.inOut2($.seg(t, t0, t1));\n  const upto = run * (P.length - 1), iu = Math.floor(upto), fu = upto - iu;\n  const drawn = P.slice(0, iu + 1);\n  if (iu < P.length - 1) drawn.push([P[iu][0] + (P[iu + 1][0] - P[iu][0]) * fu, P[iu][1] + (P[iu + 1][1] - P[iu][1]) * fu]);\n  const dd = drawn.map(function (q, i) { return (i ? \"L\" : \"M\") + $.fx(q[0]) + \" \" + $.fx(q[1]); }).join(\"\");\n  const full = P.map(function (q, i) { return (i ? \"L\" : \"M\") + $.fx(q[0]) + \" \" + $.fx(q[1]); }).join(\"\");\n  s += M.part(\"route\", { at: t0, in: \"none\", out: \"fade\" }, $.path(full, { stroke: $.rgba(c.fg, 0.2), sw: 3 * k, dash: 10 * k + \" \" + 10 * k, fill: \"none\" }), drawn.length > 1 ? $.path(dd, { stroke: c.accent, sw: 6 * k, fill: \"none\" }) : \"\");\n  // The plane at the tip, pointing along the route.\n  if (run > 0 && run < 1) {\n    const tip = drawn[drawn.length - 1], prev = drawn[Math.max(0, drawn.length - 2)];\n    const ang = Math.atan2(tip[1] - prev[1], tip[0] - prev[0]) * 180 / Math.PI;\n    const sz = 30 * k;\n    const plane = \"M\" + sz + \" 0L\" + -sz * 0.6 + \" \" + -sz * 0.75 + \"L\" + -sz * 0.35 + \" 0L\" + -sz * 0.6 + \" \" + sz * 0.75 + \"Z\";\n    s += M.part(\"plane\", { at: t0, in: \"none\", out: \"fade\" }, $.g({ transform: \"translate(\" + $.fx(tip[0]) + \" \" + $.fx(tip[1]) + \") rotate(\" + $.fx(ang) + \")\" }, $.path(plane, { fill: c.fg, stroke: c.bg, sw: 2 * k })));\n  }\n  // Pins and names at both ends.\n  const nf = { ...$.font(\"display\"), size: 40 * k };\n  // The destination is marked from the start as an open ring; it fills, with a pulse, when the plane lands.\n  const pin = function (q, name, at, right, landAt) {\n    const pr = 13 * k, landed = landAt == null ? 1 : $.E.out3($.seg(t, landAt, landAt + 0.3));\n    let ring = \"\";\n    if (landAt != null && t > landAt) for (let i = 0; i < 2; i++) {\n      const u = $.clamp((t - landAt - i * 0.35) / 1.1);\n      if (u > 0 && u < 1) ring += $.circle(q[0], q[1], pr * (1 + u * 3.2), { stroke: c.accent, sw: 3 * k, op: 0.7 * (1 - u) });\n    }\n    return M.part(\"pins\", { at: at, in: \"pop\", out: \"fade\", box: { x: q[0] - pr, y: q[1] - pr, w: 2 * pr, h: 2 * pr } },\n      ring,\n      $.circle(q[0], q[1], pr, { fill: landed >= 1 ? c.accent : c.bg, stroke: c.accent, sw: 4 * k }),\n      landed > 0 && landed < 1 ? $.circle(q[0], q[1], pr * landed, { fill: c.accent }) : \"\",\n      $.text($.case(String(name)), { ...nf, x: q[0] + (right ? 1 : -1) * 26 * k, y: q[1] - nf.size * 0.55, anchor: right ? \"start\" : \"end\", fill: c.fg, shadow: \"sm\" }));\n  };\n  const ra = P[0], rb = P[P.length - 1];\n  // Names face inward, toward the middle of the frame.\n  s += pin(ra, p.from, tin + 0.5, ra[0] < W / 2);\n  s += pin(rb, p.to, tin + 0.75, rb[0] < W / 2, t1);\n  // A boarding-pass card: from → to, and the note (distance, time) under it.\n  const cf = { ...$.font(\"display\"), size: 34 * k }, mf = { ...$.font(\"mono\"), size: 24 * k, ls: 0.05 };\n  // From and to with a drawn arrow between them (not every face has an arrow glyph).\n  const fromS = $.case(String(p.from)), toS = $.case(String(p.to)), aw = cf.size * 0.8, gap = cf.size * 0.4;\n  const routeW = $.measure(fromS, cf) + gap + aw + gap + $.measure(toS, cf);\n  const cw = Math.min(A.w, Math.max(routeW, p.note ? $.measure(String(p.note), mf) : 0) + 64 * k), ch = 32 * k + cf.size * 1.2 + (p.note ? mf.size * 1.6 : 0);\n  const cx = W / 2 - cw / 2, cy = A.y + A.h - ch;\n  let card = $.rect(cx, cy, cw, ch, { r: 18 * k, fill: c.surface, stroke: c.line, sw: 1.5 * k, shadow: \"md\" });\n  card += $.rect(cx, cy, 10 * k, ch, { r: 4 * k, fill: c.accent });\n  const rx = W / 2 + 5 * k - routeW / 2;\n  card += $.text(fromS, { ...cf, x: rx, y: cy + 16 * k, fill: c.fg });\n  card += $.icon(\"arrowRight\", rx + $.measure(fromS, cf) + gap, cy + 16 * k + cf.size * 0.1, aw, { color: c.accent, sw: 3 * k });\n  card += $.text(toS, { ...cf, x: rx + $.measure(fromS, cf) + gap + aw + gap, y: cy + 16 * k, fill: c.fg });\n  if (p.note) card += $.text(String(p.note), { ...mf, x: W / 2 + 5 * k, y: cy + 16 * k + cf.size * 1.25, anchor: \"middle\", fill: c.mute });\n  s += M.part(\"note\", { at: t0, in: \"rise\", distance: 24, out: \"fade\", box: { x: cx, y: cy, w: cw, h: ch } }, card);\n  return s;\n}\n"},{"id":"heatmap","name":"Activity heatmap","description":"A calendar heatmap: weeks across, days down, each day shaded by how busy it was, filling in week by week under a total that counts up, with a Less–More key.","category":"chart","tags":["chart","heatmap","calendar","activity","streak"],"placement":"full","duration":{"default":6,"min":3,"max":30},"props":{"title":{"type":"text","label":"Title","default":"A year of uploads","maxLength":50},"total":{"type":"number","label":"Total","default":1284,"min":0,"max":1000000000},"label":{"type":"text","label":"Total label","default":"uploads since January","maxLength":60},"weeks":{"type":"number","label":"Weeks","default":26,"min":4,"max":53},"seed":{"type":"number","label":"Pattern seed","default":3,"min":1,"max":999}},"parts":{"ground":"the ground","title":"the heading","total":"the total and its label","cells":"the day cells","key":"the day names and key"},"beats":{"in":0.15,"out":-0.45},"style":"cream","pack":"data","ref":"data/heatmap","code":"// MONARC Data — helpers this pack's charts share.\n\n/** Series colours: the brand's two accents first, then fixed hues that sit well beside them. */\nfunction seriesColor($, i) {\n  var list = [$.c.accent, $.c.accent2, \"#3DD6FF\", \"#7C5CFF\", \"#22C55E\", \"#F59E0B\", \"#EC4899\", \"#94A3B8\"];\n  return list[i % list.length];\n}\n\n/** A number as a reader expects it: thousands separators, fixed decimals, a prefix and a suffix. */\nfunction fmtNum(v, dec, prefix, suffix) {\n  var n = Number(v) || 0, d = Math.max(0, Math.min(3, Math.round(dec || 0)));\n  var s = n.toFixed(d).replace(/\\B(?=(\\d{3})+(?!\\d))/g, \",\");\n  if (d) { var parts = n.toFixed(d).split(\".\"); s = parts[0].replace(/\\B(?=(\\d{3})+(?!\\d))/g, \",\") + \".\" + parts[1]; }\n  return (prefix || \"\") + s + (suffix || \"\");\n}\n\n/** Round \"nice\" steps for an axis that reaches max: 1, 2 or 5 × a power of ten. */\nfunction niceStep(max, count) {\n  var raw = (max || 1) / Math.max(1, count || 4), pow = Math.pow(10, Math.floor(Math.log(raw) / Math.LN10)), m = raw / pow;\n  return (m <= 1 ? 1 : m <= 2 ? 2 : m <= 5 ? 5 : 10) * pow;\n}\n\n/** A heading (and kicker) at the top of the safe area; returns the y below it. */\nfunction chartHead($, M, t, at, title, A, k) {\n  if (!title) return { s: \"\", y: A.y };\n  var tf = fitLines($, $.case(title), Object.assign({}, $.font(\"display\"), { ls: -0.02 }), A.w, Math.min((A.tall ? 96 : 80) * k, A.h * 0.1), 40 * k, 2);\n  var s = \"\";\n  tf.lines.forEach(function (line, i) { s += M.text(\"title\", Object.assign({ at: at + i * 0.1, in: \"rise\", distance: 22 }, tf.font, { x: A.x, y: A.y + i * tf.size * 1.05, fill: $.c.fg }), line); });\n  return { s: s, y: A.y + tf.lines.length * tf.size * 1.05 + 40 * k };\n}\n\n/** A donut or ring segment from angle a0 to a1 (radians, 0 = up, clockwise). */\nfunction arcPath(cx, cy, r0, r1, a0, a1) {\n  var big = a1 - a0 > Math.PI ? 1 : 0;\n  var p = function (r, a) { return (cx + r * Math.sin(a)).toFixed(2) + \" \" + (cy - r * Math.cos(a)).toFixed(2); };\n  if (a1 - a0 >= Math.PI * 2 - 1e-4) a1 = a0 + Math.PI * 2 - 1e-4;\n  return \"M\" + p(r1, a0) + \"A\" + r1 + \" \" + r1 + \" 0 \" + big + \" 1 \" + p(r1, a1) + \"L\" + p(r0, a1) + \"A\" + r0 + \" \" + r0 + \" 0 \" + big + \" 0 \" + p(r0, a0) + \"Z\";\n}\n\n// A calendar heatmap: weeks across, days down, each cell shaded by how busy that day was, filling in week by week,\n// with the total above and a Less … More key beneath.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const k = $.k;\n  const tin = $.beat(\"in\", 0.15);\n  let s = M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: W, h: H } }));\n  const head = chartHead($, M, t, tin, p.title, A, k);\n  s += head.s;\n  // The total: a big number counting up, its label beside or under it.\n  const nf = Object.assign({}, $.font(\"display\"), { size: (A.tall ? 150 : 120) * k, ls: -0.04 });\n  const lf = Object.assign({}, $.font(\"text\"), { size: (A.tall ? 36 : 32) * k, weight: 500 });\n  const total = Number(p.total) || 0;\n  s += M.text(\"total\", Object.assign({ at: tin + 0.2, in: \"rise\", distance: 30 }, nf, { x: A.x, y: head.y - 10 * k, fill: c.accent }), $.counter(t, tin + 0.2, tin + 1.4, total, { dec: 0 }));\n  s += M.text(\"total\", Object.assign({ at: tin + 0.35, in: \"fade\", dur: 0.4 }, lf, { x: A.x, y: head.y - 10 * k + nf.size * 1.02, fill: c.mute, maxW: A.w }), String(p.label || \"\"));\n  // The grid: as many weeks as fit, seven days a week, under the total.\n  // Wide: weeks across, days down. Tall: days across, the most recent weeks down, as many as fit.\n  const turn = A.tall;\n  const dayW = turn ? 0 : 46 * k, dayH = turn ? 40 * k : 0;\n  const gx = A.x + dayW, gy = head.y + nf.size * 1.05 + lf.size * 1.4 + 40 * k + dayH;\n  const room = { w: A.w - dayW, h: A.y + A.h - gy - 70 * k };\n  const gap = 0.18;\n  let weeks = Math.max(4, Math.min(53, Math.round(p.weeks)));\n  let cell;\n  if (turn) {\n    cell = Math.min(room.w / (7 * (1 + gap)), 120 * k);\n    weeks = Math.max(3, Math.min(weeks, Math.floor(room.h / (cell * (1 + gap)))));\n    cell = Math.min(cell, room.h / (weeks * (1 + gap)));\n  } else {\n    cell = Math.min(room.w / (weeks * (1 + gap)), room.h / (7 * (1 + gap)), 64 * k);\n    if (cell < 14 * k) { weeks = Math.max(4, Math.floor(room.w / (14 * k * (1 + gap)))); cell = room.w / (weeks * (1 + gap)); }\n  }\n  const step = cell * (1 + gap), r = cell * 0.22;\n  const gridW = turn ? 7 * step : weeks * step;\n  const ox = turn ? gx + (room.w - gridW) / 2 : gx;\n  const levels = [$.rgba(c.fg, 0.07), $.mix($.mix(c.bg, c.accent, 0.2), c.accent, 0), $.mix(c.bg, c.accent, 0.45), $.mix(c.bg, c.accent, 0.72), c.accent];\n  const seed = Number(p.seed) || 1;\n  let cells = \"\";\n  for (let w = 0; w < weeks; w++) {\n    const at = tin + 0.5 + (w / weeks) * 1.6;\n    const f = $.E.outBack($.seg(t, at, at + 0.35));\n    if (f <= 0) continue;\n    for (let d = 0; d < 7; d++) {\n      // Busier on weekdays and later in the range: a fixed pattern from the seed.\n      const v = $.hash(seed * 97.3 + w * 7.1 + d * 1.37) * (d === 0 || d === 6 ? 0.6 : 1) * (0.55 + 0.45 * (w / weeks)) + 0.08 * $.hash(seed + w);\n      const lvl = v < 0.18 ? 0 : v < 0.38 ? 1 : v < 0.56 ? 2 : v < 0.74 ? 3 : 4;\n      const cx = ox + (turn ? d : w) * step + cell / 2, cy = gy + (turn ? w : d) * step + cell / 2, sz = cell * Math.max(0, f);\n      cells += $.rect(cx - sz / 2, cy - sz / 2, sz, sz, { r: r, fill: levels[lvl] });\n    }\n  }\n  s += M.part(\"cells\", { at: tin + 0.5, in: \"none\", out: \"fade\" }, cells);\n  // Day names down the left, and the key under the grid.\n  const df = Object.assign({}, $.font(\"mono\"), { size: Math.min(20 * k, cell * 0.55), fill: c.mute });\n  let side = \"\";\n  if (turn) [\"M\", \"T\", \"W\", \"T\", \"F\", \"S\", \"S\"].forEach(function (dn, i) { side += $.text(dn, Object.assign({}, df, { x: ox + i * step + cell / 2, y: gy - dayH, anchor: \"middle\" })); });\n  else [\"Mon\", \"Wed\", \"Fri\"].forEach(function (dn, i) { side += $.text(dn, Object.assign({}, df, { x: A.x, y: gy + (1 + i * 2) * step + (cell - df.size) / 2 })); });\n  const ky = gy + (turn ? weeks : 7) * step + 24 * k, kc = Math.min(cell, 26 * k);\n  const kx = ox + gridW - cell * 0.18 - 5 * kc * 1.25 - $.measure(\"More\", df) - 12 * k;\n  side += $.text(\"Less\", Object.assign({}, df, { x: kx - 12 * k, y: ky + (kc - df.size) / 2, anchor: \"end\" }));\n  levels.forEach(function (col, i) { side += $.rect(kx + i * kc * 1.25, ky, kc, kc, { r: kc * 0.22, fill: col }); });\n  side += $.text(\"More\", Object.assign({}, df, { x: kx + 5 * kc * 1.25 + 4 * k, y: ky + (kc - df.size) / 2 }));\n  s += M.part(\"key\", { at: tin + 0.6, in: \"fade\", dur: 0.5, out: \"fade\" }, side);\n  return s;\n}\n","libs":[]},{"id":"gauge","name":"Gauge","description":"A half-dial gauge: the needle swings to the value with a little overshoot, the arc fills behind it and the number counts up in the middle, with the label under it.","category":"stat","tags":["gauge","dial","meter","score","kpi"],"placement":"full","duration":{"default":6,"min":3,"max":30},"props":{"title":{"type":"text","label":"Title","default":"Audience retention","maxLength":50},"value":{"type":"number","label":"Value","default":72,"min":0,"max":1000000000},"max":{"type":"number","label":"Maximum","default":100,"min":1,"max":1000000000},"label":{"type":"text","label":"Label","default":"watched past the first minute","maxLength":60},"prefix":{"type":"text","label":"Before numbers","default":"","maxLength":3},"suffix":{"type":"text","label":"After numbers","default":"%","maxLength":6},"decimals":{"type":"number","label":"Decimals","default":0,"min":0,"max":3},"zones":{"type":"boolean","label":"Good zone","default":true}},"parts":{"ground":"the ground","title":"the heading","dial":"the dial, ticks and zone","needle":"the needle","value":"the number","label":"the label"},"beats":{"in":0.15,"out":-0.45},"style":"cream","pack":"data","ref":"data/gauge","code":"// MONARC Data — helpers this pack's charts share.\n\n/** Series colours: the brand's two accents first, then fixed hues that sit well beside them. */\nfunction seriesColor($, i) {\n  var list = [$.c.accent, $.c.accent2, \"#3DD6FF\", \"#7C5CFF\", \"#22C55E\", \"#F59E0B\", \"#EC4899\", \"#94A3B8\"];\n  return list[i % list.length];\n}\n\n/** A number as a reader expects it: thousands separators, fixed decimals, a prefix and a suffix. */\nfunction fmtNum(v, dec, prefix, suffix) {\n  var n = Number(v) || 0, d = Math.max(0, Math.min(3, Math.round(dec || 0)));\n  var s = n.toFixed(d).replace(/\\B(?=(\\d{3})+(?!\\d))/g, \",\");\n  if (d) { var parts = n.toFixed(d).split(\".\"); s = parts[0].replace(/\\B(?=(\\d{3})+(?!\\d))/g, \",\") + \".\" + parts[1]; }\n  return (prefix || \"\") + s + (suffix || \"\");\n}\n\n/** Round \"nice\" steps for an axis that reaches max: 1, 2 or 5 × a power of ten. */\nfunction niceStep(max, count) {\n  var raw = (max || 1) / Math.max(1, count || 4), pow = Math.pow(10, Math.floor(Math.log(raw) / Math.LN10)), m = raw / pow;\n  return (m <= 1 ? 1 : m <= 2 ? 2 : m <= 5 ? 5 : 10) * pow;\n}\n\n/** A heading (and kicker) at the top of the safe area; returns the y below it. */\nfunction chartHead($, M, t, at, title, A, k) {\n  if (!title) return { s: \"\", y: A.y };\n  var tf = fitLines($, $.case(title), Object.assign({}, $.font(\"display\"), { ls: -0.02 }), A.w, Math.min((A.tall ? 96 : 80) * k, A.h * 0.1), 40 * k, 2);\n  var s = \"\";\n  tf.lines.forEach(function (line, i) { s += M.text(\"title\", Object.assign({ at: at + i * 0.1, in: \"rise\", distance: 22 }, tf.font, { x: A.x, y: A.y + i * tf.size * 1.05, fill: $.c.fg }), line); });\n  return { s: s, y: A.y + tf.lines.length * tf.size * 1.05 + 40 * k };\n}\n\n/** A donut or ring segment from angle a0 to a1 (radians, 0 = up, clockwise). */\nfunction arcPath(cx, cy, r0, r1, a0, a1) {\n  var big = a1 - a0 > Math.PI ? 1 : 0;\n  var p = function (r, a) { return (cx + r * Math.sin(a)).toFixed(2) + \" \" + (cy - r * Math.cos(a)).toFixed(2); };\n  if (a1 - a0 >= Math.PI * 2 - 1e-4) a1 = a0 + Math.PI * 2 - 1e-4;\n  return \"M\" + p(r1, a0) + \"A\" + r1 + \" \" + r1 + \" 0 \" + big + \" 1 \" + p(r1, a1) + \"L\" + p(r0, a1) + \"A\" + r0 + \" \" + r0 + \" 0 \" + big + \" 0 \" + p(r0, a0) + \"Z\";\n}\n\n// A half-dial gauge: the track, tick marks, a filled arc and a needle that swings to the value with a little\n// overshoot, the number counting up in the middle and the label under it.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const k = $.k;\n  const tin = $.beat(\"in\", 0.15);\n  let s = M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: W, h: H } }));\n  const head = chartHead($, M, t, tin, p.title, A, k);\n  s += head.s;\n  const max = Number(p.max) || 100, val = Math.max(0, Math.min(max, Number(p.value) || 0));\n  const lf = Object.assign({}, $.font(\"text\"), { size: (A.tall ? 40 : 34) * k, weight: 600 });\n  const roomH = A.y + A.h - head.y;\n  // The dial, then the number under its pivot, then the label: one block, centred in the room left.\n  const R = Math.min(A.w * (A.tall ? 0.47 : 0.4), roomH * 0.55);\n  const blockH = R + R * 0.36 * 1.1 + 70 * k + lf.size * 1.4;\n  const cx = W / 2, cy = head.y + Math.max(0, (roomH - blockH) / 2) + R;\n  const sw = R * 0.16, a0 = -Math.PI / 2, a1 = Math.PI / 2;\n  // The needle springs to its angle; the arc and the number follow it.\n  const sp = Math.max(0, $.pop(t, tin + 0.7, 1.6, 5.5));\n  const f = (val / max) * sp;\n  const ang = a0 + (a1 - a0) * Math.min(1.04, f);\n  let dial = $.path(arcPath(cx, cy, R - sw, R, a0, a1), { fill: $.rgba(c.fg, 0.1) });\n  // Zones: the last third in the accent's tint, so \"good\" reads at a glance.\n  if (p.zones !== false) dial += $.path(arcPath(cx, cy, R + sw * 0.35, R + sw * 0.6, a0 + (a1 - a0) * 0.66, a1), { fill: $.rgba(c.accent, 0.35) });\n  if (f > 0.001) dial += $.path(arcPath(cx, cy, R - sw, R, a0, a0 + (a1 - a0) * Math.min(1, f)), { fill: c.accent });\n  for (let i = 0; i <= 10; i++) {\n    const a = a0 + (a1 - a0) * (i / 10), long = i % 5 === 0;\n    const r0 = R - sw - (long ? 30 : 18) * k, r1 = R - sw - 8 * k;\n    dial += $.line(cx + r0 * Math.sin(a), cy - r0 * Math.cos(a), cx + r1 * Math.sin(a), cy - r1 * Math.cos(a), { stroke: $.rgba(c.fg, long ? 0.5 : 0.25), sw: (long ? 4 : 2.5) * k });\n  }\n  const mf = Object.assign({}, $.font(\"mono\"), { size: 24 * k, fill: c.mute });\n  dial += $.text(fmtNum(0, p.decimals, p.prefix, p.suffix), Object.assign({}, mf, { x: cx - R + sw / 2, y: cy + 14 * k, anchor: \"middle\" }));\n  dial += $.text(fmtNum(max, p.decimals, p.prefix, p.suffix), Object.assign({}, mf, { x: cx + R - sw / 2, y: cy + 14 * k, anchor: \"middle\" }));\n  s += M.part(\"dial\", { at: tin + 0.2, in: \"pop\", out: \"fade\", box: { x: cx - R, y: cy - R, w: 2 * R, h: R } }, dial);\n  const nl = R - sw - 46 * k;\n  const needle = $.path(\"M\" + $.fx(cx + nl * Math.sin(ang)) + \" \" + $.fx(cy - nl * Math.cos(ang)) + \"L\" + $.fx(cx + 12 * k * Math.cos(ang)) + \" \" + $.fx(cy + 12 * k * Math.sin(ang)) + \"L\" + $.fx(cx - 12 * k * Math.cos(ang)) + \" \" + $.fx(cy - 12 * k * Math.sin(ang)) + \"Z\", { fill: c.fg });\n  s += M.part(\"needle\", { at: tin + 0.6, in: \"fade\", dur: 0.2, out: \"fade\" }, needle + $.circle(cx, cy, 20 * k, { fill: c.fg }) + $.circle(cx, cy, 8 * k, { fill: c.bg }));\n  const nf = Object.assign({}, $.font(\"display\"), { size: R * 0.36, ls: -0.04 });\n  const shown = val * Math.min(1, sp);\n  s += M.text(\"value\", Object.assign({ at: tin + 0.7, in: \"rise\", distance: 20 }, nf, { x: cx, y: cy + 50 * k, anchor: \"middle\", fill: c.fg }), fmtNum(shown, p.decimals, p.prefix, p.suffix));\n  if (p.label) s += M.text(\"label\", Object.assign({ at: tin + 0.9, in: \"rise\", distance: 16 }, lf, { x: cx, y: cy + 60 * k + nf.size * 1.1, anchor: \"middle\", fill: c.mute, maxW: A.w }), String(p.label));\n  return s;\n}\n","libs":[]},{"id":"kpi-grid","name":"KPI tiles","description":"Key numbers as tiles: each label, its value counting up, a change chip (up in green, down in red) and a small trend line — four across, two by two or stacked.","category":"stat","tags":["kpi","dashboard","metrics","tiles","numbers"],"placement":"full","duration":{"default":6,"min":3,"max":30},"props":{"title":{"type":"text","label":"Title","default":"This month","maxLength":50},"rows":{"type":"list","label":"Tiles (Label: value)","default":[{"label":"Views","value":1240000,"suffix":"","delta":18},{"label":"Watch hours","value":48200,"suffix":"","delta":9},{"label":"Click-through","value":6.4,"suffix":"%","delta":-2},{"label":"New subscribers","value":3120,"suffix":"","delta":24}],"maxItems":8,"minItems":1,"of":{"label":"text","value":"number","suffix":"text","delta":"number"}}},"parts":{"ground":"the ground","title":"the heading","tiles":"each tile"},"beats":{"in":0.15,"out":-0.45},"style":"cream","pack":"data","ref":"data/kpi-grid","code":"// MONARC Data — helpers this pack's charts share.\n\n/** Series colours: the brand's two accents first, then fixed hues that sit well beside them. */\nfunction seriesColor($, i) {\n  var list = [$.c.accent, $.c.accent2, \"#3DD6FF\", \"#7C5CFF\", \"#22C55E\", \"#F59E0B\", \"#EC4899\", \"#94A3B8\"];\n  return list[i % list.length];\n}\n\n/** A number as a reader expects it: thousands separators, fixed decimals, a prefix and a suffix. */\nfunction fmtNum(v, dec, prefix, suffix) {\n  var n = Number(v) || 0, d = Math.max(0, Math.min(3, Math.round(dec || 0)));\n  var s = n.toFixed(d).replace(/\\B(?=(\\d{3})+(?!\\d))/g, \",\");\n  if (d) { var parts = n.toFixed(d).split(\".\"); s = parts[0].replace(/\\B(?=(\\d{3})+(?!\\d))/g, \",\") + \".\" + parts[1]; }\n  return (prefix || \"\") + s + (suffix || \"\");\n}\n\n/** Round \"nice\" steps for an axis that reaches max: 1, 2 or 5 × a power of ten. */\nfunction niceStep(max, count) {\n  var raw = (max || 1) / Math.max(1, count || 4), pow = Math.pow(10, Math.floor(Math.log(raw) / Math.LN10)), m = raw / pow;\n  return (m <= 1 ? 1 : m <= 2 ? 2 : m <= 5 ? 5 : 10) * pow;\n}\n\n/** A heading (and kicker) at the top of the safe area; returns the y below it. */\nfunction chartHead($, M, t, at, title, A, k) {\n  if (!title) return { s: \"\", y: A.y };\n  var tf = fitLines($, $.case(title), Object.assign({}, $.font(\"display\"), { ls: -0.02 }), A.w, Math.min((A.tall ? 96 : 80) * k, A.h * 0.1), 40 * k, 2);\n  var s = \"\";\n  tf.lines.forEach(function (line, i) { s += M.text(\"title\", Object.assign({ at: at + i * 0.1, in: \"rise\", distance: 22 }, tf.font, { x: A.x, y: A.y + i * tf.size * 1.05, fill: $.c.fg }), line); });\n  return { s: s, y: A.y + tf.lines.length * tf.size * 1.05 + 40 * k };\n}\n\n/** A donut or ring segment from angle a0 to a1 (radians, 0 = up, clockwise). */\nfunction arcPath(cx, cy, r0, r1, a0, a1) {\n  var big = a1 - a0 > Math.PI ? 1 : 0;\n  var p = function (r, a) { return (cx + r * Math.sin(a)).toFixed(2) + \" \" + (cy - r * Math.cos(a)).toFixed(2); };\n  if (a1 - a0 >= Math.PI * 2 - 1e-4) a1 = a0 + Math.PI * 2 - 1e-4;\n  return \"M\" + p(r1, a0) + \"A\" + r1 + \" \" + r1 + \" 0 \" + big + \" 1 \" + p(r1, a1) + \"L\" + p(r0, a1) + \"A\" + r0 + \" \" + r0 + \" 0 \" + big + \" 0 \" + p(r0, a0) + \"Z\";\n}\n\n// Key numbers as tiles: each label, its value counting up, a change chip (up in green, down in red) and a small\n// trend line. Four across on wide frames, two by two on square ones, stacked on tall ones.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const k = $.k;\n  const tin = $.beat(\"in\", 0.15);\n  let s = M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: W, h: H } }));\n  const head = chartHead($, M, t, tin, p.title, A, k);\n  s += head.s;\n  const rows = (p.rows || []).slice(0, 8);\n  const n = Math.max(1, rows.length);\n  const cols = A.wide ? Math.min(n, 4) : A.tall ? 1 : 2;\n  const lines = Math.ceil(n / cols);\n  const gap = 28 * k;\n  const tw = (A.w - gap * (cols - 1)) / cols;\n  const th = Math.min((A.y + A.h - head.y - gap * (lines - 1)) / lines, A.tall ? 300 * k : 420 * k);\n  const top = head.y + Math.max(0, (A.y + A.h - head.y - (th * lines + gap * (lines - 1))) / 2);\n  let tiles = \"\";\n  rows.forEach(function (r, i) {\n    const col = i % cols, row = Math.floor(i / cols);\n    const x = A.x + col * (tw + gap), y = top + row * (th + gap);\n    const at = tin + 0.3 + i * 0.12;\n    const pad = Math.min(36 * k, tw * 0.09);\n    const lf = Object.assign({}, $.font(\"text\"), { size: Math.min(30 * k, th * 0.12), weight: 600 });\n    const val = Number(r.value) || 0, d = Number(r.delta) || 0;\n    const big = fitFont($, fmtNum(val, 0, \"\", r.suffix || \"\"), \"display\", Math.min(th * 0.34, 130 * k), tw * (A.tall ? 0.5 : 0.9) - pad * 2, { ls: -0.03 });\n    let m = $.rect(x, y, tw, th, { r: 22 * k, fill: c.surface, stroke: c.line, sw: 1.5 * k, shadow: \"sm\" });\n    m += $.text(String(r.label), Object.assign({}, lf, { x: x + pad, y: y + pad, fill: c.mute, maxW: tw - pad * 2 }));\n    const dec = val < 10 && val % 1 ? 1 : 0;\n    m += $.text($.counter(t, at + 0.1, at + 1.2, val, { dec: dec, suffix: r.suffix || \"\" }), Object.assign({}, big, { x: x + pad, y: y + pad + lf.size * 1.5, fill: c.fg }));\n    // The change: an arrow and a percentage on a chip, green up, red down.\n    const up = d >= 0, chipCol = up ? c.green : c.red;\n    const cf = Object.assign({}, $.font(\"mono\"), { size: Math.min(24 * k, th * 0.1), weight: 700 });\n    const ctext = (up ? \"+\" : \"−\") + Math.abs(d) + \"%\";\n    const cw = $.measure(ctext, cf) + cf.size * 2.2, chh = cf.size * 1.7;\n    const cy = y + th - pad - chh;\n    m += $.rect(x + pad, cy, cw, chh, { r: chh / 2, fill: $.rgba(chipCol, 0.14) });\n    m += $.path(\"M\" + $.fx(x + pad + cf.size * 0.5) + \" \" + $.fx(cy + chh / 2 + (up ? 0.25 : -0.25) * cf.size) + \"L\" + $.fx(x + pad + cf.size * 0.85) + \" \" + $.fx(cy + chh / 2 + (up ? -0.3 : 0.3) * cf.size) + \"L\" + $.fx(x + pad + cf.size * 1.2) + \" \" + $.fx(cy + chh / 2 + (up ? 0.25 : -0.25) * cf.size) + \"Z\", { fill: chipCol });\n    m += $.text(ctext, Object.assign({}, cf, { x: x + pad + cf.size * 1.5, y: cy + (chh - cf.size) / 2, fill: chipCol }));\n    // A small trend line in the tile's corner, rising or falling with the change.\n    const sx = x + tw - pad - Math.min(tw * 0.42, 220 * k), sx1 = x + tw - pad, sy0 = cy - th * 0.05, sh = Math.min(th * 0.28, 90 * k);\n    const pts = [];\n    for (let j = 0; j <= 9; j++) {\n      const trend = up ? j / 9 : 1 - j / 9;\n      pts.push([sx + (sx1 - sx) * (j / 9), sy0 + chh - sh * (0.15 + 0.7 * trend + 0.15 * ($.hash(i * 13 + j) - 0.5))]);\n    }\n    m += $.path($.smooth(pts), { stroke: up ? c.accent : c.red, sw: 4 * k, draw: $.E.out3($.seg(t, at + 0.4, at + 1.3)) });\n    tiles += M.part(\"tiles\", { at: at, in: \"rise\", distance: 30, out: \"fade\", box: { x: x, y: y, w: tw, h: th } }, m);\n  });\n  return s + tiles;\n}\n","libs":[]},{"id":"waffle","name":"Waffle chart","description":"A ten-by-ten waffle chart where each cell is one percent, filling in to the value beside the number counting up and what it measures. Squares, dots or people.","category":"chart","tags":["chart","waffle","percent","share","grid"],"placement":"full","duration":{"default":6,"min":3,"max":30},"props":{"title":{"type":"text","label":"Title","default":"Who stays to the end","maxLength":50},"value":{"type":"number","label":"Percent","default":68,"min":0,"max":100},"label":{"type":"text","label":"Label","default":"of viewers watch the whole video","maxLength":70},"shape":{"type":"choice","label":"Cells","options":["square","dot","person"],"default":"square"}},"parts":{"ground":"the ground","title":"the heading","grid":"the grid","value":"the number","label":"the label"},"beats":{"in":0.15,"out":-0.45},"style":"cream","pack":"data","ref":"data/waffle","code":"// MONARC Data — helpers this pack's charts share.\n\n/** Series colours: the brand's two accents first, then fixed hues that sit well beside them. */\nfunction seriesColor($, i) {\n  var list = [$.c.accent, $.c.accent2, \"#3DD6FF\", \"#7C5CFF\", \"#22C55E\", \"#F59E0B\", \"#EC4899\", \"#94A3B8\"];\n  return list[i % list.length];\n}\n\n/** A number as a reader expects it: thousands separators, fixed decimals, a prefix and a suffix. */\nfunction fmtNum(v, dec, prefix, suffix) {\n  var n = Number(v) || 0, d = Math.max(0, Math.min(3, Math.round(dec || 0)));\n  var s = n.toFixed(d).replace(/\\B(?=(\\d{3})+(?!\\d))/g, \",\");\n  if (d) { var parts = n.toFixed(d).split(\".\"); s = parts[0].replace(/\\B(?=(\\d{3})+(?!\\d))/g, \",\") + \".\" + parts[1]; }\n  return (prefix || \"\") + s + (suffix || \"\");\n}\n\n/** Round \"nice\" steps for an axis that reaches max: 1, 2 or 5 × a power of ten. */\nfunction niceStep(max, count) {\n  var raw = (max || 1) / Math.max(1, count || 4), pow = Math.pow(10, Math.floor(Math.log(raw) / Math.LN10)), m = raw / pow;\n  return (m <= 1 ? 1 : m <= 2 ? 2 : m <= 5 ? 5 : 10) * pow;\n}\n\n/** A heading (and kicker) at the top of the safe area; returns the y below it. */\nfunction chartHead($, M, t, at, title, A, k) {\n  if (!title) return { s: \"\", y: A.y };\n  var tf = fitLines($, $.case(title), Object.assign({}, $.font(\"display\"), { ls: -0.02 }), A.w, Math.min((A.tall ? 96 : 80) * k, A.h * 0.1), 40 * k, 2);\n  var s = \"\";\n  tf.lines.forEach(function (line, i) { s += M.text(\"title\", Object.assign({ at: at + i * 0.1, in: \"rise\", distance: 22 }, tf.font, { x: A.x, y: A.y + i * tf.size * 1.05, fill: $.c.fg }), line); });\n  return { s: s, y: A.y + tf.lines.length * tf.size * 1.05 + 40 * k };\n}\n\n/** A donut or ring segment from angle a0 to a1 (radians, 0 = up, clockwise). */\nfunction arcPath(cx, cy, r0, r1, a0, a1) {\n  var big = a1 - a0 > Math.PI ? 1 : 0;\n  var p = function (r, a) { return (cx + r * Math.sin(a)).toFixed(2) + \" \" + (cy - r * Math.cos(a)).toFixed(2); };\n  if (a1 - a0 >= Math.PI * 2 - 1e-4) a1 = a0 + Math.PI * 2 - 1e-4;\n  return \"M\" + p(r1, a0) + \"A\" + r1 + \" \" + r1 + \" 0 \" + big + \" 1 \" + p(r1, a1) + \"L\" + p(r0, a1) + \"A\" + r0 + \" \" + r0 + \" 0 \" + big + \" 0 \" + p(r0, a0) + \"Z\";\n}\n\n// A waffle chart: a ten-by-ten grid where each cell is one percent, filling in up to the value, beside the\n// number counting up and what it measures.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const k = $.k;\n  const tin = $.beat(\"in\", 0.15);\n  let s = M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: W, h: H } }));\n  const head = chartHead($, M, t, tin, p.title, A, k);\n  s += head.s;\n  const val = Math.max(0, Math.min(100, Math.round(Number(p.value) || 0)));\n  const side = A.wide;\n  const roomH = A.y + A.h - head.y;\n  const gridS = side ? Math.min(roomH, A.w * 0.48) : Math.min(A.w, roomH * 0.62);\n  const gx = side ? A.x : W / 2 - gridS / 2, gy = side ? head.y + (roomH - gridS) / 2 : head.y;\n  const cell = gridS / 10, inner = cell * 0.82;\n  const shape = p.shape || \"square\";\n  const filled = Math.round(val * $.E.inOut2($.seg(t, tin + 0.6, tin + 2.2)));\n  let g = \"\";\n  for (let i = 0; i < 100; i++) {\n    // Fill from the bottom-left, row by row.\n    const col = i % 10, row = 9 - Math.floor(i / 10);\n    const x = gx + col * cell + (cell - inner) / 2, y = gy + row * cell + (cell - inner) / 2;\n    const on = i < filled;\n    const pop = on ? 1 + 0.25 * (1 - $.E.out3($.clamp((filled - i) / 6))) : 1;\n    const fill = on ? c.accent : $.rgba(c.fg, 0.1);\n    const ox = x + inner / 2, oy = y + inner / 2;\n    let m;\n    if (shape === \"dot\") m = $.circle(ox, oy, inner / 2, { fill: fill });\n    else if (shape === \"person\") m = $.icon(\"user\", x, y, inner, { color: fill, fill: true, sw: 2 * k });\n    else m = $.rect(x, y, inner, inner, { r: inner * 0.18, fill: fill });\n    g += pop !== 1 ? $.g({ transform: $.tf({ s: pop, ox: ox, oy: oy }) }, m) : m;\n  }\n  s += M.part(\"grid\", { at: tin + 0.3, in: \"fade\", dur: 0.4, out: \"fade\" }, g);\n  // The number and its label: beside the grid on wide frames, under it otherwise.\n  const tx = side ? A.x + gridS + A.w * 0.06 : W / 2, ty = side ? head.y + roomH * 0.28 : gy + gridS + 50 * k;\n  const nf = fitFont($, \"100%\", \"display\", Math.min(260 * k, side ? A.h * 0.4 : roomH * 0.2), side ? A.w - gridS - A.w * 0.06 : A.w, { ls: -0.05 });\n  const anchor = side ? \"start\" : \"middle\";\n  s += M.text(\"value\", Object.assign({ at: tin + 0.6, in: \"rise\", distance: 30 }, nf, { x: tx, y: ty, anchor: anchor, fill: c.accent }), filled + \"%\");\n  const lf = Object.assign({}, $.font(\"text\"), { size: (A.tall ? 40 : 36) * k, weight: 600, lh: 1.25 });\n  if (p.label) s += M.text(\"label\", Object.assign({ at: tin + 0.8, in: \"rise\", distance: 16 }, lf, { x: tx, y: ty + nf.size * 1.02, anchor: anchor, fill: c.fg, maxW: side ? A.w - gridS - A.w * 0.06 : A.w }), String(p.label));\n  return s;\n}\n","libs":[]},{"id":"leaderboard","name":"Leaderboard","description":"A leaderboard: ranked rows slide in, each with its place, an initials badge, the name, a bar and the score counting up, and how many places it moved; the leader wears a crown.","category":"list","tags":["leaderboard","ranking","scores","top","list"],"placement":"full","duration":{"default":6,"min":3,"max":30},"props":{"title":{"type":"text","label":"Title","default":"Top creators this week","maxLength":50},"rows":{"type":"list","label":"Rows (Name: score)","default":[{"name":"Ana Ruiz","score":9820,"change":2},{"name":"Kenji Mori","score":9410,"change":-1},{"name":"Lea Novak","score":8760,"change":1},{"name":"Sam Okafor","score":8125,"change":0},{"name":"Priya Das","score":7690,"change":-2}],"maxItems":10,"minItems":1,"of":{"name":"text","score":"number","change":"number"}},"suffix":{"type":"text","label":"After scores","default":"pts","maxLength":6}},"parts":{"ground":"the ground","title":"the heading","rows":"each row"},"beats":{"in":0.15,"out":-0.45},"style":"cream","pack":"data","ref":"data/leaderboard","code":"// MONARC Data — helpers this pack's charts share.\n\n/** Series colours: the brand's two accents first, then fixed hues that sit well beside them. */\nfunction seriesColor($, i) {\n  var list = [$.c.accent, $.c.accent2, \"#3DD6FF\", \"#7C5CFF\", \"#22C55E\", \"#F59E0B\", \"#EC4899\", \"#94A3B8\"];\n  return list[i % list.length];\n}\n\n/** A number as a reader expects it: thousands separators, fixed decimals, a prefix and a suffix. */\nfunction fmtNum(v, dec, prefix, suffix) {\n  var n = Number(v) || 0, d = Math.max(0, Math.min(3, Math.round(dec || 0)));\n  var s = n.toFixed(d).replace(/\\B(?=(\\d{3})+(?!\\d))/g, \",\");\n  if (d) { var parts = n.toFixed(d).split(\".\"); s = parts[0].replace(/\\B(?=(\\d{3})+(?!\\d))/g, \",\") + \".\" + parts[1]; }\n  return (prefix || \"\") + s + (suffix || \"\");\n}\n\n/** Round \"nice\" steps for an axis that reaches max: 1, 2 or 5 × a power of ten. */\nfunction niceStep(max, count) {\n  var raw = (max || 1) / Math.max(1, count || 4), pow = Math.pow(10, Math.floor(Math.log(raw) / Math.LN10)), m = raw / pow;\n  return (m <= 1 ? 1 : m <= 2 ? 2 : m <= 5 ? 5 : 10) * pow;\n}\n\n/** A heading (and kicker) at the top of the safe area; returns the y below it. */\nfunction chartHead($, M, t, at, title, A, k) {\n  if (!title) return { s: \"\", y: A.y };\n  var tf = fitLines($, $.case(title), Object.assign({}, $.font(\"display\"), { ls: -0.02 }), A.w, Math.min((A.tall ? 96 : 80) * k, A.h * 0.1), 40 * k, 2);\n  var s = \"\";\n  tf.lines.forEach(function (line, i) { s += M.text(\"title\", Object.assign({ at: at + i * 0.1, in: \"rise\", distance: 22 }, tf.font, { x: A.x, y: A.y + i * tf.size * 1.05, fill: $.c.fg }), line); });\n  return { s: s, y: A.y + tf.lines.length * tf.size * 1.05 + 40 * k };\n}\n\n/** A donut or ring segment from angle a0 to a1 (radians, 0 = up, clockwise). */\nfunction arcPath(cx, cy, r0, r1, a0, a1) {\n  var big = a1 - a0 > Math.PI ? 1 : 0;\n  var p = function (r, a) { return (cx + r * Math.sin(a)).toFixed(2) + \" \" + (cy - r * Math.cos(a)).toFixed(2); };\n  if (a1 - a0 >= Math.PI * 2 - 1e-4) a1 = a0 + Math.PI * 2 - 1e-4;\n  return \"M\" + p(r1, a0) + \"A\" + r1 + \" \" + r1 + \" 0 \" + big + \" 1 \" + p(r1, a1) + \"L\" + p(r0, a1) + \"A\" + r0 + \" \" + r0 + \" 0 \" + big + \" 0 \" + p(r0, a0) + \"Z\";\n}\n\n// A leaderboard: ranked rows slide in, each with its place, a coloured initials badge, the name, a bar and the\n// score counting up, and how many places it moved; the leader gets a crown.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const k = $.k;\n  const tin = $.beat(\"in\", 0.15);\n  let s = M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: W, h: H } }));\n  const head = chartHead($, M, t, tin, p.title, A, k);\n  s += head.s;\n  const rows = (p.rows || []).slice(0, 10).slice().sort(function (a, b) { return (Number(b.score) || 0) - (Number(a.score) || 0); });\n  const n = Math.max(1, rows.length);\n  const avail = A.y + A.h - head.y;\n  const rh = Math.min(avail / n, (A.tall ? 150 : 120) * k);\n  const top = head.y + (avail - rh * n) / 2;\n  const maxS = Math.max.apply(null, rows.map(function (r) { return Number(r.score) || 0; }).concat([1]));\n  const nf = Object.assign({}, $.font(\"display\"), { size: Math.min(44 * k, rh * 0.36) });\n  const rf = Object.assign({}, $.font(\"mono\"), { size: Math.min(36 * k, rh * 0.3), weight: 700 });\n  const nameW = Math.min(A.w * 0.34, Math.max.apply(null, rows.map(function (r) { return $.measure(String(r.name), nf); })) + 20 * k);\n  let out = \"\";\n  rows.forEach(function (r, i) {\n    const at = tin + 0.3 + i * 0.12, y = top + i * rh, mid = y + rh / 2;\n    const lead = i === 0;\n    let m = $.rect(A.x, y + rh * 0.08, A.w, rh * 0.84, { r: rh * 0.2, fill: lead ? $.rgba(c.accent, 0.12) : $.rgba(c.fg, 0.04) });\n    m += $.text(String(i + 1), Object.assign({}, rf, { x: A.x + rh * 0.42, y: mid - rf.size * 0.55, anchor: \"middle\", fill: lead ? c.accent : c.mute }));\n    const ar = rh * 0.28, ax = A.x + rh * 0.95;\n    m += $.circle(ax, mid, ar, { fill: seriesColor($, i) });\n    m += $.text(initials(String(r.name)), Object.assign({}, $.font(\"text\"), { size: ar * 0.8, weight: 700, x: ax, y: mid - ar * 0.45, anchor: \"middle\", fill: $.on(seriesColor($, i)) }));\n    if (lead) m += $.icon(\"crown\", ax - ar * 0.6, mid - ar * 2.05, ar * 1.2, { color: c.accent, fill: true });\n    const nx = ax + ar + 22 * k;\n    m += $.text(String(r.name), Object.assign({}, nf, { x: nx, y: mid - nf.size * 0.55, fill: c.fg, maxW: nameW }));\n    // The score bar and number, counting up together.\n    const sc = Number(r.score) || 0, f = $.E.out3($.seg(t, at + 0.15, at + 1.1));\n    const valW = $.measure(fmtNum(maxS, 0, \"\", p.suffix ? \" \" + p.suffix : \"\"), rf) + 24 * k;\n    const chW = 64 * k;\n    const bx = nx + nameW + 20 * k, bw = Math.max(0, A.x + A.w - valW - chW - 30 * k - bx);\n    m += $.rect(bx, mid - rh * 0.07, bw, rh * 0.14, { r: rh * 0.07, fill: $.rgba(c.fg, 0.08) });\n    m += $.rect(bx, mid - rh * 0.07, bw * (sc / maxS) * f, rh * 0.14, { r: rh * 0.07, fill: lead ? c.accent : $.mix(c.bg, c.accent, 0.55) });\n    m += $.text(fmtNum(Math.round(sc * f), 0, \"\", p.suffix ? \" \" + p.suffix : \"\"), Object.assign({}, rf, { x: bx + bw + valW, y: mid - rf.size * 0.55, anchor: \"end\", fill: c.fg }));\n    // Places moved since last time.\n    const mv = Number(r.change) || 0;\n    if (mv) {\n      const up = mv > 0, col = up ? c.green : c.red, cx = A.x + A.w - chW / 2 - 6 * k, sz = rf.size * 0.42;\n      m += $.path(\"M\" + $.fx(cx - sz * 1.2) + \" \" + $.fx(mid + (up ? 0.2 : -0.2) * sz) + \"L\" + $.fx(cx - sz * 0.6) + \" \" + $.fx(mid + (up ? -0.7 : 0.7) * sz) + \"L\" + $.fx(cx) + \" \" + $.fx(mid + (up ? 0.2 : -0.2) * sz) + \"Z\", { fill: col });\n      m += $.text(String(Math.abs(mv)), Object.assign({}, rf, { size: rf.size * 0.8, x: cx + sz * 0.4, y: mid - rf.size * 0.42, fill: col }));\n    }\n    out += M.part(\"rows\", { at: at, in: \"slide\", from: \"left\", distance: 40, out: \"fade\", box: { x: A.x, y: y, w: A.w, h: rh } }, m);\n  });\n  return s + out;\n}\n","libs":[]}]},{"format":1,"id":"3d","name":"MONARC 3D","version":"1.0.0","description":"Extruded logos, titles and numbers, a card that flips, a word cube, a 3D bar chart, a globe, an exploded isometric stack and isometric blocks — real depth, light and camera moves, rendered without a GPU, in any brand.","author":"MONARC STUDIOS","license":"MONARC Packs License 1.0 (free to use in any video; not for resale as templates)","homepage":"https://volito.dev/docs/packs/3d","style":"ember","shared":"shared.js","items":["logo-3d","title-3d","number-3d","card-flip","cube","bars-3d","globe","exploded-stack","iso-blocks"],"entries":[{"id":"logo-3d","name":"3D logo","description":"An extruded wordmark swings in on a spring and settles as a light sweeps across its faces; the tagline rises beneath. Real depth, light and perspective.","category":"logo","tags":["3d","logo","sting","intro","brand","extruded"],"placement":"full","libs":["3d"],"duration":{"default":4,"min":2.5,"max":20},"props":{"wordmark":{"type":"text","label":"Wordmark","default":"MONARC","maxLength":16},"tagline":{"type":"text","label":"Tagline","default":"Made with motion","maxLength":40},"depth":{"type":"number","label":"Depth (share of the height)","default":0.3,"min":0.05,"max":1}},"parts":{"ground":"the ground","wordmark":"the 3D wordmark","tagline":"the tagline"},"beats":{"in":0.1,"out":-0.45},"style":"ember","pack":"3d","ref":"3d/logo-3d","code":"// MONARC 3D — helpers this pack's items share. The scenes are built with scene3d (the 3D kit).\n\n/** The focal length a scene3d camera with this field of view has on this canvas. */\nfunction focalFor($, fov) {\n  return ($.H / 2) / Math.tan(((fov || 35) * Math.PI) / 360);\n}\n\n/** Lines of 3D text no wider than maxW (px at the target depth), by words. */\nfunction wrap3d(S, str, o, maxW, maxLines) {\n  var words = String(str || \"\").split(/\\s+/).filter(Boolean), lines = [], cur = \"\";\n  words.forEach(function (w) {\n    var next = cur ? cur + \" \" + w : w;\n    if (cur && S.measure(next, o) > maxW) { lines.push(cur); cur = w; } else cur = next;\n  });\n  if (cur) lines.push(cur);\n  if (maxLines && lines.length > maxLines) { var rest = lines.slice(maxLines - 1).join(\" \"); lines = lines.slice(0, maxLines - 1).concat([rest]); }\n  return lines;\n}\n\n/** The biggest size (≤ max) at which str fits maxW on one line, in 3D text. */\nfunction fit3d(S, str, o, maxW, max) {\n  var w = S.measure(str, Object.assign({}, o, { size: 100 }));\n  return w > 0 ? Math.min(max, (maxW / w) * 100) : max;\n}\n\n/** A soft contact shadow on the floor under a point (2D, drawn before the scene). */\nfunction floorShadow($, S, p, rx, op) {\n  var q = S.project(p);\n  if (!q) return \"\";\n  var id = $.uid(\"fs\");\n  $.def('<radialGradient id=\"' + id + '\"><stop offset=\"0\" stop-color=\"#000\" stop-opacity=\"' + (op || 0.45) + '\"/><stop offset=\"1\" stop-color=\"#000\" stop-opacity=\"0\"/></radialGradient>');\n  return '<ellipse cx=\"' + $.fx(q.x) + '\" cy=\"' + $.fx(q.y) + '\" rx=\"' + $.fx(rx * q.s) + '\" ry=\"' + $.fx(rx * q.s * 0.18) + '\" fill=\"url(#' + id + ')\"/>';\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const k = $.k;\n  const tin = $.beat(\"in\", 0.1);\n  let s = M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: W, h: H }, glow: [W / 2, H / 2] }));\n  const word = $.case(String(p.wordmark || \"\"));\n  const face = $.font(\"display\");\n  const cy = A.y + A.h * 0.46;\n  const e = $.spring(Math.max(0, t - tin), 2.1, 6.2);\n  // The light sweeps right across the letters as they settle.\n  const sweep = $.seg(t, tin + 0.45, tin + 1.5);\n  const lx = -1.4 + 2.8 * $.E.inOut2(sweep);\n  const S = scene3d($, { camera: { fov: 30, yaw: -7 + 14 * $.seg(t, tin, $.dur), pitch: -5, y: cy }, light: { dir: [lx, 0.4, 0.85], spec: 0.2 + 0.7 * Math.sin(Math.PI * sweep), shine: 14 } });\n  const size = fit3d(S, word, { face: face }, A.w * 0.82, Math.min(A.h * 0.3, 300 * k));\n  const mesh = S.text(word, { face: face, size: size, depth: size * p.depth, color: c.fg, side: c.accent });\n  S.add(mesh, { rotate: [10 * (1 - e), -78 * (1 - e), 0], scale: 0.88 + 0.12 * e });\n  const shadow = floorShadow($, S, [0, size * 0.62, 0], mesh.w * 0.55, 0.4 * Math.min(1, e));\n  s += M.part(\"wordmark\", { at: tin, in: \"fade\", dur: 0.2, out: \"zoom\", box: { x: W / 2 - mesh.w / 2, y: cy - size, w: mesh.w, h: size * 2 } }, shadow + S.render());\n  if (p.tagline) {\n    const tf = fitFont($, String(p.tagline).toUpperCase(), \"mono\", 34 * k, A.w * 0.8, { ls: 0.24 });\n    s += M.text(\"tagline\", { at: tin + 1.1, in: \"rise\", distance: 18, out: \"fade\", ...tf, x: W / 2, y: cy + size * 0.75, anchor: \"middle\", fill: c.mute }, String(p.tagline).toUpperCase());\n  }\n  return s;\n}\n"},{"id":"title-3d","name":"3D title","description":"Extruded letters flip up one by one on springs while the camera eases in; the subtitle rises beneath.","category":"title","tags":["3d","title","chapter","kinetic","extruded"],"placement":"full","libs":["3d"],"duration":{"default":4.5,"min":2.5,"max":20},"props":{"title":{"type":"text","label":"Title","default":"Depth over reach","maxLength":28},"subtitle":{"type":"text","label":"Subtitle","default":"Chapter two","maxLength":40},"depth":{"type":"number","label":"Depth (share of the height)","default":0.22,"min":0.05,"max":1}},"parts":{"ground":"the ground","title":"the 3D title","subtitle":"the subtitle"},"beats":{"in":0.1,"out":-0.45},"style":"ember","pack":"3d","ref":"3d/title-3d","code":"// MONARC 3D — helpers this pack's items share. The scenes are built with scene3d (the 3D kit).\n\n/** The focal length a scene3d camera with this field of view has on this canvas. */\nfunction focalFor($, fov) {\n  return ($.H / 2) / Math.tan(((fov || 35) * Math.PI) / 360);\n}\n\n/** Lines of 3D text no wider than maxW (px at the target depth), by words. */\nfunction wrap3d(S, str, o, maxW, maxLines) {\n  var words = String(str || \"\").split(/\\s+/).filter(Boolean), lines = [], cur = \"\";\n  words.forEach(function (w) {\n    var next = cur ? cur + \" \" + w : w;\n    if (cur && S.measure(next, o) > maxW) { lines.push(cur); cur = w; } else cur = next;\n  });\n  if (cur) lines.push(cur);\n  if (maxLines && lines.length > maxLines) { var rest = lines.slice(maxLines - 1).join(\" \"); lines = lines.slice(0, maxLines - 1).concat([rest]); }\n  return lines;\n}\n\n/** The biggest size (≤ max) at which str fits maxW on one line, in 3D text. */\nfunction fit3d(S, str, o, maxW, max) {\n  var w = S.measure(str, Object.assign({}, o, { size: 100 }));\n  return w > 0 ? Math.min(max, (maxW / w) * 100) : max;\n}\n\n/** A soft contact shadow on the floor under a point (2D, drawn before the scene). */\nfunction floorShadow($, S, p, rx, op) {\n  var q = S.project(p);\n  if (!q) return \"\";\n  var id = $.uid(\"fs\");\n  $.def('<radialGradient id=\"' + id + '\"><stop offset=\"0\" stop-color=\"#000\" stop-opacity=\"' + (op || 0.45) + '\"/><stop offset=\"1\" stop-color=\"#000\" stop-opacity=\"0\"/></radialGradient>');\n  return '<ellipse cx=\"' + $.fx(q.x) + '\" cy=\"' + $.fx(q.y) + '\" rx=\"' + $.fx(rx * q.s) + '\" ry=\"' + $.fx(rx * q.s * 0.18) + '\" fill=\"url(#' + id + ')\"/>';\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const k = $.k;\n  const tin = $.beat(\"in\", 0.1);\n  let s = M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: W, h: H }, glow: [W * 0.3, H * 0.3] }));\n  const face = $.font(\"display\");\n  const title = $.case(String(p.title || \"\"));\n  const cy = A.y + A.h * 0.45;\n  const fov = 32, f = focalFor($, fov);\n  const S = scene3d($, { camera: { fov: fov, distance: f * (1.18 - 0.18 * $.E.out3($.seg(t, tin, $.dur))), pitch: -8, y: cy }, light: { spec: 0.35 } });\n  // One line when it holds at a good size, else two.\n  let lines = [title];\n  let size = fit3d(S, title, { face: face }, A.w * 0.86, Math.min(A.h * 0.24, 260 * k));\n  if (size < Math.min(A.h * 0.24, 260 * k) * 0.62) {\n    lines = wrap3d(S, title, { face: face, size: 100 }, (A.w * 0.86 * 100) / (Math.min(A.h * 0.2, 220 * k)), 2);\n    size = Math.min.apply(null, lines.map(function (l) { return fit3d(S, l, { face: face }, A.w * 0.86, Math.min(A.h * 0.2, 220 * k)); }));\n  }\n  let n0 = 0;\n  lines.forEach(function (line, li) {\n    const base = n0;\n    const mesh = S.text(line, { face: face, size: size, depth: size * p.depth, color: c.fg, side: c.accent, each: function (i) {\n      const at = tin + 0.25 + (base + i) * 0.055;\n      if (t < at) return { hide: true };\n      const e = $.spring(t - at, 2.4, 6.8);\n      return { rotate: [-100 * (1 - e), 0, 0], offset: [0, size * 0.25 * (1 - Math.min(1, e)), 0] };\n    } });\n    S.add(mesh, { position: [0, (li - (lines.length - 1) / 2) * size * 1.12, 0] });\n    n0 += Array.from(line).length;\n  });\n  s += M.part(\"title\", { at: tin, in: \"fade\", dur: 0.2, out: \"blur\", box: { x: A.x, y: cy - size * lines.length, w: A.w, h: size * lines.length * 2 } }, S.render());\n  if (p.subtitle) {\n    const sf = fitFont($, $.case(String(p.subtitle)), \"text\", 46 * k, A.w * 0.8);\n    s += M.text(\"subtitle\", { at: tin + 0.4 + n0 * 0.055, in: \"rise\", distance: 20, out: \"fade\", ...sf, x: W / 2, y: cy + size * (lines.length * 0.56 + 0.35), anchor: \"middle\", fill: c.mute }, $.case(String(p.subtitle)));\n  }\n  return s;\n}\n"},{"id":"number-3d","name":"3D number","description":"A big extruded number counts up, tilted toward the camera, with a light sweep when it lands and the label beneath.","category":"stat","tags":["3d","number","stat","counter","extruded"],"placement":"full","libs":["3d"],"duration":{"default":4,"min":2.5,"max":20},"props":{"value":{"type":"number","label":"Value","default":3.2,"min":-1000000000000,"max":1000000000000},"decimals":{"type":"number","label":"Decimals","default":1,"min":0,"max":3},"prefix":{"type":"text","label":"Before","default":"","maxLength":3},"suffix":{"type":"text","label":"After","default":"×","maxLength":4},"label":{"type":"text","label":"Label","default":"more videos shipped","maxLength":48}},"parts":{"ground":"the ground","number":"the 3D number","label":"the label"},"beats":{"in":0.1,"out":-0.45},"style":"ember","pack":"3d","ref":"3d/number-3d","code":"// MONARC 3D — helpers this pack's items share. The scenes are built with scene3d (the 3D kit).\n\n/** The focal length a scene3d camera with this field of view has on this canvas. */\nfunction focalFor($, fov) {\n  return ($.H / 2) / Math.tan(((fov || 35) * Math.PI) / 360);\n}\n\n/** Lines of 3D text no wider than maxW (px at the target depth), by words. */\nfunction wrap3d(S, str, o, maxW, maxLines) {\n  var words = String(str || \"\").split(/\\s+/).filter(Boolean), lines = [], cur = \"\";\n  words.forEach(function (w) {\n    var next = cur ? cur + \" \" + w : w;\n    if (cur && S.measure(next, o) > maxW) { lines.push(cur); cur = w; } else cur = next;\n  });\n  if (cur) lines.push(cur);\n  if (maxLines && lines.length > maxLines) { var rest = lines.slice(maxLines - 1).join(\" \"); lines = lines.slice(0, maxLines - 1).concat([rest]); }\n  return lines;\n}\n\n/** The biggest size (≤ max) at which str fits maxW on one line, in 3D text. */\nfunction fit3d(S, str, o, maxW, max) {\n  var w = S.measure(str, Object.assign({}, o, { size: 100 }));\n  return w > 0 ? Math.min(max, (maxW / w) * 100) : max;\n}\n\n/** A soft contact shadow on the floor under a point (2D, drawn before the scene). */\nfunction floorShadow($, S, p, rx, op) {\n  var q = S.project(p);\n  if (!q) return \"\";\n  var id = $.uid(\"fs\");\n  $.def('<radialGradient id=\"' + id + '\"><stop offset=\"0\" stop-color=\"#000\" stop-opacity=\"' + (op || 0.45) + '\"/><stop offset=\"1\" stop-color=\"#000\" stop-opacity=\"0\"/></radialGradient>');\n  return '<ellipse cx=\"' + $.fx(q.x) + '\" cy=\"' + $.fx(q.y) + '\" rx=\"' + $.fx(rx * q.s) + '\" ry=\"' + $.fx(rx * q.s * 0.18) + '\" fill=\"url(#' + id + ')\"/>';\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const k = $.k;\n  const tin = $.beat(\"in\", 0.1);\n  let s = M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: W, h: H }, glow: [W / 2, H * 0.45] }));\n  const dec = Math.max(0, Math.min(3, Math.round(p.decimals)));\n  const fmt = function (v) { const s2 = v.toFixed(dec); const parts = s2.split(\".\"); return (p.prefix || \"\") + parts[0].replace(/\\B(?=(\\d{3})+(?!\\d))/g, \",\") + (parts[1] ? \".\" + parts[1] : \"\") + (p.suffix || \"\"); };\n  const face = $.font(\"display\");\n  const cy = A.y + A.h * 0.42;\n  const land = tin + 1.4;\n  const sweep = $.seg(t, land, land + 0.9);\n  const S = scene3d($, { camera: { fov: 30, yaw: -16 + 10 * $.E.out3($.seg(t, tin, $.dur)), pitch: -10, y: cy }, light: { dir: [-1.2 + 2.4 * sweep, 0.4, 0.85], spec: 0.2 + 0.6 * Math.sin(Math.PI * sweep), shine: 14 } });\n  const final = fmt(p.value);\n  const size = fit3d(S, final, { face: face }, A.w * 0.8, Math.min(A.h * 0.36, 380 * k));\n  const v = p.value * $.E.out3($.seg(t, tin + 0.2, land));\n  const e = $.spring(Math.max(0, t - tin), 2, 7);\n  S.add(S.text(fmt(v), { face: face, size: size, depth: size * 0.28, color: c.accent, side: $.mix(c.accent, c.bg, 0.45) }), { rotate: [14 * (1 - e) + 8, 0, 0], scale: 0.8 + 0.2 * e });\n  s += M.part(\"number\", { at: tin, in: \"fade\", dur: 0.2, out: \"zoom\", box: { x: A.x, y: cy - size, w: A.w, h: size * 2 } }, floorShadow($, S, [0, size * 0.6, 0], S.measure(final, { face: face, size: size }) * 0.5, 0.35) + S.render());\n  if (p.label) {\n    const lf = fitFont($, $.case(String(p.label)), \"text\", 50 * k, A.w * 0.86);\n    s += M.text(\"label\", { at: land - 0.2, in: \"rise\", distance: 20, out: \"fade\", ...lf, x: W / 2, y: cy + size * 0.78, anchor: \"middle\", fill: c.fg }, $.case(String(p.label)));\n  }\n  return s;\n}\n"},{"id":"card-flip","name":"3D card flip","description":"A card with real thickness tilts in, holds its front, then flips over to its back. Good for before/after, question/answer, myth/fact.","category":"scene","tags":["3d","card","flip","reveal","before after"],"placement":"full","libs":["3d"],"faces3d":["display","text"],"duration":{"default":5,"min":3,"max":20},"props":{"frontTitle":{"type":"text","label":"Front","default":"Before","maxLength":24},"frontText":{"type":"text","label":"Front line","default":"9 hours in the timeline","maxLength":60},"backTitle":{"type":"text","label":"Back","default":"After","maxLength":24},"backText":{"type":"text","label":"Back line","default":"One sentence to the agent","maxLength":60},"flipAt":{"type":"number","label":"Flip at (s)","default":2.1,"min":0.5,"max":20}},"parts":{"ground":"the ground","card":"the card"},"beats":{"in":0.1,"out":-0.45},"style":"ember","pack":"3d","ref":"3d/card-flip","code":"// MONARC 3D — helpers this pack's items share. The scenes are built with scene3d (the 3D kit).\n\n/** The focal length a scene3d camera with this field of view has on this canvas. */\nfunction focalFor($, fov) {\n  return ($.H / 2) / Math.tan(((fov || 35) * Math.PI) / 360);\n}\n\n/** Lines of 3D text no wider than maxW (px at the target depth), by words. */\nfunction wrap3d(S, str, o, maxW, maxLines) {\n  var words = String(str || \"\").split(/\\s+/).filter(Boolean), lines = [], cur = \"\";\n  words.forEach(function (w) {\n    var next = cur ? cur + \" \" + w : w;\n    if (cur && S.measure(next, o) > maxW) { lines.push(cur); cur = w; } else cur = next;\n  });\n  if (cur) lines.push(cur);\n  if (maxLines && lines.length > maxLines) { var rest = lines.slice(maxLines - 1).join(\" \"); lines = lines.slice(0, maxLines - 1).concat([rest]); }\n  return lines;\n}\n\n/** The biggest size (≤ max) at which str fits maxW on one line, in 3D text. */\nfunction fit3d(S, str, o, maxW, max) {\n  var w = S.measure(str, Object.assign({}, o, { size: 100 }));\n  return w > 0 ? Math.min(max, (maxW / w) * 100) : max;\n}\n\n/** A soft contact shadow on the floor under a point (2D, drawn before the scene). */\nfunction floorShadow($, S, p, rx, op) {\n  var q = S.project(p);\n  if (!q) return \"\";\n  var id = $.uid(\"fs\");\n  $.def('<radialGradient id=\"' + id + '\"><stop offset=\"0\" stop-color=\"#000\" stop-opacity=\"' + (op || 0.45) + '\"/><stop offset=\"1\" stop-color=\"#000\" stop-opacity=\"0\"/></radialGradient>');\n  return '<ellipse cx=\"' + $.fx(q.x) + '\" cy=\"' + $.fx(q.y) + '\" rx=\"' + $.fx(rx * q.s) + '\" ry=\"' + $.fx(rx * q.s * 0.18) + '\" fill=\"url(#' + id + ')\"/>';\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const k = $.k;\n  const tin = $.beat(\"in\", 0.1);\n  let s = M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: W, h: H }, glow: [W / 2, H * 0.4] }));\n  const cw = A.wide ? A.w * 0.58 : A.tall ? A.w * 0.92 : A.w * 0.78;\n  const ch = A.wide ? cw * 0.56 : A.tall ? cw * 1.05 : cw * 0.8;\n  const d = 18 * k;\n  const cy = A.y + A.h * 0.5;\n  const S = scene3d($, { camera: { fov: 30, pitch: -6, y: cy }, light: { spec: 0.3 } });\n  // In: tilted back, settling on a spring. Flip: a half turn with a lift toward the camera.\n  const e = $.spring(Math.max(0, t - tin), 2, 6.5);\n  const fl = $.E.inOut3($.seg(t, p.flipAt, p.flipAt + 0.9));\n  const card = { rotate: [42 * (1 - e) + 12 * Math.sin(Math.PI * fl), 180 * fl - 14 * (1 - e), 0], position: [0, 80 * k * (1 - e), -140 * k * Math.sin(Math.PI * fl)] };\n  S.add(S.card(cw, ch, { depth: d, radius: 36 * k, color: c.surface, back: c.accent, side: $.mix(c.accent, c.bg, 0.35) }), card);\n  const side = function (title, line, back) {\n    const ink = back ? $.on(c.accent) : c.fg;\n    const tface = $.font(\"display\"), lface = $.font(\"text\");\n    const ts = fit3d(S, title, { face: tface }, cw * 0.8, ch * 0.32);\n    const ls = Math.min(ts * 0.34, ch * 0.09);\n    const lines = wrap3d(S, line, { face: lface, size: ls }, cw * 0.8, 2);\n    const z = back ? d / 2 + 1.5 : -d / 2 - 1.5;\n    const ry = back ? 180 : 0;\n    const total = ts + (lines.length ? ts * 0.35 + lines.length * ls * 1.3 : 0);\n    let y = -total / 2 + ts / 2;\n    S.add(S.text(title, { face: tface, size: ts, depth: 0, color: ink }), { position: [0, y, z], rotate: [0, ry, 0], parent: card, flat: true, decal: true });\n    y += ts / 2 + ts * 0.35 + ls / 2;\n    lines.forEach(function (l, i) { S.add(S.text(l, { face: lface, size: ls, depth: 0, color: ink }), { position: [0, y + i * ls * 1.3, z], rotate: [0, ry, 0], parent: card, flat: true, decal: true }); });\n  };\n  side($.case(String(p.frontTitle || \"\")), String(p.frontText || \"\"), false);\n  side($.case(String(p.backTitle || \"\")), String(p.backText || \"\"), true);\n  s += M.part(\"card\", { at: tin, in: \"fade\", dur: 0.25, out: \"zoom\", box: { x: W / 2 - cw / 2, y: cy - ch / 2, w: cw, h: ch } }, floorShadow($, S, [0, ch * 0.62, 0], cw * 0.5, 0.35) + S.render());\n  return s;\n}\n"},{"id":"cube","name":"3D word cube","description":"A cube turns a quarter at a time on a spring, each side showing the next word — a kinetic way through a list of 2 to 4 words.","category":"text","tags":["3d","cube","words","kinetic","rotate"],"placement":"full","libs":["3d"],"duration":{"default":5,"min":2.5,"max":30},"props":{"lead":{"type":"text","label":"Lead-in","default":"Edit","maxLength":20},"words":{"type":"list","label":"Words (2–4)","default":["faster","smarter","together","anywhere"],"maxItems":4,"minItems":2,"of":"text"}},"parts":{"ground":"the ground","lead":"the lead-in","cube":"the cube"},"beats":{"in":0.1,"out":-0.45},"style":"ember","pack":"3d","ref":"3d/cube","code":"// MONARC 3D — helpers this pack's items share. The scenes are built with scene3d (the 3D kit).\n\n/** The focal length a scene3d camera with this field of view has on this canvas. */\nfunction focalFor($, fov) {\n  return ($.H / 2) / Math.tan(((fov || 35) * Math.PI) / 360);\n}\n\n/** Lines of 3D text no wider than maxW (px at the target depth), by words. */\nfunction wrap3d(S, str, o, maxW, maxLines) {\n  var words = String(str || \"\").split(/\\s+/).filter(Boolean), lines = [], cur = \"\";\n  words.forEach(function (w) {\n    var next = cur ? cur + \" \" + w : w;\n    if (cur && S.measure(next, o) > maxW) { lines.push(cur); cur = w; } else cur = next;\n  });\n  if (cur) lines.push(cur);\n  if (maxLines && lines.length > maxLines) { var rest = lines.slice(maxLines - 1).join(\" \"); lines = lines.slice(0, maxLines - 1).concat([rest]); }\n  return lines;\n}\n\n/** The biggest size (≤ max) at which str fits maxW on one line, in 3D text. */\nfunction fit3d(S, str, o, maxW, max) {\n  var w = S.measure(str, Object.assign({}, o, { size: 100 }));\n  return w > 0 ? Math.min(max, (maxW / w) * 100) : max;\n}\n\n/** A soft contact shadow on the floor under a point (2D, drawn before the scene). */\nfunction floorShadow($, S, p, rx, op) {\n  var q = S.project(p);\n  if (!q) return \"\";\n  var id = $.uid(\"fs\");\n  $.def('<radialGradient id=\"' + id + '\"><stop offset=\"0\" stop-color=\"#000\" stop-opacity=\"' + (op || 0.45) + '\"/><stop offset=\"1\" stop-color=\"#000\" stop-opacity=\"0\"/></radialGradient>');\n  return '<ellipse cx=\"' + $.fx(q.x) + '\" cy=\"' + $.fx(q.y) + '\" rx=\"' + $.fx(rx * q.s) + '\" ry=\"' + $.fx(rx * q.s * 0.18) + '\" fill=\"url(#' + id + ')\"/>';\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const k = $.k;\n  const tin = $.beat(\"in\", 0.1);\n  let s = M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: W, h: H } }));\n  const words = (p.words || []).map(function (w) { return $.case(String(w)); });\n  const n = Math.max(2, words.length);\n  const face = $.font(\"display\");\n  const lead = $.case(String(p.lead || \"\"));\n  // A long box that rolls about its horizontal axis: four faces as wide as the longest word.\n  const probe = scene3d($, {});\n  const box = A.wide ? Math.min(A.h * 0.36, 360 * k) : Math.min(A.w * 0.42, 380 * k);\n  const cw = Math.min(A.w * (A.wide && lead ? 0.6 : 0.88), Math.max.apply(null, words.map(function (w) { return probe.measure(w, { face: face, size: box * 0.6 }); })) + box * 0.5);\n  const lf = fitFont($, lead, \"display\", box * 0.5, A.w * 0.9);\n  const lw = lead ? $.measure(lead, lf) : 0;\n  const stack = !A.wide;\n  const cx = stack ? W / 2 : W / 2 + (lw + 30 * k) / 2;\n  const cy = A.y + A.h * (stack ? 0.55 : 0.5);\n  const S = scene3d($, { camera: { fov: 30, pitch: -6, yaw: -16, x: cx, y: cy }, light: { spec: 0.25 } });\n  // A quarter roll for each word after the first, on springs, evenly through the hold.\n  const span = Math.max(0.8, $.beat(\"out\", $.dur - 0.45) - tin - 0.8);\n  const step = span / n;\n  let turn = 0;\n  for (let i = 1; i < n; i++) turn += Math.min(1.08, $.spring(Math.max(0, t - (tin + 0.5 + i * step)), 2.2, 7));\n  const cube = { rotate: [90 * turn, 0, 0], position: [0, 0, 0] };\n  const hw = cw / 2, hh = box / 2, hd = box / 2;\n  const quad = function (pts, nrm, col) { return { loops: [pts], n: nrm, color: col, role: \"side\" }; };\n  const faceCol = function (i) { return i % 2 ? c.accent : c.surface; };\n  const faces = [\n    quad([[-hw, -hh, -hd], [hw, -hh, -hd], [hw, hh, -hd], [-hw, hh, -hd]], [0, 0, -1], faceCol(0)),\n    quad([[-hw, -hh, hd], [hw, -hh, hd], [hw, -hh, -hd], [-hw, -hh, -hd]], [0, -1, 0], faceCol(1)),\n    quad([[hw, -hh, hd], [-hw, -hh, hd], [-hw, hh, hd], [hw, hh, hd]], [0, 0, 1], faceCol(2)),\n    quad([[-hw, hh, -hd], [hw, hh, -hd], [hw, hh, hd], [-hw, hh, hd]], [0, 1, 0], faceCol(3)),\n    quad([[hw, -hh, -hd], [hw, -hh, hd], [hw, hh, hd], [hw, hh, -hd]], [1, 0, 0], $.mix(c.surface, c.bg, 0.45)),\n    quad([[-hw, -hh, hd], [-hw, -hh, -hd], [-hw, hh, -hd], [-hw, hh, hd]], [-1, 0, 0], $.mix(c.surface, c.bg, 0.45)),\n  ];\n  S.add({ faces: faces }, cube);\n  // Word i sits on the face that rolls to the front after i quarter turns: the front face turned back by i quarters.\n  words.slice(0, 4).forEach(function (w, i) {\n    const size = fit3d(S, w, { face: face }, cw * 0.84, box * 0.56);\n    const ink = i % 2 ? $.on(c.accent) : c.fg;\n    S.add(S.text(w, { face: face, size: size, depth: 0, color: ink }), { position: [0, 0, -hd - 1.5], parent: { rotate: [-90 * i, 0, 0], parent: cube }, flat: true, decal: true });\n  });\n  s += M.part(\"cube\", { at: tin, in: \"pop\", out: \"zoom\", box: { x: cx - hw, y: cy - hh, w: cw, h: box } }, S.render());\n  if (lead) s += M.text(\"lead\", { at: tin + 0.15, in: \"rise\", distance: 20, out: \"fade\", ...lf, x: stack ? W / 2 : cx - hw - 40 * k, y: stack ? cy - hh - lf.size * 1.6 : cy - lf.size * 0.55, anchor: stack ? \"middle\" : \"end\", fill: c.fg }, lead);\n  return s;\n}\n"},{"id":"bars-3d","name":"3D bar chart","description":"Bars rise from a floor one after another while the camera orbits; values count up on their tops and the leader takes the accent.","category":"chart","tags":["3d","chart","bars","data","orbit"],"placement":"full","libs":["3d"],"duration":{"default":6,"min":3,"max":30},"props":{"title":{"type":"text","label":"Title","default":"Videos shipped per week","maxLength":50},"rows":{"type":"list","label":"Rows (Label: value)","default":[{"label":"Jan","value":4},{"label":"Feb","value":6},{"label":"Mar","value":9},{"label":"Apr","value":13},{"label":"May","value":21}],"maxItems":8,"minItems":2,"of":{"label":"text","value":"number"}},"suffix":{"type":"text","label":"After numbers","default":"","maxLength":6}},"parts":{"ground":"the ground","title":"the heading","bars":"the bars and floor","labels":"the names and values"},"beats":{"in":0.1,"out":-0.45},"style":"ember","pack":"3d","ref":"3d/bars-3d","code":"// MONARC 3D — helpers this pack's items share. The scenes are built with scene3d (the 3D kit).\n\n/** The focal length a scene3d camera with this field of view has on this canvas. */\nfunction focalFor($, fov) {\n  return ($.H / 2) / Math.tan(((fov || 35) * Math.PI) / 360);\n}\n\n/** Lines of 3D text no wider than maxW (px at the target depth), by words. */\nfunction wrap3d(S, str, o, maxW, maxLines) {\n  var words = String(str || \"\").split(/\\s+/).filter(Boolean), lines = [], cur = \"\";\n  words.forEach(function (w) {\n    var next = cur ? cur + \" \" + w : w;\n    if (cur && S.measure(next, o) > maxW) { lines.push(cur); cur = w; } else cur = next;\n  });\n  if (cur) lines.push(cur);\n  if (maxLines && lines.length > maxLines) { var rest = lines.slice(maxLines - 1).join(\" \"); lines = lines.slice(0, maxLines - 1).concat([rest]); }\n  return lines;\n}\n\n/** The biggest size (≤ max) at which str fits maxW on one line, in 3D text. */\nfunction fit3d(S, str, o, maxW, max) {\n  var w = S.measure(str, Object.assign({}, o, { size: 100 }));\n  return w > 0 ? Math.min(max, (maxW / w) * 100) : max;\n}\n\n/** A soft contact shadow on the floor under a point (2D, drawn before the scene). */\nfunction floorShadow($, S, p, rx, op) {\n  var q = S.project(p);\n  if (!q) return \"\";\n  var id = $.uid(\"fs\");\n  $.def('<radialGradient id=\"' + id + '\"><stop offset=\"0\" stop-color=\"#000\" stop-opacity=\"' + (op || 0.45) + '\"/><stop offset=\"1\" stop-color=\"#000\" stop-opacity=\"0\"/></radialGradient>');\n  return '<ellipse cx=\"' + $.fx(q.x) + '\" cy=\"' + $.fx(q.y) + '\" rx=\"' + $.fx(rx * q.s) + '\" ry=\"' + $.fx(rx * q.s * 0.18) + '\" fill=\"url(#' + id + ')\"/>';\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const k = $.k;\n  const tin = $.beat(\"in\", 0.1);\n  let s = M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: W, h: H } }));\n  const title = $.case(String(p.title || \"\"));\n  let top = A.y;\n  if (title) {\n    const tf = fitLines($, title, { ...$.font(\"display\"), ls: -0.02 }, A.w, Math.min(84 * k, A.h * 0.09), 40 * k, 2);\n    tf.lines.forEach(function (l, i) { s += M.text(\"title\", { at: tin + i * 0.1, in: \"rise\", distance: 20, out: \"fade\", ...tf.font, x: A.x, y: A.y + i * tf.size * 1.05, fill: c.fg }, l); });\n    top = A.y + tf.lines.length * tf.size * 1.05 + 30 * k;\n  }\n  const rows = p.rows || [];\n  const n = rows.length;\n  const maxV = Math.max.apply(null, rows.map(function (r) { return r.value; }).concat([1e-9]));\n  const span = A.w * (A.wide ? 0.78 : 0.9);\n  const gap = span / n, bw = gap * 0.56;\n  const maxH = (A.y + A.h - top) * 0.62;\n  const cy = top + (A.y + A.h - top) * 0.52;\n  const floorY = maxH / 2;\n  const S = scene3d($, { camera: { fov: 32, yaw: -26 + 14 * $.E.inOut2($.seg(t, tin, $.dur)), pitch: -20, y: cy }, light: { spec: 0.22 } });\n  // The floor: a thin slab under the bars.\n  S.add(S.box(span + gap * 0.4, 6 * k, gap * 1.4, { color: $.mix(c.bg, c.fg, 0.1) }), { position: [0, floorY + 3 * k, 0], rotate: [0, 0, 0] });\n  let lab = \"\";\n  rows.forEach(function (r, i) {\n    const at = tin + 0.4 + i * 0.16;\n    const e = $.spring(Math.max(0, t - at), 2.2, 7.5);\n    const h = Math.max(2 * k, maxH * (r.value / maxV) * e);\n    const x = -span / 2 + gap * (i + 0.5);\n    const lead = r.value === maxV;\n    S.add(S.box(bw, h, bw, { color: lead ? c.accent : $.mix(c.fg, c.bg, 0.45) }), { position: [x, floorY - h / 2, 0] });\n    S.at([x, floorY + 18 * k, -bw / 2], function (px, py, sc) { return $.text(String(r.label), { ...$.font(\"text\"), size: 30 * k, x: px, y: py, anchor: \"middle\", fill: c.mute }); });\n    if (t > at) S.at([x, floorY - h - 14 * k, 0], function (px, py, sc) { return $.text(Math.round(r.value * Math.min(1, e)) + (p.suffix || \"\"), { ...$.font(\"display\"), size: 36 * k, x: px, y: py - 40 * k, anchor: \"middle\", fill: lead ? c.accent : c.fg }); }, { onTop: true });\n  });\n  s += M.part(\"bars\", { at: tin + 0.2, in: \"fade\", dur: 0.3, out: \"fade\", box: { x: A.x, y: top, w: A.w, h: A.y + A.h - top } }, S.render());\n  return s + lab;\n}\n"},{"id":"globe","name":"3D globe","description":"A lit globe spins to a country, which lights up in the accent; routes arc in from other countries and a pin names the place.","category":"scene","tags":["3d","globe","map","world","geo","travel"],"placement":"full","libs":["3d","world"],"duration":{"default":6,"min":3,"max":30},"props":{"target":{"type":"text","label":"Country","default":"India","maxLength":40},"label":{"type":"text","label":"Label","default":"Mumbai studio","maxLength":28},"from":{"type":"list","label":"Routes from","default":["United States","Brazil","United Kingdom","Australia"],"maxItems":8,"of":"text"},"title":{"type":"text","label":"Title","default":"","maxLength":50}},"parts":{"ground":"the ground","title":"the heading","globe":"the globe","routes":"the routes","pin":"the pin and label"},"beats":{"in":0.1,"out":-0.45},"style":"ember","pack":"3d","ref":"3d/globe","code":"// MONARC 3D — helpers this pack's items share. The scenes are built with scene3d (the 3D kit).\n\n/** The focal length a scene3d camera with this field of view has on this canvas. */\nfunction focalFor($, fov) {\n  return ($.H / 2) / Math.tan(((fov || 35) * Math.PI) / 360);\n}\n\n/** Lines of 3D text no wider than maxW (px at the target depth), by words. */\nfunction wrap3d(S, str, o, maxW, maxLines) {\n  var words = String(str || \"\").split(/\\s+/).filter(Boolean), lines = [], cur = \"\";\n  words.forEach(function (w) {\n    var next = cur ? cur + \" \" + w : w;\n    if (cur && S.measure(next, o) > maxW) { lines.push(cur); cur = w; } else cur = next;\n  });\n  if (cur) lines.push(cur);\n  if (maxLines && lines.length > maxLines) { var rest = lines.slice(maxLines - 1).join(\" \"); lines = lines.slice(0, maxLines - 1).concat([rest]); }\n  return lines;\n}\n\n/** The biggest size (≤ max) at which str fits maxW on one line, in 3D text. */\nfunction fit3d(S, str, o, maxW, max) {\n  var w = S.measure(str, Object.assign({}, o, { size: 100 }));\n  return w > 0 ? Math.min(max, (maxW / w) * 100) : max;\n}\n\n/** A soft contact shadow on the floor under a point (2D, drawn before the scene). */\nfunction floorShadow($, S, p, rx, op) {\n  var q = S.project(p);\n  if (!q) return \"\";\n  var id = $.uid(\"fs\");\n  $.def('<radialGradient id=\"' + id + '\"><stop offset=\"0\" stop-color=\"#000\" stop-opacity=\"' + (op || 0.45) + '\"/><stop offset=\"1\" stop-color=\"#000\" stop-opacity=\"0\"/></radialGradient>');\n  return '<ellipse cx=\"' + $.fx(q.x) + '\" cy=\"' + $.fx(q.y) + '\" rx=\"' + $.fx(rx * q.s) + '\" ry=\"' + $.fx(rx * q.s * 0.18) + '\" fill=\"url(#' + id + ')\"/>';\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const k = $.k;\n  const tin = $.beat(\"in\", 0.1);\n  let s = M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: W, h: H }, glow: [W / 2, H / 2] }));\n  let top = A.y;\n  if (p.title) {\n    const tf = fitFont($, $.case(String(p.title)), \"display\", 72 * k, A.w);\n    s += M.text(\"title\", { at: tin, in: \"rise\", distance: 20, out: \"fade\", ...tf, x: W / 2, y: A.y, anchor: \"middle\", fill: c.fg }, $.case(String(p.title)));\n    top = A.y + tf.size * 1.4;\n  }\n  const target = worldFind(p.target) || worldFind(\"India\");\n  const R = Math.min(A.w, A.y + A.h - top) * 0.4;\n  const cy = top + (A.y + A.h - top) / 2;\n  const S = scene3d($, { camera: { fov: 28, y: cy }, light: { dir: [0.45, 0.35, 0.82], spec: 0.15 } });\n  // Spin from well behind to the country, easing in; tilt toward its latitude.\n  const arrive = tin + 2.2;\n  const g = $.E.inOut3($.seg(t, tin, arrive));\n  const spin = -target.c[0] - 150 * (1 - g) - 6 * $.seg(t, arrive, $.dur);\n  const tilt = Math.max(-35, Math.min(35, target.c[1])) * g + 12 * (1 - g);\n  S.add(S.sphere(R, { color: $.mix(c.bg, c.accent, 0.1) }), { position: [0, 0, 0] });\n  const lit = $.seg(t, arrive - 0.3, arrive + 0.3);\n  worldGlobe(S, { r: R, spin: spin, tilt: tilt, colorOf: function (cn) { return cn === target ? $.mix($.mix(c.fg, c.bg, 0.55), c.accent, lit) : $.mix(c.fg, c.bg, 0.55); } });\n  // Routes: great circles from each country to the target, lifted off the surface as they travel.\n  const tp = target.c;\n  (p.from || []).forEach(function (name, i) {\n    const src = worldFind(name);\n    if (!src || src === target) return;\n    const at = arrive + 0.1 + i * 0.25;\n    const run = $.E.inOut2($.seg(t, at, at + 1));\n    if (run <= 0) return;\n    const v = function (q) { const l = q[0] * Math.PI / 180, f = q[1] * Math.PI / 180; return [Math.cos(f) * Math.cos(l), Math.cos(f) * Math.sin(l), Math.sin(f)]; };\n    const va = v(src.c), vb = v(tp);\n    const om = Math.acos(Math.max(-1, Math.min(1, va[0] * vb[0] + va[1] * vb[1] + va[2] * vb[2]))) || 1e-6;\n    const pts = [];\n    const steps = 40;\n    for (let j = 0; j <= Math.round(steps * run); j++) {\n      const f = j / steps, s1 = Math.sin((1 - f) * om) / Math.sin(om), s2 = Math.sin(f * om) / Math.sin(om);\n      const x = s1 * va[0] + s2 * vb[0], y = s1 * va[1] + s2 * vb[1], z = s1 * va[2] + s2 * vb[2];\n      const lon = Math.atan2(y, x) * 180 / Math.PI, lat = Math.asin(Math.max(-1, Math.min(1, z))) * 180 / Math.PI;\n      pts.push(worldOnSphere(lon, lat, R * (1.01 + 0.05 * Math.sin(Math.PI * f) * Math.min(1, om)), spin, tilt));\n    }\n    // Only the stretch on the near side of the globe: a route round the back would cross the picture.\n    const near = pts.filter(function (q) { return q[2] < -R * 0.08; });\n    if (near.length > 1) S.line(near, { stroke: $.style.mode === \"light\" ? c.accent : c.accent2, width: 4 * k, opacity: 0.9 });\n  });\n  s += M.part(\"globe\", { at: tin, in: \"zoom\", dur: 0.8, out: \"fade\", box: { x: W / 2 - R, y: cy - R, w: 2 * R, h: 2 * R } }, S.render());\n  // The pin: where the country's label point sits on the turned globe.\n  if (t > arrive) {\n    const q = S.project(worldOnSphere(tp[0], tp[1], R * 1.01, spin, tilt));\n    if (q) {\n      const lf = fitFont($, $.case(String(p.label || target.name)), \"display\", 40 * k, A.w * 0.6);\n      const lw = $.measure($.case(String(p.label || target.name)), lf) + 40 * k, lh = lf.size * 1.6;\n      const right = q.x < W / 2 + R * 0.4;\n      const bx = right ? q.x + 30 * k : q.x - 30 * k - lw;\n      s += M.part(\"pin\", { at: arrive, in: \"pop\", out: \"fade\", box: { x: Math.min(bx, q.x) - 20 * k, y: q.y - lh - 30 * k, w: lw + 60 * k, h: lh + 50 * k } },\n        $.line(q.x, q.y, right ? bx : bx + lw, q.y - lh / 2 - 20 * k, { stroke: c.fg, sw: 3 * k }),\n        $.circle(q.x, q.y, 10 * k, { fill: c.accent, stroke: c.fg, sw: 3 * k }),\n        $.rect(bx, q.y - lh - 20 * k, lw, lh, { r: lh / 2, fill: c.surface, shadow: \"md\" }),\n        $.text($.case(String(p.label || target.name)), { ...lf, x: bx + lw / 2, y: q.y - lh - 20 * k + (lh - lf.size) / 2 - lf.size * 0.04, anchor: \"middle\", fill: c.fg }));\n    }\n  }\n  return s;\n}\n"},{"id":"exploded-stack","name":"Exploded stack","description":"Isometric plates, one per layer of a system, start as one stack, spread apart with a numbered label on a leader line beside each, light up one after another, then close back into one block. For architecture and system diagrams.","category":"chart","tags":["exploded","isometric","layers","architecture","stack"],"placement":"full","duration":{"default":6,"min":3,"max":30},"props":{"title":{"type":"text","label":"Title","default":"How a request moves","maxLength":50},"layers":{"type":"list","label":"Layers (top first)","default":["Interface","API gateway","Services","Database"],"maxItems":6,"minItems":1,"of":"text"}},"parts":{"ground":"the ground","title":"the heading","stack":"the plates","labels":"the labels"},"beats":{"in":0.15,"out":-0.45},"style":"ember","pack":"3d","ref":"3d/exploded-stack","code":"// MONARC 3D — helpers this pack's items share. The scenes are built with scene3d (the 3D kit).\n\n/** The focal length a scene3d camera with this field of view has on this canvas. */\nfunction focalFor($, fov) {\n  return ($.H / 2) / Math.tan(((fov || 35) * Math.PI) / 360);\n}\n\n/** Lines of 3D text no wider than maxW (px at the target depth), by words. */\nfunction wrap3d(S, str, o, maxW, maxLines) {\n  var words = String(str || \"\").split(/\\s+/).filter(Boolean), lines = [], cur = \"\";\n  words.forEach(function (w) {\n    var next = cur ? cur + \" \" + w : w;\n    if (cur && S.measure(next, o) > maxW) { lines.push(cur); cur = w; } else cur = next;\n  });\n  if (cur) lines.push(cur);\n  if (maxLines && lines.length > maxLines) { var rest = lines.slice(maxLines - 1).join(\" \"); lines = lines.slice(0, maxLines - 1).concat([rest]); }\n  return lines;\n}\n\n/** The biggest size (≤ max) at which str fits maxW on one line, in 3D text. */\nfunction fit3d(S, str, o, maxW, max) {\n  var w = S.measure(str, Object.assign({}, o, { size: 100 }));\n  return w > 0 ? Math.min(max, (maxW / w) * 100) : max;\n}\n\n/** A soft contact shadow on the floor under a point (2D, drawn before the scene). */\nfunction floorShadow($, S, p, rx, op) {\n  var q = S.project(p);\n  if (!q) return \"\";\n  var id = $.uid(\"fs\");\n  $.def('<radialGradient id=\"' + id + '\"><stop offset=\"0\" stop-color=\"#000\" stop-opacity=\"' + (op || 0.45) + '\"/><stop offset=\"1\" stop-color=\"#000\" stop-opacity=\"0\"/></radialGradient>');\n  return '<ellipse cx=\"' + $.fx(q.x) + '\" cy=\"' + $.fx(q.y) + '\" rx=\"' + $.fx(rx * q.s) + '\" ry=\"' + $.fx(rx * q.s * 0.18) + '\" fill=\"url(#' + id + ')\"/>';\n}\n\n// An exploded stack: isometric plates, one per layer of a system, start as a single stack, spread apart with a\n// numbered label on a leader line beside each, light up one after another, then close back into one block.\nfunction isoFace($, ox, oy, S, inner) {\n  // The plate's top face: local (x, y) in 0…S mapped onto the isometric rhombus whose back corner is (ox, oy).\n  return '<g transform=\"matrix(0.866 0.5 -0.866 0.5 ' + $.fx(ox) + \" \" + $.fx(oy) + ')\">' + inner + \"</g>\";\n}\nfunction glyph($, kind, S, col, acc) {\n  const sw = S * 0.012;\n  switch (kind % 5) {\n    case 0: // an app window: a bar and three panes\n      return $.rect(S * 0.14, S * 0.16, S * 0.72, S * 0.12, { r: S * 0.02, stroke: col, sw: sw }) + $.rect(S * 0.14, S * 0.34, S * 0.3, S * 0.5, { r: S * 0.02, stroke: col, sw: sw }) + $.rect(S * 0.5, S * 0.34, S * 0.36, S * 0.22, { r: S * 0.02, stroke: acc, sw: sw }) + $.rect(S * 0.5, S * 0.62, S * 0.36, S * 0.22, { r: S * 0.02, stroke: col, sw: sw });\n    case 1: // requests: parallel lines of different lengths\n      let l = \"\";\n      for (let i = 0; i < 5; i++) l += $.line(S * 0.16, S * (0.2 + i * 0.15), S * (0.45 + 0.4 * ((i * 37) % 10) / 10), S * (0.2 + i * 0.15), { stroke: i === 1 ? acc : col, sw: sw * 1.4 });\n      return l;\n    case 2: // services: a grid of blocks\n      let g = \"\";\n      for (let r = 0; r < 3; r++) for (let q = 0; q < 3; q++) g += $.rect(S * (0.15 + q * 0.25), S * (0.15 + r * 0.25), S * 0.18, S * 0.18, { r: S * 0.02, fill: r === 1 && q === 1 ? acc : col, op: r === 1 && q === 1 ? 0.9 : 0.35 });\n      return g;\n    case 3: // storage: discs\n      return $.circle(S * 0.32, S * 0.5, S * 0.17, { stroke: col, sw: sw }) + $.circle(S * 0.68, S * 0.5, S * 0.17, { stroke: acc, sw: sw }) + $.circle(S * 0.5, S * 0.5, S * 0.06, { fill: col, op: 0.5 });\n    default: // a network: nodes and links\n      const pts = [[0.25, 0.3], [0.7, 0.25], [0.5, 0.55], [0.3, 0.75], [0.75, 0.72]];\n      let n = $.line(S * 0.25, S * 0.3, S * 0.5, S * 0.55, { stroke: col, sw: sw }) + $.line(S * 0.7, S * 0.25, S * 0.5, S * 0.55, { stroke: col, sw: sw }) + $.line(S * 0.5, S * 0.55, S * 0.3, S * 0.75, { stroke: col, sw: sw }) + $.line(S * 0.5, S * 0.55, S * 0.75, S * 0.72, { stroke: acc, sw: sw });\n      pts.forEach(function (q, i) { n += $.circle(S * q[0], S * q[1], S * 0.05, { fill: i === 2 ? acc : col }); });\n      return n;\n  }\n}\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const tin = $.beat(\"in\", 0.15);\n  let s = M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: W, h: H } }));\n  const layers = (p.layers || []).slice(0, 6).map(function (r) { return typeof r === \"string\" ? r : String(r.label || \"\"); });\n  const n = Math.max(1, layers.length);\n  let top = A.y;\n  if (p.title) {\n    const tf = fitFont($, $.case(String(p.title)), \"display\", (A.tall ? 84 : 72) * k, A.w, { ls: -0.03 });\n    s += M.text(\"title\", Object.assign({ at: tin, in: \"rise\", distance: 24 }, tf, { x: A.x, y: A.y, fill: c.fg }), $.case(String(p.title)));\n    top += tf.size * 1.5;\n  }\n  // Sizes: plate side S, thickness, and the spread between plates when exploded.\n  const roomH = A.y + A.h - top;\n  const labelW = A.tall ? A.w * 0.42 : A.w * 0.34;\n  let S = Math.min((A.w - labelW) / 1.9, roomH / (1.0 + n * 0.42));\n  const thick = S * 0.06, spread = Math.min(S * 0.42, (roomH - S * 1.0 - thick) / Math.max(1, n - 1));\n  // Explode, hold, collapse.\n  const open = $.E.inOut3($.seg(t, tin + 0.5, tin + 1.4)) - $.E.inOut3($.seg(t, $.dur - 1.5, $.dur - 0.7));\n  const gap = thick * 1.25 + (spread - thick * 1.25) * Math.max(0, open);\n  const cx = A.tall ? A.x + (A.w - labelW) / 2 + S * 0.05 : A.x + (A.w - labelW) * 0.5;\n  const stackH = S * 1.0 + thick + gap * (n - 1);\n  const oy0 = top + (roomH - stackH) / 2;\n  // The plate lit now: each in turn while the stack is open.\n  const holdA = tin + 1.5, holdB = $.dur - 1.6;\n  const lit = t >= holdA && t < holdB ? Math.floor(((t - holdA) / Math.max(0.01, holdB - holdA)) * n) : -1;\n  let stack = $.circle(cx, oy0 + gap * (n - 1) + S * 0.5 + thick + S * 0.06, S * 0.9, { fill: $.gradient([[0, \"#000000\", 0.18], [1, \"#000000\", 0]], { radial: true }), transform: \"translate(\" + cx + \" \" + (oy0 + gap * (n - 1) + S * 0.56) + \") scale(1 0.42) translate(\" + -cx + \" \" + -(oy0 + gap * (n - 1) + S * 0.56) + \")\" });\n  const side = $.mix(c.surface, c.fg, 0.1), side2 = $.mix(c.surface, c.fg, 0.18);\n  const labels = [];\n  // Draw bottom plate first so upper plates sit over it.\n  for (let i = n - 1; i >= 0; i--) {\n    const oy = oy0 + i * gap, ox = cx;\n    const R = [ox + S * 0.866, oy + S * 0.5], F = [ox, oy + S], L = [ox - S * 0.866, oy + S * 0.5];\n    const on = i === lit;\n    let pl = $.path(\"M\" + L[0] + \" \" + L[1] + \"L\" + F[0] + \" \" + F[1] + \"L\" + F[0] + \" \" + (F[1] + thick) + \"L\" + L[0] + \" \" + (L[1] + thick) + \"Z\", { fill: side });\n    pl += $.path(\"M\" + F[0] + \" \" + F[1] + \"L\" + R[0] + \" \" + R[1] + \"L\" + R[0] + \" \" + (R[1] + thick) + \"L\" + F[0] + \" \" + (F[1] + thick) + \"Z\", { fill: side2 });\n    pl += isoFace($, ox, oy, S, $.rect(0, 0, S, S, { fill: on ? $.mix(c.surface, c.accent, 0.12) : c.surface, stroke: on ? c.accent : c.line, sw: S * 0.008 }) + glyph($, i, S, $.rgba(c.fg, 0.55), c.accent));\n    stack += pl;\n    labels.push({ i: i, y: R[1], x: R[0] });\n  }\n  s += M.part(\"stack\", { at: tin + 0.1, in: \"rise\", distance: 40, out: \"fade\", box: { x: cx - S, y: oy0, w: 2 * S, h: stackH } }, stack);\n  // Labels: a leader from each plate's right corner, the number and the name.\n  const nf = Object.assign({}, $.font(\"mono\"), { size: (A.tall ? 26 : 22) * k, weight: 700 });\n  const lf = fitFont($, layers.reduce(function (a, b) { return b.length > a.length ? b : a; }, \"\"), \"display\", (A.tall ? 46 : 40) * k, labelW - 120 * k, { ls: -0.01 });\n  const lx = cx + S * 0.866 + 70 * k;\n  let lab = \"\";\n  labels.forEach(function (q) {\n    const a = $.clamp((open - 0.55) / 0.3) * $.fadeIn(t, tin + 0.9 + q.i * 0.12, 0.3);\n    if (a <= 0) return;\n    const on = q.i === lit;\n    lab += $.g({ op: a }, $.line(q.x + 10 * k, q.y, lx - 16 * k, q.y, { stroke: on ? c.accent : c.line, sw: 2 * k }) + $.circle(lx - 16 * k, q.y, 5 * k, { fill: on ? c.accent : c.mute }) +\n      $.text((q.i + 1 < 10 ? \"0\" : \"\") + (q.i + 1), Object.assign({}, nf, { x: lx, y: q.y - nf.size * 0.55, fill: on ? c.accent : c.mute })) +\n      $.text(layers[q.i], Object.assign({}, lf, { x: lx + nf.size * 2.2, y: q.y - lf.size * 0.56, fill: c.fg })));\n  });\n  s += M.part(\"labels\", { at: tin + 0.9, in: \"none\", out: \"fade\" }, lab);\n  return s;\n}\n","libs":["3d"]},{"id":"iso-blocks","name":"Isometric blocks","description":"An isometric block chart: a floor grid, then a row of blocks rising to their values one after another, the tallest in the accent, each with its value on top and its label on the floor.","category":"chart","tags":["isometric","3d chart","blocks","bars","data"],"placement":"full","duration":{"default":5,"min":3,"max":30},"props":{"title":{"type":"text","label":"Title","default":"Renders per day","maxLength":50},"rows":{"type":"list","label":"Blocks (Label: value)","default":[{"label":"Mon","value":120},{"label":"Tue","value":180},{"label":"Wed","value":150},{"label":"Thu","value":260},{"label":"Fri","value":210}],"maxItems":8,"minItems":1,"of":{"label":"text","value":"number"}},"suffix":{"type":"text","label":"After values","default":"","maxLength":6}},"parts":{"ground":"the ground","title":"the heading","floor":"the floor","blocks":"the blocks"},"beats":{"in":0.15,"out":-0.45},"style":"ember","pack":"3d","ref":"3d/iso-blocks","code":"// MONARC 3D — helpers this pack's items share. The scenes are built with scene3d (the 3D kit).\n\n/** The focal length a scene3d camera with this field of view has on this canvas. */\nfunction focalFor($, fov) {\n  return ($.H / 2) / Math.tan(((fov || 35) * Math.PI) / 360);\n}\n\n/** Lines of 3D text no wider than maxW (px at the target depth), by words. */\nfunction wrap3d(S, str, o, maxW, maxLines) {\n  var words = String(str || \"\").split(/\\s+/).filter(Boolean), lines = [], cur = \"\";\n  words.forEach(function (w) {\n    var next = cur ? cur + \" \" + w : w;\n    if (cur && S.measure(next, o) > maxW) { lines.push(cur); cur = w; } else cur = next;\n  });\n  if (cur) lines.push(cur);\n  if (maxLines && lines.length > maxLines) { var rest = lines.slice(maxLines - 1).join(\" \"); lines = lines.slice(0, maxLines - 1).concat([rest]); }\n  return lines;\n}\n\n/** The biggest size (≤ max) at which str fits maxW on one line, in 3D text. */\nfunction fit3d(S, str, o, maxW, max) {\n  var w = S.measure(str, Object.assign({}, o, { size: 100 }));\n  return w > 0 ? Math.min(max, (maxW / w) * 100) : max;\n}\n\n/** A soft contact shadow on the floor under a point (2D, drawn before the scene). */\nfunction floorShadow($, S, p, rx, op) {\n  var q = S.project(p);\n  if (!q) return \"\";\n  var id = $.uid(\"fs\");\n  $.def('<radialGradient id=\"' + id + '\"><stop offset=\"0\" stop-color=\"#000\" stop-opacity=\"' + (op || 0.45) + '\"/><stop offset=\"1\" stop-color=\"#000\" stop-opacity=\"0\"/></radialGradient>');\n  return '<ellipse cx=\"' + $.fx(q.x) + '\" cy=\"' + $.fx(q.y) + '\" rx=\"' + $.fx(rx * q.s) + '\" ry=\"' + $.fx(rx * q.s * 0.18) + '\" fill=\"url(#' + id + ')\"/>';\n}\n\n// An isometric block chart: a floor grid, then a row of blocks rising to their values one after another, the\n// tallest in the accent, each with its value on top and its label on the floor in front.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const tin = $.beat(\"in\", 0.15);\n  let s = M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: W, h: H } }));\n  const rows = (p.rows || []).slice(0, 8);\n  const n = Math.max(1, rows.length);\n  let top = A.y;\n  if (p.title) {\n    const tf = fitFont($, $.case(String(p.title)), \"display\", (A.tall ? 84 : 72) * k, A.w, { ls: -0.03 });\n    s += M.text(\"title\", Object.assign({ at: tin, in: \"rise\", distance: 24 }, tf, { x: A.x, y: A.y, fill: c.fg }), $.case(String(p.title)));\n    top += tf.size * 1.5;\n  }\n  const max = Math.max.apply(null, rows.map(function (r) { return Number(r.value) || 0; }).concat([1]));\n  const big = rows.reduce(function (b, r, i) { return (Number(r.value) || 0) > (Number(rows[b].value) || 0) ? i : b; }, 0);\n  // The blocks stand along the floor's u axis; each is B wide and deep, rising up to Hmax.\n  const roomH = A.y + A.h - top;\n  const cs = 0.866, sn = 0.5;\n  // An isometric row grows down the frame as it grows across it: fit the floor to the width and to half the\n  // height, and give the blocks the rest of the height to rise into.\n  const span = n * 1.5 + 2.5;\n  const B = Math.min(A.w / (span * cs), (roomH * 0.5) / (span * sn), 260 * k);\n  const Hmax = Math.max(roomH * 0.3, roomH - span * B * sn - 70 * k);\n  const floorW = (n * 1.5 + 0.5) * B, floorD = B * 2;\n  // The floor's back-left corner, placed so floor and the tallest block fit.\n  const ox = W / 2 - (floorW - floorD) * cs / 2, oy = top + Hmax + 30 * k;\n  const P = function (u, v, h) { return [ox + (u - v) * cs, oy + (u + v) * sn - (h || 0)]; };\n  const poly = function (pts, o) { return $.path(\"M\" + pts.map(function (q) { return $.fx(q[0]) + \" \" + $.fx(q[1]); }).join(\"L\") + \"Z\", o); };\n  let floor = poly([P(0, 0), P(floorW, 0), P(floorW, floorD), P(0, floorD)], { fill: $.rgba(c.fg, 0.04), stroke: c.line, sw: 1.5 * k });\n  for (let u = B * 0.5; u < floorW; u += B * 0.5) floor += $.line(P(u, 0)[0], P(u, 0)[1], P(u, floorD)[0], P(u, floorD)[1], { stroke: $.rgba(c.fg, 0.06), sw: 1 * k });\n  s += M.part(\"floor\", { at: tin + 0.1, in: \"fade\", dur: 0.5, out: \"fade\" }, floor);\n  const vf = Object.assign({}, $.font(\"mono\"), { size: (A.tall ? 28 : 24) * k, weight: 700 }), lf = Object.assign({}, $.font(\"text\"), { size: (A.tall ? 26 : 22) * k, weight: 600 });\n  let blocks = \"\";\n  rows.forEach(function (r, i) {\n    const at = tin + 0.5 + i * 0.15, g = $.E.outBack($.seg(t, at, at + 0.7));\n    const v = Number(r.value) || 0, h = Math.max(2 * k, Hmax * (v / max) * Math.max(0, g));\n    const u0 = B * (0.5 + i * 1.5), v0 = B * 0.5, u1 = u0 + B, v1 = v0 + B;\n    const col = i === big ? c.accent : $.mix(c.bg, c.accent2, 0.75);\n    const topF = poly([P(u0, v0, h), P(u1, v0, h), P(u1, v1, h), P(u0, v1, h)], { fill: col });\n    const right = poly([P(u1, v0, 0), P(u1, v1, 0), P(u1, v1, h), P(u1, v0, h)], { fill: $.mix(col, \"#000000\", 0.18) });\n    const left = poly([P(u0, v1, 0), P(u1, v1, 0), P(u1, v1, h), P(u0, v1, h)], { fill: $.mix(col, \"#000000\", 0.32) });\n    const tipP = P(u0 + B / 2, v0 + B / 2, h);\n    let m = right + left + topF;\n    m += $.text(fmtValue(v, p.suffix), Object.assign({}, vf, { x: tipP[0], y: tipP[1] - B * 0.5 - vf.size * 1.3, anchor: \"middle\", fill: i === big ? c.accent : c.fg, op: $.fadeIn(t, at + 0.4, 0.3) }));\n    const fp = P(u0 + B / 2, v1 + B * 0.25, 0);\n    m += $.text(String(r.label || \"\"), Object.assign({}, lf, { x: fp[0], y: fp[1] + 10 * k, anchor: \"middle\", fill: c.mute, op: $.fadeIn(t, at, 0.3) }));\n    blocks += m;\n  });\n  s += M.part(\"blocks\", { at: tin + 0.4, in: \"none\", out: \"fade\" }, blocks);\n  return s;\n}\nfunction fmtValue(v, suffix) { return String(v).replace(/\\B(?=(\\d{3})+(?!\\d))/g, \",\") + (suffix || \"\"); }\n","libs":["3d"]}]},{"format":1,"id":"carousels","name":"MONARC Carousels","version":"1.0.0","description":"Cards in motion: a ring that swings card to card, cards orbiting a title, a conveyor along a curve, words circling a centre and a cover flow — your pictures, or designed tiles in the brand's colours.","author":"MONARC STUDIOS","license":"MONARC Packs License 1.0 (free to use in any video; not for resale as templates)","homepage":"https://volito.dev/docs/packs/carousels","style":"riso","shared":"shared.js","items":["ring-carousel","orbit-carousel","path-carousel","text-ring","cover-flow"],"entries":[{"id":"ring-carousel","name":"Ring carousel","description":"Cards round a ring seen from just above: it swings one card at a time, fast then slow, cards shrinking and fading round the back, the one in front named underneath. Your pictures, or tiles in the brand's colours.","category":"scene","tags":["carousel","ring","gallery","3d","portfolio"],"placement":"full","duration":{"default":8,"min":3,"max":60},"props":{"title":{"type":"text","label":"Title","default":"The collection","maxLength":50},"cards":{"type":"list","label":"Cards (Title: tag)","default":[{"title":"Morning light","tag":"Lookbook 01"},{"title":"City nights","tag":"Lookbook 02"},{"title":"Studio session","tag":"Behind the scenes"},{"title":"Coastline","tag":"Travel"},{"title":"Golden hour","tag":"Portraits"},{"title":"Night market","tag":"Street"},{"title":"Snow days","tag":"Winter"},{"title":"Road trip","tag":"Travel"}],"maxItems":16,"minItems":1,"of":{"title":"text","tag":"text"}},"count":{"type":"number","label":"Cards","default":10,"min":4,"max":16},"pace":{"type":"number","label":"Seconds per card","default":1.1,"min":0.6,"max":4,"step":0.1},"direction":{"type":"choice","label":"Turns","options":["right","left"],"default":"right"},"image1":{"type":"image","label":"Picture 1","default":""},"image2":{"type":"image","label":"Picture 2","default":""},"image3":{"type":"image","label":"Picture 3","default":""},"image4":{"type":"image","label":"Picture 4","default":""},"image5":{"type":"image","label":"Picture 5","default":""},"image6":{"type":"image","label":"Picture 6","default":""},"image7":{"type":"image","label":"Picture 7","default":""},"image8":{"type":"image","label":"Picture 8","default":""}},"parts":{"ground":"the ground","title":"the heading","ring":"the cards"},"beats":{"in":0.15,"out":-0.45},"style":"riso","pack":"carousels","ref":"carousels/ring-carousel","code":"// MONARC Carousels — helpers this pack's items share. A card shows its picture when one is given\n// (image1 … image8), else a designed tile in the brand's colours with its title.\n\n/** The cards to show: the list's titles, each with its picture prop when that slot has one. */\nfunction cardsOf($, n) {\n  const list = ($.data.cards || []).map(function (r) { return typeof r === \"string\" ? { title: r } : r; });\n  const out = [];\n  const count = Math.max(1, n || list.length);\n  for (let i = 0; i < count; i++) {\n    const r = list[i % Math.max(1, list.length)] || { title: \"\" };\n    const slot = \"image\" + ((i % 8) + 1);\n    out.push({ title: String(r.title || \"\"), tag: String(r.tag || \"\"), image: $.data[slot] ? slot : \"\", i: i });\n  }\n  return out;\n}\n\n/** A tile colour for card i, from the brand: the accents, blends of them, and the text colour. */\nfunction tileColor($, i) {\n  const c = $.c;\n  const list = [c.accent, c.accent2, $.mix(c.accent, c.accent2, 0.5), $.mix(c.accent, c.fg, 0.35), $.mix(c.accent2, c.fg, 0.3), $.mix(c.accent, c.bg, 0.35)];\n  return list[i % list.length];\n}\n\n/** Text on a card that travels off the frame on purpose (a conveyor, a flow) is not \"cut off\". */\nvar ANYWHERE = { x: -1e5, y: -1e5, w: 2e5, h: 2e5 };\n\n/** One card at (x, y, w, h): its picture, or a tile with a soft pattern, its number and title. o.loose: text may leave the frame. */\nfunction cardAt($, t, card, x, y, w, h, o) {\n  o = o || {};\n  const within = o.loose ? { within: ANYWHERE } : {};\n  const r = Math.min(w, h) * (o.r == null ? 0.08 : o.r);\n  let m = \"\";\n  if (card.image) {\n    m += $.image(card.image, x, y, w, h, { r: r, fit: \"cover\" });\n    if (card.title && o.caption !== false) {\n      m += $.rect(x, y + h * 0.62, w, h * 0.38, { r: r, fill: $.gradient([[0, \"#000000\", 0], [1, \"#000000\", 0.6]], { angle: 180 }) });\n      m += $.text(card.title, Object.assign({ x: x + w * 0.07, y: y + h - w * 0.07 - Math.min(w * 0.09, h * 0.1), size: Math.min(w * 0.09, h * 0.1), weight: 700, fill: \"#FFFFFF\", maxW: w * 0.86 }, within));\n    }\n  } else {\n    const col = tileColor($, card.i), ink = $.on(col);\n    m += $.rect(x, y, w, h, { r: r, fill: $.gradient([col, $.mix(col, ink === \"#FFFFFF\" || ink === $.c.paper ? \"#000000\" : \"#FFFFFF\", 0.18)], { angle: 135 }) });\n    m += $.clip(x, y, w, h, r, $.pattern(t, card.i % 2 ? \"dots\" : \"stripes\", { box: { x: x, y: y, w: w, h: h }, color: ink, a: 0.1, scale: Math.max(0.4, w / 400) }));\n    const num = (card.i + 1 < 10 ? \"0\" : \"\") + (card.i + 1);\n    m += $.text(num, Object.assign({}, $.font(\"display\"), { size: Math.min(w, h) * 0.34, x: x + w * 0.07, y: y + h * 0.06, fill: ink, op: 0.28 }, within));\n    if (card.title && o.caption !== false) {\n      const tf = fitFont($, card.title, \"display\", Math.min(w * 0.12, h * 0.12), w * 0.86, { ls: -0.02 });\n      m += $.text(card.title, Object.assign({}, tf, { x: x + w * 0.07, y: y + h - w * 0.07 - tf.size, fill: ink }, within));\n    }\n  }\n  if (o.stroke) m += $.rect(x, y, w, h, { r: r, stroke: o.stroke, sw: o.sw || 2 });\n  return m;\n}\n\n/** A heading at the top of the safe area (or nothing): { s, y } with y below it. */\nfunction headOf($, M, t, at, A) {\n  const title = String($.data.title || \"\");\n  if (!title) return { s: \"\", y: A.y };\n  const tf = fitFont($, $.case(title), \"display\", (A.tall ? 92 : 80) * $.k, A.w, { ls: -0.03 });\n  return { s: M.text(\"title\", Object.assign({ at: at, in: \"rise\", distance: 26 }, tf, { x: $.W / 2, y: A.y, anchor: \"middle\", fill: $.c.fg }), $.case(title)), y: A.y + tf.size * 1.3 };\n}\n\n/** Position along a loop of n stops, with fast-slow-fast swings: each step eases, then holds `hold` of its slot. */\nfunction swings(t, start, slot, hold) {\n  const u = Math.max(0, t - start) / slot, k = Math.floor(u), f = u - k;\n  const move = Math.min(1, f / (1 - hold));\n  const e = move < 0.5 ? 4 * move * move * move : 1 - Math.pow(-2 * move + 2, 3) / 2;\n  return k + e;\n}\n\n// Cards round a ring seen from just above: the ring swings one card at a time, fast then slow, cards shrink and\n// fade as they go round the back, and the one in front is named underneath.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const tin = $.beat(\"in\", 0.1);\n  let s = M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: W, h: H } }));\n  const head = headOf($, M, t, tin, A);\n  s += head.s;\n  const n = Math.max(4, Math.min(16, Math.round(p.count)));\n  const cards = cardsOf($, n);\n  const capH = 80 * k;\n  const room = { x: A.x, y: head.y, w: A.w, h: A.y + A.h - head.y - capH };\n  const cw = Math.min(room.w * (A.tall ? 0.32 : 0.2), room.h * 0.4), ch = cw * 1.25;\n  const R = Math.min(room.w / 2 - cw * 0.4, room.w * 0.4);\n  // Seen from above: the ring's back rises as far as the room allows, so it reads as a ring, not a row.\n  const tilt = Math.max(0.12, Math.min(0.42, (room.h - ch * 1.05) / (2 * R)));\n  const cx = W / 2, cy = room.y + (room.h - ch * 1.05) / 2 + ch * 0.55;\n  // The ring turns one card per `slot` seconds with eased swings.\n  const slot = Math.max(0.6, Number(p.pace) || 1.1);\n  const pos = swings(t, tin + 0.6, slot, 0.45) * (p.direction === \"left\" ? -1 : 1);\n  const list = cards.map(function (cd, i) {\n    const a = ((i - pos) / n) * 2 * Math.PI;\n    const z = Math.cos(a);\n    return { cd: cd, a: a, z: z, x: cx + R * Math.sin(a), y: cy + R * tilt * z, s: 0.42 + 0.58 * ((z + 1) / 2) };\n  }).sort(function (a, b) { return a.z - b.z; });\n  let ring = $.circle(cx, cy + R * tilt * 0.25, R * 1.02, { stroke: $.rgba(c.fg, 0.08), sw: 2 * k, transform: \"translate(\" + cx + \" \" + cy + \") scale(1 \" + tilt + \") translate(\" + -cx + \" \" + -cy + \")\" });\n  list.forEach(function (q, j) {\n    const w = cw * q.s, h = ch * q.s, op = 0.35 + 0.65 * ((q.z + 1) / 2);\n    const lift = 1 - Math.max(0, q.z);\n    ring += $.g({ op: op }, cardAt($, t, q.cd, q.x - w / 2, q.y - h / 2, w, h, { caption: q.z > 0.6, r: 0.07 }) + (q.z > 0.95 ? \"\" : $.rect(q.x - w / 2, q.y - h / 2, w, h, { r: Math.min(w, h) * 0.07, fill: c.bg, op: lift * 0.25 })));\n  });\n  s += M.part(\"ring\", { at: tin + 0.2, in: \"pop\", out: \"fade\", box: { x: cx - R - cw / 2, y: cy - ch, w: 2 * R + cw, h: ch * 2 } }, ring);\n  // The front card's name, swapping as it settles.\n  const front = cards[((Math.round(pos) % n) + n) % n];\n  const settle = 1 - Math.abs(pos - Math.round(pos)) * 2;\n  if (front.title) {\n    const lf = fitFont($, $.case(front.title), \"display\", 48 * k, A.w, { ls: -0.02 });\n    // Under the front card (which sits at the ring's near edge).\n    s += $.text($.case(front.title), Object.assign({}, lf, { x: cx, y: Math.min(cy + R * tilt + ch / 2 + 20 * k, A.y + A.h - lf.size), anchor: \"middle\", fill: c.fg, op: $.clamp(settle) * $.fadeIn(t, tin + 0.8, 0.3) }));\n  }\n  return s;\n}\n","libs":[]},{"id":"orbit-carousel","name":"Orbit carousel","description":"Cards orbiting a title on a tilted ellipse: they pass behind the words and swing round in front, larger and brighter when near, each tilting with its path.","category":"scene","tags":["carousel","orbit","gallery","title","3d"],"placement":"full","duration":{"default":8,"min":3,"max":60},"props":{"title":{"type":"text","label":"Title","default":"Made this year","maxLength":50},"cards":{"type":"list","label":"Cards (Title: tag)","default":[{"title":"Morning light","tag":"Lookbook 01"},{"title":"City nights","tag":"Lookbook 02"},{"title":"Studio session","tag":"Behind the scenes"},{"title":"Coastline","tag":"Travel"},{"title":"Golden hour","tag":"Portraits"},{"title":"Night market","tag":"Street"},{"title":"Snow days","tag":"Winter"},{"title":"Road trip","tag":"Travel"}],"maxItems":16,"minItems":1,"of":{"title":"text","tag":"text"}},"count":{"type":"number","label":"Cards","default":7,"min":3,"max":10},"speed":{"type":"number","label":"Speed","default":1,"min":0.2,"max":3,"step":0.1},"image1":{"type":"image","label":"Picture 1","default":""},"image2":{"type":"image","label":"Picture 2","default":""},"image3":{"type":"image","label":"Picture 3","default":""},"image4":{"type":"image","label":"Picture 4","default":""},"image5":{"type":"image","label":"Picture 5","default":""},"image6":{"type":"image","label":"Picture 6","default":""},"image7":{"type":"image","label":"Picture 7","default":""},"image8":{"type":"image","label":"Picture 8","default":""}},"parts":{"ground":"the ground","title":"the title","cards":"the cards"},"beats":{"in":0.15,"out":-0.45},"style":"riso","pack":"carousels","ref":"carousels/orbit-carousel","code":"// MONARC Carousels — helpers this pack's items share. A card shows its picture when one is given\n// (image1 … image8), else a designed tile in the brand's colours with its title.\n\n/** The cards to show: the list's titles, each with its picture prop when that slot has one. */\nfunction cardsOf($, n) {\n  const list = ($.data.cards || []).map(function (r) { return typeof r === \"string\" ? { title: r } : r; });\n  const out = [];\n  const count = Math.max(1, n || list.length);\n  for (let i = 0; i < count; i++) {\n    const r = list[i % Math.max(1, list.length)] || { title: \"\" };\n    const slot = \"image\" + ((i % 8) + 1);\n    out.push({ title: String(r.title || \"\"), tag: String(r.tag || \"\"), image: $.data[slot] ? slot : \"\", i: i });\n  }\n  return out;\n}\n\n/** A tile colour for card i, from the brand: the accents, blends of them, and the text colour. */\nfunction tileColor($, i) {\n  const c = $.c;\n  const list = [c.accent, c.accent2, $.mix(c.accent, c.accent2, 0.5), $.mix(c.accent, c.fg, 0.35), $.mix(c.accent2, c.fg, 0.3), $.mix(c.accent, c.bg, 0.35)];\n  return list[i % list.length];\n}\n\n/** Text on a card that travels off the frame on purpose (a conveyor, a flow) is not \"cut off\". */\nvar ANYWHERE = { x: -1e5, y: -1e5, w: 2e5, h: 2e5 };\n\n/** One card at (x, y, w, h): its picture, or a tile with a soft pattern, its number and title. o.loose: text may leave the frame. */\nfunction cardAt($, t, card, x, y, w, h, o) {\n  o = o || {};\n  const within = o.loose ? { within: ANYWHERE } : {};\n  const r = Math.min(w, h) * (o.r == null ? 0.08 : o.r);\n  let m = \"\";\n  if (card.image) {\n    m += $.image(card.image, x, y, w, h, { r: r, fit: \"cover\" });\n    if (card.title && o.caption !== false) {\n      m += $.rect(x, y + h * 0.62, w, h * 0.38, { r: r, fill: $.gradient([[0, \"#000000\", 0], [1, \"#000000\", 0.6]], { angle: 180 }) });\n      m += $.text(card.title, Object.assign({ x: x + w * 0.07, y: y + h - w * 0.07 - Math.min(w * 0.09, h * 0.1), size: Math.min(w * 0.09, h * 0.1), weight: 700, fill: \"#FFFFFF\", maxW: w * 0.86 }, within));\n    }\n  } else {\n    const col = tileColor($, card.i), ink = $.on(col);\n    m += $.rect(x, y, w, h, { r: r, fill: $.gradient([col, $.mix(col, ink === \"#FFFFFF\" || ink === $.c.paper ? \"#000000\" : \"#FFFFFF\", 0.18)], { angle: 135 }) });\n    m += $.clip(x, y, w, h, r, $.pattern(t, card.i % 2 ? \"dots\" : \"stripes\", { box: { x: x, y: y, w: w, h: h }, color: ink, a: 0.1, scale: Math.max(0.4, w / 400) }));\n    const num = (card.i + 1 < 10 ? \"0\" : \"\") + (card.i + 1);\n    m += $.text(num, Object.assign({}, $.font(\"display\"), { size: Math.min(w, h) * 0.34, x: x + w * 0.07, y: y + h * 0.06, fill: ink, op: 0.28 }, within));\n    if (card.title && o.caption !== false) {\n      const tf = fitFont($, card.title, \"display\", Math.min(w * 0.12, h * 0.12), w * 0.86, { ls: -0.02 });\n      m += $.text(card.title, Object.assign({}, tf, { x: x + w * 0.07, y: y + h - w * 0.07 - tf.size, fill: ink }, within));\n    }\n  }\n  if (o.stroke) m += $.rect(x, y, w, h, { r: r, stroke: o.stroke, sw: o.sw || 2 });\n  return m;\n}\n\n/** A heading at the top of the safe area (or nothing): { s, y } with y below it. */\nfunction headOf($, M, t, at, A) {\n  const title = String($.data.title || \"\");\n  if (!title) return { s: \"\", y: A.y };\n  const tf = fitFont($, $.case(title), \"display\", (A.tall ? 92 : 80) * $.k, A.w, { ls: -0.03 });\n  return { s: M.text(\"title\", Object.assign({ at: at, in: \"rise\", distance: 26 }, tf, { x: $.W / 2, y: A.y, anchor: \"middle\", fill: $.c.fg }), $.case(title)), y: A.y + tf.size * 1.3 };\n}\n\n/** Position along a loop of n stops, with fast-slow-fast swings: each step eases, then holds `hold` of its slot. */\nfunction swings(t, start, slot, hold) {\n  const u = Math.max(0, t - start) / slot, k = Math.floor(u), f = u - k;\n  const move = Math.min(1, f / (1 - hold));\n  const e = move < 0.5 ? 4 * move * move * move : 1 - Math.pow(-2 * move + 2, 3) / 2;\n  return k + e;\n}\n\n// Cards orbiting a title on a tilted ellipse: they pass behind the words and swing round in front, larger and\n// brighter when near, each tilting a little with its path.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const tin = $.beat(\"in\", 0.1);\n  let s = M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: W, h: H } }));\n  const n = Math.max(3, Math.min(10, Math.round(p.count)));\n  const cards = cardsOf($, n);\n  const cx = W / 2, cy = A.y + A.h / 2;\n  const cw = Math.min(A.w * (A.tall ? 0.3 : 0.15), A.h * 0.22), ch = cw * 1.2;\n  const rx = A.w * (A.tall ? 0.4 : 0.42) - cw * 0.3, ry = A.h / 2 - ch * 0.55;\n  const speed = (Number(p.speed) || 1) * 0.35;\n  const ang0 = (t - tin) * speed;\n  const items = cards.map(function (cd, i) {\n    const a = ang0 + (i / n) * 2 * Math.PI;\n    const z = Math.sin(a);\n    // Behind the title at the top of the orbit, in front of it at the bottom: clear of the words either way.\n    return { cd: cd, x: cx + rx * Math.cos(a), y: cy + ry * Math.sin(a), z: z, s: 0.55 + 0.45 * ((z + 1) / 2), r: -Math.cos(a) * 8 };\n  });\n  const draw = function (q) {\n    const w = cw * q.s, h = ch * q.s;\n    return $.g({ op: 0.45 + 0.55 * ((q.z + 1) / 2), transform: $.tf({ r: q.r, ox: q.x, oy: q.y }) }, cardAt($, t, q.cd, q.x - w / 2, q.y - h / 2, w, h, { r: 0.08 }));\n  };\n  const back = items.filter(function (q) { return q.z < 0; }).sort(function (a, b) { return a.z - b.z; }).map(draw).join(\"\");\n  const front = items.filter(function (q) { return q.z >= 0; }).sort(function (a, b) { return a.z - b.z; }).map(draw).join(\"\");\n  s += M.part(\"cards\", { at: tin, in: \"fade\", dur: 0.5, out: \"fade\" }, back);\n  // The title sits between the two halves of the orbit.\n  const title = $.case(String(p.title || \"\"));\n  if (title) {\n    const tf = fitLines($, title, Object.assign({}, $.font(\"display\"), { ls: -0.03 }), A.w * (A.tall ? 0.8 : 0.5), (A.tall ? 130 : 120) * k, 50 * k, 3);\n    const top = cy - (tf.lines.length * tf.size * 1.04) / 2;\n    tf.lines.forEach(function (l, i) { s += M.text(\"title\", Object.assign({ at: tin + 0.15 + i * 0.08, in: \"rise\", distance: 30 }, tf.font, { x: cx, y: top + i * tf.size * 1.04, anchor: \"middle\", fill: c.fg, shadow: \"sm\" }), l); });\n  }\n  s += M.part(\"cards\", { at: tin, in: \"fade\", dur: 0.5, out: \"fade\" }, front);\n  return s;\n}\n","libs":[]},{"id":"path-carousel","name":"Path carousel","description":"A conveyor of cards along a gentle S-curve: they glide in from one side, grow as they pass the middle and leave on the other, on a loop (top to bottom on tall frames).","category":"scene","tags":["carousel","conveyor","gallery","marquee","loop"],"placement":"full","duration":{"default":8,"min":3,"max":60},"props":{"title":{"type":"text","label":"Title","default":"","maxLength":50},"cards":{"type":"list","label":"Cards (Title: tag)","default":[{"title":"Morning light","tag":"Lookbook 01"},{"title":"City nights","tag":"Lookbook 02"},{"title":"Studio session","tag":"Behind the scenes"},{"title":"Coastline","tag":"Travel"},{"title":"Golden hour","tag":"Portraits"},{"title":"Night market","tag":"Street"},{"title":"Snow days","tag":"Winter"},{"title":"Road trip","tag":"Travel"}],"maxItems":16,"minItems":1,"of":{"title":"text","tag":"text"}},"count":{"type":"number","label":"Cards","default":8,"min":4,"max":12},"speed":{"type":"number","label":"Speed","default":1,"min":0.2,"max":3,"step":0.1},"image1":{"type":"image","label":"Picture 1","default":""},"image2":{"type":"image","label":"Picture 2","default":""},"image3":{"type":"image","label":"Picture 3","default":""},"image4":{"type":"image","label":"Picture 4","default":""},"image5":{"type":"image","label":"Picture 5","default":""},"image6":{"type":"image","label":"Picture 6","default":""},"image7":{"type":"image","label":"Picture 7","default":""},"image8":{"type":"image","label":"Picture 8","default":""}},"parts":{"ground":"the ground","title":"the heading","path":"the path line","cards":"the cards"},"beats":{"in":0.15,"out":-0.45},"style":"riso","pack":"carousels","ref":"carousels/path-carousel","code":"// MONARC Carousels — helpers this pack's items share. A card shows its picture when one is given\n// (image1 … image8), else a designed tile in the brand's colours with its title.\n\n/** The cards to show: the list's titles, each with its picture prop when that slot has one. */\nfunction cardsOf($, n) {\n  const list = ($.data.cards || []).map(function (r) { return typeof r === \"string\" ? { title: r } : r; });\n  const out = [];\n  const count = Math.max(1, n || list.length);\n  for (let i = 0; i < count; i++) {\n    const r = list[i % Math.max(1, list.length)] || { title: \"\" };\n    const slot = \"image\" + ((i % 8) + 1);\n    out.push({ title: String(r.title || \"\"), tag: String(r.tag || \"\"), image: $.data[slot] ? slot : \"\", i: i });\n  }\n  return out;\n}\n\n/** A tile colour for card i, from the brand: the accents, blends of them, and the text colour. */\nfunction tileColor($, i) {\n  const c = $.c;\n  const list = [c.accent, c.accent2, $.mix(c.accent, c.accent2, 0.5), $.mix(c.accent, c.fg, 0.35), $.mix(c.accent2, c.fg, 0.3), $.mix(c.accent, c.bg, 0.35)];\n  return list[i % list.length];\n}\n\n/** Text on a card that travels off the frame on purpose (a conveyor, a flow) is not \"cut off\". */\nvar ANYWHERE = { x: -1e5, y: -1e5, w: 2e5, h: 2e5 };\n\n/** One card at (x, y, w, h): its picture, or a tile with a soft pattern, its number and title. o.loose: text may leave the frame. */\nfunction cardAt($, t, card, x, y, w, h, o) {\n  o = o || {};\n  const within = o.loose ? { within: ANYWHERE } : {};\n  const r = Math.min(w, h) * (o.r == null ? 0.08 : o.r);\n  let m = \"\";\n  if (card.image) {\n    m += $.image(card.image, x, y, w, h, { r: r, fit: \"cover\" });\n    if (card.title && o.caption !== false) {\n      m += $.rect(x, y + h * 0.62, w, h * 0.38, { r: r, fill: $.gradient([[0, \"#000000\", 0], [1, \"#000000\", 0.6]], { angle: 180 }) });\n      m += $.text(card.title, Object.assign({ x: x + w * 0.07, y: y + h - w * 0.07 - Math.min(w * 0.09, h * 0.1), size: Math.min(w * 0.09, h * 0.1), weight: 700, fill: \"#FFFFFF\", maxW: w * 0.86 }, within));\n    }\n  } else {\n    const col = tileColor($, card.i), ink = $.on(col);\n    m += $.rect(x, y, w, h, { r: r, fill: $.gradient([col, $.mix(col, ink === \"#FFFFFF\" || ink === $.c.paper ? \"#000000\" : \"#FFFFFF\", 0.18)], { angle: 135 }) });\n    m += $.clip(x, y, w, h, r, $.pattern(t, card.i % 2 ? \"dots\" : \"stripes\", { box: { x: x, y: y, w: w, h: h }, color: ink, a: 0.1, scale: Math.max(0.4, w / 400) }));\n    const num = (card.i + 1 < 10 ? \"0\" : \"\") + (card.i + 1);\n    m += $.text(num, Object.assign({}, $.font(\"display\"), { size: Math.min(w, h) * 0.34, x: x + w * 0.07, y: y + h * 0.06, fill: ink, op: 0.28 }, within));\n    if (card.title && o.caption !== false) {\n      const tf = fitFont($, card.title, \"display\", Math.min(w * 0.12, h * 0.12), w * 0.86, { ls: -0.02 });\n      m += $.text(card.title, Object.assign({}, tf, { x: x + w * 0.07, y: y + h - w * 0.07 - tf.size, fill: ink }, within));\n    }\n  }\n  if (o.stroke) m += $.rect(x, y, w, h, { r: r, stroke: o.stroke, sw: o.sw || 2 });\n  return m;\n}\n\n/** A heading at the top of the safe area (or nothing): { s, y } with y below it. */\nfunction headOf($, M, t, at, A) {\n  const title = String($.data.title || \"\");\n  if (!title) return { s: \"\", y: A.y };\n  const tf = fitFont($, $.case(title), \"display\", (A.tall ? 92 : 80) * $.k, A.w, { ls: -0.03 });\n  return { s: M.text(\"title\", Object.assign({ at: at, in: \"rise\", distance: 26 }, tf, { x: $.W / 2, y: A.y, anchor: \"middle\", fill: $.c.fg }), $.case(title)), y: A.y + tf.size * 1.3 };\n}\n\n/** Position along a loop of n stops, with fast-slow-fast swings: each step eases, then holds `hold` of its slot. */\nfunction swings(t, start, slot, hold) {\n  const u = Math.max(0, t - start) / slot, k = Math.floor(u), f = u - k;\n  const move = Math.min(1, f / (1 - hold));\n  const e = move < 0.5 ? 4 * move * move * move : 1 - Math.pow(-2 * move + 2, 3) / 2;\n  return k + e;\n}\n\n// A conveyor of cards along a gentle S-curve across the frame: they glide in from one side, grow as they pass the\n// middle, and leave on the other, on a loop.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const tin = $.beat(\"in\", 0.1);\n  let s = M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: W, h: H } }));\n  const head = headOf($, M, t, tin, A);\n  s += head.s;\n  const n = Math.max(4, Math.min(12, Math.round(p.count)));\n  const cards = cardsOf($, n);\n  const vert = A.tall;\n  const room = { x: A.x, y: head.y, w: A.w, h: A.y + A.h - head.y };\n  const cw = vert ? room.w * 0.5 : Math.min(room.w * 0.2, room.h * 0.5), ch = cw * (vert ? 0.7 : 1.25);\n  // The path: across the frame (down it on tall frames), bowing in a sine; u runs 0 … 1 along it.\n  const P = function (u) {\n    if (vert) return [W / 2 + Math.sin(u * Math.PI * 2) * room.w * 0.18, room.y - ch + u * (room.h + ch * 2)];\n    return [room.x - cw + u * (room.w + cw * 2), room.y + room.h / 2 + Math.sin(u * Math.PI * 2) * room.h * 0.16];\n  };\n  const speed = (Number(p.speed) || 1) * 0.07;\n  let out = [];\n  cards.forEach(function (cd, i) {\n    const u = (((i / n) + (t - tin) * speed) % 1 + 1) % 1;\n    const mid = 1 - Math.min(1, Math.abs(u - 0.5) * 2);\n    const sc = 0.62 + 0.38 * Math.pow(mid, 1.5);\n    const q = P(u), q2 = P(Math.min(1, u + 0.01));\n    const rot = Math.atan2(q2[1] - q[1], q2[0] - q[0]) * 180 / Math.PI * (vert ? 0 : 0.35);\n    out.push({ z: mid, m: $.g({ transform: $.tf({ r: rot, ox: q[0], oy: q[1] }) }, cardAt($, t, cd, q[0] - (cw * sc) / 2, q[1] - (ch * sc) / 2, cw * sc, ch * sc, { r: 0.08, caption: mid > 0.55, loose: true })) });\n  });\n  out.sort(function (a, b) { return a.z - b.z; });\n  const trail = [];\n  for (let i = 0; i <= 40; i++) trail.push(P(i / 40));\n  s += M.part(\"path\", { at: tin, in: \"fade\", dur: 0.6, out: \"fade\", vary: false }, $.path($.smooth(trail), { stroke: $.rgba(c.fg, 0.1), sw: 3 * k, dash: 10 * k + \" \" + 12 * k }));\n  s += M.part(\"cards\", { at: tin + 0.1, in: \"fade\", dur: 0.5, out: \"fade\" }, out.map(function (o) { return o.m; }).join(\"\"));\n  return s;\n}\n","libs":[]},{"id":"text-ring","name":"Text ring","description":"Words set round a circle and turning slowly, a second ring turning the other way inside it, and a big word (or a mark) in the centre — a badge, a loop, a seal.","category":"scene","tags":["text circle","badge","ring","rotating","seal"],"placement":"full","duration":{"default":8,"min":3,"max":60},"props":{"text":{"type":"text","label":"Outer ring","default":"Made with Volito · Motion as code","maxLength":60},"inner":{"type":"text","label":"Inner ring","default":"Any brand · any aspect","maxLength":50},"center":{"type":"text","label":"Centre","default":"V","maxLength":8},"speed":{"type":"number","label":"Speed","default":1,"min":0,"max":4,"step":0.1}},"parts":{"ground":"the ground","rings":"the text rings","center":"the centre"},"beats":{"in":0.15,"out":-0.45},"style":"riso","pack":"carousels","ref":"carousels/text-ring","code":"// MONARC Carousels — helpers this pack's items share. A card shows its picture when one is given\n// (image1 … image8), else a designed tile in the brand's colours with its title.\n\n/** The cards to show: the list's titles, each with its picture prop when that slot has one. */\nfunction cardsOf($, n) {\n  const list = ($.data.cards || []).map(function (r) { return typeof r === \"string\" ? { title: r } : r; });\n  const out = [];\n  const count = Math.max(1, n || list.length);\n  for (let i = 0; i < count; i++) {\n    const r = list[i % Math.max(1, list.length)] || { title: \"\" };\n    const slot = \"image\" + ((i % 8) + 1);\n    out.push({ title: String(r.title || \"\"), tag: String(r.tag || \"\"), image: $.data[slot] ? slot : \"\", i: i });\n  }\n  return out;\n}\n\n/** A tile colour for card i, from the brand: the accents, blends of them, and the text colour. */\nfunction tileColor($, i) {\n  const c = $.c;\n  const list = [c.accent, c.accent2, $.mix(c.accent, c.accent2, 0.5), $.mix(c.accent, c.fg, 0.35), $.mix(c.accent2, c.fg, 0.3), $.mix(c.accent, c.bg, 0.35)];\n  return list[i % list.length];\n}\n\n/** Text on a card that travels off the frame on purpose (a conveyor, a flow) is not \"cut off\". */\nvar ANYWHERE = { x: -1e5, y: -1e5, w: 2e5, h: 2e5 };\n\n/** One card at (x, y, w, h): its picture, or a tile with a soft pattern, its number and title. o.loose: text may leave the frame. */\nfunction cardAt($, t, card, x, y, w, h, o) {\n  o = o || {};\n  const within = o.loose ? { within: ANYWHERE } : {};\n  const r = Math.min(w, h) * (o.r == null ? 0.08 : o.r);\n  let m = \"\";\n  if (card.image) {\n    m += $.image(card.image, x, y, w, h, { r: r, fit: \"cover\" });\n    if (card.title && o.caption !== false) {\n      m += $.rect(x, y + h * 0.62, w, h * 0.38, { r: r, fill: $.gradient([[0, \"#000000\", 0], [1, \"#000000\", 0.6]], { angle: 180 }) });\n      m += $.text(card.title, Object.assign({ x: x + w * 0.07, y: y + h - w * 0.07 - Math.min(w * 0.09, h * 0.1), size: Math.min(w * 0.09, h * 0.1), weight: 700, fill: \"#FFFFFF\", maxW: w * 0.86 }, within));\n    }\n  } else {\n    const col = tileColor($, card.i), ink = $.on(col);\n    m += $.rect(x, y, w, h, { r: r, fill: $.gradient([col, $.mix(col, ink === \"#FFFFFF\" || ink === $.c.paper ? \"#000000\" : \"#FFFFFF\", 0.18)], { angle: 135 }) });\n    m += $.clip(x, y, w, h, r, $.pattern(t, card.i % 2 ? \"dots\" : \"stripes\", { box: { x: x, y: y, w: w, h: h }, color: ink, a: 0.1, scale: Math.max(0.4, w / 400) }));\n    const num = (card.i + 1 < 10 ? \"0\" : \"\") + (card.i + 1);\n    m += $.text(num, Object.assign({}, $.font(\"display\"), { size: Math.min(w, h) * 0.34, x: x + w * 0.07, y: y + h * 0.06, fill: ink, op: 0.28 }, within));\n    if (card.title && o.caption !== false) {\n      const tf = fitFont($, card.title, \"display\", Math.min(w * 0.12, h * 0.12), w * 0.86, { ls: -0.02 });\n      m += $.text(card.title, Object.assign({}, tf, { x: x + w * 0.07, y: y + h - w * 0.07 - tf.size, fill: ink }, within));\n    }\n  }\n  if (o.stroke) m += $.rect(x, y, w, h, { r: r, stroke: o.stroke, sw: o.sw || 2 });\n  return m;\n}\n\n/** A heading at the top of the safe area (or nothing): { s, y } with y below it. */\nfunction headOf($, M, t, at, A) {\n  const title = String($.data.title || \"\");\n  if (!title) return { s: \"\", y: A.y };\n  const tf = fitFont($, $.case(title), \"display\", (A.tall ? 92 : 80) * $.k, A.w, { ls: -0.03 });\n  return { s: M.text(\"title\", Object.assign({ at: at, in: \"rise\", distance: 26 }, tf, { x: $.W / 2, y: A.y, anchor: \"middle\", fill: $.c.fg }), $.case(title)), y: A.y + tf.size * 1.3 };\n}\n\n/** Position along a loop of n stops, with fast-slow-fast swings: each step eases, then holds `hold` of its slot. */\nfunction swings(t, start, slot, hold) {\n  const u = Math.max(0, t - start) / slot, k = Math.floor(u), f = u - k;\n  const move = Math.min(1, f / (1 - hold));\n  const e = move < 0.5 ? 4 * move * move * move : 1 - Math.pow(-2 * move + 2, 3) / 2;\n  return k + e;\n}\n\n// Words set round a circle, turning slowly, a second smaller ring turning the other way inside it, and a big\n// word (or the brand's mark) in the centre.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const tin = $.beat(\"in\", 0.1);\n  let s = M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: W, h: H } }));\n  const cx = W / 2, cy = A.y + A.h / 2;\n  const R = Math.min(A.w, A.h) * 0.44;\n  const speed = (Number(p.speed) || 1) * 14;\n  // One ring of text: each letter placed and turned along the circle; the phrase repeats to fill it.\n  const ring = function (str, r, size, dir, col, phase) {\n    const f = Object.assign({}, $.font(\"mono\"), { size: size, weight: 700 });\n    const unit = (str.toUpperCase() + \"  ·  \");\n    const ulen = $.measure(unit, f) + unit.length * size * 0.12;\n    const reps = Math.max(1, Math.round((2 * Math.PI * r) / ulen));\n    const text = unit.repeat(reps);\n    const step = (2 * Math.PI) / text.length;\n    let g = \"\";\n    for (let i = 0; i < text.length; i++) {\n      const ch = text[i];\n      if (ch === \" \") continue;\n      const a = i * step + ((dir * (t - tin) * speed + phase) * Math.PI) / 180;\n      const x = cx + r * Math.sin(a), y = cy - r * Math.cos(a);\n      g += $.g({ transform: \"translate(\" + $.fx(x) + \" \" + $.fx(y) + \") rotate(\" + $.fx((a * 180) / Math.PI) + \")\" }, $.text(ch, Object.assign({}, f, { x: 0, y: -size * 0.45, anchor: \"middle\", fill: col })));\n    }\n    return g;\n  };\n  const outer = String(p.text || \"\"), inner = String(p.inner || \"\");\n  s += M.part(\"rings\", { at: tin, in: \"pop\", out: \"fade\", box: { x: cx - R, y: cy - R, w: 2 * R, h: 2 * R } },\n    $.circle(cx, cy, R + R * 0.08, { stroke: c.line, sw: 2 * k }) + ring(outer, R, R * 0.085, 1, c.fg, 0) +\n    (inner ? $.circle(cx, cy, R * 0.66, { stroke: c.line, sw: 2 * k }) + ring(inner, R * 0.58, R * 0.07, -1, c.accent, 40) : \"\"));\n  const word = $.case(String(p.center || \"\"));\n  if (word) {\n    const wf = fitFont($, word, \"display\", R * 0.42, R * (inner ? 0.86 : 1.3), { ls: -0.03 });\n    s += M.text(\"center\", Object.assign({ at: tin + 0.2, in: \"pop\" }, wf, { x: cx, y: cy - wf.size * 0.55, anchor: \"middle\", fill: c.accent }), word);\n  } else s += M.part(\"center\", { at: tin + 0.2, in: \"pop\", box: { x: cx - R * 0.2, y: cy - R * 0.2, w: R * 0.4, h: R * 0.4 } }, $.circle(cx, cy, R * 0.16, { fill: c.accent }) + $.icon(\"sparkle\", cx - R * 0.09, cy - R * 0.09, R * 0.18, { color: $.on(c.accent), fill: true }));\n  return s;\n}\n","libs":[]},{"id":"cover-flow","name":"Cover flow","description":"A cover flow: the middle card faces you, the ones either side lean away in a stack, and the row steps along one card at a time, with a reflection on the floor and the centre card's title and tag under it.","category":"scene","tags":["cover flow","carousel","albums","gallery","showcase"],"placement":"full","duration":{"default":8,"min":3,"max":60},"props":{"title":{"type":"text","label":"Title","default":"","maxLength":50},"cards":{"type":"list","label":"Cards (Title: tag)","default":[{"title":"Morning light","tag":"Lookbook 01"},{"title":"City nights","tag":"Lookbook 02"},{"title":"Studio session","tag":"Behind the scenes"},{"title":"Coastline","tag":"Travel"},{"title":"Golden hour","tag":"Portraits"},{"title":"Night market","tag":"Street"},{"title":"Snow days","tag":"Winter"},{"title":"Road trip","tag":"Travel"}],"maxItems":16,"minItems":1,"of":{"title":"text","tag":"text"}},"count":{"type":"number","label":"Cards","default":8,"min":3,"max":12},"pace":{"type":"number","label":"Seconds per card","default":1.2,"min":0.6,"max":4,"step":0.1},"image1":{"type":"image","label":"Picture 1","default":""},"image2":{"type":"image","label":"Picture 2","default":""},"image3":{"type":"image","label":"Picture 3","default":""},"image4":{"type":"image","label":"Picture 4","default":""},"image5":{"type":"image","label":"Picture 5","default":""},"image6":{"type":"image","label":"Picture 6","default":""},"image7":{"type":"image","label":"Picture 7","default":""},"image8":{"type":"image","label":"Picture 8","default":""}},"parts":{"ground":"the ground","title":"the heading","row":"the cards"},"beats":{"in":0.15,"out":-0.45},"style":"riso","pack":"carousels","ref":"carousels/cover-flow","code":"// MONARC Carousels — helpers this pack's items share. A card shows its picture when one is given\n// (image1 … image8), else a designed tile in the brand's colours with its title.\n\n/** The cards to show: the list's titles, each with its picture prop when that slot has one. */\nfunction cardsOf($, n) {\n  const list = ($.data.cards || []).map(function (r) { return typeof r === \"string\" ? { title: r } : r; });\n  const out = [];\n  const count = Math.max(1, n || list.length);\n  for (let i = 0; i < count; i++) {\n    const r = list[i % Math.max(1, list.length)] || { title: \"\" };\n    const slot = \"image\" + ((i % 8) + 1);\n    out.push({ title: String(r.title || \"\"), tag: String(r.tag || \"\"), image: $.data[slot] ? slot : \"\", i: i });\n  }\n  return out;\n}\n\n/** A tile colour for card i, from the brand: the accents, blends of them, and the text colour. */\nfunction tileColor($, i) {\n  const c = $.c;\n  const list = [c.accent, c.accent2, $.mix(c.accent, c.accent2, 0.5), $.mix(c.accent, c.fg, 0.35), $.mix(c.accent2, c.fg, 0.3), $.mix(c.accent, c.bg, 0.35)];\n  return list[i % list.length];\n}\n\n/** Text on a card that travels off the frame on purpose (a conveyor, a flow) is not \"cut off\". */\nvar ANYWHERE = { x: -1e5, y: -1e5, w: 2e5, h: 2e5 };\n\n/** One card at (x, y, w, h): its picture, or a tile with a soft pattern, its number and title. o.loose: text may leave the frame. */\nfunction cardAt($, t, card, x, y, w, h, o) {\n  o = o || {};\n  const within = o.loose ? { within: ANYWHERE } : {};\n  const r = Math.min(w, h) * (o.r == null ? 0.08 : o.r);\n  let m = \"\";\n  if (card.image) {\n    m += $.image(card.image, x, y, w, h, { r: r, fit: \"cover\" });\n    if (card.title && o.caption !== false) {\n      m += $.rect(x, y + h * 0.62, w, h * 0.38, { r: r, fill: $.gradient([[0, \"#000000\", 0], [1, \"#000000\", 0.6]], { angle: 180 }) });\n      m += $.text(card.title, Object.assign({ x: x + w * 0.07, y: y + h - w * 0.07 - Math.min(w * 0.09, h * 0.1), size: Math.min(w * 0.09, h * 0.1), weight: 700, fill: \"#FFFFFF\", maxW: w * 0.86 }, within));\n    }\n  } else {\n    const col = tileColor($, card.i), ink = $.on(col);\n    m += $.rect(x, y, w, h, { r: r, fill: $.gradient([col, $.mix(col, ink === \"#FFFFFF\" || ink === $.c.paper ? \"#000000\" : \"#FFFFFF\", 0.18)], { angle: 135 }) });\n    m += $.clip(x, y, w, h, r, $.pattern(t, card.i % 2 ? \"dots\" : \"stripes\", { box: { x: x, y: y, w: w, h: h }, color: ink, a: 0.1, scale: Math.max(0.4, w / 400) }));\n    const num = (card.i + 1 < 10 ? \"0\" : \"\") + (card.i + 1);\n    m += $.text(num, Object.assign({}, $.font(\"display\"), { size: Math.min(w, h) * 0.34, x: x + w * 0.07, y: y + h * 0.06, fill: ink, op: 0.28 }, within));\n    if (card.title && o.caption !== false) {\n      const tf = fitFont($, card.title, \"display\", Math.min(w * 0.12, h * 0.12), w * 0.86, { ls: -0.02 });\n      m += $.text(card.title, Object.assign({}, tf, { x: x + w * 0.07, y: y + h - w * 0.07 - tf.size, fill: ink }, within));\n    }\n  }\n  if (o.stroke) m += $.rect(x, y, w, h, { r: r, stroke: o.stroke, sw: o.sw || 2 });\n  return m;\n}\n\n/** A heading at the top of the safe area (or nothing): { s, y } with y below it. */\nfunction headOf($, M, t, at, A) {\n  const title = String($.data.title || \"\");\n  if (!title) return { s: \"\", y: A.y };\n  const tf = fitFont($, $.case(title), \"display\", (A.tall ? 92 : 80) * $.k, A.w, { ls: -0.03 });\n  return { s: M.text(\"title\", Object.assign({ at: at, in: \"rise\", distance: 26 }, tf, { x: $.W / 2, y: A.y, anchor: \"middle\", fill: $.c.fg }), $.case(title)), y: A.y + tf.size * 1.3 };\n}\n\n/** Position along a loop of n stops, with fast-slow-fast swings: each step eases, then holds `hold` of its slot. */\nfunction swings(t, start, slot, hold) {\n  const u = Math.max(0, t - start) / slot, k = Math.floor(u), f = u - k;\n  const move = Math.min(1, f / (1 - hold));\n  const e = move < 0.5 ? 4 * move * move * move : 1 - Math.pow(-2 * move + 2, 3) / 2;\n  return k + e;\n}\n\n// A cover flow: the card in the middle faces you, the ones either side lean away in a stack, and the row steps\n// along one card at a time, with a soft reflection on the floor and the centre card's title under it.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const tin = $.beat(\"in\", 0.1);\n  let s = M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: W, h: H } }));\n  const head = headOf($, M, t, tin, A);\n  s += head.s;\n  const n = Math.max(3, Math.min(12, Math.round(p.count)));\n  const cards = cardsOf($, n);\n  const capH = 80 * k;\n  const room = { y: head.y, h: A.y + A.h - head.y - capH };\n  const cw = Math.min(A.w * (A.tall ? 0.6 : 0.3), room.h * 0.62), ch = cw;\n  const cx = W / 2, cy = room.y + room.h * 0.42;\n  const slot = Math.max(0.6, Number(p.pace) || 1.2);\n  const pos = swings(t, tin + 0.6, slot, 0.5) % n;\n  const items = cards.map(function (cd, i) {\n    let d = i - pos;\n    if (d > n / 2) d -= n;\n    if (d < -n / 2) d += n;\n    return { cd: cd, d: d };\n  }).filter(function (q) { return Math.abs(q.d) < (A.tall ? 2.6 : 4.2); }).sort(function (a, b) { return Math.abs(b.d) - Math.abs(a.d); });\n  const gap = cw * 0.32;\n  let row = \"\";\n  items.forEach(function (q) {\n    const a = Math.max(-1, Math.min(1, q.d));\n    // Off centre a card is squeezed (it turns away) and sheared, and stacked closer the further out it is.\n    const x = cx + (Math.abs(q.d) <= 1 ? q.d * (cw * 0.62) : Math.sign(q.d) * (cw * 0.62 + (Math.abs(q.d) - 1) * gap));\n    const sx = 1 - 0.55 * Math.abs(a), sk = -a * 18, sc = 1 - 0.12 * Math.abs(a);\n    const w = cw, h = ch;\n    const card = cardAt($, t, q.cd, -w / 2, -h / 2, w, h, { r: 0.04, caption: false, loose: true });\n    const flip = '<g transform=\"translate(0 ' + $.fx(h * 1.02) + ') scale(1 -1) translate(0 ' + $.fx(-h * 0.0) + ')\" opacity=\"0.18\">' + card + \"</g>\";\n    row += '<g transform=\"translate(' + $.fx(x) + \" \" + $.fx(cy) + \") scale(\" + $.fx(sx * sc) + \" \" + $.fx(sc) + \") skewY(\" + $.fx(sk) + ')\">' + flip + card + \"</g>\";\n  });\n  // A floor fade over the reflections.\n  row += $.rect(0, cy + ch / 2, W, ch * 0.8, { fill: $.gradient([[0, c.bg, 0], [0.5, c.bg, 0.85], [1, c.bg, 1]], { angle: 180 }) });\n  s += M.part(\"row\", { at: tin + 0.2, in: \"fade\", dur: 0.5, out: \"fade\" }, row);\n  const front = cards[((Math.round(pos) % n) + n) % n];\n  const settle = 1 - Math.abs(pos - Math.round(pos)) * 2;\n  if (front.title) {\n    const lf = fitFont($, $.case(front.title), \"display\", 50 * k, A.w, { ls: -0.02 });\n    s += $.text($.case(front.title), Object.assign({}, lf, { x: cx, y: room.y + room.h + 6 * k, anchor: \"middle\", fill: c.fg, op: $.clamp(settle) * $.fadeIn(t, tin + 0.8, 0.3) }));\n    if (front.tag) s += $.text(front.tag, Object.assign({}, $.font(\"mono\"), { size: 24 * k, x: cx, y: room.y + room.h + 6 * k + lf.size * 1.2, anchor: \"middle\", fill: c.mute, op: $.clamp(settle) }));\n  }\n  return s;\n}\n","libs":[]}]},{"format":1,"id":"transitions","name":"MONARC Transitions","version":"1.0.0","description":"Eleven transitions that cover the frame by the cut and reveal the next shot — iris, blinds, doors, glitch, ink, clock, Bauhaus tiles, pixels, stripes, slices and a whip pan — in any brand's colours.","author":"MONARC STUDIOS","license":"MONARC Packs License 1.0 (free to use in any video; not for resale as templates)","homepage":"https://volito.dev/docs/packs/transitions","style":"cobalt","shared":"shared.js","items":["iris","blinds","doors","glitch","ink","clock","tiles","pixels","stripes","slices","whip"],"entries":[{"id":"iris","name":"Iris","description":"A camera iris: the frame closes down to a point through a ring of the accent, then opens on the next shot. Circle, diamond, star or square, from any point.","category":"transition","tags":["transition","iris","circle","close","open","shape"],"placement":"overlay","duration":{"default":1.2,"min":0.5,"max":4},"aspects":["16:9","9:16","1:1","4:5"],"props":{"shape":{"type":"choice","label":"Shape","options":["circle","diamond","star","square"],"default":"circle"},"x":{"type":"number","label":"Centre across (%)","default":50,"min":0,"max":100,"step":1},"y":{"type":"number","label":"Centre down (%)","default":50,"min":0,"max":100,"step":1},"colors":{"type":"choice","label":"Colours","options":["brand","accent","black","white"],"default":"brand","description":"brand: the accents lead and the brand's ground covers"},"label":{"type":"text","label":"Word at the cut","default":"","maxLength":24,"description":"shown while the frame is covered (empty for none)"}},"parts":{"cover":"the shapes that cover the frame","label":"the word at the cut"},"style":"cobalt","pack":"transitions","ref":"transitions/iris","code":"// Transitions cover the frame by the cut (the midpoint, or the \"cut\" beat) and uncover after it: cut your\n// two clips there. Everything is transparent at the first and last frame.\nfunction cutAt($) { return $.clamp($.beat(\"cut\", $.dur / 2), 0.1, $.dur - 0.1); }\n/** Progress before and after the cut: cover 0→1, then uncover 0→1. */\nfunction phases($, t) {\n  const mid = cutAt($);\n  return { mid: mid, cover: $.clamp(t / mid), uncover: $.clamp((t - mid) / ($.dur - mid)), before: t < mid };\n}\n/** The sound of a cut: air as the cover arrives, a soft low drop on the cut, air going as it opens (and the label's pop). */\nfunction sound($) {\n  const mid = cutAt($);\n  return {\n    cues: [\n      { at: Math.max(0, mid - 0.45), kind: \"whoosh\", dur: Math.min(0.5, mid), level: 0.9 },\n      { at: mid, kind: \"sub\", level: 0.55 },\n      { at: mid + 0.04, kind: \"whoosh\", dur: Math.min(0.5, $.dur - mid), level: 0.55, pitch: -3, pan: 0.2 },\n    ],\n    auto: true,\n  };\n}\n/** The paints: lead (the edge that arrives first), lead2, and cover (what fills the frame at the cut). */\nfunction paints($, scheme) {\n  const c = $.c;\n  if (scheme === \"accent\") return { lead: c.accent2, lead2: c.bg, cover: c.accent };\n  if (scheme === \"black\") return { lead: c.accent, lead2: c.accent2, cover: \"#0A0A0C\" };\n  if (scheme === \"white\") return { lead: c.accent, lead2: c.accent2, cover: \"#FFFFFF\" };\n  return { lead: c.accent, lead2: c.accent2, cover: c.bg };\n}\n/** The word shown while the frame is covered (on the cover colour). */\nfunction cutWord(M, $, t, label, bg) {\n  if (!label) return \"\";\n  const mid = cutAt($), k = $.k, W = $.W, H = $.H, after = $.dur - mid;\n  const str = $.case(label);\n  const f = fitFont($, str, \"display\", 150 * k, Math.min(W, H * 1.4) * 0.74, { ls: -0.03 });\n  return M.text(\"label\", Object.assign({ at: Math.max(0, mid - Math.min(0.3, mid * 0.5)), in: \"pop\", outAt: mid + after * 0.25, out: \"fade\", outDur: after * 0.3 }, f, { x: W / 2, y: H / 2 - f.size * 0.55, anchor: \"middle\", fill: $.on(bg) }), str);\n}\n/** The whole frame with a hole cut in it (hole: path data). */\nfunction frameWithHole($, hole, fill) {\n  return $.path(\"M0 0H\" + $.W + \"V\" + $.H + \"H0Z\" + hole, { fill: fill, extra: 'fill-rule=\"evenodd\"' });\n}\n/** A closed shape as path data: circle, diamond, star or square, radius r about (cx, cy). */\nfunction shapePath(shape, cx, cy, r) {\n  if (r <= 0.01) return \"\";\n  if (shape === \"diamond\") return \"M\" + cx + \" \" + (cy - r) + \"L\" + (cx + r) + \" \" + cy + \"L\" + cx + \" \" + (cy + r) + \"L\" + (cx - r) + \" \" + cy + \"Z\";\n  if (shape === \"square\") return \"M\" + (cx - r) + \" \" + (cy - r) + \"H\" + (cx + r) + \"V\" + (cy + r) + \"H\" + (cx - r) + \"Z\";\n  if (shape === \"star\") {\n    let d = \"\";\n    for (let i = 0; i < 10; i++) {\n      const a = -Math.PI / 2 + (i * Math.PI) / 5, rr = i % 2 ? r * 0.5 : r;\n      d += (i ? \"L\" : \"M\") + (cx + rr * Math.cos(a)) + \" \" + (cy + rr * Math.sin(a));\n    }\n    return d + \"Z\";\n  }\n  return \"M\" + (cx + r) + \" \" + cy + \"A\" + r + \" \" + r + \" 0 1 0 \" + (cx - r) + \" \" + cy + \"A\" + r + \" \" + r + \" 0 1 0 \" + (cx + r) + \" \" + cy + \"Z\";\n}\n/** A wobbly closed blob through n points (radius r, lumps seeded), as smooth path data. */\nfunction blobPath(cx, cy, r, seed, t) {\n  const n = 48, pts = [];\n  for (let i = 0; i < n; i++) {\n    const a = (i / n) * Math.PI * 2;\n    const q = 1 + 0.1 * Math.sin(3 * a + seed) + 0.06 * Math.sin(7 * a + seed * 2.3 + t * 2) + 0.035 * Math.sin(13 * a + seed * 4.1 - t * 3);\n    pts.push([cx + Math.cos(a) * r * q, cy + Math.sin(a) * r * q]);\n  }\n  let d = \"M\" + ((pts[0][0] + pts[n - 1][0]) / 2) + \" \" + ((pts[0][1] + pts[n - 1][1]) / 2);\n  for (let i = 0; i < n; i++) {\n    const p = pts[i], q2 = pts[(i + 1) % n];\n    d += \"Q\" + p[0] + \" \" + p[1] + \" \" + (p[0] + q2[0]) / 2 + \" \" + (p[1] + q2[1]) / 2;\n  }\n  return d + \"Z\";\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const M = motion($, t);\n  const ph = phases($, t), col = paints($, p.colors);\n  let s = \"\";\n  const cx = W * p.x / 100, cy = H * p.y / 100;\n  const far = Math.hypot(Math.max(cx, W - cx), Math.max(cy, H - cy));\n  // How far the shape must open to clear the frame.\n  const R = far * (p.shape === \"star\" ? 2.6 : p.shape === \"diamond\" ? 1.45 : p.shape === \"square\" ? 1.02 : 1.02);\n  const e = ph.before ? 1 - $.E.inOut3(ph.cover) : $.E.inOut3(ph.uncover);\n  if (e < 1) {\n    const band = 34 * k * e + (1 - e) * 0;\n    s += frameWithHole($, shapePath(p.shape, cx, cy, R * e), col.lead);\n    s += frameWithHole($, shapePath(p.shape, cx, cy, R * e + band * 0.5), col.lead2);\n    s += frameWithHole($, shapePath(p.shape, cx, cy, R * e + band), col.cover);\n  }\n  s = M.part(\"cover\", { in: \"none\", out: \"none\", vary: false }, s);\n  return s + cutWord(M, $, t, p.label, col.cover);\n}\n","libs":[]},{"id":"blinds","name":"Blinds","description":"Venetian blinds: slats swing shut one after another in alternating colours, then open on the next shot.","category":"transition","tags":["transition","blinds","slats","stripes","venetian"],"placement":"overlay","duration":{"default":1.2,"min":0.5,"max":4},"aspects":["16:9","9:16","1:1","4:5"],"props":{"slats":{"type":"number","label":"Slats","default":8,"min":3,"max":20,"step":1},"direction":{"type":"choice","label":"Slats run","options":["across","down"],"default":"across"},"colors":{"type":"choice","label":"Colours","options":["brand","accent","black","white"],"default":"brand","description":"brand: the accents lead and the brand's ground covers"},"label":{"type":"text","label":"Word at the cut","default":"","maxLength":24,"description":"shown while the frame is covered (empty for none)"}},"parts":{"cover":"the shapes that cover the frame","label":"the word at the cut"},"style":"cobalt","pack":"transitions","ref":"transitions/blinds","code":"// Transitions cover the frame by the cut (the midpoint, or the \"cut\" beat) and uncover after it: cut your\n// two clips there. Everything is transparent at the first and last frame.\nfunction cutAt($) { return $.clamp($.beat(\"cut\", $.dur / 2), 0.1, $.dur - 0.1); }\n/** Progress before and after the cut: cover 0→1, then uncover 0→1. */\nfunction phases($, t) {\n  const mid = cutAt($);\n  return { mid: mid, cover: $.clamp(t / mid), uncover: $.clamp((t - mid) / ($.dur - mid)), before: t < mid };\n}\n/** The sound of a cut: air as the cover arrives, a soft low drop on the cut, air going as it opens (and the label's pop). */\nfunction sound($) {\n  const mid = cutAt($);\n  return {\n    cues: [\n      { at: Math.max(0, mid - 0.45), kind: \"whoosh\", dur: Math.min(0.5, mid), level: 0.9 },\n      { at: mid, kind: \"sub\", level: 0.55 },\n      { at: mid + 0.04, kind: \"whoosh\", dur: Math.min(0.5, $.dur - mid), level: 0.55, pitch: -3, pan: 0.2 },\n    ],\n    auto: true,\n  };\n}\n/** The paints: lead (the edge that arrives first), lead2, and cover (what fills the frame at the cut). */\nfunction paints($, scheme) {\n  const c = $.c;\n  if (scheme === \"accent\") return { lead: c.accent2, lead2: c.bg, cover: c.accent };\n  if (scheme === \"black\") return { lead: c.accent, lead2: c.accent2, cover: \"#0A0A0C\" };\n  if (scheme === \"white\") return { lead: c.accent, lead2: c.accent2, cover: \"#FFFFFF\" };\n  return { lead: c.accent, lead2: c.accent2, cover: c.bg };\n}\n/** The word shown while the frame is covered (on the cover colour). */\nfunction cutWord(M, $, t, label, bg) {\n  if (!label) return \"\";\n  const mid = cutAt($), k = $.k, W = $.W, H = $.H, after = $.dur - mid;\n  const str = $.case(label);\n  const f = fitFont($, str, \"display\", 150 * k, Math.min(W, H * 1.4) * 0.74, { ls: -0.03 });\n  return M.text(\"label\", Object.assign({ at: Math.max(0, mid - Math.min(0.3, mid * 0.5)), in: \"pop\", outAt: mid + after * 0.25, out: \"fade\", outDur: after * 0.3 }, f, { x: W / 2, y: H / 2 - f.size * 0.55, anchor: \"middle\", fill: $.on(bg) }), str);\n}\n/** The whole frame with a hole cut in it (hole: path data). */\nfunction frameWithHole($, hole, fill) {\n  return $.path(\"M0 0H\" + $.W + \"V\" + $.H + \"H0Z\" + hole, { fill: fill, extra: 'fill-rule=\"evenodd\"' });\n}\n/** A closed shape as path data: circle, diamond, star or square, radius r about (cx, cy). */\nfunction shapePath(shape, cx, cy, r) {\n  if (r <= 0.01) return \"\";\n  if (shape === \"diamond\") return \"M\" + cx + \" \" + (cy - r) + \"L\" + (cx + r) + \" \" + cy + \"L\" + cx + \" \" + (cy + r) + \"L\" + (cx - r) + \" \" + cy + \"Z\";\n  if (shape === \"square\") return \"M\" + (cx - r) + \" \" + (cy - r) + \"H\" + (cx + r) + \"V\" + (cy + r) + \"H\" + (cx - r) + \"Z\";\n  if (shape === \"star\") {\n    let d = \"\";\n    for (let i = 0; i < 10; i++) {\n      const a = -Math.PI / 2 + (i * Math.PI) / 5, rr = i % 2 ? r * 0.5 : r;\n      d += (i ? \"L\" : \"M\") + (cx + rr * Math.cos(a)) + \" \" + (cy + rr * Math.sin(a));\n    }\n    return d + \"Z\";\n  }\n  return \"M\" + (cx + r) + \" \" + cy + \"A\" + r + \" \" + r + \" 0 1 0 \" + (cx - r) + \" \" + cy + \"A\" + r + \" \" + r + \" 0 1 0 \" + (cx + r) + \" \" + cy + \"Z\";\n}\n/** A wobbly closed blob through n points (radius r, lumps seeded), as smooth path data. */\nfunction blobPath(cx, cy, r, seed, t) {\n  const n = 48, pts = [];\n  for (let i = 0; i < n; i++) {\n    const a = (i / n) * Math.PI * 2;\n    const q = 1 + 0.1 * Math.sin(3 * a + seed) + 0.06 * Math.sin(7 * a + seed * 2.3 + t * 2) + 0.035 * Math.sin(13 * a + seed * 4.1 - t * 3);\n    pts.push([cx + Math.cos(a) * r * q, cy + Math.sin(a) * r * q]);\n  }\n  let d = \"M\" + ((pts[0][0] + pts[n - 1][0]) / 2) + \" \" + ((pts[0][1] + pts[n - 1][1]) / 2);\n  for (let i = 0; i < n; i++) {\n    const p = pts[i], q2 = pts[(i + 1) % n];\n    d += \"Q\" + p[0] + \" \" + p[1] + \" \" + (p[0] + q2[0]) / 2 + \" \" + (p[1] + q2[1]) / 2;\n  }\n  return d + \"Z\";\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const M = motion($, t);\n  const ph = phases($, t), col = paints($, p.colors);\n  let s = \"\";\n  const n = Math.max(3, Math.round(p.slats)), across = p.direction === \"across\";\n  const L = across ? H : W, band = L / n;\n  for (let i = 0; i < n; i++) {\n    const lag = (i / (n - 1)) * 0.4;\n    const ci = $.E.inOut3($.clamp((ph.cover - lag) / (1 - lag)));\n    const ui = $.E.inOut3($.clamp((ph.uncover - lag) / (1 - lag)));\n    const f = ph.before ? ci : 1 - ui;\n    if (f <= 0) continue;\n    const h = band * f + 1;\n    // Closing they grow down from the slat's top; opening they shrink to its bottom.\n    const a = i * band + (ph.before ? 0 : band - h);\n    const fill = i % 2 ? col.lead : col.cover;\n    s += across ? $.rect(0, a, W, h, { fill: fill }) : $.rect(a, 0, h, H, { fill: fill });\n  }\n  // Once shut, the cover colour fills the frame for the word.\n  if (Math.abs(t - ph.mid) < 0.06 * $.dur && p.label) s += $.rect(0, 0, W, H, { fill: col.cover });\n  s = M.part(\"cover\", { in: \"none\", out: \"none\", vary: false }, s);\n  return s + cutWord(M, $, t, p.label, col.cover);\n}\n","libs":[]},{"id":"doors","name":"Doors","description":"Two doors slide shut and a Bauhaus disc locks them, then they slide open on the next shot. Across on wide frames, top and bottom on tall ones.","category":"transition","tags":["transition","doors","split","close","open","bauhaus"],"placement":"overlay","duration":{"default":1.4,"min":0.6,"max":4},"aspects":["16:9","9:16","1:1","4:5"],"props":{"disc":{"type":"boolean","label":"Disc at the seam","default":true},"colors":{"type":"choice","label":"Colours","options":["brand","accent","black","white"],"default":"brand","description":"brand: the accents lead and the brand's ground covers"},"label":{"type":"text","label":"Word at the cut","default":"","maxLength":24,"description":"shown while the frame is covered (empty for none)"}},"parts":{"cover":"the shapes that cover the frame","label":"the word at the cut"},"style":"cobalt","pack":"transitions","ref":"transitions/doors","code":"// Transitions cover the frame by the cut (the midpoint, or the \"cut\" beat) and uncover after it: cut your\n// two clips there. Everything is transparent at the first and last frame.\nfunction cutAt($) { return $.clamp($.beat(\"cut\", $.dur / 2), 0.1, $.dur - 0.1); }\n/** Progress before and after the cut: cover 0→1, then uncover 0→1. */\nfunction phases($, t) {\n  const mid = cutAt($);\n  return { mid: mid, cover: $.clamp(t / mid), uncover: $.clamp((t - mid) / ($.dur - mid)), before: t < mid };\n}\n/** The sound of a cut: air as the cover arrives, a soft low drop on the cut, air going as it opens (and the label's pop). */\nfunction sound($) {\n  const mid = cutAt($);\n  return {\n    cues: [\n      { at: Math.max(0, mid - 0.45), kind: \"whoosh\", dur: Math.min(0.5, mid), level: 0.9 },\n      { at: mid, kind: \"sub\", level: 0.55 },\n      { at: mid + 0.04, kind: \"whoosh\", dur: Math.min(0.5, $.dur - mid), level: 0.55, pitch: -3, pan: 0.2 },\n    ],\n    auto: true,\n  };\n}\n/** The paints: lead (the edge that arrives first), lead2, and cover (what fills the frame at the cut). */\nfunction paints($, scheme) {\n  const c = $.c;\n  if (scheme === \"accent\") return { lead: c.accent2, lead2: c.bg, cover: c.accent };\n  if (scheme === \"black\") return { lead: c.accent, lead2: c.accent2, cover: \"#0A0A0C\" };\n  if (scheme === \"white\") return { lead: c.accent, lead2: c.accent2, cover: \"#FFFFFF\" };\n  return { lead: c.accent, lead2: c.accent2, cover: c.bg };\n}\n/** The word shown while the frame is covered (on the cover colour). */\nfunction cutWord(M, $, t, label, bg) {\n  if (!label) return \"\";\n  const mid = cutAt($), k = $.k, W = $.W, H = $.H, after = $.dur - mid;\n  const str = $.case(label);\n  const f = fitFont($, str, \"display\", 150 * k, Math.min(W, H * 1.4) * 0.74, { ls: -0.03 });\n  return M.text(\"label\", Object.assign({ at: Math.max(0, mid - Math.min(0.3, mid * 0.5)), in: \"pop\", outAt: mid + after * 0.25, out: \"fade\", outDur: after * 0.3 }, f, { x: W / 2, y: H / 2 - f.size * 0.55, anchor: \"middle\", fill: $.on(bg) }), str);\n}\n/** The whole frame with a hole cut in it (hole: path data). */\nfunction frameWithHole($, hole, fill) {\n  return $.path(\"M0 0H\" + $.W + \"V\" + $.H + \"H0Z\" + hole, { fill: fill, extra: 'fill-rule=\"evenodd\"' });\n}\n/** A closed shape as path data: circle, diamond, star or square, radius r about (cx, cy). */\nfunction shapePath(shape, cx, cy, r) {\n  if (r <= 0.01) return \"\";\n  if (shape === \"diamond\") return \"M\" + cx + \" \" + (cy - r) + \"L\" + (cx + r) + \" \" + cy + \"L\" + cx + \" \" + (cy + r) + \"L\" + (cx - r) + \" \" + cy + \"Z\";\n  if (shape === \"square\") return \"M\" + (cx - r) + \" \" + (cy - r) + \"H\" + (cx + r) + \"V\" + (cy + r) + \"H\" + (cx - r) + \"Z\";\n  if (shape === \"star\") {\n    let d = \"\";\n    for (let i = 0; i < 10; i++) {\n      const a = -Math.PI / 2 + (i * Math.PI) / 5, rr = i % 2 ? r * 0.5 : r;\n      d += (i ? \"L\" : \"M\") + (cx + rr * Math.cos(a)) + \" \" + (cy + rr * Math.sin(a));\n    }\n    return d + \"Z\";\n  }\n  return \"M\" + (cx + r) + \" \" + cy + \"A\" + r + \" \" + r + \" 0 1 0 \" + (cx - r) + \" \" + cy + \"A\" + r + \" \" + r + \" 0 1 0 \" + (cx + r) + \" \" + cy + \"Z\";\n}\n/** A wobbly closed blob through n points (radius r, lumps seeded), as smooth path data. */\nfunction blobPath(cx, cy, r, seed, t) {\n  const n = 48, pts = [];\n  for (let i = 0; i < n; i++) {\n    const a = (i / n) * Math.PI * 2;\n    const q = 1 + 0.1 * Math.sin(3 * a + seed) + 0.06 * Math.sin(7 * a + seed * 2.3 + t * 2) + 0.035 * Math.sin(13 * a + seed * 4.1 - t * 3);\n    pts.push([cx + Math.cos(a) * r * q, cy + Math.sin(a) * r * q]);\n  }\n  let d = \"M\" + ((pts[0][0] + pts[n - 1][0]) / 2) + \" \" + ((pts[0][1] + pts[n - 1][1]) / 2);\n  for (let i = 0; i < n; i++) {\n    const p = pts[i], q2 = pts[(i + 1) % n];\n    d += \"Q\" + p[0] + \" \" + p[1] + \" \" + (p[0] + q2[0]) / 2 + \" \" + (p[1] + q2[1]) / 2;\n  }\n  return d + \"Z\";\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const M = motion($, t);\n  const ph = phases($, t), col = paints($, p.colors);\n  let s = \"\";\n  const tall = H > W * 1.2;\n  const L = tall ? H : W, half = L / 2;\n  const e = ph.before ? $.E.out3(ph.cover) : 1 - $.E.in3(ph.uncover);\n  const off = half * (1 - e);\n  const R = Math.min(W, H) * 0.17;\n  const doorA = tall ? $.rect(0, -off, W, half + 1, { fill: col.cover }) + $.rect(0, half - off - 10 * k, W, 10 * k, { fill: col.lead }) : $.rect(-off, 0, half + 1, H, { fill: col.cover }) + $.rect(half - off - 10 * k, 0, 10 * k, H, { fill: col.lead });\n  const doorB = tall ? $.rect(0, half + off, W, half, { fill: col.cover }) + $.rect(0, half + off, W, 10 * k, { fill: col.lead2 }) : $.rect(half + off, 0, half, H, { fill: col.cover }) + $.rect(half + off, 0, 10 * k, H, { fill: col.lead2 });\n  s += doorA + doorB;\n  if (p.disc && !p.label) {\n    // Each door carries half a disc; shut, they make one.\n    const cx = W / 2, cy = H / 2;\n    const spin = ph.before ? (1 - e) * -90 : (1 - e) * 90;\n    if (tall) {\n      s += $.path(\"M\" + (cx - R) + \" \" + (cy - off) + \"A\" + R + \" \" + R + \" 0 0 1 \" + (cx + R) + \" \" + (cy - off) + \"Z\", { fill: col.lead, transform: \"rotate(\" + spin + \" \" + cx + \" \" + (cy - off) + \")\" });\n      s += $.path(\"M\" + (cx - R) + \" \" + (cy + off) + \"A\" + R + \" \" + R + \" 0 0 0 \" + (cx + R) + \" \" + (cy + off) + \"Z\", { fill: col.lead2, transform: \"rotate(\" + spin + \" \" + cx + \" \" + (cy + off) + \")\" });\n    } else {\n      s += $.path(\"M\" + (cx - off) + \" \" + (cy - R) + \"A\" + R + \" \" + R + \" 0 0 0 \" + (cx - off) + \" \" + (cy + R) + \"Z\", { fill: col.lead, transform: \"rotate(\" + spin + \" \" + (cx - off) + \" \" + cy + \")\" });\n      s += $.path(\"M\" + (cx + off) + \" \" + (cy - R) + \"A\" + R + \" \" + R + \" 0 0 1 \" + (cx + off) + \" \" + (cy + R) + \"Z\", { fill: col.lead2, transform: \"rotate(\" + spin + \" \" + (cx + off) + \" \" + cy + \")\" });\n    }\n  }\n  s = M.part(\"cover\", { in: \"none\", out: \"none\", vary: false }, s);\n  return s + cutWord(M, $, t, p.label, col.cover);\n}\n","libs":[]},{"id":"glitch","name":"Glitch","description":"A digital glitch: torn slices and RGB-split blocks flicker in, the frame breaks fully at the cut, then glitches out to the next shot.","category":"transition","tags":["transition","glitch","digital","rgb","noise","tech"],"placement":"overlay","duration":{"default":1,"min":0.4,"max":3},"aspects":["16:9","9:16","1:1","4:5"],"props":{"intensity":{"type":"number","label":"Intensity","default":1,"min":0.3,"max":1.5,"step":0.1},"colors":{"type":"choice","label":"Colours","options":["brand","accent","black","white"],"default":"brand","description":"brand: the accents lead and the brand's ground covers"},"label":{"type":"text","label":"Word at the cut","default":"","maxLength":24,"description":"shown while the frame is covered (empty for none)"}},"parts":{"cover":"the shapes that cover the frame","label":"the word at the cut"},"style":"cobalt","pack":"transitions","ref":"transitions/glitch","code":"// Transitions cover the frame by the cut (the midpoint, or the \"cut\" beat) and uncover after it: cut your\n// two clips there. Everything is transparent at the first and last frame.\nfunction cutAt($) { return $.clamp($.beat(\"cut\", $.dur / 2), 0.1, $.dur - 0.1); }\n/** Progress before and after the cut: cover 0→1, then uncover 0→1. */\nfunction phases($, t) {\n  const mid = cutAt($);\n  return { mid: mid, cover: $.clamp(t / mid), uncover: $.clamp((t - mid) / ($.dur - mid)), before: t < mid };\n}\n/** The sound of a cut: air as the cover arrives, a soft low drop on the cut, air going as it opens (and the label's pop). */\nfunction sound($) {\n  const mid = cutAt($);\n  return {\n    cues: [\n      { at: Math.max(0, mid - 0.45), kind: \"whoosh\", dur: Math.min(0.5, mid), level: 0.9 },\n      { at: mid, kind: \"sub\", level: 0.55 },\n      { at: mid + 0.04, kind: \"whoosh\", dur: Math.min(0.5, $.dur - mid), level: 0.55, pitch: -3, pan: 0.2 },\n    ],\n    auto: true,\n  };\n}\n/** The paints: lead (the edge that arrives first), lead2, and cover (what fills the frame at the cut). */\nfunction paints($, scheme) {\n  const c = $.c;\n  if (scheme === \"accent\") return { lead: c.accent2, lead2: c.bg, cover: c.accent };\n  if (scheme === \"black\") return { lead: c.accent, lead2: c.accent2, cover: \"#0A0A0C\" };\n  if (scheme === \"white\") return { lead: c.accent, lead2: c.accent2, cover: \"#FFFFFF\" };\n  return { lead: c.accent, lead2: c.accent2, cover: c.bg };\n}\n/** The word shown while the frame is covered (on the cover colour). */\nfunction cutWord(M, $, t, label, bg) {\n  if (!label) return \"\";\n  const mid = cutAt($), k = $.k, W = $.W, H = $.H, after = $.dur - mid;\n  const str = $.case(label);\n  const f = fitFont($, str, \"display\", 150 * k, Math.min(W, H * 1.4) * 0.74, { ls: -0.03 });\n  return M.text(\"label\", Object.assign({ at: Math.max(0, mid - Math.min(0.3, mid * 0.5)), in: \"pop\", outAt: mid + after * 0.25, out: \"fade\", outDur: after * 0.3 }, f, { x: W / 2, y: H / 2 - f.size * 0.55, anchor: \"middle\", fill: $.on(bg) }), str);\n}\n/** The whole frame with a hole cut in it (hole: path data). */\nfunction frameWithHole($, hole, fill) {\n  return $.path(\"M0 0H\" + $.W + \"V\" + $.H + \"H0Z\" + hole, { fill: fill, extra: 'fill-rule=\"evenodd\"' });\n}\n/** A closed shape as path data: circle, diamond, star or square, radius r about (cx, cy). */\nfunction shapePath(shape, cx, cy, r) {\n  if (r <= 0.01) return \"\";\n  if (shape === \"diamond\") return \"M\" + cx + \" \" + (cy - r) + \"L\" + (cx + r) + \" \" + cy + \"L\" + cx + \" \" + (cy + r) + \"L\" + (cx - r) + \" \" + cy + \"Z\";\n  if (shape === \"square\") return \"M\" + (cx - r) + \" \" + (cy - r) + \"H\" + (cx + r) + \"V\" + (cy + r) + \"H\" + (cx - r) + \"Z\";\n  if (shape === \"star\") {\n    let d = \"\";\n    for (let i = 0; i < 10; i++) {\n      const a = -Math.PI / 2 + (i * Math.PI) / 5, rr = i % 2 ? r * 0.5 : r;\n      d += (i ? \"L\" : \"M\") + (cx + rr * Math.cos(a)) + \" \" + (cy + rr * Math.sin(a));\n    }\n    return d + \"Z\";\n  }\n  return \"M\" + (cx + r) + \" \" + cy + \"A\" + r + \" \" + r + \" 0 1 0 \" + (cx - r) + \" \" + cy + \"A\" + r + \" \" + r + \" 0 1 0 \" + (cx + r) + \" \" + cy + \"Z\";\n}\n/** A wobbly closed blob through n points (radius r, lumps seeded), as smooth path data. */\nfunction blobPath(cx, cy, r, seed, t) {\n  const n = 48, pts = [];\n  for (let i = 0; i < n; i++) {\n    const a = (i / n) * Math.PI * 2;\n    const q = 1 + 0.1 * Math.sin(3 * a + seed) + 0.06 * Math.sin(7 * a + seed * 2.3 + t * 2) + 0.035 * Math.sin(13 * a + seed * 4.1 - t * 3);\n    pts.push([cx + Math.cos(a) * r * q, cy + Math.sin(a) * r * q]);\n  }\n  let d = \"M\" + ((pts[0][0] + pts[n - 1][0]) / 2) + \" \" + ((pts[0][1] + pts[n - 1][1]) / 2);\n  for (let i = 0; i < n; i++) {\n    const p = pts[i], q2 = pts[(i + 1) % n];\n    d += \"Q\" + p[0] + \" \" + p[1] + \" \" + (p[0] + q2[0]) / 2 + \" \" + (p[1] + q2[1]) / 2;\n  }\n  return d + \"Z\";\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const M = motion($, t);\n  const ph = phases($, t), col = paints($, p.colors);\n  let s = \"\";\n  const fr = Math.floor(t * 24);\n  // Strength rises to full at the cut and falls after it.\n  const g = Math.min(1, (ph.before ? $.E.in2(ph.cover) : 1 - $.E.out2(ph.uncover)) * p.intensity * 1.15);\n  const near = Math.abs(t - ph.mid) < Math.max(0.05, $.dur * 0.07);\n  const pal = [col.lead, col.lead2, col.cover, c.fg, \"#00F0FF\", \"#FF2E88\"];\n  if (near) s += $.rect(0, 0, W, H, { fill: col.cover });\n  const n = 22;\n  for (let i = 0; i < n; i++) {\n    const h1 = $.hash(i * 7.3 + fr * 1.91), h2 = $.hash(i * 3.1 + fr * 0.77), h3 = $.hash(i * 11.7 + fr * 2.3);\n    if (h1 > g) continue;\n    const y = h2 * H, hh = (0.015 + 0.12 * h3 * h3) * H;\n    const x = (h3 - 0.5) * W * 0.5 * g, w = W * (0.35 + 0.65 * $.hash(i + fr * 5.1));\n    s += $.rect(x + (h1 > 0.5 ? W - w : 0) * (1 - g), y, w, hh, { fill: pal[Math.floor(h2 * 997) % pal.length] });\n  }\n  // Pixel blocks.\n  for (let j = 0; j < 14; j++) {\n    const b1 = $.hash(j * 5.7 + fr * 3.3);\n    if (b1 > g * 0.8) continue;\n    const sz = (20 + 60 * $.hash(j + fr)) * k;\n    s += $.rect($.hash(j * 2.2 + fr) * W, $.hash(j * 9.9 + fr) * H, sz * 2.2, sz, { fill: pal[(j + fr) % pal.length] });\n  }\n  // Scanlines over it all while it is breaking.\n  if (g > 0.2) {\n    const id = $.uid(\"sl\");\n    $.def('<pattern id=\"' + id + '\" width=\"8\" height=\"' + 6 * k + '\" patternUnits=\"userSpaceOnUse\"><rect width=\"8\" height=\"' + 2 * k + '\" fill=\"rgba(0,0,0,0.22)\"/></pattern>');\n    s += $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\", op: g });\n  }\n  s = M.part(\"cover\", { in: \"none\", out: \"none\", vary: false }, s);\n  if (p.label) {\n    // The word with an RGB split that jitters.\n    const word = cutWord(M, $, t, p.label, col.cover);\n    const j = (($.hash(fr * 1.3) - 0.5) * 18) * k;\n    s += word ? '<g opacity=\"0.75\" transform=\"translate(' + j + ' 0)\" style=\"mix-blend-mode:screen\">' + word.replace(/fill=\"[^\"]*\"/g, 'fill=\"#00F0FF\"') + \"</g>\" + '<g opacity=\"0.75\" transform=\"translate(' + -j + ' 0)\">' + word.replace(/fill=\"[^\"]*\"/g, 'fill=\"#FF2E88\"') + \"</g>\" + word : \"\";\n  }\n  return s;\n}\n\n/** A glitch cuts on static: zaps into the cut, on it, and one more as it clears. */\nfunction sound($) {\n  const mid = cutAt($);\n  return {\n    cues: [\n      { at: Math.max(0, mid - 0.3), kind: \"zap\", level: 0.8 },\n      { at: mid, kind: \"zap\", level: 1.1, pitch: 4 },\n      { at: mid, kind: \"sub\", level: 0.4 },\n      { at: Math.min($.dur - 0.1, mid + 0.14), kind: \"zap\", level: 0.6, pitch: -3 },\n    ],\n    auto: true,\n  };\n}\n","libs":[]},{"id":"ink","name":"Ink bleed","description":"Ink spreads from a point in wobbly blooms of the accents until it floods the frame, then a hole blooms open on the next shot.","category":"transition","tags":["transition","ink","paint","blob","organic","liquid","splash"],"placement":"overlay","duration":{"default":1.6,"min":0.6,"max":4},"aspects":["16:9","9:16","1:1","4:5"],"props":{"x":{"type":"number","label":"From across (%)","default":30,"min":0,"max":100,"step":1},"y":{"type":"number","label":"From down (%)","default":60,"min":0,"max":100,"step":1},"colors":{"type":"choice","label":"Colours","options":["brand","accent","black","white"],"default":"brand","description":"brand: the accents lead and the brand's ground covers"},"label":{"type":"text","label":"Word at the cut","default":"","maxLength":24,"description":"shown while the frame is covered (empty for none)"}},"parts":{"cover":"the shapes that cover the frame","label":"the word at the cut"},"style":"cobalt","pack":"transitions","ref":"transitions/ink","code":"// Transitions cover the frame by the cut (the midpoint, or the \"cut\" beat) and uncover after it: cut your\n// two clips there. Everything is transparent at the first and last frame.\nfunction cutAt($) { return $.clamp($.beat(\"cut\", $.dur / 2), 0.1, $.dur - 0.1); }\n/** Progress before and after the cut: cover 0→1, then uncover 0→1. */\nfunction phases($, t) {\n  const mid = cutAt($);\n  return { mid: mid, cover: $.clamp(t / mid), uncover: $.clamp((t - mid) / ($.dur - mid)), before: t < mid };\n}\n/** The sound of a cut: air as the cover arrives, a soft low drop on the cut, air going as it opens (and the label's pop). */\nfunction sound($) {\n  const mid = cutAt($);\n  return {\n    cues: [\n      { at: Math.max(0, mid - 0.45), kind: \"whoosh\", dur: Math.min(0.5, mid), level: 0.9 },\n      { at: mid, kind: \"sub\", level: 0.55 },\n      { at: mid + 0.04, kind: \"whoosh\", dur: Math.min(0.5, $.dur - mid), level: 0.55, pitch: -3, pan: 0.2 },\n    ],\n    auto: true,\n  };\n}\n/** The paints: lead (the edge that arrives first), lead2, and cover (what fills the frame at the cut). */\nfunction paints($, scheme) {\n  const c = $.c;\n  if (scheme === \"accent\") return { lead: c.accent2, lead2: c.bg, cover: c.accent };\n  if (scheme === \"black\") return { lead: c.accent, lead2: c.accent2, cover: \"#0A0A0C\" };\n  if (scheme === \"white\") return { lead: c.accent, lead2: c.accent2, cover: \"#FFFFFF\" };\n  return { lead: c.accent, lead2: c.accent2, cover: c.bg };\n}\n/** The word shown while the frame is covered (on the cover colour). */\nfunction cutWord(M, $, t, label, bg) {\n  if (!label) return \"\";\n  const mid = cutAt($), k = $.k, W = $.W, H = $.H, after = $.dur - mid;\n  const str = $.case(label);\n  const f = fitFont($, str, \"display\", 150 * k, Math.min(W, H * 1.4) * 0.74, { ls: -0.03 });\n  return M.text(\"label\", Object.assign({ at: Math.max(0, mid - Math.min(0.3, mid * 0.5)), in: \"pop\", outAt: mid + after * 0.25, out: \"fade\", outDur: after * 0.3 }, f, { x: W / 2, y: H / 2 - f.size * 0.55, anchor: \"middle\", fill: $.on(bg) }), str);\n}\n/** The whole frame with a hole cut in it (hole: path data). */\nfunction frameWithHole($, hole, fill) {\n  return $.path(\"M0 0H\" + $.W + \"V\" + $.H + \"H0Z\" + hole, { fill: fill, extra: 'fill-rule=\"evenodd\"' });\n}\n/** A closed shape as path data: circle, diamond, star or square, radius r about (cx, cy). */\nfunction shapePath(shape, cx, cy, r) {\n  if (r <= 0.01) return \"\";\n  if (shape === \"diamond\") return \"M\" + cx + \" \" + (cy - r) + \"L\" + (cx + r) + \" \" + cy + \"L\" + cx + \" \" + (cy + r) + \"L\" + (cx - r) + \" \" + cy + \"Z\";\n  if (shape === \"square\") return \"M\" + (cx - r) + \" \" + (cy - r) + \"H\" + (cx + r) + \"V\" + (cy + r) + \"H\" + (cx - r) + \"Z\";\n  if (shape === \"star\") {\n    let d = \"\";\n    for (let i = 0; i < 10; i++) {\n      const a = -Math.PI / 2 + (i * Math.PI) / 5, rr = i % 2 ? r * 0.5 : r;\n      d += (i ? \"L\" : \"M\") + (cx + rr * Math.cos(a)) + \" \" + (cy + rr * Math.sin(a));\n    }\n    return d + \"Z\";\n  }\n  return \"M\" + (cx + r) + \" \" + cy + \"A\" + r + \" \" + r + \" 0 1 0 \" + (cx - r) + \" \" + cy + \"A\" + r + \" \" + r + \" 0 1 0 \" + (cx + r) + \" \" + cy + \"Z\";\n}\n/** A wobbly closed blob through n points (radius r, lumps seeded), as smooth path data. */\nfunction blobPath(cx, cy, r, seed, t) {\n  const n = 48, pts = [];\n  for (let i = 0; i < n; i++) {\n    const a = (i / n) * Math.PI * 2;\n    const q = 1 + 0.1 * Math.sin(3 * a + seed) + 0.06 * Math.sin(7 * a + seed * 2.3 + t * 2) + 0.035 * Math.sin(13 * a + seed * 4.1 - t * 3);\n    pts.push([cx + Math.cos(a) * r * q, cy + Math.sin(a) * r * q]);\n  }\n  let d = \"M\" + ((pts[0][0] + pts[n - 1][0]) / 2) + \" \" + ((pts[0][1] + pts[n - 1][1]) / 2);\n  for (let i = 0; i < n; i++) {\n    const p = pts[i], q2 = pts[(i + 1) % n];\n    d += \"Q\" + p[0] + \" \" + p[1] + \" \" + (p[0] + q2[0]) / 2 + \" \" + (p[1] + q2[1]) / 2;\n  }\n  return d + \"Z\";\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const M = motion($, t);\n  const ph = phases($, t), col = paints($, p.colors);\n  let s = \"\";\n  const cx = W * p.x / 100, cy = H * p.y / 100;\n  const far = Math.hypot(Math.max(cx, W - cx), Math.max(cy, H - cy)) * 1.25;\n  if (ph.before) {\n    const lags = [0, 0.12, 0.24], fills = [col.lead, col.lead2, col.cover];\n    lags.forEach(function (lag, i) {\n      const e = $.E.inOut3($.clamp((ph.cover - lag) / (1 - lag)));\n      if (e > 0) s += $.path(blobPath(cx, cy, far * e, 1.7 + i * 2.1, t), { fill: fills[i] });\n    });\n  } else {\n    // The hole opens from the frame's centre, the cover first, the accents trailing as rims.\n    const lags = [0.2, 0.1, 0], fills = [col.lead, col.lead2, col.cover];\n    const hx = W / 2, hy = H / 2, far2 = Math.hypot(W, H) * 0.7;\n    lags.forEach(function (lag, i) {\n      const e = $.E.inOut3($.clamp((ph.uncover - lag) / (1 - lag)));\n      if (e < 1) s += frameWithHole($, e > 0 ? blobPath(hx, hy, far2 * e, 4.2 + i, t) : \"\", fills[i]);\n    });\n  }\n  s = M.part(\"cover\", { in: \"none\", out: \"none\", vary: false }, s);\n  return s + cutWord(M, $, t, p.label, col.cover);\n}\n","libs":[]},{"id":"clock","name":"Clock wipe","description":"A clock-hand sweep: the next colour wipes round the frame like a second hand with an accent edge, then sweeps on round to reveal the next shot.","category":"transition","tags":["transition","clock","radial","sweep","pie","wipe"],"placement":"overlay","duration":{"default":1.2,"min":0.5,"max":4},"aspects":["16:9","9:16","1:1","4:5"],"props":{"direction":{"type":"choice","label":"Turns","options":["clockwise","anticlockwise"],"default":"clockwise"},"colors":{"type":"choice","label":"Colours","options":["brand","accent","black","white"],"default":"brand","description":"brand: the accents lead and the brand's ground covers"},"label":{"type":"text","label":"Word at the cut","default":"","maxLength":24,"description":"shown while the frame is covered (empty for none)"}},"parts":{"cover":"the shapes that cover the frame","label":"the word at the cut"},"style":"cobalt","pack":"transitions","ref":"transitions/clock","code":"// Transitions cover the frame by the cut (the midpoint, or the \"cut\" beat) and uncover after it: cut your\n// two clips there. Everything is transparent at the first and last frame.\nfunction cutAt($) { return $.clamp($.beat(\"cut\", $.dur / 2), 0.1, $.dur - 0.1); }\n/** Progress before and after the cut: cover 0→1, then uncover 0→1. */\nfunction phases($, t) {\n  const mid = cutAt($);\n  return { mid: mid, cover: $.clamp(t / mid), uncover: $.clamp((t - mid) / ($.dur - mid)), before: t < mid };\n}\n/** The sound of a cut: air as the cover arrives, a soft low drop on the cut, air going as it opens (and the label's pop). */\nfunction sound($) {\n  const mid = cutAt($);\n  return {\n    cues: [\n      { at: Math.max(0, mid - 0.45), kind: \"whoosh\", dur: Math.min(0.5, mid), level: 0.9 },\n      { at: mid, kind: \"sub\", level: 0.55 },\n      { at: mid + 0.04, kind: \"whoosh\", dur: Math.min(0.5, $.dur - mid), level: 0.55, pitch: -3, pan: 0.2 },\n    ],\n    auto: true,\n  };\n}\n/** The paints: lead (the edge that arrives first), lead2, and cover (what fills the frame at the cut). */\nfunction paints($, scheme) {\n  const c = $.c;\n  if (scheme === \"accent\") return { lead: c.accent2, lead2: c.bg, cover: c.accent };\n  if (scheme === \"black\") return { lead: c.accent, lead2: c.accent2, cover: \"#0A0A0C\" };\n  if (scheme === \"white\") return { lead: c.accent, lead2: c.accent2, cover: \"#FFFFFF\" };\n  return { lead: c.accent, lead2: c.accent2, cover: c.bg };\n}\n/** The word shown while the frame is covered (on the cover colour). */\nfunction cutWord(M, $, t, label, bg) {\n  if (!label) return \"\";\n  const mid = cutAt($), k = $.k, W = $.W, H = $.H, after = $.dur - mid;\n  const str = $.case(label);\n  const f = fitFont($, str, \"display\", 150 * k, Math.min(W, H * 1.4) * 0.74, { ls: -0.03 });\n  return M.text(\"label\", Object.assign({ at: Math.max(0, mid - Math.min(0.3, mid * 0.5)), in: \"pop\", outAt: mid + after * 0.25, out: \"fade\", outDur: after * 0.3 }, f, { x: W / 2, y: H / 2 - f.size * 0.55, anchor: \"middle\", fill: $.on(bg) }), str);\n}\n/** The whole frame with a hole cut in it (hole: path data). */\nfunction frameWithHole($, hole, fill) {\n  return $.path(\"M0 0H\" + $.W + \"V\" + $.H + \"H0Z\" + hole, { fill: fill, extra: 'fill-rule=\"evenodd\"' });\n}\n/** A closed shape as path data: circle, diamond, star or square, radius r about (cx, cy). */\nfunction shapePath(shape, cx, cy, r) {\n  if (r <= 0.01) return \"\";\n  if (shape === \"diamond\") return \"M\" + cx + \" \" + (cy - r) + \"L\" + (cx + r) + \" \" + cy + \"L\" + cx + \" \" + (cy + r) + \"L\" + (cx - r) + \" \" + cy + \"Z\";\n  if (shape === \"square\") return \"M\" + (cx - r) + \" \" + (cy - r) + \"H\" + (cx + r) + \"V\" + (cy + r) + \"H\" + (cx - r) + \"Z\";\n  if (shape === \"star\") {\n    let d = \"\";\n    for (let i = 0; i < 10; i++) {\n      const a = -Math.PI / 2 + (i * Math.PI) / 5, rr = i % 2 ? r * 0.5 : r;\n      d += (i ? \"L\" : \"M\") + (cx + rr * Math.cos(a)) + \" \" + (cy + rr * Math.sin(a));\n    }\n    return d + \"Z\";\n  }\n  return \"M\" + (cx + r) + \" \" + cy + \"A\" + r + \" \" + r + \" 0 1 0 \" + (cx - r) + \" \" + cy + \"A\" + r + \" \" + r + \" 0 1 0 \" + (cx + r) + \" \" + cy + \"Z\";\n}\n/** A wobbly closed blob through n points (radius r, lumps seeded), as smooth path data. */\nfunction blobPath(cx, cy, r, seed, t) {\n  const n = 48, pts = [];\n  for (let i = 0; i < n; i++) {\n    const a = (i / n) * Math.PI * 2;\n    const q = 1 + 0.1 * Math.sin(3 * a + seed) + 0.06 * Math.sin(7 * a + seed * 2.3 + t * 2) + 0.035 * Math.sin(13 * a + seed * 4.1 - t * 3);\n    pts.push([cx + Math.cos(a) * r * q, cy + Math.sin(a) * r * q]);\n  }\n  let d = \"M\" + ((pts[0][0] + pts[n - 1][0]) / 2) + \" \" + ((pts[0][1] + pts[n - 1][1]) / 2);\n  for (let i = 0; i < n; i++) {\n    const p = pts[i], q2 = pts[(i + 1) % n];\n    d += \"Q\" + p[0] + \" \" + p[1] + \" \" + (p[0] + q2[0]) / 2 + \" \" + (p[1] + q2[1]) / 2;\n  }\n  return d + \"Z\";\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const M = motion($, t);\n  const ph = phases($, t), col = paints($, p.colors);\n  let s = \"\";\n  const cx = W / 2, cy = H / 2, R = Math.hypot(W, H);\n  const dirn = p.direction === \"anticlockwise\" ? -1 : 1;\n  const pt = function (deg) { const a = (deg - 90) * Math.PI / 180; return [cx + R * Math.cos(a) * 1, cy + R * Math.sin(a)]; };\n  // A wedge from angle a to angle b (degrees from 12 o'clock, in the turning direction).\n  const wedge = function (a, b, fill) {\n    if (b - a <= 0.2) return \"\";\n    if (b - a >= 359.8) return $.rect(0, 0, W, H, { fill: fill });\n    const steps = Math.ceil((b - a) / 30), d = [\"M\" + cx + \" \" + cy];\n    for (let i = 0; i <= steps; i++) { const q = pt(dirn * (a + ((b - a) * i) / steps)); d.push(\"L\" + q[0] + \" \" + q[1]); }\n    return $.path(d.join(\"\") + \"Z\", { fill: fill });\n  };\n  if (ph.before) {\n    const e = $.E.inOut2(ph.cover) * 360;\n    s += wedge(0, e, col.cover) + wedge(Math.max(0, e - 14), e, col.lead);\n  } else {\n    const e = $.E.inOut2(ph.uncover) * 360;\n    s += wedge(e, 360, col.cover) + wedge(e, Math.min(360, e + 14), col.lead2);\n  }\n  s = M.part(\"cover\", { in: \"none\", out: \"none\", vary: false }, s);\n  return s + cutWord(M, $, t, p.label, col.cover);\n}\n","libs":[]},{"id":"tiles","name":"Bauhaus tiles","description":"A grid of Bauhaus shapes — discs, quarter circles, squares — pops in tile by tile in a diagonal wave to cover the frame, then shrinks away.","category":"transition","tags":["transition","tiles","bauhaus","shapes","grid","geometric","mosaic"],"placement":"overlay","duration":{"default":1.4,"min":0.6,"max":4},"aspects":["16:9","9:16","1:1","4:5"],"props":{"size":{"type":"number","label":"Tile size","default":4,"min":2,"max":9,"step":1,"description":"tiles across the short side"},"order":{"type":"choice","label":"Order","options":["diagonal","random","center"],"default":"diagonal"},"colors":{"type":"choice","label":"Colours","options":["brand","accent","black","white"],"default":"brand","description":"brand: the accents lead and the brand's ground covers"},"label":{"type":"text","label":"Word at the cut","default":"","maxLength":24,"description":"shown while the frame is covered (empty for none)"}},"parts":{"cover":"the shapes that cover the frame","label":"the word at the cut"},"style":"cobalt","pack":"transitions","ref":"transitions/tiles","code":"// Transitions cover the frame by the cut (the midpoint, or the \"cut\" beat) and uncover after it: cut your\n// two clips there. Everything is transparent at the first and last frame.\nfunction cutAt($) { return $.clamp($.beat(\"cut\", $.dur / 2), 0.1, $.dur - 0.1); }\n/** Progress before and after the cut: cover 0→1, then uncover 0→1. */\nfunction phases($, t) {\n  const mid = cutAt($);\n  return { mid: mid, cover: $.clamp(t / mid), uncover: $.clamp((t - mid) / ($.dur - mid)), before: t < mid };\n}\n/** The sound of a cut: air as the cover arrives, a soft low drop on the cut, air going as it opens (and the label's pop). */\nfunction sound($) {\n  const mid = cutAt($);\n  return {\n    cues: [\n      { at: Math.max(0, mid - 0.45), kind: \"whoosh\", dur: Math.min(0.5, mid), level: 0.9 },\n      { at: mid, kind: \"sub\", level: 0.55 },\n      { at: mid + 0.04, kind: \"whoosh\", dur: Math.min(0.5, $.dur - mid), level: 0.55, pitch: -3, pan: 0.2 },\n    ],\n    auto: true,\n  };\n}\n/** The paints: lead (the edge that arrives first), lead2, and cover (what fills the frame at the cut). */\nfunction paints($, scheme) {\n  const c = $.c;\n  if (scheme === \"accent\") return { lead: c.accent2, lead2: c.bg, cover: c.accent };\n  if (scheme === \"black\") return { lead: c.accent, lead2: c.accent2, cover: \"#0A0A0C\" };\n  if (scheme === \"white\") return { lead: c.accent, lead2: c.accent2, cover: \"#FFFFFF\" };\n  return { lead: c.accent, lead2: c.accent2, cover: c.bg };\n}\n/** The word shown while the frame is covered (on the cover colour). */\nfunction cutWord(M, $, t, label, bg) {\n  if (!label) return \"\";\n  const mid = cutAt($), k = $.k, W = $.W, H = $.H, after = $.dur - mid;\n  const str = $.case(label);\n  const f = fitFont($, str, \"display\", 150 * k, Math.min(W, H * 1.4) * 0.74, { ls: -0.03 });\n  return M.text(\"label\", Object.assign({ at: Math.max(0, mid - Math.min(0.3, mid * 0.5)), in: \"pop\", outAt: mid + after * 0.25, out: \"fade\", outDur: after * 0.3 }, f, { x: W / 2, y: H / 2 - f.size * 0.55, anchor: \"middle\", fill: $.on(bg) }), str);\n}\n/** The whole frame with a hole cut in it (hole: path data). */\nfunction frameWithHole($, hole, fill) {\n  return $.path(\"M0 0H\" + $.W + \"V\" + $.H + \"H0Z\" + hole, { fill: fill, extra: 'fill-rule=\"evenodd\"' });\n}\n/** A closed shape as path data: circle, diamond, star or square, radius r about (cx, cy). */\nfunction shapePath(shape, cx, cy, r) {\n  if (r <= 0.01) return \"\";\n  if (shape === \"diamond\") return \"M\" + cx + \" \" + (cy - r) + \"L\" + (cx + r) + \" \" + cy + \"L\" + cx + \" \" + (cy + r) + \"L\" + (cx - r) + \" \" + cy + \"Z\";\n  if (shape === \"square\") return \"M\" + (cx - r) + \" \" + (cy - r) + \"H\" + (cx + r) + \"V\" + (cy + r) + \"H\" + (cx - r) + \"Z\";\n  if (shape === \"star\") {\n    let d = \"\";\n    for (let i = 0; i < 10; i++) {\n      const a = -Math.PI / 2 + (i * Math.PI) / 5, rr = i % 2 ? r * 0.5 : r;\n      d += (i ? \"L\" : \"M\") + (cx + rr * Math.cos(a)) + \" \" + (cy + rr * Math.sin(a));\n    }\n    return d + \"Z\";\n  }\n  return \"M\" + (cx + r) + \" \" + cy + \"A\" + r + \" \" + r + \" 0 1 0 \" + (cx - r) + \" \" + cy + \"A\" + r + \" \" + r + \" 0 1 0 \" + (cx + r) + \" \" + cy + \"Z\";\n}\n/** A wobbly closed blob through n points (radius r, lumps seeded), as smooth path data. */\nfunction blobPath(cx, cy, r, seed, t) {\n  const n = 48, pts = [];\n  for (let i = 0; i < n; i++) {\n    const a = (i / n) * Math.PI * 2;\n    const q = 1 + 0.1 * Math.sin(3 * a + seed) + 0.06 * Math.sin(7 * a + seed * 2.3 + t * 2) + 0.035 * Math.sin(13 * a + seed * 4.1 - t * 3);\n    pts.push([cx + Math.cos(a) * r * q, cy + Math.sin(a) * r * q]);\n  }\n  let d = \"M\" + ((pts[0][0] + pts[n - 1][0]) / 2) + \" \" + ((pts[0][1] + pts[n - 1][1]) / 2);\n  for (let i = 0; i < n; i++) {\n    const p = pts[i], q2 = pts[(i + 1) % n];\n    d += \"Q\" + p[0] + \" \" + p[1] + \" \" + (p[0] + q2[0]) / 2 + \" \" + (p[1] + q2[1]) / 2;\n  }\n  return d + \"Z\";\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const M = motion($, t);\n  const ph = phases($, t), col = paints($, p.colors);\n  let s = \"\";\n  const n = Math.max(2, Math.round(p.size)), cell = Math.min(W, H) / n;\n  const cols = Math.ceil(W / cell), rows = Math.ceil(H / cell);\n  const ox = (W - cols * cell) / 2, oy = (H - rows * cell) / 2;\n  const pal = [col.cover, col.lead, col.lead2, col.cover];\n  for (let r = 0; r < rows; r++) for (let q = 0; q < cols; q++) {\n    const h = $.hash(r * 17.3 + q * 5.1 + 0.4);\n    const rank = p.order === \"random\" ? h : p.order === \"center\" ? Math.hypot(q - (cols - 1) / 2, r - (rows - 1) / 2) / Math.hypot(cols / 2, rows / 2) : (q + r) / (cols + rows - 2);\n    const lag = rank * 0.5;\n    const e = ph.before ? $.E.outBack($.clamp((ph.cover - lag) / 0.5)) : 1 - $.E.in3($.clamp((ph.uncover - lag) / 0.5));\n    if (e <= 0) continue;\n    const x = ox + q * cell, y = oy + r * cell, cx = x + cell / 2, cy = y + cell / 2;\n    const fill = pal[Math.floor(h * 4) % 4], kind = Math.floor($.hash(r * 3.3 + q * 9.7) * 3);\n    let shape;\n    if (kind === 0) shape = $.circle(cx, cy, cell * 0.72 * e, { fill: fill });\n    else if (kind === 1) { const rr = cell * 1.42 * e, rot = Math.floor(h * 4) * 90; shape = $.g({ transform: \"rotate(\" + rot + \" \" + cx + \" \" + cy + \")\" }, $.path(\"M\" + (cx - cell / 2) + \" \" + (cy - cell / 2) + \"h\" + rr + \"A\" + rr + \" \" + rr + \" 0 0 1 \" + (cx - cell / 2) + \" \" + (cy - cell / 2 + rr) + \"Z\", { fill: fill })); }\n    else shape = $.rect(cx - cell * 0.51 * e, cy - cell * 0.51 * e, cell * 1.02 * e, cell * 1.02 * e, { fill: fill });\n    s += $.clip(x - 0.5, y - 0.5, cell + 1, cell + 1, 0, shape);\n  }\n  s = M.part(\"cover\", { in: \"none\", out: \"none\", vary: false }, s);\n  if (p.label) {\n    const str = $.case(p.label), f = fitFont($, str, \"display\", 120 * k, Math.min(W, H * 1.4) * 0.66, { ls: -0.03 });\n    const w = $.measure(str, f) + 80 * k, hh = f.size * 1.5;\n    const a = Math.max(0, ph.mid - Math.min(0.3, ph.mid * 0.5));\n    s += M.part(\"label\", { at: a, in: \"pop\", outAt: ph.mid + ($.dur - ph.mid) * 0.2, out: \"pop\", outDur: 0.2, box: { x: W / 2 - w / 2, y: H / 2 - hh / 2, w: w, h: hh } },\n      $.rect(W / 2 - w / 2, H / 2 - hh / 2, w, hh, { r: hh / 2, fill: col.cover }) + $.text(str, Object.assign({}, f, { x: W / 2, y: H / 2 - f.size * 0.55, anchor: \"middle\", fill: $.on(col.cover) })));\n  }\n  return s;\n}\n","libs":[]},{"id":"pixels","name":"Pixel dissolve","description":"The frame dissolves into square pixels in random order — a few in the accents — until it is covered, then dissolves away to the next shot.","category":"transition","tags":["transition","pixels","dissolve","mosaic","retro","digital"],"placement":"overlay","duration":{"default":1.2,"min":0.5,"max":4},"aspects":["16:9","9:16","1:1","4:5"],"props":{"size":{"type":"number","label":"Pixel size","default":48,"min":16,"max":160,"step":4},"colors":{"type":"choice","label":"Colours","options":["brand","accent","black","white"],"default":"brand","description":"brand: the accents lead and the brand's ground covers"},"label":{"type":"text","label":"Word at the cut","default":"","maxLength":24,"description":"shown while the frame is covered (empty for none)"}},"parts":{"cover":"the shapes that cover the frame","label":"the word at the cut"},"style":"cobalt","pack":"transitions","ref":"transitions/pixels","code":"// Transitions cover the frame by the cut (the midpoint, or the \"cut\" beat) and uncover after it: cut your\n// two clips there. Everything is transparent at the first and last frame.\nfunction cutAt($) { return $.clamp($.beat(\"cut\", $.dur / 2), 0.1, $.dur - 0.1); }\n/** Progress before and after the cut: cover 0→1, then uncover 0→1. */\nfunction phases($, t) {\n  const mid = cutAt($);\n  return { mid: mid, cover: $.clamp(t / mid), uncover: $.clamp((t - mid) / ($.dur - mid)), before: t < mid };\n}\n/** The sound of a cut: air as the cover arrives, a soft low drop on the cut, air going as it opens (and the label's pop). */\nfunction sound($) {\n  const mid = cutAt($);\n  return {\n    cues: [\n      { at: Math.max(0, mid - 0.45), kind: \"whoosh\", dur: Math.min(0.5, mid), level: 0.9 },\n      { at: mid, kind: \"sub\", level: 0.55 },\n      { at: mid + 0.04, kind: \"whoosh\", dur: Math.min(0.5, $.dur - mid), level: 0.55, pitch: -3, pan: 0.2 },\n    ],\n    auto: true,\n  };\n}\n/** The paints: lead (the edge that arrives first), lead2, and cover (what fills the frame at the cut). */\nfunction paints($, scheme) {\n  const c = $.c;\n  if (scheme === \"accent\") return { lead: c.accent2, lead2: c.bg, cover: c.accent };\n  if (scheme === \"black\") return { lead: c.accent, lead2: c.accent2, cover: \"#0A0A0C\" };\n  if (scheme === \"white\") return { lead: c.accent, lead2: c.accent2, cover: \"#FFFFFF\" };\n  return { lead: c.accent, lead2: c.accent2, cover: c.bg };\n}\n/** The word shown while the frame is covered (on the cover colour). */\nfunction cutWord(M, $, t, label, bg) {\n  if (!label) return \"\";\n  const mid = cutAt($), k = $.k, W = $.W, H = $.H, after = $.dur - mid;\n  const str = $.case(label);\n  const f = fitFont($, str, \"display\", 150 * k, Math.min(W, H * 1.4) * 0.74, { ls: -0.03 });\n  return M.text(\"label\", Object.assign({ at: Math.max(0, mid - Math.min(0.3, mid * 0.5)), in: \"pop\", outAt: mid + after * 0.25, out: \"fade\", outDur: after * 0.3 }, f, { x: W / 2, y: H / 2 - f.size * 0.55, anchor: \"middle\", fill: $.on(bg) }), str);\n}\n/** The whole frame with a hole cut in it (hole: path data). */\nfunction frameWithHole($, hole, fill) {\n  return $.path(\"M0 0H\" + $.W + \"V\" + $.H + \"H0Z\" + hole, { fill: fill, extra: 'fill-rule=\"evenodd\"' });\n}\n/** A closed shape as path data: circle, diamond, star or square, radius r about (cx, cy). */\nfunction shapePath(shape, cx, cy, r) {\n  if (r <= 0.01) return \"\";\n  if (shape === \"diamond\") return \"M\" + cx + \" \" + (cy - r) + \"L\" + (cx + r) + \" \" + cy + \"L\" + cx + \" \" + (cy + r) + \"L\" + (cx - r) + \" \" + cy + \"Z\";\n  if (shape === \"square\") return \"M\" + (cx - r) + \" \" + (cy - r) + \"H\" + (cx + r) + \"V\" + (cy + r) + \"H\" + (cx - r) + \"Z\";\n  if (shape === \"star\") {\n    let d = \"\";\n    for (let i = 0; i < 10; i++) {\n      const a = -Math.PI / 2 + (i * Math.PI) / 5, rr = i % 2 ? r * 0.5 : r;\n      d += (i ? \"L\" : \"M\") + (cx + rr * Math.cos(a)) + \" \" + (cy + rr * Math.sin(a));\n    }\n    return d + \"Z\";\n  }\n  return \"M\" + (cx + r) + \" \" + cy + \"A\" + r + \" \" + r + \" 0 1 0 \" + (cx - r) + \" \" + cy + \"A\" + r + \" \" + r + \" 0 1 0 \" + (cx + r) + \" \" + cy + \"Z\";\n}\n/** A wobbly closed blob through n points (radius r, lumps seeded), as smooth path data. */\nfunction blobPath(cx, cy, r, seed, t) {\n  const n = 48, pts = [];\n  for (let i = 0; i < n; i++) {\n    const a = (i / n) * Math.PI * 2;\n    const q = 1 + 0.1 * Math.sin(3 * a + seed) + 0.06 * Math.sin(7 * a + seed * 2.3 + t * 2) + 0.035 * Math.sin(13 * a + seed * 4.1 - t * 3);\n    pts.push([cx + Math.cos(a) * r * q, cy + Math.sin(a) * r * q]);\n  }\n  let d = \"M\" + ((pts[0][0] + pts[n - 1][0]) / 2) + \" \" + ((pts[0][1] + pts[n - 1][1]) / 2);\n  for (let i = 0; i < n; i++) {\n    const p = pts[i], q2 = pts[(i + 1) % n];\n    d += \"Q\" + p[0] + \" \" + p[1] + \" \" + (p[0] + q2[0]) / 2 + \" \" + (p[1] + q2[1]) / 2;\n  }\n  return d + \"Z\";\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const M = motion($, t);\n  const ph = phases($, t), col = paints($, p.colors);\n  let s = \"\";\n  const px = Math.max(12, p.size * k), cols = Math.ceil(W / px), rows = Math.ceil(H / px);\n  const cover = ph.before ? $.E.inOut2(ph.cover) : 1 - $.E.inOut2(ph.uncover);\n  let d = \"\", dl = \"\", dl2 = \"\";\n  for (let r = 0; r < rows; r++) for (let q = 0; q < cols; q++) {\n    const h = $.hash(r * 31.7 + q * 7.9 + (ph.before ? 0 : 3.3));\n    if (h >= cover) continue;\n    const cell = \"M\" + q * px + \" \" + r * px + \"h\" + (px + 0.5) + \"v\" + (px + 0.5) + \"h\" + -(px + 0.5) + \"z\";\n    // The last pixels to land are the accents.\n    if (cover < 0.98 && h > cover - 0.08) { if ((q + r) % 2) dl += cell; else dl2 += cell; } else d += cell;\n  }\n  s += (d ? $.path(d, { fill: col.cover }) : \"\") + (dl ? $.path(dl, { fill: col.lead }) : \"\") + (dl2 ? $.path(dl2, { fill: col.lead2 }) : \"\");\n  s = M.part(\"cover\", { in: \"none\", out: \"none\", vary: false }, s);\n  return s + cutWord(M, $, t, p.label, col.cover);\n}\n","libs":[]},{"id":"stripes","name":"Speed stripes","description":"Diagonal stripes in the brand's colours race across in a stagger to cover the frame, then race off to reveal the next shot.","category":"transition","tags":["transition","stripes","diagonal","speed","sport","swipe"],"placement":"overlay","duration":{"default":1.1,"min":0.5,"max":4},"aspects":["16:9","9:16","1:1","4:5"],"props":{"stripes":{"type":"number","label":"Stripes","default":7,"min":3,"max":16,"step":1},"direction":{"type":"choice","label":"Direction","options":["right","left"],"default":"right"},"colors":{"type":"choice","label":"Colours","options":["brand","accent","black","white"],"default":"brand","description":"brand: the accents lead and the brand's ground covers"},"label":{"type":"text","label":"Word at the cut","default":"","maxLength":24,"description":"shown while the frame is covered (empty for none)"}},"parts":{"cover":"the shapes that cover the frame","label":"the word at the cut"},"style":"cobalt","pack":"transitions","ref":"transitions/stripes","code":"// Transitions cover the frame by the cut (the midpoint, or the \"cut\" beat) and uncover after it: cut your\n// two clips there. Everything is transparent at the first and last frame.\nfunction cutAt($) { return $.clamp($.beat(\"cut\", $.dur / 2), 0.1, $.dur - 0.1); }\n/** Progress before and after the cut: cover 0→1, then uncover 0→1. */\nfunction phases($, t) {\n  const mid = cutAt($);\n  return { mid: mid, cover: $.clamp(t / mid), uncover: $.clamp((t - mid) / ($.dur - mid)), before: t < mid };\n}\n/** The sound of a cut: air as the cover arrives, a soft low drop on the cut, air going as it opens (and the label's pop). */\nfunction sound($) {\n  const mid = cutAt($);\n  return {\n    cues: [\n      { at: Math.max(0, mid - 0.45), kind: \"whoosh\", dur: Math.min(0.5, mid), level: 0.9 },\n      { at: mid, kind: \"sub\", level: 0.55 },\n      { at: mid + 0.04, kind: \"whoosh\", dur: Math.min(0.5, $.dur - mid), level: 0.55, pitch: -3, pan: 0.2 },\n    ],\n    auto: true,\n  };\n}\n/** The paints: lead (the edge that arrives first), lead2, and cover (what fills the frame at the cut). */\nfunction paints($, scheme) {\n  const c = $.c;\n  if (scheme === \"accent\") return { lead: c.accent2, lead2: c.bg, cover: c.accent };\n  if (scheme === \"black\") return { lead: c.accent, lead2: c.accent2, cover: \"#0A0A0C\" };\n  if (scheme === \"white\") return { lead: c.accent, lead2: c.accent2, cover: \"#FFFFFF\" };\n  return { lead: c.accent, lead2: c.accent2, cover: c.bg };\n}\n/** The word shown while the frame is covered (on the cover colour). */\nfunction cutWord(M, $, t, label, bg) {\n  if (!label) return \"\";\n  const mid = cutAt($), k = $.k, W = $.W, H = $.H, after = $.dur - mid;\n  const str = $.case(label);\n  const f = fitFont($, str, \"display\", 150 * k, Math.min(W, H * 1.4) * 0.74, { ls: -0.03 });\n  return M.text(\"label\", Object.assign({ at: Math.max(0, mid - Math.min(0.3, mid * 0.5)), in: \"pop\", outAt: mid + after * 0.25, out: \"fade\", outDur: after * 0.3 }, f, { x: W / 2, y: H / 2 - f.size * 0.55, anchor: \"middle\", fill: $.on(bg) }), str);\n}\n/** The whole frame with a hole cut in it (hole: path data). */\nfunction frameWithHole($, hole, fill) {\n  return $.path(\"M0 0H\" + $.W + \"V\" + $.H + \"H0Z\" + hole, { fill: fill, extra: 'fill-rule=\"evenodd\"' });\n}\n/** A closed shape as path data: circle, diamond, star or square, radius r about (cx, cy). */\nfunction shapePath(shape, cx, cy, r) {\n  if (r <= 0.01) return \"\";\n  if (shape === \"diamond\") return \"M\" + cx + \" \" + (cy - r) + \"L\" + (cx + r) + \" \" + cy + \"L\" + cx + \" \" + (cy + r) + \"L\" + (cx - r) + \" \" + cy + \"Z\";\n  if (shape === \"square\") return \"M\" + (cx - r) + \" \" + (cy - r) + \"H\" + (cx + r) + \"V\" + (cy + r) + \"H\" + (cx - r) + \"Z\";\n  if (shape === \"star\") {\n    let d = \"\";\n    for (let i = 0; i < 10; i++) {\n      const a = -Math.PI / 2 + (i * Math.PI) / 5, rr = i % 2 ? r * 0.5 : r;\n      d += (i ? \"L\" : \"M\") + (cx + rr * Math.cos(a)) + \" \" + (cy + rr * Math.sin(a));\n    }\n    return d + \"Z\";\n  }\n  return \"M\" + (cx + r) + \" \" + cy + \"A\" + r + \" \" + r + \" 0 1 0 \" + (cx - r) + \" \" + cy + \"A\" + r + \" \" + r + \" 0 1 0 \" + (cx + r) + \" \" + cy + \"Z\";\n}\n/** A wobbly closed blob through n points (radius r, lumps seeded), as smooth path data. */\nfunction blobPath(cx, cy, r, seed, t) {\n  const n = 48, pts = [];\n  for (let i = 0; i < n; i++) {\n    const a = (i / n) * Math.PI * 2;\n    const q = 1 + 0.1 * Math.sin(3 * a + seed) + 0.06 * Math.sin(7 * a + seed * 2.3 + t * 2) + 0.035 * Math.sin(13 * a + seed * 4.1 - t * 3);\n    pts.push([cx + Math.cos(a) * r * q, cy + Math.sin(a) * r * q]);\n  }\n  let d = \"M\" + ((pts[0][0] + pts[n - 1][0]) / 2) + \" \" + ((pts[0][1] + pts[n - 1][1]) / 2);\n  for (let i = 0; i < n; i++) {\n    const p = pts[i], q2 = pts[(i + 1) % n];\n    d += \"Q\" + p[0] + \" \" + p[1] + \" \" + (p[0] + q2[0]) / 2 + \" \" + (p[1] + q2[1]) / 2;\n  }\n  return d + \"Z\";\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const M = motion($, t);\n  const ph = phases($, t), col = paints($, p.colors);\n  let s = \"\";\n  const n = Math.max(3, Math.round(p.stripes)), D = Math.hypot(W, H), band = D / n + 2;\n  const ang = -24, sign = p.direction === \"left\" ? -1 : 1;\n  const pal = [col.lead, col.lead2, col.cover];\n  let g = \"\";\n  for (let i = 0; i < n; i++) {\n    const lag = (i / (n - 1)) * 0.35;\n    const ci = $.E.inOut3($.clamp((ph.cover - lag) / (1 - lag))), ui = $.E.inOut3($.clamp((ph.uncover - lag) / (1 - lag)));\n    // Each stripe slides in from one side, then out the other.\n    const x0 = ph.before ? -D + D * ci : D * ui;\n    if (ph.before ? ci <= 0 : ui >= 1) continue;\n    g += $.rect(x0 - D / 2, -D / 2 + i * (D / n), D * 1.02, band, { fill: i === n - 1 || i % 3 === 2 ? col.cover : pal[i % 3] });\n  }\n  if (ph.before && ph.cover > 0.98) g += $.rect(-D, -D, D * 2, D * 2, { fill: col.cover });\n  s += '<g transform=\"translate(' + W / 2 + \" \" + H / 2 + \") scale(\" + sign + \" 1) rotate(\" + ang + ')\">' + g + \"</g>\";\n  s = M.part(\"cover\", { in: \"none\", out: \"none\", vary: false }, s);\n  return s + cutWord(M, $, t, p.label, col.cover);\n}\n","libs":[]},{"id":"slices","name":"Slices","description":"The frame is sliced into bands that shoot in from alternate sides, each with an accent edge, then shoot out the far side to reveal the next shot.","category":"transition","tags":["transition","slices","bands","slide","split"],"placement":"overlay","duration":{"default":1.2,"min":0.5,"max":4},"aspects":["16:9","9:16","1:1","4:5"],"props":{"slices":{"type":"number","label":"Slices","default":5,"min":2,"max":12,"step":1},"colors":{"type":"choice","label":"Colours","options":["brand","accent","black","white"],"default":"brand","description":"brand: the accents lead and the brand's ground covers"},"label":{"type":"text","label":"Word at the cut","default":"","maxLength":24,"description":"shown while the frame is covered (empty for none)"}},"parts":{"cover":"the shapes that cover the frame","label":"the word at the cut"},"style":"cobalt","pack":"transitions","ref":"transitions/slices","code":"// Transitions cover the frame by the cut (the midpoint, or the \"cut\" beat) and uncover after it: cut your\n// two clips there. Everything is transparent at the first and last frame.\nfunction cutAt($) { return $.clamp($.beat(\"cut\", $.dur / 2), 0.1, $.dur - 0.1); }\n/** Progress before and after the cut: cover 0→1, then uncover 0→1. */\nfunction phases($, t) {\n  const mid = cutAt($);\n  return { mid: mid, cover: $.clamp(t / mid), uncover: $.clamp((t - mid) / ($.dur - mid)), before: t < mid };\n}\n/** The sound of a cut: air as the cover arrives, a soft low drop on the cut, air going as it opens (and the label's pop). */\nfunction sound($) {\n  const mid = cutAt($);\n  return {\n    cues: [\n      { at: Math.max(0, mid - 0.45), kind: \"whoosh\", dur: Math.min(0.5, mid), level: 0.9 },\n      { at: mid, kind: \"sub\", level: 0.55 },\n      { at: mid + 0.04, kind: \"whoosh\", dur: Math.min(0.5, $.dur - mid), level: 0.55, pitch: -3, pan: 0.2 },\n    ],\n    auto: true,\n  };\n}\n/** The paints: lead (the edge that arrives first), lead2, and cover (what fills the frame at the cut). */\nfunction paints($, scheme) {\n  const c = $.c;\n  if (scheme === \"accent\") return { lead: c.accent2, lead2: c.bg, cover: c.accent };\n  if (scheme === \"black\") return { lead: c.accent, lead2: c.accent2, cover: \"#0A0A0C\" };\n  if (scheme === \"white\") return { lead: c.accent, lead2: c.accent2, cover: \"#FFFFFF\" };\n  return { lead: c.accent, lead2: c.accent2, cover: c.bg };\n}\n/** The word shown while the frame is covered (on the cover colour). */\nfunction cutWord(M, $, t, label, bg) {\n  if (!label) return \"\";\n  const mid = cutAt($), k = $.k, W = $.W, H = $.H, after = $.dur - mid;\n  const str = $.case(label);\n  const f = fitFont($, str, \"display\", 150 * k, Math.min(W, H * 1.4) * 0.74, { ls: -0.03 });\n  return M.text(\"label\", Object.assign({ at: Math.max(0, mid - Math.min(0.3, mid * 0.5)), in: \"pop\", outAt: mid + after * 0.25, out: \"fade\", outDur: after * 0.3 }, f, { x: W / 2, y: H / 2 - f.size * 0.55, anchor: \"middle\", fill: $.on(bg) }), str);\n}\n/** The whole frame with a hole cut in it (hole: path data). */\nfunction frameWithHole($, hole, fill) {\n  return $.path(\"M0 0H\" + $.W + \"V\" + $.H + \"H0Z\" + hole, { fill: fill, extra: 'fill-rule=\"evenodd\"' });\n}\n/** A closed shape as path data: circle, diamond, star or square, radius r about (cx, cy). */\nfunction shapePath(shape, cx, cy, r) {\n  if (r <= 0.01) return \"\";\n  if (shape === \"diamond\") return \"M\" + cx + \" \" + (cy - r) + \"L\" + (cx + r) + \" \" + cy + \"L\" + cx + \" \" + (cy + r) + \"L\" + (cx - r) + \" \" + cy + \"Z\";\n  if (shape === \"square\") return \"M\" + (cx - r) + \" \" + (cy - r) + \"H\" + (cx + r) + \"V\" + (cy + r) + \"H\" + (cx - r) + \"Z\";\n  if (shape === \"star\") {\n    let d = \"\";\n    for (let i = 0; i < 10; i++) {\n      const a = -Math.PI / 2 + (i * Math.PI) / 5, rr = i % 2 ? r * 0.5 : r;\n      d += (i ? \"L\" : \"M\") + (cx + rr * Math.cos(a)) + \" \" + (cy + rr * Math.sin(a));\n    }\n    return d + \"Z\";\n  }\n  return \"M\" + (cx + r) + \" \" + cy + \"A\" + r + \" \" + r + \" 0 1 0 \" + (cx - r) + \" \" + cy + \"A\" + r + \" \" + r + \" 0 1 0 \" + (cx + r) + \" \" + cy + \"Z\";\n}\n/** A wobbly closed blob through n points (radius r, lumps seeded), as smooth path data. */\nfunction blobPath(cx, cy, r, seed, t) {\n  const n = 48, pts = [];\n  for (let i = 0; i < n; i++) {\n    const a = (i / n) * Math.PI * 2;\n    const q = 1 + 0.1 * Math.sin(3 * a + seed) + 0.06 * Math.sin(7 * a + seed * 2.3 + t * 2) + 0.035 * Math.sin(13 * a + seed * 4.1 - t * 3);\n    pts.push([cx + Math.cos(a) * r * q, cy + Math.sin(a) * r * q]);\n  }\n  let d = \"M\" + ((pts[0][0] + pts[n - 1][0]) / 2) + \" \" + ((pts[0][1] + pts[n - 1][1]) / 2);\n  for (let i = 0; i < n; i++) {\n    const p = pts[i], q2 = pts[(i + 1) % n];\n    d += \"Q\" + p[0] + \" \" + p[1] + \" \" + (p[0] + q2[0]) / 2 + \" \" + (p[1] + q2[1]) / 2;\n  }\n  return d + \"Z\";\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const M = motion($, t);\n  const ph = phases($, t), col = paints($, p.colors);\n  let s = \"\";\n  const n = Math.max(2, Math.round(p.slices)), band = H / n, edge = 12 * k;\n  for (let i = 0; i < n; i++) {\n    const lag = $.hash(i * 4.4 + 1) * 0.3, dir = i % 2 ? -1 : 1;\n    const ci = $.E.out3($.clamp((ph.cover - lag) / (1 - lag))), ui = $.E.in3($.clamp((ph.uncover - lag) / (1 - lag)));\n    const x = ph.before ? -dir * W * (1 - ci) : dir * W * ui;\n    if (ph.before ? ci <= 0 : ui >= 1) continue;\n    s += $.rect(x, i * band - 0.5, W, band + 1, { fill: col.cover });\n    // The leading edge in an accent.\n    const ex = dir > 0 ? (ph.before ? x + W - edge : x) : (ph.before ? x : x + W - edge);\n    s += $.rect(ex, i * band - 0.5, edge, band + 1, { fill: i % 2 ? col.lead2 : col.lead });\n  }\n  s = M.part(\"cover\", { in: \"none\", out: \"none\", vary: false }, s);\n  return s + cutWord(M, $, t, p.label, col.cover);\n}\n","libs":[]},{"id":"whip","name":"Whip pan","description":"A whip pan: speed streaks blur across, thicken into a smear of the brand's colours at the cut, then streak away the same way onto the next shot.","category":"transition","tags":["transition","whip","pan","speed","blur","streak","swish"],"placement":"overlay","duration":{"default":0.8,"min":0.3,"max":3},"aspects":["16:9","9:16","1:1","4:5"],"props":{"direction":{"type":"choice","label":"Direction","options":["left","right","up","down"],"default":"left"},"colors":{"type":"choice","label":"Colours","options":["brand","accent","black","white"],"default":"brand","description":"brand: the accents lead and the brand's ground covers"},"label":{"type":"text","label":"Word at the cut","default":"","maxLength":24,"description":"shown while the frame is covered (empty for none)"}},"parts":{"cover":"the shapes that cover the frame","label":"the word at the cut"},"style":"cobalt","pack":"transitions","ref":"transitions/whip","code":"// Transitions cover the frame by the cut (the midpoint, or the \"cut\" beat) and uncover after it: cut your\n// two clips there. Everything is transparent at the first and last frame.\nfunction cutAt($) { return $.clamp($.beat(\"cut\", $.dur / 2), 0.1, $.dur - 0.1); }\n/** Progress before and after the cut: cover 0→1, then uncover 0→1. */\nfunction phases($, t) {\n  const mid = cutAt($);\n  return { mid: mid, cover: $.clamp(t / mid), uncover: $.clamp((t - mid) / ($.dur - mid)), before: t < mid };\n}\n/** The sound of a cut: air as the cover arrives, a soft low drop on the cut, air going as it opens (and the label's pop). */\nfunction sound($) {\n  const mid = cutAt($);\n  return {\n    cues: [\n      { at: Math.max(0, mid - 0.45), kind: \"whoosh\", dur: Math.min(0.5, mid), level: 0.9 },\n      { at: mid, kind: \"sub\", level: 0.55 },\n      { at: mid + 0.04, kind: \"whoosh\", dur: Math.min(0.5, $.dur - mid), level: 0.55, pitch: -3, pan: 0.2 },\n    ],\n    auto: true,\n  };\n}\n/** The paints: lead (the edge that arrives first), lead2, and cover (what fills the frame at the cut). */\nfunction paints($, scheme) {\n  const c = $.c;\n  if (scheme === \"accent\") return { lead: c.accent2, lead2: c.bg, cover: c.accent };\n  if (scheme === \"black\") return { lead: c.accent, lead2: c.accent2, cover: \"#0A0A0C\" };\n  if (scheme === \"white\") return { lead: c.accent, lead2: c.accent2, cover: \"#FFFFFF\" };\n  return { lead: c.accent, lead2: c.accent2, cover: c.bg };\n}\n/** The word shown while the frame is covered (on the cover colour). */\nfunction cutWord(M, $, t, label, bg) {\n  if (!label) return \"\";\n  const mid = cutAt($), k = $.k, W = $.W, H = $.H, after = $.dur - mid;\n  const str = $.case(label);\n  const f = fitFont($, str, \"display\", 150 * k, Math.min(W, H * 1.4) * 0.74, { ls: -0.03 });\n  return M.text(\"label\", Object.assign({ at: Math.max(0, mid - Math.min(0.3, mid * 0.5)), in: \"pop\", outAt: mid + after * 0.25, out: \"fade\", outDur: after * 0.3 }, f, { x: W / 2, y: H / 2 - f.size * 0.55, anchor: \"middle\", fill: $.on(bg) }), str);\n}\n/** The whole frame with a hole cut in it (hole: path data). */\nfunction frameWithHole($, hole, fill) {\n  return $.path(\"M0 0H\" + $.W + \"V\" + $.H + \"H0Z\" + hole, { fill: fill, extra: 'fill-rule=\"evenodd\"' });\n}\n/** A closed shape as path data: circle, diamond, star or square, radius r about (cx, cy). */\nfunction shapePath(shape, cx, cy, r) {\n  if (r <= 0.01) return \"\";\n  if (shape === \"diamond\") return \"M\" + cx + \" \" + (cy - r) + \"L\" + (cx + r) + \" \" + cy + \"L\" + cx + \" \" + (cy + r) + \"L\" + (cx - r) + \" \" + cy + \"Z\";\n  if (shape === \"square\") return \"M\" + (cx - r) + \" \" + (cy - r) + \"H\" + (cx + r) + \"V\" + (cy + r) + \"H\" + (cx - r) + \"Z\";\n  if (shape === \"star\") {\n    let d = \"\";\n    for (let i = 0; i < 10; i++) {\n      const a = -Math.PI / 2 + (i * Math.PI) / 5, rr = i % 2 ? r * 0.5 : r;\n      d += (i ? \"L\" : \"M\") + (cx + rr * Math.cos(a)) + \" \" + (cy + rr * Math.sin(a));\n    }\n    return d + \"Z\";\n  }\n  return \"M\" + (cx + r) + \" \" + cy + \"A\" + r + \" \" + r + \" 0 1 0 \" + (cx - r) + \" \" + cy + \"A\" + r + \" \" + r + \" 0 1 0 \" + (cx + r) + \" \" + cy + \"Z\";\n}\n/** A wobbly closed blob through n points (radius r, lumps seeded), as smooth path data. */\nfunction blobPath(cx, cy, r, seed, t) {\n  const n = 48, pts = [];\n  for (let i = 0; i < n; i++) {\n    const a = (i / n) * Math.PI * 2;\n    const q = 1 + 0.1 * Math.sin(3 * a + seed) + 0.06 * Math.sin(7 * a + seed * 2.3 + t * 2) + 0.035 * Math.sin(13 * a + seed * 4.1 - t * 3);\n    pts.push([cx + Math.cos(a) * r * q, cy + Math.sin(a) * r * q]);\n  }\n  let d = \"M\" + ((pts[0][0] + pts[n - 1][0]) / 2) + \" \" + ((pts[0][1] + pts[n - 1][1]) / 2);\n  for (let i = 0; i < n; i++) {\n    const p = pts[i], q2 = pts[(i + 1) % n];\n    d += \"Q\" + p[0] + \" \" + p[1] + \" \" + (p[0] + q2[0]) / 2 + \" \" + (p[1] + q2[1]) / 2;\n  }\n  return d + \"Z\";\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const M = motion($, t);\n  const ph = phases($, t), col = paints($, p.colors);\n  let s = \"\";\n  const horiz = p.direction === \"left\" || p.direction === \"right\";\n  const L = horiz ? W : H, A2 = horiz ? H : W;\n  const g = ph.before ? $.E.in3(ph.cover) : 1 - $.E.out3(ph.uncover);\n  const n = 46;\n  let lines = \"\";\n  for (let i = 0; i < n; i++) {\n    const h1 = $.hash(i * 3.7), h2 = $.hash(i * 9.1 + 2), h3 = $.hash(i * 5.3 + 7);\n    if (h3 > g * 1.3) continue;\n    const pos = h1 * A2, th = (4 + 40 * h2 * h2) * k * (0.4 + g);\n    const len = L * (0.3 + 0.9 * h2) * (0.4 + g), travel = (t * (2.2 + h3 * 2) + h1) % 1;\n    const a = -len + (L + len) * travel;\n    const fill = i % 5 === 0 ? col.lead : i % 5 === 1 ? col.lead2 : col.cover;\n    lines += horiz ? $.rect(a, pos, len, th, { fill: fill, r: th / 2 }) : $.rect(pos, a, th, len, { fill: fill, r: th / 2 });\n  }\n  // The smear: the cover colour washing in at the cut.\n  const wash = $.clamp((g - 0.55) / 0.35);\n  if (wash > 0) lines = $.rect(0, 0, W, H, { fill: col.cover, op: wash }) + lines;\n  const flip = p.direction === \"left\" ? \"translate(\" + W + \" 0) scale(-1 1)\" : p.direction === \"up\" ? \"translate(0 \" + H + \") scale(1 -1)\" : \"\";\n  s += flip ? '<g transform=\"' + flip + '\">' + lines + \"</g>\" : lines;\n  s = M.part(\"cover\", { in: \"none\", out: \"none\", vary: false }, s);\n  return s + cutWord(M, $, t, p.label, col.cover);\n}\n","libs":[]}]},{"format":1,"id":"annotate","name":"MONARC Annotate","version":"1.0.0","description":"Point things out in footage: hand-drawn arrows, circles and boxes, brackets, a spotlight, focus brackets, highlighter marks, a rubber stamp, a cursor that clicks, dimension lines, ticks and crosses, live reactions, a tech pointer, emphasis marks and numbered markers.","author":"MONARC STUDIOS","license":"MONARC Packs License 1.0 (free to use in any video; not for resale as templates)","homepage":"https://volito.dev/docs/packs/annotate","style":"candy","shared":"shared.js","items":["arrow","circle","spotlight","focus","highlight","stamp","reactions","pointer","emphasis","markers","bracket","box-label","cursor","measure","check"],"entries":[{"id":"arrow","name":"Arrow","description":"A hand-drawn (or clean) arrow sweeps from a label to the thing you point at, with a pulse on the spot.","category":"callout","tags":["annotation","arrow","point","look","here","hand-drawn"],"placement":"overlay","duration":{"default":3,"min":1.5,"max":12},"aspects":["16:9","9:16","1:1","4:5"],"props":{"label":{"type":"text","label":"Label","default":"Look here","maxLength":32},"x":{"type":"number","label":"Point across (%)","default":62,"min":0,"max":100,"step":1},"y":{"type":"number","label":"Point down (%)","default":45,"min":0,"max":100,"step":1},"side":{"type":"choice","label":"Label side","options":["auto","left","right","above","below"],"default":"auto"},"look":{"type":"choice","label":"Look","options":["marker","clean"],"default":"marker"},"color":{"type":"color","label":"Colour","default":"","description":"empty: the brand's colour"}},"parts":{"label":"the label","arrow":"the arrow","spot":"the pulse on the spot"},"beats":{"in":0.1},"style":"candy","pack":"annotate","ref":"annotate/arrow","code":"// Annotations point at things in the footage: every place is a percentage of the frame (0–100).\nfunction spot($, x, y) { return [$.W * $.clamp(x / 100), $.H * $.clamp(y / 100)]; }\nfunction boxOf($, x, y, w, h) { return { x: $.W * x / 100, y: $.H * y / 100, w: $.W * w / 100, h: $.H * h / 100 }; }\n/** The annotation colour: the prop when given, else the accent. */\nfunction inkOf($, col) { return col || $.c.accent; }\n/** Which side of a point has the most room for a label. */\nfunction roomy($, x, y) {\n  const room = { left: x, right: $.W - x, above: y, below: $.H - y };\n  const wide = $.W >= $.H;\n  let best = wide ? (x < $.W / 2 ? \"right\" : \"left\") : (y < $.H / 2 ? \"below\" : \"above\");\n  if (room[best] < Math.min($.W, $.H) * 0.3) for (const s in room) if (room[s] > room[best]) best = s;\n  return best;\n}\n/** A label tag (the brand's chip) centred on (cx, cy), kept inside the frame: { markup, box }. */\nfunction tagAt($, str, cx, cy, fill, size) {\n  const txt = $.case(str), o = { size: (size || 40) * $.k, fill: fill, color: $.on(fill) };\n  const sz = $.chip.size(txt, o), m = 24 * $.k;\n  const x = Math.min($.W - m - sz.w, Math.max(m, cx - sz.w / 2)), y = Math.min($.H - m - sz.h, Math.max(m, cy - sz.h / 2));\n  return { markup: $.chip(txt, x, y, o), box: { x: x, y: y, w: sz.w, h: sz.h } };\n}\n/** Pulsing rings on a point (from at). */\nfunction pulse($, t, at, x, y, r, col) {\n  let s = \"\";\n  for (let i = 0; i < 2; i++) {\n    const q = ((t - at - i * 0.6) % 1.2) / 1.2;\n    if (t - at - i * 0.6 < 0) continue;\n    s += $.circle(x, y, r * (1 + q * 2.2), { stroke: col, sw: 4 * $.k, op: 0.8 * (1 - q) });\n  }\n  return s;\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const M = motion($, t);\n  const tin = $.beat(\"in\", 0.1), col = inkOf($, p.color);\n  let s = \"\";\n  const tx = W * p.x / 100, ty = H * p.y / 100;\n  const side = p.side === \"auto\" ? roomy($, tx, ty) : p.side;\n  const v = { left: [-1, 0], right: [1, 0], above: [0, -1], below: [0, 1] }[side] || [1, 0];\n  const gap = Math.min(W, H) * 0.34;\n  const tg = tagAt($, p.label, tx + v[0] * gap * 1.3, ty + v[1] * gap - (v[0] ? gap * 0.35 : 0), col, 44);\n  const b = tg.box, lcx = b.x + b.w / 2, lcy = b.y + b.h / 2;\n  // From the label's edge facing the spot, to just short of the spot.\n  const sx = v[0] ? (v[0] > 0 ? b.x - 14 * k : b.x + b.w + 14 * k) : lcx, sy = v[1] ? (v[1] > 0 ? b.y - 14 * k : b.y + b.h + 14 * k) : lcy;\n  const d = Math.hypot(tx - sx, ty - sy) || 1, ex = tx - ((tx - sx) / d) * 26 * k, ey = ty - ((ty - sy) / d) * 26 * k;\n  const ta = tin + 0.25;\n  let arrow;\n  if (p.look === \"clean\") {\n    const bend = 0.25, mx = (sx + ex) / 2 - (ey - sy) * bend, my = (sy + ey) / 2 + (ex - sx) * bend;\n    const pr = $.E.inOut2($.seg(t, ta, ta + 0.55));\n    const ang = Math.atan2(ey - my, ex - mx), hl = 26 * k;\n    arrow = $.path(\"M\" + sx + \" \" + sy + \"Q\" + mx + \" \" + my + \" \" + ex + \" \" + ey, { stroke: col, sw: 7 * k, draw: pr });\n    if (pr > 0.95) arrow += $.path(\"M\" + (ex - hl * Math.cos(ang - 0.5)) + \" \" + (ey - hl * Math.sin(ang - 0.5)) + \"L\" + ex + \" \" + ey + \"L\" + (ex - hl * Math.cos(ang + 0.5)) + \" \" + (ey - hl * Math.sin(ang + 0.5)), { stroke: col, sw: 7 * k });\n  } else arrow = $.ink(t, ta, $.hand.arrow(sx, sy, ex, ey, { bend: 0.22, seed: 3, rough: 3 * k }), { color: col, sw: 9 * k, dur: 0.55 });\n  s += M.part(\"arrow\", { at: ta, in: \"none\" }, arrow);\n  s += M.part(\"spot\", { at: ta + 0.55, in: \"pop\", box: { x: tx - 20 * k, y: ty - 20 * k, w: 40 * k, h: 40 * k } }, $.circle(tx, ty, 9 * k, { fill: col }) + pulse($, t, ta + 0.55, tx, ty, 12 * k, col));\n  s += M.part(\"label\", { at: tin, in: \"pop\", box: b }, tg.markup);\n  return s;\n}\n","libs":[]},{"id":"circle","name":"Circle it","description":"A marker loop drawn round the thing that matters — once, twice, or a clean ring — with an optional label.","category":"callout","tags":["annotation","circle","loop","ring","marker","hand-drawn"],"placement":"overlay","duration":{"default":3,"min":1.5,"max":12},"aspects":["16:9","9:16","1:1","4:5"],"props":{"x":{"type":"number","label":"Box left (%)","default":40,"min":0,"max":100,"step":1},"y":{"type":"number","label":"Box top (%)","default":30,"min":0,"max":100,"step":1},"w":{"type":"number","label":"Box width (%)","default":22,"min":2,"max":100,"step":1},"h":{"type":"number","label":"Box height (%)","default":30,"min":2,"max":100,"step":1},"look":{"type":"choice","label":"Look","options":["marker","double","clean"],"default":"marker"},"label":{"type":"text","label":"Label","default":"","maxLength":28,"description":"a tag by the loop (empty for none)"},"color":{"type":"color","label":"Colour","default":"","description":"empty: the brand's colour"}},"parts":{"loop":"the loop","label":"the label"},"beats":{"in":0.1},"style":"candy","pack":"annotate","ref":"annotate/circle","code":"// Annotations point at things in the footage: every place is a percentage of the frame (0–100).\nfunction spot($, x, y) { return [$.W * $.clamp(x / 100), $.H * $.clamp(y / 100)]; }\nfunction boxOf($, x, y, w, h) { return { x: $.W * x / 100, y: $.H * y / 100, w: $.W * w / 100, h: $.H * h / 100 }; }\n/** The annotation colour: the prop when given, else the accent. */\nfunction inkOf($, col) { return col || $.c.accent; }\n/** Which side of a point has the most room for a label. */\nfunction roomy($, x, y) {\n  const room = { left: x, right: $.W - x, above: y, below: $.H - y };\n  const wide = $.W >= $.H;\n  let best = wide ? (x < $.W / 2 ? \"right\" : \"left\") : (y < $.H / 2 ? \"below\" : \"above\");\n  if (room[best] < Math.min($.W, $.H) * 0.3) for (const s in room) if (room[s] > room[best]) best = s;\n  return best;\n}\n/** A label tag (the brand's chip) centred on (cx, cy), kept inside the frame: { markup, box }. */\nfunction tagAt($, str, cx, cy, fill, size) {\n  const txt = $.case(str), o = { size: (size || 40) * $.k, fill: fill, color: $.on(fill) };\n  const sz = $.chip.size(txt, o), m = 24 * $.k;\n  const x = Math.min($.W - m - sz.w, Math.max(m, cx - sz.w / 2)), y = Math.min($.H - m - sz.h, Math.max(m, cy - sz.h / 2));\n  return { markup: $.chip(txt, x, y, o), box: { x: x, y: y, w: sz.w, h: sz.h } };\n}\n/** Pulsing rings on a point (from at). */\nfunction pulse($, t, at, x, y, r, col) {\n  let s = \"\";\n  for (let i = 0; i < 2; i++) {\n    const q = ((t - at - i * 0.6) % 1.2) / 1.2;\n    if (t - at - i * 0.6 < 0) continue;\n    s += $.circle(x, y, r * (1 + q * 2.2), { stroke: col, sw: 4 * $.k, op: 0.8 * (1 - q) });\n  }\n  return s;\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const M = motion($, t);\n  const tin = $.beat(\"in\", 0.1), col = inkOf($, p.color);\n  let s = \"\";\n  const b = boxOf($, p.x, p.y, p.w, p.h);\n  const cx = b.x + b.w / 2, cy = b.y + b.h / 2, rx = b.w * 0.62 + 14 * k, ry = b.h * 0.62 + 14 * k;\n  let loop;\n  if (p.look === \"clean\") {\n    const d = \"M\" + (cx + rx) + \" \" + cy + \"A\" + rx + \" \" + ry + \" 0 1 1 \" + (cx - rx) + \" \" + cy + \"A\" + rx + \" \" + ry + \" 0 1 1 \" + (cx + rx) + \" \" + cy;\n    loop = $.path(d, { stroke: col, sw: 7 * k, draw: $.E.inOut2($.seg(t, tin, tin + 0.6)), transform: \"rotate(-90 \" + cx + \" \" + cy + \")\" });\n  } else {\n    loop = $.ink(t, tin, $.hand.circle(cx, cy, rx, ry, { turns: 1.12, seed: 2, rough: 4 * k }), { color: col, sw: 9 * k, dur: 0.6 });\n    if (p.look === \"double\") loop += $.ink(t, tin + 0.35, $.hand.circle(cx, cy, rx * 1.06, ry * 0.96, { turns: 1.05, seed: 9, rough: 5 * k }), { color: col, sw: 6 * k, dur: 0.5 });\n  }\n  s += M.part(\"loop\", { at: tin, in: \"none\" }, loop);\n  if (p.label) {\n    const tg = tagAt($, p.label, cx + rx * 0.75, cy - ry - 34 * k, col, 36);\n    s += M.part(\"label\", { at: tin + 0.5, in: \"pop\", box: tg.box }, tg.markup);\n  }\n  return s;\n}\n","libs":[]},{"id":"spotlight","name":"Spotlight","description":"Everything but one area dims; a feathered spotlight closes onto it with an accent ring, and a label sits by it.","category":"callout","tags":["annotation","spotlight","focus","dim","highlight","isolate"],"placement":"overlay","duration":{"default":4,"min":1.5,"max":15},"aspects":["16:9","9:16","1:1","4:5"],"props":{"x":{"type":"number","label":"Box left (%)","default":38,"min":0,"max":100,"step":1},"y":{"type":"number","label":"Box top (%)","default":28,"min":0,"max":100,"step":1},"w":{"type":"number","label":"Box width (%)","default":24,"min":2,"max":100,"step":1},"h":{"type":"number","label":"Box height (%)","default":34,"min":2,"max":100,"step":1},"shape":{"type":"choice","label":"Shape","options":["circle","box"],"default":"circle"},"dim":{"type":"number","label":"Dim (%)","default":70,"min":10,"max":95,"step":5},"label":{"type":"text","label":"Label","default":"This part","maxLength":28,"description":"empty for none"},"note":{"type":"text","label":"Note","default":"","maxLength":60,"description":"a line under the label"},"color":{"type":"color","label":"Colour","default":"","description":"empty: the brand's colour"}},"parts":{"dim":"the dimmed frame","ring":"the ring","label":"the label and note"},"beats":{"in":0.1},"style":"candy","pack":"annotate","ref":"annotate/spotlight","code":"// Annotations point at things in the footage: every place is a percentage of the frame (0–100).\nfunction spot($, x, y) { return [$.W * $.clamp(x / 100), $.H * $.clamp(y / 100)]; }\nfunction boxOf($, x, y, w, h) { return { x: $.W * x / 100, y: $.H * y / 100, w: $.W * w / 100, h: $.H * h / 100 }; }\n/** The annotation colour: the prop when given, else the accent. */\nfunction inkOf($, col) { return col || $.c.accent; }\n/** Which side of a point has the most room for a label. */\nfunction roomy($, x, y) {\n  const room = { left: x, right: $.W - x, above: y, below: $.H - y };\n  const wide = $.W >= $.H;\n  let best = wide ? (x < $.W / 2 ? \"right\" : \"left\") : (y < $.H / 2 ? \"below\" : \"above\");\n  if (room[best] < Math.min($.W, $.H) * 0.3) for (const s in room) if (room[s] > room[best]) best = s;\n  return best;\n}\n/** A label tag (the brand's chip) centred on (cx, cy), kept inside the frame: { markup, box }. */\nfunction tagAt($, str, cx, cy, fill, size) {\n  const txt = $.case(str), o = { size: (size || 40) * $.k, fill: fill, color: $.on(fill) };\n  const sz = $.chip.size(txt, o), m = 24 * $.k;\n  const x = Math.min($.W - m - sz.w, Math.max(m, cx - sz.w / 2)), y = Math.min($.H - m - sz.h, Math.max(m, cy - sz.h / 2));\n  return { markup: $.chip(txt, x, y, o), box: { x: x, y: y, w: sz.w, h: sz.h } };\n}\n/** Pulsing rings on a point (from at). */\nfunction pulse($, t, at, x, y, r, col) {\n  let s = \"\";\n  for (let i = 0; i < 2; i++) {\n    const q = ((t - at - i * 0.6) % 1.2) / 1.2;\n    if (t - at - i * 0.6 < 0) continue;\n    s += $.circle(x, y, r * (1 + q * 2.2), { stroke: col, sw: 4 * $.k, op: 0.8 * (1 - q) });\n  }\n  return s;\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const M = motion($, t);\n  const tin = $.beat(\"in\", 0.1), col = inkOf($, p.color);\n  let s = \"\";\n  const b = boxOf($, p.x, p.y, p.w, p.h);\n  const cx = b.x + b.w / 2, cy = b.y + b.h / 2;\n  const e = $.E.inOut3($.seg(t, tin, tin + 0.9));\n  const circ = p.shape !== \"box\";\n  const rx1 = circ ? b.w * 0.62 + 18 * k : b.w / 2 + 18 * k, ry1 = circ ? b.h * 0.62 + 18 * k : b.h / 2 + 18 * k;\n  const rx = $.lerp(Math.hypot(W, H), rx1, e), ry = $.lerp(Math.hypot(W, H), ry1, e);\n  const rr = circ ? 0 : Math.min(rx, ry, 28 * k);\n  const hole = circ\n    ? \"M\" + (cx + rx) + \" \" + cy + \"A\" + rx + \" \" + ry + \" 0 1 0 \" + (cx - rx) + \" \" + cy + \"A\" + rx + \" \" + ry + \" 0 1 0 \" + (cx + rx) + \" \" + cy + \"Z\"\n    : \"M\" + (cx - rx + rr) + \" \" + (cy - ry) + \"H\" + (cx + rx - rr) + \"Q\" + (cx + rx) + \" \" + (cy - ry) + \" \" + (cx + rx) + \" \" + (cy - ry + rr) + \"V\" + (cy + ry - rr) + \"Q\" + (cx + rx) + \" \" + (cy + ry) + \" \" + (cx + rx - rr) + \" \" + (cy + ry) + \"H\" + (cx - rx + rr) + \"Q\" + (cx - rx) + \" \" + (cy + ry) + \" \" + (cx - rx) + \" \" + (cy + ry - rr) + \"V\" + (cy - ry + rr) + \"Q\" + (cx - rx) + \" \" + (cy - ry) + \" \" + (cx - rx + rr) + \" \" + (cy - ry) + \"Z\";\n  const fid = $.uid(\"fb\"), m = 80 * k;\n  $.def('<filter id=\"' + fid + '\" x=\"-5%\" y=\"-5%\" width=\"110%\" height=\"110%\"><feGaussianBlur stdDeviation=\"' + 16 * k + '\"/></filter>');\n  s += M.part(\"dim\", { at: tin, in: \"fade\", dur: 0.4, vary: false }, $.path(\"M\" + -m + \" \" + -m + \"H\" + (W + m) + \"V\" + (H + m) + \"H\" + -m + \"Z\" + hole, { fill: \"rgba(0,0,0,\" + p.dim / 100 + \")\", extra: 'fill-rule=\"evenodd\" filter=\"url(#' + fid + ')\"' }));\n  const ringP = $.E.inOut2($.seg(t, tin + 0.8, tin + 1.3));\n  if (ringP > 0) s += M.part(\"ring\", { at: tin + 0.8, in: \"none\" }, $.path(hole, { stroke: col, sw: 5 * k, draw: ringP }));\n  if (p.label) {\n    const below = cy + ry1 + 150 * k < H;\n    const ly = below ? cy + ry1 + 30 * k : cy - ry1 - 30 * k - 56 * k;\n    const tg = tagAt($, p.label, cx, ly + 28 * k, col, 40);\n    let lab = tg.markup;\n    if (p.note) {\n      const nf = Object.assign({}, $.font(\"text\"), { size: 32 * k });\n      const nw = Math.min(W * 0.8, $.measure(p.note, nf));\n      const nx = Math.min(W - 30 * k - nw / 2, Math.max(30 * k + nw / 2, cx));\n      lab += $.text(p.note, Object.assign({}, nf, { x: nx, y: below ? tg.box.y + tg.box.h + 16 * k : tg.box.y - 16 * k - 40 * k, anchor: \"middle\", fill: \"#FFFFFF\", maxW: W * 0.8 }));\n    }\n    s += M.part(\"label\", { at: tin + 1.0, in: \"rise\", distance: 16, box: tg.box }, lab);\n  }\n  return s;\n}\n","libs":[]},{"id":"focus","name":"Focus lock","description":"Camera focus brackets hunt in from the frame's corners, snap onto the subject with a blink, and lock in the accent with a label and crosshair.","category":"callout","tags":["annotation","focus","brackets","camera","lock","target","track"],"placement":"overlay","duration":{"default":3,"min":1.5,"max":12},"aspects":["16:9","9:16","1:1","4:5"],"props":{"x":{"type":"number","label":"Box left (%)","default":36,"min":0,"max":100,"step":1},"y":{"type":"number","label":"Box top (%)","default":26,"min":0,"max":100,"step":1},"w":{"type":"number","label":"Box width (%)","default":28,"min":2,"max":100,"step":1},"h":{"type":"number","label":"Box height (%)","default":40,"min":2,"max":100,"step":1},"label":{"type":"text","label":"Label","default":"Subject","maxLength":28,"description":"empty for none"},"color":{"type":"color","label":"Colour","default":"","description":"empty: the brand's colour"}},"parts":{"brackets":"the corner brackets","cross":"the crosshair","label":"the label"},"beats":{"in":0.1},"style":"candy","pack":"annotate","ref":"annotate/focus","code":"// Annotations point at things in the footage: every place is a percentage of the frame (0–100).\nfunction spot($, x, y) { return [$.W * $.clamp(x / 100), $.H * $.clamp(y / 100)]; }\nfunction boxOf($, x, y, w, h) { return { x: $.W * x / 100, y: $.H * y / 100, w: $.W * w / 100, h: $.H * h / 100 }; }\n/** The annotation colour: the prop when given, else the accent. */\nfunction inkOf($, col) { return col || $.c.accent; }\n/** Which side of a point has the most room for a label. */\nfunction roomy($, x, y) {\n  const room = { left: x, right: $.W - x, above: y, below: $.H - y };\n  const wide = $.W >= $.H;\n  let best = wide ? (x < $.W / 2 ? \"right\" : \"left\") : (y < $.H / 2 ? \"below\" : \"above\");\n  if (room[best] < Math.min($.W, $.H) * 0.3) for (const s in room) if (room[s] > room[best]) best = s;\n  return best;\n}\n/** A label tag (the brand's chip) centred on (cx, cy), kept inside the frame: { markup, box }. */\nfunction tagAt($, str, cx, cy, fill, size) {\n  const txt = $.case(str), o = { size: (size || 40) * $.k, fill: fill, color: $.on(fill) };\n  const sz = $.chip.size(txt, o), m = 24 * $.k;\n  const x = Math.min($.W - m - sz.w, Math.max(m, cx - sz.w / 2)), y = Math.min($.H - m - sz.h, Math.max(m, cy - sz.h / 2));\n  return { markup: $.chip(txt, x, y, o), box: { x: x, y: y, w: sz.w, h: sz.h } };\n}\n/** Pulsing rings on a point (from at). */\nfunction pulse($, t, at, x, y, r, col) {\n  let s = \"\";\n  for (let i = 0; i < 2; i++) {\n    const q = ((t - at - i * 0.6) % 1.2) / 1.2;\n    if (t - at - i * 0.6 < 0) continue;\n    s += $.circle(x, y, r * (1 + q * 2.2), { stroke: col, sw: 4 * $.k, op: 0.8 * (1 - q) });\n  }\n  return s;\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const M = motion($, t);\n  const tin = $.beat(\"in\", 0.1), col = inkOf($, p.color);\n  let s = \"\";\n  const b = boxOf($, p.x, p.y, p.w, p.h), m = Math.min(W, H) * 0.08;\n  const q = $.clamp($.spring(Math.max(0, t - tin), 2.2, 6.5), 0, 1.15);\n  const bx = $.lerp(m, b.x, q), by = $.lerp(m, b.y, q), bw = $.lerp(W - 2 * m, b.w, q), bh = $.lerp(H - 2 * m, b.h, q);\n  const lock = tin + 0.7, locked = t >= lock;\n  const blink = locked && t < lock + 0.3 && Math.floor((t - lock) * 14) % 2 === 1;\n  const stroke = locked ? col : \"#FFFFFF\", L = Math.min(bw, bh) * 0.22, sw = 6 * k;\n  const corner = function (x, y, dx, dy) { return $.path(\"M\" + x + \" \" + (y + dy * L) + \"V\" + y + \"H\" + (x + dx * L), { stroke: stroke, sw: sw, cap: \"square\", join: \"miter\" }); };\n  const br = corner(bx, by, 1, 1) + corner(bx + bw, by, -1, 1) + corner(bx, by + bh, 1, -1) + corner(bx + bw, by + bh, -1, -1);\n  s += M.part(\"brackets\", { at: tin, in: \"fade\", dur: 0.2 }, blink ? \"\" : br);\n  const cx = bx + bw / 2, cy = by + bh / 2, cr = 18 * k;\n  s += M.part(\"cross\", { at: lock, in: \"pop\", box: { x: cx - cr, y: cy - cr, w: 2 * cr, h: 2 * cr } }, $.path(\"M\" + (cx - cr) + \" \" + cy + \"H\" + (cx - 6 * k) + \"M\" + (cx + 6 * k) + \" \" + cy + \"H\" + (cx + cr) + \"M\" + cx + \" \" + (cy - cr) + \"V\" + (cy - 6 * k) + \"M\" + cx + \" \" + (cy + 6 * k) + \"V\" + (cy + cr), { stroke: col, sw: 3 * k }));\n  if (p.label) {\n    const txt = $.case(p.label), f = Object.assign({}, $.font(\"mono\"), { size: 30 * k });\n    const w = $.measure(txt, f) + 28 * k, h = 48 * k;\n    const lx = Math.min(W - w - 20 * k, Math.max(20 * k, b.x)), ly = b.y - h - 14 * k > 20 * k ? b.y - h - 14 * k : b.y + b.h + 14 * k;\n    s += M.part(\"label\", { at: lock + 0.1, in: \"wipe\", from: \"left\", box: { x: lx, y: ly, w: w, h: h } }, $.rect(lx, ly, w, h, { fill: col }) + $.text(txt, Object.assign({}, f, { x: lx + 14 * k, y: ly + (h - f.size) / 2 - 2 * k, fill: $.on(col) })));\n  }\n  return s;\n}\n","libs":[]},{"id":"highlight","name":"Highlight","description":"A highlighter swipes across a word or area — or a marker underline, box or strike-through drawn on by hand.","category":"callout","tags":["annotation","highlight","marker","underline","strike","emphasis"],"placement":"overlay","duration":{"default":2.5,"min":1,"max":12},"aspects":["16:9","9:16","1:1","4:5"],"props":{"x":{"type":"number","label":"Box left (%)","default":30,"min":0,"max":100,"step":1},"y":{"type":"number","label":"Box top (%)","default":44,"min":0,"max":100,"step":1},"w":{"type":"number","label":"Box width (%)","default":40,"min":2,"max":100,"step":1},"h":{"type":"number","label":"Box height (%)","default":10,"min":1,"max":100,"step":1},"mode":{"type":"choice","label":"Mark","options":["highlight","underline","box","strike"],"default":"highlight"},"color":{"type":"color","label":"Colour","default":"","description":"empty: the brand's colour"}},"parts":{"mark":"the mark"},"beats":{"in":0.1},"style":"candy","pack":"annotate","ref":"annotate/highlight","code":"// Annotations point at things in the footage: every place is a percentage of the frame (0–100).\nfunction spot($, x, y) { return [$.W * $.clamp(x / 100), $.H * $.clamp(y / 100)]; }\nfunction boxOf($, x, y, w, h) { return { x: $.W * x / 100, y: $.H * y / 100, w: $.W * w / 100, h: $.H * h / 100 }; }\n/** The annotation colour: the prop when given, else the accent. */\nfunction inkOf($, col) { return col || $.c.accent; }\n/** Which side of a point has the most room for a label. */\nfunction roomy($, x, y) {\n  const room = { left: x, right: $.W - x, above: y, below: $.H - y };\n  const wide = $.W >= $.H;\n  let best = wide ? (x < $.W / 2 ? \"right\" : \"left\") : (y < $.H / 2 ? \"below\" : \"above\");\n  if (room[best] < Math.min($.W, $.H) * 0.3) for (const s in room) if (room[s] > room[best]) best = s;\n  return best;\n}\n/** A label tag (the brand's chip) centred on (cx, cy), kept inside the frame: { markup, box }. */\nfunction tagAt($, str, cx, cy, fill, size) {\n  const txt = $.case(str), o = { size: (size || 40) * $.k, fill: fill, color: $.on(fill) };\n  const sz = $.chip.size(txt, o), m = 24 * $.k;\n  const x = Math.min($.W - m - sz.w, Math.max(m, cx - sz.w / 2)), y = Math.min($.H - m - sz.h, Math.max(m, cy - sz.h / 2));\n  return { markup: $.chip(txt, x, y, o), box: { x: x, y: y, w: sz.w, h: sz.h } };\n}\n/** Pulsing rings on a point (from at). */\nfunction pulse($, t, at, x, y, r, col) {\n  let s = \"\";\n  for (let i = 0; i < 2; i++) {\n    const q = ((t - at - i * 0.6) % 1.2) / 1.2;\n    if (t - at - i * 0.6 < 0) continue;\n    s += $.circle(x, y, r * (1 + q * 2.2), { stroke: col, sw: 4 * $.k, op: 0.8 * (1 - q) });\n  }\n  return s;\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const M = motion($, t);\n  const tin = $.beat(\"in\", 0.1), col = inkOf($, p.color);\n  let s = \"\";\n  const b = boxOf($, p.x, p.y, p.w, p.h);\n  // A highlighter is yellow unless told otherwise; pens are the accent.\n  const hi = p.color || c.accent2;\n  let mark;\n  if (p.mode === \"highlight\") {\n    // A marker band with ragged ends, swiped on left to right.\n    const j = function (i) { return ($.hash(i * 3.7) - 0.5) * 10 * k; };\n    const d = \"M\" + (b.x - 10 * k + j(1)) + \" \" + (b.y + j(2)) + \"L\" + (b.x + b.w + 8 * k + j(3)) + \" \" + (b.y - 4 * k + j(4)) + \"L\" + (b.x + b.w + 14 * k + j(5)) + \" \" + (b.y + b.h + j(6)) + \"L\" + (b.x - 4 * k + j(7)) + \" \" + (b.y + b.h + 4 * k + j(8)) + \"Z\";\n    mark = M.part(\"mark\", { at: tin, in: \"wipe\", from: \"left\", dur: 0.45, box: { x: b.x - 20 * k, y: b.y - 12 * k, w: b.w + 40 * k, h: b.h + 24 * k } }, $.path(d, { fill: $.rgba(hi, 0.5) }));\n  } else {\n    const shape = p.mode === \"box\" ? $.hand.rect(b.x - 12 * k, b.y - 10 * k, b.w + 24 * k, b.h + 20 * k, { seed: 4, rough: 4 * k })\n      : p.mode === \"strike\" ? $.hand.line(b.x - 10 * k, b.y + b.h * 0.55, b.x + b.w + 10 * k, b.y + b.h * 0.45, { seed: 5, rough: 3 * k })\n      : $.hand.curve(b.x - 6 * k, b.y + b.h + 12 * k, b.x + b.w + 6 * k, b.y + b.h + 6 * k, { bend: 0.04, seed: 6, rough: 3 * k });\n    mark = M.part(\"mark\", { at: tin, in: \"none\" }, $.ink(t, tin, shape, { color: col, sw: (p.mode === \"box\" ? 7 : 12) * k, dur: 0.5 }));\n  }\n  return s + mark;\n}\n","libs":[]},{"id":"stamp","name":"Stamp","description":"A rubber stamp slams down at an angle — APPROVED, SOLD OUT, NEW — with a rough inked double border and a jolt.","category":"callout","tags":["annotation","stamp","approved","sold","rubber","badge","seal"],"placement":"overlay","duration":{"default":2.5,"min":1,"max":12},"aspects":["16:9","9:16","1:1","4:5"],"props":{"text":{"type":"text","label":"Words","default":"Approved","maxLength":18},"x":{"type":"number","label":"Centre across (%)","default":50,"min":0,"max":100,"step":1},"y":{"type":"number","label":"Centre down (%)","default":50,"min":0,"max":100,"step":1},"angle":{"type":"number","label":"Angle","default":-12,"min":-45,"max":45,"step":1},"size":{"type":"number","label":"Size","default":100,"min":40,"max":240,"step":5},"color":{"type":"color","label":"Colour","default":"","description":"empty: the brand's colour"}},"parts":{"stamp":"the stamp"},"beats":{"in":0.1},"style":"candy","pack":"annotate","ref":"annotate/stamp","code":"// Annotations point at things in the footage: every place is a percentage of the frame (0–100).\nfunction spot($, x, y) { return [$.W * $.clamp(x / 100), $.H * $.clamp(y / 100)]; }\nfunction boxOf($, x, y, w, h) { return { x: $.W * x / 100, y: $.H * y / 100, w: $.W * w / 100, h: $.H * h / 100 }; }\n/** The annotation colour: the prop when given, else the accent. */\nfunction inkOf($, col) { return col || $.c.accent; }\n/** Which side of a point has the most room for a label. */\nfunction roomy($, x, y) {\n  const room = { left: x, right: $.W - x, above: y, below: $.H - y };\n  const wide = $.W >= $.H;\n  let best = wide ? (x < $.W / 2 ? \"right\" : \"left\") : (y < $.H / 2 ? \"below\" : \"above\");\n  if (room[best] < Math.min($.W, $.H) * 0.3) for (const s in room) if (room[s] > room[best]) best = s;\n  return best;\n}\n/** A label tag (the brand's chip) centred on (cx, cy), kept inside the frame: { markup, box }. */\nfunction tagAt($, str, cx, cy, fill, size) {\n  const txt = $.case(str), o = { size: (size || 40) * $.k, fill: fill, color: $.on(fill) };\n  const sz = $.chip.size(txt, o), m = 24 * $.k;\n  const x = Math.min($.W - m - sz.w, Math.max(m, cx - sz.w / 2)), y = Math.min($.H - m - sz.h, Math.max(m, cy - sz.h / 2));\n  return { markup: $.chip(txt, x, y, o), box: { x: x, y: y, w: sz.w, h: sz.h } };\n}\n/** Pulsing rings on a point (from at). */\nfunction pulse($, t, at, x, y, r, col) {\n  let s = \"\";\n  for (let i = 0; i < 2; i++) {\n    const q = ((t - at - i * 0.6) % 1.2) / 1.2;\n    if (t - at - i * 0.6 < 0) continue;\n    s += $.circle(x, y, r * (1 + q * 2.2), { stroke: col, sw: 4 * $.k, op: 0.8 * (1 - q) });\n  }\n  return s;\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const M = motion($, t);\n  const tin = $.beat(\"in\", 0.1), col = inkOf($, p.color);\n  let s = \"\";\n  const cx = W * p.x / 100, cy = H * p.y / 100, txt = String(p.text || \"\").toUpperCase();\n  const f = fitFont($, txt, \"display\", p.size * k, W * 0.7, { ls: 0.04 });\n  const tw = $.measure(txt, f), pad = f.size * 0.42, w = tw + pad * 2, h = f.size * 1.5;\n  // Ink that did not take: a fixed noise mask so the stamp looks pressed, not printed.\n  const nid = $.uid(\"sn\"), mid = $.uid(\"sm\");\n  $.def('<filter id=\"' + nid + '\" x=\"0\" y=\"0\" width=\"100%\" height=\"100%\"><feTurbulence type=\"fractalNoise\" baseFrequency=\"' + (0.9 / k).toFixed(3) + '\" numOctaves=\"2\" seed=\"7\"/><feColorMatrix type=\"matrix\" values=\"0 0 0 0 1  0 0 0 0 1  0 0 0 0 1  0 0 0 -2.4 1.75\"/></filter>');\n  $.def('<mask id=\"' + mid + '\" maskUnits=\"userSpaceOnUse\" x=\"' + (cx - w) + '\" y=\"' + (cy - h) + '\" width=\"' + 2 * w + '\" height=\"' + 2 * h + '\"><rect x=\"' + (cx - w) + '\" y=\"' + (cy - h) + '\" width=\"' + 2 * w + '\" height=\"' + 2 * h + '\" fill=\"#fff\" filter=\"url(#' + nid + ')\"/></mask>');\n  const body = $.rect(cx - w / 2, cy - h / 2, w, h, { r: 14 * k, stroke: col, sw: 9 * k }) + $.rect(cx - w / 2 + 16 * k, cy - h / 2 + 16 * k, w - 32 * k, h - 32 * k, { r: 8 * k, stroke: col, sw: 4 * k }) +\n    $.text(txt, Object.assign({}, f, { x: cx, y: cy - f.size * 0.56, anchor: \"middle\", fill: col }));\n  const inked = '<g mask=\"url(#' + mid + ')\" transform=\"rotate(' + p.angle + \" \" + cx + \" \" + cy + ')\">' + body + \"</g>\";\n  s += M.part(\"stamp\", { at: tin, in: \"slam\", box: { x: cx - w / 2, y: cy - h / 2, w: w, h: h } }, inked);\n  return s;\n}\n","libs":[]},{"id":"reactions","name":"Reactions","description":"Live reactions float up in a stream — hearts, likes, fire, laughs — swaying, popping in and fading out, like a live stream.","category":"social","tags":["annotation","reactions","hearts","likes","live","emoji","float"],"placement":"overlay","duration":{"default":5,"min":2,"max":30},"aspects":["16:9","9:16","1:1","4:5"],"props":{"kind":{"type":"choice","label":"Reactions","options":["mixed","hearts","likes","fire","laughs","stars"],"default":"mixed"},"x":{"type":"number","label":"Stream across (%)","default":86,"min":0,"max":100,"step":1},"rate":{"type":"number","label":"Per second","default":4,"min":1,"max":12,"step":1},"size":{"type":"number","label":"Size","default":88,"min":36,"max":160,"step":4},"color":{"type":"color","label":"Colour","default":"","description":"empty: the brand's colour"}},"parts":{"stream":"the reactions"},"beats":{"in":0.1},"style":"candy","pack":"annotate","ref":"annotate/reactions","code":"// Annotations point at things in the footage: every place is a percentage of the frame (0–100).\nfunction spot($, x, y) { return [$.W * $.clamp(x / 100), $.H * $.clamp(y / 100)]; }\nfunction boxOf($, x, y, w, h) { return { x: $.W * x / 100, y: $.H * y / 100, w: $.W * w / 100, h: $.H * h / 100 }; }\n/** The annotation colour: the prop when given, else the accent. */\nfunction inkOf($, col) { return col || $.c.accent; }\n/** Which side of a point has the most room for a label. */\nfunction roomy($, x, y) {\n  const room = { left: x, right: $.W - x, above: y, below: $.H - y };\n  const wide = $.W >= $.H;\n  let best = wide ? (x < $.W / 2 ? \"right\" : \"left\") : (y < $.H / 2 ? \"below\" : \"above\");\n  if (room[best] < Math.min($.W, $.H) * 0.3) for (const s in room) if (room[s] > room[best]) best = s;\n  return best;\n}\n/** A label tag (the brand's chip) centred on (cx, cy), kept inside the frame: { markup, box }. */\nfunction tagAt($, str, cx, cy, fill, size) {\n  const txt = $.case(str), o = { size: (size || 40) * $.k, fill: fill, color: $.on(fill) };\n  const sz = $.chip.size(txt, o), m = 24 * $.k;\n  const x = Math.min($.W - m - sz.w, Math.max(m, cx - sz.w / 2)), y = Math.min($.H - m - sz.h, Math.max(m, cy - sz.h / 2));\n  return { markup: $.chip(txt, x, y, o), box: { x: x, y: y, w: sz.w, h: sz.h } };\n}\n/** Pulsing rings on a point (from at). */\nfunction pulse($, t, at, x, y, r, col) {\n  let s = \"\";\n  for (let i = 0; i < 2; i++) {\n    const q = ((t - at - i * 0.6) % 1.2) / 1.2;\n    if (t - at - i * 0.6 < 0) continue;\n    s += $.circle(x, y, r * (1 + q * 2.2), { stroke: col, sw: 4 * $.k, op: 0.8 * (1 - q) });\n  }\n  return s;\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const M = motion($, t);\n  const tin = $.beat(\"in\", 0.1), col = inkOf($, p.color);\n  let s = \"\";\n  const KINDS = { hearts: [\"heart\", \"#FF3B5C\"], likes: [\"thumbsUp\", \"#2F7BFF\"], fire: [\"fire\", \"#FF7A1A\"], laughs: [\"smile\", \"#FFC21A\"], stars: [\"star\", \"#B55CFF\"] };\n  const names = p.kind === \"mixed\" ? [\"hearts\", \"likes\", \"fire\", \"laughs\", \"hearts\", \"stars\"] : [p.kind];\n  const x0 = W * p.x / 100, life = 2.6, rise = H * 0.62, n = Math.ceil(($.dur - tin) * p.rate);\n  const end = M.outAt;\n  let out = \"\";\n  for (let i = 0; i < n; i++) {\n    const born = tin + i / p.rate + ($.hash(i * 1.7) - 0.5) * 0.15, age = t - born;\n    if (age < 0 || age > life || born > end) continue;\n    const kd = KINDS[names[Math.floor($.hash(i * 4.1) * names.length) % names.length]];\n    const q = age / life, r = p.size * k * 0.5 * (0.8 + 0.4 * $.hash(i * 2.9));\n    const x = x0 + Math.sin(age * 2.4 + i) * 34 * k + ($.hash(i * 6.3) - 0.5) * 60 * k;\n    const y = H * 0.9 - rise * $.E.out2(q);\n    const sc = Math.min(1, $.E.outBack($.clamp(age / 0.3))), op = q > 0.7 ? 1 - (q - 0.7) / 0.3 : 1;\n    out += '<g opacity=\"' + op.toFixed(3) + '\" transform=\"translate(' + x + \" \" + y + \") scale(\" + sc + ')\">' + $.circle(0, 0, r, { fill: kd[1] }) + $.icon(kd[0], -r * 0.55, -r * 0.55, r * 1.1, { color: \"#FFFFFF\", fill: \"#FFFFFF\", sw: 2 }) + \"</g>\";\n  }\n  return s + M.part(\"stream\", { in: \"none\", out: \"none\", vary: false }, out);\n}\n\n/** A small pop as each reaction is born, panned with it and pitched a little differently. */\nfunction sound($) {\n  const p = $.data, tin = $.beat(\"in\", 0.1), end = motion($, 0).outAt, n = Math.ceil(($.dur - tin) * p.rate);\n  const out = [];\n  for (let i = 0; i < n; i++) {\n    const born = tin + i / p.rate + ($.hash(i * 1.7) - 0.5) * 0.15;\n    if (born > end) continue;\n    out.push({ at: Math.max(0, born), kind: \"pop\", level: 0.45, pitch: Math.round(($.hash(i * 3.3) - 0.5) * 8), pan: (p.x / 50 - 1) * 0.6 });\n  }\n  return out;\n}\n","libs":[]},{"id":"pointer","name":"Pointer","description":"A tech callout: a pulsing dot on the spot, a leader line that draws out at an angle, and a card with a title and a detail.","category":"callout","tags":["annotation","pointer","callout","leader","label","spec","tech"],"placement":"overlay","duration":{"default":4,"min":1.5,"max":15},"aspects":["16:9","9:16","1:1","4:5"],"props":{"title":{"type":"text","label":"Title","default":"48 MP sensor","maxLength":32},"detail":{"type":"text","label":"Detail","default":"Larger pixels, better low light","maxLength":64},"x":{"type":"number","label":"Point across (%)","default":42,"min":0,"max":100,"step":1},"y":{"type":"number","label":"Point down (%)","default":55,"min":0,"max":100,"step":1},"side":{"type":"choice","label":"Card side","options":["auto","left","right"],"default":"auto"},"color":{"type":"color","label":"Colour","default":"","description":"empty: the brand's colour"}},"parts":{"dot":"the dot on the spot","line":"the leader line","card":"the card"},"beats":{"in":0.1},"style":"candy","pack":"annotate","ref":"annotate/pointer","code":"// Annotations point at things in the footage: every place is a percentage of the frame (0–100).\nfunction spot($, x, y) { return [$.W * $.clamp(x / 100), $.H * $.clamp(y / 100)]; }\nfunction boxOf($, x, y, w, h) { return { x: $.W * x / 100, y: $.H * y / 100, w: $.W * w / 100, h: $.H * h / 100 }; }\n/** The annotation colour: the prop when given, else the accent. */\nfunction inkOf($, col) { return col || $.c.accent; }\n/** Which side of a point has the most room for a label. */\nfunction roomy($, x, y) {\n  const room = { left: x, right: $.W - x, above: y, below: $.H - y };\n  const wide = $.W >= $.H;\n  let best = wide ? (x < $.W / 2 ? \"right\" : \"left\") : (y < $.H / 2 ? \"below\" : \"above\");\n  if (room[best] < Math.min($.W, $.H) * 0.3) for (const s in room) if (room[s] > room[best]) best = s;\n  return best;\n}\n/** A label tag (the brand's chip) centred on (cx, cy), kept inside the frame: { markup, box }. */\nfunction tagAt($, str, cx, cy, fill, size) {\n  const txt = $.case(str), o = { size: (size || 40) * $.k, fill: fill, color: $.on(fill) };\n  const sz = $.chip.size(txt, o), m = 24 * $.k;\n  const x = Math.min($.W - m - sz.w, Math.max(m, cx - sz.w / 2)), y = Math.min($.H - m - sz.h, Math.max(m, cy - sz.h / 2));\n  return { markup: $.chip(txt, x, y, o), box: { x: x, y: y, w: sz.w, h: sz.h } };\n}\n/** Pulsing rings on a point (from at). */\nfunction pulse($, t, at, x, y, r, col) {\n  let s = \"\";\n  for (let i = 0; i < 2; i++) {\n    const q = ((t - at - i * 0.6) % 1.2) / 1.2;\n    if (t - at - i * 0.6 < 0) continue;\n    s += $.circle(x, y, r * (1 + q * 2.2), { stroke: col, sw: 4 * $.k, op: 0.8 * (1 - q) });\n  }\n  return s;\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const M = motion($, t);\n  const tin = $.beat(\"in\", 0.1), col = inkOf($, p.color);\n  let s = \"\";\n  const tx = W * p.x / 100, ty = H * p.y / 100;\n  const right = p.side === \"right\" || (p.side === \"auto\" && tx < W / 2);\n  const up = ty > H * 0.4, dir = right ? 1 : -1;\n  const tf = Object.assign({}, $.font(\"display\"), { size: 46 * k }), df = Object.assign({}, $.font(\"text\"), { size: 28 * k });\n  const title = $.case(p.title), maxW = Math.min(W * 0.4, 560 * k);\n  const dl = p.detail ? $.wrap(p.detail, maxW, df).slice(0, 3) : [];\n  const cw = Math.min(maxW, Math.max($.measure(title, tf), widest($, dl, df))) + 56 * k, ch = 40 * k + tf.size * 1.1 + (dl.length ? 14 * k + dl.length * df.size * 1.3 : 0) + 4 * k;\n  // An elbow: out at 45° for d1, then level to the card.\n  const d1 = Math.min(W, H) * 0.14, ex = tx + dir * d1, ey = ty + (up ? -d1 : d1);\n  let cx = right ? ex + Math.min(W, H) * 0.08 : ex - Math.min(W, H) * 0.08 - cw;\n  cx = Math.min(W - cw - 24 * k, Math.max(24 * k, cx));\n  const lineEnd = right ? cx : cx + cw, cy = Math.min(H - ch - 24 * k, Math.max(24 * k, ey - ch / 2));\n  const lp = $.E.inOut2($.seg(t, tin + 0.3, tin + 0.8));\n  s += M.part(\"dot\", { at: tin, in: \"pop\", box: { x: tx - 16 * k, y: ty - 16 * k, w: 32 * k, h: 32 * k } }, $.circle(tx, ty, 10 * k, { fill: col, stroke: \"#FFFFFF\", sw: 3 * k }) + pulse($, t, tin + 0.2, tx, ty, 12 * k, col));\n  if (lp > 0) s += M.part(\"line\", { at: tin + 0.3, in: \"none\" }, $.path(\"M\" + tx + \" \" + ty + \"L\" + ex + \" \" + ey + \"L\" + lineEnd + \" \" + ey, { stroke: col, sw: 4 * k, draw: lp, cap: \"round\" }));\n  let card = $.card(cx, cy, cw, ch, { t: t }) + $.rect(right ? cx : cx + cw - 8 * k, cy, 8 * k, ch, { fill: col });\n  card += $.text(title, Object.assign({}, tf, { x: cx + 28 * k, y: cy + 22 * k, fill: c.fg }));\n  dl.forEach(function (l, i) { card += $.text(l, Object.assign({}, df, { x: cx + 28 * k, y: cy + 22 * k + tf.size * 1.1 + 14 * k + i * df.size * 1.3, fill: c.mute })); });\n  s += M.part(\"card\", { at: tin + 0.75, in: \"slide\", from: right ? \"left\" : \"right\", distance: 20, box: { x: cx, y: cy, w: cw, h: ch } }, card);\n  return s;\n}\n","libs":[]},{"id":"emphasis","name":"Emphasis marks","description":"Comic emphasis round a point: action lines burst out and sparkles twinkle — for a punchline, a reveal or a wow.","category":"callout","tags":["annotation","emphasis","burst","sparkle","wow","lines","comic"],"placement":"overlay","duration":{"default":2.5,"min":1,"max":12},"aspects":["16:9","9:16","1:1","4:5"],"props":{"x":{"type":"number","label":"Centre across (%)","default":50,"min":0,"max":100,"step":1},"y":{"type":"number","label":"Centre down (%)","default":45,"min":0,"max":100,"step":1},"size":{"type":"number","label":"Size","default":100,"min":40,"max":260,"step":5},"kind":{"type":"choice","label":"Marks","options":["both","lines","sparkles"],"default":"both"},"color":{"type":"color","label":"Colour","default":"","description":"empty: the brand's colour"}},"parts":{"lines":"the action lines","sparkles":"the sparkles"},"beats":{"in":0.1},"style":"candy","pack":"annotate","ref":"annotate/emphasis","code":"// Annotations point at things in the footage: every place is a percentage of the frame (0–100).\nfunction spot($, x, y) { return [$.W * $.clamp(x / 100), $.H * $.clamp(y / 100)]; }\nfunction boxOf($, x, y, w, h) { return { x: $.W * x / 100, y: $.H * y / 100, w: $.W * w / 100, h: $.H * h / 100 }; }\n/** The annotation colour: the prop when given, else the accent. */\nfunction inkOf($, col) { return col || $.c.accent; }\n/** Which side of a point has the most room for a label. */\nfunction roomy($, x, y) {\n  const room = { left: x, right: $.W - x, above: y, below: $.H - y };\n  const wide = $.W >= $.H;\n  let best = wide ? (x < $.W / 2 ? \"right\" : \"left\") : (y < $.H / 2 ? \"below\" : \"above\");\n  if (room[best] < Math.min($.W, $.H) * 0.3) for (const s in room) if (room[s] > room[best]) best = s;\n  return best;\n}\n/** A label tag (the brand's chip) centred on (cx, cy), kept inside the frame: { markup, box }. */\nfunction tagAt($, str, cx, cy, fill, size) {\n  const txt = $.case(str), o = { size: (size || 40) * $.k, fill: fill, color: $.on(fill) };\n  const sz = $.chip.size(txt, o), m = 24 * $.k;\n  const x = Math.min($.W - m - sz.w, Math.max(m, cx - sz.w / 2)), y = Math.min($.H - m - sz.h, Math.max(m, cy - sz.h / 2));\n  return { markup: $.chip(txt, x, y, o), box: { x: x, y: y, w: sz.w, h: sz.h } };\n}\n/** Pulsing rings on a point (from at). */\nfunction pulse($, t, at, x, y, r, col) {\n  let s = \"\";\n  for (let i = 0; i < 2; i++) {\n    const q = ((t - at - i * 0.6) % 1.2) / 1.2;\n    if (t - at - i * 0.6 < 0) continue;\n    s += $.circle(x, y, r * (1 + q * 2.2), { stroke: col, sw: 4 * $.k, op: 0.8 * (1 - q) });\n  }\n  return s;\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const M = motion($, t);\n  const tin = $.beat(\"in\", 0.1), col = inkOf($, p.color);\n  let s = \"\";\n  const cx = W * p.x / 100, cy = H * p.y / 100, R = p.size * k * 1.6;\n  if (p.kind !== \"sparkles\") {\n    let lines = \"\";\n    for (let i = 0; i < 9; i++) {\n      const a = -Math.PI * 0.95 + (i / 8) * Math.PI * 0.9 + ($.hash(i * 2.1) - 0.5) * 0.12;\n      const r0 = R * (0.62 + 0.1 * $.hash(i * 5.5)), r1 = R * (0.95 + 0.25 * $.hash(i * 7.7));\n      const pr = $.E.out3($.seg(t, tin + i * 0.03, tin + i * 0.03 + 0.25));\n      const wob = 1 + 0.06 * Math.sin((t - tin) * 9 + i);\n      lines += $.line(cx + Math.cos(a) * r0 * wob, cy + Math.sin(a) * r0 * wob, cx + Math.cos(a) * r1 * wob, cy + Math.sin(a) * r1 * wob, { p: pr, stroke: col, sw: (7 + 5 * $.hash(i)) * k });\n    }\n    s += M.part(\"lines\", { at: tin, in: \"none\" }, lines);\n  }\n  if (p.kind !== \"lines\") {\n    let sp = \"\";\n    const spots = [[-1.05, -0.55, 1], [1.0, -0.75, 0.7], [0.8, 0.45, 0.55], [-0.75, 0.6, 0.45]];\n    spots.forEach(function (q, i) {\n      const a = tin + 0.15 + i * 0.12, sc = $.E.outBack($.clamp((t - a) / 0.3)) * (1 + 0.18 * Math.sin((t - a) * 7 + i * 2)) * q[2];\n      if (sc <= 0) return;\n      const x = cx + q[0] * R, y = cy + q[1] * R, r = 34 * k * sc, n = r * 0.22;\n      sp += $.path(\"M\" + x + \" \" + (y - r) + \"Q\" + (x + n) + \" \" + (y - n) + \" \" + (x + r) + \" \" + y + \"Q\" + (x + n) + \" \" + (y + n) + \" \" + x + \" \" + (y + r) + \"Q\" + (x - n) + \" \" + (y + n) + \" \" + (x - r) + \" \" + y + \"Q\" + (x - n) + \" \" + (y - n) + \" \" + x + \" \" + (y - r) + \"Z\", { fill: i % 2 ? c.accent2 : col });\n    });\n    s += M.part(\"sparkles\", { at: tin + 0.15, in: \"none\" }, sp);\n  }\n  return s;\n}\n\n/** A pop as the lines burst out, a shimmer as the sparkles turn up. */\nfunction sound($) {\n  const p = $.data, tin = $.beat(\"in\", 0.1), out = [];\n  if (p.kind !== \"sparkles\") out.push({ at: tin, kind: \"pop\", level: 0.9, pan: (p.x / 50 - 1) * 0.6 });\n  if (p.kind !== \"lines\") out.push({ at: tin + 0.15, kind: \"shimmer\", dur: 0.7, level: 0.6, pan: (p.x / 50 - 1) * 0.6 });\n  return out;\n}\n","libs":[]},{"id":"markers","name":"Numbered markers","description":"Numbered dots pop onto the frame one after another, each with a ring and an optional label — for walking through the parts of a shot.","category":"callout","tags":["annotation","markers","numbers","steps","points","parts","hotspots"],"placement":"overlay","duration":{"default":4,"min":1.5,"max":20},"aspects":["16:9","9:16","1:1","4:5"],"props":{"points":{"type":"list","label":"Points","of":"text","default":["22 38 Lens","52 62 Grip","78 30 Screen"],"maxItems":8,"description":"one per line: across% down% label"},"gap":{"type":"number","label":"Seconds apart","default":0.45,"min":0.1,"max":3,"step":0.05},"color":{"type":"color","label":"Colour","default":"","description":"empty: the brand's colour"}},"parts":{"markers":"the numbered dots","labels":"their labels"},"beats":{"in":0.1},"style":"candy","pack":"annotate","ref":"annotate/markers","code":"// Annotations point at things in the footage: every place is a percentage of the frame (0–100).\nfunction spot($, x, y) { return [$.W * $.clamp(x / 100), $.H * $.clamp(y / 100)]; }\nfunction boxOf($, x, y, w, h) { return { x: $.W * x / 100, y: $.H * y / 100, w: $.W * w / 100, h: $.H * h / 100 }; }\n/** The annotation colour: the prop when given, else the accent. */\nfunction inkOf($, col) { return col || $.c.accent; }\n/** Which side of a point has the most room for a label. */\nfunction roomy($, x, y) {\n  const room = { left: x, right: $.W - x, above: y, below: $.H - y };\n  const wide = $.W >= $.H;\n  let best = wide ? (x < $.W / 2 ? \"right\" : \"left\") : (y < $.H / 2 ? \"below\" : \"above\");\n  if (room[best] < Math.min($.W, $.H) * 0.3) for (const s in room) if (room[s] > room[best]) best = s;\n  return best;\n}\n/** A label tag (the brand's chip) centred on (cx, cy), kept inside the frame: { markup, box }. */\nfunction tagAt($, str, cx, cy, fill, size) {\n  const txt = $.case(str), o = { size: (size || 40) * $.k, fill: fill, color: $.on(fill) };\n  const sz = $.chip.size(txt, o), m = 24 * $.k;\n  const x = Math.min($.W - m - sz.w, Math.max(m, cx - sz.w / 2)), y = Math.min($.H - m - sz.h, Math.max(m, cy - sz.h / 2));\n  return { markup: $.chip(txt, x, y, o), box: { x: x, y: y, w: sz.w, h: sz.h } };\n}\n/** Pulsing rings on a point (from at). */\nfunction pulse($, t, at, x, y, r, col) {\n  let s = \"\";\n  for (let i = 0; i < 2; i++) {\n    const q = ((t - at - i * 0.6) % 1.2) / 1.2;\n    if (t - at - i * 0.6 < 0) continue;\n    s += $.circle(x, y, r * (1 + q * 2.2), { stroke: col, sw: 4 * $.k, op: 0.8 * (1 - q) });\n  }\n  return s;\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const M = motion($, t);\n  const tin = $.beat(\"in\", 0.1), col = inkOf($, p.color);\n  let s = \"\";\n  const pts = (p.points || []).map(function (row) {\n    const m = /^\\s*(-?[\\d.]+)[\\s,]+(-?[\\d.]+)\\s*(.*)$/.exec(String(row));\n    return m ? { x: W * $.clamp(Number(m[1]) / 100), y: H * $.clamp(Number(m[2]) / 100), label: m[3] } : null;\n  }).filter(Boolean);\n  const r = 34 * k, nf = Object.assign({}, $.font(\"display\"), { size: 36 * k });\n  let dots = \"\", labels = \"\";\n  pts.forEach(function (q, i) {\n    const a = tin + i * p.gap;\n    dots += M.part(\"markers\", { at: a, in: \"pop\", box: { x: q.x - r, y: q.y - r, w: 2 * r, h: 2 * r } },\n      $.circle(q.x, q.y, r, { fill: col, stroke: \"#FFFFFF\", sw: 4 * k, shadow: \"md\" }) + $.text(String(i + 1), Object.assign({}, nf, { x: q.x, y: q.y - nf.size * 0.56, anchor: \"middle\", fill: $.on(col) })) + pulse($, t, a + 0.2, q.x, q.y, r, col));\n    if (q.label) {\n      const tg = tagAt($, q.label, q.x + (q.x < W * 0.7 ? 1 : -1) * (r + 20 * k + $.chip.size($.case(q.label), { size: 32 * k }).w / 2), q.y, c.surface, 32);\n      labels += M.part(\"labels\", { at: a + 0.15, in: \"rise\", distance: 12, box: tg.box }, tg.markup);\n    }\n  });\n  return s + dots + labels;\n}\n","libs":[]},{"id":"bracket","name":"Bracket","description":"A curly bracket drawn along one side of a region from its middle out, its point reaching to a note — \"these three\", \"all of this\". Places in % of the frame.","category":"callout","tags":["bracket","brace","group","annotation","note"],"placement":"overlay","duration":{"default":3,"min":1.5,"max":12},"props":{"x":{"type":"number","label":"Box left (%)","default":34,"min":0,"max":100,"step":1},"y":{"type":"number","label":"Box top (%)","default":28,"min":0,"max":100,"step":1},"w":{"type":"number","label":"Box width (%)","default":26,"min":0,"max":100,"step":1},"h":{"type":"number","label":"Box height (%)","default":40,"min":0,"max":100,"step":1},"side":{"type":"choice","label":"Bracket side","options":["right","left","below","above"],"default":"right"},"label":{"type":"text","label":"Note","default":"These three","maxLength":28},"color":{"type":"color","label":"Colour","default":"","description":"empty: the brand's colour"}},"parts":{"bracket":"the bracket","label":"the note"},"beats":{"in":0.1},"style":"candy","pack":"annotate","ref":"annotate/bracket","code":"// Annotations point at things in the footage: every place is a percentage of the frame (0–100).\nfunction spot($, x, y) { return [$.W * $.clamp(x / 100), $.H * $.clamp(y / 100)]; }\nfunction boxOf($, x, y, w, h) { return { x: $.W * x / 100, y: $.H * y / 100, w: $.W * w / 100, h: $.H * h / 100 }; }\n/** The annotation colour: the prop when given, else the accent. */\nfunction inkOf($, col) { return col || $.c.accent; }\n/** Which side of a point has the most room for a label. */\nfunction roomy($, x, y) {\n  const room = { left: x, right: $.W - x, above: y, below: $.H - y };\n  const wide = $.W >= $.H;\n  let best = wide ? (x < $.W / 2 ? \"right\" : \"left\") : (y < $.H / 2 ? \"below\" : \"above\");\n  if (room[best] < Math.min($.W, $.H) * 0.3) for (const s in room) if (room[s] > room[best]) best = s;\n  return best;\n}\n/** A label tag (the brand's chip) centred on (cx, cy), kept inside the frame: { markup, box }. */\nfunction tagAt($, str, cx, cy, fill, size) {\n  const txt = $.case(str), o = { size: (size || 40) * $.k, fill: fill, color: $.on(fill) };\n  const sz = $.chip.size(txt, o), m = 24 * $.k;\n  const x = Math.min($.W - m - sz.w, Math.max(m, cx - sz.w / 2)), y = Math.min($.H - m - sz.h, Math.max(m, cy - sz.h / 2));\n  return { markup: $.chip(txt, x, y, o), box: { x: x, y: y, w: sz.w, h: sz.h } };\n}\n/** Pulsing rings on a point (from at). */\nfunction pulse($, t, at, x, y, r, col) {\n  let s = \"\";\n  for (let i = 0; i < 2; i++) {\n    const q = ((t - at - i * 0.6) % 1.2) / 1.2;\n    if (t - at - i * 0.6 < 0) continue;\n    s += $.circle(x, y, r * (1 + q * 2.2), { stroke: col, sw: 4 * $.k, op: 0.8 * (1 - q) });\n  }\n  return s;\n}\n\n// A curly bracket drawn along one side of a region, its point reaching out to a note: \"these three\", \"all of\n// this\". The bracket draws on from its middle, then the note pops beside the point.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const M = motion($, t);\n  const tin = $.beat(\"in\", 0.1), col = inkOf($, p.color);\n  const b = boxOf($, p.x, p.y, p.w, p.h);\n  const side = p.side || \"right\";\n  const vert = side === \"left\" || side === \"right\";\n  // The bracket's spine runs along the chosen side; its point sticks out at the middle, deeper on longer spans.\n  const L = vert ? b.h : b.w;\n  const gap = 24 * k, depth = Math.max(44 * k, Math.min(96 * k, L * 0.14));\n  const sx = side === \"right\" ? b.x + b.w + gap : side === \"left\" ? b.x - gap : b.x;\n  const sy = side === \"below\" ? b.y + b.h + gap : side === \"above\" ? b.y - gap : b.y;\n  const dir = side === \"right\" || side === \"below\" ? 1 : -1;\n  const q = function (u, d) { return vert ? [sx + d * dir, sy + u] : [sx + u, sy + d * dir]; };\n  const P = function (pt) { return $.fx(pt[0]) + \" \" + $.fx(pt[1]); };\n  const h = depth / 2;\n  // Two halves, each from the middle point out to an end, so it draws from the centre.\n  const half = function (s0) {\n    const m = L / 2, e = s0 < 0 ? 0 : L;\n    return \"M\" + P(q(m, depth)) + \"C\" + P(q(m, h)) + \" \" + P(q(m + s0 * h, h)) + \" \" + P(q(m + s0 * depth, h)) +\n      \"L\" + P(q(e - s0 * depth, h)) + \"C\" + P(q(e - s0 * h, h)) + \" \" + P(q(e, h)) + \" \" + P(q(e, 0));\n  };\n  const d = $.E.inOut2($.seg(t, tin, tin + 0.55));\n  let s = M.part(\"bracket\", { at: tin, in: \"none\", out: \"fade\" }, $.path(half(-1), { stroke: col, sw: 7 * k, draw: d }) + $.path(half(1), { stroke: col, sw: 7 * k, draw: d }));\n  if (p.label) {\n    const tip = q(L / 2, depth + 30 * k);\n    const tg = tagAt($, p.label, tip[0] + (vert ? dir * 120 * k : 0), tip[1] + (vert ? 0 : dir * 30 * k), col, 38);\n    s += M.part(\"label\", { at: tin + 0.45, in: \"pop\", out: \"fade\", box: tg.box }, tg.markup);\n  }\n  return s;\n}\n","libs":[]},{"id":"box-label","name":"Box with label","description":"A box drawn round something with its label on a tab hanging off the corner — \"the new menu\", \"fixed\". Hand-drawn or clean. Places in % of the frame.","category":"callout","tags":["box","label","frame","annotation","tag"],"placement":"overlay","duration":{"default":3,"min":1.5,"max":12},"props":{"x":{"type":"number","label":"Box left (%)","default":36,"min":0,"max":100,"step":1},"y":{"type":"number","label":"Box top (%)","default":34,"min":0,"max":100,"step":1},"w":{"type":"number","label":"Box width (%)","default":28,"min":0,"max":100,"step":1},"h":{"type":"number","label":"Box height (%)","default":26,"min":0,"max":100,"step":1},"label":{"type":"text","label":"Label","default":"The new menu","maxLength":28},"look":{"type":"choice","label":"Look","options":["marker","clean"],"default":"marker"},"color":{"type":"color","label":"Colour","default":"","description":"empty: the brand's colour"}},"parts":{"box":"the box","label":"the tab"},"beats":{"in":0.1},"style":"candy","pack":"annotate","ref":"annotate/box-label","code":"// Annotations point at things in the footage: every place is a percentage of the frame (0–100).\nfunction spot($, x, y) { return [$.W * $.clamp(x / 100), $.H * $.clamp(y / 100)]; }\nfunction boxOf($, x, y, w, h) { return { x: $.W * x / 100, y: $.H * y / 100, w: $.W * w / 100, h: $.H * h / 100 }; }\n/** The annotation colour: the prop when given, else the accent. */\nfunction inkOf($, col) { return col || $.c.accent; }\n/** Which side of a point has the most room for a label. */\nfunction roomy($, x, y) {\n  const room = { left: x, right: $.W - x, above: y, below: $.H - y };\n  const wide = $.W >= $.H;\n  let best = wide ? (x < $.W / 2 ? \"right\" : \"left\") : (y < $.H / 2 ? \"below\" : \"above\");\n  if (room[best] < Math.min($.W, $.H) * 0.3) for (const s in room) if (room[s] > room[best]) best = s;\n  return best;\n}\n/** A label tag (the brand's chip) centred on (cx, cy), kept inside the frame: { markup, box }. */\nfunction tagAt($, str, cx, cy, fill, size) {\n  const txt = $.case(str), o = { size: (size || 40) * $.k, fill: fill, color: $.on(fill) };\n  const sz = $.chip.size(txt, o), m = 24 * $.k;\n  const x = Math.min($.W - m - sz.w, Math.max(m, cx - sz.w / 2)), y = Math.min($.H - m - sz.h, Math.max(m, cy - sz.h / 2));\n  return { markup: $.chip(txt, x, y, o), box: { x: x, y: y, w: sz.w, h: sz.h } };\n}\n/** Pulsing rings on a point (from at). */\nfunction pulse($, t, at, x, y, r, col) {\n  let s = \"\";\n  for (let i = 0; i < 2; i++) {\n    const q = ((t - at - i * 0.6) % 1.2) / 1.2;\n    if (t - at - i * 0.6 < 0) continue;\n    s += $.circle(x, y, r * (1 + q * 2.2), { stroke: col, sw: 4 * $.k, op: 0.8 * (1 - q) });\n  }\n  return s;\n}\n\n// A hand-drawn box round something, with a label on a tab that hangs off its corner: \"the new menu\", \"fixed\".\n// The box inks on corner to corner, then the tab drops in.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const M = motion($, t);\n  const tin = $.beat(\"in\", 0.1), col = inkOf($, p.color);\n  const b = boxOf($, p.x, p.y, p.w, p.h);\n  const pad = 14 * k;\n  const box = p.look === \"clean\"\n    ? $.path(\"M\" + (b.x - pad) + \" \" + (b.y - pad) + \"H\" + (b.x + b.w + pad) + \"V\" + (b.y + b.h + pad) + \"H\" + (b.x - pad) + \"Z\", { stroke: col, sw: 6 * k, draw: $.E.inOut2($.seg(t, tin, tin + 0.6)) })\n    : $.ink(t, tin, $.hand.rect(b.x - pad, b.y - pad, b.w + pad * 2, b.h + pad * 2, { seed: 5, rough: 4 * k }), { color: col, sw: 8 * k, dur: 0.7 });\n  let s = M.part(\"box\", { at: tin, in: \"none\", out: \"fade\" }, box);\n  if (p.label) {\n    // The tab: on the top edge, flush with the box's left corner, unless that runs off the frame.\n    const lf = Object.assign({}, $.font(\"display\"), { size: 34 * k });\n    const txt = $.case(String(p.label)), tw = $.measure(txt, lf) + 40 * k, th = lf.size * 1.55;\n    const above = b.y - pad - th > 20 * k;\n    const tx = Math.min(W - tw - 20 * k, Math.max(20 * k, b.x - pad)), ty = above ? b.y - pad - th + 4 * k : b.y + b.h + pad - 4 * k;\n    const tab = $.rect(tx, ty, tw, th, { r: 10 * k, fill: col }) + $.text(txt, Object.assign({}, lf, { x: tx + 20 * k, y: ty + (th - lf.size) / 2 - 1 * k, fill: $.on(col) }));\n    s += M.part(\"label\", { at: tin + 0.55, in: \"drop\", distance: 24, ease: \"bouncy\", out: \"fade\", box: { x: tx, y: ty, w: tw, h: th } }, tab);\n  }\n  return s;\n}\n","libs":[]},{"id":"cursor","name":"Cursor click","description":"A cursor glides in along a gentle curve and clicks the spot — a press and a ripple — then a tooltip pops beside it. For walkthroughs over screen recordings.","category":"callout","tags":["cursor","click","pointer","walkthrough","tutorial"],"placement":"overlay","duration":{"default":3,"min":1.5,"max":12},"props":{"x":{"type":"number","label":"Click across (%)","default":58,"min":0,"max":100,"step":1},"y":{"type":"number","label":"Click down (%)","default":46,"min":0,"max":100,"step":1},"fromX":{"type":"number","label":"Start across (%)","default":86,"min":0,"max":100,"step":1},"fromY":{"type":"number","label":"Start down (%)","default":86,"min":0,"max":100,"step":1},"label":{"type":"text","label":"Tooltip","default":"Click Export","maxLength":28},"size":{"type":"choice","label":"Size","options":["normal","large"],"default":"normal"},"color":{"type":"color","label":"Colour","default":"","description":"empty: the brand's colour"}},"parts":{"cursor":"the cursor and ripple","label":"the tooltip"},"beats":{"in":0.1},"style":"candy","pack":"annotate","ref":"annotate/cursor","code":"// Annotations point at things in the footage: every place is a percentage of the frame (0–100).\nfunction spot($, x, y) { return [$.W * $.clamp(x / 100), $.H * $.clamp(y / 100)]; }\nfunction boxOf($, x, y, w, h) { return { x: $.W * x / 100, y: $.H * y / 100, w: $.W * w / 100, h: $.H * h / 100 }; }\n/** The annotation colour: the prop when given, else the accent. */\nfunction inkOf($, col) { return col || $.c.accent; }\n/** Which side of a point has the most room for a label. */\nfunction roomy($, x, y) {\n  const room = { left: x, right: $.W - x, above: y, below: $.H - y };\n  const wide = $.W >= $.H;\n  let best = wide ? (x < $.W / 2 ? \"right\" : \"left\") : (y < $.H / 2 ? \"below\" : \"above\");\n  if (room[best] < Math.min($.W, $.H) * 0.3) for (const s in room) if (room[s] > room[best]) best = s;\n  return best;\n}\n/** A label tag (the brand's chip) centred on (cx, cy), kept inside the frame: { markup, box }. */\nfunction tagAt($, str, cx, cy, fill, size) {\n  const txt = $.case(str), o = { size: (size || 40) * $.k, fill: fill, color: $.on(fill) };\n  const sz = $.chip.size(txt, o), m = 24 * $.k;\n  const x = Math.min($.W - m - sz.w, Math.max(m, cx - sz.w / 2)), y = Math.min($.H - m - sz.h, Math.max(m, cy - sz.h / 2));\n  return { markup: $.chip(txt, x, y, o), box: { x: x, y: y, w: sz.w, h: sz.h } };\n}\n/** Pulsing rings on a point (from at). */\nfunction pulse($, t, at, x, y, r, col) {\n  let s = \"\";\n  for (let i = 0; i < 2; i++) {\n    const q = ((t - at - i * 0.6) % 1.2) / 1.2;\n    if (t - at - i * 0.6 < 0) continue;\n    s += $.circle(x, y, r * (1 + q * 2.2), { stroke: col, sw: 4 * $.k, op: 0.8 * (1 - q) });\n  }\n  return s;\n}\n\n// A cursor glides in along a gentle curve, clicks the spot (a press and a ripple) and, if there is a label,\n// a tooltip pops beside it. For walkthroughs over screen recordings.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const M = motion($, t);\n  const tin = $.beat(\"in\", 0.1), col = inkOf($, p.color);\n  const to = spot($, p.x, p.y);\n  const from = spot($, p.fromX, p.fromY);\n  const arrive = tin + 0.9, click = arrive + 0.15;\n  // The path: a quadratic curve that bows to one side, eased in and out.\n  const u = $.E.inOut3($.seg(t, tin, arrive));\n  const mx = (from[0] + to[0]) / 2 + (to[1] - from[1]) * 0.25, my = (from[1] + to[1]) / 2 - (to[0] - from[0]) * 0.25;\n  const x = (1 - u) * (1 - u) * from[0] + 2 * (1 - u) * u * mx + u * u * to[0];\n  const y = (1 - u) * (1 - u) * from[1] + 2 * (1 - u) * u * my + u * u * to[1];\n  let s = \"\";\n  // The ripple where it clicks.\n  if (t >= click) for (let i = 0; i < 2; i++) {\n    const q = $.clamp((t - click - i * 0.18) / 0.6);\n    if (q > 0 && q < 1) s += $.circle(to[0], to[1], (12 + 70 * q) * k, { stroke: col, sw: 5 * k * (1 - q), op: 1 - q });\n  }\n  if (t >= click) s += $.circle(to[0], to[1], 14 * k, { fill: col, op: 0.35 * (1 - $.clamp((t - click) / 0.5)) });\n  // The pointer itself: squashes a little on the press.\n  const press = 1 - 0.14 * $.bump(t, click, 0.06, 0.18);\n  const sz = 56 * k * (p.size === \"large\" ? 1.4 : 1);\n  const arrow = \"M0 0 L0 \" + $.fx(sz) + \" L\" + $.fx(sz * 0.27) + \" \" + $.fx(sz * 0.76) + \" L\" + $.fx(sz * 0.47) + \" \" + $.fx(sz * 1.18) + \" L\" + $.fx(sz * 0.62) + \" \" + $.fx(sz * 1.1) + \" L\" + $.fx(sz * 0.43) + \" \" + $.fx(sz * 0.7) + \" L\" + $.fx(sz * 0.76) + \" \" + $.fx(sz * 0.7) + \" Z\";\n  // Its shadow is an offset, translucent copy (a filter shadow on a path draws a box round it).\n  s += $.g({ transform: \"translate(\" + $.fx(x) + \" \" + $.fx(y) + \") scale(\" + $.fx(press) + \")\" }, $.g({ transform: \"translate(\" + $.fx(3 * k) + \" \" + $.fx(5 * k) + \")\" }, $.path(arrow, { fill: \"#000000\", op: 0.22 })) + $.path(arrow, { fill: c.ink, stroke: \"#FFFFFF\", sw: 3.5 * k }));\n  let out = M.part(\"cursor\", { at: tin, in: \"fade\", dur: 0.2, out: \"fade\" }, s);\n  if (p.label) {\n    const tg = tagAt($, p.label, to[0] + 40 * k + $.measure(String(p.label), { size: 36 * k, weight: 700 }) / 2, to[1] + sz * 1.6, col, 36);\n    out += M.part(\"label\", { at: click + 0.15, in: \"pop\", out: \"fade\", box: tg.box }, tg.markup);\n  }\n  return out;\n}\n","libs":[]},{"id":"measure","name":"Dimension line","description":"A dimension line between two points, like a technical drawing: end ticks and arrowheads, the line drawing out from its middle and the measurement on a pill at the centre.","category":"callout","tags":["measure","dimension","size","distance","spec"],"placement":"overlay","duration":{"default":3,"min":1.5,"max":12},"props":{"x1":{"type":"number","label":"Start across (%)","default":30,"min":0,"max":100,"step":1},"y1":{"type":"number","label":"Start down (%)","default":62,"min":0,"max":100,"step":1},"x2":{"type":"number","label":"End across (%)","default":70,"min":0,"max":100,"step":1},"y2":{"type":"number","label":"End down (%)","default":62,"min":0,"max":100,"step":1},"label":{"type":"text","label":"Measurement","default":"146.7 mm","maxLength":20},"dashed":{"type":"boolean","label":"Dashed","default":false},"color":{"type":"color","label":"Colour","default":"","description":"empty: the brand's colour"}},"parts":{"line":"the line, ticks and arrowheads","label":"the measurement"},"beats":{"in":0.1},"style":"candy","pack":"annotate","ref":"annotate/measure","code":"// Annotations point at things in the footage: every place is a percentage of the frame (0–100).\nfunction spot($, x, y) { return [$.W * $.clamp(x / 100), $.H * $.clamp(y / 100)]; }\nfunction boxOf($, x, y, w, h) { return { x: $.W * x / 100, y: $.H * y / 100, w: $.W * w / 100, h: $.H * h / 100 }; }\n/** The annotation colour: the prop when given, else the accent. */\nfunction inkOf($, col) { return col || $.c.accent; }\n/** Which side of a point has the most room for a label. */\nfunction roomy($, x, y) {\n  const room = { left: x, right: $.W - x, above: y, below: $.H - y };\n  const wide = $.W >= $.H;\n  let best = wide ? (x < $.W / 2 ? \"right\" : \"left\") : (y < $.H / 2 ? \"below\" : \"above\");\n  if (room[best] < Math.min($.W, $.H) * 0.3) for (const s in room) if (room[s] > room[best]) best = s;\n  return best;\n}\n/** A label tag (the brand's chip) centred on (cx, cy), kept inside the frame: { markup, box }. */\nfunction tagAt($, str, cx, cy, fill, size) {\n  const txt = $.case(str), o = { size: (size || 40) * $.k, fill: fill, color: $.on(fill) };\n  const sz = $.chip.size(txt, o), m = 24 * $.k;\n  const x = Math.min($.W - m - sz.w, Math.max(m, cx - sz.w / 2)), y = Math.min($.H - m - sz.h, Math.max(m, cy - sz.h / 2));\n  return { markup: $.chip(txt, x, y, o), box: { x: x, y: y, w: sz.w, h: sz.h } };\n}\n/** Pulsing rings on a point (from at). */\nfunction pulse($, t, at, x, y, r, col) {\n  let s = \"\";\n  for (let i = 0; i < 2; i++) {\n    const q = ((t - at - i * 0.6) % 1.2) / 1.2;\n    if (t - at - i * 0.6 < 0) continue;\n    s += $.circle(x, y, r * (1 + q * 2.2), { stroke: col, sw: 4 * $.k, op: 0.8 * (1 - q) });\n  }\n  return s;\n}\n\n// A dimension line between two points, like a technical drawing: end ticks, arrowheads, the line drawing out\n// from the middle and the measurement on a pill at its centre.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const M = motion($, t);\n  const tin = $.beat(\"in\", 0.1), col = inkOf($, p.color);\n  const a = spot($, p.x1, p.y1), b = spot($, p.x2, p.y2);\n  const dx = b[0] - a[0], dy = b[1] - a[1], len = Math.max(1, Math.hypot(dx, dy));\n  const ux = dx / len, uy = dy / len, nx = -uy, ny = ux;\n  const mid = [(a[0] + b[0]) / 2, (a[1] + b[1]) / 2];\n  // The line grows out from the middle to both ends.\n  const g = $.E.inOut2($.seg(t, tin, tin + 0.6));\n  const ea = [mid[0] - ux * len / 2 * g, mid[1] - uy * len / 2 * g], eb = [mid[0] + ux * len / 2 * g, mid[1] + uy * len / 2 * g];\n  const sw = 7 * k, tick = 30 * k, head = 28 * k;\n  let s = $.line(ea[0], ea[1], eb[0], eb[1], { stroke: col, sw: sw, dash: p.dashed ? 14 * k + \" \" + 10 * k : null });\n  const end = function (pt, dir, show) {\n    if (!show) return \"\";\n    const hx = pt[0], hy = pt[1];\n    return $.line(hx + nx * tick, hy + ny * tick, hx - nx * tick, hy - ny * tick, { stroke: col, sw: sw }) +\n      $.path(\"M\" + $.fx(hx) + \" \" + $.fx(hy) + \"L\" + $.fx(hx - dir * ux * head + nx * head * 0.5) + \" \" + $.fx(hy - dir * uy * head + ny * head * 0.5) + \"L\" + $.fx(hx - dir * ux * head - nx * head * 0.5) + \" \" + $.fx(hy - dir * uy * head - ny * head * 0.5) + \"Z\", { fill: col });\n  };\n  s += end(ea, -1, g > 0.02) + end(eb, 1, g > 0.02);\n  let out = M.part(\"line\", { at: tin, in: \"none\", out: \"fade\" }, s);\n  if (p.label) {\n    const lf = Object.assign({}, $.font(\"mono\"), { size: 40 * k, weight: 700 });\n    const txt = String(p.label), tw = $.measure(txt, lf) + 36 * k, th = lf.size * 1.6;\n    const pill = $.rect(mid[0] - tw / 2, mid[1] - th / 2, tw, th, { r: th / 2, fill: col }) + $.text(txt, Object.assign({}, lf, { x: mid[0], y: mid[1] - lf.size * 0.56, anchor: \"middle\", fill: $.on(col) }));\n    out += M.part(\"label\", { at: tin + 0.4, in: \"pop\", out: \"fade\", box: { x: mid[0] - tw / 2, y: mid[1] - th / 2, w: tw, h: th } }, pill);\n  }\n  return out;\n}\n","libs":[]},{"id":"check","name":"Tick or cross","description":"A verdict mark: a big hand-drawn tick or cross inked onto the spot in green or red over a soft disc, with an optional word under it — \"Do this\", \"Not this\".","category":"callout","tags":["check","tick","cross","right","wrong"],"placement":"overlay","duration":{"default":2.5,"min":1,"max":12},"props":{"x":{"type":"number","label":"Across (%)","default":50,"min":0,"max":100,"step":1},"y":{"type":"number","label":"Down (%)","default":45,"min":0,"max":100,"step":1},"mark":{"type":"choice","label":"Mark","options":["tick","cross"],"default":"tick"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"label":{"type":"text","label":"Word","default":"Do this","maxLength":20},"color":{"type":"color","label":"Colour","default":"","description":"empty: the brand's colour"}},"parts":{"mark":"the mark","label":"the word"},"beats":{"in":0.1},"style":"candy","pack":"annotate","ref":"annotate/check","code":"// Annotations point at things in the footage: every place is a percentage of the frame (0–100).\nfunction spot($, x, y) { return [$.W * $.clamp(x / 100), $.H * $.clamp(y / 100)]; }\nfunction boxOf($, x, y, w, h) { return { x: $.W * x / 100, y: $.H * y / 100, w: $.W * w / 100, h: $.H * h / 100 }; }\n/** The annotation colour: the prop when given, else the accent. */\nfunction inkOf($, col) { return col || $.c.accent; }\n/** Which side of a point has the most room for a label. */\nfunction roomy($, x, y) {\n  const room = { left: x, right: $.W - x, above: y, below: $.H - y };\n  const wide = $.W >= $.H;\n  let best = wide ? (x < $.W / 2 ? \"right\" : \"left\") : (y < $.H / 2 ? \"below\" : \"above\");\n  if (room[best] < Math.min($.W, $.H) * 0.3) for (const s in room) if (room[s] > room[best]) best = s;\n  return best;\n}\n/** A label tag (the brand's chip) centred on (cx, cy), kept inside the frame: { markup, box }. */\nfunction tagAt($, str, cx, cy, fill, size) {\n  const txt = $.case(str), o = { size: (size || 40) * $.k, fill: fill, color: $.on(fill) };\n  const sz = $.chip.size(txt, o), m = 24 * $.k;\n  const x = Math.min($.W - m - sz.w, Math.max(m, cx - sz.w / 2)), y = Math.min($.H - m - sz.h, Math.max(m, cy - sz.h / 2));\n  return { markup: $.chip(txt, x, y, o), box: { x: x, y: y, w: sz.w, h: sz.h } };\n}\n/** Pulsing rings on a point (from at). */\nfunction pulse($, t, at, x, y, r, col) {\n  let s = \"\";\n  for (let i = 0; i < 2; i++) {\n    const q = ((t - at - i * 0.6) % 1.2) / 1.2;\n    if (t - at - i * 0.6 < 0) continue;\n    s += $.circle(x, y, r * (1 + q * 2.2), { stroke: col, sw: 4 * $.k, op: 0.8 * (1 - q) });\n  }\n  return s;\n}\n\n// A verdict mark: a big hand-drawn tick (or cross) inked onto the spot in green (or red), with an optional\n// word beside it — \"Do this\", \"Not this\". Put one on each side of a comparison.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const M = motion($, t);\n  const tin = $.beat(\"in\", 0.1);\n  const good = p.mark !== \"cross\";\n  const col = p.color || (good ? c.green : c.red);\n  const at = spot($, p.x, p.y);\n  const sz = (p.size === \"large\" ? 220 : p.size === \"small\" ? 110 : 160) * k;\n  // A soft disc behind the mark, then the mark inked on.\n  const disc = $.circle(at[0], at[1], sz * 0.72 * Math.max(0, $.pop(t, tin, 2.6, 6.5)), { fill: $.rgba(col, 0.16) });\n  const shape = good ? $.hand.check(at[0] - sz / 2, at[1] - sz / 2, sz, { seed: 4, rough: 3 * k }) : $.hand.cross(at[0], at[1], sz * 0.7, { seed: 6, rough: 3 * k });\n  let s = M.part(\"mark\", { at: tin, in: \"none\", out: \"fade\" }, disc + $.ink(t, tin + 0.1, shape, { color: col, sw: 16 * k, dur: good ? 0.4 : 0.5 }));\n  if (p.label) {\n    const tg = tagAt($, p.label, at[0], at[1] + sz * 0.72 + 40 * k, col, 40);\n    s += M.part(\"label\", { at: tin + 0.5, in: \"pop\", out: \"fade\", box: tg.box }, tg.markup);\n  }\n  return s;\n}\n","libs":[]}]},{"format":1,"id":"cursors","name":"MONARC Cursors","version":"1.0.0","description":"Cursors that act out a product: an AI agent working through steps, several people's cursors at once, a drag and drop, a text selection and a right-click menu — over your screen recordings, in the brand's colours.","author":"MONARC STUDIOS","license":"MONARC Packs License 1.0 (free to use in any video; not for resale as templates)","homepage":"https://volito.dev/docs/packs/cursors","style":"studio","shared":"shared.js","items":["agent-cursor","multiplayer","drag-drop","text-select","context-menu"],"entries":[{"id":"agent-cursor","name":"Agent cursor","description":"An AI agent working a UI: its cursor glides to each target while its name tag types out the step, clicks with a ring, moves on, and ends with a check and the done line. Places in % of the frame.","category":"callout","tags":["agent","ai","cursor","automation","walkthrough"],"placement":"overlay","duration":{"default":6,"min":2,"max":30},"props":{"name":{"type":"text","label":"Agent name","default":"Agent","maxLength":20},"steps":{"type":"list","label":"Steps (Step: across %, down %)","default":[{"step":"Opening the timeline","x":30,"y":38},{"step":"Selecting the interview","x":55,"y":56},{"step":"Trimming the pauses","x":72,"y":40},{"step":"Exporting in 4K","x":82,"y":72}],"maxItems":8,"minItems":1,"of":{"step":"text","x":"number","y":"number"}},"done":{"type":"text","label":"Done line","default":"Done","maxLength":20},"color":{"type":"color","label":"Colour","default":"","description":"empty: the brand's colour"}},"parts":{"cursor":"the cursor, tag and clicks"},"beats":{"in":0.15,"out":-0.45},"style":"studio","pack":"cursors","ref":"cursors/agent-cursor","code":"// MONARC Cursors — helpers this pack's items share. Places are percentages of the frame (0–100).\n\n/** A point on the frame from percentages. */\nfunction at$($, x, y) { return [($.W * Math.max(0, Math.min(100, Number(x) || 0))) / 100, ($.H * Math.max(0, Math.min(100, Number(y) || 0))) / 100]; }\n\n/** The pointer: an arrow with its tip at (x, y), filled, outlined, squashed a little while pressed (0–1). */\nfunction pointer($, x, y, size, o) {\n  o = o || {};\n  const sz = size * (1 - 0.12 * (o.press || 0));\n  const d = \"M0 0 L0 \" + $.fx(sz) + \" L\" + $.fx(sz * 0.27) + \" \" + $.fx(sz * 0.76) + \" L\" + $.fx(sz * 0.47) + \" \" + $.fx(sz * 1.18) + \" L\" + $.fx(sz * 0.62) + \" \" + $.fx(sz * 1.1) + \" L\" + $.fx(sz * 0.43) + \" \" + $.fx(sz * 0.7) + \" L\" + $.fx(sz * 0.76) + \" \" + $.fx(sz * 0.7) + \" Z\";\n  // A soft shadow as an offset, translucent copy (a filter shadow on a path draws a box round it).\n  return '<g transform=\"translate(' + $.fx(x) + \" \" + $.fx(y) + ')\">' + '<g transform=\"translate(' + $.fx(size * 0.05) + \" \" + $.fx(size * 0.08) + ')\">' + $.path(d, { fill: \"#000000\", op: 0.22 }) + \"</g>\" + $.path(d, { fill: o.fill || $.c.ink, stroke: o.stroke || \"#FFFFFF\", sw: size * 0.06 }) + \"</g>\";\n}\n\n/** A name tag beside a pointer: a pill in the colour with a white dot (or a drawn tick), the name, and optional text after it. */\nfunction nameTag($, x, y, name, col, size, rest, tick) {\n  const nf = { size: size, weight: 700 }, rf = { size: size, weight: 500 };\n  const nw = $.measure(name, nf), rw = rest ? $.measure(rest, rf) : 0;\n  const padX = size * 0.65, dot = size * 0.32, h = size * 1.75;\n  const w = padX + dot * 2 + size * 0.45 + nw + (rest ? size * 0.5 + rw : 0) + padX;\n  let m = $.rect(x, y, w, h, { r: h / 2, fill: col, shadow: \"sm\" });\n  if (tick) m += $.path(\"M\" + $.fx(x + padX) + \" \" + $.fx(y + h / 2) + \"L\" + $.fx(x + padX + dot * 0.75) + \" \" + $.fx(y + h / 2 + dot * 0.7) + \"L\" + $.fx(x + padX + dot * 2) + \" \" + $.fx(y + h / 2 - dot * 0.75), { stroke: \"#FFFFFF\", sw: size * 0.16 });\n  else m += $.circle(x + padX + dot, y + h / 2, dot, { fill: \"#FFFFFF\" });\n  const tx = x + padX + dot * 2 + size * 0.45, ink = $.on(col);\n  m += $.text(name, Object.assign({}, nf, { x: tx, y: y + (h - size) / 2 - size * 0.05, fill: ink }));\n  if (rest) m += $.text(rest, Object.assign({}, rf, { x: tx + nw + size * 0.5, y: y + (h - size) / 2 - size * 0.05, fill: ink, op: 0.88 }));\n  return { m: m, w: w, h: h };\n}\n\n/** A click: two rings spreading out from (x, y) from `at`. */\nfunction clickRings($, t, at, x, y, col, r) {\n  let s = \"\";\n  for (let i = 0; i < 2; i++) {\n    const q = $.clamp((t - at - i * 0.16) / 0.55);\n    if (q > 0 && q < 1) s += $.circle(x, y, r * (0.3 + 1.6 * q), { stroke: col, sw: r * 0.12 * (1 - q), op: 1 - q });\n  }\n  return s;\n}\n\n/** Eased travel from a to b over [t0, t1], bowing sideways by `bend` of the distance. */\nfunction glide($, t, t0, t1, a, b, bend) {\n  const u = $.E.inOut3($.seg(t, t0, t1));\n  const mx = (a[0] + b[0]) / 2 + (b[1] - a[1]) * (bend || 0.2), my = (a[1] + b[1]) / 2 - (b[0] - a[0]) * (bend || 0.2);\n  return [(1 - u) * (1 - u) * a[0] + 2 * (1 - u) * u * mx + u * u * b[0], (1 - u) * (1 - u) * a[1] + 2 * (1 - u) * u * my + u * u * b[1]];\n}\n\n/** A colour per person, from the brand's accents and fixed hues. */\nfunction personColor($, i) { return [$.c.accent, \"#7C5CFF\", \"#22C55E\", \"#F59E0B\", \"#EC4899\", \"#06B6D4\"][i % 6]; }\n\n// An AI agent working a UI: its cursor glides to each target while its name tag types out the step, clicks with a\n// ring, moves on to the next, and ends with a check and the done line.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const M = motion($, t);\n  const tin = $.beat(\"in\", 0.1);\n  const col = p.color || c.accent;\n  const steps = (p.steps || []).slice(0, 8).map(function (s) { return { text: String(s.step || \"\"), pt: at$($, s.x, s.y) }; });\n  if (!steps.length) return \"\";\n  const end = $.beat(\"out\", $.dur - 0.45) - 0.7;\n  const slot = (end - tin) / steps.length;\n  const size = 30 * k, ps = 50 * k;\n  // Where it is: gliding into step i during the first 45% of its slot, then still while it clicks.\n  let pos = at$($, 92, 92), cur = -1, clickAt = 0;\n  steps.forEach(function (st, i) {\n    const t0 = tin + i * slot, t1 = t0 + slot * 0.45;\n    if (t >= t0) { cur = i; pos = glide($, t, t0, t1, i ? steps[i - 1].pt : at$($, 92, 92), st.pt, 0.18); clickAt = t1 + 0.05; }\n  });\n  let s = \"\";\n  // Clicks left behind: a soft dot at every target clicked so far.\n  steps.forEach(function (st, i) { if (i < cur || (i === cur && t > clickAt)) s += $.circle(st.pt[0], st.pt[1], 9 * k, { fill: col, op: 0.35 }); });\n  if (cur >= 0) s += clickRings($, t, clickAt, steps[cur].pt[0], steps[cur].pt[1], col, 46 * k);\n  const press = cur >= 0 ? $.bump(t, clickAt, 0.06, 0.16) : 0;\n  s += pointer($, pos[0], pos[1], ps, { fill: col, press: press });\n  // The tag: the agent's name and the step typing out; at the end, a check and the done line.\n  const done = t > end;\n  // (The tick is drawn: not every face has a check mark glyph.)\n  const rest = done ? String(p.done || \"Done\") : cur >= 0 ? $.typed(t, tin + cur * slot, steps[cur].text, 28) : \"\";\n  const tag = nameTag($, 0, 0, String(p.name || \"Agent\"), done ? c.green : col, size, rest, done);\n  const tx = Math.min(W - tag.w - 16 * k, pos[0] + ps * 0.55), ty = Math.min(H - tag.h - 16 * k, pos[1] + ps * 1.05);\n  s += '<g transform=\"translate(' + $.fx(tx) + \" \" + $.fx(ty) + ')\">' + tag.m + \"</g>\";\n  return M.part(\"cursor\", { at: tin, in: \"fade\", dur: 0.25, out: \"fade\" }, s);\n}\n","libs":[]},{"id":"multiplayer","name":"Multiplayer cursors","description":"Several people's cursors at once, each with a name tag in its own colour, drifting over the frame on their own paths and clicking now and then — a live, shared document.","category":"callout","tags":["multiplayer","collaboration","cursors","team","live"],"placement":"overlay","duration":{"default":6,"min":2,"max":60},"props":{"names":{"type":"list","label":"People","default":["Ana","Kenji","Lea","Sam"],"maxItems":6,"minItems":1,"of":"text"},"speed":{"type":"number","label":"Speed","default":1,"min":0.2,"max":3,"step":0.1}},"parts":{"cursors":"the cursors and tags"},"beats":{"in":0.15,"out":-0.45},"style":"studio","pack":"cursors","ref":"cursors/multiplayer","code":"// MONARC Cursors — helpers this pack's items share. Places are percentages of the frame (0–100).\n\n/** A point on the frame from percentages. */\nfunction at$($, x, y) { return [($.W * Math.max(0, Math.min(100, Number(x) || 0))) / 100, ($.H * Math.max(0, Math.min(100, Number(y) || 0))) / 100]; }\n\n/** The pointer: an arrow with its tip at (x, y), filled, outlined, squashed a little while pressed (0–1). */\nfunction pointer($, x, y, size, o) {\n  o = o || {};\n  const sz = size * (1 - 0.12 * (o.press || 0));\n  const d = \"M0 0 L0 \" + $.fx(sz) + \" L\" + $.fx(sz * 0.27) + \" \" + $.fx(sz * 0.76) + \" L\" + $.fx(sz * 0.47) + \" \" + $.fx(sz * 1.18) + \" L\" + $.fx(sz * 0.62) + \" \" + $.fx(sz * 1.1) + \" L\" + $.fx(sz * 0.43) + \" \" + $.fx(sz * 0.7) + \" L\" + $.fx(sz * 0.76) + \" \" + $.fx(sz * 0.7) + \" Z\";\n  // A soft shadow as an offset, translucent copy (a filter shadow on a path draws a box round it).\n  return '<g transform=\"translate(' + $.fx(x) + \" \" + $.fx(y) + ')\">' + '<g transform=\"translate(' + $.fx(size * 0.05) + \" \" + $.fx(size * 0.08) + ')\">' + $.path(d, { fill: \"#000000\", op: 0.22 }) + \"</g>\" + $.path(d, { fill: o.fill || $.c.ink, stroke: o.stroke || \"#FFFFFF\", sw: size * 0.06 }) + \"</g>\";\n}\n\n/** A name tag beside a pointer: a pill in the colour with a white dot (or a drawn tick), the name, and optional text after it. */\nfunction nameTag($, x, y, name, col, size, rest, tick) {\n  const nf = { size: size, weight: 700 }, rf = { size: size, weight: 500 };\n  const nw = $.measure(name, nf), rw = rest ? $.measure(rest, rf) : 0;\n  const padX = size * 0.65, dot = size * 0.32, h = size * 1.75;\n  const w = padX + dot * 2 + size * 0.45 + nw + (rest ? size * 0.5 + rw : 0) + padX;\n  let m = $.rect(x, y, w, h, { r: h / 2, fill: col, shadow: \"sm\" });\n  if (tick) m += $.path(\"M\" + $.fx(x + padX) + \" \" + $.fx(y + h / 2) + \"L\" + $.fx(x + padX + dot * 0.75) + \" \" + $.fx(y + h / 2 + dot * 0.7) + \"L\" + $.fx(x + padX + dot * 2) + \" \" + $.fx(y + h / 2 - dot * 0.75), { stroke: \"#FFFFFF\", sw: size * 0.16 });\n  else m += $.circle(x + padX + dot, y + h / 2, dot, { fill: \"#FFFFFF\" });\n  const tx = x + padX + dot * 2 + size * 0.45, ink = $.on(col);\n  m += $.text(name, Object.assign({}, nf, { x: tx, y: y + (h - size) / 2 - size * 0.05, fill: ink }));\n  if (rest) m += $.text(rest, Object.assign({}, rf, { x: tx + nw + size * 0.5, y: y + (h - size) / 2 - size * 0.05, fill: ink, op: 0.88 }));\n  return { m: m, w: w, h: h };\n}\n\n/** A click: two rings spreading out from (x, y) from `at`. */\nfunction clickRings($, t, at, x, y, col, r) {\n  let s = \"\";\n  for (let i = 0; i < 2; i++) {\n    const q = $.clamp((t - at - i * 0.16) / 0.55);\n    if (q > 0 && q < 1) s += $.circle(x, y, r * (0.3 + 1.6 * q), { stroke: col, sw: r * 0.12 * (1 - q), op: 1 - q });\n  }\n  return s;\n}\n\n/** Eased travel from a to b over [t0, t1], bowing sideways by `bend` of the distance. */\nfunction glide($, t, t0, t1, a, b, bend) {\n  const u = $.E.inOut3($.seg(t, t0, t1));\n  const mx = (a[0] + b[0]) / 2 + (b[1] - a[1]) * (bend || 0.2), my = (a[1] + b[1]) / 2 - (b[0] - a[0]) * (bend || 0.2);\n  return [(1 - u) * (1 - u) * a[0] + 2 * (1 - u) * u * mx + u * u * b[0], (1 - u) * (1 - u) * a[1] + 2 * (1 - u) * u * my + u * u * b[1]];\n}\n\n/** A colour per person, from the brand's accents and fixed hues. */\nfunction personColor($, i) { return [$.c.accent, \"#7C5CFF\", \"#22C55E\", \"#F59E0B\", \"#EC4899\", \"#06B6D4\"][i % 6]; }\n\n// Several people's cursors at once, each with its name tag in its own colour, drifting over the frame on their own\n// paths and clicking now and then — a live, shared document.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const M = motion($, t);\n  const tin = $.beat(\"in\", 0.1);\n  const names = (p.names || []).map(String).filter(Boolean).slice(0, 6);\n  const ps = 46 * k, size = 26 * k;\n  const sp = Number(p.speed) || 1;\n  let s = \"\";\n  names.forEach(function (nm, i) {\n    const col = personColor($, i);\n    // A slow Lissajous wander, different for each person, kept inside the frame.\n    const ph = $.hash(i * 7.3) * 6.28, fx = 0.13 + 0.07 * $.hash(i * 3.1), fy = 0.17 + 0.06 * $.hash(i * 5.9);\n    const x = W * (0.5 + 0.34 * Math.sin((t - tin) * fx * sp * 2.2 + ph) * Math.cos((t - tin) * 0.07 * sp + i));\n    const y = H * (0.48 + 0.3 * Math.sin((t - tin) * fy * sp * 2.6 + ph * 1.7));\n    const appear = $.clamp((t - tin - i * 0.25) / 0.3);\n    if (appear <= 0) return;\n    // A click every few seconds, at a different time for each.\n    const period = 2.6 + i * 0.4, last = tin + 0.8 + i * 0.5 + Math.floor(Math.max(0, t - tin - 0.8 - i * 0.5) / period) * period;\n    let m = clickRings($, t, last, x, y, col, 36 * k);\n    m += pointer($, x, y, ps, { fill: col, press: $.bump(t, last, 0.05, 0.14) });\n    const tag = nameTag($, 0, 0, nm, col, size);\n    m += '<g transform=\"translate(' + $.fx(Math.min(W - tag.w - 10 * k, x + ps * 0.55)) + \" \" + $.fx(Math.min(H - tag.h - 10 * k, y + ps * 1.05)) + ')\">' + tag.m + \"</g>\";\n    s += $.g({ op: appear }, m);\n  });\n  return M.part(\"cursors\", { at: tin, in: \"none\", out: \"fade\" }, s);\n}\n","libs":[]},{"id":"drag-drop","name":"Drag and drop","description":"The cursor picks up a file card — it lifts, with a deeper shadow — carries it along a curve to a dashed drop zone that lights up as it arrives, lets go, and the zone confirms.","category":"callout","tags":["drag and drop","upload","file","cursor","ui"],"placement":"overlay","duration":{"default":5,"min":3,"max":20},"props":{"file":{"type":"text","label":"File","default":"interview_final.mp4","maxLength":40},"size":{"type":"text","label":"Size","default":"2.4 GB · 4K","maxLength":24},"zone":{"type":"text","label":"Zone","default":"Drop to upload","maxLength":30},"done":{"type":"text","label":"Done","default":"Uploaded","maxLength":24},"fromX":{"type":"number","label":"Card across (%)","default":28,"min":0,"max":100,"step":1},"fromY":{"type":"number","label":"Card down (%)","default":50,"min":0,"max":100,"step":1},"toX":{"type":"number","label":"Zone across (%)","default":70,"min":0,"max":100,"step":1},"toY":{"type":"number","label":"Zone down (%)","default":50,"min":0,"max":100,"step":1}},"parts":{"zone":"the drop zone","card":"the file card","cursor":"the cursor"},"beats":{"in":0.15,"out":-0.45},"style":"studio","pack":"cursors","ref":"cursors/drag-drop","code":"// MONARC Cursors — helpers this pack's items share. Places are percentages of the frame (0–100).\n\n/** A point on the frame from percentages. */\nfunction at$($, x, y) { return [($.W * Math.max(0, Math.min(100, Number(x) || 0))) / 100, ($.H * Math.max(0, Math.min(100, Number(y) || 0))) / 100]; }\n\n/** The pointer: an arrow with its tip at (x, y), filled, outlined, squashed a little while pressed (0–1). */\nfunction pointer($, x, y, size, o) {\n  o = o || {};\n  const sz = size * (1 - 0.12 * (o.press || 0));\n  const d = \"M0 0 L0 \" + $.fx(sz) + \" L\" + $.fx(sz * 0.27) + \" \" + $.fx(sz * 0.76) + \" L\" + $.fx(sz * 0.47) + \" \" + $.fx(sz * 1.18) + \" L\" + $.fx(sz * 0.62) + \" \" + $.fx(sz * 1.1) + \" L\" + $.fx(sz * 0.43) + \" \" + $.fx(sz * 0.7) + \" L\" + $.fx(sz * 0.76) + \" \" + $.fx(sz * 0.7) + \" Z\";\n  // A soft shadow as an offset, translucent copy (a filter shadow on a path draws a box round it).\n  return '<g transform=\"translate(' + $.fx(x) + \" \" + $.fx(y) + ')\">' + '<g transform=\"translate(' + $.fx(size * 0.05) + \" \" + $.fx(size * 0.08) + ')\">' + $.path(d, { fill: \"#000000\", op: 0.22 }) + \"</g>\" + $.path(d, { fill: o.fill || $.c.ink, stroke: o.stroke || \"#FFFFFF\", sw: size * 0.06 }) + \"</g>\";\n}\n\n/** A name tag beside a pointer: a pill in the colour with a white dot (or a drawn tick), the name, and optional text after it. */\nfunction nameTag($, x, y, name, col, size, rest, tick) {\n  const nf = { size: size, weight: 700 }, rf = { size: size, weight: 500 };\n  const nw = $.measure(name, nf), rw = rest ? $.measure(rest, rf) : 0;\n  const padX = size * 0.65, dot = size * 0.32, h = size * 1.75;\n  const w = padX + dot * 2 + size * 0.45 + nw + (rest ? size * 0.5 + rw : 0) + padX;\n  let m = $.rect(x, y, w, h, { r: h / 2, fill: col, shadow: \"sm\" });\n  if (tick) m += $.path(\"M\" + $.fx(x + padX) + \" \" + $.fx(y + h / 2) + \"L\" + $.fx(x + padX + dot * 0.75) + \" \" + $.fx(y + h / 2 + dot * 0.7) + \"L\" + $.fx(x + padX + dot * 2) + \" \" + $.fx(y + h / 2 - dot * 0.75), { stroke: \"#FFFFFF\", sw: size * 0.16 });\n  else m += $.circle(x + padX + dot, y + h / 2, dot, { fill: \"#FFFFFF\" });\n  const tx = x + padX + dot * 2 + size * 0.45, ink = $.on(col);\n  m += $.text(name, Object.assign({}, nf, { x: tx, y: y + (h - size) / 2 - size * 0.05, fill: ink }));\n  if (rest) m += $.text(rest, Object.assign({}, rf, { x: tx + nw + size * 0.5, y: y + (h - size) / 2 - size * 0.05, fill: ink, op: 0.88 }));\n  return { m: m, w: w, h: h };\n}\n\n/** A click: two rings spreading out from (x, y) from `at`. */\nfunction clickRings($, t, at, x, y, col, r) {\n  let s = \"\";\n  for (let i = 0; i < 2; i++) {\n    const q = $.clamp((t - at - i * 0.16) / 0.55);\n    if (q > 0 && q < 1) s += $.circle(x, y, r * (0.3 + 1.6 * q), { stroke: col, sw: r * 0.12 * (1 - q), op: 1 - q });\n  }\n  return s;\n}\n\n/** Eased travel from a to b over [t0, t1], bowing sideways by `bend` of the distance. */\nfunction glide($, t, t0, t1, a, b, bend) {\n  const u = $.E.inOut3($.seg(t, t0, t1));\n  const mx = (a[0] + b[0]) / 2 + (b[1] - a[1]) * (bend || 0.2), my = (a[1] + b[1]) / 2 - (b[0] - a[0]) * (bend || 0.2);\n  return [(1 - u) * (1 - u) * a[0] + 2 * (1 - u) * u * mx + u * u * b[0], (1 - u) * (1 - u) * a[1] + 2 * (1 - u) * u * my + u * u * b[1]];\n}\n\n/** A colour per person, from the brand's accents and fixed hues. */\nfunction personColor($, i) { return [$.c.accent, \"#7C5CFF\", \"#22C55E\", \"#F59E0B\", \"#EC4899\", \"#06B6D4\"][i % 6]; }\n\n// Drag and drop: the cursor picks up a file card (it lifts, with a deeper shadow), carries it along a curve to a\n// dashed drop zone that lights up as it arrives, lets go — the card settles in and the zone confirms.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const M = motion($, t);\n  const tin = $.beat(\"in\", 0.1);\n  const from = at$($, p.fromX, p.fromY), to = at$($, p.toX, p.toY);\n  const cw = 440 * k, ch = 118 * k, ps = 50 * k;\n  // The zone: a dashed panel round the target.\n  const zw = cw * 1.35, zh = ch * 2.1, zx = to[0] - zw / 2, zy = to[1] - zh / 2;\n  const grab = tin + 0.9, drop = grab + 1.3;\n  const near = $.clamp((t - (drop - 0.5)) / 0.4), landed = t > drop;\n  let zone = $.rect(zx, zy, zw, zh, { r: 24 * k, fill: landed ? $.rgba(c.green, 0.12) : $.rgba(c.accent, 0.06 + 0.12 * near), stroke: landed ? c.green : near > 0 ? c.accent : c.line, sw: 3 * k, dash: 14 * k + \" \" + 10 * k });\n  const zf = { size: 30 * k, weight: 600 };\n  const zText = landed ? String(p.done || \"Uploaded\") : String(p.zone || \"Drop to upload\");\n  zone += $.icon(landed ? \"check\" : \"upload\", to[0] - 22 * k, zy + zh - 112 * k, 44 * k, { color: landed ? c.green : c.mute, sw: 2.4 });\n  zone += $.text(zText, Object.assign({}, zf, { x: to[0], y: zy + zh - 58 * k, anchor: \"middle\", fill: landed ? c.green : c.mute }));\n  let s = M.part(\"zone\", { at: tin, in: \"fade\", dur: 0.4, out: \"fade\" }, zone);\n  // The card: at rest at `from`, then carried; it settles into the top of the zone when dropped.\n  const cardAtPos = t < grab ? from : t < drop ? glide($, t, grab + 0.1, drop - 0.05, from, [to[0], zy + ch * 0.62], -0.22) : [to[0], zy + ch * 0.62 + (1 - $.E.outBack($.seg(t, drop, drop + 0.3))) * -10 * k];\n  const lifted = t >= grab && t < drop ? $.E.out3($.seg(t, grab, grab + 0.2)) : t >= drop ? 1 - $.E.out3($.seg(t, drop, drop + 0.25)) : 0;\n  const sc = 1 + 0.05 * lifted, tilt = -3 * lifted;\n  let card = $.rect(-cw / 2, -ch / 2, cw, ch, { r: 18 * k, fill: c.surface, stroke: c.line, sw: 1.5 * k, shadow: lifted > 0.3 ? \"lg\" : \"md\" });\n  card += $.rect(-cw / 2 + 22 * k, -ch / 2 + 22 * k, ch - 44 * k, ch - 44 * k, { r: 12 * k, fill: c.accent });\n  card += $.icon(\"video\", -cw / 2 + 22 * k + (ch - 44 * k) * 0.22, -ch / 2 + 22 * k + (ch - 44 * k) * 0.22, (ch - 44 * k) * 0.56, { color: $.on(c.accent), sw: 2.4 });\n  const nf = fitFont($, String(p.file || \"\"), \"text\", 30 * k, cw - ch - 30 * k, { weight: 700 });\n  // The card is drawn round its own centre and moved into place, so its text is placed relative to it.\n  const local = { within: { x: -1e5, y: -1e5, w: 2e5, h: 2e5 } };\n  card += $.text(String(p.file || \"\"), Object.assign({}, nf, { x: -cw / 2 + ch, y: -ch / 2 + 26 * k, fill: c.fg }, local));\n  card += $.text(String(p.size || \"\"), Object.assign({ size: 24 * k, weight: 500, x: -cw / 2 + ch, y: -ch / 2 + 26 * k + nf.size * 1.4, fill: c.mute }, local));\n  s += M.part(\"card\", { at: tin + 0.1, in: \"pop\", out: \"fade\", box: { x: from[0] - cw / 2, y: from[1] - ch / 2, w: cw, h: ch } }, $.g({ transform: \"translate(\" + $.fx(cardAtPos[0]) + \" \" + $.fx(cardAtPos[1]) + \") rotate(\" + $.fx(tilt) + \") scale(\" + $.fx(sc) + \")\" }, card));\n  // The cursor: in to the card, holds it while carrying, then away a little after the drop.\n  const grip = [cw * 0.18, ch * 0.05];\n  const start = at$($, 50, 96);\n  let cur;\n  if (t < grab) cur = glide($, t, tin + 0.2, grab - 0.05, start, [from[0] + grip[0], from[1] + grip[1]], 0.15);\n  else if (t < drop + 0.1) cur = [cardAtPos[0] + grip[0], cardAtPos[1] + grip[1]];\n  else cur = glide($, t, drop + 0.25, drop + 0.9, [cardAtPos[0] + grip[0], cardAtPos[1] + grip[1]], [cardAtPos[0] + cw * 0.5, cardAtPos[1] + ch * 1.4], 0.1);\n  const holding = t >= grab && t < drop ? 1 : 0;\n  s += M.part(\"cursor\", { at: tin + 0.2, in: \"fade\", dur: 0.2, out: \"fade\" }, clickRings($, t, grab, cur[0], cur[1], c.accent, 34 * k) + pointer($, cur[0], cur[1], ps, { press: holding * 0.8 + $.bump(t, grab, 0.05, 0.12) }));\n  return s;\n}\n","libs":[]},{"id":"text-select","name":"Text select","description":"Selecting text: a paragraph on a card, an I-beam drags across a phrase as the highlight grows word by word, lets go, and a small toolbar pops over the selection with one action pressed.","category":"callout","tags":["select","text","highlight","toolbar","docs"],"placement":"overlay","duration":{"default":5,"min":3,"max":20},"props":{"text":{"type":"longtext","label":"Paragraph","default":"The best edits feel invisible. Every cut lands on purpose, every caption arrives on the word, and the story never waits for the tools.","maxLength":260},"select":{"type":"text","label":"Phrase to select","default":"every caption arrives on the word","maxLength":80},"actions":{"type":"list","label":"Toolbar","default":["Copy","Highlight","Comment"],"maxItems":4,"minItems":1,"of":"text"},"press":{"type":"number","label":"Pressed button","default":2,"min":1,"max":4}},"parts":{"card":"the paragraph card","toolbar":"the toolbar","cursor":"the cursor"},"beats":{"in":0.15,"out":-0.45},"style":"studio","pack":"cursors","ref":"cursors/text-select","code":"// MONARC Cursors — helpers this pack's items share. Places are percentages of the frame (0–100).\n\n/** A point on the frame from percentages. */\nfunction at$($, x, y) { return [($.W * Math.max(0, Math.min(100, Number(x) || 0))) / 100, ($.H * Math.max(0, Math.min(100, Number(y) || 0))) / 100]; }\n\n/** The pointer: an arrow with its tip at (x, y), filled, outlined, squashed a little while pressed (0–1). */\nfunction pointer($, x, y, size, o) {\n  o = o || {};\n  const sz = size * (1 - 0.12 * (o.press || 0));\n  const d = \"M0 0 L0 \" + $.fx(sz) + \" L\" + $.fx(sz * 0.27) + \" \" + $.fx(sz * 0.76) + \" L\" + $.fx(sz * 0.47) + \" \" + $.fx(sz * 1.18) + \" L\" + $.fx(sz * 0.62) + \" \" + $.fx(sz * 1.1) + \" L\" + $.fx(sz * 0.43) + \" \" + $.fx(sz * 0.7) + \" L\" + $.fx(sz * 0.76) + \" \" + $.fx(sz * 0.7) + \" Z\";\n  // A soft shadow as an offset, translucent copy (a filter shadow on a path draws a box round it).\n  return '<g transform=\"translate(' + $.fx(x) + \" \" + $.fx(y) + ')\">' + '<g transform=\"translate(' + $.fx(size * 0.05) + \" \" + $.fx(size * 0.08) + ')\">' + $.path(d, { fill: \"#000000\", op: 0.22 }) + \"</g>\" + $.path(d, { fill: o.fill || $.c.ink, stroke: o.stroke || \"#FFFFFF\", sw: size * 0.06 }) + \"</g>\";\n}\n\n/** A name tag beside a pointer: a pill in the colour with a white dot (or a drawn tick), the name, and optional text after it. */\nfunction nameTag($, x, y, name, col, size, rest, tick) {\n  const nf = { size: size, weight: 700 }, rf = { size: size, weight: 500 };\n  const nw = $.measure(name, nf), rw = rest ? $.measure(rest, rf) : 0;\n  const padX = size * 0.65, dot = size * 0.32, h = size * 1.75;\n  const w = padX + dot * 2 + size * 0.45 + nw + (rest ? size * 0.5 + rw : 0) + padX;\n  let m = $.rect(x, y, w, h, { r: h / 2, fill: col, shadow: \"sm\" });\n  if (tick) m += $.path(\"M\" + $.fx(x + padX) + \" \" + $.fx(y + h / 2) + \"L\" + $.fx(x + padX + dot * 0.75) + \" \" + $.fx(y + h / 2 + dot * 0.7) + \"L\" + $.fx(x + padX + dot * 2) + \" \" + $.fx(y + h / 2 - dot * 0.75), { stroke: \"#FFFFFF\", sw: size * 0.16 });\n  else m += $.circle(x + padX + dot, y + h / 2, dot, { fill: \"#FFFFFF\" });\n  const tx = x + padX + dot * 2 + size * 0.45, ink = $.on(col);\n  m += $.text(name, Object.assign({}, nf, { x: tx, y: y + (h - size) / 2 - size * 0.05, fill: ink }));\n  if (rest) m += $.text(rest, Object.assign({}, rf, { x: tx + nw + size * 0.5, y: y + (h - size) / 2 - size * 0.05, fill: ink, op: 0.88 }));\n  return { m: m, w: w, h: h };\n}\n\n/** A click: two rings spreading out from (x, y) from `at`. */\nfunction clickRings($, t, at, x, y, col, r) {\n  let s = \"\";\n  for (let i = 0; i < 2; i++) {\n    const q = $.clamp((t - at - i * 0.16) / 0.55);\n    if (q > 0 && q < 1) s += $.circle(x, y, r * (0.3 + 1.6 * q), { stroke: col, sw: r * 0.12 * (1 - q), op: 1 - q });\n  }\n  return s;\n}\n\n/** Eased travel from a to b over [t0, t1], bowing sideways by `bend` of the distance. */\nfunction glide($, t, t0, t1, a, b, bend) {\n  const u = $.E.inOut3($.seg(t, t0, t1));\n  const mx = (a[0] + b[0]) / 2 + (b[1] - a[1]) * (bend || 0.2), my = (a[1] + b[1]) / 2 - (b[0] - a[0]) * (bend || 0.2);\n  return [(1 - u) * (1 - u) * a[0] + 2 * (1 - u) * u * mx + u * u * b[0], (1 - u) * (1 - u) * a[1] + 2 * (1 - u) * u * my + u * u * b[1]];\n}\n\n/** A colour per person, from the brand's accents and fixed hues. */\nfunction personColor($, i) { return [$.c.accent, \"#7C5CFF\", \"#22C55E\", \"#F59E0B\", \"#EC4899\", \"#06B6D4\"][i % 6]; }\n\n// Selecting text: a paragraph on a card, the cursor (an I-beam) drags across a phrase as the highlight grows word\n// by word, lets go, and a small toolbar pops over the selection with one action pressed.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const tin = $.beat(\"in\", 0.1);\n  const text = String(p.text || \"\");\n  const pick = String(p.select || \"\").trim();\n  const cw = Math.min(A.w, (A.wide ? 1150 : 940) * k), pad = 48 * k;\n  const f = { size: (A.tall ? 46 : 40) * k, weight: 500, lh: 1.5 };\n  const lines = $.wrap(text, cw - pad * 2, f);\n  const lh = f.size * 1.5, ch = pad * 2 + lines.length * lh;\n  const cx = W / 2 - cw / 2, cy = A.y + (A.h - ch) / 2 + 40 * k;\n  // Lay the words out so the selection can be measured word by word.\n  const words = [];\n  lines.forEach(function (line, li) {\n    let x = cx + pad;\n    line.split(\" \").forEach(function (w) { const ww = $.measure(w, f); words.push({ w: w, x: x, y: cy + pad + li * lh, ww: ww, line: li }); x += ww + $.measure(\" \", f); });\n  });\n  // Which words the phrase covers (the first place it appears).\n  const all = words.map(function (w) { return w.w; });\n  const pw = pick.split(/\\s+/).filter(Boolean);\n  let i0 = -1;\n  for (let i = 0; i + pw.length <= all.length && pw.length; i++) if (pw.every(function (q, j) { return all[i + j].replace(/[.,!?;:]/g, \"\") === q.replace(/[.,!?;:]/g, \"\"); })) { i0 = i; break; }\n  if (i0 < 0) { i0 = Math.min(2, Math.max(0, all.length - 1)); pw.length = Math.min(3, all.length - i0); }\n  const i1 = i0 + Math.max(1, pw.length) - 1;\n  const t0 = tin + 0.9, t1 = t0 + 0.25 * (i1 - i0 + 1) + 0.3;\n  const prog = $.E.inOut2($.seg(t, t0, t1));\n  const reach = i0 + (i1 - i0 + 1) * prog;\n  let card = $.rect(cx, cy, cw, ch, { r: 22 * k, fill: c.surface, stroke: c.line, sw: 1.5 * k, shadow: \"lg\" });\n  // The highlight under the selected words so far.\n  words.forEach(function (w, i) {\n    if (i < i0 || i >= reach) return;\n    const part = Math.min(1, reach - i);\n    const next = words[i + 1];\n    const extra = i < i1 && next && next.line === w.line ? next.x - (w.x + w.ww) : 0;\n    card += $.rect(w.x - 3 * k, w.y - f.size * 0.08, (w.ww + extra) * part + 6 * k, f.size * 1.25, { r: 5 * k, fill: $.rgba(c.accent, 0.3) });\n  });\n  words.forEach(function (w) { card += $.text(w.w, Object.assign({}, f, { x: w.x, y: w.y, fill: c.fg })); });\n  let s = M.part(\"card\", { at: tin, in: \"rise\", distance: 30, out: \"fade\", box: { x: cx, y: cy, w: cw, h: ch } }, card);\n  // The I-beam: in from below to the start, across with the selection, then up to the toolbar.\n  const ws = words[i0], we = words[i1];\n  const startPt = [ws.x, ws.y + f.size * 0.55], endPt = [we.x + we.ww, we.y + f.size * 0.55];\n  let pos;\n  if (t < t0) pos = glide($, t, tin + 0.2, t0 - 0.05, [W * 0.7, H * 0.95], startPt, 0.12);\n  else if (t < t1) { const wi = Math.min(i1, Math.floor(reach - 0.001)), wd = words[Math.max(i0, wi)], part = Math.min(1, reach - wi); pos = [wd.x + wd.ww * part, wd.y + f.size * 0.55]; }\n  else pos = endPt;\n  // The toolbar over the selection once it is made.\n  const actions = (p.actions || [\"Copy\", \"Highlight\", \"Comment\"]).map(String).slice(0, 4);\n  const pressI = Math.max(0, Math.min(actions.length - 1, Math.round(p.press) - 1));\n  if (t > t1 + 0.1) {\n    const bf = { size: 26 * k, weight: 600 };\n    const bw = actions.map(function (a) { return $.measure(a, bf) + 36 * k; });\n    const tw = bw.reduce(function (a, b) { return a + b; }, 0) + 12 * k, th = 60 * k;\n    // Above the card, centred over the selection, so it never covers a line of text.\n    const tx = Math.max(A.x, Math.min(A.x + A.w - tw, (ws.x + endPt[0]) / 2 - tw / 2)), ty = cy - th - 16 * k;\n    let tb = $.rect(tx, ty, tw, th, { r: 14 * k, fill: c.ink, shadow: \"md\" });\n    let x = tx + 6 * k;\n    const pressAt = t1 + 0.9;\n    actions.forEach(function (a, i) {\n      const lit = i === pressI && t > pressAt;\n      if (lit) tb += $.rect(x, ty + 6 * k, bw[i], th - 12 * k, { r: 10 * k, fill: c.accent });\n      tb += $.text(a, Object.assign({}, bf, { x: x + bw[i] / 2, y: ty + (th - bf.size) / 2 - 1 * k, anchor: \"middle\", fill: lit ? $.on(c.accent) : \"#FFFFFF\" }));\n      if (i === pressI) pos = glide($, t, t1 + 0.3, pressAt - 0.05, endPt, [x + bw[i] / 2, ty + th / 2], 0.1);\n      x += bw[i];\n    });\n    s += M.part(\"toolbar\", { at: t1 + 0.1, in: \"pop\", out: \"fade\", box: { x: tx, y: ty, w: tw, h: th } }, tb);\n    s += clickRings($, t, pressAt, pos[0], pos[1], c.accent, 30 * k);\n  }\n  // An I-beam over text, the arrow once it heads to the toolbar.\n  const beam = t < t1 + 0.3\n    ? $.path(\"M\" + $.fx(pos[0] - 8 * k) + \" \" + $.fx(pos[1] - f.size * 0.7) + \"H\" + $.fx(pos[0] + 8 * k) + \"M\" + $.fx(pos[0]) + \" \" + $.fx(pos[1] - f.size * 0.7) + \"V\" + $.fx(pos[1] + f.size * 0.7) + \"M\" + $.fx(pos[0] - 8 * k) + \" \" + $.fx(pos[1] + f.size * 0.7) + \"H\" + $.fx(pos[0] + 8 * k), { stroke: c.ink, sw: 4 * k })\n    : pointer($, pos[0], pos[1], 46 * k, {});\n  return s + M.part(\"cursor\", { at: tin + 0.2, in: \"fade\", dur: 0.2, out: \"fade\" }, beam);\n}\n","libs":[]},{"id":"context-menu","name":"Context menu","description":"A right-click: the cursor arrives and clicks, a menu opens beside it with items and shortcuts, the cursor moves to one item — it lights up — and clicks it, and the menu closes. Places in % of the frame.","category":"callout","tags":["context menu","right click","menu","cursor","ui"],"placement":"overlay","duration":{"default":4,"min":2,"max":20},"props":{"x":{"type":"number","label":"Click across (%)","default":40,"min":0,"max":100,"step":1},"y":{"type":"number","label":"Click down (%)","default":34,"min":0,"max":100,"step":1},"items":{"type":"list","label":"Items (Label: shortcut)","default":[{"label":"Cut","keys":"Cmd X"},{"label":"Copy","keys":"Cmd C"},{"label":"Duplicate","keys":"Cmd D"},{"label":"Split at playhead","keys":"S"},{"label":"Delete","keys":"Del"}],"maxItems":8,"minItems":1,"of":{"label":"text","keys":"text"}},"pick":{"type":"number","label":"Chosen item","default":4,"min":1,"max":8}},"parts":{"menu":"the cursor and menu"},"beats":{"in":0.15,"out":-0.45},"style":"studio","pack":"cursors","ref":"cursors/context-menu","code":"// MONARC Cursors — helpers this pack's items share. Places are percentages of the frame (0–100).\n\n/** A point on the frame from percentages. */\nfunction at$($, x, y) { return [($.W * Math.max(0, Math.min(100, Number(x) || 0))) / 100, ($.H * Math.max(0, Math.min(100, Number(y) || 0))) / 100]; }\n\n/** The pointer: an arrow with its tip at (x, y), filled, outlined, squashed a little while pressed (0–1). */\nfunction pointer($, x, y, size, o) {\n  o = o || {};\n  const sz = size * (1 - 0.12 * (o.press || 0));\n  const d = \"M0 0 L0 \" + $.fx(sz) + \" L\" + $.fx(sz * 0.27) + \" \" + $.fx(sz * 0.76) + \" L\" + $.fx(sz * 0.47) + \" \" + $.fx(sz * 1.18) + \" L\" + $.fx(sz * 0.62) + \" \" + $.fx(sz * 1.1) + \" L\" + $.fx(sz * 0.43) + \" \" + $.fx(sz * 0.7) + \" L\" + $.fx(sz * 0.76) + \" \" + $.fx(sz * 0.7) + \" Z\";\n  // A soft shadow as an offset, translucent copy (a filter shadow on a path draws a box round it).\n  return '<g transform=\"translate(' + $.fx(x) + \" \" + $.fx(y) + ')\">' + '<g transform=\"translate(' + $.fx(size * 0.05) + \" \" + $.fx(size * 0.08) + ')\">' + $.path(d, { fill: \"#000000\", op: 0.22 }) + \"</g>\" + $.path(d, { fill: o.fill || $.c.ink, stroke: o.stroke || \"#FFFFFF\", sw: size * 0.06 }) + \"</g>\";\n}\n\n/** A name tag beside a pointer: a pill in the colour with a white dot (or a drawn tick), the name, and optional text after it. */\nfunction nameTag($, x, y, name, col, size, rest, tick) {\n  const nf = { size: size, weight: 700 }, rf = { size: size, weight: 500 };\n  const nw = $.measure(name, nf), rw = rest ? $.measure(rest, rf) : 0;\n  const padX = size * 0.65, dot = size * 0.32, h = size * 1.75;\n  const w = padX + dot * 2 + size * 0.45 + nw + (rest ? size * 0.5 + rw : 0) + padX;\n  let m = $.rect(x, y, w, h, { r: h / 2, fill: col, shadow: \"sm\" });\n  if (tick) m += $.path(\"M\" + $.fx(x + padX) + \" \" + $.fx(y + h / 2) + \"L\" + $.fx(x + padX + dot * 0.75) + \" \" + $.fx(y + h / 2 + dot * 0.7) + \"L\" + $.fx(x + padX + dot * 2) + \" \" + $.fx(y + h / 2 - dot * 0.75), { stroke: \"#FFFFFF\", sw: size * 0.16 });\n  else m += $.circle(x + padX + dot, y + h / 2, dot, { fill: \"#FFFFFF\" });\n  const tx = x + padX + dot * 2 + size * 0.45, ink = $.on(col);\n  m += $.text(name, Object.assign({}, nf, { x: tx, y: y + (h - size) / 2 - size * 0.05, fill: ink }));\n  if (rest) m += $.text(rest, Object.assign({}, rf, { x: tx + nw + size * 0.5, y: y + (h - size) / 2 - size * 0.05, fill: ink, op: 0.88 }));\n  return { m: m, w: w, h: h };\n}\n\n/** A click: two rings spreading out from (x, y) from `at`. */\nfunction clickRings($, t, at, x, y, col, r) {\n  let s = \"\";\n  for (let i = 0; i < 2; i++) {\n    const q = $.clamp((t - at - i * 0.16) / 0.55);\n    if (q > 0 && q < 1) s += $.circle(x, y, r * (0.3 + 1.6 * q), { stroke: col, sw: r * 0.12 * (1 - q), op: 1 - q });\n  }\n  return s;\n}\n\n/** Eased travel from a to b over [t0, t1], bowing sideways by `bend` of the distance. */\nfunction glide($, t, t0, t1, a, b, bend) {\n  const u = $.E.inOut3($.seg(t, t0, t1));\n  const mx = (a[0] + b[0]) / 2 + (b[1] - a[1]) * (bend || 0.2), my = (a[1] + b[1]) / 2 - (b[0] - a[0]) * (bend || 0.2);\n  return [(1 - u) * (1 - u) * a[0] + 2 * (1 - u) * u * mx + u * u * b[0], (1 - u) * (1 - u) * a[1] + 2 * (1 - u) * u * my + u * u * b[1]];\n}\n\n/** A colour per person, from the brand's accents and fixed hues. */\nfunction personColor($, i) { return [$.c.accent, \"#7C5CFF\", \"#22C55E\", \"#F59E0B\", \"#EC4899\", \"#06B6D4\"][i % 6]; }\n\n// A right-click: the cursor arrives, clicks, a menu opens beside it (items, shortcuts, a divider), the cursor moves\n// down to one item — it lights up — and clicks it; the menu closes.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const M = motion($, t);\n  const tin = $.beat(\"in\", 0.1);\n  const pt = at$($, p.x, p.y);\n  const items = (p.items || []).slice(0, 8).map(function (r) { return typeof r === \"string\" ? { label: r, keys: \"\" } : { label: String(r.label || \"\"), keys: String(r.keys || \"\") }; });\n  const pick = Math.max(0, Math.min(items.length - 1, Math.round(p.pick) - 1));\n  const rc = tin + 0.9, choose = rc + 1.1, close = choose + 0.35;\n  const ps = 48 * k, f = { size: 28 * k, weight: 500 }, kf = Object.assign({}, $.font(\"mono\"), { size: 22 * k });\n  const rowH = 58 * k, padY = 10 * k;\n  const mw = Math.max.apply(null, items.map(function (r) { return $.measure(r.label, f) + (r.keys ? $.measure(r.keys, kf) + 60 * k : 0); }).concat([200 * k])) + 70 * k;\n  const mh = padY * 2 + items.length * rowH;\n  // The menu opens below-right of the click, flipping to stay on the frame.\n  const mx = pt[0] + mw + 20 * k > W ? pt[0] - mw : pt[0] + 6 * k, my = pt[1] + mh + 20 * k > H ? pt[1] - mh : pt[1] + 6 * k;\n  let s = \"\";\n  const open = t > rc && t < close + 0.2;\n  if (open) {\n    const pop = $.E.outBack($.seg(t, rc, rc + 0.22)), fade = 1 - $.seg(t, close, close + 0.2);\n    let m = $.rect(mx, my, mw, mh, { r: 16 * k, fill: c.surface, stroke: c.line, sw: 1.5 * k, shadow: \"lg\" });\n    const hover = t > choose - 0.45;\n    items.forEach(function (r, i) {\n      const y = my + padY + i * rowH;\n      const lit = hover && i === pick;\n      if (lit) m += $.rect(mx + 8 * k, y + 4 * k, mw - 16 * k, rowH - 8 * k, { r: 10 * k, fill: t > choose ? c.accent : $.rgba(c.accent, 0.16) });\n      m += $.text(r.label, Object.assign({}, f, { x: mx + 28 * k, y: y + (rowH - f.size) / 2 - 1 * k, fill: lit && t > choose ? $.on(c.accent) : /delete|remove/i.test(r.label) ? c.red : c.fg }));\n      if (r.keys) m += $.text(r.keys, Object.assign({}, kf, { x: mx + mw - 28 * k, y: y + (rowH - kf.size) / 2, anchor: \"end\", fill: lit && t > choose ? $.on(c.accent) : c.mute }));\n      if (i < items.length - 1 && /copy|paste/i.test(r.label) && !/copy|paste/i.test(items[i + 1].label)) m += $.line(mx + 16 * k, y + rowH, mx + mw - 16 * k, y + rowH, { stroke: c.line, sw: 1.5 * k });\n    });\n    s += $.g({ op: Math.max(0, fade), transform: $.tf({ s: 0.85 + 0.15 * pop, ox: pt[0], oy: pt[1] }) }, m);\n  }\n  // The cursor: in to the spot, right-clicks, moves down to the item, clicks it.\n  const itemPt = [mx + Math.min(mw * 0.45, 160 * k), my + padY + pick * rowH + rowH / 2];\n  let pos;\n  if (t < rc) pos = glide($, t, tin + 0.1, rc - 0.05, at$($, Number(p.x) + 22, 96), pt, 0.15);\n  else if (t < choose - 0.45) pos = pt;\n  else pos = glide($, t, choose - 0.45, choose - 0.05, pt, itemPt, 0.05);\n  s += clickRings($, t, rc, pt[0], pt[1], c.accent, 34 * k) + clickRings($, t, choose, itemPt[0], itemPt[1], c.accent, 30 * k);\n  s += pointer($, pos[0], pos[1], ps, { press: $.bump(t, rc, 0.05, 0.14) + $.bump(t, choose, 0.05, 0.14) });\n  return M.part(\"menu\", { at: tin, in: \"fade\", dur: 0.25, out: \"fade\" }, s);\n}\n","libs":[]}]},{"format":1,"id":"type","name":"MONARC Type","version":"1.0.0","description":"Kinetic typography on the brand's ground: typewriter, word swap, split-flap board, slot reel, tile flip, marquee bands, echo stack, liquid fill, outline draw, weight flex, shimmer, decode, word by word, key phrase, a streaming AI answer and an editorial stack.","author":"MONARC STUDIOS","license":"MONARC Packs License 1.0 (free to use in any video; not for resale as templates)","homepage":"https://volito.dev/docs/packs/type","style":"pop","shared":"shared.js","items":["typewriter","word-swap","split-flap","marquee","echo","liquid-fill","decode","word-by-word","key-phrase","stack","slot","outline-draw","flex","shimmer","stream","tile-flip"],"entries":[{"id":"typewriter","name":"Typewriter","description":"Words type on letter by letter with a blinking caret — in the brand's mono, text or display face.","category":"text","tags":["kinetic type","text","typewriter","typing","type on","caret","terminal"],"placement":"full","duration":{"default":5,"min":1.5,"max":30},"aspects":["16:9","9:16","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","default":"Every great edit starts with a single cut.","maxLength":200},"face":{"type":"choice","label":"Face","options":["mono","display","text"],"default":"mono"},"speed":{"type":"number","label":"Letters a second","default":18,"min":4,"max":60,"step":1}},"parts":{"ground":"the brand's ground","text":"the typed text"},"beats":{"in":0.2},"style":"pop","pack":"type","ref":"type/typewriter","code":"// Kinetic type on the brand's ground (and its pattern). Text sits inside the title-safe area.\nfunction ground(M, $, t) {\n  return M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: $.W, h: $.H } }));\n}\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const A = safeArea($), M = motion($, t);\n  const tin = $.beat(\"in\", 0.2);\n  let s = ground(M, $, t);\n  const face = p.face, txt = face === \"display\" ? $.case(p.text) : String(p.text || \"\");\n  const font = Object.assign({}, $.font(face), { ls: face === \"mono\" ? 0 : -0.015 });\n  const fit = fitLines($, txt, font, A.w * 0.86, Math.min(A.h * 0.2, 140 * k), 34 * k, 6);\n  const w = widest($, fit.lines, fit.font), lh = face === \"display\" ? 1.08 : 1.3;\n  const y0 = H / 2 - (fit.lines.length * fit.size * lh) / 2;\n  s += M.text(\"text\", Object.assign({ at: tin, in: \"typewriter\", split: \"letters\", stagger: 1 / p.speed }, fit.font, { x: W / 2 - w / 2, y: y0, lh: lh, maxW: A.w * 0.86, fill: c.fg }), fit.lines.join(\"\\n\"));\n  return s;\n}\n","libs":[]},{"id":"word-swap","name":"Word swap","description":"A fixed lead and a word that keeps changing: each new word rolls up into an accent box that stretches to fit it — “We make videos / ads / stories”.","category":"text","tags":["kinetic type","text","word swap","rotating","changing word","slot","carousel","hero"],"placement":"full","duration":{"default":6,"min":2,"max":30},"aspects":["16:9","9:16","1:1","4:5"],"props":{"lead":{"type":"text","label":"Lead","default":"We make","maxLength":24},"words":{"type":"list","label":"Words","of":"text","default":["videos","ads","stories","brands"],"maxItems":8},"end":{"type":"text","label":"After","default":"","maxLength":12,"description":"words after the box (empty for none)"}},"parts":{"ground":"the brand's ground","lead":"the fixed words","box":"the box and its word"},"beats":{"in":0.2},"style":"pop","pack":"type","ref":"type/word-swap","code":"// Kinetic type on the brand's ground (and its pattern). Text sits inside the title-safe area.\nfunction ground(M, $, t) {\n  return M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: $.W, h: $.H } }));\n}\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const A = safeArea($), M = motion($, t);\n  const tin = $.beat(\"in\", 0.2);\n  let s = ground(M, $, t);\n  const lead = $.case(p.lead), end = p.end ? $.case(p.end) : \"\";\n  const words = (p.words || []).map(function (w) { return $.case(String(w)); }).filter(Boolean);\n  if (!words.length) words.push(\"…\");\n  const tall = A.tall || A.square;\n  const base = Object.assign({}, $.font(\"display\"), { ls: -0.03 });\n  const pad = 0.28;\n  const wid = function (sz, str) { return $.measure(str, Object.assign({}, base, { size: sz })); };\n  // One size for every word: the widest must fit with the lead (side by side on wide frames, stacked on tall).\n  const widestWord = words.reduce(function (a, w) { return wid(100, w) > wid(100, a) ? w : a; }, words[0]);\n  const row = function (sz) { return tall ? Math.max(wid(sz, lead), wid(sz, widestWord) + sz * pad * 2, end ? wid(sz, end) : 0) : wid(sz, lead) + sz * 0.3 + wid(sz, widestWord) + sz * pad * 2 + (end ? sz * 0.3 + wid(sz, end) : 0); };\n  let size = Math.min(150 * k, A.h * (tall ? 0.16 : 0.28));\n  if (row(size) > A.w * 0.94) size *= (A.w * 0.94) / row(size);\n  const f = Object.assign({}, base, { size: size });\n  const per = Math.max(0.6, (M.outAt - tin - 0.3) / words.length);\n  const rel = t - tin - 0.3, idx = $.clamp(Math.floor(rel / per), 0, words.length - 1), loc = rel - idx * per;\n  const sw = $.E.out3($.clamp(loc / 0.4)), cur = words[idx], prev = idx > 0 ? words[idx - 1] : null;\n  const bwOf = function (w) { return wid(size, w) + size * pad * 2; };\n  const bw = prev && idx > 0 ? $.lerp(bwOf(prev), bwOf(cur), sw) : bwOf(cur), bh = size * 1.32;\n  const lw = wid(size, lead), ew = end ? wid(size, end) : 0, gap = size * 0.3;\n  let lx, bx, ex, ly, by;\n  if (tall) {\n    const h = bh * 2.1 + (end ? size * 1.15 : 0);\n    ly = H / 2 - h / 2; by = ly + size * 1.25;\n    lx = W / 2 - lw / 2; bx = W / 2 - bw / 2; ex = W / 2 - ew / 2;\n  } else {\n    const total = lw + gap + bw + (end ? gap + ew : 0);\n    lx = W / 2 - total / 2; bx = lx + lw + gap; ex = bx + bw + gap;\n    by = H / 2 - bh / 2; ly = by + (bh - size) / 2 - size * 0.04;\n  }\n  s += M.text(\"lead\", Object.assign({ at: tin, in: \"rise\" }, f, { x: lx, y: ly, fill: c.fg }), lead);\n  if (end) s += M.text(\"lead\", Object.assign({ at: tin, in: \"rise\" }, f, { x: ex, y: tall ? by + bh + size * 0.1 : ly, fill: c.fg }), end);\n  // The words roll through the box: the old one up and out, the new one up from below.\n  const ty = by + (bh - size) / 2 - size * 0.04, roll = bh;\n  let inner = $.text(cur, Object.assign({}, f, { x: bx + bw / 2, y: ty + (idx > 0 ? (1 - sw) * roll : 0), anchor: \"middle\", fill: $.on(c.accent) }));\n  if (prev && sw < 1) inner += $.text(prev, Object.assign({}, f, { x: bx + bw / 2, y: ty - sw * roll, anchor: \"middle\", fill: $.on(c.accent) }));\n  s += M.part(\"box\", { at: tin + 0.2, in: \"pop\", box: { x: bx, y: by, w: bw, h: bh } }, $.rect(bx, by, bw, bh, { r: Math.min($.style.radius, bh / 2), fill: c.accent }) + $.clip(bx, by, bw, bh, 0, inner));\n  return s;\n}\n","libs":[]},{"id":"split-flap","name":"Split-flap board","description":"An airport split-flap board: every cell flutters through letters and clacks onto the message, left to right.","category":"text","tags":["kinetic type","text","split flap","flap","board","airport","departures","solari"],"placement":"full","duration":{"default":4,"min":1.5,"max":20},"aspects":["16:9","9:16","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Message","default":"NOW BOARDING\nGATE 7","maxLength":60,"description":"up to three lines; new line for a new row"}},"parts":{"ground":"the brand's ground","board":"the board"},"beats":{"in":0.2},"style":"pop","pack":"type","ref":"type/split-flap","code":"// Kinetic type on the brand's ground (and its pattern). Text sits inside the title-safe area.\nfunction ground(M, $, t) {\n  return M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: $.W, h: $.H } }));\n}\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const A = safeArea($), M = motion($, t);\n  const tin = $.beat(\"in\", 0.2);\n  let s = ground(M, $, t);\n  const POOL = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\";\n  let rows = String(p.text || \"\").toUpperCase().split(\"\\n\").map(function (r) { return r.trim(); }).filter(Boolean).slice(0, 3);\n  if (!rows.length) rows.push(\"HELLO\");\n  // Tall frames: long rows break between words so the cells stay big.\n  if (A.tall || A.square) {\n    const max = A.tall ? 8 : 11, out = [];\n    rows.forEach(function (r) {\n      let cur = \"\";\n      r.split(/\\s+/).forEach(function (w) { if (cur && (cur + \" \" + w).length > max) { out.push(cur); cur = w; } else cur = cur ? cur + \" \" + w : w; });\n      if (cur) out.push(cur);\n    });\n    rows = out.slice(0, 5);\n  }\n  const cols = Math.max.apply(null, rows.map(function (r) { return r.length; }));\n  // A board is black with white letters whatever the brand; the brand shows in the landing flash.\n  const cellFill = \"#151517\", ink = \"#F4F1EA\";\n  const cw = Math.min((A.w * 0.96) / cols, (A.h * 0.7) / rows.length / 1.36) * 0.94, ch = cw * 1.36, gap = cw * 0.07;\n  const bw = cols * (cw + gap) - gap, bh = rows.length * (ch + gap * 2) - gap * 2;\n  const x0 = W / 2 - bw / 2, y0 = H / 2 - bh / 2;\n  const f = Object.assign({}, $.font(\"mono\"), { size: ch * 0.62, weight: 700 });\n  let out = \"\";\n  rows.forEach(function (row, r) {\n    const padL = Math.floor((cols - row.length) / 2);\n    for (let q = 0; q < cols; q++) {\n      const target = row.charAt(q - padL) || \" \";\n      const x = x0 + q * (cw + gap), y = y0 + r * (ch + gap * 2);\n      const start = tin + q * 0.045 + r * 0.12, flips = target === \" \" ? 0 : 5 + Math.floor($.hash(q * 3.1 + r * 7.7) * 9), fd = 0.065;\n      const n = Math.floor((t - start) / fd), fr = ((t - start) / fd) % 1;\n      const done = t < start ? false : n >= flips;\n      const ch2 = t < start ? \" \" : done ? target : POOL.charAt(Math.floor($.hash(n * 1.37 + q + r * 9) * POOL.length));\n      out += $.rect(x, y, cw, ch, { r: cw * 0.1, fill: cellFill });\n      if (ch2 !== \" \") out += $.text(ch2, Object.assign({}, f, { x: x + cw / 2, y: y + ch / 2 - f.size * 0.56, anchor: \"middle\", fill: done && t - start - flips * fd < 0.25 ? c.accent : ink }));\n      // The flap falling: a shade over the top half shrinking to the hinge, then over the bottom half growing.\n      if (t >= start && !done) {\n        const half = ch / 2;\n        if (fr < 0.5) out += $.rect(x, y + half * 2 * fr, cw, half * (1 - 2 * fr), { fill: $.rgba(cellFill, 0.85) });\n        else out += $.rect(x, y + half, cw, half * (2 * fr - 1), { fill: $.rgba(\"#000000\", 0.35) });\n      }\n      out += $.rect(x, y + ch / 2 - Math.max(1, cw * 0.018), cw, Math.max(2, cw * 0.036), { fill: $.rgba(\"#000000\", 0.45) });\n    }\n  });\n  s += M.part(\"board\", { at: tin, in: \"fade\", dur: 0.25 }, out);\n  return s;\n}\n","libs":[]},{"id":"marquee","name":"Marquee bands","description":"Big words repeat along tilted bands that scroll in opposite directions — solid on accent, outlined on the ground — like a poster in motion.","category":"text","tags":["kinetic type","text","marquee","ticker","scrolling","bands","poster","repeat"],"placement":"full","duration":{"default":6,"min":2,"max":30},"aspects":["16:9","9:16","1:1","4:5"],"props":{"text":{"type":"text","label":"Words","default":"New drop","maxLength":28},"bands":{"type":"number","label":"Bands","default":3,"min":1,"max":5,"step":1},"angle":{"type":"number","label":"Tilt","default":-8,"min":-30,"max":30,"step":1},"speed":{"type":"number","label":"Speed","default":1,"min":0.2,"max":3,"step":0.1}},"parts":{"ground":"the brand's ground","bands":"the bands"},"beats":{"in":0.2},"style":"pop","pack":"type","ref":"type/marquee","code":"// Kinetic type on the brand's ground (and its pattern). Text sits inside the title-safe area.\nfunction ground(M, $, t) {\n  return M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: $.W, h: $.H } }));\n}\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const A = safeArea($), M = motion($, t);\n  const tin = $.beat(\"in\", 0.2);\n  let s = ground(M, $, t);\n  const str = $.case(p.text), n = Math.max(1, Math.round(p.bands));\n  const D = Math.hypot(W, H), bandH = Math.min(H, W) * (n === 1 ? 0.3 : 0.62 / n + 0.06);\n  const f = Object.assign({}, $.font(\"display\"), { size: bandH * 0.64, ls: -0.02 });\n  const sep = \"  •  \", unit = $.measure(str + sep, f);\n  let out = \"\";\n  for (let i = 0; i < n; i++) {\n    const y = (i - (n - 1) / 2) * bandH * 1.12, solid = i % 2 === 0;\n    const dir = i % 2 ? -1 : 1, off = (((t * 140 * k * p.speed * dir) % unit) + unit) % unit;\n    let row = \"\";\n    for (let x = -D / 2 - unit + off; x < D / 2 + unit; x += unit) row += $.text(str + sep, Object.assign({}, f, { within: { x: -1e5, y: -1e5, w: 2e5, h: 2e5 }, x: x, y: y - f.size * 0.56, fill: solid ? $.on(c.accent) : \"none\", stroke: solid ? undefined : c.fg, sw: solid ? undefined : 2.6 * k }));\n    const band = (solid ? $.rect(-D / 2, y - bandH / 2, D, bandH, { fill: c.accent }) : \"\") + row;\n    out += M.part(\"bands\", { at: tin + i * 0.12, in: \"slide\", from: i % 2 ? \"right\" : \"left\", distance: 160, box: { x: -D / 2, y: y - bandH / 2, w: D, h: bandH } }, band);\n  }\n  s += '<g transform=\"translate(' + W / 2 + \" \" + H / 2 + \") rotate(\" + p.angle + ')\">' + out + \"</g>\";\n  return s;\n}\n","libs":[]},{"id":"echo","name":"Echo stack","description":"One word with outlined echoes fanning out above and below it, rippling in a slow wave — a poster headline that breathes.","category":"text","tags":["kinetic type","text","echo","repeat","outline","stack","poster","wave"],"placement":"full","duration":{"default":4,"min":1.5,"max":20},"aspects":["16:9","9:16","1:1","4:5"],"props":{"word":{"type":"text","label":"Word","default":"Echo","maxLength":16},"copies":{"type":"number","label":"Echoes each side","default":4,"min":1,"max":7,"step":1}},"parts":{"ground":"the brand's ground","word":"the solid word","echoes":"the outlined echoes"},"beats":{"in":0.2},"style":"pop","pack":"type","ref":"type/echo","code":"// Kinetic type on the brand's ground (and its pattern). Text sits inside the title-safe area.\nfunction ground(M, $, t) {\n  return M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: $.W, h: $.H } }));\n}\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const A = safeArea($), M = motion($, t);\n  const tin = $.beat(\"in\", 0.2);\n  let s = ground(M, $, t);\n  const str = $.case(p.word), n = Math.max(1, Math.round(p.copies));\n  const f = fitFont($, str, \"display\", Math.min(A.h / (2 * n + 1) * 1.45, 260 * k), A.w * 0.92, { ls: -0.03 });\n  const step = f.size * 0.74, cy = H / 2 - f.size * 0.56;\n  let ech = \"\";\n  for (let j = n; j >= 1; j--) {\n    const open = $.E.outBack($.clamp((t - tin - 0.25 - j * 0.07) / 0.6));\n    for (const sgn of [-1, 1]) {\n      const wob = Math.sin((t - tin) * 2.2 - j * 0.55) * 16 * k * j / n;\n      ech += $.text(str, Object.assign({}, f, { x: W / 2 + wob * sgn, y: cy + sgn * j * step * open, anchor: \"middle\", fill: \"none\", stroke: j % 2 ? c.accent : c.accent2, sw: 2.6 * k, op: Math.max(0.15, 1 - j * 0.16) }));\n    }\n  }\n  s += M.part(\"echoes\", { at: tin + 0.2, in: \"fade\", dur: 0.3 }, ech);\n  s += M.text(\"word\", Object.assign({ at: tin, in: \"pop\" }, f, { x: W / 2, y: cy, anchor: \"middle\", fill: c.fg }), str);\n  return s;\n}\n","libs":[]},{"id":"liquid-fill","name":"Liquid fill","description":"A big outlined word fills from the bottom with a wavy liquid in the brand's accents, then a tagline rises under it.","category":"text","tags":["kinetic type","text","liquid","fill","outline","wave","water","pour"],"placement":"full","duration":{"default":4,"min":1.5,"max":20},"aspects":["16:9","9:16","1:1","4:5"],"props":{"word":{"type":"text","label":"Word","default":"Fresh","maxLength":16},"tagline":{"type":"text","label":"Tagline","default":"Squeezed this morning","maxLength":48,"description":"empty for none"}},"parts":{"ground":"the brand's ground","outline":"the outlined word","fill":"the liquid","tagline":"the tagline"},"beats":{"in":0.2},"style":"pop","pack":"type","ref":"type/liquid-fill","code":"// Kinetic type on the brand's ground (and its pattern). Text sits inside the title-safe area.\nfunction ground(M, $, t) {\n  return M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: $.W, h: $.H } }));\n}\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const A = safeArea($), M = motion($, t);\n  const tin = $.beat(\"in\", 0.2);\n  let s = ground(M, $, t);\n  const str = $.case(p.word);\n  const f = fitFont($, str, \"display\", Math.min(A.h * 0.42, 320 * k), A.w * 0.92, { ls: -0.03 });\n  const tw = $.measure(str, f), x0 = W / 2 - tw / 2, y = H / 2 - f.size * (p.tagline ? 0.72 : 0.56);\n  const top = y + f.size * 0.12, bot = y + f.size * 0.96;\n  const word = function (fill, stroke) { return $.text(str, Object.assign({}, f, { x: W / 2, y: y, anchor: \"middle\", fill: fill, stroke: stroke, sw: stroke ? 3.2 * k : undefined })); };\n  const lvl = $.E.inOut2($.seg(t, tin + 0.4, tin + 2.2));\n  const wave = function (level, amp, ph, id) {\n    let d = \"M\" + (x0 - 20 * k) + \" \" + (bot + 20 * k);\n    for (let i = 0; i <= 30; i++) { const x = x0 - 20 * k + ((tw + 40 * k) * i) / 30; d += \"L\" + x + \" \" + (level + Math.sin(i * 0.65 + (t - tin) * 4 + ph) * amp); }\n    d += \"L\" + (x0 + tw + 20 * k) + \" \" + (bot + 20 * k) + \"Z\";\n    $.def('<clipPath id=\"' + id + '\"><path d=\"' + d + '\"/></clipPath>');\n    return 'clip-path=\"url(#' + id + ')\"';\n  };\n  const amp = f.size * 0.045 * (1 - 0.6 * lvl), L = $.lerp(bot + amp, top - amp * 2, lvl);\n  s += M.part(\"outline\", { at: tin, in: \"fade\" }, word(\"none\", c.fg));\n  if (lvl > 0) s += M.part(\"fill\", { at: tin + 0.4, in: \"none\" }, \"<g \" + wave(L - f.size * 0.05, amp, 1.8, $.uid(\"lq\")) + \">\" + word(c.accent2) + \"</g><g \" + wave(L, amp, 0, $.uid(\"lq\")) + \">\" + word(c.accent) + \"</g>\");\n  if (p.tagline) {\n    const tf = Object.assign({}, $.font(\"text\"), { size: Math.max(32 * k, f.size * 0.16) });\n    s += M.text(\"tagline\", Object.assign({ at: tin + 2, in: \"rise\", distance: 18 }, tf, { x: W / 2, y: bot + f.size * 0.18, anchor: \"middle\", fill: c.mute, maxW: A.w }), $.case(p.tagline));\n  }\n  return s;\n}\n","libs":[]},{"id":"decode","name":"Decode","description":"A title decodes letter by letter out of scrambled glyphs between brackets, then a mono line types on under it.","category":"text","tags":["kinetic type","text","decode","scramble","hacker","glitch","reveal","tech"],"placement":"full","duration":{"default":4,"min":1.5,"max":20},"aspects":["16:9","9:16","1:1","4:5"],"props":{"title":{"type":"text","label":"Title","default":"Access granted","maxLength":28},"line":{"type":"text","label":"Line","default":"loading your workspace","maxLength":60,"description":"empty for none"}},"parts":{"ground":"the brand's ground","brackets":"the brackets","title":"the title","line":"the mono line"},"beats":{"in":0.2},"style":"pop","pack":"type","ref":"type/decode","code":"// Kinetic type on the brand's ground (and its pattern). Text sits inside the title-safe area.\nfunction ground(M, $, t) {\n  return M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: $.W, h: $.H } }));\n}\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const A = safeArea($), M = motion($, t);\n  const tin = $.beat(\"in\", 0.2);\n  let s = ground(M, $, t);\n  const str = $.case(p.title);\n  const fit = fitLines($, str, Object.assign({}, $.font(\"display\"), { ls: -0.02 }), A.w * 0.8, Math.min(170 * k, A.h * 0.3), 60 * k, 2);\n  const lh = fit.size * 1.05, h = fit.lines.length * lh, y0 = H / 2 - h / 2 - (p.line ? 40 * k : 0);\n  const w = widest($, fit.lines, fit.font);\n  fit.lines.forEach(function (l, i) {\n    s += M.text(\"title\", Object.assign({ at: tin + 0.2 + i * 0.25, in: \"scramble\", split: \"letters\", dur: 0.7, stagger: Math.min(0.06, 0.9 / l.length) }, fit.font, { x: W / 2, y: y0 + i * lh, anchor: \"middle\", fill: c.fg }), l);\n  });\n  // Brackets open out from the centre to frame the title.\n  const bo = $.E.out3($.seg(t, tin, tin + 0.5)), bx = (w / 2 + 50 * k) * bo, bh = h + 30 * k, by = y0 - 18 * k, arm = 34 * k;\n  const br = function (x, dir) { return $.path(\"M\" + (x + dir * arm) + \" \" + by + \"H\" + x + \"V\" + (by + bh) + \"H\" + (x + dir * arm), { stroke: c.accent, sw: 7 * k, cap: \"square\", join: \"miter\" }); };\n  s += M.part(\"brackets\", { at: tin, in: \"fade\", dur: 0.2 }, br(W / 2 - bx, 1) + br(W / 2 + bx, -1));\n  if (p.line) {\n    const mf = Object.assign({}, $.font(\"mono\"), { size: Math.max(28 * k, fit.size * 0.22) });\n    const txt = \"> \" + p.line, mw = $.measure(txt, mf);\n    s += M.text(\"line\", Object.assign({ at: tin + 1.1, in: \"typewriter\", split: \"letters\", stagger: 0.04 }, mf, { x: W / 2 - Math.min(mw, A.w) / 2, y: by + bh + 40 * k, fill: c.accent, maxW: A.w }), txt);\n  }\n  return s;\n}\n","libs":[]},{"id":"word-by-word","name":"Word by word","description":"A sentence one word at a time, each word slammed full-frame on a new ground — the brand's, the accent, the second accent — cut to a steady pace. *Marked* words go big.","category":"text","tags":["kinetic type","text","word by word","kinetic","fast","punchy","quote","beat"],"placement":"full","duration":{"default":5,"min":1.5,"max":30},"aspects":["16:9","9:16","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","default":"Stop scrolling. *This* changes everything.","maxLength":160,"description":"wrap a word in *stars* to make it the accent"},"pace":{"type":"number","label":"Seconds a word","default":0.42,"min":0.15,"max":2,"step":0.01}},"parts":{"ground":"the brand's ground","words":"the words"},"beats":{"in":0.2},"style":"pop","pack":"type","ref":"type/word-by-word","code":"// Kinetic type on the brand's ground (and its pattern). Text sits inside the title-safe area.\nfunction ground(M, $, t) {\n  return M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: $.W, h: $.H } }));\n}\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const A = safeArea($), M = motion($, t);\n  const tin = $.beat(\"in\", 0.2);\n  let s = ground(M, $, t);\n  const ws = markedWords($.case(p.text));\n  if (!ws.length) return s;\n  const pace = Math.max(0.15, Math.min(p.pace, (M.outAt - tin) / ws.length));\n  const i = $.clamp(Math.floor((t - tin) / pace), 0, ws.length - 1), loc = t - tin - i * pace;\n  if (t < tin) return s;\n  const w = ws[i], grounds = [null, c.accent, c.fg, null, c.accent2];\n  const g = w.marked ? c.accent : grounds[i % grounds.length];\n  let fr = g ? $.rect(0, 0, W, H, { fill: g }) : \"\";\n  const ink = g ? $.on(g) : c.fg;\n  const font = w.marked ? Object.assign({}, $.font(\"accent\")) : Object.assign({}, $.font(\"display\"), { ls: -0.035 });\n  const f = fitFont($, w.word, w.marked ? \"accent\" : \"display\", Math.min(A.h * 0.5, (w.marked ? 340 : 280) * k), A.w * 0.94, { ls: w.marked ? -0.01 : -0.035 });\n  const pop = 1 + 0.22 * (1 - $.E.out3($.clamp(loc / 0.14))), rot = (i % 2 ? 1 : -1) * 2.5 * (1 - $.E.out3($.clamp(loc / 0.2)));\n  fr += '<g transform=\"translate(' + W / 2 + \" \" + H / 2 + \") rotate(\" + rot + \") scale(\" + pop + \") translate(\" + -W / 2 + \" \" + -H / 2 + ')\">' + $.text(w.word, Object.assign({}, f, { x: W / 2, y: H / 2 - f.size * 0.56, anchor: \"middle\", fill: ink })) + \"</g>\";\n  s += M.part(\"words\", { at: tin, in: \"none\" }, fr);\n  return s;\n}\n\n/** A tick for every word as it lands, a hit for a marked one. */\nfunction sound($) {\n  const p = $.data, tin = $.beat(\"in\", 0.2), ws = markedWords($.case(p.text));\n  if (!ws.length) return [];\n  const pace = Math.max(0.15, Math.min(p.pace, (motion($, 0).outAt - tin) / ws.length));\n  return ws.map(function (w, i) {\n    return w.marked ? { at: tin + i * pace, kind: \"hit\", level: 0.8 } : { at: tin + i * pace, kind: \"tick\", level: 0.9, pitch: (i % 3) - 1 };\n  });\n}\n","libs":[]},{"id":"key-phrase","name":"Key phrase","description":"A sentence sets in big type, then its *marked* words get a marker swipe one after another — the line people should remember.","category":"text","tags":["kinetic type","text","key phrase","highlight","marker","quote","emphasis","sentence"],"placement":"full","duration":{"default":5,"min":1.5,"max":30},"aspects":["16:9","9:16","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","default":"Good editing is *invisible* until it is *missing*.","maxLength":140,"description":"wrap words in *stars* to mark them"}},"parts":{"ground":"the brand's ground","text":"the sentence","marks":"the marker swipes"},"beats":{"in":0.2},"style":"pop","pack":"type","ref":"type/key-phrase","code":"// Kinetic type on the brand's ground (and its pattern). Text sits inside the title-safe area.\nfunction ground(M, $, t) {\n  return M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: $.W, h: $.H } }));\n}\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const A = safeArea($), M = motion($, t);\n  const tin = $.beat(\"in\", 0.2);\n  let s = ground(M, $, t);\n  const ws = markedWords($.case(p.text)), plain = ws.map(function (w) { return w.word; }).join(\" \");\n  const fit = fitLines($, plain, Object.assign({}, $.font(\"display\"), { ls: -0.025 }), A.w * 0.9, Math.min(150 * k, A.h * 0.22), 48 * k, 5);\n  const lh = fit.size * 1.18, y0 = H / 2 - (fit.lines.length * lh) / 2;\n  let wi = 0, marks = \"\", nMark = 0;\n  const flags = [], boxes = [];\n  fit.lines.forEach(function (line, li) {\n    const lay = M.layout(line, Object.assign({}, fit.font, { x: W / 2, y: y0 + li * lh, anchor: \"middle\" }), \"words\");\n    lay.units.forEach(function (u) { flags.push(ws[wi] && ws[wi].marked); boxes.push(u); wi++; });\n  });\n  const tMarks = tin + 0.35 + fit.lines.length * 0.12, markAt = [];\n  boxes.forEach(function (u, j) {\n    if (!flags[j]) return;\n    const a = tMarks + nMark * 0.35;\n    markAt[j] = a;\n    const pr = $.E.out3($.seg(t, a, a + 0.32)), pad = fit.size * 0.1;\n    if (pr > 0) marks += $.path(\"M\" + (u.x - pad) + \" \" + (u.y + fit.size * 0.18) + \"h\" + (u.w + 2 * pad) * pr + \"l\" + -pad * 0.4 + \" \" + fit.size * 0.86 + \"h\" + -(u.w + 2 * pad) * pr + \"Z\", { fill: c.accent2 });\n    nMark++;\n  });\n  s += M.part(\"marks\", { at: tMarks, in: \"none\" }, marks);\n  let base = 0;\n  fit.lines.forEach(function (line, li) {\n    const off = base;\n    s += M.text(\"text\", Object.assign({ at: tin + li * 0.12, in: \"rise\", split: \"words\", stagger: 0.05, fillFor: function (u) { const j = off + u.index; return flags[j] && t >= markAt[j] + 0.1 ? $.on(c.accent2) : c.fg; } }, fit.font, { x: W / 2, y: y0 + li * lh, anchor: \"middle\", fill: c.fg }), line);\n    base += line.split(/\\s+/).filter(Boolean).length;\n  });\n  return s;\n}\n","libs":[]},{"id":"stack","name":"Editorial stack","description":"Lines stack like a magazine cover — heavy capitals alternating with *italic* accent lines — sliding in from alternate sides between hairline rules.","category":"text","tags":["kinetic type","text","stack","editorial","magazine","serif","italic","headline"],"placement":"full","duration":{"default":4,"min":1.5,"max":20},"aspects":["16:9","9:16","1:1","4:5"],"props":{"lines":{"type":"list","label":"Lines","of":"text","default":["Make","*things*","people","*remember*"],"maxItems":6,"description":"wrap a line in *stars* for the italic accent face"}},"parts":{"ground":"the brand's ground","lines":"the lines","rules":"the hairlines"},"beats":{"in":0.2},"style":"pop","pack":"type","ref":"type/stack","code":"// Kinetic type on the brand's ground (and its pattern). Text sits inside the title-safe area.\nfunction ground(M, $, t) {\n  return M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: $.W, h: $.H } }));\n}\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const A = safeArea($), M = motion($, t);\n  const tin = $.beat(\"in\", 0.2);\n  let s = ground(M, $, t);\n  const rows = (p.lines || []).map(function (l) { const m = /^\\*(.*)\\*$/.exec(String(l).trim()); return m ? { text: m[1], acc: true } : { text: $.case(String(l).trim()), acc: false }; }).filter(function (r) { return r.text; });\n  if (!rows.length) return s;\n  const maxH = (A.h * 0.92) / rows.length;\n  const sized = rows.map(function (r) { return fitFont($, r.text, r.acc ? \"accent\" : \"display\", maxH / 1.0, A.w * 0.92, { ls: r.acc ? -0.01 : -0.04 }); });\n  const total = sized.reduce(function (a, f) { return a + f.size * 0.98; }, 0);\n  let y = H / 2 - total / 2;\n  rows.forEach(function (r, i) {\n    const f = sized[i];\n    s += M.text(\"lines\", Object.assign({ at: tin + i * 0.16, in: \"slide\", from: i % 2 ? \"right\" : \"left\", distance: 60 }, f, { x: W / 2, y: y - f.size * 0.06, anchor: \"middle\", fill: r.acc ? c.accent : c.fg }), r.text);\n    y += f.size * 0.98;\n    if (i < rows.length - 1) s += M.part(\"rules\", { at: tin + i * 0.16 + 0.25, in: \"grow-x\", origin: \"center\", box: { x: A.x, y: y - 2 * k, w: A.w, h: 4 * k } }, $.rect(A.x + A.w * 0.2, y - 1.5 * k, A.w * 0.6, 3 * k, { fill: $.rgba(c.fg, 0.35) }));\n  });\n  return s;\n}\n","libs":[]},{"id":"slot","name":"Slot reel","description":"A slot-machine reel: the line's last word rolls through the list, fast and blurred, slows and lands on the final word with a bounce, in an accent window beside the words before it.","category":"text","tags":["slot","reel","rolling","word","casino"],"placement":"full","duration":{"default":3.5,"min":2,"max":15},"props":{"before":{"type":"text","label":"Before the reel","default":"We make","maxLength":30},"words":{"type":"list","label":"Words (it lands on the last)","default":["films","reels","shorts","ads","stories"],"maxItems":10,"minItems":2,"of":"text"}},"parts":{"ground":"the ground","lead":"the words before","reel":"the reel"},"beats":{"in":0.15,"out":-0.45},"style":"pop","pack":"type","ref":"type/slot","code":"// Kinetic type on the brand's ground (and its pattern). Text sits inside the title-safe area.\nfunction ground(M, $, t) {\n  return M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: $.W, h: $.H } }));\n}\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n// A slot-machine reel: the line's last word rolls through the list, fast and blurred, slows and lands on the\n// final word with a little bounce, in an accent window beside the words before it.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const tin = $.beat(\"in\", 0.15);\n  let s = ground(M, $, t);\n  const words = (p.words || []).map(function (w) { return $.case(String(w)); }).filter(Boolean);\n  if (!words.length) return s;\n  const lead = $.case(String(p.before || \"\"));\n  const longest = words.reduce(function (a, w) { return w.length > a.length ? w : a; }, \"\");\n  // One line on wide frames (lead + reel), two on narrow ones (lead over reel).\n  const one = A.wide;\n  const size = Math.min((A.tall ? 150 : 170) * k, one ? A.w / (lead.length + longest.length + 2) * 1.6 : A.w / (longest.length + 1) * 1.6);\n  const f = Object.assign({}, $.font(\"display\"), { size: size, ls: -0.03 });\n  const leadW = lead ? $.measure(lead, f) : 0;\n  const reelW = Math.max.apply(null, words.map(function (w) { return $.measure(w, f); })) + size * 0.5;\n  const reelH = size * 1.3;\n  const totalW = one ? leadW + (lead ? size * 0.3 : 0) + reelW : Math.max(leadW, reelW);\n  const x0 = W / 2 - totalW / 2;\n  const cy = A.y + A.h / 2 - (one ? 0 : size * 0.65);\n  if (lead) s += M.text(\"lead\", Object.assign({ at: tin, in: \"rise\", distance: 30 }, f, { x: one ? x0 : W / 2, y: cy - size * 0.55, anchor: one ? \"start\" : \"middle\", fill: c.fg }), lead);\n  const rx = one ? x0 + leadW + (lead ? size * 0.3 : 0) : W / 2 - reelW / 2, ry = one ? cy - reelH / 2 : cy + size * 0.75;\n  // The roll: two spins through the list, easing out onto the last word; overshoot then settle.\n  const n = words.length, turns = 2 * n + (n - 1);\n  const t0 = tin + 0.4, t1 = t0 + 1.6;\n  const u = $.E.out5($.seg(t, t0, t1));\n  const settle = t > t1 ? 0.08 * Math.exp(-(t - t1) * 9) * Math.sin((t - t1) * 30) : 0;\n  const pos = u * turns + settle;\n  const speed = t < t1 ? (turns * 5 * Math.pow(1 - $.seg(t, t0, t1), 4)) / 1.6 : 0;\n  const clip = $.uid(), blur = $.uid();\n  $.def('<clipPath id=\"' + clip + '\"><rect x=\"' + rx + '\" y=\"' + ry + '\" width=\"' + reelW + '\" height=\"' + reelH + '\" rx=\"' + reelH * 0.22 + '\"/></clipPath>');\n  $.def('<filter id=\"' + blur + '\" x=\"-10%\" y=\"-50%\" width=\"120%\" height=\"200%\"><feGaussianBlur stdDeviation=\"0 ' + $.fx(Math.min(size * 0.25, speed * size * 0.02)) + '\"/></filter>');\n  let reel = $.rect(rx, ry, reelW, reelH, { r: reelH * 0.22, fill: c.accent });\n  let roll = \"\";\n  const base = Math.floor(pos);\n  for (let j = -1; j <= 1; j++) {\n    const idx = base + j, w = words[((idx % n) + n) % n];\n    // Capitals sit about 0.45 of the size below the text top: centre them in the window.\n    const y = ry + reelH / 2 - size * 0.47 + (idx - pos) * reelH;\n    roll += $.text(w, Object.assign({}, f, { x: rx + reelW / 2, y: y, anchor: \"middle\", fill: $.on(c.accent) }));\n  }\n  reel += '<g clip-path=\"url(#' + clip + ')\"><g filter=\"url(#' + blur + ')\">' + roll + \"</g></g>\";\n  s += M.part(\"reel\", { at: tin + 0.2, in: \"pop\", out: \"fade\", box: { x: rx, y: ry, w: reelW, h: reelH } }, reel);\n  return s;\n}\nfunction sound($) {\n  const t0 = $.beat(\"in\", 0.15) + 0.4;\n  const cues = [];\n  for (let i = 0; i < 12; i++) cues.push({ at: t0 + 1.6 * (1 - Math.pow(1 - i / 12, 0.45)), kind: \"tick\", level: 0.5 });\n  cues.push({ at: t0 + 1.6, kind: \"pop\", level: 0.9 });\n  return cues;\n}\n","libs":[]},{"id":"outline-draw","name":"Outline draw","description":"A word traced on as an outline, every letter's contour drawing at once, then filled in a wipe from the left, with a line under it rising after.","category":"text","tags":["outline","stroke","draw","trace","reveal"],"placement":"full","duration":{"default":3.5,"min":2,"max":15},"props":{"text":{"type":"text","label":"Word","default":"Crafted","maxLength":18},"subtitle":{"type":"text","label":"Line under","default":"Every frame on purpose","maxLength":60},"fill":{"type":"choice","label":"Fill","options":["accent","fg"],"default":"accent"}},"parts":{"ground":"the ground","word":"the word","subtitle":"the line under it"},"beats":{"in":0.15,"out":-0.45},"style":"pop","pack":"type","ref":"type/outline-draw","code":"// Kinetic type on the brand's ground (and its pattern). Text sits inside the title-safe area.\nfunction ground(M, $, t) {\n  return M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: $.W, h: $.H } }));\n}\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n// A word traced on as an outline, every letter's contour drawing at once, then filled in a wipe from the left,\n// with an optional line under it rising after.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const tin = $.beat(\"in\", 0.15);\n  let s = ground(M, $, t);\n  const word = $.case(String(p.text || \"\"));\n  const f = fitFont($, word, \"display\", (A.tall ? 300 : 280) * k, A.w * 0.92, { ls: -0.02 });\n  const sub = String(p.subtitle || \"\");\n  const sf = Object.assign({}, $.font(\"text\"), { size: (A.tall ? 46 : 40) * k, weight: 500 });\n  const blockH = f.size + (sub ? sf.size * 2 : 0);\n  const y = A.y + (A.h - blockH) / 2;\n  const tw = $.measure(word, f), x = W / 2 - tw / 2;\n  // The trace: a dash as long as a big letter's contour, sliding off as it draws.\n  const L = f.size * 4;\n  const d = $.E.inOut2($.seg(t, tin, tin + 1.4));\n  const line = $.text(word, Object.assign({}, f, { x: W / 2, y: y, anchor: \"middle\", fill: \"none\", stroke: c.accent, sw: Math.max(2, f.size * 0.018), extra: 'stroke-dasharray=\"' + $.fx(L) + \" \" + $.fx(L) + '\" stroke-dashoffset=\"' + $.fx(L * (1 - d)) + '\"' }));\n  // The fill wipes in from the left once the trace is nearly done.\n  const w = $.E.inOut3($.seg(t, tin + 1.1, tin + 1.8));\n  const clip = $.uid();\n  $.def('<clipPath id=\"' + clip + '\"><rect x=\"' + (x - f.size * 0.2) + '\" y=\"' + (y - f.size * 0.3) + '\" width=\"' + ((tw + f.size * 0.4) * w) + '\" height=\"' + f.size * 1.6 + '\"/></clipPath>');\n  const fill = '<g clip-path=\"url(#' + clip + ')\">' + $.text(word, Object.assign({}, f, { x: W / 2, y: y, anchor: \"middle\", fill: p.fill === \"fg\" ? c.fg : c.accent })) + \"</g>\";\n  s += M.part(\"word\", { at: tin, in: \"none\", out: \"fade\", box: { x: x, y: y, w: tw, h: f.size } }, line + fill);\n  if (sub) s += M.text(\"subtitle\", Object.assign({ at: tin + 1.7, in: \"rise\", distance: 20 }, sf, { x: W / 2, y: y + f.size * 1.08, anchor: \"middle\", fill: c.mute, maxW: A.w }), sub);\n  return s;\n}\n","libs":[]},{"id":"flex","name":"Weight flex","description":"Weight in motion: each letter swells from thin to heavy and back in a wave that runs along the word, the word re-centring as it breathes, then settles on the heaviest cut.","category":"text","tags":["variable","weight","flex","wave","breathing"],"placement":"full","duration":{"default":4,"min":2,"max":20},"props":{"text":{"type":"text","label":"Word","default":"FLEXIBLE","maxLength":14},"subtitle":{"type":"text","label":"Line under","default":"Edits that move with you","maxLength":50},"speed":{"type":"number","label":"Waves a second","default":0.8,"min":0.2,"max":3,"step":0.1},"face":{"type":"choice","label":"Face","options":["inter","brand"],"default":"inter"}},"parts":{"ground":"the ground","word":"the word","subtitle":"the line under it"},"beats":{"in":0.15,"out":-0.45},"style":"pop","pack":"type","ref":"type/flex","code":"// Kinetic type on the brand's ground (and its pattern). Text sits inside the title-safe area.\nfunction ground(M, $, t) {\n  return M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: $.W, h: $.H } }));\n}\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n// Weight in motion: each letter of a word swells from thin to heavy and back in a wave that runs along it, the\n// word re-centring as it breathes, then every letter settles on the heaviest cut.\nvar CUTS = [300, 400, 500, 600, 700, 800, 900];\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const tin = $.beat(\"in\", 0.15);\n  let s = ground(M, $, t);\n  const word = $.case(String(p.text || \"\"));\n  const chars = word.split(\"\");\n  const n = chars.length;\n  const family = p.face === \"brand\" ? $.font(\"display\").family : \"Inter\";\n  // Sized so the word at its heaviest fits the width.\n  const big = fitFont($, word, \"display\", (A.tall ? 260 : 240) * k, A.w * 0.94, { family: family, weight: 900, ls: -0.02 });\n  const size = big.size;\n  const end = $.beat(\"out\", $.dur - 0.45) - 0.8;\n  const settle = $.E.inOut2($.seg(t, end - 0.6, end));\n  const weights = chars.map(function (ch, i) {\n    const wave = 0.5 + 0.5 * Math.sin(2 * Math.PI * ((t - tin) * (Number(p.speed) || 0.8) - i / Math.max(1, n) * 1.2));\n    const v = $.lerp(300 + 600 * wave, 900, settle);\n    return CUTS.reduce(function (a, b) { return Math.abs(b - v) < Math.abs(a - v) ? b : a; }, 300);\n  });\n  const widths = chars.map(function (ch, i) { return $.measure(ch, { family: family, weight: weights[i], size: size, ls: -0.02 }); });\n  const total = widths.reduce(function (a, b) { return a + b; }, 0);\n  let x = W / 2 - total / 2;\n  const y = A.y + A.h / 2 - size * 0.55;\n  let line = \"\";\n  chars.forEach(function (ch, i) {\n    const lit = weights[i] >= 800;\n    line += $.text(ch, { family: family, weight: weights[i], size: size, ls: -0.02, x: x, y: y, fill: lit ? c.accent : c.fg });\n    x += widths[i];\n  });\n  s += M.part(\"word\", { at: tin, in: \"rise\", distance: 40, out: \"fade\", box: { x: W / 2 - total / 2, y: y, w: total, h: size } }, line);\n  if (p.subtitle) {\n    const sf = Object.assign({}, $.font(\"mono\"), { size: 30 * k, ls: 0.12 });\n    s += M.text(\"subtitle\", Object.assign({ at: tin + 0.4, in: \"fade\", dur: 0.5 }, sf, { x: W / 2, y: y + size * 1.15, anchor: \"middle\", fill: c.mute }), String(p.subtitle).toUpperCase());\n  }\n  return s;\n}\n","libs":[]},{"id":"shimmer","name":"Shimmer","description":"A headline with a band of light sweeping across it: the lines rise in, then a bright sheen passes over the letters on a loop, with a sparkle where it leaves.","category":"text","tags":["shimmer","shine","gloss","premium","sheen"],"placement":"full","duration":{"default":4,"min":2,"max":20},"props":{"text":{"type":"text","label":"Text","default":"Now in gold","maxLength":60},"every":{"type":"number","label":"Seconds between sweeps","default":2.4,"min":1.2,"max":10,"step":0.1},"color":{"type":"choice","label":"Colour","options":["accent","fg"],"default":"accent"}},"parts":{"ground":"the ground","text":"the headline"},"beats":{"in":0.15,"out":-0.45},"style":"pop","pack":"type","ref":"type/shimmer","code":"// Kinetic type on the brand's ground (and its pattern). Text sits inside the title-safe area.\nfunction ground(M, $, t) {\n  return M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: $.W, h: $.H } }));\n}\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n// A headline with a band of light sweeping across it: the lines rise in, then a bright sheen passes over the\n// letters (and again on a loop), with a sparkle where it leaves.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const tin = $.beat(\"in\", 0.15);\n  let s = ground(M, $, t);\n  const text = $.case(String(p.text || \"\"));\n  const tf = fitLines($, text, Object.assign({}, $.font(\"display\"), { ls: -0.03 }), A.w * 0.9, (A.tall ? 190 : 170) * k, 60 * k, 3);\n  const lh = tf.size * 1.04;\n  const blockH = tf.lines.length * lh;\n  const y0 = A.y + (A.h - blockH) / 2;\n  const widest = Math.max.apply(null, tf.lines.map(function (l) { return $.measure(l, tf.font); }));\n  const x0 = W / 2 - widest / 2;\n  // The sheen: a gradient band in user space, sweeping from left to right every `every` seconds.\n  const every = Math.max(1.2, Number(p.every) || 2.4);\n  const start = tin + 0.8;\n  const ph = t < start ? -1 : ((t - start) % every) / Math.min(1.1, every * 0.6);\n  const band = widest * 0.28, bx = x0 - band + (widest + band * 2) * $.E.inOut2($.clamp(ph));\n  const base = p.color === \"fg\" ? c.fg : c.accent;\n  const hi = $.mix(base, \"#FFFFFF\", 0.85);\n  const id = $.uid();\n  $.def('<linearGradient id=\"' + id + '\" gradientUnits=\"userSpaceOnUse\" x1=\"' + $.fx(bx - band) + '\" y1=\"0\" x2=\"' + $.fx(bx + band) + '\" y2=\"' + $.fx(band * 0.4) + '\"><stop offset=\"0\" stop-color=\"' + base + '\"/><stop offset=\"0.5\" stop-color=\"' + hi + '\"/><stop offset=\"1\" stop-color=\"' + base + '\"/></linearGradient>');\n  const lit = ph >= 0 && ph <= 1;\n  tf.lines.forEach(function (line, i) {\n    s += M.text(\"text\", Object.assign({ at: tin + i * 0.1, in: \"rise\", distance: 40 }, tf.font, { x: W / 2, y: y0 + i * lh, anchor: \"middle\", fill: lit ? \"url(#\" + id + \")\" : base }), line);\n  });\n  // A small four-point sparkle where the sheen leaves the last line.\n  const sp = $.bump(t, start + Math.min(1.1, every * 0.6) * 0.95, 0.1, 0.4);\n  if (sp > 0.01) {\n    const lx = W / 2 + $.measure(tf.lines[tf.lines.length - 1], tf.font) / 2 + tf.size * 0.12, ly = y0 + (tf.lines.length - 1) * lh + tf.size * 0.12, r = tf.size * 0.22 * sp;\n    s += $.path(\"M\" + lx + \" \" + (ly - r) + \"Q\" + lx + \" \" + ly + \" \" + (lx + r) + \" \" + ly + \"Q\" + lx + \" \" + ly + \" \" + lx + \" \" + (ly + r) + \"Q\" + lx + \" \" + ly + \" \" + (lx - r) + \" \" + ly + \"Q\" + lx + \" \" + ly + \" \" + lx + \" \" + (ly - r) + \"Z\", { fill: hi });\n  }\n  return s;\n}\n","libs":[]},{"id":"stream","name":"Streaming answer","description":"An assistant's answer streaming in: the prompt in a bubble, a moment of thinking dots, then the reply arriving word by word, each fading up, with a caret while it writes.","category":"text","tags":["ai","chat","streaming","prompt","assistant"],"placement":"full","duration":{"default":6,"min":3,"max":30},"props":{"prompt":{"type":"text","label":"Prompt","default":"Cut this interview into a 30-second teaser","maxLength":120},"response":{"type":"longtext","label":"Reply","default":"Done. I kept the three strongest answers, trimmed the pauses, put the hook first and timed the cuts to the music. Captions are on and the colour matches your last video.","maxLength":400},"speed":{"type":"number","label":"Words a second","default":9,"min":2,"max":30}},"parts":{"ground":"the ground","prompt":"the prompt bubble"},"beats":{"in":0.15,"out":-0.45},"style":"pop","pack":"type","ref":"type/stream","code":"// Kinetic type on the brand's ground (and its pattern). Text sits inside the title-safe area.\nfunction ground(M, $, t) {\n  return M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: $.W, h: $.H } }));\n}\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n// An assistant's answer streaming in: the prompt in a bubble, a moment of thinking dots, then the reply arriving\n// a few words at a time, each new word fading up, with a caret at the end while it writes.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const tin = $.beat(\"in\", 0.15);\n  let s = ground(M, $, t);\n  const w = Math.min(A.w, (A.wide ? 1300 : 1000) * k);\n  const x = W / 2 - w / 2;\n  const qf = Object.assign({}, $.font(\"text\"), { size: (A.tall ? 40 : 36) * k, weight: 600, lh: 1.3 });\n  const rf = Object.assign({}, $.font(\"text\"), { size: (A.tall ? 44 : 40) * k, weight: 500, lh: 1.42 });\n  const qLines = $.wrap(String(p.prompt || \"\"), w * 0.7 - 50 * k, qf).slice(0, 3);\n  const qw = Math.max.apply(null, qLines.map(function (l) { return $.measure(l, qf); }).concat([60 * k])) + 50 * k, qh = qLines.length * qf.size * 1.3 + 36 * k;\n  const words = String(p.response || \"\").split(/\\s+/).filter(Boolean);\n  const rLines = $.wrap(words.join(\" \"), w - 20 * k, rf);\n  const blockH = qh + 50 * k + rLines.length * rf.size * 1.42;\n  let y = A.y + Math.max(0, (A.h - blockH) / 2);\n  // The prompt, right-aligned in its bubble.\n  const qx = x + w - qw;\n  let q = $.rect(qx, y, qw, qh, { r: 26 * k, fill: $.rgba(c.accent, 0.14) });\n  qLines.forEach(function (l, i) { q += $.text(l, Object.assign({}, qf, { x: qx + 25 * k, y: y + 18 * k + i * qf.size * 1.3, fill: c.fg })); });\n  s += M.part(\"prompt\", { at: tin, in: \"rise\", distance: 30, out: \"fade\", box: { x: qx, y: y, w: qw, h: qh } }, q);\n  y += qh + 50 * k;\n  // Thinking, then the words arrive in small bursts.\n  const think = tin + 0.5, go = think + 0.8;\n  const rate = Math.max(2, Number(p.speed) || 9);\n  if (t >= think && t < go) {\n    let dots = \"\";\n    for (let i = 0; i < 3; i++) dots += $.circle(x + 20 * k + i * 30 * k, y + rf.size * 0.6, 9 * k, { fill: c.mute, op: 0.35 + 0.65 * $.wave(t, 0.9, i * 0.33) });\n    s += dots;\n  }\n  const shown = t < go ? 0 : Math.min(words.length, Math.floor((t - go) * rate) + 1);\n  let i = 0, out = \"\", caret = null;\n  rLines.forEach(function (line, li) {\n    let lx = x;\n    line.split(\" \").forEach(function (wd) {\n      if (i < shown) {\n        const born = go + i / rate, op = $.clamp((t - born) / 0.25);\n        out += $.text(wd, Object.assign({}, rf, { x: lx, y: y + li * rf.size * 1.42, fill: c.fg, op: op }));\n        caret = [lx + $.measure(wd, rf) + 8 * k, y + li * rf.size * 1.42];\n      }\n      lx += $.measure(wd + \" \", rf);\n      i++;\n    });\n  });\n  s += out;\n  // The caret blinks while the reply is being written.\n  if (caret && shown < words.length) s += $.rect(caret[0], caret[1] + rf.size * 0.12, rf.size * 0.5, rf.size * 0.95, { r: 3 * k, fill: c.accent, op: 0.9 });\n  else if (caret && $.wave(t, 1.1) > 0.5) s += $.rect(caret[0], caret[1] + rf.size * 0.12, rf.size * 0.5, rf.size * 0.95, { r: 3 * k, fill: c.accent, op: 0.9 });\n  return s;\n}\n","libs":[]},{"id":"tile-flip","name":"Tile flip","description":"A headline split into tiles that flip one after another in a diagonal sweep, each turning over to show its slice of a second headline — lifting as the sweep passes, shaded as it turns edge-on.","category":"text","tags":["flip","tiles","reveal","3d","headline"],"placement":"full","duration":{"default":4,"min":2,"max":20},"props":{"front":{"type":"text","label":"Front headline","default":"MAKE IT","maxLength":16},"rear":{"type":"text","label":"Rear headline","default":"MATTER.","maxLength":16},"tiles":{"type":"number","label":"Tiles across","default":12,"min":4,"max":24},"sweep":{"type":"number","label":"Sweep (seconds)","default":1.4,"min":0.6,"max":4,"step":0.1},"rearColor":{"type":"choice","label":"Rear side","options":["accent","same"],"default":"accent"}},"parts":{"ground":"the ground","tiles":"the tiles"},"beats":{"in":0.15,"out":-0.45},"style":"pop","pack":"type","ref":"type/tile-flip","code":"// Kinetic type on the brand's ground (and its pattern). Text sits inside the title-safe area.\nfunction ground(M, $, t) {\n  return M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: $.W, h: $.H } }));\n}\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n// A headline split into tiles that flip one after another in a diagonal sweep, each turning over to show its slice\n// of the second headline — lifting a little as the sweep passes, shaded as it turns edge-on.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const A = safeArea($);\n  const M = motion($, t);\n  const tin = $.beat(\"in\", 0.15);\n  let s = ground(M, $, t);\n  const front = $.case(String(p.front || \"\")), rear = $.case(String(p.rear || \"\"));\n  const longest = front.length > rear.length ? front : rear;\n  const f = fitFont($, longest, \"display\", (A.tall ? 230 : 260) * k, A.w * 0.9, { ls: -0.03 });\n  const bw = Math.max($.measure(front, f), $.measure(rear, f)) + f.size * 0.5, bh = f.size * 1.3;\n  const bx = W / 2 - bw / 2, by = A.y + (A.h - bh) / 2;\n  const cols = Math.max(4, Math.min(24, Math.round(Number(p.tiles) || 12))), rows = Math.max(2, Math.round(cols * (bh / bw) * 1.6));\n  const tw = bw / cols, th = bh / rows;\n  const rearBg = p.rearColor === \"same\" ? c.bg : c.accent, rearInk = p.rearColor === \"same\" ? c.fg : $.on(c.accent);\n  // The sweep: a diagonal front that travels across the block; each tile turns over 0.45 s as it passes.\n  const t0 = tin + 0.8, run = Math.max(0.6, Number(p.sweep) || 1.4), turn = 0.45;\n  // The front is just the letters (blank tiles turn away unseen); the rear is a solid block with its letters.\n  const face = function (str, bg, ink) { return (bg ? $.rect(bx, by, bw, bh, { fill: bg }) : \"\") + $.text(str, Object.assign({}, f, { x: W / 2, y: by + (bh - f.size) / 2 - f.size * 0.02, anchor: \"middle\", fill: ink })); };\n  const frontFace = face(front, \"\", c.fg), rearFace = face(rear, p.rearColor === \"same\" ? \"\" : rearBg, rearInk);\n  let tiles = \"\";\n  // Every tile turned: the rear in one piece, with no seams between tiles.\n  if (t >= t0 + run + turn) return s + M.part(\"tiles\", { at: tin, in: \"fade\", dur: 0.4, out: \"fade\", box: { x: bx, y: by, w: bw, h: bh } }, rearFace);\n  for (let r = 0; r < rows; r++) {\n    for (let q = 0; q < cols; q++) {\n      const d = (q / cols) * 0.75 + (r / rows) * 0.25;\n      const u = $.clamp((t - t0 - d * run) / turn);\n      const ang = u * Math.PI, sy = Math.abs(Math.cos(ang)), back = u > 0.5;\n      const x = bx + q * tw, y = by + r * th, cx = x + tw / 2, cy = y + th / 2;\n      const lift = 1 + 0.06 * Math.sin(ang);\n      const id = $.uid();\n      $.def('<clipPath id=\"' + id + '\"><rect x=\"' + $.fx(x) + '\" y=\"' + $.fx(y) + '\" width=\"' + $.fx(tw + 0.6) + '\" height=\"' + $.fx(th + 0.6) + '\"/></clipPath>');\n      let tile = '<g clip-path=\"url(#' + id + ')\">' + (back ? rearFace : frontFace) + \"</g>\";\n      // The rear block darkens as it turns edge-on (the front is only letters, so it simply narrows away).\n      if (u > 0.5 && u < 1 && p.rearColor !== \"same\") tile += $.rect(x, y, tw, th, { fill: \"#000000\", op: 0.3 * (1 - sy) });\n      tiles += u > 0 && u < 1 ? '<g transform=\"translate(' + $.fx(cx) + \" \" + $.fx(cy) + \") scale(\" + $.fx(lift) + \" \" + $.fx(Math.max(0.02, sy) * lift) + \") translate(\" + $.fx(-cx) + \" \" + $.fx(-cy) + ')\">' + tile + \"</g>\" : tile;\n    }\n  }\n  s += M.part(\"tiles\", { at: tin, in: \"fade\", dur: 0.4, out: \"fade\", box: { x: bx, y: by, w: bw, h: bh } }, tiles);\n  return s;\n}\n","libs":[]}]},{"format":1,"id":"captions","name":"MONARC Captions","version":"1.0.0","description":"Captions timed to the voice from a transcript, in 67 styles: script captions (pen-written signatures, cursive neon, seventies stacks, brush ribbons, doodles), social caption looks (paper cards, outlined pops, glowing capitals, key-word lockups, notebook notes, script flourishes), a camera that follows the words, karaoke, lyrics, chat messages, stacks, neon, a flipboard, stickers, icons, a news crawl and clean subtitles — in the brand's faces and colours.","author":"MONARC STUDIOS","license":"MONARC Packs License 1.0 (free to use in any video; not for resale as templates)","homepage":"https://volito.dev/docs/packs/captions","style":"tomato","shared":"shared.js","items":["camera-follow","karaoke","pop","subtitles","marker","neon-tubes","scramble","slam","typed","editorial","stacked","blur-in","gradient-fill","wipe-on","rgb-split","burst","boxes","sticker","focus-pull","wave","outline","bubble","crawl","extrude","two-tone","drop","punch-in","flipboard","progress","lyrics","messages","striped","icons","scribble","index-card","weight-shift","italic-aside","tile-step","soft-rise","ember-caps","aura-caps","tape-bars","punch-outline","wide-pair","signed-line","spaced-serif","night-script","headline-word","flare-word","zine-stack","type-medley","tube-light","tide-poster","block-justify","circled-word","notebook","loop-script","pen-write","cursive-neon","groovy-script","script-sandwich","swash-mark","vow-script","brush-ribbon","script-fill","doodle-script","script-duet"],"entries":[{"id":"camera-follow","name":"Camera-follow captions","description":"Each sentence written across a canvas far bigger than the frame while a camera follows the words as they are said: every word a step bigger than the last with the camera pulling back to keep it the same size (or the camera gliding along lines of text), motion blur on every move, and a pull-back at the sentence's end to show it whole. Give a transcript with --words; *starred* words and the ones in `accents` take the accent.","category":"captions","tags":["captions","kinetic typography","camera","zoom","transcript","words","social","reels","hook"],"placement":"overlay","duration":{"default":6,"min":1.5,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* for the accent","default":"Captions that follow every word you say, *right* on time.","maxLength":600},"accents":{"type":"text","label":"Accent words","description":"words to put in the accent wherever they are said, comma separated","default":"","maxLength":120},"camera":{"type":"choice","label":"Camera","description":"grow: each word a step bigger, the camera pulling back to keep up · track: lines of one size, the camera gliding along them","options":["grow","track"],"default":"grow"},"framing":{"type":"choice","label":"Framing","description":"how big the newest word is on screen","options":["standard","tight","wide"],"default":"standard"},"growth":{"type":"choice","label":"Growth","description":"grow camera: how much bigger each word is than the last","options":["standard","steady","bold"],"default":"standard"},"blur":{"type":"choice","label":"Motion blur","options":["standard","subtle","heavy","off"],"default":"standard"},"ending":{"type":"choice","label":"Sentence end","description":"pull back: the camera shows each sentence whole once it is said · hold: it stays on the last word","options":["pull back","hold"],"default":"pull back"},"ground":{"type":"choice","label":"Behind the words","description":"vignette: a soft dark edge over the footage · none: straight on the footage with a shadow · panel: the brand's ground, full frame","options":["vignette","none","panel"],"default":"vignette"},"case":{"type":"choice","label":"Case","options":["upper","as typed","lower","style"],"default":"upper"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.42,"min":0.1,"max":2,"step":0.01},"ticks":{"type":"boolean","label":"Sound on every word","default":false}},"beats":{"in":0.15},"parts":{"ground":"the vignette or panel behind the words","words":"the words and the camera over them"},"style":"tomato","pack":"captions","ref":"captions/camera-follow","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// Each sentence is written across a canvas far bigger than the frame, and a camera follows the words as they\n// are said. \"grow\": every word lands a step bigger than the last, beside it or on a new row, and the camera pulls\n// back to keep the newest word the same size on screen, so the sentence shrinks away behind it. \"track\": the\n// words sit in lines like a page and the camera glides along them. At each sentence's end the camera pulls back\n// to show it whole, then flies on to the next one.\n\nconst WORLD = { x: -1e7, y: -1e7, w: 2e7, h: 2e7 };\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const A = safeArea($), M = motion($, t);\n  const tin = $.beat(\"in\", 0.15);\n  let s = \"\";\n  if (p.ground === \"panel\") s += M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: W, h: H } }));\n  else if (p.ground !== \"none\") s += M.part(\"ground\", { at: tin - 0.15, in: \"fade\", dur: 0.5, out: \"fade\", vary: false }, vignette($, 0.62));\n  const ws = captionWords($, p, tin);\n  if (!ws.length) return s;\n  const plan = cameraPlan($, ws, A, p);\n  const cam = cameraAt(plan.keys, t), was = cameraAt(plan.keys, t - 1 / 30);\n  const ink = p.ground === \"panel\" ? c.fg : \"#FFFFFF\";\n  // The sentence being said (earlier ones dim once the camera has moved on).\n  let block = 0;\n  plan.words.forEach(function (u) { if (t >= u.s - 0.06) block = u.block; });\n  let words = \"\";\n  plan.words.forEach(function (u) {\n    const since = t - (u.s - 0.06);\n    if (since < 0) return;\n    const px = u.size * cam.sc;\n    if (px < 1.2) return;\n    const sx = (u.x - cam.fx) * cam.sc + W / 2, sy = (u.y - cam.fy) * cam.sc + H / 2;\n    if (sx > W * 1.3 || sy > H * 1.3 || sx + u.ww * cam.sc < -W * 0.3 || sy + px < -H * 0.3) return;\n    const op = $.clamp(since / 0.1) * (u.block < block ? 0.32 : 1);\n    // Words are cropped by the frame on purpose here (the camera keeps the newest one inside it), so text is checked against the canvas.\n    words += $.text(u.w, Object.assign({}, plan.font, { size: u.size, x: u.x, y: u.y, fill: u.accent ? c.accent : ink, op: op, within: WORLD }));\n  });\n  let g = '<g transform=\"translate(' + $.fx(W / 2) + \" \" + $.fx(H / 2) + \") scale(\" + cam.sc.toPrecision(6) + \") translate(\" + $.fx(-cam.fx) + \" \" + $.fx(-cam.fy) + ')\">' + words + \"</g>\";\n  // Motion blur along the camera's move: sideways for a pan, both ways for a zoom, gone when it settles.\n  const amount = { off: 0, subtle: 0.45, standard: 1, heavy: 1.8 }[p.blur];\n  const dz = Math.abs(Math.log(cam.sc / was.sc));\n  const cap = 14 * k, f = amount === undefined ? 1 : amount;\n  const bx = Math.min(cap, (Math.abs(cam.fx - was.fx) * cam.sc * 0.25 + dz * W * 0.05) * f);\n  const by = Math.min(cap, (Math.abs(cam.fy - was.fy) * cam.sc * 0.25 + dz * H * 0.035) * f);\n  if (bx + by > 0.8) {\n    const id = $.uid(\"cfb\");\n    $.def('<filter id=\"' + id + '\" filterUnits=\"userSpaceOnUse\" x=\"' + -W * 0.1 + '\" y=\"' + -H * 0.1 + '\" width=\"' + W * 1.2 + '\" height=\"' + H * 1.2 + '\"><feGaussianBlur stdDeviation=\"' + (Math.round(bx * 2) / 2) + \" \" + (Math.round(by * 2) / 2) + '\"/></filter>');\n    g = '<g filter=\"url(#' + id + ')\">' + g + \"</g>\";\n  }\n  if (p.ground === \"none\") g = $.g({ extra: 'filter=\"url(#mg-sm)\"' }, g);\n  return s + M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.3, outDur: 0.26, vary: false }, g);\n}\n\n/**\n * Where every word sits in the world and where the camera goes: { words, keys, font }. Sentences (at most twelve\n * words) are blocks laid side by side; each word is a camera move, and each block ends with a pull-back.\n */\nfunction cameraPlan($, ws, A, p) {\n  const font = Object.assign({}, $.font(\"display\"), { ls: -0.02 });\n  const growth = { steady: 1.12, standard: 1.22, bold: 1.34 }[p.growth] || 1.22;\n  const W = $.W, H = $.H;\n  const words = [], boxes = [];\n  let ox = 0;\n  phrases(ws, 1e9, 12).forEach(function (b, bi) {\n    const L = p.camera === \"track\" ? trackBlock($, b, font, A) : growBlock($, b, font, A, growth);\n    L.words.forEach(function (u) { u.x += ox; u.block = bi; words.push(u); });\n    boxes.push({ x: ox, y: 0, w: L.w, h: L.h });\n    ox += L.w + Math.max(L.w, L.h) * 0.6;\n  });\n  // How big the newest word is on screen: its height against the frame (bigger on a phone).\n  const frac = ({ tight: 0.21, standard: 0.155, wide: 0.11 }[p.framing] || 0.155) * (A.tall ? 1.3 : 1);\n  // Grow: the newest word at the centre. Track: its whole line in frame, leaning a little towards the word.\n  const target = function (u) {\n    if (p.camera === \"track\") {\n      const line = words.filter(function (v) { return v.block === u.block && v.y === u.y; });\n      const x0 = line[0].x, x1 = line[line.length - 1].x + line[line.length - 1].ww;\n      const sc = Math.min((Math.min(W, H) * frac) / u.size, (W * 0.86) / (x1 - x0));\n      return { sc: sc, fx: (x0 + x1) / 2 + (u.x + u.ww / 2 - (x0 + x1) / 2) * 0.18, fy: u.y + u.size / 2 };\n    }\n    const sc = Math.min((Math.min(W, H) * frac) / u.size, (W * 0.84) / u.ww);\n    return { sc: sc, fx: u.x + u.ww / 2, fy: u.y + u.size / 2 };\n  };\n  const whole = function (b) { return { sc: Math.min((W * 0.86) / b.w, (H * (A.tall ? 0.62 : 0.74)) / b.h), fx: b.x + b.w / 2, fy: b.y + b.h / 2 }; };\n  const keys = [];\n  words.forEach(function (u, i) {\n    const next = words[i + 1];\n    const gap = next ? next.s - u.s : 0.6;\n    const dur = Math.max(0.2, Math.min(0.55, gap * 0.85));\n    keys.push({ at: u.s - 0.04, dur: dur, to: target(u), word: true });\n    const last = !next || next.block !== u.block;\n    if (last && p.ending !== \"hold\") {\n      // After the last word lands; the last sentence's pull-back settles before the clip fades.\n      const at = u.s + dur * 0.7 + 0.1, room = (next ? next.s - 0.1 : $.dur - 0.35) - at;\n      if (room > 0.3) keys.push({ at: at, dur: Math.min(0.9, room * 0.85), to: whole(boxes[u.block]), word: false });\n    }\n  });\n  return { words: words, keys: keys, font: font };\n}\n\n/** A block where each word is a step bigger than the last: beside it while the row stays in shape, else on a new row below. */\nfunction growBlock($, ws, font, A, growth) {\n  const rowShape = A.tall ? 4.6 : A.wide ? 7 : 5.5;\n  const items = [], rows = [];\n  let size = 100, row = 0, rowW = 0, rowH = 0;\n  ws.forEach(function (w, i) {\n    if (i) size *= growth;\n    const ww = $.measure(w.w, Object.assign({}, font, { size: size })), gap = size * 0.28;\n    if (i && rowW + gap + ww > rowShape * Math.max(rowH, size)) {\n      rows.push({ w: rowW, h: rowH });\n      row++;\n      rowW = 0;\n      rowH = 0;\n    }\n    const x = rowW ? rowW + gap : 0;\n    items.push({ w: w, row: row, x: x, ww: ww, size: size });\n    rowW = x + ww;\n    rowH = Math.max(rowH, size);\n  });\n  rows.push({ w: rowW, h: rowH });\n  const tops = [];\n  let y = 0;\n  rows.forEach(function (r) { tops.push(y); y += r.h * 1.04; });\n  // Words in a row stand on the same line.\n  const out = items.map(function (it) { return Object.assign({}, it.w, { x: it.x, y: tops[it.row] + rows[it.row].h - it.size, ww: it.ww, size: it.size }); });\n  return { words: out, w: Math.max.apply(null, rows.map(function (r) { return r.w; })), h: y - rows[rows.length - 1].h * 0.04 };\n}\n\n/** A block set like a page: one size, lines a few words wide. */\nfunction trackBlock($, ws, font, A) {\n  const size = 100, f = Object.assign({}, font, { size: size }), gap = size * 0.28, lh = size * 1.14;\n  const maxW = size * (A.tall ? 6.5 : A.wide ? 11 : 8);\n  const out = [];\n  let x = 0, y = 0, w = 0;\n  ws.forEach(function (wd) {\n    const ww = $.measure(wd.w, f);\n    if (x > 0 && x + gap + ww > maxW) { x = 0; y += lh; }\n    if (x > 0) x += gap;\n    out.push(Object.assign({}, wd, { x: x, y: y, ww: ww, size: size }));\n    x += ww;\n    w = Math.max(w, x);\n  });\n  return { words: out, w: w, h: y + size };\n}\n\n/** The camera at t: each move starts from wherever the last one had got to, eased to a stop. */\nfunction cameraAt(keys, t) {\n  const ease = function (x) { x = Math.max(0, Math.min(1, x)); return 1 - Math.pow(1 - x, 4); };\n  const mix = function (a, b, e) {\n    return { sc: Math.exp(Math.log(a.sc) + (Math.log(b.sc) - Math.log(a.sc)) * e), fx: a.fx + (b.fx - a.fx) * e, fy: a.fy + (b.fy - a.fy) * e };\n  };\n  let cur = keys[0].to, prevStart = keys[0].to;\n  for (let j = 1; j < keys.length; j++) {\n    const K = keys[j], P = keys[j - 1];\n    if (t < K.at) break;\n    const start = j === 1 ? keys[0].to : mix(prevStart, P.to, ease((K.at - P.at) / P.dur));\n    cur = mix(start, K.to, ease((t - K.at) / K.dur));\n    prevStart = start;\n  }\n  return cur;\n}\n\n/** A tick on every word when asked, and a soft whoosh as the camera pulls back on a sentence. */\nfunction sound($) {\n  const p = $.data, ws = captionWords($, p, $.beat(\"in\", 0.15));\n  if (!ws.length) return [];\n  const plan = cameraPlan($, ws, safeArea($), p);\n  const pulls = plan.keys.filter(function (K) { return !K.word; }).map(function (K) { return { at: K.at, kind: \"whoosh\", level: 0.45, pitch: -4, dur: Math.min(0.8, K.dur) }; });\n  return (p.ticks ? wordCues(ws) : []).concat(pulls);\n}\n","libs":[]},{"id":"karaoke","name":"Karaoke captions","description":"Social captions on the voice: a line or two at a time, the word being said lit by a pill that slides from word to word (or in the accent colour, or underlined), the rest of the line waiting dimmed. Give a transcript with --words; *starred* words and the ones in `accents` take the accent.","category":"captions","tags":["captions","subtitles","karaoke","transcript","words","social","reels","shorts","tiktok"],"placement":"overlay","captionLook":true,"duration":{"default":7,"min":1.5,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* for the accent","default":"Captions keep people watching, so every *word* lands right on time.","maxLength":600},"accents":{"type":"text","label":"Accent words","description":"words to put in the accent wherever they are said, comma separated","default":"","maxLength":120},"look":{"type":"choice","label":"Highlight","description":"pill: a block of accent behind the word being said · color: the word turns the accent · underline: a bar draws under it","options":["pill","color","underline"],"default":"pill"},"upcoming":{"type":"choice","label":"Words not said yet","options":["dim","hidden"],"default":"dim"},"lines":{"type":"choice","label":"Lines","options":["two","one"],"default":"two"},"position":{"type":"choice","label":"Position","options":["bottom","center","top"],"default":"bottom"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"ground":{"type":"choice","label":"Behind the words","description":"shadow: straight on the footage with a soft shadow · box: a dark rounded box behind each line","options":["shadow","box"],"default":"shadow"},"case":{"type":"choice","label":"Case","options":["as typed","upper","lower","style"],"default":"as typed"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.3,"min":0.08,"max":2,"step":0.01},"ticks":{"type":"boolean","label":"Sound on every word","default":false}},"beats":{"in":0.05},"parts":{"words":"the caption lines, their highlight and box"},"style":"tomato","pack":"captions","ref":"captions/karaoke","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const tin = $.beat(\"in\", 0.05);\n  const ws = captionWords($, p, tin);\n  if (!ws.length) return \"\";\n  const scale = { small: 0.82, medium: 1, large: 1.22 }[p.size] || 1;\n  const font = Object.assign({}, $.font(\"display\"), { ls: -0.01, size: (A.tall ? 76 : A.wide ? 64 : 70) * k * scale });\n  const maxW = A.w * (A.tall ? 0.92 : 0.78);\n  const two = p.lines !== \"one\";\n  const perLine = Math.max(8, Math.floor(maxW / (font.size * 0.58)));\n  const g = groupAt(phrases(ws, perLine * (two ? 2 : 1), two ? 10 : 5), t, 0.7);\n  if (!g) return \"\";\n  const lh = 1.18;\n  const probe = lineUp($, g.words, font, maxW, W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, W / 2, captionY($, A, p.position) - probe.h / 2, lh);\n  // The word being said: the last one whose time has come.\n  let ci = -1;\n  lay.words.forEach(function (u, j) { if (t >= u.s - 0.02) ci = j; });\n  const ink = \"#FFFFFF\";\n  const padX = font.size * 0.2, padY = font.size * 0.1;\n  const box = function (u) { return { x: u.x - padX, y: u.y - padY, w: u.ww + padX * 2, h: font.size + padY * 2 }; };\n  let s = \"\";\n  if (p.ground === \"box\") {\n    lay.widths.forEach(function (w, li) {\n      const y = lay.words.find(function (u) { return u.line === li; }).y;\n      s += $.rect(W / 2 - w / 2 - font.size * 0.42, y - font.size * 0.2, w + font.size * 0.84, font.size * 1.4, { r: font.size * 0.24, fill: \"#000000\", op: 0.58 });\n    });\n  }\n  // The highlight under the word being said slides over from the word before it on the same line.\n  if (p.look === \"pill\" && ci >= 0) {\n    const cur = lay.words[ci], prev = lay.words[ci - 1];\n    const b = box(cur);\n    const e = $.E.out3($.clamp((t - cur.s + 0.02) / 0.13));\n    const a = prev && prev.line === cur.line ? box(prev) : { x: b.x + b.w * 0.2, y: b.y + b.h * 0.1, w: b.w * 0.6, h: b.h * 0.8 };\n    const r = { x: $.lerp(a.x, b.x, e), y: $.lerp(a.y, b.y, e), w: $.lerp(a.w, b.w, e), h: $.lerp(a.h, b.h, e) };\n    s += $.rect(r.x, r.y, r.w, r.h, { r: r.h * 0.26, fill: c.accent });\n  }\n  lay.words.forEach(function (u, j) {\n    const said = j <= ci, isCur = j === ci;\n    let fill = u.accent ? (p.look === \"pill\" ? c.accent2 : c.accent) : ink;\n    let op = said ? 1 : p.upcoming === \"hidden\" ? 0 : 0.42;\n    if (isCur && p.look === \"pill\") fill = $.on(c.accent);\n    else if (isCur && p.look === \"color\") fill = u.accent ? c.accent2 : c.accent;\n    if (op <= 0) return;\n    const lift = isCur ? 1 + 0.07 * (1 - $.E.out3($.clamp((t - u.s + 0.02) / 0.16))) : 1;\n    const word = $.text(u.w, Object.assign({}, font, { x: u.x, y: u.y, fill: fill, op: op }));\n    s += lift === 1 ? word : $.g({ transform: $.tf({ s: lift, ox: u.x + u.ww / 2, oy: u.y + font.size / 2 }) }, word);\n    if (isCur && p.look === \"underline\") {\n      const e = $.E.out3($.clamp((t - u.s + 0.02) / 0.14));\n      s += $.rect(u.x, u.y + font.size * 1.04, u.ww * e, font.size * 0.09, { r: font.size * 0.045, fill: c.accent });\n    }\n  });\n  // Each group rises in as it starts; words over footage get a soft shadow to read on anything.\n  const ein = $.E.out3($.clamp((t - g.start) / 0.16));\n  s = $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - ein) * 14 * k }) }, s);\n  if (p.ground !== \"box\") s = $.g({ extra: 'filter=\"url(#mg-sm)\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, s);\n}\n\nfunction sound($) {\n  return $.data.ticks ? wordCues(captionWords($, $.data, $.beat(\"in\", 0.05))) : [];\n}\n","libs":[]},{"id":"pop","name":"Pop captions","description":"Big captions a few words at a time, each word landing on its own time with a pop — the newest one in the accent. Give a transcript with --words; *starred* words and the ones in `accents` take the second accent.","category":"captions","tags":["captions","subtitles","kinetic typography","transcript","words","social","reels","shorts","tiktok","hook"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* for the accent","default":"Most videos lose people in the first *three seconds.*","maxLength":600},"accents":{"type":"text","label":"Accent words","description":"words to put in the accent wherever they are said, comma separated","default":"","maxLength":120},"words":{"type":"choice","label":"Words at a time","options":["two","one","three"],"default":"two"},"highlight":{"type":"choice","label":"Highlight","description":"newest: the word just said is in the accent · none: only the accent words","options":["newest","none"],"default":"newest"},"position":{"type":"choice","label":"Position","options":["center","bottom","top"],"default":"center"},"size":{"type":"choice","label":"Size","options":["medium","large","huge"],"default":"medium"},"case":{"type":"choice","label":"Case","options":["upper","as typed","lower","style"],"default":"upper"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01},"ticks":{"type":"boolean","label":"Sound on every word","default":false}},"beats":{"in":0.05},"parts":{"words":"the words"},"style":"tomato","pack":"captions","ref":"captions/pop","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const tin = $.beat(\"in\", 0.05);\n  const ws = captionWords($, p, tin);\n  if (!ws.length) return \"\";\n  const per = { one: 1, two: 2, three: 3 }[p.words] || 2;\n  const g = groupAt(phrases(ws, A.tall ? 18 : 26, per), t, 0.5);\n  if (!g) return \"\";\n  const scale = { medium: 1, large: 1.2, huge: 1.45 }[p.size] || 1;\n  // The group on one line, as big as it fits.\n  const line = g.words.map(function (w) { return w.w; }).join(\" \");\n  const font = fitFont($, line, \"display\", (A.tall ? 190 : A.wide ? 140 : 160) * k * scale, A.w * 0.9, { ls: -0.02 });\n  const lay = lineUp($, g.words, font, A.w * 0.92, W / 2, captionY($, A, p.position) - font.size / 2, 1.05);\n  let s = \"\";\n  let newest = -1;\n  lay.words.forEach(function (u, j) { if (t >= u.s - 0.03) newest = j; });\n  lay.words.forEach(function (u, j) {\n    const since = t - (u.s - 0.03);\n    if (since < 0) return;\n    // Each word lands on its own time: in big, then settles with a little overshoot.\n    const sc = 1 + 0.42 * (1 - settle(since / 0.2));\n    const op = $.clamp(since / 0.05);\n    const lit = p.highlight === \"newest\" ? j === newest : false;\n    const fill = u.accent ? c.accent2 : lit ? c.accent : \"#FFFFFF\";\n    const cx = u.x + u.ww / 2, cy = u.y + font.size / 2;\n    const word = $.text(u.w, Object.assign({}, font, { x: u.x, y: u.y, fill: fill, op: op }));\n    s += $.g({ transform: $.tf({ s: sc, ox: cx, oy: cy, r: (1 - $.E.out3($.clamp(since / 0.2))) * (j % 2 ? 3 : -3) }) }, word);\n  });\n  s = $.g({ extra: 'filter=\"url(#mg-md)\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.18, outDur: 0.16, vary: false }, s);\n}\n\nfunction sound($) {\n  return $.data.ticks ? wordCues(captionWords($, $.data, $.beat(\"in\", 0.05))) : [];\n}\n","libs":[]},{"id":"subtitles","name":"Subtitles","description":"Clean subtitles timed to the voice: up to two lines at a time in the brand's text face, broken at pauses and sentence ends, on a dark box behind each line or with a shadow. Give a transcript with --words.","category":"captions","tags":["captions","subtitles","accessibility","transcript","words","interview","broadcast"],"placement":"overlay","captionLook":true,"duration":{"default":8,"min":1.5,"max":300},"aspects":["16:9","9:16","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* for the accent","default":"Most videos lose people in the first three seconds. Captions keep them watching.","maxLength":1200},"accents":{"type":"text","label":"Accent words","description":"words to put in the accent wherever they are said, comma separated","default":"","maxLength":120},"ground":{"type":"choice","label":"Behind the words","description":"box: a translucent box behind each line · solid: a near-opaque box · shadow: a soft shadow only","options":["box","solid","shadow"],"default":"box"},"position":{"type":"choice","label":"Position","options":["bottom","top"],"default":"bottom"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"case":{"type":"choice","label":"Case","options":["as typed","upper","lower","style"],"default":"as typed"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01}},"beats":{"in":0},"parts":{"words":"the subtitle lines and their boxes"},"style":"tomato","pack":"captions","ref":"captions/subtitles","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\nfunction frame(t, $) {\n  const p = $.data, k = $.k, W = $.W, H = $.H;\n  const A = safeArea($), M = motion($, t);\n  const tin = $.beat(\"in\", 0);\n  const ws = captionWords($, p, tin);\n  if (!ws.length) return \"\";\n  const scale = { small: 0.85, medium: 1, large: 1.2 }[p.size] || 1;\n  const font = Object.assign({}, $.font(\"text\"), { weight: 600, ls: 0, size: (A.tall ? 50 : A.wide ? 46 : 48) * k * scale });\n  const maxW = A.w * (A.tall ? 0.88 : 0.72);\n  const perLine = Math.max(16, Math.min(42, Math.floor(maxW / (font.size * 0.5))));\n  const groups = phrases(ws, perLine * 2, 16);\n  // A subtitle stays at least a second (or until the next one), so it can be read.\n  const g = groupAt(groups, t, 0.9);\n  if (!g) return \"\";\n  const lh = 1.3;\n  const probe = lineUp($, g.words, font, maxW, W / 2, 0, lh);\n  const bottom = p.position === \"top\" ? null : H * (A.tall ? 0.76 : 0.9);\n  const top = p.position === \"top\" ? H * (A.tall ? 0.14 : 0.08) : bottom - probe.h;\n  const lay = lineUp($, g.words, font, maxW, W / 2, top, lh);\n  let s = \"\";\n  if (p.ground !== \"shadow\") {\n    lay.widths.forEach(function (w, li) {\n      const y = lay.words.find(function (u) { return u.line === li; }).y;\n      s += $.rect(W / 2 - w / 2 - font.size * 0.36, y - font.size * 0.18, w + font.size * 0.72, font.size * 1.36, { r: font.size * 0.14, fill: \"#000000\", op: p.ground === \"solid\" ? 0.86 : 0.6 });\n    });\n  }\n  lay.words.forEach(function (u) {\n    s += $.text(u.w, Object.assign({}, font, { x: u.x, y: u.y, fill: u.accent ? $.c.accent : \"#FFFFFF\" }));\n  });\n  const fade = Math.min($.clamp((t - g.start) / 0.1), $.clamp((g.end - t) / 0.1));\n  s = $.g({ op: fade }, s);\n  if (p.ground === \"shadow\") s = $.g({ extra: 'filter=\"url(#mg-sm)\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.2, outDur: 0.18, vary: false }, s);\n}\n","libs":[]},{"id":"marker","name":"Marker captions","description":"A highlighter swipes under each word as it is said and stays lit, a little crooked like a real pen, so the line fills with the brand's colour as it is spoken (or an underline stroke). Give a transcript with --words; *starred* words take the second accent.","category":"captions","tags":["captions","highlight","marker","transcript","social","reels"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* for the accent","default":"Every word you say gets *highlighted* as you say it.","maxLength":600},"accents":{"type":"text","label":"Accent words","description":"words to put in the accent wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["bottom","center","top"],"default":"bottom"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"case":{"type":"choice","label":"Case","options":["as typed","upper","lower","style"],"default":"as typed"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01},"ticks":{"type":"boolean","label":"Sound on every word","default":false},"style":{"type":"choice","label":"Marker","options":["highlight","underline"],"default":"highlight","description":"highlight: a block behind the word · underline: a thick stroke under it"}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/marker","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// A highlighter swipes under each word as it is said and stays lit, so the line fills with colour as it is spoken.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const C = captionLines($, A, ws, t, { size: (A.tall ? 74 : A.wide ? 62 : 68) * k * sizeOf(p.size), position: p.position, lh: 1.26 });\n  if (!C) return \"\";\n  const f = C.font;\n  let marks = \"\", words = \"\";\n  C.lay.words.forEach(function (u, j) {\n    const since = t - (u.s - 0.02);\n    const ink = u.accent ? c.accent2 : c.accent;\n    if (since >= 0) {\n      // The swipe takes about as long as the word, a little crooked, like a real pen.\n      const e = $.E.out3($.clamp(since / Math.max(0.12, Math.min(0.32, (u.e - u.s) * 0.85))));\n      const x = u.x - f.size * 0.14, y = u.y + f.size * (p.style === \"underline\" ? 0.62 : 0.06), h = f.size * (p.style === \"underline\" ? 0.36 : 1.0);\n      const tilt = (($.hash(j + 3) - 0.5) * 2.4);\n      marks += $.rect(x, y, (u.ww + f.size * 0.28) * e, h, { r: f.size * 0.1, fill: ink, op: 0.95, transform: $.tf({ r: tilt, ox: x, oy: y + h / 2 }) });\n    }\n    const on = since >= 0 && p.style !== \"underline\";\n    words += $.text(u.w, Object.assign({}, f, { x: u.x, y: u.y, fill: on ? $.on(ink) : \"#FFFFFF\", op: since >= 0 ? 1 : 0.55 }));\n  });\n  const s = $.g({ extra: 'filter=\"url(#mg-sm)\"' }, groupIn($, C.g, t, marks + words));\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, s);\n}\n\nfunction sound($) {\n  return $.data.ticks ? wordCues(captionWords($, $.data, $.beat(\"in\", 0.05))) : [];\n}\n","libs":[]},{"id":"neon-tubes","name":"Neon captions","description":"The line hangs as unlit glass tubes and each word flickers on as it is said, glowing in the accent (accent words in the second accent) and drifting a hair once lit. Give a transcript with --words.","category":"captions","tags":["captions","neon","glow","night","transcript","music","reels"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* for the accent","default":"Light up *every* word on the beat.","maxLength":600},"accents":{"type":"text","label":"Accent words","description":"words to put in the accent wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["center","bottom","top"],"default":"center"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"case":{"type":"choice","label":"Case","options":["upper","as typed","lower","style"],"default":"upper"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01},"ticks":{"type":"boolean","label":"Sound on every word","default":false}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/neon-tubes","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// Neon tubes: the line hangs unlit, and each word flickers on as it is said with a glow in the brand's colours.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const C = captionLines($, A, ws, t, { size: (A.tall ? 76 : A.wide ? 64 : 70) * k * sizeOf(p.size), position: p.position, lh: 1.22 });\n  if (!C) return \"\";\n  const f = C.font, blurs = {};\n  const wide = blurFilter($, blurs, 14 * k), tight = blurFilter($, blurs, 4 * k);\n  let glow = \"\", core = \"\";\n  C.lay.words.forEach(function (u, j) {\n    const since = t - (u.s - 0.02);\n    const hue = u.accent ? c.accent2 : c.accent;\n    // A tube buzzes as it catches, then holds; lit tubes drift a hair.\n    const lit = since < 0 ? 0 : since > 0.2 ? 1 : $.hash(Math.floor(t * 30) * 7.13 + j) < 0.35 + since * 3.2 ? 1 : 0.12;\n    const dy = Math.sin(t * 2.1 + j * 1.7) * 1.6 * k * (since > 0.2 ? 1 : 0);\n    const at = Object.assign({}, f, { x: u.x, y: u.y + dy });\n    if (lit < 0.5) {\n      core += $.text(u.w, Object.assign({}, at, { fill: \"none\", stroke: $.rgba(hue, 0.42), sw: 1.6 * k, op: 1 }));\n      return;\n    }\n    glow += $.g({ extra: wide }, $.text(u.w, Object.assign({}, at, { fill: hue, stroke: hue, sw: f.size * 0.06, op: 0.85 })));\n    glow += $.g({ extra: tight }, $.text(u.w, Object.assign({}, at, { fill: hue, op: 0.9 })));\n    core += $.text(u.w, Object.assign({}, at, { fill: \"#FFFFFF\", stroke: hue, sw: f.size * 0.025 }));\n  });\n  const s = groupIn($, C.g, t, glow + core);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, s);\n}\n\nfunction sound($) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  return $.data.ticks ? ws.map(function (w) { return { at: w.s, kind: \"zap\", level: w.accent ? 0.6 : 0.35 }; }) : [];\n}\n","libs":[]},{"id":"scramble","name":"Decode captions","description":"Each word arrives as scrambled characters that settle into the word letter by letter as it is said, in the mono face (or the display face). Give a transcript with --words.","category":"captions","tags":["captions","decode","scramble","glitch","tech","transcript"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* for the accent","default":"Decoding the signal, one *word* at a time.","maxLength":600},"accents":{"type":"text","label":"Accent words","description":"words to put in the accent wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["bottom","center","top"],"default":"bottom"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"case":{"type":"choice","label":"Case","options":["upper","as typed","lower","style"],"default":"upper"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01},"ticks":{"type":"boolean","label":"Sound on every word","default":false},"face":{"type":"choice","label":"Face","options":["mono","display"],"default":"mono"}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/scramble","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// Each word arrives as scrambled characters that settle, letter by letter, into the word as it is said.\nconst GLYPHS = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789#%&*+=<>/\\\\\";\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const role = p.face === \"display\" ? \"display\" : \"mono\";\n  const C = captionLines($, A, ws, t, { role: role, size: (A.tall ? 70 : A.wide ? 58 : 64) * k * sizeOf(p.size), position: p.position, charW: role === \"mono\" ? 0.62 : 0.58, lh: 1.22 });\n  if (!C) return \"\";\n  const f = C.font, frameNo = Math.floor(t * 30);\n  let s = \"\";\n  C.lay.words.forEach(function (u, j) {\n    const since = t - (u.s - 0.04);\n    if (since < 0) return;\n    const chars = Array.from(u.w);\n    const step = Math.min(0.045, Math.max(0.02, (u.e - u.s) / Math.max(1, chars.length)));\n    chars.forEach(function (ch, i) {\n      const x = u.x + $.measure(chars.slice(0, i).join(\"\"), f);\n      const settled = since >= 0.06 + i * step;\n      const g = settled || /\\s|[.,!?]/.test(ch) ? ch : GLYPHS.charAt(Math.floor($.hash(frameNo * 3.7 + i * 11.3 + j * 5.1) * GLYPHS.length));\n      s += $.text(g, Object.assign({}, f, { x: x, y: u.y, fill: settled ? (u.accent ? c.accent : \"#FFFFFF\") : c.accent, op: settled ? 1 : 0.85, within: { x: -1e6, y: -1e6, w: 2e6, h: 2e6 } }));\n    });\n  });\n  s = $.g({ extra: 'filter=\"url(#mg-sm)\"' }, groupIn($, C.g, t, s));\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, s);\n}\n\nfunction sound($) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  return $.data.ticks ? ws.map(function (w) { return { at: w.s, kind: \"type\", level: 0.5 }; }) : [];\n}\n","libs":[]},{"id":"slam","name":"Slam captions","description":"One word at a time (or two), as big as the frame allows, slammed in from alternating sides with motion blur and a punch, accent words bigger still. Give a transcript with --words.","category":"captions","tags":["captions","kinetic typography","slam","hook","full screen","transcript","reels","shorts"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* for the accent","default":"Stop. Look. *Listen.*","maxLength":600},"accents":{"type":"text","label":"Accent words","description":"words to put in the accent wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["center","bottom","top"],"default":"center"},"size":{"type":"choice","label":"Size","options":["medium","large","huge","small"],"default":"medium"},"case":{"type":"choice","label":"Case","options":["upper","as typed","lower","style"],"default":"upper"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01},"ticks":{"type":"boolean","label":"Sound on every word","default":false},"words":{"type":"choice","label":"Words at a time","options":["one","two"],"default":"one"}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/slam","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// One word at a time, as big as the frame allows, slammed in from alternating sides with a smear of motion blur.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const g = groupAt(phrases(ws, A.tall ? 12 : 18, p.words === \"two\" ? 2 : 1), t, 0.45);\n  if (!g) return \"\";\n  const line = g.words.map(function (w) { return w.w; }).join(\" \");\n  const accent = g.words.some(function (w) { return w.accent; });\n  const base = (A.tall ? 300 : A.wide ? 240 : 270) * k * sizeOf(p.size) * (accent ? 1.12 : 1);\n  const f = fitFont($, line, \"display\", base, A.w * 0.9, { ls: -0.03 });\n  const cy = captionY($, A, p.position);\n  const lay = lineUp($, g.words, f, A.w * 0.92, W / 2, cy - f.size / 2, 1);\n  const since = t - g.start;\n  // Alternating directions: left, right, up, down; the word travels in fast and settles with a small overshoot.\n  const dir = [[-1, 0], [1, 0], [0, -1], [0, 1]][g.k % 4];\n  const e = settle(since / 0.24);\n  const dist = (dir[0] ? W : H) * 0.55;\n  const dx = -dir[0] * (1 - e) * dist, dy = -dir[1] * (1 - e) * dist;\n  const speed = 1 - $.E.out3($.clamp(since / 0.2));\n  const blur = blurFilter($, {}, Math.abs(dir[0]) * speed * 34 * k, Math.abs(dir[1]) * speed * 34 * k);\n  const punch = 1 + 0.08 * (1 - settle(since / 0.3));\n  let s = \"\";\n  lay.words.forEach(function (u) {\n    s += $.text(u.w, Object.assign({}, f, { x: u.x, y: u.y, fill: u.accent ? c.accent : \"#FFFFFF\" }));\n  });\n  s = $.g({ transform: $.tf({ x: dx, y: dy, s: punch, ox: W / 2, oy: cy }) }, s);\n  if (blur) s = $.g({ extra: blur }, s);\n  s = $.g({ extra: 'filter=\"url(#mg-md)\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.18, outDur: 0.16, vary: false }, s);\n}\n\nfunction sound($) {\n  const p = $.data, ws = captionWords($, p, $.beat(\"in\", 0.05));\n  return ws.map(function (w, i) { return { at: w.s - 0.06, kind: \"whoosh\", level: 0.4, pitch: (i % 2) * 3, dur: 0.22 }; });\n}\n","libs":[]},{"id":"typed","name":"Typewriter captions","description":"Typed as it is said: each word's letters land across its spoken time in the mono face (or the text face), with a caret that blinks while the voice rests, straight on the footage or on dark boxes. Give a transcript with --words.","category":"captions","tags":["captions","typewriter","typing","terminal","transcript","tutorial"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* for the accent","default":"Typed out as you *say* it, letter by letter.","maxLength":600},"accents":{"type":"text","label":"Accent words","description":"words to put in the accent wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["bottom","center","top"],"default":"bottom"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"case":{"type":"choice","label":"Case","options":["as typed","upper","lower","style"],"default":"as typed"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01},"ticks":{"type":"boolean","label":"Sound on every word","default":false},"face":{"type":"choice","label":"Face","options":["mono","text"],"default":"mono"},"ground":{"type":"choice","label":"Behind the words","options":["none","box"],"default":"none","description":"none: on the footage with a soft shadow · box: a dark box behind each line"}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/typed","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// Typed as it is said: each word's letters land across its spoken time, with a caret that blinks while the voice rests.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const role = p.face === \"text\" ? \"text\" : \"mono\";\n  const C = captionLines($, A, ws, t, { role: role, size: (A.tall ? 62 : A.wide ? 52 : 56) * k * sizeOf(p.size), position: p.position, charW: role === \"mono\" ? 0.62 : 0.55, lh: 1.3 });\n  if (!C) return \"\";\n  const f = C.font;\n  let s = \"\", caret = null;\n  C.lay.words.forEach(function (u) {\n    const since = t - u.s;\n    if (since < 0) return;\n    const chars = Array.from(u.w);\n    const span = Math.max(0.08, Math.min(u.e - u.s, 0.07 * chars.length + 0.04));\n    const n = Math.min(chars.length, Math.floor((since / span) * chars.length) + 1);\n    const shown = chars.slice(0, n).join(\"\");\n    s += $.text(shown, Object.assign({}, f, { x: u.x, y: u.y, fill: u.accent ? c.accent : \"#FFFFFF\" }));\n    caret = { x: u.x + $.measure(shown, f) + f.size * 0.06, y: u.y, typing: n < chars.length };\n  });\n  if (caret) {\n    const on = caret.typing || Math.floor(t * 2.2) % 2 === 0;\n    if (on) s += $.rect(caret.x, caret.y + f.size * 0.02, Math.max(3, f.size * 0.09), f.size * 0.98, { fill: c.accent });\n  }\n  let box = \"\";\n  if (p.ground === \"box\") {\n    C.lay.widths.forEach(function (w, li) {\n      const y = C.lay.words.find(function (u) { return u.line === li; }).y;\n      box += $.rect(W / 2 - w / 2 - f.size * 0.4, y - f.size * 0.2, w + f.size * 0.8 + f.size * 0.2, f.size * 1.4, { r: f.size * 0.12, fill: \"#000000\", op: 0.62 });\n    });\n  }\n  s = groupIn($, C.g, t, box + s);\n  if (p.ground !== \"box\") s = $.g({ extra: 'filter=\"url(#mg-sm)\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, s);\n}\n\nfunction sound($) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  return $.data.ticks ? ws.map(function (w) { return { at: w.s, kind: \"type\", level: 0.55 }; }) : [];\n}\n","libs":[]},{"id":"editorial","name":"Editorial captions","description":"Magazine captions: the line in the brand's text face with its key word set far bigger in the accent face — the *starred* words, else the longest word of each phrase — every word rising in as it is said. Give a transcript with --words.","category":"captions","tags":["captions","editorial","magazine","serif","emphasis","transcript","interview"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* for the accent","default":"The best stories are told *slowly*.","maxLength":600},"accents":{"type":"text","label":"Accent words","description":"words to put in the accent wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["bottom","center","top"],"default":"bottom"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"case":{"type":"choice","label":"Case","options":["as typed","upper","lower","style"],"default":"as typed"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01},"ticks":{"type":"boolean","label":"Sound on every word","default":false},"emphasis":{"type":"choice","label":"Emphasis","options":["auto","marked"],"default":"auto","description":"auto: lift the longest word of each phrase when none is starred · marked: only starred or listed words"},"contrast":{"type":"choice","label":"Size contrast","options":["standard","subtle","dramatic"],"default":"standard"},"upcoming":{"type":"choice","label":"Words not said yet","options":["dim","hidden"],"default":"dim"}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/editorial","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// Editorial: the line in the brand's text face, its key words set far bigger in the accent face, each landing as it is said.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? 58 : A.wide ? 48 : 52) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.8);\n  const g = groupAt(phrases(ws, Math.floor(maxW / (size * 0.62)) * 2, 9), t, 0.8);\n  if (!g) return \"\";\n  // Without marked words, the longest word in each group is the one to lift.\n  const marked = g.words.some(function (w) { return w.accent; });\n  const longest = g.words.reduce(function (a, w) { return w.w.replace(/[^\\w]/g, \"\").length > a.w.replace(/[^\\w]/g, \"\").length ? w : a; }, g.words[0]);\n  const big = function (w) { return marked ? w.accent : p.emphasis !== \"marked\" && w === longest; };\n  const scale = { subtle: 1.5, standard: 2.1, dramatic: 2.8 }[p.contrast] || 2.1;\n  const fontFor = function (w) {\n    return big(w) ? Object.assign({}, $.font(\"accent\"), { size: Math.min(size * scale, (maxW * 0.95) / Math.max(1, w.w.length * 0.52)), ls: -0.02 })\n      : Object.assign({}, $.font(\"text\"), { size: size, weight: 500 });\n  };\n  const lay = lineUpMixed($, g.words, fontFor, maxW, W / 2, captionY($, A, p.position), 1.12);\n  let s = \"\";\n  lay.forEach(function (u) {\n    const since = t - (u.s - 0.04);\n    if (since < 0 && p.upcoming === \"hidden\") return;\n    const e = $.E.out3($.clamp(since / 0.3));\n    const lifted = big(u);\n    const word = $.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y + (1 - e) * u.size * 0.18, fill: lifted ? c.accent : \"#FFFFFF\", op: since < 0 ? 0.32 : 0.32 + 0.68 * e }));\n    s += lifted && since >= 0 ? $.g({ transform: $.tf({ s: 1 + 0.06 * (1 - settle(since / 0.35)), ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }) }, word) : word;\n  });\n  s = $.g({ extra: 'filter=\"url(#mg-sm)\"' }, groupIn($, g, t, s));\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, s);\n}\n\nfunction sound($) {\n  return $.data.ticks ? wordCues(captionWords($, $.data, $.beat(\"in\", 0.05))) : [];\n}\n","libs":[]},{"id":"stacked","name":"Stacked captions","description":"Short lines stack up the frame: the newest at the bottom at full size, its words popping in as they are said, while the lines before it rise, shrink and fade. Give a transcript with --words.","category":"captions","tags":["captions","stack","rolling","transcript","social","reels","podcast"],"placement":"overlay","duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* for the accent","default":"Short lines that stack up as you keep on talking.","maxLength":600},"accents":{"type":"text","label":"Accent words","description":"words to put in the accent wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["center","bottom","top"],"default":"center"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"case":{"type":"choice","label":"Case","options":["as typed","upper","lower","style"],"default":"as typed"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01},"ticks":{"type":"boolean","label":"Sound on every word","default":false},"lines":{"type":"choice","label":"Lines kept","options":["three","two"],"default":"three"}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/stacked","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// Lines stack up the frame: the newest at the bottom, full size, written as it is said; the ones before rise, shrink and fade.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const groups = phrases(ws, A.tall ? 16 : 24, 4);\n  const g = groupAt(groups, t, 1.2);\n  if (!g) return \"\";\n  const base = (A.tall ? 92 : A.wide ? 72 : 80) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.74);\n  const keep = p.lines === \"two\" ? 2 : 3;\n  // By age (0 = newest): how big a line is and how much of it shows; the last step is a line leaving.\n  const shrink = [1, 0.62, 0.42, 0.32], fade = keep === 2 ? [1, 0.45, 0, 0] : [1, 0.5, 0.24, 0];\n  const at = function (arr, x) { const i = Math.min(arr.length - 1, Math.floor(x)), j = Math.min(arr.length - 1, i + 1); return $.lerp(arr[i], arr[j], x - i); };\n  // When a line arrives the whole stack slides up by its height.\n  const e = $.E.out3($.clamp((t - g.start) / 0.3));\n  const gap = base * 0.14;\n  let cur = captionY($, A, p.position) + base * 0.5 + (1 - e) * (base + gap);\n  let s = \"\";\n  for (let a = 0; a <= keep && g.k - a >= 0; a++) {\n    const words = groups[g.k - a];\n    const age = a === 0 ? 0 : a - 1 + e;\n    const op = at(fade, age) * (a === 0 ? e : 1);\n    const f = Object.assign({}, $.font(\"display\"), { size: base * at(shrink, age), ls: -0.02 });\n    const text = words.map(function (w) { return w.w; }).join(\" \");\n    f.size *= Math.min(1, maxW / Math.max(1, $.measure(text, f)));\n    const top = cur - f.size;\n    cur = top - gap * at(shrink, age);\n    if (op <= 0.01) continue;\n    const lay = lineUp($, words, f, maxW * 1.5, W / 2, top, 1);\n    lay.words.forEach(function (u) {\n      const since = t - (u.s - 0.03);\n      if (a === 0 && since < 0) return;\n      const word = $.text(u.w, Object.assign({}, f, { x: u.x, y: u.y, fill: u.accent ? c.accent : \"#FFFFFF\", op: op }));\n      const pop = a === 0 ? 1 + 0.18 * (1 - settle(since / 0.2)) : 1;\n      s += pop === 1 ? word : $.g({ transform: $.tf({ s: pop, ox: u.x + u.ww / 2, oy: u.y + f.size / 2 }) }, word);\n    });\n  }\n  s = $.g({ extra: 'filter=\"url(#mg-sm)\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, s);\n}\n\nfunction sound($) {\n  return $.data.ticks ? wordCues(captionWords($, $.data, $.beat(\"in\", 0.05))) : [];\n}\n","libs":[]},{"id":"blur-in","name":"Blur-in captions","description":"Calm captions: each word resolves from a soft blur and rises into place as it is said, in the brand's text face (or display face). Give a transcript with --words.","category":"captions","tags":["captions","subtitles","calm","keynote","blur","transcript","interview"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* for the accent","default":"Calm words that settle into focus as they are said.","maxLength":600},"accents":{"type":"text","label":"Accent words","description":"words to put in the accent wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["bottom","center","top"],"default":"bottom"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"case":{"type":"choice","label":"Case","options":["as typed","upper","lower","style"],"default":"as typed"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01},"face":{"type":"choice","label":"Face","options":["text","display"],"default":"text"}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/blur-in","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// Calm captions: each word resolves from a soft blur and rises into place as it is said.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const role = p.face === \"display\" ? \"display\" : \"text\";\n  const C = captionLines($, A, ws, t, { role: role, size: (A.tall ? 66 : A.wide ? 54 : 60) * k * sizeOf(p.size), font: role === \"text\" ? { weight: 600 } : {}, position: p.position, lh: 1.24 });\n  if (!C) return \"\";\n  const f = C.font, blurs = {};\n  let s = \"\";\n  C.lay.words.forEach(function (u) {\n    const since = t - (u.s - 0.06);\n    if (since < 0) return;\n    const e = $.E.out3($.clamp(since / 0.42));\n    const blur = blurFilter($, blurs, (1 - e) * 12 * k);\n    const word = $.text(u.w, Object.assign({}, f, { x: u.x, y: u.y + (1 - e) * 22 * k, fill: u.accent ? c.accent : \"#FFFFFF\", op: $.clamp(since / 0.25) }));\n    s += blur ? $.g({ extra: blur }, word) : word;\n  });\n  s = $.g({ extra: 'filter=\"url(#mg-sm)\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, s);\n}\n","libs":[]},{"id":"gradient-fill","name":"Gradient captions","description":"Bold words filled with a gradient of the brand's two accents, each landing on its time with an elastic bounce and a tilt. Give a transcript with --words; *starred* words get a lighter gradient.","category":"captions","tags":["captions","gradient","bounce","kinetic typography","transcript","social","reels"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* for the accent","default":"Bold words with a *gradient* bounce.","maxLength":600},"accents":{"type":"text","label":"Accent words","description":"words to put in the accent wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["center","bottom","top"],"default":"center"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"case":{"type":"choice","label":"Case","options":["upper","as typed","lower","style"],"default":"upper"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01},"ticks":{"type":"boolean","label":"Sound on every word","default":false},"direction":{"type":"choice","label":"Gradient","options":["across","down","diagonal"],"default":"across"}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/gradient-fill","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// Bold words filled with a gradient of the brand's two accents, each bouncing in on its time.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const C = captionLines($, A, ws, t, { size: (A.tall ? 88 : A.wide ? 72 : 80) * k * sizeOf(p.size), font: { ls: -0.02 }, position: p.position, lines: 2, maxWords: 6, lh: 1.08 });\n  if (!C) return \"\";\n  const f = C.font;\n  const angle = { across: 90, down: 180, diagonal: 135 }[p.direction] || 90;\n  let s = \"\";\n  C.lay.words.forEach(function (u, j) {\n    const since = t - (u.s - 0.04);\n    if (since < 0) return;\n    // An elastic land: in from small, past full size, back, settled in a third of a second.\n    const x = $.clamp(since / 0.45);\n    const el = x >= 1 ? 1 : 1 - Math.pow(2, -9 * x) * Math.cos(x * Math.PI * 3.2);\n    const fill = u.accent ? $.gradient([c.accent2, \"#FFFFFF\"], { angle: angle }) : $.gradient([c.accent, c.accent2], { angle: angle });\n    const word = $.text(u.w, Object.assign({}, f, { x: u.x, y: u.y, fill: fill, op: $.clamp(since / 0.06) }));\n    s += $.g({ transform: $.tf({ s: Math.max(0.01, 0.4 + 0.6 * el), r: (1 - $.clamp(since / 0.3)) * (j % 2 ? 4 : -4), ox: u.x + u.ww / 2, oy: u.y + f.size / 2 }) }, word);\n  });\n  s = $.g({ extra: 'filter=\"url(#mg-md)\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.2, outDur: 0.18, vary: false }, s);\n}\n\nfunction sound($) {\n  return $.data.ticks ? wordCues(captionWords($, $.data, $.beat(\"in\", 0.05))) : [];\n}\n","libs":[]},{"id":"wipe-on","name":"Wipe captions","description":"Each word wiped on from the left as it is said, an accent edge leading the reveal; the words still to come can wait dimmed. Give a transcript with --words.","category":"captions","tags":["captions","wipe","reveal","clean","transcript","corporate"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* for the accent","default":"Clean words that wipe on as they are *said*.","maxLength":600},"accents":{"type":"text","label":"Accent words","description":"words to put in the accent wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["bottom","center","top"],"default":"bottom"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"case":{"type":"choice","label":"Case","options":["as typed","upper","lower","style"],"default":"as typed"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01},"upcoming":{"type":"choice","label":"Words not said yet","options":["hidden","dim"],"default":"hidden"}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/wipe-on","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// Each word is wiped on from the left as it is said, an accent edge leading the reveal.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const C = captionLines($, A, ws, t, { size: (A.tall ? 76 : A.wide ? 62 : 68) * k * sizeOf(p.size), position: p.position, lh: 1.2 });\n  if (!C) return \"\";\n  const f = C.font;\n  let s = \"\";\n  C.lay.words.forEach(function (u) {\n    const since = t - (u.s - 0.02);\n    const fill = u.accent ? c.accent : \"#FFFFFF\";\n    if (since < 0) {\n      if (p.upcoming === \"dim\") s += $.text(u.w, Object.assign({}, f, { x: u.x, y: u.y, fill: \"#FFFFFF\", op: 0.3 }));\n      return;\n    }\n    const e = $.E.inOut3($.clamp(since / Math.max(0.14, Math.min(0.34, (u.e - u.s) * 0.9))));\n    const pad = f.size * 0.08, w = (u.ww + pad * 2) * e;\n    s += $.clip(u.x - pad, u.y - f.size * 0.25, w, f.size * 1.5, 0, $.text(u.w, Object.assign({}, f, { x: u.x, y: u.y, fill: fill })));\n    if (e < 1) s += $.rect(u.x - pad + w - f.size * 0.03, u.y - f.size * 0.06, f.size * 0.07, f.size * 1.12, { r: f.size * 0.03, fill: c.accent });\n  });\n  s = $.g({ extra: 'filter=\"url(#mg-sm)\"' }, groupIn($, C.g, t, s));\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, s);\n}\n","libs":[]},{"id":"rgb-split","name":"Glitch captions","description":"Each word lands as a broken signal — split into red and cyan, jittering, a slice torn sideways — then locks clean; accent words flare again now and then. Give a transcript with --words.","category":"captions","tags":["captions","glitch","rgb split","tech","gaming","transcript"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* for the accent","default":"Signal lost. Signal *found.*","maxLength":600},"accents":{"type":"text","label":"Accent words","description":"words to put in the accent wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["center","bottom","top"],"default":"center"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"case":{"type":"choice","label":"Case","options":["upper","as typed","lower","style"],"default":"upper"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01},"ticks":{"type":"boolean","label":"Sound on every word","default":false},"intensity":{"type":"choice","label":"Glitch","options":["standard","subtle","heavy"],"default":"standard"}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/rgb-split","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// Each word lands as a broken signal — split into red and cyan, jittering, a slice torn sideways — then locks clean.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const C = captionLines($, A, ws, t, { size: (A.tall ? 80 : A.wide ? 64 : 72) * k * sizeOf(p.size), font: { ls: 0 }, position: p.position, lh: 1.18 });\n  if (!C) return \"\";\n  const f = C.font, fr = Math.floor(t * 30);\n  const amount = { subtle: 0.5, standard: 1, heavy: 1.7 }[p.intensity] || 1;\n  let s = \"\";\n  C.lay.words.forEach(function (u, j) {\n    const since = t - (u.s - 0.03);\n    if (since < 0) return;\n    // Glitch strength: high as the word lands, gone after a quarter second (accent words flare again now and then).\n    let g = Math.max(0, 1 - since / 0.26);\n    if (u.accent && since > 0.5 && $.hash(Math.floor(t * 6) * 3.1 + j) > 0.86) g = 0.6;\n    g *= amount;\n    const fill = u.accent ? c.accent : \"#FFFFFF\";\n    const at = function (dx, dy, col, op) { return $.text(u.w, Object.assign({}, f, { x: u.x + dx, y: u.y + dy, fill: col, op: op })); };\n    if (g <= 0.01) { s += at(0, 0, fill, 1); return; }\n    const jx = ($.hash(fr * 3.3 + j) - 0.5) * 26 * k * g, jy = ($.hash(fr * 5.7 + j) - 0.5) * 8 * k * g;\n    const split = (5 + 16 * g) * k;\n    s += at(jx - split, jy, \"#FF2D55\", 0.85) + at(jx + split, jy, \"#22E4FF\", 0.85);\n    if ($.hash(fr * 9.1 + j) > 0.18 * g) s += at(jx, jy, fill, 1);\n    // A torn slice: one band of the word shoved sideways.\n    const band = u.y + f.size * (0.1 + 0.7 * $.hash(fr * 2.9 + j)), bh = f.size * 0.18;\n    s += $.clip(u.x - f.size, band, u.ww + f.size * 2, bh, 0, at(jx + ($.hash(fr * 7.7 + j) - 0.5) * 60 * k * g, jy, fill, 1));\n  });\n  s = $.g({ extra: 'filter=\"url(#mg-sm)\"' }, groupIn($, C.g, t, s));\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, s);\n}\n\nfunction sound($) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  return $.data.ticks ? ws.map(function (w) { return { at: w.s, kind: \"zap\", level: 0.45 }; }) : [];\n}\n","libs":[]},{"id":"burst","name":"Burst captions","description":"Words pop in on their time and the key words go off with a burst of particles in the brand's colours — the *starred* or listed words, else the last word of each phrase. Give a transcript with --words.","category":"captions","tags":["captions","particles","burst","celebration","hook","transcript","reels"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* for the accent","default":"This is going to be *huge.*","maxLength":600},"accents":{"type":"text","label":"Accent words","description":"words to put in the accent wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["center","bottom","top"],"default":"center"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"case":{"type":"choice","label":"Case","options":["upper","as typed","lower","style"],"default":"upper"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/burst","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// Words pop in on their time; the key words go off with a burst of particles in the brand's colours.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const C = captionLines($, A, ws, t, { size: (A.tall ? 84 : A.wide ? 68 : 76) * k * sizeOf(p.size), font: { ls: -0.02 }, position: p.position, lines: 2, maxWords: 6, lh: 1.14 });\n  if (!C) return \"\";\n  const f = C.font;\n  // Without marked words, the last word of each group bursts.\n  const marked = C.lay.words.some(function (u) { return u.accent; });\n  let back = \"\", front = \"\";\n  C.lay.words.forEach(function (u, j) {\n    const since = t - (u.s - 0.03);\n    if (since < 0) return;\n    const key = marked ? u.accent : j === C.lay.words.length - 1;\n    const cx = u.x + u.ww / 2, cy = u.y + f.size / 2;\n    if (key) back += $.burst(t, u.s, cx, cy, { n: 18, r: f.size * 1.7 + u.ww * 0.4, size: f.size * 0.11, colors: [c.accent, c.accent2, \"#FFFFFF\"], dur: 0.75 });\n    const sc = 1 + (key ? 0.5 : 0.3) * (1 - settle(since / 0.22));\n    front += $.g({ transform: $.tf({ s: sc, ox: cx, oy: cy }) }, $.text(u.w, Object.assign({}, f, { x: u.x, y: u.y, fill: key ? c.accent : \"#FFFFFF\", op: $.clamp(since / 0.05) })));\n  });\n  const s = back + $.g({ extra: 'filter=\"url(#mg-sm)\"' }, front);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.2, outDur: 0.18, vary: false }, s);\n}\n\nfunction sound($) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  return ws.filter(function (w) { return w.accent; }).map(function (w) { return { at: w.s, kind: \"pop\", level: 0.7 }; });\n}\n","libs":[]},{"id":"boxes","name":"Boxed captions","description":"Every word on its own block, dropped in at a slight tilt as it is said — white blocks, dark blocks or the brand's colour, accent words on the accent. Give a transcript with --words.","category":"captions","tags":["captions","boxes","blocks","social","transcript","reels","shorts"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* for the accent","default":"Every word gets its *own* block.","maxLength":600},"accents":{"type":"text","label":"Accent words","description":"words to put in the accent wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["center","bottom","top"],"default":"center"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"case":{"type":"choice","label":"Case","options":["upper","as typed","lower","style"],"default":"upper"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01},"ticks":{"type":"boolean","label":"Sound on every word","default":false},"look":{"type":"choice","label":"Blocks","options":["light","dark","brand"],"default":"light"}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/boxes","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// Every word on its own block, dropped in at a slight tilt as it is said — light, dark or in the brand's colour.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const C = captionLines($, A, ws, t, { size: (A.tall ? 70 : A.wide ? 58 : 64) * k * sizeOf(p.size), font: { ls: -0.01 }, position: p.position, lines: 2, maxWords: 6, charW: 0.68, lh: 1.42 });\n  if (!C) return \"\";\n  const f = C.font;\n  const looks = { light: [\"#FFFFFF\", \"#111111\"], dark: [\"#111111\", \"#FFFFFF\"], brand: [c.accent, $.on(c.accent)] };\n  const look = looks[p.look] || looks.light;\n  let s = \"\";\n  C.lay.words.forEach(function (u, j) {\n    const since = t - (u.s - 0.03);\n    if (since < 0) return;\n    const bg = u.accent ? (p.look === \"brand\" ? c.accent2 : c.accent) : look[0];\n    const ink = u.accent ? $.on(bg) : look[1];\n    const e = settle(since / 0.22);\n    const tilt = ($.hash(j * 4.3 + C.g.k) - 0.5) * 5;\n    const px = f.size * 0.13, py = f.size * 0.12;\n    const box = $.rect(u.x - px, u.y - py, u.ww + px * 2, f.size + py * 2, { r: f.size * 0.08, fill: bg }) + $.text(u.w, Object.assign({}, f, { x: u.x, y: u.y, fill: ink }));\n    s += $.g({ op: $.clamp(since / 0.06), transform: $.tf({ y: -(1 - e) * f.size * 0.7, r: tilt * (0.4 + 0.6 * e), ox: u.x + u.ww / 2, oy: u.y + f.size / 2 }) }, box);\n  });\n  s = $.g({ extra: 'filter=\"url(#mg-sm)\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.2, outDur: 0.18, vary: false }, s);\n}\n\nfunction sound($) {\n  return $.data.ticks ? wordCues(captionWords($, $.data, $.beat(\"in\", 0.05))) : [];\n}\n","libs":[]},{"id":"sticker","name":"Sticker captions","description":"Words as stickers: bold letters in a thick white die-cut border with a shadow, slapped on at a tilt as they are said. Give a transcript with --words.","category":"captions","tags":["captions","sticker","playful","social","transcript","reels"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* for the accent","default":"Slap a *sticker* on every word.","maxLength":600},"accents":{"type":"text","label":"Accent words","description":"words to put in the accent wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["center","bottom","top"],"default":"center"},"size":{"type":"choice","label":"Size","options":["medium","small","large","huge"],"default":"medium"},"case":{"type":"choice","label":"Case","options":["upper","as typed","lower","style"],"default":"upper"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01},"ticks":{"type":"boolean","label":"Sound on every word","default":false},"color":{"type":"choice","label":"Letters","options":["accent","ink","mixed"],"default":"accent","description":"accent: the brand's colour · ink: near black · mixed: the two accents and black in turn"}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/sticker","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// Words as stickers: bold letters in a thick white die-cut border, slapped on at a tilt as they are said.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const C = captionLines($, A, ws, t, { size: (A.tall ? 92 : A.wide ? 72 : 82) * k * sizeOf(p.size), font: { ls: -0.01 }, position: p.position, lines: 2, maxWords: 5, charW: 0.66, lh: 1.12 });\n  if (!C) return \"\";\n  const f = C.font;\n  const palette = p.color === \"ink\" ? [\"#151515\"] : p.color === \"mixed\" ? [c.accent, c.accent2, \"#151515\"] : [c.accent];\n  const groupTilt = ($.hash(C.g.k * 2.3) - 0.5) * 7;\n  let s = \"\";\n  C.lay.words.forEach(function (u, j) {\n    const since = t - (u.s - 0.03);\n    if (since < 0) return;\n    const fill = u.accent ? c.accent2 : palette[j % palette.length];\n    const e = settle(since / 0.2);\n    const cx = u.x + u.ww / 2, cy = u.y + f.size / 2;\n    const word = $.text(u.w, Object.assign({}, f, { x: u.x, y: u.y, fill: fill, stroke: \"#FFFFFF\", sw: f.size * 0.2 }));\n    s += $.g({ op: $.clamp(since / 0.04), transform: $.tf({ s: 1 + 0.55 * (1 - e), r: (1 - e) * (j % 2 ? 9 : -9), ox: cx, oy: cy }) }, word);\n  });\n  s = $.g({ transform: $.tf({ r: groupTilt, ox: $.W / 2, oy: captionY($, A, p.position) }) }, s);\n  s = $.g({ extra: 'filter=\"url(#mg-md)\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.2, outDur: 0.18, vary: false }, s);\n}\n\nfunction sound($) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  return $.data.ticks ? ws.map(function (w) { return { at: w.s, kind: \"stamp\", level: 0.5 }; }) : [];\n}\n","libs":[]},{"id":"focus-pull","name":"Focus captions","description":"The whole line on screen with the word being said pulled into sharp focus while the rest sits soft and dim around it. Give a transcript with --words.","category":"captions","tags":["captions","focus","blur","cinematic","transcript","interview"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* for the accent","default":"Only the word being said is in *focus.*","maxLength":600},"accents":{"type":"text","label":"Accent words","description":"words to put in the accent wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["bottom","center","top"],"default":"bottom"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"case":{"type":"choice","label":"Case","options":["as typed","upper","lower","style"],"default":"as typed"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01},"depth":{"type":"choice","label":"Blur","options":["standard","light","deep"],"default":"standard"}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/focus-pull","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// The whole line on screen, the word being said in sharp focus; the rest sits soft and dim around it.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const C = captionLines($, A, ws, t, { size: (A.tall ? 74 : A.wide ? 62 : 68) * k * sizeOf(p.size), position: p.position, lh: 1.2 });\n  if (!C) return \"\";\n  const f = C.font, blurs = {};\n  let ci = -1;\n  C.lay.words.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const soft = { light: 2.5, standard: 5, deep: 9 }[p.depth] || 5;\n  let s = \"\";\n  C.lay.words.forEach(function (u, j) {\n    // Focus pulls in as the word starts and lets go as the next one takes it.\n    const cur = j === ci;\n    const pull = cur ? $.E.out3($.clamp((t - u.s + 0.03) / 0.16)) : 0;\n    const said = j < ci;\n    const blur = blurFilter($, blurs, (said ? soft * 0.45 : soft) * k * (1 - pull));\n    const op = cur ? 0.55 + 0.45 * pull : said ? 0.62 : 0.34;\n    const word = $.text(u.w, Object.assign({}, f, { x: u.x, y: u.y, fill: u.accent ? c.accent : \"#FFFFFF\", op: op }));\n    const sc = 1 + 0.05 * pull;\n    const g = sc === 1 ? word : $.g({ transform: $.tf({ s: sc, ox: u.x + u.ww / 2, oy: u.y + f.size / 2 }) }, word);\n    s += blur ? $.g({ extra: blur }, g) : g;\n  });\n  s = $.g({ extra: 'filter=\"url(#mg-sm)\"' }, groupIn($, C.g, t, s));\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, s);\n}\n","libs":[]},{"id":"wave","name":"Wave captions","description":"Letters rise into place one after another as each word is said, and the line keeps riding a gentle wave. Give a transcript with --words.","category":"captions","tags":["captions","wave","letters","playful","music","transcript"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* for the accent","default":"Letters riding a gentle *wave.*","maxLength":600},"accents":{"type":"text","label":"Accent words","description":"words to put in the accent wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["center","bottom","top"],"default":"center"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"case":{"type":"choice","label":"Case","options":["as typed","upper","lower","style"],"default":"as typed"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01},"ticks":{"type":"boolean","label":"Sound on every word","default":false},"wave":{"type":"choice","label":"Wave","options":["standard","calm","big"],"default":"standard"}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/wave","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// Letters rise into place one after another as each word is said, and the line keeps riding a gentle wave.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const C = captionLines($, A, ws, t, { size: (A.tall ? 80 : A.wide ? 66 : 72) * k * sizeOf(p.size), font: { ls: 0 }, position: p.position, lh: 1.24 });\n  if (!C) return \"\";\n  const f = C.font;\n  const amp = ({ calm: 0.035, standard: 0.07, big: 0.13 }[p.wave] || 0.07) * f.size;\n  let s = \"\";\n  C.lay.words.forEach(function (u) {\n    const since = t - (u.s - 0.04);\n    if (since < 0) return;\n    const chars = Array.from(u.w);\n    const fill = u.accent ? c.accent : \"#FFFFFF\";\n    chars.forEach(function (ch, i) {\n      const x = u.x + $.measure(chars.slice(0, i).join(\"\"), f);\n      const ls = since - i * 0.028;\n      if (ls < 0) return;\n      const rise = (1 - settle(ls / 0.3)) * f.size * 0.55;\n      const swell = Math.sin(t * 4.2 - (x / f.size) * 0.85) * amp * $.clamp(ls / 0.4);\n      s += $.text(ch, Object.assign({}, f, { x: x, y: u.y + rise + swell, fill: fill, op: $.clamp(ls / 0.08) }));\n    });\n  });\n  s = $.g({ extra: 'filter=\"url(#mg-sm)\"' }, groupIn($, C.g, t, s));\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, s);\n}\n\nfunction sound($) {\n  return $.data.ticks ? wordCues(captionWords($, $.data, $.beat(\"in\", 0.05))) : [];\n}\n","libs":[]},{"id":"outline","name":"Outline captions","description":"The line hangs as hollow outlines and each word fills solid from the left as it is said. Give a transcript with --words.","category":"captions","tags":["captions","outline","stroke","fill","transcript","social"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* for the accent","default":"Hollow words that fill as you *speak.*","maxLength":600},"accents":{"type":"text","label":"Accent words","description":"words to put in the accent wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["center","bottom","top"],"default":"center"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"case":{"type":"choice","label":"Case","options":["upper","as typed","lower","style"],"default":"upper"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01},"fill":{"type":"choice","label":"Fill","options":["white","accent"],"default":"white"}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/outline","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// The line hangs as hollow outlines; each word fills solid from the left as it is said.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const C = captionLines($, A, ws, t, { size: (A.tall ? 86 : A.wide ? 70 : 78) * k * sizeOf(p.size), font: { ls: -0.01 }, position: p.position, lh: 1.12 });\n  if (!C) return \"\";\n  const f = C.font;\n  let s = \"\";\n  C.lay.words.forEach(function (u) {\n    const fill = u.accent ? c.accent : p.fill === \"accent\" ? c.accent : \"#FFFFFF\";\n    const line = u.accent ? c.accent : \"#FFFFFF\";\n    s += $.text(u.w, Object.assign({}, f, { x: u.x, y: u.y, fill: \"none\", stroke: line, sw: Math.max(1.5, f.size * 0.03), op: 0.85 }));\n    const since = t - (u.s - 0.02);\n    if (since < 0) return;\n    const e = $.E.out3($.clamp(since / Math.max(0.12, Math.min(0.32, (u.e - u.s) * 0.85))));\n    const pad = f.size * 0.06;\n    s += $.clip(u.x - pad, u.y - f.size * 0.25, (u.ww + pad * 2) * e, f.size * 1.5, 0, $.text(u.w, Object.assign({}, f, { x: u.x, y: u.y, fill: fill })));\n  });\n  s = $.g({ extra: 'filter=\"url(#mg-sm)\"' }, groupIn($, C.g, t, s));\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, s);\n}\n","libs":[]},{"id":"bubble","name":"Speech-bubble captions","description":"Each phrase pops up in a speech bubble with a tail, its words appearing as they are said; bubbles alternate sides like a conversation, or stay on one side. Give a transcript with --words.","category":"captions","tags":["captions","speech bubble","chat","dialogue","podcast","transcript"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* for the accent","default":"Hey! Did you see this? It's *wild.*","maxLength":600},"accents":{"type":"text","label":"Accent words","description":"words to put in the accent wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["bottom","center","top"],"default":"bottom"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"case":{"type":"choice","label":"Case","options":["as typed","upper","lower","style"],"default":"as typed"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01},"side":{"type":"choice","label":"Side","options":["alternate","left","right"],"default":"alternate"},"look":{"type":"choice","label":"Bubble","options":["light","dark","brand"],"default":"light"},"upcoming":{"type":"choice","label":"Words not said yet","options":["hidden","dim"],"default":"hidden"}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/bubble","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// Speech bubbles: each phrase pops up in a bubble with a tail, its words appearing as they are said; bubbles can alternate sides like a conversation.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? 60 : A.wide ? 50 : 54) * k * sizeOf(p.size);\n  const font = Object.assign({}, $.font(\"text\"), { weight: 700, size: size });\n  const maxW = A.w * (A.tall ? 0.7 : 0.5);\n  const g = groupAt(phrases(ws, Math.floor(maxW / (size * 0.56)) * 2, 10), t, 0.8);\n  if (!g) return \"\";\n  const side = p.side === \"right\" ? 1 : p.side === \"left\" ? -1 : g.k % 2 ? 1 : -1;\n  const probe = lineUp($, g.words, font, maxW, 0, 0, 1.22);\n  const bw = Math.max.apply(null, probe.widths) + size * 1.2, bh = probe.h + size * 0.9;\n  const cy = captionY($, A, p.position);\n  const bx = side < 0 ? A.x + A.w * 0.04 : A.x + A.w * 0.96 - bw, by = cy - bh / 2;\n  const lay = lineUp($, g.words, font, maxW, bx + bw / 2, by + size * 0.45, 1.22);\n  const looks = { light: [\"#FFFFFF\", \"#141414\"], dark: [\"#141414\", \"#FFFFFF\"], brand: [c.accent, $.on(c.accent)] };\n  const look = looks[p.look] || looks.light;\n  const r = size * 0.55;\n  // The tail hangs from the bubble's lower corner on its side.\n  const tx = side < 0 ? bx + r * 1.2 : bx + bw - r * 1.2, ty = by + bh;\n  const tail = \"M\" + (tx - side * size * 0.1) + \" \" + (ty - 2) + \" L\" + (tx + side * size * 0.45) + \" \" + (ty - 2) + \" L\" + (tx - side * size * 0.35) + \" \" + (ty + size * 0.62) + \" Z\";\n  let s = $.rect(bx, by, bw, bh, { r: r, fill: look[0] }) + $.path(tail, { fill: look[0] });\n  lay.words.forEach(function (u) {\n    const since = t - (u.s - 0.03);\n    if (since < 0 && p.upcoming !== \"dim\") return;\n    s += $.text(u.w, Object.assign({}, font, { x: u.x, y: u.y, fill: u.accent ? (p.look === \"brand\" ? c.accent2 : c.accent) : look[1], op: since < 0 ? 0.3 : $.clamp(since / 0.08) }));\n  });\n  // The bubble pops from its tail.\n  const e = settle((t - g.start) / 0.28);\n  s = $.g({ op: $.clamp((t - g.start) / 0.06), transform: $.tf({ s: Math.max(0.01, e), ox: tx, oy: ty + size * 0.6 }) }, s);\n  s = $.g({ extra: 'filter=\"url(#mg-md)\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, s);\n}\n\nfunction sound($) {\n  const p = $.data, ws = captionWords($, p, $.beat(\"in\", 0.05));\n  const out = [];\n  ws.forEach(function (w, i) { if (i === 0 || w.s - ws[i - 1].end > 0.45 || /[.!?]$/.test(ws[i - 1].w)) out.push({ at: w.s - 0.05, kind: \"pop\", level: 0.55 }); });\n  return out;\n}\n","libs":[]},{"id":"crawl","name":"Ticker captions","description":"A news crawl: the whole transcript on one line in a dark band, sliding so the word being said sits at the reading point, lit in the accent, with an optional label tab. Give a transcript with --words.","category":"captions","tags":["captions","ticker","crawl","news","broadcast","live","transcript"],"placement":"overlay","duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* for the accent","default":"Breaking: captions now follow every word you say, right on time.","maxLength":600},"accents":{"type":"text","label":"Accent words","description":"words to put in the accent wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["bottom","top"],"default":"bottom"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"case":{"type":"choice","label":"Case","options":["as typed","upper","lower","style"],"default":"as typed"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01},"label":{"type":"text","label":"Label","description":"a tab on the left of the band, e.g. LIVE; empty for none","default":"LIVE","maxLength":16}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/crawl","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// A news crawl: the whole transcript on one line in a band, sliding so the word being said sits at the reading point, lit in the accent.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? 54 : A.wide ? 46 : 50) * k * sizeOf(p.size);\n  const font = Object.assign({}, $.font(\"display\"), { size: size, ls: 0 });\n  const line = lineUp($, ws, font, 1e9, 0, 0, 1);\n  const lay = line.words.map(function (u) { return Object.assign({}, u, { x: u.x + line.widths[0] / 2 }); });\n  const bandH = size * 1.9;\n  const by = p.position === \"top\" ? H * (A.tall ? 0.12 : 0.08) : H * (A.tall ? 0.74 : 0.86) - bandH / 2;\n  const label = String(p.label || \"\").trim();\n  const labelW = label ? $.measure(label.toUpperCase(), Object.assign({}, font, { size: size * 0.62 })) + size * 1.2 : 0;\n  // The reading point: where each word starts as it is said, with room before it for the words just gone by.\n  const readX = Math.max(labelW + size * 4, W * (A.tall ? 0.42 : 0.34));\n  // The line slides from word to word: each word's start reaches the reading point as it is said.\n  let i = 0;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.05) i = j; });\n  const a = lay[i], b = lay[Math.min(lay.length - 1, i + 1)];\n  const e = b === a ? 0 : $.E.inOut3($.clamp((t - a.s) / Math.max(0.1, b.s - a.s)));\n  const offset = readX - $.lerp(a.x, b.x, e);\n  let words = \"\";\n  lay.forEach(function (u, j) {\n    const x = u.x + offset;\n    if (x > W + size || x + u.ww < labelW - size) return;\n    const said = t >= u.s - 0.05;\n    words += $.text(u.w, Object.assign({}, font, { x: x, y: by + (bandH - size) / 2, fill: j === i && said ? c.accent : u.accent ? c.accent2 : \"#FFFFFF\", op: said ? 1 : 0.55, within: { x: -1e7, y: -1e7, w: 2e7, h: 2e7 } }));\n  });\n  let s = $.rect(0, by, W, bandH, { fill: \"#000000\", op: 0.62 });\n  s += $.clip(labelW, by, W - labelW, bandH, 0, words);\n  if (label) {\n    s += $.rect(0, by, labelW, bandH, { fill: c.accent });\n    s += $.text(label.toUpperCase(), Object.assign({}, font, { size: size * 0.62, x: labelW / 2, y: by + (bandH - size * 0.62) / 2, anchor: \"middle\", fill: $.on(c.accent) }));\n  }\n  const ein = $.E.out3($.clamp((t - (ws[0].s - 0.4)) / 0.35));\n  s = $.g({ op: ein, transform: $.tf({ y: (1 - ein) * bandH * (p.position === \"top\" ? -1 : 1) }) }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.25, outDur: 0.22, vary: false }, s);\n}\n","libs":[]},{"id":"extrude","name":"Extruded captions","description":"Chunky retro 3D: each word pops up out of the frame with a deep extrusion in the brand's colour behind its face. Give a transcript with --words.","category":"captions","tags":["captions","3d","retro","extrude","bold","transcript","reels"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* for the accent","default":"Big bold words with *depth.*","maxLength":600},"accents":{"type":"text","label":"Accent words","description":"words to put in the accent wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["center","bottom","top"],"default":"center"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"case":{"type":"choice","label":"Case","options":["upper","as typed","lower","style"],"default":"upper"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01},"ticks":{"type":"boolean","label":"Sound on every word","default":false},"direction":{"type":"choice","label":"Depth","options":["down-right","down","down-left"],"default":"down-right"}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/extrude","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// Chunky retro 3D: each word pops up out of the frame with a deep extrusion in the brand's colour behind its face.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const C = captionLines($, A, ws, t, { size: (A.tall ? 88 : A.wide ? 72 : 80) * k * sizeOf(p.size), font: { ls: 0 }, position: p.position, lines: 2, maxWords: 6, charW: 0.66, lh: 1.24 });\n  if (!C) return \"\";\n  const f = C.font, steps = 10;\n  const dir = { \"down-right\": [1, 1], down: [0, 1.3], \"down-left\": [-1, 1] }[p.direction] || [1, 1];\n  let s = \"\";\n  C.lay.words.forEach(function (u) {\n    const since = t - (u.s - 0.03);\n    if (since < 0) return;\n    const e = settle(since / 0.28);\n    const depth = f.size * 0.11 * Math.max(0, e);\n    const face = u.accent ? c.accent2 : \"#FFFFFF\", side = u.accent ? c.accent : c.accent;\n    // The face rises off the extrusion as it grows.\n    for (let i = steps; i >= 1; i--) {\n      const d = (depth * i) / steps;\n      s += $.text(u.w, Object.assign({}, f, { x: u.x + dir[0] * d - dir[0] * depth, y: u.y + dir[1] * d - dir[1] * depth, fill: side, op: $.clamp(since / 0.05) * (0.55 + 0.45 * (i / steps)) }));\n    }\n    s += $.text(u.w, Object.assign({}, f, { x: u.x - dir[0] * depth, y: u.y - dir[1] * depth, fill: face, op: $.clamp(since / 0.05) }));\n  });\n  s = $.g({ extra: 'filter=\"url(#mg-md)\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.2, outDur: 0.18, vary: false }, s);\n}\n\nfunction sound($) {\n  return $.data.ticks ? wordCues(captionWords($, $.data, $.beat(\"in\", 0.05))) : [];\n}\n","libs":[]},{"id":"two-tone","name":"Two-tone captions","description":"Each word cut through the middle — its top half white, its bottom half in the accent — the halves sliding together as it is said. Give a transcript with --words.","category":"captions","tags":["captions","two tone","split","fashion","bold","transcript"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* for the accent","default":"Two tones, *one* voice.","maxLength":600},"accents":{"type":"text","label":"Accent words","description":"words to put in the accent wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["center","bottom","top"],"default":"center"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"case":{"type":"choice","label":"Case","options":["upper","as typed","lower","style"],"default":"upper"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01},"colors":{"type":"choice","label":"Colours","options":["white and accent","accents"],"default":"white and accent","description":"white and accent: a white top over an accent bottom · accents: the brand's two accents"}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/two-tone","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// Two-tone: each word cut through the middle, its top half in white and its bottom half in the accent, the halves sliding together as it is said.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const C = captionLines($, A, ws, t, { size: (A.tall ? 90 : A.wide ? 74 : 82) * k * sizeOf(p.size), font: { ls: -0.01 }, position: p.position, lines: 2, maxWords: 6, charW: 0.64, lh: 1.08 });\n  if (!C) return \"\";\n  const f = C.font;\n  const lower = p.colors === \"accents\" ? c.accent2 : c.accent, upper = p.colors === \"accents\" ? c.accent : \"#FFFFFF\";\n  let s = \"\";\n  C.lay.words.forEach(function (u, j) {\n    const since = t - (u.s - 0.03);\n    if (since < 0) return;\n    const e = $.E.out3($.clamp(since / 0.24));\n    const slide = (1 - e) * f.size * 0.6 * (j % 2 ? 1 : -1);\n    const pad = f.size * 0.2, top = u.y - f.size * 0.3, mid = u.y + f.size * 0.5, bottom = u.y + f.size * 1.3;\n    const word = function (fill, dx) { return $.text(u.w, Object.assign({}, f, { x: u.x + dx, y: u.y, fill: fill, op: $.clamp(since / 0.06) })); };\n    s += $.clip(u.x - pad - f.size, top, u.ww + pad * 2 + f.size * 2, mid - top, 0, word(u.accent ? lower : upper, slide));\n    s += $.clip(u.x - pad - f.size, mid, u.ww + pad * 2 + f.size * 2, bottom - mid, 0, word(u.accent ? upper : lower, -slide));\n  });\n  s = $.g({ extra: 'filter=\"url(#mg-sm)\"' }, groupIn($, C.g, t, s));\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.2, outDur: 0.18, vary: false }, s);\n}\n","libs":[]},{"id":"drop","name":"Drop captions","description":"Words fall in from above on their time and bounce to rest on the line, squashing a little as they land. Give a transcript with --words.","category":"captions","tags":["captions","bounce","drop","playful","kids","transcript"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* for the accent","default":"Words that drop in and *bounce.*","maxLength":600},"accents":{"type":"text","label":"Accent words","description":"words to put in the accent wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["center","bottom","top"],"default":"center"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"case":{"type":"choice","label":"Case","options":["upper","as typed","lower","style"],"default":"upper"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01},"ticks":{"type":"boolean","label":"Sound on every word","default":false}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/drop","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// Words fall in from above on their time and bounce to rest on the line, squashing a little as they land.\nfunction bounce(x) {\n  const n = 7.5625, d = 2.75;\n  x = Math.max(0, Math.min(1, x));\n  if (x < 1 / d) return n * x * x;\n  if (x < 2 / d) { x -= 1.5 / d; return n * x * x + 0.75; }\n  if (x < 2.5 / d) { x -= 2.25 / d; return n * x * x + 0.9375; }\n  x -= 2.625 / d;\n  return n * x * x + 0.984375;\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, H = $.H;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const C = captionLines($, A, ws, t, { size: (A.tall ? 82 : A.wide ? 68 : 74) * k * sizeOf(p.size), font: { ls: -0.01 }, position: p.position, lines: 2, maxWords: 7, lh: 1.55 });\n  if (!C) return \"\";\n  const f = C.font;\n  // Fall from a couple of lines up: the first bounce stays below the line above.\n  const fall = Math.min(H * 0.16, f.size * 2.4);\n  // Each line's words fall within the space below the line above, so they never cross it.\n  const tops = [];\n  C.lay.words.forEach(function (u) { tops[u.line] = tops[u.line] === undefined ? u.y : Math.min(tops[u.line], u.y); });\n  let s = \"\";\n  C.lay.words.forEach(function (u) {\n    const since = t - (u.s - 0.14);\n    if (since < 0) return;\n    const x = $.clamp(since / 0.62);\n    const y = -(1 - bounce(x)) * fall;\n    // A squash at each contact with the line, smaller with every bounce.\n    const sq = 1 - [[0.364, 0.16], [0.727, 0.08], [0.909, 0.04]].reduce(function (a, h) { return a + h[1] * Math.exp(-Math.pow((x - h[0]) / 0.035, 2)); }, 0);\n    const cx = u.x + u.ww / 2, base = u.y + f.size;\n    const word = $.g({ transform: $.tf({ y: y, sx: 2 - sq, sy: sq, ox: cx, oy: base }) }, $.text(u.w, Object.assign({}, f, { x: u.x, y: u.y, fill: u.accent ? c.accent : \"#FFFFFF\", op: $.clamp(since / 0.12) })));\n    const top = u.line ? tops[u.line - 1] + f.size * 1.08 : tops[0] - fall - f.size;\n    s += $.clip(0, top, $.W, tops[u.line] + f.size * 1.35 - top, 0, word);\n  });\n  s = $.g({ extra: 'filter=\"url(#mg-sm)\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.2, outDur: 0.18, vary: false }, s);\n}\n\nfunction sound($) {\n  return $.data.ticks ? captionWords($, $.data, $.beat(\"in\", 0.05)).map(function (w) { return { at: w.s + 0.08, kind: \"pop\", level: 0.45 }; }) : [];\n}\n","libs":[]},{"id":"punch-in","name":"Punch-in captions","description":"Each phrase punches in from far too close out of a blur, then holds with a slow push while its words light up as they are said, the word being said in the accent. Give a transcript with --words.","category":"captions","tags":["captions","zoom","punch in","kinetic typography","hook","transcript","reels"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* for the accent","default":"Every phrase *punches* straight in.","maxLength":600},"accents":{"type":"text","label":"Accent words","description":"words to put in the accent wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["center","bottom","top"],"default":"center"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"case":{"type":"choice","label":"Case","options":["upper","as typed","lower","style"],"default":"upper"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/punch-in","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// Each phrase punches in from far too close, out of a blur, then holds with a slow push while its words light up as they are said.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const groups = phrases(ws, A.tall ? 18 : 28, 5);\n  const g = groupAt(groups, t, 0.6);\n  if (!g) return \"\";\n  const line = g.words.map(function (w) { return w.w; }).join(\" \");\n  const f = fitFont($, line, \"display\", (A.tall ? 130 : A.wide ? 110 : 120) * k * sizeOf(p.size), A.w * 0.9, { ls: -0.02 });\n  const cy = captionY($, A, p.position);\n  const lay = lineUp($, g.words, f, A.w * 0.92, W / 2, cy - f.size / 2, 1.05);\n  const since = t - g.start;\n  const e = 1 - Math.pow(2, -10 * $.clamp(since / 0.32));\n  const sc = 2.6 - 1.6 * e + 0.035 * $.clamp((since - 0.32) / Math.max(0.5, g.end - g.start));\n  const blur = blurFilter($, {}, (1 - e) * 16 * k);\n  let ci = -1;\n  lay.words.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  let s = \"\";\n  lay.words.forEach(function (u, j) {\n    const fill = j === ci ? c.accent : u.accent ? c.accent2 : \"#FFFFFF\";\n    s += $.text(u.w, Object.assign({}, f, { x: u.x, y: u.y, fill: fill, op: j <= ci ? 1 : 0.4 }));\n  });\n  s = $.g({ op: $.clamp(since / 0.1), transform: $.tf({ s: sc, ox: W / 2, oy: cy }) }, s);\n  if (blur) s = $.g({ extra: blur }, s);\n  s = $.g({ extra: 'filter=\"url(#mg-md)\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.2, outDur: 0.18, vary: false }, s);\n}\n\nfunction sound($) {\n  const groups = phrases(captionWords($, $.data, $.beat(\"in\", 0.05)), 22, 5);\n  return groups.map(function (g) { return { at: g[0].s - 0.08, kind: \"whoosh\", level: 0.4, pitch: 3, dur: 0.3 }; });\n}\n","libs":[]},{"id":"flipboard","name":"Flipboard captions","description":"An airport departures board: each word's letters sit on dark tiles that flip through the alphabet and settle one after another as it is said. Give a transcript with --words.","category":"captions","tags":["captions","split flap","flipboard","airport","retro","transcript"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* for the accent","default":"Now boarding: every *word* on time.","maxLength":600},"accents":{"type":"text","label":"Accent words","description":"words to put in the accent wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["bottom","center","top"],"default":"bottom"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"case":{"type":"choice","label":"Case","options":["upper","as typed","lower","style"],"default":"upper"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01},"ticks":{"type":"boolean","label":"Sound on every word","default":false}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/flipboard","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// An airport board: each word's letters sit on dark tiles that flip through the alphabet and settle, one after another, as it is said.\nconst FLAPS = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\";\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const C = captionLines($, A, ws, t, { role: \"mono\", size: (A.tall ? 62 : A.wide ? 52 : 56) * k * sizeOf(p.size), font: { weight: 700 }, position: p.position, charW: 0.75, width: A.tall ? 0.94 : 0.82, lh: 1.5 });\n  if (!C) return \"\";\n  const f = C.font, fr = Math.floor(t * 30);\n  // One tile per letter, as wide as a mono letter, so words keep the gaps the line gave them.\n  const adv = $.measure(\"M\", f), tw = adv, th = f.size * 1.3;\n  let s = \"\";\n  C.lay.words.forEach(function (u, j) {\n    const since = t - (u.s - 0.06);\n    if (since < 0) return;\n    const chars = Array.from(u.w);\n    const ink = u.accent ? c.accent : \"#FFFFFF\";\n    const x0 = u.x;\n    chars.forEach(function (ch, i) {\n      const x = x0 + i * tw, y = u.y - (th - f.size) / 2;\n      const done = since >= 0.08 + i * 0.05;\n      const flap = done ? ch : FLAPS.charAt(Math.floor($.hash(Math.floor(fr / 2) * 3.1 + i * 7.7 + j * 1.3) * FLAPS.length));\n      const sy = done ? 1 : Math.max(0.15, Math.abs(Math.cos(Math.PI * ((t * 14 + i * 0.3) % 1))));\n      s += $.rect(x + adv * 0.04, y, tw - adv * 0.08, th, { r: 4 * k, fill: \"#16181C\" });\n      s += $.g({ transform: $.tf({ sy: sy, ox: x + tw / 2, oy: y + th / 2 }) }, $.text(flap, Object.assign({}, f, { x: x + tw / 2, y: u.y, anchor: \"middle\", fill: done ? ink : \"#C9CED6\", within: { x: -1e6, y: -1e6, w: 2e6, h: 2e6 } })));\n      s += $.rect(x + adv * 0.04, y + th / 2 - 1, tw - adv * 0.08, 2, { fill: \"#000000\", op: 0.7 });\n    });\n  });\n  s = $.g({ extra: 'filter=\"url(#mg-sm)\"' }, groupIn($, C.g, t, s));\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, s);\n}\n\nfunction sound($) {\n  return $.data.ticks ? captionWords($, $.data, $.beat(\"in\", 0.05)).map(function (w) { return { at: w.s, kind: \"click\", level: 0.5 }; }) : [];\n}\n","libs":[]},{"id":"progress","name":"Progress captions","description":"The phrase on screen with a thin bar under it that fills with the voice, a glowing head at its end, the words lighting up as they are said. Give a transcript with --words.","category":"captions","tags":["captions","progress","subtitles","clean","podcast","transcript"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* for the accent","default":"A thin bar that fills as the *voice* goes on.","maxLength":600},"accents":{"type":"text","label":"Accent words","description":"words to put in the accent wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["bottom","center","top"],"default":"bottom"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"case":{"type":"choice","label":"Case","options":["as typed","upper","lower","style"],"default":"as typed"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/progress","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// The phrase on screen with a thin bar under it that fills with the voice; the words light up as they are said.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const C = captionLines($, A, ws, t, { size: (A.tall ? 70 : A.wide ? 58 : 64) * k * sizeOf(p.size), position: p.position, lh: 1.2 });\n  if (!C) return \"\";\n  const f = C.font, words = C.g.words;\n  const first = words[0], last = words[words.length - 1];\n  const share = $.clamp((t - first.s) / Math.max(0.2, last.end - first.s));\n  const bw = Math.max.apply(null, C.lay.widths), bx = W / 2 - bw / 2;\n  const by = C.lay.words[C.lay.words.length - 1].y + f.size * 1.32, bh = Math.max(4, f.size * 0.08);\n  let s = $.rect(bx, by, bw, bh, { r: bh / 2, fill: \"#FFFFFF\", op: 0.22 });\n  s += $.rect(bx, by, Math.max(bh, bw * share), bh, { r: bh / 2, fill: c.accent });\n  s += $.circle(bx + Math.max(bh / 2, bw * share), by + bh / 2, bh * 1.3, { fill: c.accent });\n  C.lay.words.forEach(function (u) {\n    const said = t >= u.s - 0.03;\n    s += $.text(u.w, Object.assign({}, f, { x: u.x, y: u.y, fill: said && u.accent ? c.accent : \"#FFFFFF\", op: said ? 1 : 0.4 }));\n  });\n  s = $.g({ extra: 'filter=\"url(#mg-sm)\"' }, groupIn($, C.g, t, s));\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, s);\n}\n","libs":[]},{"id":"lyrics","name":"Lyrics captions","description":"Lyrics as a music app shows them: the line being sung big in the middle, the ones before and after small and dim around it, the view scrolling as the song moves on and the sung words brightening. Give a transcript with --words.","category":"captions","tags":["captions","lyrics","music","karaoke","song","transcript"],"placement":"overlay","duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* for the accent","default":"Every line you sing scrolls up into view, the one you sing now right in the middle.","maxLength":600},"accents":{"type":"text","label":"Accent words","description":"words to put in the accent wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["center","bottom"],"default":"center"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"case":{"type":"choice","label":"Case","options":["as typed","upper","lower","style"],"default":"as typed"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01},"align":{"type":"choice","label":"Align","options":["left","center"],"default":"left"}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/lyrics","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// Lyrics: the line being sung sits big in the middle, the ones before and after small and dim around it, and the view scrolls as the song moves on.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? 78 : A.wide ? 64 : 70) * k * sizeOf(p.size);\n  const maxW = A.w * 0.9;\n  const groups = phrases(ws, Math.floor(maxW / (size * 0.58)), 7);\n  const g = groupAt(groups, t, 1.5);\n  if (!g) return \"\";\n  // Where the view is: it eases from the line before to this one over 0.4 s.\n  const pos = g.k - 1 + $.E.out3($.clamp((t - g.start) / 0.4));\n  const cy = captionY($, A, p.position === \"bottom\" ? \"bottom\" : \"center\");\n  const left = p.align === \"center\" ? null : A.x + A.w * 0.05;\n  let s = \"\";\n  for (let j = Math.max(0, Math.floor(pos) - 2); j <= Math.min(groups.length - 1, Math.ceil(pos) + 2); j++) {\n    const d = j - pos;\n    const near = Math.max(0, 1 - Math.abs(d));\n    const f = Object.assign({}, $.font(\"display\"), { size: size * (0.66 + 0.34 * near), ls: -0.02 });\n    const op = (0.26 + 0.74 * near) * $.clamp(1.6 - Math.abs(d) * 0.55);\n    if (op <= 0.02) continue;\n    const y = cy + d * size * 1.5 - f.size / 2;\n    const text = groups[j].map(function (w) { return w.w; }).join(\" \");\n    const fit = Math.min(1, maxW / Math.max(1, $.measure(text, f)));\n    f.size *= fit;\n    const lay = lineUp($, groups[j], f, maxW * 2, left === null ? $.W / 2 : left + $.measure(text, f) / 2, y, 1);\n    lay.words.forEach(function (u) {\n      const sung = t >= u.s - 0.03;\n      const wop = j === g.k ? (sung ? 1 : 0.45) : 1;\n      s += $.text(u.w, Object.assign({}, f, { x: u.x, y: u.y, fill: j === g.k && sung && u.accent ? c.accent : \"#FFFFFF\", op: op * wop }));\n    });\n  }\n  s = $.g({ extra: 'filter=\"url(#mg-sm)\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.25, outDur: 0.22, vary: false }, s);\n}\n","libs":[]},{"id":"messages","name":"Message captions","description":"A chat thread: each phrase arrives as a message bubble at the bottom, taking turns left and right (or all from one side), and the thread slides up to make room. Give a transcript with --words.","category":"captions","tags":["captions","chat","messages","imessage","dialogue","podcast","transcript"],"placement":"overlay","duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* for the accent","default":"Hey, are you there? Yes! Did you see the new captions? They look amazing.","maxLength":600},"accents":{"type":"text","label":"Accent words","description":"words to put in the accent wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["bottom"],"default":"bottom"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"case":{"type":"choice","label":"Case","options":["as typed","upper","lower","style"],"default":"as typed"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01},"sides":{"type":"choice","label":"Sides","options":["turns","one"],"default":"turns","description":"turns: alternate left and right · one: every message from the right"},"upcoming":{"type":"choice","label":"Words not said yet","options":["hidden","dim"],"default":"hidden"}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/messages","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// A chat thread: each phrase arrives as a message bubble at the bottom, taking turns left and right, and the thread slides up to make room.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? 50 : A.wide ? 40 : 44) * k * sizeOf(p.size);\n  const font = Object.assign({}, $.font(\"text\"), { weight: 600, size: size });\n  const maxW = A.w * (A.tall ? 0.62 : 0.42);\n  const groups = phrases(ws, Math.floor(maxW / (size * 0.52)) * 2, 12);\n  const g = groupAt(groups, t, 1.5);\n  if (!g) return \"\";\n  const padX = size * 0.65, padY = size * 0.42, gap = size * 0.5;\n  const bottom = H * (A.tall ? 0.8 : 0.88);\n  const e = $.E.out3($.clamp((t - g.start) / 0.3));\n  const keep = 4;\n  // The newest bubble's height pushes the thread up as it arrives.\n  const sizes = [];\n  for (let a = 0; a < keep && g.k - a >= 0; a++) {\n    const L = lineUp($, groups[g.k - a], font, maxW, 0, 0, 1.25);\n    sizes.push({ L: L, w: Math.max.apply(null, L.widths) + padX * 2, h: L.h + padY * 2 });\n  }\n  let y = bottom + (1 - e) * (sizes[0].h + gap);\n  const theirs = $.rgba(\"#FFFFFF\", 0.94), mine = c.accent;\n  let s = \"\";\n  sizes.forEach(function (b, a) {\n    const gi = g.k - a;\n    const right = p.sides === \"one\" ? true : gi % 2 === 1;\n    const top = y - b.h;\n    y = top - gap;\n    const op = a === 0 ? $.clamp((t - g.start) / 0.12) : Math.max(0, 1 - Math.max(0, a - 2 + e) * 0.6);\n    if (op <= 0.02) return;\n    const bx = right ? A.x + A.w - b.w : A.x;\n    const fill = right ? mine : theirs, ink = right ? $.on(mine) : \"#111111\";\n    let m = $.rect(bx, top, b.w, b.h, { r: Math.min(size * 0.9, b.h / 2), fill: fill });\n    const lay = lineUp($, groups[gi], font, maxW, bx + b.w / 2, top + padY, 1.25);\n    lay.words.forEach(function (u) {\n      if (a === 0 && t < u.s - 0.05 && p.upcoming !== \"dim\") return;\n      m += $.text(u.w, Object.assign({}, font, { x: u.x, y: u.y, fill: u.accent ? (right ? $.on(mine) : c.accent) : ink, op: a === 0 && t < u.s - 0.05 ? 0.35 : 1 }));\n    });\n    const pop = a === 0 ? settle((t - g.start) / 0.26) : 1;\n    s += $.g({ op: op, transform: $.tf({ s: Math.max(0.01, pop), ox: right ? bx + b.w : bx, oy: top + b.h }) }, m);\n  });\n  s = $.g({ extra: 'filter=\"url(#mg-md)\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, s);\n}\n\nfunction sound($) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  return phrases(ws, 30, 12).map(function (g, i) { return { at: g[0].s - 0.05, kind: i % 2 ? \"ding\" : \"pop\", level: 0.45 }; });\n}\n","libs":[]},{"id":"striped","name":"Striped captions","description":"Bold words filled with moving diagonal stripes in the brand's two accents and edged in white, each popping in on its time. Give a transcript with --words.","category":"captions","tags":["captions","stripes","pattern","bold","playful","transcript","reels"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* for the accent","default":"Bold words with *moving* stripes.","maxLength":600},"accents":{"type":"text","label":"Accent words","description":"words to put in the accent wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["center","bottom","top"],"default":"center"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"case":{"type":"choice","label":"Case","options":["upper","as typed","lower","style"],"default":"upper"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01},"ticks":{"type":"boolean","label":"Sound on every word","default":false},"stripes":{"type":"choice","label":"Stripes","options":["standard","thin","wide"],"default":"standard"}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/striped","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// Bold words filled with moving diagonal stripes in the brand's two accents, edged in white, each popping in on its time.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const C = captionLines($, A, ws, t, { size: (A.tall ? 96 : A.wide ? 78 : 86) * k * sizeOf(p.size), font: { ls: 0 }, position: p.position, lines: 2, maxWords: 5, charW: 0.66, lh: 1.1 });\n  if (!C) return \"\";\n  const f = C.font;\n  const band = f.size * ({ thin: 0.12, standard: 0.2, wide: 0.32 }[p.stripes] || 0.2);\n  const pid = $.uid(\"cst\");\n  const shift = (t * f.size * 0.8) % (band * 2);\n  $.def('<pattern id=\"' + pid + '\" width=\"' + band * 2 + '\" height=\"' + band * 2 + '\" patternUnits=\"userSpaceOnUse\" patternTransform=\"rotate(-35) translate(' + shift + ' 0)\"><rect width=\"' + band * 2 + '\" height=\"' + band * 2 + '\" fill=\"' + c.accent + '\"/><rect width=\"' + band + '\" height=\"' + band * 2 + '\" fill=\"' + c.accent2 + '\"/></pattern>');\n  let s = \"\";\n  C.lay.words.forEach(function (u) {\n    const since = t - (u.s - 0.03);\n    if (since < 0) return;\n    const sc = 1 + 0.3 * (1 - settle(since / 0.22));\n    const word = $.text(u.w, Object.assign({}, f, { x: u.x, y: u.y, fill: u.accent ? \"#FFFFFF\" : \"url(#\" + pid + \")\", stroke: \"#FFFFFF\", sw: f.size * 0.07, op: $.clamp(since / 0.05) }));\n    s += $.g({ transform: $.tf({ s: sc, ox: u.x + u.ww / 2, oy: u.y + f.size / 2 }) }, word);\n  });\n  s = $.g({ extra: 'filter=\"url(#mg-md)\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.2, outDur: 0.18, vary: false }, s);\n}\n\nfunction sound($) {\n  return $.data.ticks ? wordCues(captionWords($, $.data, $.beat(\"in\", 0.05))) : [];\n}\n","libs":[]},{"id":"icons","name":"Icon captions","description":"Captions with pictures: when a word that has an icon is said, the icon pops up above it on an accent badge — from a built-in list (time, video, money, idea, launch…) and your own `word:icon` pairs. Give a transcript with --words.","category":"captions","tags":["captions","icons","emoji","explainer","social","transcript"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* for the accent","default":"Your *videos* keep people watching, right on time.","maxLength":600},"accents":{"type":"text","label":"Accent words","description":"words to put in the accent wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["bottom","center","top"],"default":"bottom"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"case":{"type":"choice","label":"Case","options":["as typed","upper","lower","style"],"default":"as typed"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01},"ticks":{"type":"boolean","label":"Sound on every word","default":false},"icons":{"type":"text","label":"Icons","description":"your own word:icon pairs, comma separated, e.g. launch:rocket, price:dollar (motion_api lists the icon names)","default":"","maxLength":300},"auto":{"type":"boolean","label":"Built-in icons","description":"pick icons for common words (time, video, money, idea, launch…)","default":true}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/icons","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// Captions with pictures: when a word with an icon is said, the icon pops up above it on an accent badge.\nconst AUTO = {\n  time: \"clock\", second: \"clock\", seconds: \"clock\", minute: \"clock\", minutes: \"clock\", hour: \"clock\", hours: \"clock\", late: \"clock\",\n  video: \"video\", videos: \"video\", film: \"reel\", movie: \"reel\", camera: \"camera\", photo: \"image\", photos: \"image\", picture: \"image\",\n  watch: \"eye\", watching: \"eye\", see: \"eye\", look: \"eye\", view: \"eye\", views: \"eye\",\n  money: \"dollar\", price: \"dollar\", cost: \"dollar\", dollars: \"dollar\", pay: \"dollar\", sale: \"tag\", buy: \"cart\", shop: \"cart\",\n  love: \"heart\", like: \"thumbsUp\", likes: \"thumbsUp\", fire: \"fire\", hot: \"fire\", idea: \"bulb\", ideas: \"bulb\", think: \"brain\",\n  fast: \"bolt\", quick: \"bolt\", power: \"bolt\", win: \"trophy\", winner: \"trophy\", best: \"trophy\", star: \"star\", top: \"star\",\n  grow: \"trend\", growth: \"trend\", up: \"arrowUp\", down: \"arrowDown\", launch: \"rocket\", rocket: \"rocket\", start: \"rocket\",\n  world: \"globe\", global: \"globe\", home: \"home\", call: \"phone\", phone: \"phone\", mail: \"mail\", email: \"mail\", message: \"msg\",\n  captions: \"msg\", words: \"msg\", word: \"chat\", talk: \"chat\", say: \"mic\", voice: \"mic\", music: \"play\", play: \"play\",\n  people: \"users\", team: \"users\", you: \"user\", me: \"user\", goal: \"target\", target: \"target\", done: \"check\", yes: \"check\",\n  no: \"x\", search: \"search\", find: \"search\", map: \"map\", where: \"pin\", place: \"pin\", lock: \"lock\", secure: \"lock\", safe: \"lock\",\n  data: \"database\", code: \"code\", cloud: \"cloud\", news: \"newspaper\", new: \"sparkle\", magic: \"sparkle\", gift: \"sparkle\",\n  happy: \"smile\", fun: \"smile\", day: \"calendar\", date: \"calendar\", week: \"calendar\", share: \"share\", link: \"link\",\n};\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const C = captionLines($, A, ws, t, { size: (A.tall ? 70 : A.wide ? 58 : 64) * k * sizeOf(p.size), position: p.position, lines: 2, maxWords: 7, lh: 2.05 });\n  if (!C) return \"\";\n  const f = C.font;\n  const own = {};\n  String(p.icons || \"\").split(\",\").forEach(function (pair) {\n    const m = /^\\s*([^:]+?)\\s*:\\s*([A-Za-z]+)\\s*$/.exec(pair);\n    if (m) own[plainWord(m[1])] = m[2];\n  });\n  let badges = \"\", words = \"\";\n  C.lay.words.forEach(function (u, j) {\n    const since = t - (u.s - 0.03);\n    const said = since >= 0;\n    words += $.text(u.w, Object.assign({}, f, { x: u.x, y: u.y, fill: said && u.accent ? c.accent : \"#FFFFFF\", op: said ? 1 : 0.4 }));\n    const key = plainWord(u.w);\n    const name = own[key] || (p.auto === false ? null : AUTO[key]);\n    if (!name || !said) return;\n    const r = f.size * 0.42, cx = u.x + u.ww / 2, cy = u.y - r * 1.25;\n    const pop = settle(since / 0.3), wob = (1 - $.clamp(since / 0.4)) * Math.sin(since * 30) * 10;\n    const badge = $.circle(cx, cy, r, { fill: u.accent ? c.accent2 : c.accent }) + $.icon(name, cx - r * 0.62, cy - r * 0.62, r * 1.24, { color: $.on(u.accent ? c.accent2 : c.accent), sw: 2.2 });\n    badges += $.g({ transform: $.tf({ s: Math.max(0.01, pop), r: wob, ox: cx, oy: cy + r }) }, badge);\n  });\n  const s = $.g({ extra: 'filter=\"url(#mg-sm)\"' }, groupIn($, C.g, t, badges + words));\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, s);\n}\n\nfunction sound($) {\n  return captionWords($, $.data, $.beat(\"in\", 0.05)).filter(function (w) { return AUTO[plainWord(w.w)]; }).map(function (w) { return { at: w.s, kind: \"pop\", level: 0.5 }; });\n}\n","libs":[]},{"id":"scribble","name":"Scribble captions","description":"Handwritten captions: each word written on in a marker hand as it is said, the key words underlined or circled with a quick scribble in the second accent. Give a transcript with --words.","category":"captions","tags":["captions","handwritten","marker","scribble","doodle","transcript","explainer"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* for the accent","default":"Write it down, *underline* the good bits.","maxLength":600},"accents":{"type":"text","label":"Accent words","description":"words to put in the accent wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["bottom","center","top"],"default":"bottom"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"case":{"type":"choice","label":"Case","options":["as typed","upper","lower","style"],"default":"as typed"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01},"ticks":{"type":"boolean","label":"Sound on every word","default":false},"hand":{"type":"choice","label":"Hand","options":["marker","print"],"default":"marker","description":"marker: a fast marker script · print: neat printed handwriting"},"mark":{"type":"choice","label":"Mark","options":["underline","circle"],"default":"underline"}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/scribble","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// Handwritten captions: each word written on in a marker hand as it is said, the key words underlined or circled with a quick scribble.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const hand = { family: p.hand === \"print\" ? \"Patrick Hand\" : \"Caveat\", weight: p.hand === \"print\" ? 400 : 700 };\n  const C = captionLines($, A, ws, t, { size: (A.tall ? 104 : A.wide ? 86 : 94) * k * sizeOf(p.size), font: Object.assign({ ls: 0 }, hand), position: p.position, charW: 0.46, lh: 1.08 });\n  if (!C) return \"\";\n  const f = C.font;\n  let ink = \"\", marks = \"\";\n  C.lay.words.forEach(function (u, j) {\n    const since = t - (u.s - 0.03);\n    if (since < 0) return;\n    // Written left to right across the word's own time.\n    const e = $.clamp(since / Math.max(0.12, Math.min(0.45, (u.e - u.s) * 0.95)));\n    const pad = f.size * 0.1;\n    ink += $.clip(u.x - pad, u.y - f.size * 0.3, (u.ww + pad * 2) * e, f.size * 1.6, 0, $.text(u.w, Object.assign({}, f, { x: u.x, y: u.y, fill: u.accent ? c.accent : \"#FFFFFF\" })));\n    if (!u.accent) return;\n    const d = $.clamp((since - 0.2) / 0.35);\n    if (d <= 0) return;\n    const wob = function (n) { return ($.hash(j * 9.1 + n) - 0.5) * f.size * 0.08; };\n    const x0 = u.x - pad, x1 = u.x + u.ww + pad, base = u.y + f.size * 0.92;\n    const path = p.mark === \"circle\"\n      ? \"M\" + (x0 + f.size * 0.1) + \" \" + (u.y + f.size * 0.18 + wob(1)) + \" C\" + (x0 - f.size * 0.25) + \" \" + (u.y + f.size * 0.9) + \" \" + (x1 + f.size * 0.1) + \" \" + (u.y + f.size * 1.15 + wob(2)) + \" \" + (x1 + f.size * 0.12) + \" \" + (u.y + f.size * 0.45) + \" C\" + (x1 + f.size * 0.1) + \" \" + (u.y - f.size * 0.05) + \" \" + (x0 + f.size * 0.2) + \" \" + (u.y - f.size * 0.12 + wob(3)) + \" \" + (x0 - f.size * 0.02) + \" \" + (u.y + f.size * 0.35)\n      : \"M\" + x0 + \" \" + (base + wob(1)) + \" Q\" + (x0 + x1) / 2 + \" \" + (base + f.size * 0.12 + wob(2)) + \" \" + x1 + \" \" + (base + wob(3)) + \" M\" + (x0 + f.size * 0.15) + \" \" + (base + f.size * 0.16 + wob(4)) + \" Q\" + (x0 + x1) / 2 + \" \" + (base + f.size * 0.24 + wob(5)) + \" \" + (x1 - f.size * 0.1) + \" \" + (base + f.size * 0.13 + wob(6));\n    marks += $.path(path, { stroke: c.accent2, sw: f.size * 0.07, draw: d });\n  });\n  const s = $.g({ extra: 'filter=\"url(#mg-sm)\"' }, groupIn($, C.g, t, marks + ink));\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, s);\n}\n","libs":[]},{"id":"index-card","name":"Index-card captions","description":"Navy ink words on a warm paper card — said, being said and still to come in three shades. Give a transcript with --words.","category":"captions","tags":["captions","social","transcript","template","reels"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* to make it the key word","default":"Welcome to the *new* way to caption.","maxLength":600},"accents":{"type":"text","label":"Key words","description":"words to treat as key words wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["bottom","center","top"],"default":"bottom"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"palette":{"type":"choice","label":"Colours","options":["original","brand"],"default":"original","description":"original: the look's own colours · brand: the brand's accents in their place"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/index-card","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// Navy ink on a warm paper card — said, being said and still to come in three shades.\nconst LOOK = {\"size\": [64, 52, 58], \"font\": {\"family\": \"Poppins\", \"weight\": 600}, \"colors\": {\"said\": \"#14213D\", \"current\": \"rgba(20, 33, 61, 0.74)\", \"upcoming\": \"rgba(20, 33, 61, 0.26)\", \"upOp\": 1}, \"card\": {\"fill\": \"#FFF6E5\", \"r\": 0.27, \"px\": 0.38, \"py\": 0.25}, \"lh\": 1.15, \"maxWords\": 7, \"hero\": \"#FFF6E5\"};\nfunction frame(t, $) {\n  return lineLook(t, $, LOOK);\n}\n","libs":[]},{"id":"weight-shift","name":"Weight-shift captions","description":"Two rows: the first in a heavy white sans, the second in a light ice-blue one, each word rising in as it is said. Give a transcript with --words.","category":"captions","tags":["captions","social","transcript","template","reels"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* to make it the key word","default":"Big ideas need small words to land.","maxLength":600},"accents":{"type":"text","label":"Key words","description":"words to treat as key words wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["bottom","center","top"],"default":"bottom"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"palette":{"type":"choice","label":"Colours","options":["original","brand"],"default":"original","description":"original: the look's own colours · brand: the brand's accents in their place"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/weight-shift","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// Two rows: the first in a heavy white sans, the second in a light ice-blue one, each word rising in as it is said.\nconst LOOK = {\"size\": [80, 64, 72], \"rows\": [{\"family\": \"Montserrat\", \"weight\": 800, \"fill\": \"#FFFFFF\"}, {\"family\": \"Inter\", \"weight\": 300, \"scale\": 0.92, \"fill\": \"#A8DCFF\"}], \"split\": 0.5, \"colors\": {\"said\": \"#FFFFFF\", \"upcoming\": null}, \"enter\": \"rise\", \"shadow\": \"sm\", \"hero\": \"#A8DCFF\"};\nfunction frame(t, $) {\n  return lineLook(t, $, LOOK);\n}\n","libs":[]},{"id":"italic-aside","name":"Italic-aside captions","description":"A clean white sans with the key word set in a glowing peach italic serif. Give a transcript with --words.","category":"captions","tags":["captions","social","transcript","template","reels"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* to make it the key word","default":"Welcome to something *beautiful*.","maxLength":600},"accents":{"type":"text","label":"Key words","description":"words to treat as key words wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["bottom","center","top"],"default":"bottom"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"palette":{"type":"choice","label":"Colours","options":["original","brand"],"default":"original","description":"original: the look's own colours · brand: the brand's accents in their place"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/italic-aside","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// A clean white sans with the key word set in a glowing peach italic serif.\nconst LOOK = {\"size\": [76, 62, 68], \"font\": {\"family\": \"Instrument Sans\", \"weight\": 500}, \"accent\": {\"family\": \"Instrument Serif\", \"weight\": 400, \"italic\": true, \"auto\": true, \"scale\": 1.14, \"fill\": \"#FFC2A1\", \"glow\": {\"color\": \"#FFC2A1\", \"blur\": 0.12, \"op\": 0.75}}, \"colors\": {\"said\": \"#FFFFFF\", \"upcoming\": null}, \"enter\": \"rise\", \"shadow\": \"sm\", \"hero\": \"#FFC2A1\"};\nfunction frame(t, $) {\n  return lineLook(t, $, LOOK);\n}\n","libs":[]},{"id":"tile-step","name":"Tile-step captions","description":"The whole line in white with the word being said on a violet tile that slides along. Give a transcript with --words.","category":"captions","tags":["captions","social","transcript","template","reels"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* to make it the key word","default":"Every word gets its moment in the box.","maxLength":600},"accents":{"type":"text","label":"Key words","description":"words to treat as key words wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["bottom","center","top"],"default":"bottom"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"palette":{"type":"choice","label":"Colours","options":["original","brand"],"default":"original","description":"original: the look's own colours · brand: the brand's accents in their place"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/tile-step","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// The whole line in white with the word being said on a violet tile that slides along.\nconst LOOK = {\"size\": [72, 60, 66], \"font\": {\"family\": \"Inter\", \"weight\": 500}, \"colors\": {\"said\": \"#FFFFFF\", \"upcoming\": \"#FFFFFF\", \"upOp\": 1}, \"box\": {\"fill\": \"#7B5CFF\", \"r\": 0.1}, \"shadow\": \"sm\", \"hero\": \"#7B5CFF\"};\nfunction frame(t, $) {\n  return lineLook(t, $, LOOK);\n}\n","libs":[]},{"id":"soft-rise","name":"Soft-rise captions","description":"Plain warm-white words that rise in softly as they are said, the phrase's key word in mint. Give a transcript with --words.","category":"captions","tags":["captions","social","transcript","template","reels"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* to make it the key word","default":"Clean words that simply rise into place.","maxLength":600},"accents":{"type":"text","label":"Key words","description":"words to treat as key words wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["bottom","center","top"],"default":"bottom"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"palette":{"type":"choice","label":"Colours","options":["original","brand"],"default":"original","description":"original: the look's own colours · brand: the brand's accents in their place"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/soft-rise","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// Plain warm-white words that rise in softly as they are said, the phrase's key word in mint.\nconst LOOK = {\"size\": [72, 60, 66], \"font\": {\"family\": \"Inter\", \"weight\": 600}, \"colors\": {\"said\": \"#FFF8EC\", \"key\": \"#8CF0CC\", \"upcoming\": null}, \"enter\": \"rise\", \"shadow\": \"sm\", \"hero\": \"#8CF0CC\"};\nfunction frame(t, $) {\n  return lineLook(t, $, LOOK);\n}\n","libs":[]},{"id":"ember-caps","name":"Ember captions","description":"Heavy amber capitals with a warm ember glow, popping in word by word. Give a transcript with --words.","category":"captions","tags":["captions","social","transcript","template","reels"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* to make it the key word","default":"This changes *everything* tonight.","maxLength":600},"accents":{"type":"text","label":"Key words","description":"words to treat as key words wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["center","bottom","top"],"default":"center"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"palette":{"type":"choice","label":"Colours","options":["original","brand"],"default":"original","description":"original: the look's own colours · brand: the brand's accents in their place"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/ember-caps","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// Heavy amber capitals with a warm ember glow, popping in word by word.\nconst LOOK = {\"size\": [80, 64, 72], \"font\": {\"family\": \"Montserrat\", \"weight\": 900, \"upper\": true}, \"colors\": {\"said\": \"#FFA53D\", \"upcoming\": null}, \"glow\": {\"color\": \"self\", \"blur\": 0.14, \"op\": 0.7}, \"enter\": \"pop\", \"hero\": \"#FFA53D\", \"charW\": 0.68, \"maxWords\": 5};\nfunction frame(t, $) {\n  return lineLook(t, $, LOOK);\n}\n","libs":[]},{"id":"aura-caps","name":"Aura captions","description":"White capitals with a soft aura, the key word lit up in aqua. Give a transcript with --words.","category":"captions","tags":["captions","social","transcript","template","reels"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* to make it the key word","default":"Welcome to the *future* of video.","maxLength":600},"accents":{"type":"text","label":"Key words","description":"words to treat as key words wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["center","bottom","top"],"default":"center"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"palette":{"type":"choice","label":"Colours","options":["original","brand"],"default":"original","description":"original: the look's own colours · brand: the brand's accents in their place"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/aura-caps","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// White capitals with a soft aura, the key word lit up in aqua.\nconst LOOK = {\"size\": [72, 58, 66], \"font\": {\"family\": \"Montserrat\", \"weight\": 700, \"upper\": true, \"ls\": 0.02}, \"colors\": {\"said\": \"#FFFFFF\", \"key\": \"#7DF2FF\", \"upcoming\": null}, \"glow\": {\"color\": \"self\", \"blur\": 0.16, \"op\": 0.6}, \"enter\": \"fade\", \"hero\": \"#7DF2FF\", \"charW\": 0.68};\nfunction frame(t, $) {\n  return lineLook(t, $, LOOK);\n}\n","libs":[]},{"id":"tape-bars","name":"Tape-bar captions","description":"White words on a cobalt tape behind each line, the tapes sweeping in. Give a transcript with --words.","category":"captions","tags":["captions","social","transcript","template","reels"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* to make it the key word","default":"Hello and welcome to the show.","maxLength":600},"accents":{"type":"text","label":"Key words","description":"words to treat as key words wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["bottom","center","top"],"default":"bottom"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"palette":{"type":"choice","label":"Colours","options":["original","brand"],"default":"original","description":"original: the look's own colours · brand: the brand's accents in their place"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/tape-bars","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// White words on a cobalt tape behind each line, the tapes sweeping in.\nconst LOOK = {\"size\": [70, 58, 64], \"font\": {\"family\": \"Inter\", \"weight\": 600}, \"colors\": {\"said\": \"#FFFFFF\", \"upcoming\": \"#FFFFFF\", \"upOp\": 1}, \"bars\": {\"fill\": \"#2F5BFF\"}, \"hero\": \"#2F5BFF\", \"lh\": 1.25};\nfunction frame(t, $) {\n  return lineLook(t, $, LOOK);\n}\n","libs":[]},{"id":"punch-outline","name":"Punch-outline captions","description":"Heavy white capitals with a thick black outline; the word being said turns hot coral and pops. Give a transcript with --words.","category":"captions","tags":["captions","social","transcript","template","reels"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* to make it the key word","default":"Stop scrolling and *watch* this.","maxLength":600},"accents":{"type":"text","label":"Key words","description":"words to treat as key words wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["center","bottom","top"],"default":"center"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"palette":{"type":"choice","label":"Colours","options":["original","brand"],"default":"original","description":"original: the look's own colours · brand: the brand's accents in their place"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/punch-outline","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// Heavy white capitals with a thick black outline; the word being said turns hot coral and pops.\nconst LOOK = {\"size\": [84, 68, 76], \"font\": {\"family\": \"Montserrat\", \"weight\": 900, \"upper\": true}, \"colors\": {\"said\": \"#FFFFFF\", \"current\": \"#FF4F79\", \"upcoming\": null}, \"stroke\": {\"color\": \"#000000\", \"w\": 0.06, \"cur\": 0.08}, \"enter\": \"pop\", \"hero\": \"#FF4F79\", \"charW\": 0.7, \"maxWords\": 4};\nfunction frame(t, $) {\n  return lineLook(t, $, LOOK);\n}\n","libs":[]},{"id":"wide-pair","name":"Wide-pair captions","description":"Wide extra-bold capitals in two rows — white over mint — with a soft glow and a drop shadow. Give a transcript with --words.","category":"captions","tags":["captions","social","transcript","template","reels"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* to make it the key word","default":"Hello and welcome to the channel.","maxLength":600},"accents":{"type":"text","label":"Key words","description":"words to treat as key words wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["center","bottom","top"],"default":"center"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"palette":{"type":"choice","label":"Colours","options":["original","brand"],"default":"original","description":"original: the look's own colours · brand: the brand's accents in their place"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/wide-pair","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// Wide extra-bold capitals in two rows — white over mint — with a soft glow and a drop shadow.\nconst LOOK = {\"size\": [62, 50, 56], \"rows\": [{\"family\": \"Unbounded\", \"weight\": 800, \"upper\": true, \"fill\": \"#FFFFFF\"}, {\"family\": \"Unbounded\", \"weight\": 800, \"upper\": true, \"fill\": \"#3BE8B0\"}], \"split\": 0.4, \"colors\": {\"upcoming\": null}, \"glow\": {\"color\": \"#FFFFFF\", \"blur\": 0.2, \"op\": 0.25}, \"shadow\": \"md\", \"enter\": \"fade\", \"hero\": \"#3BE8B0\", \"charW\": 0.85};\nfunction frame(t, $) {\n  return lineLook(t, $, LOOK);\n}\n","libs":[]},{"id":"signed-line","name":"Signed-line captions","description":"Bold words opened by a gold brush-script word, the word being said underlined in gold. Give a transcript with --words.","category":"captions","tags":["captions","social","transcript","template","reels"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* to make it the key word","default":"Hello and welcome to the studio.","maxLength":600},"accents":{"type":"text","label":"Key words","description":"words to treat as key words wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["bottom","center","top"],"default":"bottom"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"palette":{"type":"choice","label":"Colours","options":["original","brand"],"default":"original","description":"original: the look's own colours · brand: the brand's accents in their place"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/signed-line","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// Bold words opened by a gold brush-script word, the word being said underlined in gold.\nconst LOOK = {\"size\": [70, 58, 64], \"font\": {\"family\": \"Outfit\", \"weight\": 700}, \"first\": {\"family\": \"Kaushan Script\", \"weight\": 400, \"fill\": \"#FFC23D\", \"scale\": 1.1}, \"colors\": {\"said\": \"#FFFFFF\", \"upcoming\": null}, \"underline\": {\"color\": \"#FFC23D\"}, \"enter\": \"rise\", \"shadow\": \"sm\", \"hero\": \"#FFC23D\"};\nfunction frame(t, $) {\n  return lineLook(t, $, LOOK);\n}\n","libs":[]},{"id":"spaced-serif","name":"Spaced-serif captions","description":"A small tracked-out white line over glowing periwinkle serif capitals. Give a transcript with --words.","category":"captions","tags":["captions","social","transcript","template","reels"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* to make it the key word","default":"Hello and welcome to the city.","maxLength":600},"accents":{"type":"text","label":"Key words","description":"words to treat as key words wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["center","bottom","top"],"default":"center"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"palette":{"type":"choice","label":"Colours","options":["original","brand"],"default":"original","description":"original: the look's own colours · brand: the brand's accents in their place"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/spaced-serif","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// A small tracked-out white line over glowing periwinkle serif capitals.\nconst LOOK = {\"size\": [80, 64, 72], \"rows\": [{\"family\": \"Inter\", \"weight\": 500, \"upper\": true, \"ls\": 0.12, \"scale\": 0.5, \"fill\": \"#FFFFFF\"}, {\"family\": \"DM Serif Display\", \"weight\": 400, \"upper\": true, \"fill\": \"#8FB4FF\", \"glow\": {\"color\": \"#8FB4FF\", \"blur\": 0.15, \"op\": 0.7}}], \"split\": 0.3, \"colors\": {\"upcoming\": null}, \"enter\": \"fade\", \"hero\": \"#8FB4FF\", \"charW\": 0.7};\nfunction frame(t, $) {\n  return lineLook(t, $, LOOK);\n}\n","libs":[]},{"id":"night-script","name":"Night-script captions","description":"A glowing ice-cyan line of condensed capitals over a big white brush script. Give a transcript with --words.","category":"captions","tags":["captions","social","transcript","template","reels"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* to make it the key word","default":"Hello and welcome to the party.","maxLength":600},"accents":{"type":"text","label":"Key words","description":"words to treat as key words wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["center","bottom","top"],"default":"center"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"palette":{"type":"choice","label":"Colours","options":["original","brand"],"default":"original","description":"original: the look's own colours · brand: the brand's accents in their place"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/night-script","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// A glowing ice-cyan line of condensed capitals over a big white brush script.\nconst LOOK = {\"size\": [84, 68, 76], \"rows\": [{\"family\": \"Bebas Neue\", \"weight\": 400, \"upper\": true, \"fill\": \"#9AF4FF\", \"scale\": 0.85, \"glow\": {\"color\": \"#9AF4FF\", \"blur\": 0.18, \"op\": 0.8}}, {\"family\": \"Kaushan Script\", \"weight\": 400, \"fill\": \"#FFFFFF\", \"scale\": 1.1}], \"split\": 0.3, \"colors\": {\"upcoming\": null}, \"enter\": \"rise\", \"shadow\": \"sm\", \"hero\": \"#9AF4FF\"};\nfunction frame(t, $) {\n  return lineLook(t, $, LOOK);\n}\n","libs":[]},{"id":"headline-word","name":"Headline-word captions","description":"A lockup around each phrase's key word: the key word huge in condensed jade capitals, the words before and after it small in an italic serif above it. Give a transcript with --words.","category":"captions","tags":["captions","social","transcript","template","reels","key word","lockup"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* to make it the key word","default":"Hello and *welcome* to the show.","maxLength":600},"accents":{"type":"text","label":"Key words","description":"words to treat as key words wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["center","bottom","top"],"default":"center"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"palette":{"type":"choice","label":"Colours","options":["original","brand"],"default":"original","description":"original: the look's own colours · brand: the brand's accents in their place"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/headline-word","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// A lockup around each phrase's key word: the key word huge in condensed jade capitals, the words before and after it small in an italic serif above it.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const L = lockupAt($, t, A, 5);\n  if (!L) return \"\";\n  const key = String(L.key.w).toUpperCase();\n  const kf = fitFont($, key, \"display\", (A.tall ? 230 : A.wide ? 190 : 210) * k * sizeOf(p.size), A.w * 0.86, { family: \"Anton\", weight: 400, ls: 0 });\n  const cy = captionY($, A, p.position);\n  const kw = $.measure(key, kf), kx = W / 2 - kw / 2, ky = cy - kf.size * 0.4;\n  const sf = { family: \"Instrument Serif\", weight: 400, italic: true, ls: 0, size: kf.size * 0.24 };\n  const jade = p.palette === \"brand\" ? c.accent : \"#2DD4A0\", cream = \"#F1F5FF\";\n  let s = lockRun($, t, L.before, sf, kx, ky - sf.size * 0.95, \"start\", cream).s + lockRun($, t, L.after, sf, kx + kw, ky - sf.size * 0.95, \"end\", cream).s;\n  const ki = keyIn($, t, L.key, W / 2, ky + kf.size / 2);\n  if (ki.show) s += $.g({ op: ki.op, transform: ki.tf }, $.text(key, Object.assign({}, kf, { x: kx, y: ky, fill: jade })));\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.2, outDur: 0.18, vary: false }, groupIn($, L.g, t, $.g({ extra: 'filter=\"url(#mg-md)\"' }, s)));\n}\n","libs":[]},{"id":"flare-word","name":"Flare-word captions","description":"A lockup around each phrase's key word: the key word in huge glowing magenta capitals, the words before it small above on the left, the words after it small below on the right. Give a transcript with --words.","category":"captions","tags":["captions","social","transcript","template","reels","key word","lockup"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* to make it the key word","default":"Hello and *welcome* to the show.","maxLength":600},"accents":{"type":"text","label":"Key words","description":"words to treat as key words wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["center","bottom","top"],"default":"center"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"palette":{"type":"choice","label":"Colours","options":["original","brand"],"default":"original","description":"original: the look's own colours · brand: the brand's accents in their place"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/flare-word","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// A lockup around each phrase's key word: the key word in huge glowing magenta capitals, the words before it small above on the left, the words after it small below on the right.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const L = lockupAt($, t, A, 5);\n  if (!L) return \"\";\n  const key = String(L.key.w).toUpperCase();\n  const kf = fitFont($, key, \"display\", (A.tall ? 190 : A.wide ? 160 : 175) * k * sizeOf(p.size), A.w * 0.84, { family: \"Inter\", weight: 900, ls: -0.01 });\n  const cy = captionY($, A, p.position);\n  const kw = $.measure(key, kf), kx = W / 2 - kw / 2, ky = cy - kf.size * 0.45;\n  const sf = { family: \"Inter\", weight: 800, ls: -0.01, size: kf.size * 0.3 };\n  const magenta = p.palette === \"brand\" ? c.accent : \"#FF4FD8\";\n  let s = lockRun($, t, L.before, sf, kx + kf.size * 0.04, ky - sf.size * 0.92, \"start\", \"#FFFFFF\").s + lockRun($, t, L.after, sf, kx + kw, ky + kf.size * 0.98, \"end\", \"#FFFFFF\").s;\n  const ki = keyIn($, t, L.key, W / 2, ky + kf.size / 2);\n  if (ki.show) {\n    const word = $.text(key, Object.assign({}, kf, { x: kx, y: ky, fill: magenta }));\n    s += $.g({ op: ki.op * 0.85, transform: ki.tf, extra: blurFilter($, {}, kf.size * 0.12) }, word) + $.g({ op: ki.op, transform: ki.tf }, word);\n  }\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.2, outDur: 0.18, vary: false }, groupIn($, L.g, t, s));\n}\n","libs":[]},{"id":"zine-stack","name":"Zine-stack captions","description":"A lowercase collage around each phrase's key word: the key word big in bubblegum pink, the others smaller in white on staggered rows, all in a soft glow. Give a transcript with --words.","category":"captions","tags":["captions","social","transcript","template","reels","key word","lockup"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* to make it the key word","default":"hello and *welcome* to the show.","maxLength":600},"accents":{"type":"text","label":"Key words","description":"words to treat as key words wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["center","bottom","top"],"default":"center"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"palette":{"type":"choice","label":"Colours","options":["original","brand"],"default":"original","description":"original: the look's own colours · brand: the brand's accents in their place"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/zine-stack","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// A lowercase collage around each phrase's key word: the key word big in bubblegum pink, the others smaller in white on staggered rows, all in a soft glow.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const L = lockupAt($, t, A, 5);\n  if (!L) return \"\";\n  const key = String(L.key.w).toLowerCase();\n  const kf = fitFont($, key, \"display\", (A.tall ? 170 : A.wide ? 140 : 155) * k * sizeOf(p.size), A.w * 0.7, { family: \"Montserrat\", weight: 800, ls: -0.05 });\n  const sf = Object.assign({}, kf, { size: kf.size * 0.62 });\n  const cy = captionY($, A, p.position);\n  const kw = $.measure(key, kf), kx = W / 2 - kw / 2 - kf.size * 0.3, ky = cy - kf.size * 0.5;\n  const pink = p.palette === \"brand\" ? c.accent : \"#FFB5E8\";\n  const low = function (ws) { return ws.map(function (w) { return Object.assign({}, w, { w: String(w.w).toLowerCase() }); }); };\n  let s = lockRun($, t, low(L.before), sf, kx + kw * 0.12, ky - sf.size * 0.86, \"start\", \"#FFFFFF\").s;\n  const ki = keyIn($, t, L.key, kx + kw / 2, ky + kf.size / 2);\n  if (ki.show) s += $.g({ op: ki.op, transform: ki.tf }, $.text(key, Object.assign({}, kf, { x: kx, y: ky, fill: pink })));\n  const after = low(L.after);\n  s += lockRun($, t, after.slice(0, 1), Object.assign({}, sf, { size: sf.size * 0.62 }), kx + kw + kf.size * 0.12, ky + kf.size * 0.45, \"start\", \"#FFFFFF\").s;\n  s += lockRun($, t, after.slice(1), sf, kx + kw * 0.35, ky + kf.size * 0.9, \"start\", \"#FFFFFF\").s;\n  // The glow: the same words blurred behind, in a pink haze.\n  const haze = $.g({ op: 0.55, extra: blurFilter($, {}, kf.size * 0.2) }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.2, outDur: 0.18, vary: false }, groupIn($, L.g, t, haze + s));\n}\n","libs":[]},{"id":"type-medley","name":"Type-medley captions","description":"Mixed type around each phrase's key word: an italic serif, a small sans and the key word in bold coral capitals, stacked into one composition. Give a transcript with --words.","category":"captions","tags":["captions","social","transcript","template","reels","key word","lockup"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* to make it the key word","default":"Hello and *welcome* to the show.","maxLength":600},"accents":{"type":"text","label":"Key words","description":"words to treat as key words wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["center","bottom","top"],"default":"center"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"palette":{"type":"choice","label":"Colours","options":["original","brand"],"default":"original","description":"original: the look's own colours · brand: the brand's accents in their place"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/type-medley","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// Mixed type around each phrase's key word: an italic serif, a small sans and the key word in bold coral capitals, stacked into one composition.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const L = lockupAt($, t, A, 5);\n  if (!L) return \"\";\n  const key = String(L.key.w).toUpperCase();\n  const kf = fitFont($, key, \"display\", (A.tall ? 150 : A.wide ? 120 : 135) * k * sizeOf(p.size), A.w * 0.62, { family: \"Inter\", weight: 700, ls: -0.01 });\n  const serif = { family: \"Instrument Serif\", weight: 400, italic: true, ls: 0, size: kf.size * 0.62 };\n  const small = { family: \"Inter\", weight: 500, ls: 0, size: kf.size * 0.3 };\n  const cy = captionY($, A, p.position);\n  const kw = $.measure(key, kf), kx = W / 2 - kw / 2 + kf.size * 0.4, ky = cy - kf.size * 0.4;\n  const coral = p.palette === \"brand\" ? c.accent : \"#FF8C5A\";\n  let s = \"\";\n  // Above: the first word in the serif; beside the key: the rest of the words before it, small.\n  s += lockRun($, t, L.before.slice(0, 1), serif, kx + kw * 0.1, ky - serif.size * 0.92, \"start\", \"#FFFFFF\").s;\n  s += lockRun($, t, L.before.slice(1), small, kx - kf.size * 0.1, ky + kf.size * 0.52, \"end\", \"#FFFFFF\").s;\n  const ki = keyIn($, t, L.key, kx + kw / 2, ky + kf.size / 2);\n  if (ki.show) s += $.g({ op: ki.op, transform: ki.tf }, $.text(key, Object.assign({}, kf, { x: kx, y: ky, fill: coral })));\n  // Below: a small word, then the rest in the serif.\n  const a1 = lockRun($, t, L.after.slice(0, 1), small, kx + kw * 0.08, ky + kf.size * 1.12, \"start\", \"#FFFFFF\");\n  s += a1.s + lockRun($, t, L.after.slice(1), serif, kx + kw * 0.08 + a1.w + small.size * 0.3, ky + kf.size * 0.86, \"start\", \"#FFFFFF\").s;\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.2, outDur: 0.18, vary: false }, groupIn($, L.g, t, $.g({ extra: 'filter=\"url(#mg-sm)\"' }, s)));\n}\n","libs":[]},{"id":"tube-light","name":"Tube-light captions","description":"Each phrase's key word in a wide futuristic face lit like a warm glowing tube that flickers on, light words above and below it. Give a transcript with --words.","category":"captions","tags":["captions","social","transcript","template","reels","key word","lockup"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* to make it the key word","default":"Hello and *welcome* to the show.","maxLength":600},"accents":{"type":"text","label":"Key words","description":"words to treat as key words wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["center","bottom","top"],"default":"center"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"palette":{"type":"choice","label":"Colours","options":["original","brand"],"default":"original","description":"original: the look's own colours · brand: the brand's accents in their place"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/tube-light","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// Each phrase's key word in a wide futuristic face lit like a warm glowing tube that flickers on, light words above and below it.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const L = lockupAt($, t, A, 5);\n  if (!L) return \"\";\n  const key = String(L.key.w);\n  const kf = fitFont($, key, \"display\", (A.tall ? 120 : A.wide ? 100 : 110) * k * sizeOf(p.size), A.w * 0.84, { family: \"Michroma\", weight: 400, ls: 0 });\n  const sf = { family: \"Inter\", weight: 300, ls: 0, size: kf.size * 0.4 };\n  const cy = captionY($, A, p.position);\n  const kw = $.measure(key, kf), kx = W / 2 - kw / 2, ky = cy - kf.size * 0.45;\n  const tube = p.palette === \"brand\" ? c.accent : \"#FFE08A\";\n  let s = lockRun($, t, L.before, sf, kx, ky - sf.size * 1.15, \"start\", \"#FFFFFF\").s + lockRun($, t, L.after, sf, kx + kw, ky + kf.size * 1.12, \"end\", \"#FFFFFF\").s;\n  const since = t - (L.key.s - 0.03);\n  if (since >= 0) {\n    // The tube buzzes on for a fifth of a second, then holds.\n    const lit = since > 0.22 ? 1 : $.hash(Math.floor(t * 30) * 5.3) < 0.35 + since * 3 ? 1 : 0.1;\n    const word = $.text(key, Object.assign({}, kf, { x: kx, y: ky, fill: tube, stroke: tube, sw: kf.size * 0.06 }));\n    s += $.g({ op: 0.75 * lit, extra: blurFilter($, {}, kf.size * 0.13) }, word) + $.g({ op: lit }, word);\n  }\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.2, outDur: 0.18, vary: false }, groupIn($, L.g, t, s));\n}\n","libs":[]},{"id":"tide-poster","name":"Tide-poster captions","description":"A poster lockup: each phrase's key word in huge condensed capitals filled with a cool tide-blue gradient, the other words in a white serif laid across it. Give a transcript with --words.","category":"captions","tags":["captions","social","transcript","template","reels","key word","lockup"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* to make it the key word","default":"Welcome to *studio* time.","maxLength":600},"accents":{"type":"text","label":"Key words","description":"words to treat as key words wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["center","bottom","top"],"default":"center"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"palette":{"type":"choice","label":"Colours","options":["original","brand"],"default":"original","description":"original: the look's own colours · brand: the brand's accents in their place"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/tide-poster","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// A poster lockup: each phrase's key word in huge condensed capitals filled with a cool tide-blue gradient, the other words in a white serif laid across it.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const L = lockupAt($, t, A, 5);\n  if (!L) return \"\";\n  const key = String(L.key.w).toUpperCase();\n  const kf = fitFont($, key, \"display\", (A.tall ? 250 : A.wide ? 210 : 230) * k * sizeOf(p.size), A.w * 0.9, { family: \"Anton\", weight: 400, ls: 0 });\n  const cy = captionY($, A, p.position);\n  const kw = $.measure(key, kf), kx = W / 2 - kw / 2, ky = cy - kf.size * 0.45;\n  const fillKey = p.palette === \"brand\" ? $.gradient([c.accent2, c.accent], { angle: 180 }) : $.gradient([\"#B5F4FF\", \"#3FC5F0\", \"#3A5BEF\"], { angle: 180 });\n  let s = \"\";\n  const ki = keyIn($, t, L.key, W / 2, ky + kf.size / 2);\n  if (ki.show) s += $.g({ op: ki.op, transform: ki.tf }, $.text(key, Object.assign({}, kf, { x: kx, y: ky, fill: fillKey })));\n  // The words before the key laid across its middle; the words after it beneath.\n  const sf = { family: \"Playfair Display\", weight: 400, ls: -0.01, size: kf.size * 0.24 };\n  s += lockRun($, t, L.before, sf, W / 2, ky + kf.size * 0.36, \"middle\", \"#FFFFFF\").s;\n  s += lockRun($, t, L.after, sf, W / 2, ky + kf.size * 1.02, \"middle\", \"#FFFFFF\").s;\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.2, outDur: 0.18, vary: false }, groupIn($, L.g, t, $.g({ extra: 'filter=\"url(#mg-md)\"' }, s)));\n}\n","libs":[]},{"id":"block-justify","name":"Block-justify captions","description":"A justified two-row lockup in chartreuse capitals: the words before the key word spread across the top, the key word and what follows big and bold beneath. Give a transcript with --words.","category":"captions","tags":["captions","social","transcript","template","reels","key word","lockup"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* to make it the key word","default":"Hello there, I'm *Sam Rivera*.","maxLength":600},"accents":{"type":"text","label":"Key words","description":"words to treat as key words wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["center","bottom","top"],"default":"center"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"palette":{"type":"choice","label":"Colours","options":["original","brand"],"default":"original","description":"original: the look's own colours · brand: the brand's accents in their place"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/block-justify","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// A justified two-row lockup in chartreuse capitals: the words before the key word spread across the top, the key word and what follows big and bold beneath.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const L = lockupAt($, t, A, 5);\n  if (!L) return \"\";\n  const lime = p.palette === \"brand\" ? c.accent : \"#E9FF8F\";\n  const big = [L.key].concat(L.after);\n  const line = big.map(function (w) { return String(w.w).toUpperCase(); }).join(\" \");\n  const bf = fitFont($, line, \"display\", (A.tall ? 130 : A.wide ? 110 : 120) * k * sizeOf(p.size), A.w * 0.86, { family: \"Archivo Black\", weight: 400, ls: 0 });\n  const cy = captionY($, A, p.position);\n  const bw = $.measure(line, bf), bx = W / 2 - bw / 2, by = cy - bf.size * 0.25;\n  let s = lockRun($, t, big, bf, bx, by, \"start\", lime, true).s;\n  // The top row spreads its words to the bottom row's width.\n  const tf = { family: \"Inter\", weight: 500, ls: 0.02, size: bf.size * 0.48 };\n  const tops = L.before.map(function (w) { return Object.assign({}, w, { w: String(w.w).toUpperCase() }); });\n  if (tops.length) {\n    // A top row wider than the bottom one (with a word space between its words) is set smaller until it fits.\n    const sumOf = function () { return tops.reduce(function (a, w) { return a + $.measure(w.w, tf); }, 0); };\n    const natural = sumOf() + tf.size * 0.3 * (tops.length - 1);\n    if (natural > bw) tf.size *= bw / natural;\n    const widths = tops.map(function (w) { return $.measure(w.w, tf); });\n    const free = Math.max(tf.size * 0.3 * (tops.length - 1), bw - widths.reduce(function (a, b) { return a + b; }, 0));\n    const gap = tops.length > 1 ? free / (tops.length - 1) : 0;\n    let x = tops.length > 1 ? bx : W / 2 - widths[0] / 2;\n    tops.forEach(function (w, i) {\n      s += lockRun($, t, [w], tf, x, by - tf.size * 1.12, \"start\", lime).s;\n      x += widths[i] + gap;\n    });\n  }\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.2, outDur: 0.18, vary: false }, groupIn($, L.g, t, $.g({ extra: 'filter=\"url(#mg-sm)\"' }, s)));\n}\n","libs":[]},{"id":"circled-word","name":"Circled-word captions","description":"A left-aligned stack of slanted condensed capitals with the key word circled in yellow marker, and an optional handwritten note beneath. Give a transcript with --words.","category":"captions","tags":["captions","social","transcript","template","reels","key word","lockup"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* to make it the key word","default":"Hello and *welcome* to the show.","maxLength":600},"accents":{"type":"text","label":"Key words","description":"words to treat as key words wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["center","bottom","top"],"default":"center"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"palette":{"type":"choice","label":"Colours","options":["original","brand"],"default":"original","description":"original: the look's own colours · brand: the brand's accents in their place"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01},"note":{"type":"text","label":"Note","description":"a handwritten line under the stack once the phrase is said; empty for none","default":"","maxLength":60}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/circled-word","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// A left-aligned stack of slanted condensed capitals with the key word circled in yellow marker, and an optional handwritten note beneath.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const L = lockupAt($, t, A, 6);\n  if (!L) return \"\";\n  const words = L.g.words;\n  const size = (A.tall ? 110 : A.wide ? 88 : 98) * k * sizeOf(p.size);\n  const f = { family: \"Anton\", weight: 400, ls: 0.01, size: size };\n  const x0 = A.x + A.w * (A.tall ? 0.1 : 0.22), maxW = A.w * (A.tall ? 0.78 : 0.5);\n  // Lines, left-aligned, as many words as fit.\n  const gap = size * 0.24, lines = [];\n  let cur = [], curW = 0;\n  words.forEach(function (w, i) {\n    const ww = $.measure(String(w.w).toUpperCase(), f);\n    if (cur.length && curW + gap + ww > maxW) { lines.push(cur); cur = []; curW = 0; }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww, i: i });\n  });\n  if (cur.length) lines.push(cur);\n  const lh = size * 1.02, note = String(p.note || \"\").trim();\n  const total = lines.length * lh + (note ? size * 0.55 : 0);\n  let y = captionY($, A, p.position) - total / 2, s = \"\", ring = \"\", keyBox = null;\n  const ki = words.indexOf(L.key);\n  lines.forEach(function (ln) {\n    let x = x0;\n    ln.forEach(function (it) {\n      s += lockRun($, t, [it.w], f, x, y, \"start\", \"#FFFFFF\", true).s;\n      if (it.i === ki) keyBox = { x: x, y: y, w: it.ww };\n      x += it.ww + gap;\n    });\n    y += lh;\n  });\n  // The marker ring round the key word, drawn on once it is said.\n  const since = t - (L.key.s + 0.08);\n  if (keyBox && since > 0) {\n    const yellow = p.palette === \"brand\" ? c.accent : \"#FFD23F\";\n    const cx = keyBox.x + keyBox.w / 2, cy = keyBox.y + size * 0.55, rx = keyBox.w / 2 + size * 0.28, ry = size * 0.62;\n    const d = \"M\" + (cx - rx * 0.6) + \" \" + (cy - ry * 0.95) + \" C\" + (cx + rx * 1.1) + \" \" + (cy - ry * 1.25) + \" \" + (cx + rx * 1.15) + \" \" + (cy + ry * 1.05) + \" \" + cx + \" \" + (cy + ry) +\n      \" C\" + (cx - rx * 1.2) + \" \" + (cy + ry * 0.95) + \" \" + (cx - rx * 1.1) + \" \" + (cy - ry * 1.15) + \" \" + (cx + rx * 0.2) + \" \" + (cy - ry * 1.05);\n    ring = $.path(d, { stroke: yellow, sw: size * 0.07, draw: $.E.out3($.clamp(since / 0.4)) });\n  }\n  if (note) {\n    const last = words[words.length - 1];\n    s += lockRun($, t, [{ w: note, s: last.end + 0.1, e: last.end + 0.6 }], { family: \"Caveat\", weight: 700, ls: 0, size: size * 0.42 }, x0 + size * 0.1, y + size * 0.05, \"start\", \"#FFFFFF\").s;\n  }\n  // A slant, like italic capitals.\n  s = '<g transform=\"translate(' + x0 + \" \" + y + \") skewX(-9) translate(\" + -x0 + \" \" + -y + ')\">' + s + \"</g>\";\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.2, outDur: 0.18, vary: false }, groupIn($, L.g, t, $.g({ extra: 'filter=\"url(#mg-sm)\"' }, s + ring)));\n}\n","libs":[]},{"id":"notebook","name":"Notebook captions","description":"A handwritten note on a ruled pale-blue notebook card: the words before the key word small on top, the key line big below, a pink brush stroke swept under it. Give a transcript with --words.","category":"captions","tags":["captions","social","transcript","template","reels","key word","lockup"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* to make it the key word","default":"Hey there, this is *Sam Rivera*.","maxLength":600},"accents":{"type":"text","label":"Key words","description":"words to treat as key words wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["bottom","center","top"],"default":"bottom"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"palette":{"type":"choice","label":"Colours","options":["original","brand"],"default":"original","description":"original: the look's own colours · brand: the brand's accents in their place"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/notebook","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// A handwritten note on a ruled pale-blue notebook card: the words before the key word small on top, the key line big below, a pink brush stroke swept under it.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const L = lockupAt($, t, A, 6);\n  if (!L) return \"\";\n  const ink = \"#1E2A5A\", paper = \"#EEF4FF\", brush = p.palette === \"brand\" ? c.accent : \"#FF5C8A\";\n  const big = [L.key].concat(L.after);\n  const line = big.map(function (w) { return w.w; }).join(\" \");\n  const bf = fitFont($, line, \"display\", (A.tall ? 120 : A.wide ? 100 : 110) * k * sizeOf(p.size), A.w * 0.74, { family: \"Caveat\", weight: 700, ls: 0 });\n  const sf = { family: \"Caveat\", weight: 700, ls: 0.01, size: bf.size * 0.52 };\n  const top = L.before.map(function (w) { return w.w; }).join(\" \");\n  const bw = $.measure(line, bf), tw = top ? $.measure(top, sf) : 0;\n  const pad = bf.size * 0.45, cw = Math.max(bw, tw) + pad * 2, ch = bf.size * 1.05 + (top ? sf.size * 1.15 : 0) + pad * 1.4;\n  const cx = W / 2, cy = captionY($, A, p.position);\n  const x0 = cx - cw / 2, y0 = cy - ch / 2;\n  let s = $.rect(x0, y0, cw, ch, { r: bf.size * 0.12, fill: paper });\n  // Ruled lines and a margin, like a page torn from a notebook.\n  const rule = bf.size * 0.42, inset = bf.size * 0.12;\n  for (let ry = y0 + rule * 1.4; ry < y0 + ch - inset; ry += rule) s += $.line(x0 + inset, ry, x0 + cw - inset, ry, { stroke: \"#C7D7F3\", sw: Math.max(1.5, bf.size * 0.016) });\n  s += $.line(x0 + pad * 0.55, y0 + inset, x0 + pad * 0.55, y0 + ch - inset, { stroke: \"#FF9DB8\", sw: Math.max(1.5, bf.size * 0.02) });\n  const tx = x0 + pad;\n  let y = y0 + pad * 0.7;\n  if (top) { s += lockRun($, t, L.before, sf, tx, y, \"start\", ink).s; y += sf.size * 1.15; }\n  s += lockRun($, t, big, bf, tx, y, \"start\", ink).s;\n  // The brush stroke sweeps in under the key line once the key word is said.\n  const since = t - (L.key.s + 0.1);\n  if (since > 0) {\n    const by = y + bf.size * 1.0, x1 = tx + bw * 0.98;\n    const d = \"M\" + (tx + bf.size * 0.05) + \" \" + by + \" C\" + (tx + bw * 0.3) + \" \" + (by + bf.size * 0.1) + \" \" + (tx + bw * 0.6) + \" \" + (by - bf.size * 0.06) + \" \" + x1 + \" \" + (by + bf.size * 0.02);\n    const e = $.E.out3($.clamp(since / 0.35));\n    s += $.path(d, { stroke: brush, sw: bf.size * 0.1, draw: e }) + $.path(d, { stroke: brush, sw: bf.size * 0.05, draw: e, transform: \"translate(\" + bf.size * 0.06 + \" \" + bf.size * 0.09 + \")\" });\n  }\n  // The card pops up from its bottom edge with a slight tilt.\n  const pop = settle((t - L.g.start) / 0.3);\n  s = $.g({ op: $.clamp((t - L.g.start) / 0.08), transform: $.tf({ s: Math.max(0.01, pop), r: -1.5, ox: cx, oy: y0 + ch }) }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.2, outDur: 0.18, vary: false }, $.g({ extra: 'filter=\"url(#mg-md)\"' }, s));\n}\n","libs":[]},{"id":"loop-script","name":"Loop-script captions","description":"A bold white line with its key word in a glowing gold script and a dotted, looping flourish drawn underneath across the phrase. Give a transcript with --words.","category":"captions","tags":["captions","social","transcript","template","reels","key word","lockup"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* to make it the key word","default":"Welcome *to* the show.","maxLength":600},"accents":{"type":"text","label":"Key words","description":"words to treat as key words wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["center","bottom","top"],"default":"center"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"palette":{"type":"choice","label":"Colours","options":["original","brand"],"default":"original","description":"original: the look's own colours · brand: the brand's accents in their place"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/loop-script","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// A bold white line with its key word in a glowing gold script and a dotted, looping flourish drawn underneath across the phrase.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const L = lockupAt($, t, A, 6);\n  if (!L) return \"\";\n  const size = (A.tall ? 72 : A.wide ? 60 : 66) * k * sizeOf(p.size);\n  const gold = p.palette === \"brand\" ? c.accent : \"#FFC46B\";\n  const words = L.g.words;\n  const maxW = A.w * 0.9;\n  const fontFor = function (w) { return w === L.key ? { family: \"Parisienne\", weight: 400, ls: 0, size: size * 1.3 } : { family: \"Instrument Sans\", weight: 700, ls: -0.01, size: size }; };\n  const lay = lineUpMixed($, words, fontFor, maxW, W / 2, captionY($, A, p.position), 1.2);\n  const blurs = {};\n  let s = \"\";\n  lay.forEach(function (u) {\n    const since = t - (u.s - 0.03);\n    if (since < 0) return;\n    const e = $.E.out3($.clamp(since / 0.25));\n    const isKey = u.w === L.key.w && u.s === L.key.s;\n    const word = $.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y + (1 - e) * u.size * 0.2, fill: isKey ? gold : \"#FFFFFF\", op: $.clamp(since / 0.15) }));\n    if (isKey) s += $.g({ extra: blurFilter($, blurs, u.size * 0.1) }, word);\n    s += word;\n  });\n  // The flourish: loops traced as glowing dots, drawn left to right across the phrase's time.\n  const x0 = Math.min.apply(null, lay.map(function (u) { return u.x; })), x1 = Math.max.apply(null, lay.map(function (u) { return u.x + u.ww; }));\n  const base = Math.max.apply(null, lay.map(function (u) { return u.y + u.size; })) + size * 0.32;\n  const last = words[words.length - 1];\n  const prog = $.clamp((t - L.g.start) / Math.max(0.6, last.end - L.g.start + 0.2));\n  const loops = 3, a = 1, b = 2.4, span = loops * 2 * Math.PI;\n  const xAt = function (th) { return a * th - b * Math.sin(th); }, xMax = xAt(span);\n  let dots = \"\";\n  const n = 120;\n  for (let i = 0; i <= n * prog; i++) {\n    const th = (i / n) * span;\n    const x = x0 + ((xAt(th) - xAt(0)) / (xMax - xAt(0))) * (x1 - x0), y = base + size * 0.15 - b * Math.cos(th) * size * 0.17;\n    dots += $.circle(x, y, size * 0.028, { fill: gold, op: 0.9 });\n  }\n  s += $.g({ extra: blurFilter($, blurs, size * 0.06) }, dots) + dots;\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.2, outDur: 0.18, vary: false }, groupIn($, L.g, t, s));\n}\n","libs":[]},{"id":"pen-write","name":"Pen-write captions","description":"Words written on in a signature script as they are said, a glowing pen tip leading the stroke and a swoosh drawn under the line. Give a transcript with --words.","category":"captions","tags":["captions","script","social","transcript","template","reels"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* to make it the key word","default":"Welcome to the *new* way to caption.","maxLength":600},"accents":{"type":"text","label":"Key words","description":"words to treat as key words wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["bottom","center","top"],"default":"bottom"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"palette":{"type":"choice","label":"Colours","options":["original","brand"],"default":"original","description":"original: the look's own colours · brand: the brand's accents in their place"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/pen-write","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// Words written on in a signature script as they are said, a glowing pen tip leading the stroke and a swoosh drawn under the line.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? 124 : A.wide ? 100 : 112) * k * sizeOf(p.size);\n  const C = captionLines($, A, ws, t, { size: size, font: { family: \"Mr Dafoe\", weight: 400, ls: 0 }, charW: 0.52, lines: 2, maxWords: 6, lh: 1.05, position: p.position });\n  if (!C) return \"\";\n  const blush = p.palette === \"brand\" ? c.accent : \"#FFC7DE\";\n  const blurs = {};\n  let s = \"\", tip = \"\";\n  C.lay.words.forEach(function (u) {\n    const q = writeP($, t, u, 0.9);\n    if (q <= 0) return;\n    s += writeOn($, u, size, q, $.text(u.w, Object.assign({}, C.font, { x: u.x, y: u.y, fill: u.accent ? blush : \"#FFFFFF\" })));\n    // The nib rides the writing edge, bobbing with the letters.\n    if (q < 1) {\n      const px = u.x + u.ww * q, py = u.y + size * (0.62 + 0.13 * Math.sin(q * Math.PI * 7));\n      tip = $.circle(px, py, size * 0.06, { fill: blush, op: 0.85, extra: blurFilter($, blurs, size * 0.06) }) + $.circle(px, py, size * 0.022, { fill: \"#FFFFFF\" });\n    }\n  });\n  // Once the last word is written, a swoosh is drawn under its line.\n  const lay = C.lay.words, last = lay[lay.length - 1];\n  const done = t - (last.s - 0.03 + Math.max(0.16, Math.min(0.6, (last.end - last.s) * 0.9)));\n  if (done > 0) {\n    const row = lay.filter(function (u) { return u.line === last.line; });\n    const x0 = row[0].x, x1 = last.x + last.ww, y = last.y + size * 1.0;\n    const d = \"M\" + (x0 - size * 0.1) + \" \" + (y + size * 0.06) + \" C\" + (x0 + (x1 - x0) * 0.35) + \" \" + (y - size * 0.1) + \" \" + (x0 + (x1 - x0) * 0.7) + \" \" + (y + size * 0.12) + \" \" + (x1 + size * 0.3) + \" \" + (y - size * 0.1);\n    s += $.path(d, { stroke: blush, sw: size * 0.035, draw: $.E.out3($.clamp(done / 0.4)) });\n  }\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, C.g, t, $.g({ extra: 'filter=\"url(#mg-sm)\"' }, s + tip)));\n}\n","libs":[]},{"id":"cursive-neon","name":"Cursive-neon captions","description":"A neon sign in a flowing script: each word's pink tube buzzes on as it is said, the words still to come in dark glass, the key word in blue. Give a transcript with --words.","category":"captions","tags":["captions","script","social","transcript","template","reels"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* to make it the key word","default":"Welcome to the *new* way to caption.","maxLength":600},"accents":{"type":"text","label":"Key words","description":"words to treat as key words wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["bottom","center","top"],"default":"bottom"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"palette":{"type":"choice","label":"Colours","options":["original","brand"],"default":"original","description":"original: the look's own colours · brand: the brand's accents in their place"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/cursive-neon","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// A neon sign in a flowing script: each word's pink tube buzzes on as it is said, the words still to come in dark glass, the key word in blue.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? 104 : A.wide ? 84 : 94) * k * sizeOf(p.size);\n  const C = captionLines($, A, ws, t, { size: size, font: { family: \"Yellowtail\", weight: 400, ls: 0 }, charW: 0.46, lines: 2, maxWords: 7, lh: 1.1, position: p.position });\n  if (!C) return \"\";\n  const pink = p.palette === \"brand\" ? c.accent : \"#FF4FA3\", blue = p.palette === \"brand\" ? c.accent2 : \"#4FD8FF\";\n  const key = C.g.words[keyIndex(C.g.words)];\n  const blurs = {};\n  let glass = \"\", glow = \"\", tubes = \"\";\n  C.lay.words.forEach(function (u) {\n    const f = Object.assign({}, C.font, { x: u.x, y: u.y });\n    const since = t - (u.s - 0.03);\n    // Unlit: the bare glass tube, faint.\n    if (since < 0) { glass += $.text(u.w, Object.assign({}, f, { fill: \"none\", stroke: \"#FFFFFF\", sw: size * 0.02, op: 0.24 })); return; }\n    const col = u.accent || u.i === key.i ? blue : pink;\n    // The tube buzzes on for a quarter of a second, then holds with a faint hum.\n    const lit = since > 0.26 ? 0.95 + 0.05 * Math.sin(t * 40 + u.i * 2.1) : $.hash(Math.floor(t * 30) * 7.1 + u.i) < 0.3 + since * 2.8 ? 1 : 0.12;\n    glow += $.text(u.w, Object.assign({}, f, { fill: col, stroke: col, sw: size * 0.06, op: lit }));\n    tubes += $.text(u.w, Object.assign({}, f, { fill: \"#FFF6FB\", stroke: col, sw: size * 0.05, op: Math.max(0.3, lit) }));\n  });\n  const s = glass + $.g({ op: 0.5, extra: blurFilter($, blurs, size * 0.38) }, glow) + $.g({ op: 0.9, extra: blurFilter($, blurs, size * 0.1) }, glow) + tubes;\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, C.g, t, s));\n}\n","libs":[]},{"id":"groovy-script","name":"Groovy-script captions","description":"Seventies script: cream words with a stack of orange, gold and teal shadows that pull out as each word bounces in. Give a transcript with --words.","category":"captions","tags":["captions","script","social","transcript","template","reels"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* to make it the key word","default":"Welcome to the *new* way to caption.","maxLength":600},"accents":{"type":"text","label":"Key words","description":"words to treat as key words wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["bottom","center","top"],"default":"bottom"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"palette":{"type":"choice","label":"Colours","options":["original","brand"],"default":"original","description":"original: the look's own colours · brand: the brand's accents in their place"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/groovy-script","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// Seventies script: cream words with a stack of orange, gold and teal shadows that pull out as each word bounces in.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? 92 : A.wide ? 74 : 84) * k * sizeOf(p.size);\n  const C = captionLines($, A, ws, t, { size: size, font: { family: \"Pacifico\", weight: 400, ls: 0 }, charW: 0.56, lines: 2, maxWords: 6, lh: 1.28, position: p.position });\n  if (!C) return \"\";\n  const brand = p.palette === \"brand\";\n  const stack = [brand ? c.accent : \"#FF6B35\", \"#F7B32B\", brand ? c.accent2 : \"#2EC4B6\"];\n  let s = \"\";\n  C.lay.words.forEach(function (u) {\n    const since = t - (u.s - 0.03);\n    if (since < 0) return;\n    // Shadows back to front, pulled out after the word lands; then the face.\n    const d = size * 0.05 * $.E.out3($.clamp((since - 0.04) / 0.3));\n    let w = \"\";\n    for (let i = stack.length; i >= 1; i--) w += $.text(u.w, Object.assign({}, C.font, { x: u.x + d * i, y: u.y + d * i, fill: stack[i - 1] }));\n    w += $.text(u.w, Object.assign({}, C.font, { x: u.x, y: u.y, fill: u.accent ? \"#FFD23F\" : \"#FFF1D6\" }));\n    const sc = 1 + 0.3 * (1 - settle(since / 0.22));\n    s += $.g({ op: $.clamp(since / 0.08), transform: $.tf({ s: sc, ox: u.x + u.ww / 2, oy: u.y + size * 0.6 }) }, w);\n  });\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, C.g, t, s));\n}\n","libs":[]},{"id":"script-sandwich","name":"Script-sandwich captions","description":"A lockup around each phrase's key word: the key word huge in a formal champagne script, written on as it is said, the words before it in small spaced capitals above, the words after below, with fine rules either side. Give a transcript with --words.","category":"captions","tags":["captions","script","social","transcript","template","reels"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* to make it the key word","default":"Welcome to the *new* way to caption.","maxLength":600},"accents":{"type":"text","label":"Key words","description":"words to treat as key words wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["bottom","center","top"],"default":"bottom"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"palette":{"type":"choice","label":"Colours","options":["original","brand"],"default":"original","description":"original: the look's own colours · brand: the brand's accents in their place"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/script-sandwich","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// A lockup: the key word huge in a formal champagne script, written on as it is said, with the words before it in small spaced capitals above, the words after below, and fine rules either side.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const L = lockupAt($, t, A, 5);\n  if (!L) return \"\";\n  const gold = p.palette === \"brand\" ? c.accent : \"#FFE3A3\";\n  const kf = fitFont($, L.key.w, \"display\", (A.tall ? 210 : A.wide ? 170 : 190) * k * sizeOf(p.size), A.w * 0.8, { family: \"Great Vibes\", weight: 400, ls: 0 });\n  const cf = { family: \"Inter\", weight: 600, ls: 0.3, gap: 0.7, size: Math.max(kf.size * 0.15, 22 * k) };\n  const cy = captionY($, A, p.position);\n  const kw = $.measure(L.key.w, kf), kx = W / 2 - kw / 2, ky = cy - kf.size * 0.55;\n  const caps = function (ws) { return ws.map(function (w) { return Object.assign({}, w, { w: String(w.w).toUpperCase() }); }); };\n  // The capitals sit clear of the script: above its tall strokes, below its descenders.\n  const topY = ky + kf.size * 0.04 - cf.size * 1.05, botY = ky + kf.size * 1.14;\n  const top = lockRun($, t, caps(L.before), cf, W / 2, topY, \"middle\", \"#FFFFFF\");\n  const bot = lockRun($, t, caps(L.after), cf, W / 2, botY, \"middle\", \"#FFFFFF\");\n  let s = top.s + bot.s;\n  // Fine rules either side of the capitals' row (the top one, else the bottom one).\n  const row = L.before.length ? { w: top.w, y: topY } : L.after.length ? { w: bot.w, y: botY } : null;\n  if (row) {\n    const e = $.E.out3($.clamp((t - L.g.start) / 0.45)), len = kf.size * 0.32 * e, gap = cf.size * 0.9, y = row.y + cf.size * 0.42;\n    s += $.line(W / 2 - row.w / 2 - gap - len, y, W / 2 - row.w / 2 - gap, y, { stroke: gold, sw: Math.max(1.5, cf.size * 0.07), op: 0.85 });\n    s += $.line(W / 2 + row.w / 2 + gap, y, W / 2 + row.w / 2 + gap + len, y, { stroke: gold, sw: Math.max(1.5, cf.size * 0.07), op: 0.85 });\n  }\n  // The key word written on in the script, with a soft glow.\n  const q = writeP($, t, L.key, 1.1);\n  if (q > 0) {\n    const word = $.text(L.key.w, Object.assign({}, kf, { x: kx, y: ky, fill: gold }));\n    const box = { x: kx, y: ky, ww: kw };\n    s += writeOn($, box, kf.size, q, $.g({ op: 0.45, extra: blurFilter($, {}, kf.size * 0.05) }, word)) + writeOn($, box, kf.size, q, word);\n  }\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.2, outDur: 0.18, vary: false }, groupIn($, L.g, t, $.g({ extra: 'filter=\"url(#mg-sm)\"' }, s)));\n}\n","libs":[]},{"id":"swash-mark","name":"Swash-mark captions","description":"Bold white words with the key word in a dark script on a lavender paint swash that sweeps in behind it. Give a transcript with --words.","category":"captions","tags":["captions","script","social","transcript","template","reels"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* to make it the key word","default":"Welcome to the *new* way to caption.","maxLength":600},"accents":{"type":"text","label":"Key words","description":"words to treat as key words wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["bottom","center","top"],"default":"bottom"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"palette":{"type":"choice","label":"Colours","options":["original","brand"],"default":"original","description":"original: the look's own colours · brand: the brand's accents in their place"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/swash-mark","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// Bold white words with the key word in a dark script on a lavender paint swash that sweeps in behind it.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const L = lockupAt($, t, A, 6);\n  if (!L) return \"\";\n  const size = (A.tall ? 74 : A.wide ? 60 : 68) * k * sizeOf(p.size);\n  const paint = p.palette === \"brand\" ? c.accent : \"#C9B6FF\", dark = p.palette === \"brand\" ? $.on(c.accent) : \"#1B1433\";\n  const fontFor = function (w) { return w.i === L.key.i ? { family: \"Dancing Script\", weight: 700, ls: 0, size: size * 1.3, pad: size * 0.2 } : { family: \"Outfit\", weight: 700, ls: -0.01, size: size }; };\n  const lay = lineUpMixed($, L.g.words, fontFor, A.w * 0.86, W / 2, captionY($, A, p.position), 1.32);\n  let swash = \"\", s = \"\";\n  lay.forEach(function (u) {\n    const since = t - (u.s - 0.03);\n    const isKey = u.i === L.key.i;\n    if (isKey) {\n      // Three brush passes, the middle one wide, each a slightly bent stroke behind the word.\n      const sw = since + 0.08;\n      if (sw > 0) {\n        // The round brush ends reach a third of the size past the path, so it starts and stops inside the word.\n        const x0 = u.x + u.size * 0.12, x1 = Math.max(x0 + 1, u.x + u.ww - u.size * 0.12), yc = u.y + u.size * 0.6;\n        const stroke = function (dy, w, delay, op) {\n          const e = $.E.out3($.clamp((sw - delay) / 0.26));\n          const d = \"M\" + x0 + \" \" + (yc + dy + u.size * 0.06) + \" C\" + (x0 + (x1 - x0) * 0.3) + \" \" + (yc + dy - u.size * 0.04) + \" \" + (x0 + (x1 - x0) * 0.7) + \" \" + (yc + dy + u.size * 0.05) + \" \" + x1 + \" \" + (yc + dy - u.size * 0.05);\n          return e > 0 ? $.path(d, { stroke: paint, sw: u.size * w, draw: e, op: op }) : \"\";\n        };\n        swash += stroke(0, 0.62, 0, 1) + stroke(-u.size * 0.27, 0.16, 0.05, 0.8) + stroke(u.size * 0.27, 0.14, 0.08, 0.75);\n      }\n    }\n    if (since < 0) return;\n    const e = $.E.out3($.clamp(since / 0.28));\n    s += $.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y + (1 - e) * u.size * 0.22, fill: isKey ? dark : \"#FFFFFF\", op: $.clamp(since / 0.18) }));\n  });\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.2, outDur: 0.18, vary: false }, groupIn($, L.g, t, $.g({ extra: 'filter=\"url(#mg-sm)\"' }, swash + s)));\n}\n","libs":[]},{"id":"vow-script","name":"Vow-script captions","description":"Fine lowercase script in white with the key word in gold, each word coming up out of a soft blur, and a hairline rule with a small diamond drawn out beneath. Give a transcript with --words.","category":"captions","tags":["captions","script","social","transcript","template","reels"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* to make it the key word","default":"Welcome to the *new* way to caption.","maxLength":600},"accents":{"type":"text","label":"Key words","description":"words to treat as key words wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["bottom","center","top"],"default":"bottom"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"palette":{"type":"choice","label":"Colours","options":["original","brand"],"default":"original","description":"original: the look's own colours · brand: the brand's accents in their place"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/vow-script","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// Fine lowercase script in white with the key word in gold, each word coming up out of a soft blur, and a hairline rule with a small diamond drawn out beneath.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05)).map(function (w) { return Object.assign({}, w, { w: String(w.w).toLowerCase() }); });\n  if (!ws.length) return \"\";\n  const size = (A.tall ? 118 : A.wide ? 94 : 106) * k * sizeOf(p.size);\n  const C = captionLines($, A, ws, t, { size: size, font: { family: \"Sacramento\", weight: 400, ls: 0 }, charW: 0.36, lines: 2, maxWords: 7, lh: 0.95, position: p.position });\n  if (!C) return \"\";\n  const gold = p.palette === \"brand\" ? c.accent : \"#E9C46A\";\n  const key = C.g.words[keyIndex(C.g.words)];\n  const blurs = {};\n  let s = \"\";\n  C.lay.words.forEach(function (u) {\n    const since = t - (u.s - 0.03);\n    if (since < 0) return;\n    const e = $.clamp(since / 0.4), col = u.accent || u.i === key.i ? gold : \"#FFFFFF\";\n    // A hairline face, thickened a touch with its own colour so it reads over footage.\n    const word = $.text(u.w, Object.assign({}, C.font, { x: u.x, y: u.y + (1 - $.E.out3(e)) * size * 0.14, fill: col, stroke: col, sw: size * 0.012, op: $.clamp(since / 0.25) }));\n    s += e < 1 ? $.g({ extra: blurFilter($, blurs, size * 0.07 * (1 - e)) }, word) : word;\n  });\n  // The rule: two hairlines growing out from a small diamond under the last line.\n  const lay = C.lay.words, bottom = Math.max.apply(null, lay.map(function (u) { return u.y; })) + size * 1.42;\n  const half = Math.min(Math.max.apply(null, C.lay.widths) / 2 + size * 0.2, A.w * 0.44);\n  const e = $.E.inOut3($.clamp((t - C.g.start - 0.1) / 0.7)), dm = size * 0.055 * $.E.outBack($.clamp((t - C.g.start) / 0.3));\n  if (e > 0) {\n    const sw = Math.max(1.5, size * 0.014), cx = W / 2;\n    s += $.line(cx - dm * 2, bottom, cx - dm * 2 - (half - dm * 2) * e, bottom, { stroke: gold, sw: sw }) + $.line(cx + dm * 2, bottom, cx + dm * 2 + (half - dm * 2) * e, bottom, { stroke: gold, sw: sw });\n  }\n  if (dm > 0) s += $.path(\"M\" + W / 2 + \" \" + (bottom - dm) + \" L\" + (W / 2 + dm) + \" \" + bottom + \" L\" + W / 2 + \" \" + (bottom + dm) + \" L\" + (W / 2 - dm) + \" \" + bottom + \" Z\", { fill: gold });\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, C.g, t, $.g({ extra: 'filter=\"url(#mg-sm)\"' }, s)));\n}\n","libs":[]},{"id":"brush-ribbon","name":"Brush-ribbon captions","description":"Brush-script words in white on a ragged teal ribbon that paints in behind each line, the words popping on in time. Give a transcript with --words.","category":"captions","tags":["captions","script","social","transcript","template","reels"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* to make it the key word","default":"Welcome to the *new* way to caption.","maxLength":600},"accents":{"type":"text","label":"Key words","description":"words to treat as key words wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["bottom","center","top"],"default":"bottom"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"palette":{"type":"choice","label":"Colours","options":["original","brand"],"default":"original","description":"original: the look's own colours · brand: the brand's accents in their place"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/brush-ribbon","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// Brush-script words in white on a ragged teal ribbon that paints in behind each line, the words popping on in time.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? 84 : A.wide ? 68 : 76) * k * sizeOf(p.size);\n  const C = captionLines($, A, ws, t, { size: size, font: { family: \"Kaushan Script\", weight: 400, ls: 0 }, charW: 0.55, lines: 2, maxWords: 7, lh: 1.32, position: p.position });\n  if (!C) return \"\";\n  const brand = p.palette === \"brand\";\n  const teal = brand ? c.accent : \"#0E9AA7\", deep = brand ? $.mix(c.accent, \"#000000\", 0.35) : \"#08636D\";\n  let back = \"\", s = \"\";\n  for (let li = 0; li < C.lay.lines; li++) {\n    const row = C.lay.words.filter(function (u) { return u.line === li; });\n    const x0 = row[0].x - size * 0.42, x1 = row[row.length - 1].x + row[row.length - 1].ww + size * 0.42;\n    const y0 = row[0].y + size * 0.02, y1 = row[0].y + size * 1.2;\n    // A ragged brush edge: the top and bottom wander, the ends are cut on a slant.\n    const n = 14, seed = li * 31 + C.g.k * 7;\n    let d = \"M\" + (x0 + size * 0.12) + \" \" + y0;\n    for (let i = 1; i <= n; i++) d += \" L\" + (x0 + ((x1 - x0) * i) / n) + \" \" + (y0 + ($.hash(seed + i) - 0.5) * size * 0.1);\n    d += \" L\" + (x1 - size * 0.1) + \" \" + y1;\n    for (let i = n - 1; i >= 0; i--) d += \" L\" + (x0 + ((x1 - x0) * i) / n) + \" \" + (y1 + ($.hash(seed + 50 + i) - 0.5) * size * 0.1);\n    d += \" Z\";\n    // Each line's ribbon paints in as its first word comes.\n    const e = $.E.out3($.clamp((t - Math.max(C.g.start, row[0].s - 0.12)) / 0.32));\n    if (e > 0) {\n      const ribbon = $.path(d, { fill: deep, transform: \"translate(\" + size * 0.06 + \" \" + size * 0.08 + \")\" }) + $.path(d, { fill: teal });\n      back += $.clip(x0 - size * 0.2, y0 - size * 0.3, (x1 - x0 + size * 0.4) * e, y1 - y0 + size * 0.6, 0, ribbon);\n    }\n  }\n  C.lay.words.forEach(function (u) {\n    const since = t - (u.s - 0.03);\n    if (since < 0) return;\n    const sc = 1 + 0.32 * (1 - settle(since / 0.2));\n    s += $.g({ op: $.clamp(since / 0.06), transform: $.tf({ s: sc, ox: u.x + u.ww / 2, oy: u.y + size * 0.6 }) }, $.text(u.w, Object.assign({}, C.font, { x: u.x, y: u.y, fill: u.accent ? \"#FFE66D\" : \"#FFFFFF\" })));\n  });\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, C.g, t, $.g({ extra: 'filter=\"url(#mg-sm)\"' }, back + s)));\n}\n","libs":[]},{"id":"script-fill","name":"Script-fill captions","description":"Two lines of script set as fine outlines; each word fills in as it is said, the word being said in gold. Give a transcript with --words.","category":"captions","tags":["captions","script","social","transcript","template","reels"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* to make it the key word","default":"Welcome to the *new* way to caption.","maxLength":600},"accents":{"type":"text","label":"Key words","description":"words to treat as key words wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["bottom","center","top"],"default":"bottom"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"palette":{"type":"choice","label":"Colours","options":["original","brand"],"default":"original","description":"original: the look's own colours · brand: the brand's accents in their place"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/script-fill","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// Two lines of script set as fine outlines; each word fills in as it is said, the word being said in gold.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? 86 : A.wide ? 70 : 78) * k * sizeOf(p.size);\n  const C = captionLines($, A, ws, t, { size: size, font: { family: \"Dancing Script\", weight: 700, ls: 0 }, charW: 0.5, lines: 2, maxWords: 8, lh: 1.16, position: p.position });\n  if (!C) return \"\";\n  const gold = p.palette === \"brand\" ? c.accent : \"#FFCB47\";\n  let ci = -1;\n  C.lay.words.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  let lines = \"\", fills = \"\";\n  C.lay.words.forEach(function (u, j) {\n    const f = Object.assign({}, C.font, { x: u.x, y: u.y });\n    lines += $.text(u.w, Object.assign({}, f, { fill: \"none\", stroke: \"#FFFFFF\", sw: size * 0.028, op: 0.62 }));\n    const q = writeP($, t, u, 1);\n    if (q > 0) fills += writeOn($, u, size, q, $.text(u.w, Object.assign({}, f, { fill: j === ci ? gold : \"#FFFFFF\" })));\n  });\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, C.g, t, $.g({ extra: 'filter=\"url(#mg-sm)\"' }, lines + fills)));\n}\n","libs":[]},{"id":"doodle-script","name":"Doodle-script captions","description":"Handwritten words with yellow doodles: the key word in yellow, scribbled under and sparked as it is said. Give a transcript with --words.","category":"captions","tags":["captions","script","social","transcript","template","reels"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* to make it the key word","default":"Welcome to the *new* way to caption.","maxLength":600},"accents":{"type":"text","label":"Key words","description":"words to treat as key words wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["bottom","center","top"],"default":"bottom"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"palette":{"type":"choice","label":"Colours","options":["original","brand"],"default":"original","description":"original: the look's own colours · brand: the brand's accents in their place"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/doodle-script","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// Handwritten words with yellow doodles: the key word in yellow, scribbled under and sparked as it is said.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? 60 : A.wide ? 48 : 54) * k * sizeOf(p.size);\n  const C = captionLines($, A, ws, t, { size: size, font: { family: \"Homemade Apple\", weight: 400, ls: 0, gap: 0.75 }, charW: 0.7, lines: 2, maxWords: 6, lh: 1.65, position: p.position });\n  if (!C) return \"\";\n  const yellow = p.palette === \"brand\" ? c.accent : \"#FFD93D\";\n  const key = C.g.words[keyIndex(C.g.words)];\n  let s = \"\", doodles = \"\";\n  C.lay.words.forEach(function (u) {\n    const since = t - (u.s - 0.03);\n    const isKey = u.i === key.i;\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.3));\n      s += $.text(u.w, Object.assign({}, C.font, { x: u.x, y: u.y + (1 - e) * size * 0.2, fill: isKey || u.accent ? yellow : \"#FFFFFF\", op: $.clamp(since / 0.2) }));\n    }\n    if (!isKey) return;\n    // Under the key word: a scribble and a second, shorter pass; at its top right, three sparks.\n    const at = u.s + 0.08, x0 = u.x - size * 0.1, x1 = u.x + u.ww + size * 0.1, y = u.y + size * 1.2;\n    const sw = Math.max(3, size * 0.075);\n    doodles += $.ink(t, at, $.hand.line(x0, y, x1, y - size * 0.06, { seed: 3 + u.i }), { color: yellow, sw: sw, dur: 0.24 });\n    doodles += $.ink(t, at + 0.22, $.hand.line(x0 + size * 0.3, y + size * 0.14, x1 - size * 0.4, y + size * 0.1, { seed: 7 + u.i }), { color: yellow, sw: sw, dur: 0.18 });\n    const ox = u.x + u.ww + size * 0.06, oy = u.y + size * 0.3;\n    [-80, -45, -10].forEach(function (deg, i) {\n      const a = (deg * Math.PI) / 180, r0 = size * 0.12, r1 = size * 0.44;\n      doodles += $.ink(t, at + 0.36 + i * 0.07, $.hand.line(ox + Math.cos(a) * r0, oy + Math.sin(a) * r0, ox + Math.cos(a) * r1, oy + Math.sin(a) * r1, { seed: 11 + i }), { color: yellow, sw: sw * 0.85, dur: 0.1 });\n    });\n  });\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, C.g, t, $.g({ extra: 'filter=\"url(#mg-sm)\"' }, s + doodles)));\n}\n","libs":[]},{"id":"script-duet","name":"Script-duet captions","description":"Every other word swaps face: tall condensed white capitals and a flowing rose script, rising in word by word. Give a transcript with --words.","category":"captions","tags":["captions","script","social","transcript","template","reels"],"placement":"overlay","captionLook":true,"duration":{"default":6,"min":1,"max":120},"aspects":["9:16","16:9","1:1","4:5"],"props":{"text":{"type":"longtext","label":"Text","description":"what is said; leave empty to caption the transcript as it is. Wrap a word in *stars* to make it the key word","default":"Welcome to the *new* way to caption.","maxLength":600},"accents":{"type":"text","label":"Key words","description":"words to treat as key words wherever they are said, comma separated","default":"","maxLength":120},"position":{"type":"choice","label":"Position","options":["bottom","center","top"],"default":"bottom"},"size":{"type":"choice","label":"Size","options":["medium","small","large"],"default":"medium"},"palette":{"type":"choice","label":"Colours","options":["original","brand"],"default":"original","description":"original: the look's own colours · brand: the brand's accents in their place"},"pace":{"type":"number","label":"Seconds per word","description":"without a transcript","default":0.32,"min":0.08,"max":2,"step":0.01}},"beats":{"in":0.05},"parts":{"words":"the caption words and everything drawn with them"},"style":"tomato","pack":"captions","ref":"captions/script-duet","code":"// Captions on the voice. The words come from the transcript (--words) when there is one: its own words, or the\n// `text` you give timed to it. Without a transcript, `text` runs at an even pace. *Starred* words and the ones\n// listed in `accents` are the accent.\n\n/** Words with *marks* → [{ word, marked }]. */\nfunction markedWords(str) {\n  const out = [];\n  String(str || \"\").split(/\\s+/).filter(Boolean).forEach(function (w) {\n    const m = /^\\*(.*)\\*([.,!?;:]*)$/.exec(w);\n    out.push(m ? { word: m[1] + m[2], marked: true } : { word: w.replace(/\\*/g, \"\"), marked: false });\n  });\n  return out;\n}\n\n/** A word reduced to letters and digits, to compare it with another. */\nfunction plainWord(w) {\n  return String(w || \"\").toLowerCase().replace(/[^a-z0-9%À-ɏЀ-ӿऀ-ॿ]/g, \"\");\n}\n\n/** The case a caption is set in: \"upper\", \"lower\", \"as typed\", or the brand's own (\"style\"). */\nfunction inCase($, str, mode) {\n  if (mode === \"upper\") return String(str).toUpperCase();\n  if (mode === \"lower\") return String(str).toLowerCase();\n  if (mode === \"style\") return $.case(String(str));\n  return String(str);\n}\n\n/**\n * The words to caption, each with when it is said: [{ w, s, e, accent, i }]. With a transcript and no text, the\n * transcript's own words; with text, its words timed to the voice ($.sync) or, without a transcript, one every\n * `pace` seconds from `start`. A word ends when the next one starts (its own end for the last).\n */\nfunction captionWords($, p, start) {\n  const accents = String(p.accents || \"\").split(/[,\\s]+/).map(plainWord).filter(Boolean);\n  const marked = markedWords(String(p.text || \"\"));\n  let ws;\n  if (!marked.length && $.words.length) {\n    // A transcript's words keep their own times; `start` only paces text without one.\n    ws = $.words.map(function (w) { return { w: String(w.w), s: w.s, e: w.e, accent: false }; });\n  } else if (marked.length) {\n    const times = $.words.length\n      ? $.sync([marked.map(function (m) { return m.marked ? \"*\" + m.word + \"*\" : m.word; }).join(\" \")], 0, 0)[0].map(function (r) { return r.at; })\n      : marked.map(function (m, i) { return start + i * Math.max(0.05, Number(p.pace) || 0.3); });\n    ws = marked.map(function (m, i) { return { w: m.word, s: times[i], e: NaN, accent: m.marked }; });\n  } else return [];\n  // An editor's caption cues (`cues` when the graphic is built): each word belongs to the cue it is said in, and a\n  // word said in no cue is not captioned. A cue's last word ends with its cue, not when the next cue starts.\n  const cues = captionCues(p);\n  if (cues) {\n    ws = ws.filter(function (w) {\n      w.c = cueOf(cues, w.s);\n      return w.c >= 0;\n    });\n  }\n  return ws.map(function (w, i) {\n    const next = ws[i + 1];\n    const cue = cues ? cues[w.c] : null;\n    let e;\n    if (next && (!cue || next.c === w.c)) e = Math.max(w.s + 0.05, next.s);\n    else {\n      e = isFinite(w.e) && w.e > w.s ? w.e : w.s + Math.max(0.35, Number(p.pace) || 0.3);\n      if (cue) e = Math.max(w.s + 0.05, Math.min(e, cue[1]));\n    }\n    const out = { w: inCase($, w.w, p.case), s: w.s, e: e, end: isFinite(w.e) ? w.e : e, accent: w.accent || accents.indexOf(plainWord(w.w)) >= 0, i: i };\n    if (cue) {\n      out.c = w.c;\n      out.cs = cue[0];\n      out.ce = cue[1];\n    }\n    return out;\n  });\n}\n\n/** The editor's caption cues, [[start, end], …] in clip seconds and in order; null when the words are not cut into cues. */\nfunction captionCues(p) {\n  const raw = p && p.__cues;\n  if (!Array.isArray(raw) || !raw.length) return null;\n  const out = [];\n  raw.forEach(function (c) {\n    if (Array.isArray(c) && isFinite(c[0]) && isFinite(c[1]) && c[1] > c[0]) out.push([Number(c[0]), Number(c[1])]);\n  });\n  return out.length ? out : null;\n}\n\n/** The cue a moment is in (a word said a hair before its cue starts still counts), -1 when none. */\nfunction cueOf(cues, t) {\n  for (let k = 0; k < cues.length; k++) if (t >= cues[k][0] - 0.02 && t < cues[k][1]) return k;\n  return -1;\n}\n\n/**\n * Words grouped the way captions read: at most `maxChars` characters and `maxWords` words a group, a new group\n * after a sentence ends or at a pause in the voice. Words cut into an editor's cues are grouped by cue: a group\n * never holds words of two cues, and a cue longer than the look holds is shown in parts.\n */\nfunction phrases(ws, maxChars, maxWords) {\n  const out = [];\n  let cur = [], len = 0;\n  ws.forEach(function (w, i) {\n    const prev = ws[i - 1];\n    const byCue = w.c !== undefined;\n    const newCue = byCue && prev && prev.c !== w.c;\n    // Inside a cue the editor already chose where to break: only the look's own size limit splits it further.\n    const pause = !byCue && prev && w.s - prev.end > 0.45;\n    const ended = !byCue && prev && /[.!?…][\"”’)]*$/.test(prev.w);\n    // A sentence's last word stays with the words before it when it nearly fits, rather than standing alone.\n    const lastOfSentence = /[.!?…][\"”’)]*$/.test(w.w) || !ws[i + 1] || (byCue && ws[i + 1].c !== w.c);\n    const full = len + 1 + w.w.length > maxChars * (lastOfSentence ? 1.3 : 1) || cur.length >= maxWords + (lastOfSentence ? 1 : 0);\n    if (cur.length && (newCue || full || pause || ended)) {\n      out.push(cur);\n      cur = [];\n      len = 0;\n    }\n    len += (cur.length ? 1 : 0) + w.w.length;\n    cur.push(w);\n  });\n  if (cur.length) out.push(cur);\n  return out;\n}\n\n/**\n * The group on screen at t: { words, start, end, k } — on from just before its first word until the next group\n * starts (or `hold` seconds after its last word ends), k its index. Grouped by an editor's cues, a cue's first\n * group comes on when the cue starts and its last goes when the cue ends, so the caption is on screen exactly\n * while its cue is.\n */\nfunction groupAt(groups, t, hold) {\n  for (let k = groups.length - 1; k >= 0; k--) {\n    const g = groups[k], first = g[0], last = g[g.length - 1];\n    const prev = groups[k - 1], next = groups[k + 1];\n    const byCue = first.c !== undefined;\n    const start = byCue && (!prev || prev[0].c !== first.c) ? first.cs : first.s - 0.06;\n    if (t < start) continue;\n    const end = byCue && (!next || next[0].c !== last.c) ? last.ce : next ? next[0].s - 0.06 : last.end + (hold || 0.6);\n    return t < end ? { words: g, start: start, end: end, k: k } : null;\n  }\n  return null;\n}\n\n/**\n * Words laid out in lines no wider than maxW, each line centred on cx, the first line's top at top:\n * { words: [{ …word, x, y, ww, line }], lines, h }.\n */\nfunction lineUp($, ws, font, maxW, cx, top, lh) {\n  const gap = font.size * (font.gap || 0.26);\n  const lines = [];\n  let cur = [], curW = 0;\n  ws.forEach(function (w) {\n    const ww = $.measure(w.w, font);\n    if (cur.length && curW + gap + ww > maxW) {\n      lines.push({ ws: cur, w: curW });\n      cur = [];\n      curW = 0;\n    }\n    curW += (cur.length ? gap : 0) + ww;\n    cur.push({ w: w, ww: ww });\n  });\n  if (cur.length) lines.push({ ws: cur, w: curW });\n  // Two lines are balanced: the break goes where the longer line is shortest, not where the first one runs out.\n  if (lines.length === 2) {\n    const all = lines[0].ws.concat(lines[1].ws);\n    const width = function (us) { return us.reduce(function (a, u, i) { return a + u.ww + (i ? gap : 0); }, 0); };\n    let best = null;\n    for (let b = 1; b < all.length; b++) {\n      const w1 = width(all.slice(0, b)), w2 = width(all.slice(b));\n      if (w1 > maxW || w2 > maxW) continue;\n      if (!best || Math.max(w1, w2) < best.m) best = { b: b, m: Math.max(w1, w2), w1: w1, w2: w2 };\n    }\n    if (best) {\n      lines[0] = { ws: all.slice(0, best.b), w: best.w1 };\n      lines[1] = { ws: all.slice(best.b), w: best.w2 };\n    }\n  }\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.ws.forEach(function (u) {\n      out.push(Object.assign({}, u.w, { x: x, y: top + li * font.size * lh, ww: u.ww, line: li }));\n      x += u.ww + gap;\n    });\n  });\n  return { words: out, lines: lines.length, widths: lines.map(function (L) { return L.w; }), h: font.size * (1 + (lines.length - 1) * lh) };\n}\n\n/** Where captions sit: the centre line of the block for \"bottom\", \"center\" or \"top\", clear of a phone app's interface at 9:16. */\nfunction captionY($, A, position) {\n  if (position === \"top\") return $.H * (A.tall ? 0.2 : 0.17);\n  if (position === \"center\") return $.H * 0.5;\n  return $.H * (A.tall ? 0.7 : A.wide ? 0.83 : 0.8);\n}\n\n/** A soft dark vignette over the frame's edges, for words over footage. */\nfunction vignette($, a) {\n  const id = $.uid(\"cvg\"), W = $.W, H = $.H;\n  $.def('<radialGradient id=\"' + id + '\" cx=\"' + W / 2 + '\" cy=\"' + H / 2 + '\" r=\"' + Math.max(W, H) * 0.75 + '\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0.4\" stop-color=\"#000000\" stop-opacity=\"0\"/><stop offset=\"1\" stop-color=\"#000000\" stop-opacity=\"' + a + '\"/></radialGradient>');\n  return $.rect(0, 0, W, H, { fill: \"url(#\" + id + \")\" });\n}\n\n/** An ease that overshoots a little and settles. */\nfunction settle(x) {\n  const c1 = 1.70158, c3 = c1 + 1;\n  x = Math.max(0, Math.min(1, x));\n  return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);\n}\n\n/** A tick on every word as it is said, a pop on an accent. */\nfunction wordCues(ws) {\n  return ws.map(function (w, i) {\n    return w.accent ? { at: w.s, kind: \"pop\", level: 0.75 } : { at: w.s, kind: \"tick\", level: 0.6, pitch: (i % 3) - 1 };\n  });\n}\n\n/** The size multiplier for a `size` prop. */\nfunction sizeOf(size) {\n  return { small: 0.82, medium: 1, large: 1.22, huge: 1.45 }[size] || 1;\n}\n\n/**\n * The group on screen at t, laid out in lines at the caption position: { g, lay, font, maxW } or null.\n * o: role (the brand face), size, font (extra face settings), width (share of the safe width), lines, maxWords,\n * charW (a character's width in ems, to size phrases), lh, hold, position.\n */\nfunction captionLines($, A, ws, t, o) {\n  const font = Object.assign({}, $.font(o.role || \"display\"), o.font || {}, { size: o.size });\n  const maxW = A.w * (o.width || (A.tall ? 0.9 : 0.78));\n  const perLine = Math.max(6, Math.floor(maxW / (font.size * (o.charW || 0.58))));\n  const g = groupAt(phrases(ws, perLine * (o.lines || 2), o.maxWords || 10), t, o.hold || 0.7);\n  if (!g) return null;\n  const lh = o.lh || 1.16;\n  const probe = lineUp($, g.words, font, maxW, $.W / 2, 0, lh);\n  const lay = lineUp($, g.words, font, maxW, $.W / 2, captionY($, A, o.position) - probe.h / 2, lh);\n  return { g: g, lay: lay, font: font, maxW: maxW, lh: lh };\n}\n\n/** A group's markup with its entrance: it rises a touch and fades in as its first word comes. */\nfunction groupIn($, g, t, s) {\n  const e = $.E.out3($.clamp((t - g.start) / 0.16));\n  return $.g({ op: $.clamp((t - g.start) / 0.08), transform: $.tf({ y: (1 - e) * 14 * $.k }) }, s);\n}\n\n/** A Gaussian blur filter (sx, sy in px, rounded to half pixels), defined once per frame; \"\" when there is none. */\nfunction blurFilter($, cache, sx, sy) {\n  const x = Math.round(Math.max(0, sx) * 2) / 2, y = Math.round(Math.max(0, sy === undefined ? sx : sy) * 2) / 2;\n  if (x + y < 0.5) return \"\";\n  const key = x + \"x\" + y;\n  if (!cache[key]) {\n    cache[key] = $.uid(\"cb\");\n    $.def('<filter id=\"' + cache[key] + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + x + \" \" + y + '\"/></filter>');\n  }\n  return 'filter=\"url(#' + cache[key] + ')\"';\n}\n\n/** The words of a sentence-length group with their own faces and sizes, set on shared baselines and centred: [{ …word, x, y, ww, size, font, line }]. */\nfunction lineUpMixed($, ws, fontFor, maxW, cx, cy, lh) {\n  const items = ws.map(function (w) { const f = fontFor(w); return { w: w, f: f, ww: $.measure(w.w, f) }; });\n  // A face's `pad` (px) keeps extra room on both sides of its words, for marks drawn round them.\n  const gapOf = function (a, b) { return Math.max(a.f.size, b.f.size) * 0.24 + (a.f.pad || 0) + (b.f.pad || 0); };\n  const lines = [];\n  let cur = [], curW = 0;\n  items.forEach(function (it) {\n    const add = cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    if (cur.length && curW + add > maxW) { lines.push({ items: cur, w: curW }); cur = []; curW = 0; }\n    curW += cur.length ? gapOf(cur[cur.length - 1], it) + it.ww : it.ww;\n    cur.push(it);\n  });\n  if (cur.length) lines.push({ items: cur, w: curW });\n  // Baselines: each line one line-height (of its biggest word) below the last; then the block is centred on cy.\n  const sizes = lines.map(function (L) { return Math.max.apply(null, L.items.map(function (it) { return it.f.size; })); });\n  const bases = [];\n  sizes.forEach(function (sz, i) { bases.push(i ? bases[i - 1] + sz * lh : sz * 0.8); });\n  const top = 0, bottom = bases[bases.length - 1] + sizes[sizes.length - 1] * 0.22;\n  const shift = cy - (top + bottom) / 2;\n  const out = [];\n  lines.forEach(function (L, li) {\n    let x = cx - L.w / 2;\n    L.items.forEach(function (it, j) {\n      if (j) x += gapOf(L.items[j - 1], it);\n      out.push(Object.assign({}, it.w, { x: x, y: bases[li] + shift - it.f.size * 0.8, ww: it.ww, size: it.f.size, font: it.f, line: li }));\n      x += it.ww;\n    });\n  });\n  return out;\n}\n\n// ———— Caption looks: line captions described by a spec, and lockups built around a key word ————\n\n/** The word a group is built around: a starred or listed word, else the longest one. */\nfunction keyIndex(words) {\n  const marked = words.findIndex(function (w) { return w.accent; });\n  if (marked >= 0) return marked;\n  let best = 0;\n  words.forEach(function (w, i) { if (plainWord(w.w).length > plainWord(words[best].w).length) best = i; });\n  return best;\n}\n\n/** A look's face at a size: { family | role, weight, italic, ls (em), scale, pad (em of room either side, for swashes) }. */\nfunction lookFont($, f, size) {\n  const base = f.role ? $.font(f.role) : { family: f.family || \"Inter\", weight: f.weight || 700 };\n  return Object.assign({}, base, f.family ? { family: f.family } : {}, f.weight ? { weight: f.weight } : {}, { italic: !!f.italic, ls: f.ls || 0, size: size * (f.scale || 1), pad: size * (f.pad || 0) });\n}\n\n/** A look's colour, swapped for the brand's accents when `palette` is \"brand\". */\nfunction lookColor($, L, x) {\n  if ($.data.palette !== \"brand\" || !x) return x;\n  if (x === L.hero) return $.c.accent;\n  if (x === L.hero2) return $.c.accent2;\n  return x;\n}\n\n/**\n * Line captions from a look spec. L: size [tall, wide, square], font, rows (two faces, one per row, split at\n * `split` of the characters), first / accent / alt (a face for the first word / the starred or key word / every other word,\n * with fill and glow; a face that names `upper` or `lower` sets its own case),\n * colors { said, current, upcoming (null hides them), upOp, key }, stroke { color, w, cur } (em), glow { color, blur, op },\n * card { fill, r, px, py }, bars { fill }, box { fill, r }, underline { color }, enter (pop | rise | fade), shadow, maxWords,\n * charW, lh, hero / hero2 (the colours `palette: brand` swaps).\n */\nfunction lineLook(t, $, L) {\n  const p = $.data, k = $.k, W = $.W;\n  const A = safeArea($), M = motion($, t);\n  const ws = captionWords($, p, $.beat(\"in\", 0.05));\n  if (!ws.length) return \"\";\n  const size = (A.tall ? L.size[0] : A.wide ? L.size[1] : L.size[2]) * k * sizeOf(p.size);\n  const maxW = A.w * (A.tall ? 0.9 : 0.78);\n  const col = function (x) { return lookColor($, L, x); };\n  const perLine = Math.max(6, Math.floor(maxW / (size * (L.charW || 0.58))));\n  const g = groupAt(phrases(ws, Math.floor(perLine * (L.rows ? 1.7 : 2)), L.maxWords || 8), t, 0.7);\n  if (!g) return \"\";\n  const ki = keyIndex(g.words);\n  const up = function (s, f) { return f && f.upper ? String(s).toUpperCase() : f && f.lower ? String(s).toLowerCase() : s; };\n  let split = g.words.length;\n  if (L.rows) {\n    const total = g.words.reduce(function (a, w) { return a + w.w.length; }, 0);\n    let acc = 0;\n    split = 1;\n    for (let i = 0; i < g.words.length - 1; i++) { acc += g.words[i].w.length; split = i + 1; if (acc >= total * (L.split || 0.5)) break; }\n  }\n  const specOf = function (i, w) {\n    if (L.accent && (w.accent || (L.accent.auto && i === ki))) return L.accent;\n    if (L.first && i === 0) return L.first;\n    if (L.alt && i % 2 === 1) return L.alt;\n    return L.rows ? L.rows[i < split ? 0 : 1] : L.font;\n  };\n  const words = g.words.map(function (w, i) { const f = specOf(i, w); return Object.assign({}, w, { i: i, spec: f, key: i === ki, w: up(w.w, \"upper\" in f || \"lower\" in f ? f : L.font) }); });\n  let lay;\n  if (L.rows) {\n    // Two rows, each centred and cut down to fit; the block centred on the caption line.\n    const rows = [words.slice(0, split), words.slice(split)].filter(function (r) { return r.length; });\n    const laid = rows.map(function (r) {\n      let s = 1, out;\n      for (let n = 0; n < 2; n++) {\n        out = lineUpMixed($, r, function (w) { return lookFont($, w.spec, size * s); }, 1e9, W / 2, 0, 1);\n        const wide = out[out.length - 1].x + out[out.length - 1].ww - out[0].x;\n        if (wide <= maxW) break;\n        s *= maxW / wide;\n      }\n      return out;\n    });\n    const hs = laid.map(function (r) { return Math.max.apply(null, r.map(function (u) { return u.size; })); });\n    const gap = size * 0.12;\n    const total = hs.reduce(function (a, h) { return a + h; }, 0) + gap * (hs.length - 1);\n    let y = captionY($, A, p.position) - total / 2;\n    lay = [];\n    laid.forEach(function (r, ri) {\n      const shift = y - Math.min.apply(null, r.map(function (u) { return u.y; }));\n      r.forEach(function (u) { lay.push(Object.assign({}, u, { y: u.y + shift, line: ri })); });\n      y += hs[ri] + gap;\n    });\n  } else {\n    lay = lineUpMixed($, words, function (w) { return lookFont($, w.spec, size); }, maxW, W / 2, captionY($, A, p.position), L.lh || 1.18);\n  }\n  let ci = -1;\n  lay.forEach(function (u, j) { if (t >= u.s - 0.03) ci = j; });\n  const C = L.colors || {};\n  const blurs = {};\n  let back = \"\", glow = \"\", front = \"\";\n  // Behind the words: one card, a bar per line, or a box under the word being said.\n  const ext = function (us) {\n    return { x0: Math.min.apply(null, us.map(function (u) { return u.x; })), x1: Math.max.apply(null, us.map(function (u) { return u.x + u.ww; })),\n      y0: Math.min.apply(null, us.map(function (u) { return u.y; })), y1: Math.max.apply(null, us.map(function (u) { return u.y + u.size; })) };\n  };\n  if (L.card) {\n    const e = ext(lay), px = size * L.card.px, py = size * L.card.py;\n    back += $.rect(e.x0 - px, e.y0 - py, e.x1 - e.x0 + px * 2, e.y1 - e.y0 + py * 2, { r: size * L.card.r, fill: col(L.card.fill) });\n  }\n  if (L.bars) {\n    const lines = {};\n    lay.forEach(function (u) { (lines[u.line] = lines[u.line] || []).push(u); });\n    Object.keys(lines).forEach(function (li) {\n      const e = ext(lines[li]), px = size * 0.18;\n      const grow = $.E.out3($.clamp((t - g.start - li * 0.08) / 0.22));\n      back += $.rect(e.x0 - px, e.y0 - size * 0.04, (e.x1 - e.x0 + px * 2) * grow, e.y1 - e.y0 + size * 0.14, { r: size * 0.04, fill: col(L.bars.fill) });\n    });\n  }\n  if (L.box && ci >= 0) {\n    const cur = lay[ci], prev = lay[ci - 1], pad = size * 0.14;\n    const bx = function (u) { return { x: u.x - pad, y: u.y - pad * 0.5, w: u.ww + pad * 2, h: u.size * 1.1 + pad }; };\n    const b = bx(cur), e = $.E.out3($.clamp((t - cur.s + 0.03) / 0.12));\n    const a = prev && prev.line === cur.line ? bx(prev) : b;\n    back += $.rect($.lerp(a.x, b.x, e), $.lerp(a.y, b.y, e), $.lerp(a.w, b.w, e), $.lerp(a.h, b.h, e), { r: size * (L.box.r || 0.12), fill: col(L.box.fill) });\n  }\n  lay.forEach(function (u, j) {\n    const said = j <= ci, cur = j === ci, since = t - (u.s - 0.03);\n    if (!said && C.upcoming === null) return;\n    const rowFill = u.spec.fill;\n    let fill = rowFill || C.said || \"#FFFFFF\", op = 1;\n    if (!said) { fill = rowFill || C.upcoming || fill; op = C.upOp === undefined ? 0.4 : C.upOp; }\n    if (cur && C.current) fill = C.current;\n    if (u.key && C.key && said) fill = C.key;\n    if (u.accent && C.accent) fill = C.accent;\n    fill = col(fill);\n    let tf = null;\n    if (said && L.enter === \"pop\") tf = { s: 1 + 0.28 * (1 - settle(since / 0.2)) };\n    if (said && L.enter === \"rise\") { const e = $.E.out3($.clamp(since / 0.3)); tf = { y: (1 - e) * u.size * 0.28 }; op *= $.clamp(since / 0.2); }\n    if (said && L.enter === \"fade\") op *= $.clamp(since / 0.15);\n    const font = Object.assign({}, u.font, { x: u.x, y: u.y, fill: fill, op: op });\n    const stroke = u.spec.stroke || L.stroke;\n    if (stroke) Object.assign(font, { stroke: stroke.color, sw: u.size * (cur && stroke.cur ? stroke.cur : stroke.w) });\n    const wrap = function (m) { return tf ? $.g({ transform: $.tf(Object.assign({ ox: u.x + u.ww / 2, oy: u.y + u.size / 2 }, tf)) }, m) : m; };\n    const gl = u.spec.glow || L.glow;\n    if (gl && said) {\n      const blur = blurFilter($, blurs, u.size * gl.blur);\n      glow += $.g({ extra: blur }, wrap($.text(u.w, Object.assign({}, u.font, { x: u.x, y: u.y, fill: col(gl.color === \"self\" ? fill : gl.color), op: (gl.op || 0.8) * op }))));\n    }\n    front += wrap($.text(u.w, font));\n    if (cur && L.underline) {\n      const e = $.E.out3($.clamp(since / 0.16));\n      front += $.rect(u.x, u.y + u.size * 1.04, u.ww * e, Math.max(3, u.size * 0.07), { r: u.size * 0.035, fill: col(L.underline.color) });\n    }\n  });\n  let s = back + glow + front;\n  if (L.shadow) s = $.g({ extra: 'filter=\"url(#mg-' + L.shadow + ')\"' }, s);\n  return M.part(\"words\", { in: \"none\", out: \"fade\", outAt: $.dur - 0.22, outDur: 0.2, vary: false }, groupIn($, g, t, s));\n}\n\n/** The group on screen at t split around its key word: { g, before, key, after } (null when nothing is said). */\nfunction lockupAt($, t, A, maxWords) {\n  const ws = captionWords($, $.data, $.beat(\"in\", 0.05));\n  if (!ws.length) return null;\n  const g = groupAt(phrases(ws, A.tall ? 26 : 34, maxWords || 5), t, 0.6);\n  if (!g) return null;\n  const ki = keyIndex(g.words);\n  return { g: g, before: g.words.slice(0, ki), key: g.words[ki], after: g.words.slice(ki + 1) };\n}\n\n/** A run of words in one face from x (anchor start | end | middle) with its top at y, each fading in as it is said. */\nfunction lockRun($, t, words, f, x, y, anchor, fill, upper) {\n  const text = function (w) { return upper ? String(w.w).toUpperCase() : w.w; };\n  // The space between words (f.gap in ems, 0.26 by default), plus the tracking a spaced face carries after its last letter.\n  const gap = f.size * ((f.gap || 0.26) + Math.max(0, f.ls || 0));\n  const widths = words.map(function (w) { return $.measure(text(w), f); });\n  const total = widths.reduce(function (a, w) { return a + w; }, 0) + gap * Math.max(0, words.length - 1);\n  let cx = anchor === \"end\" ? x - total : anchor === \"middle\" ? x - total / 2 : x;\n  // Kept inside the safe area: a run that would cross its edge slides back in.\n  const A = safeArea($);\n  cx = Math.max(A.x, Math.min(cx, A.x + A.w - total));\n  let s = \"\";\n  words.forEach(function (w, i) {\n    const since = t - (w.s - 0.03);\n    if (since >= 0) {\n      const e = $.E.out3($.clamp(since / 0.25));\n      s += $.text(text(w), Object.assign({}, f, { x: cx, y: y + (1 - e) * f.size * 0.25, fill: fill, op: $.clamp(since / 0.15) }));\n    }\n    cx += widths[i] + gap;\n  });\n  return { s: s, w: total };\n}\n\n/** The key word's own entrance: in from a little bigger, settled. */\nfunction keyIn($, t, key, cx, cy) {\n  const since = t - (key.s - 0.03);\n  return { show: since >= 0, tf: $.tf({ s: 1 + 0.18 * (1 - settle(since / 0.26)), ox: cx, oy: cy }), op: $.clamp(since / 0.08) };\n}\n\n/** How far a word is written at t: 0 until it is said, 1 once written — rate × its time, between 0.16 and 0.6 s. */\nfunction writeP($, t, u, rate) {\n  const dur = Math.max(0.16, Math.min(0.6, (u.end - u.s) * (rate || 0.9)));\n  return $.clamp((t - (u.s - 0.03)) / dur);\n}\n\n/** A word's markup written on left to right: clipped to the first q (0–1) of its width, with room round it for a script's swashes. */\nfunction writeOn($, u, size, q, markup) {\n  if (q >= 1) return markup;\n  if (q <= 0) return \"\";\n  const pad = size * 0.5;\n  return $.clip(u.x - pad, u.y - size * 0.7, pad + u.ww * q, size * 2.4, 0, markup);\n}\n\n// Every other word swaps face: tall condensed white capitals and a flowing rose script, rising in word by word.\nconst LOOK = {\"size\": [88, 70, 80], \"font\": {\"family\": \"Bebas Neue\", \"weight\": 400, \"upper\": true}, \"alt\": {\"family\": \"Allura\", \"weight\": 400, \"upper\": false, \"scale\": 1.3, \"pad\": 0.12, \"fill\": \"#FFB3C7\"}, \"colors\": {\"said\": \"#FFFFFF\", \"upcoming\": null}, \"enter\": \"rise\", \"shadow\": \"sm\", \"hero\": \"#FFB3C7\", \"charW\": 0.5, \"maxWords\": 5, \"lh\": 1.12};\nfunction frame(t, $) {\n  return lineLook(t, $, LOOK);\n}\n","libs":[]}]},{"format":1,"id":"backdrops","name":"MONARC Backdrops","version":"1.0.0","description":"Grounds that move, to set titles, products or footage on: any of sixteen patterns, aurora, a Bauhaus field, a synthwave grid, lava blobs, a colour sweep, a starfield, bokeh, topographic lines, halftone screens, waves, orbits, a rippling dot grid and paper-cut hills.","author":"MONARC STUDIOS","license":"MONARC Packs License 1.0 (free to use in any video; not for resale as templates)","homepage":"https://volito.dev/docs/packs/backdrops","style":"memphis","shared":"shared.js","items":["pattern","aurora","tiles","retro-grid","blobs","gradient","starfield","bokeh","topo","halftone","waves","orbits","dot-grid","paper-cut"],"entries":[{"id":"pattern","name":"Pattern","description":"Any of the sixteen animated patterns as a full ground — mesh, halftone, dither, Bauhaus tiles, waves, rings, dots, confetti, pills, sunburst… — in the brand's colours or your own.","category":"scene","tags":["backdrop","background","loop","pattern","texture","motion background"],"placement":"full","duration":{"default":8,"min":2,"max":60},"aspects":["16:9","9:16","1:1","4:5"],"props":{"kind":{"type":"choice","label":"Pattern","options":["mesh","halftone","dither","tiles","bauhaus","waves","rings","dots","plus","graph","hatch","stripes","checker","confetti","pills","sunburst"],"default":"mesh"},"strength":{"type":"number","label":"Strength","default":1,"min":0.2,"max":2,"step":0.1},"scale":{"type":"number","label":"Scale","default":1,"min":0.4,"max":3,"step":0.1},"speed":{"type":"number","label":"Speed","default":1,"min":0,"max":4,"step":0.1},"color":{"type":"color","label":"Colour","default":"","description":"empty: the brand's colour"},"color2":{"type":"color","label":"Second colour","default":"","description":"empty: the brand's colour"},"vignette":{"type":"boolean","label":"Dark edges","default":false}},"parts":{"ground":"the ground","pattern":"the pattern"},"style":"memphis","pack":"backdrops","ref":"backdrops/pattern","code":"// Backdrops are grounds that move: put titles, products or footage over them. They loop smoothly.\n/** The brand's plain ground (no texture of its own: the backdrop is the texture). */\nfunction plain($, t) { return $.panel(t, { box: { x: 0, y: 0, w: $.W, h: $.H }, grid: false, glow: false, grain: false, texture: false }); }\n/** A soft dark edge round the frame (strength 0–1). */\nfunction vignetteOf($, a) {\n  return $.rect(0, 0, $.W, $.H, { fill: $.gradient([[0, \"#000000\", 0], [0.6, \"#000000\", 0], [1, \"#000000\", a]], { radial: true, r: 0.75 }) });\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const M = motion($, t);\n  let s = M.part(\"ground\", { in: \"none\", out: \"none\", vary: false }, plain($, t));\n  s += M.part(\"pattern\", { in: \"fade\", out: \"none\", dur: 0.6, vary: false }, $.pattern(t, p.kind, { a: p.strength, scale: p.scale, speed: p.speed, color: p.color || undefined, color2: p.color2 || undefined }));\n  if (p.vignette) s += vignetteOf($, 0.5);\n  return s;\n}\n","libs":[]},{"id":"aurora","name":"Aurora","description":"Soft blooms of the brand's colours drift and melt into each other under a fine grain — a calm, premium ground for titles.","category":"scene","tags":["backdrop","background","loop","aurora","mesh","gradient","glow","soft","premium"],"placement":"full","duration":{"default":8,"min":2,"max":60},"aspects":["16:9","9:16","1:1","4:5"],"props":{"intensity":{"type":"number","label":"Intensity","default":1.2,"min":0.4,"max":2,"step":0.1},"speed":{"type":"number","label":"Speed","default":1,"min":0,"max":3,"step":0.1},"vignette":{"type":"boolean","label":"Dark edges","default":false}},"parts":{"ground":"the ground","light":"the drifting light"},"style":"memphis","pack":"backdrops","ref":"backdrops/aurora","code":"// Backdrops are grounds that move: put titles, products or footage over them. They loop smoothly.\n/** The brand's plain ground (no texture of its own: the backdrop is the texture). */\nfunction plain($, t) { return $.panel(t, { box: { x: 0, y: 0, w: $.W, h: $.H }, grid: false, glow: false, grain: false, texture: false }); }\n/** A soft dark edge round the frame (strength 0–1). */\nfunction vignetteOf($, a) {\n  return $.rect(0, 0, $.W, $.H, { fill: $.gradient([[0, \"#000000\", 0], [0.6, \"#000000\", 0], [1, \"#000000\", a]], { radial: true, r: 0.75 }) });\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const M = motion($, t);\n  let s = M.part(\"ground\", { in: \"none\", out: \"none\", vary: false }, plain($, t));\n  let l = $.pattern(t, \"mesh\", { a: p.intensity, speed: p.speed * 1.4, scale: 1 });\n  // A second, smaller bloom in the second accent crossing the frame.\n  const x = W * (0.5 + 0.35 * Math.sin(t * 0.21 * p.speed)), y = H * (0.5 + 0.3 * Math.cos(t * 0.17 * p.speed));\n  l += $.circle(x, y, Math.max(W, H) * 0.35, { fill: $.gradient([[0, c.accent2, 0.35 * p.intensity], [1, c.accent2, 0]], { radial: true }) });\n  s += M.part(\"light\", { in: \"fade\", out: \"none\", dur: 0.8, vary: false }, l) + $.grain(t, { op: 0.06 });\n  if (p.vignette) s += vignetteOf($, 0.5);\n  return s;\n}\n","libs":[]},{"id":"tiles","name":"Bauhaus field","description":"A full field of Bauhaus tiles — quarter circles, half discs, discs, diamonds — turning in a diagonal wave, in the brand's colours.","category":"scene","tags":["backdrop","background","loop","bauhaus","tiles","geometric","shapes","grid","swiss"],"placement":"full","duration":{"default":8,"min":2,"max":60},"aspects":["16:9","9:16","1:1","4:5"],"props":{"size":{"type":"number","label":"Tile size","default":1.4,"min":0.5,"max":3,"step":0.1},"speed":{"type":"number","label":"Speed","default":1,"min":0,"max":3,"step":0.1},"vignette":{"type":"boolean","label":"Dark edges","default":false}},"parts":{"ground":"the ground","tiles":"the tiles"},"style":"memphis","pack":"backdrops","ref":"backdrops/tiles","code":"// Backdrops are grounds that move: put titles, products or footage over them. They loop smoothly.\n/** The brand's plain ground (no texture of its own: the backdrop is the texture). */\nfunction plain($, t) { return $.panel(t, { box: { x: 0, y: 0, w: $.W, h: $.H }, grid: false, glow: false, grain: false, texture: false }); }\n/** A soft dark edge round the frame (strength 0–1). */\nfunction vignetteOf($, a) {\n  return $.rect(0, 0, $.W, $.H, { fill: $.gradient([[0, \"#000000\", 0], [0.6, \"#000000\", 0], [1, \"#000000\", a]], { radial: true, r: 0.75 }) });\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const M = motion($, t);\n  let s = M.part(\"ground\", { in: \"none\", out: \"none\", vary: false }, plain($, t));\n  s += M.part(\"tiles\", { in: \"fade\", out: \"none\", dur: 0.5, vary: false }, $.pattern(t, \"tiles\", { a: 1, scale: p.size, speed: p.speed }));\n  if (p.vignette) s += vignetteOf($, 0.5);\n  return s;\n}\n","libs":[]},{"id":"retro-grid","name":"Retro grid","description":"Synthwave: a neon grid floor rushing toward you under a striped sun and a glowing horizon, stars twinkling above.","category":"scene","tags":["backdrop","background","loop","synthwave","retro","grid","80s","neon","vaporwave","sun"],"placement":"full","duration":{"default":8,"min":2,"max":60},"aspects":["16:9","9:16","1:1","4:5"],"props":{"speed":{"type":"number","label":"Speed","default":1,"min":0,"max":3,"step":0.1},"sun":{"type":"boolean","label":"Sun","default":true},"vignette":{"type":"boolean","label":"Dark edges","default":false}},"parts":{"ground":"the sky","sun":"the sun","floor":"the grid floor"},"style":"memphis","pack":"backdrops","ref":"backdrops/retro-grid","code":"// Backdrops are grounds that move: put titles, products or footage over them. They loop smoothly.\n/** The brand's plain ground (no texture of its own: the backdrop is the texture). */\nfunction plain($, t) { return $.panel(t, { box: { x: 0, y: 0, w: $.W, h: $.H }, grid: false, glow: false, grain: false, texture: false }); }\n/** A soft dark edge round the frame (strength 0–1). */\nfunction vignetteOf($, a) {\n  return $.rect(0, 0, $.W, $.H, { fill: $.gradient([[0, \"#000000\", 0], [0.6, \"#000000\", 0], [1, \"#000000\", a]], { radial: true, r: 0.75 }) });\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const M = motion($, t);\n  let s = M.part(\"ground\", { in: \"none\", out: \"none\", vary: false }, plain($, t));\n  const hz = H * 0.58, cx = W / 2;\n  // Sky: the ground darkening upward, a glow on the horizon, stars.\n  let sky = $.rect(0, 0, W, hz, { fill: $.gradient([[0, \"#000000\", 0.35], [1, c.accent, 0.28]], { angle: 180 }) });\n  for (let i = 0; i < 60; i++) {\n    const x = $.hash(i * 3.1) * W, y = $.hash(i * 7.3) * hz * 0.85, tw = 0.4 + 0.6 * Math.abs(Math.sin(t * (1 + $.hash(i)) + i));\n    sky += $.circle(x, y, (1 + 2 * $.hash(i * 5.5)) * k, { fill: c.fg, op: 0.6 * tw });\n  }\n  s += M.part(\"ground\", { in: \"none\", out: \"none\", vary: false }, sky);\n  if (p.sun) {\n    const R = Math.min(W, H) * 0.26, sy = hz - R * 0.35, id = $.uid(\"sun\");\n    let cuts = \"\";\n    for (let i = 0; i < 6; i++) { const yy = sy + R * (0.05 + i * 0.16) + ((t * 20 * k * p.speed) % (R * 0.16)), hh = R * (0.02 + i * 0.018); cuts += '<rect x=\"' + (cx - R) + '\" y=\"' + yy + '\" width=\"' + 2 * R + '\" height=\"' + hh + '\" fill=\"#000\"/>'; }\n    $.def('<mask id=\"' + id + '\" maskUnits=\"userSpaceOnUse\" x=\"' + (cx - R) + '\" y=\"' + (sy - R) + '\" width=\"' + 2 * R + '\" height=\"' + 2 * R + '\"><rect x=\"' + (cx - R) + '\" y=\"' + (sy - R) + '\" width=\"' + 2 * R + '\" height=\"' + 2 * R + '\" fill=\"#fff\"/>' + cuts + \"</mask>\");\n    s += M.part(\"sun\", { in: \"rise\", distance: 60, box: { x: cx - R, y: sy - R, w: 2 * R, h: 2 * R } }, $.circle(cx, sy, R * 1.35, { fill: $.gradient([[0, c.accent2, 0.35], [1, c.accent2, 0]], { radial: true }) }) + '<g mask=\"url(#' + id + ')\">' + $.circle(cx, sy, R, { fill: $.gradient([c.accent2, c.accent], { angle: 180 }) }) + \"</g>\");\n  }\n  // The floor: lines converging on the centre of the horizon; cross lines rushing toward the viewer.\n  let floor = $.rect(0, hz, W, H - hz, { fill: $.gradient([[0, c.accent, 0.25], [1, \"#000000\", 0.4]], { angle: 180 }) });\n  const fh = H - hz, sw = 2.5 * k;\n  for (let i = -16; i <= 16; i++) floor += $.line(cx + i * W * 0.012, hz, cx + i * W * 0.16, H + fh, { stroke: c.accent, sw: sw, op: 0.8 });\n  const ph = (t * 0.55 * p.speed) % 1;\n  for (let j = 0; j < 14; j++) {\n    const z = (j + 1 - ph) / 14, y = hz + fh * Math.pow(z, 2.6);\n    if (y > hz) floor += $.line(0, y, W, y, { stroke: c.accent, sw: sw * (0.4 + z), op: 0.25 + 0.7 * z });\n  }\n  floor += $.rect(0, hz - 2 * k, W, 4 * k, { fill: c.accent2 });\n  s += M.part(\"floor\", { in: \"none\", out: \"none\", vary: false }, floor);\n  if (p.vignette) s += vignetteOf($, 0.5);\n  return s;\n}\n","libs":[]},{"id":"blobs","name":"Lava blobs","description":"Gooey blobs in the accents drift, merge and pull apart like a lava lamp.","category":"scene","tags":["backdrop","background","loop","blobs","lava","goo","liquid","metaballs","organic"],"placement":"full","duration":{"default":8,"min":2,"max":60},"aspects":["16:9","9:16","1:1","4:5"],"props":{"count":{"type":"number","label":"Blobs","default":7,"min":3,"max":14,"step":1},"speed":{"type":"number","label":"Speed","default":1,"min":0,"max":3,"step":0.1},"vignette":{"type":"boolean","label":"Dark edges","default":false}},"parts":{"ground":"the ground","blobs":"the blobs"},"style":"memphis","pack":"backdrops","ref":"backdrops/blobs","code":"// Backdrops are grounds that move: put titles, products or footage over them. They loop smoothly.\n/** The brand's plain ground (no texture of its own: the backdrop is the texture). */\nfunction plain($, t) { return $.panel(t, { box: { x: 0, y: 0, w: $.W, h: $.H }, grid: false, glow: false, grain: false, texture: false }); }\n/** A soft dark edge round the frame (strength 0–1). */\nfunction vignetteOf($, a) {\n  return $.rect(0, 0, $.W, $.H, { fill: $.gradient([[0, \"#000000\", 0], [0.6, \"#000000\", 0], [1, \"#000000\", a]], { radial: true, r: 0.75 }) });\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const M = motion($, t);\n  let s = M.part(\"ground\", { in: \"none\", out: \"none\", vary: false }, plain($, t));\n  const n = Math.round(p.count), R = Math.min(W, H) * 0.13, fid = $.uid(\"goo\");\n  $.def('<filter id=\"' + fid + '\" x=\"-20%\" y=\"-20%\" width=\"140%\" height=\"140%\"><feGaussianBlur stdDeviation=\"' + 30 * k + '\"/><feColorMatrix type=\"matrix\" values=\"1 0 0 0 0  0 1 0 0 0  0 0 1 0 0  0 0 0 26 -11\"/></filter>');\n  const layer = function (col, off) {\n    let g = \"\";\n    for (let i = 0; i < n; i++) {\n      const sp = (0.15 + 0.2 * $.hash(i * 2.7 + off)) * p.speed, ph = $.hash(i * 9.1 + off) * 6.28;\n      const x = W * (0.5 + 0.42 * Math.sin(t * sp + ph)), y = H * (0.5 + 0.4 * Math.sin(t * sp * 1.3 + ph * 1.7));\n      g += $.circle(x, y, R * (0.6 + 0.8 * $.hash(i * 4.4 + off)), { fill: col });\n    }\n    return '<g filter=\"url(#' + fid + ')\">' + g + \"</g>\";\n  };\n  s += M.part(\"blobs\", { in: \"fade\", out: \"none\", dur: 0.6, vary: false }, layer(c.accent2, 3) + layer(c.accent, 0));\n  if (p.vignette) s += vignetteOf($, 0.5);\n  return s;\n}\n","libs":[]},{"id":"gradient","name":"Colour sweep","description":"A slow, rich gradient through the brand's colours that turns and shifts, with a light grain — the simplest premium ground.","category":"scene","tags":["backdrop","background","loop","gradient","sweep","colour","smooth","duotone"],"placement":"full","duration":{"default":8,"min":2,"max":60},"aspects":["16:9","9:16","1:1","4:5"],"props":{"speed":{"type":"number","label":"Speed","default":1,"min":0,"max":3,"step":0.1},"vignette":{"type":"boolean","label":"Dark edges","default":false}},"parts":{"ground":"the gradient"},"style":"memphis","pack":"backdrops","ref":"backdrops/gradient","code":"// Backdrops are grounds that move: put titles, products or footage over them. They loop smoothly.\n/** The brand's plain ground (no texture of its own: the backdrop is the texture). */\nfunction plain($, t) { return $.panel(t, { box: { x: 0, y: 0, w: $.W, h: $.H }, grid: false, glow: false, grain: false, texture: false }); }\n/** A soft dark edge round the frame (strength 0–1). */\nfunction vignetteOf($, a) {\n  return $.rect(0, 0, $.W, $.H, { fill: $.gradient([[0, \"#000000\", 0], [0.6, \"#000000\", 0], [1, \"#000000\", a]], { radial: true, r: 0.75 }) });\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, W = $.W, H = $.H;\n  const M = motion($, t);\n  const a = 120 + t * 9 * p.speed, sh = 0.08 * Math.sin(t * 0.4 * p.speed);\n  let g = $.rect(0, 0, W, H, { fill: $.gradient([[0, c.bg], [0.3 + sh, c.accent], [0.62 - sh, c.accent2], [1, c.bg]], { angle: a }) });\n  g += $.rect(0, 0, W, H, { fill: $.gradient([[0, c.bg, 0.55], [0.5, c.bg, 0], [1, c.bg, 0.5]], { angle: a + 90 }) });\n  let s = M.part(\"ground\", { in: \"none\", out: \"none\", vary: false }, g) + $.grain(t, { op: 0.07 });\n  if (p.vignette) s += vignetteOf($, 0.5);\n  return s;\n}\n\n/** A backdrop has no moments: just soft air as it comes up. */\nfunction sound($) {\n  return [{ at: 0, kind: \"swell\", level: 0.6 }];\n}\n","libs":[]},{"id":"starfield","name":"Starfield","description":"Stars stream toward you out of the dark — warp speed or a slow drift — some in the accents.","category":"scene","tags":["backdrop","background","loop","stars","space","warp","hyperspace","particles"],"placement":"full","duration":{"default":8,"min":2,"max":60},"aspects":["16:9","9:16","1:1","4:5"],"props":{"speed":{"type":"number","label":"Speed","default":1,"min":0.1,"max":4,"step":0.1},"streaks":{"type":"boolean","label":"Streaks","default":true},"vignette":{"type":"boolean","label":"Dark edges","default":false}},"parts":{"ground":"the ground","stars":"the stars"},"style":"memphis","pack":"backdrops","ref":"backdrops/starfield","code":"// Backdrops are grounds that move: put titles, products or footage over them. They loop smoothly.\n/** The brand's plain ground (no texture of its own: the backdrop is the texture). */\nfunction plain($, t) { return $.panel(t, { box: { x: 0, y: 0, w: $.W, h: $.H }, grid: false, glow: false, grain: false, texture: false }); }\n/** A soft dark edge round the frame (strength 0–1). */\nfunction vignetteOf($, a) {\n  return $.rect(0, 0, $.W, $.H, { fill: $.gradient([[0, \"#000000\", 0], [0.6, \"#000000\", 0], [1, \"#000000\", a]], { radial: true, r: 0.75 }) });\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const M = motion($, t);\n  let s = M.part(\"ground\", { in: \"none\", out: \"none\", vary: false }, plain($, t));\n  const n = 220, cx = W / 2, cy = H / 2, D = Math.hypot(W, H) * 0.6;\n  let st = \"\";\n  for (let i = 0; i < n; i++) {\n    const a = $.hash(i * 1.37) * 6.2832, r0 = 0.02 + $.hash(i * 7.1) * 0.98;\n    const z = ((r0 + t * 0.18 * p.speed * (0.6 + $.hash(i * 3.3))) % 1);\n    const d = Math.pow(z, 2.2) * D, d0 = Math.pow(Math.max(0, z - 0.04 * p.speed), 2.2) * D;\n    // On a light ground the stars are the accents and a soft ink, not solid ink.\n    const light = $.style.mode === \"light\";\n    const x = cx + Math.cos(a) * d, y = cy + Math.sin(a) * d, col = light ? (i % 3 === 0 ? c.accent : i % 3 === 1 ? c.accent2 : $.rgba(c.fg, 0.35)) : i % 9 === 0 ? c.accent : i % 13 === 0 ? c.accent2 : c.fg;\n    const sw = (0.8 + 3.2 * z) * k, op = Math.min(1, z * 2.4);\n    st += p.streaks ? $.line(cx + Math.cos(a) * d0, cy + Math.sin(a) * d0, x, y, { stroke: col, sw: sw, op: op }) : $.circle(x, y, sw, { fill: col, op: op });\n  }\n  s += M.part(\"stars\", { in: \"fade\", out: \"none\", dur: 0.4, vary: false }, st);\n  if (p.vignette) s += vignetteOf($, 0.5);\n  return s;\n}\n","libs":[]},{"id":"bokeh","name":"Bokeh","description":"Soft out-of-focus lights in the brand's colours drift and breathe — a warm, filmic ground.","category":"scene","tags":["backdrop","background","loop","bokeh","lights","blur","soft","particles","film"],"placement":"full","duration":{"default":8,"min":2,"max":60},"aspects":["16:9","9:16","1:1","4:5"],"props":{"count":{"type":"number","label":"Lights","default":26,"min":6,"max":60,"step":1},"speed":{"type":"number","label":"Speed","default":1,"min":0,"max":3,"step":0.1},"vignette":{"type":"boolean","label":"Dark edges","default":false}},"parts":{"ground":"the ground","lights":"the lights"},"style":"memphis","pack":"backdrops","ref":"backdrops/bokeh","code":"// Backdrops are grounds that move: put titles, products or footage over them. They loop smoothly.\n/** The brand's plain ground (no texture of its own: the backdrop is the texture). */\nfunction plain($, t) { return $.panel(t, { box: { x: 0, y: 0, w: $.W, h: $.H }, grid: false, glow: false, grain: false, texture: false }); }\n/** A soft dark edge round the frame (strength 0–1). */\nfunction vignetteOf($, a) {\n  return $.rect(0, 0, $.W, $.H, { fill: $.gradient([[0, \"#000000\", 0], [0.6, \"#000000\", 0], [1, \"#000000\", a]], { radial: true, r: 0.75 }) });\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const M = motion($, t);\n  let s = M.part(\"ground\", { in: \"none\", out: \"none\", vary: false }, plain($, t));\n  let l = \"\";\n  // On a light ground the third light is a blend of the accents, not ink (which reads as dirt) or a blend of the two (which turns grey).\n  const pal = [c.accent, c.accent2, $.style.mode === \"light\" ? $.mix(c.accent2, \"#FFFFFF\", 0.45) : c.fg];\n  for (let i = 0; i < Math.round(p.count); i++) {\n    const r = Math.min(W, H) * (0.03 + 0.12 * Math.pow($.hash(i * 2.3), 2));\n    const x = ($.hash(i * 5.1) * 1.2 - 0.1) * W + Math.sin(t * 0.3 * p.speed + i) * 40 * k, y = ($.hash(i * 8.7) * 1.2 - 0.1) * H - ((t * 14 * k * p.speed * (0.5 + $.hash(i))) % (H * 1.4)) + H * 0.2;\n    const yy = ((y % (H * 1.4)) + H * 1.4) % (H * 1.4) - H * 0.2;\n    const a = (0.18 + 0.35 * $.hash(i * 3.9)) * (0.75 + 0.25 * Math.sin(t * 1.3 + i * 2));\n    const col = pal[i % 3];\n    l += $.circle(x, yy, r, { fill: $.gradient([[0, col, a], [0.75, col, a * 0.8], [1, col, 0]], { radial: true }) });\n  }\n  s += M.part(\"lights\", { in: \"fade\", out: \"none\", dur: 0.6, vary: false }, l);\n  if (p.vignette) s += vignetteOf($, 0.5);\n  return s;\n}\n","libs":[]},{"id":"topo","name":"Topographic","description":"Contour lines like a topographic map, slowly shifting as if the land were breathing.","category":"scene","tags":["backdrop","background","loop","topographic","contour","lines","map","terrain","organic"],"placement":"full","duration":{"default":8,"min":2,"max":60},"aspects":["16:9","9:16","1:1","4:5"],"props":{"lines":{"type":"number","label":"Lines","default":14,"min":6,"max":40,"step":1},"speed":{"type":"number","label":"Speed","default":1,"min":0,"max":3,"step":0.1},"vignette":{"type":"boolean","label":"Dark edges","default":false}},"parts":{"ground":"the ground","lines":"the contour lines"},"style":"memphis","pack":"backdrops","ref":"backdrops/topo","code":"// Backdrops are grounds that move: put titles, products or footage over them. They loop smoothly.\n/** The brand's plain ground (no texture of its own: the backdrop is the texture). */\nfunction plain($, t) { return $.panel(t, { box: { x: 0, y: 0, w: $.W, h: $.H }, grid: false, glow: false, grain: false, texture: false }); }\n/** A soft dark edge round the frame (strength 0–1). */\nfunction vignetteOf($, a) {\n  return $.rect(0, 0, $.W, $.H, { fill: $.gradient([[0, \"#000000\", 0], [0.6, \"#000000\", 0], [1, \"#000000\", a]], { radial: true, r: 0.75 }) });\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const M = motion($, t);\n  let s = M.part(\"ground\", { in: \"none\", out: \"none\", vary: false }, plain($, t));\n  const n = Math.round(p.lines), peaks = [[0.3, 0.38, 0], [0.76, 0.66, 2.1]];\n  const D = Math.hypot(W, H);\n  let l = \"\";\n  peaks.forEach(function (pk, pi) {\n    const cx = W * pk[0], cy = H * pk[1];\n    for (let j = 1; j <= n; j++) {\n      const r = (j / n) * D * 0.55, pts = [];\n      for (let q = 0; q < 40; q++) {\n        const a = (q / 40) * 6.2832;\n        const w = 1 + 0.16 * Math.sin(3 * a + pk[2] + t * 0.25 * p.speed) + 0.08 * Math.sin(5 * a - j * 0.3 + t * 0.4 * p.speed) + 0.05 * Math.sin(9 * a + j * 0.5);\n        pts.push([cx + Math.cos(a) * r * w, cy + Math.sin(a) * r * w * 0.8]);\n      }\n      let d = \"M\" + (pts[0][0] + pts[39][0]) / 2 + \" \" + (pts[0][1] + pts[39][1]) / 2;\n      for (let q = 0; q < 40; q++) { const a1 = pts[q], b1 = pts[(q + 1) % 40]; d += \"Q\" + a1[0] + \" \" + a1[1] + \" \" + (a1[0] + b1[0]) / 2 + \" \" + (a1[1] + b1[1]) / 2; }\n      l += $.path(d + \"Z\", { stroke: j % 5 === 0 ? c.accent : $.rgba(c.fg, 0.18), sw: (j % 5 === 0 ? 3 : 1.8) * k, op: pi === 0 ? 1 : 0.65 });\n    }\n  });\n  s += M.part(\"lines\", { in: \"fade\", out: \"none\", dur: 0.6, vary: false }, l);\n  if (p.vignette) s += vignetteOf($, 0.5);\n  return s;\n}\n","libs":[]},{"id":"halftone","name":"Halftone","description":"A halftone field: a grid of dots whose size follows slow waves of light rolling across the frame, like newsprint breathing, with a second offset screen in the other accent printed over it.","category":"scene","tags":["halftone","dots","print","riso","pattern"],"placement":"full","duration":{"default":8,"min":2,"max":60},"props":{"scale":{"type":"number","label":"Dot size","default":1,"min":0.5,"max":2.5,"step":0.1},"speed":{"type":"number","label":"Speed","default":1,"min":0,"max":3,"step":0.1},"layers":{"type":"choice","label":"Screens","options":["two","one"],"default":"two"},"vignette":{"type":"boolean","label":"Dark edges","default":false}},"parts":{"ground":"the ground","dots":"the dots"},"beats":{"in":0.15,"out":-0.45},"style":"memphis","pack":"backdrops","ref":"backdrops/halftone","code":"// Backdrops are grounds that move: put titles, products or footage over them. They loop smoothly.\n/** The brand's plain ground (no texture of its own: the backdrop is the texture). */\nfunction plain($, t) { return $.panel(t, { box: { x: 0, y: 0, w: $.W, h: $.H }, grid: false, glow: false, grain: false, texture: false }); }\n/** A soft dark edge round the frame (strength 0–1). */\nfunction vignetteOf($, a) {\n  return $.rect(0, 0, $.W, $.H, { fill: $.gradient([[0, \"#000000\", 0], [0.6, \"#000000\", 0], [1, \"#000000\", a]], { radial: true, r: 0.75 }) });\n}\n\n// A halftone field: a grid of dots whose size follows a slow wave of light rolling across the frame, like\n// newsprint breathing; a second, offset screen in the other accent prints over it like riso.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const M = motion($, t);\n  let s = M.part(\"ground\", { in: \"none\", out: \"none\", vary: false }, plain($, t));\n  const step = Math.max(14, 34 * k * (Number(p.scale) || 1));\n  const sp = Number(p.speed) || 1;\n  const screen = function (col, off, ang) {\n    let g = \"\";\n    const ca = Math.cos(ang), sa = Math.sin(ang);\n    for (let y = -step; y < H + step; y += step) {\n      for (let x = -step; x < W + step; x += step) {\n        // Two slow waves crossing, plus a soft pool of light that drifts round the frame.\n        const u = (x * ca + y * sa) / Math.max(W, H), v = (y * ca - x * sa) / Math.max(W, H);\n        const lx = W * (0.5 + 0.3 * Math.sin(t * 0.25 * sp + off)), ly = H * (0.5 + 0.3 * Math.cos(t * 0.2 * sp + off));\n        const pool = Math.exp(-((x - lx) * (x - lx) + (y - ly) * (y - ly)) / (2 * Math.pow(Math.max(W, H) * 0.32, 2)));\n        const f = 0.5 + 0.25 * Math.sin((u * 6 + t * 0.6 * sp) * 2 + off) + 0.25 * Math.sin((v * 5 - t * 0.45 * sp) * 2) ;\n        const r = step * 0.48 * Math.max(0, Math.min(1, f * 0.55 + pool * 0.6));\n        if (r > step * 0.04) g += $.circle(x + off * step * 0.5, y + off * step * 0.5, r, { fill: col });\n      }\n    }\n    return g;\n  };\n  let field = screen(c.accent, 0, 0.26);\n  // The second screen overprints like ink on paper: multiply on a light ground, screen (light adding) on a dark one.\n  const blend = $.style.mode === \"light\" ? \"multiply\" : \"screen\";\n  if (p.layers !== \"one\") field = field + '<g style=\"mix-blend-mode:' + blend + '\" opacity=\"0.85\">' + screen(c.accent2, 1, -0.2) + \"</g>\";\n  s += M.part(\"dots\", { in: \"fade\", out: \"none\", dur: 0.6, vary: false }, field);\n  if (p.vignette) s += vignetteOf($, 0.5);\n  return s;\n}\n","libs":[]},{"id":"waves","name":"Waves","description":"Layered waves: bands from the brand's accents stacked up the frame, each a slow sum of sines drifting sideways at its own speed, the front ones deeper.","category":"scene","tags":["waves","sea","layers","flow","calm"],"placement":"full","duration":{"default":8,"min":2,"max":60},"props":{"layers":{"type":"number","label":"Layers","default":5,"min":2,"max":9},"height":{"type":"number","label":"Wave height","default":1,"min":0.2,"max":3,"step":0.1},"speed":{"type":"number","label":"Speed","default":1,"min":0,"max":3,"step":0.1},"vignette":{"type":"boolean","label":"Dark edges","default":false}},"parts":{"ground":"the ground","waves":"the waves"},"beats":{"in":0.15,"out":-0.45},"style":"memphis","pack":"backdrops","ref":"backdrops/waves","code":"// Backdrops are grounds that move: put titles, products or footage over them. They loop smoothly.\n/** The brand's plain ground (no texture of its own: the backdrop is the texture). */\nfunction plain($, t) { return $.panel(t, { box: { x: 0, y: 0, w: $.W, h: $.H }, grid: false, glow: false, grain: false, texture: false }); }\n/** A soft dark edge round the frame (strength 0–1). */\nfunction vignetteOf($, a) {\n  return $.rect(0, 0, $.W, $.H, { fill: $.gradient([[0, \"#000000\", 0], [0.6, \"#000000\", 0], [1, \"#000000\", a]], { radial: true, r: 0.75 }) });\n}\n\n// Layered waves: bands of colour from the brand's accents stacked up the frame, each a slow sum of sines\n// drifting sideways at its own speed, the front ones deeper.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const M = motion($, t);\n  let s = M.part(\"ground\", { in: \"none\", out: \"none\", vary: false }, plain($, t));\n  const n = Math.max(2, Math.min(9, Math.round(p.layers)));\n  const sp = Number(p.speed) || 1, amp = (Number(p.height) || 1) * H * 0.06;\n  let g = \"\";\n  for (let i = 0; i < n; i++) {\n    const f = i / Math.max(1, n - 1);\n    // Back layers sit higher and paler; front layers lower and stronger.\n    const base = H * (0.38 + 0.5 * f);\n    const col = $.mix($.mix(c.bg, c.accent2, 0.35), c.accent, f);\n    const pts = [];\n    for (let x = -20; x <= W + 20; x += W / 48) {\n      const y = base + amp * (1 + f * 0.6) * (Math.sin(x / W * 6.283 * (1.2 + f * 0.8) + t * 0.5 * sp * (1 + i * 0.3) + i * 1.7) * 0.6 + Math.sin(x / W * 6.283 * (2.7 + i * 0.4) - t * 0.35 * sp + i) * 0.4);\n      pts.push([x, y]);\n    }\n    g += $.path($.smooth(pts) + \"L\" + (W + 20) + \" \" + (H + 20) + \"L-20 \" + (H + 20) + \"Z\", { fill: col });\n  }\n  s += M.part(\"waves\", { in: \"fade\", out: \"none\", dur: 0.6, vary: false }, g);\n  if (p.vignette) s += vignetteOf($, 0.5);\n  return s;\n}\n","libs":[]},{"id":"orbits","name":"Orbits","description":"Orbits: tilted rings round a glowing core with small planets travelling along them at their own speeds, the far side of each ring dimmer so it reads as depth.","category":"scene","tags":["orbits","planets","space","rings","tech"],"placement":"full","duration":{"default":8,"min":2,"max":60},"props":{"rings":{"type":"number","label":"Rings","default":4,"min":2,"max":8},"speed":{"type":"number","label":"Speed","default":1,"min":0,"max":3,"step":0.1},"vignette":{"type":"boolean","label":"Dark edges","default":false}},"parts":{"ground":"the ground","orbits":"the rings, core and planets"},"beats":{"in":0.15,"out":-0.45},"style":"memphis","pack":"backdrops","ref":"backdrops/orbits","code":"// Backdrops are grounds that move: put titles, products or footage over them. They loop smoothly.\n/** The brand's plain ground (no texture of its own: the backdrop is the texture). */\nfunction plain($, t) { return $.panel(t, { box: { x: 0, y: 0, w: $.W, h: $.H }, grid: false, glow: false, grain: false, texture: false }); }\n/** A soft dark edge round the frame (strength 0–1). */\nfunction vignetteOf($, a) {\n  return $.rect(0, 0, $.W, $.H, { fill: $.gradient([[0, \"#000000\", 0], [0.6, \"#000000\", 0], [1, \"#000000\", a]], { radial: true, r: 0.75 }) });\n}\n\n// Orbits: tilted rings round a glowing core, small planets travelling along them at their own speeds, the far\n// side of each ring dimmer so it reads as depth.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const M = motion($, t);\n  let s = M.part(\"ground\", { in: \"none\", out: \"none\", vary: false }, plain($, t));\n  const n = Math.max(2, Math.min(8, Math.round(p.rings)));\n  const cx = W / 2, cy = H / 2, R = Math.min(W, H) * 0.46, tilt = 0.32, rot = -0.28;\n  const sp = Number(p.speed) || 1;\n  const P = function (rx, a) { const x = rx * Math.cos(a), y = rx * tilt * Math.sin(a); return [cx + x * Math.cos(rot) - y * Math.sin(rot), cy + x * Math.sin(rot) + y * Math.cos(rot)]; };\n  const ring = function (rx, from, to, col, op, sw) {\n    const pts = [];\n    for (let i = 0; i <= 48; i++) pts.push(P(rx, from + (to - from) * i / 48));\n    return $.path($.smooth(pts), { stroke: col, sw: sw, op: op });\n  };\n  let back = \"\", front = \"\", planetsBack = \"\", planetsFront = \"\";\n  for (let i = 0; i < n; i++) {\n    const rx = R * (0.32 + 0.68 * (i + 1) / n), col = i % 2 ? c.accent2 : c.accent;\n    // The far half (behind the core) dim, the near half bright.\n    back += ring(rx, Math.PI, 2 * Math.PI, c.fg, 0.14, 2 * k);\n    front += ring(rx, 0, Math.PI, c.fg, 0.32, 2.5 * k);\n    const a = ((t * sp * 0.6) / (0.6 + i * 0.35) + $.hash(i * 3.1) * 6.283) % (2 * Math.PI);\n    const q = P(rx, a), near = Math.sin(a) > 0;\n    const pr = (9 + 7 * $.hash(i * 5.7)) * k * (near ? 1.15 : 0.85);\n    const pl = $.circle(q[0], q[1], pr, { fill: col }) + $.circle(q[0], q[1], pr * 2.4, { fill: col, op: 0.12 });\n    if (near) planetsFront += pl; else planetsBack += pl;\n  }\n  const core = $.circle(cx, cy, R * 0.16, { fill: $.gradient([[0, c.accent, 1], [1, c.accent2, 1]], { angle: 135 }) }) + $.circle(cx, cy, R * 0.28, { fill: $.gradient([[0, c.accent, 0.35], [1, c.accent, 0]], { radial: true }) });\n  s += M.part(\"orbits\", { in: \"fade\", out: \"none\", dur: 0.6, vary: false }, back + planetsBack + core + front + planetsFront);\n  if (p.vignette) s += vignetteOf($, 0.5);\n  return s;\n}\n","libs":[]},{"id":"dot-grid","name":"Dot grid","description":"A grid of small dots with ripples running out from a few points: as a ripple passes, the dots swell and take the accent, then settle — a quiet, techy ground for titles.","category":"scene","tags":["dots","grid","ripple","tech","minimal"],"placement":"full","duration":{"default":8,"min":2,"max":60},"props":{"spacing":{"type":"number","label":"Spacing","default":1,"min":0.5,"max":2.5,"step":0.1},"sources":{"type":"number","label":"Ripple sources","default":2,"min":1,"max":3},"speed":{"type":"number","label":"Speed","default":1,"min":0,"max":3,"step":0.1},"vignette":{"type":"boolean","label":"Dark edges","default":false}},"parts":{"ground":"the ground","dots":"the dots"},"beats":{"in":0.15,"out":-0.45},"style":"memphis","pack":"backdrops","ref":"backdrops/dot-grid","code":"// Backdrops are grounds that move: put titles, products or footage over them. They loop smoothly.\n/** The brand's plain ground (no texture of its own: the backdrop is the texture). */\nfunction plain($, t) { return $.panel(t, { box: { x: 0, y: 0, w: $.W, h: $.H }, grid: false, glow: false, grain: false, texture: false }); }\n/** A soft dark edge round the frame (strength 0–1). */\nfunction vignetteOf($, a) {\n  return $.rect(0, 0, $.W, $.H, { fill: $.gradient([[0, \"#000000\", 0], [0.6, \"#000000\", 0], [1, \"#000000\", a]], { radial: true, r: 0.75 }) });\n}\n\n// A grid of small dots with ripples running out from a few points: as a ripple passes, the dots swell and take\n// the accent, then settle back — a quiet, techy ground for titles.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const M = motion($, t);\n  let s = M.part(\"ground\", { in: \"none\", out: \"none\", vary: false }, plain($, t));\n  const step = Math.max(16, 44 * k * (Number(p.spacing) || 1));\n  const sp = Number(p.speed) || 1;\n  // Ripple sources wander slowly; each ripple travels out at a fixed speed and repeats.\n  const src = [[0.3, 0.4], [0.72, 0.62], [0.5, 0.2]].slice(0, Math.max(1, Math.min(3, Math.round(p.sources))));\n  const period = 3.2 / sp, vel = Math.max(W, H) * 0.42 * sp;\n  let g = \"\";\n  for (let y = step / 2; y < H; y += step) {\n    for (let x = step / 2; x < W; x += step) {\n      let e = 0;\n      src.forEach(function (q, i) {\n        const sx = W * (q[0] + 0.05 * Math.sin(t * 0.3 + i)), sy = H * (q[1] + 0.05 * Math.cos(t * 0.27 + i * 2));\n        const d = Math.hypot(x - sx, y - sy);\n        const ph = ((t + i * period / src.length) % period) * vel;\n        e = Math.max(e, Math.exp(-Math.pow((d - ph) / (step * 1.8), 2)) * Math.max(0, 1 - ph / (Math.max(W, H) * 1.1)));\n      });\n      const r = step * (0.07 + 0.16 * e);\n      // Resting dots are faint text colour; a passing ripple lights them in the accent.\n      g += e > 0.12 ? $.circle(x, y, r, { fill: c.accent, op: 0.3 + 0.7 * e }) : $.circle(x, y, r, { fill: c.fg, op: 0.16 + 0.6 * e });\n    }\n  }\n  s += M.part(\"dots\", { in: \"fade\", out: \"none\", dur: 0.6, vary: false }, g);\n  if (p.vignette) s += vignetteOf($, 0.5);\n  return s;\n}\n","libs":[]},{"id":"paper-cut","name":"Paper-cut hills","description":"Paper-cut hills: layers of cut paper from pale to the accent, each casting a soft shadow on the one behind and drifting at its own depth, with a paper sun low in the sky.","category":"scene","tags":["paper","hills","layers","landscape","parallax"],"placement":"full","duration":{"default":8,"min":2,"max":60},"props":{"layers":{"type":"number","label":"Layers","default":5,"min":2,"max":7},"speed":{"type":"number","label":"Speed","default":1,"min":0,"max":3,"step":0.1},"vignette":{"type":"boolean","label":"Dark edges","default":false}},"parts":{"ground":"the ground","layers":"the sun and paper layers"},"beats":{"in":0.15,"out":-0.45},"style":"memphis","pack":"backdrops","ref":"backdrops/paper-cut","code":"// Backdrops are grounds that move: put titles, products or footage over them. They loop smoothly.\n/** The brand's plain ground (no texture of its own: the backdrop is the texture). */\nfunction plain($, t) { return $.panel(t, { box: { x: 0, y: 0, w: $.W, h: $.H }, grid: false, glow: false, grain: false, texture: false }); }\n/** A soft dark edge round the frame (strength 0–1). */\nfunction vignetteOf($, a) {\n  return $.rect(0, 0, $.W, $.H, { fill: $.gradient([[0, \"#000000\", 0], [0.6, \"#000000\", 0], [1, \"#000000\", a]], { radial: true, r: 0.75 }) });\n}\n\n// Paper-cut hills: layers of cut paper from pale to the accent, each with a soft shadow on the one behind,\n// drifting at their own depth so the scene has parallax; a paper sun sits low in the sky.\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const M = motion($, t);\n  let s = M.part(\"ground\", { in: \"none\", out: \"none\", vary: false }, plain($, t));\n  const n = Math.max(2, Math.min(7, Math.round(p.layers)));\n  const sp = Number(p.speed) || 1;\n  // The sun first, behind every layer.\n  let g = $.circle(W * 0.68, H * (0.34 + 0.02 * Math.sin(t * 0.3)), Math.min(W, H) * 0.12, { fill: $.mix(c.bg, c.accent2, 0.55) });\n  for (let i = 0; i < n; i++) {\n    const f = i / Math.max(1, n - 1);\n    const top = H * (0.42 + 0.42 * f);\n    const col = $.mix($.mix(c.bg, c.accent2, 0.25), c.accent, f);\n    // Each layer slides sideways in a slow loop; nearer layers travel further.\n    const dx = Math.sin(t * 0.18 * sp + i) * W * 0.04 * (0.4 + f);\n    const pts = [];\n    for (let x = -W * 0.1; x <= W * 1.1; x += W / 36) {\n      const y = top + H * 0.06 * (Math.sin((x + dx) / W * 6.283 * (0.9 + i * 0.35) + i * 2.1) * 0.7 + Math.sin((x + dx) / W * 6.283 * (2.3 + i * 0.2) + i) * 0.3);\n      pts.push([x, y]);\n    }\n    const d = $.smooth(pts) + \"L\" + W * 1.1 + \" \" + (H + 20) + \"L\" + -W * 0.1 + \" \" + (H + 20) + \"Z\";\n    g += $.path(d, { fill: \"#000000\", op: 0.12, transform: \"translate(0 \" + -10 * k + \")\", extra: 'filter=\"url(#mg-md)\"' });\n    g += $.path(d, { fill: col });\n  }\n  s += M.part(\"layers\", { in: \"fade\", out: \"none\", dur: 0.6, vary: false }, g);\n  if (p.vignette) s += vignetteOf($, 0.4);\n  return s;\n}\n","libs":[]}]},{"format":1,"id":"promo","name":"MONARC Promo","version":"1.0.0","description":"Offers that sell: a sale starburst, a price card that counts down, a coupon with a code to copy, an offer countdown, a feature checklist, a call to action with a click, a launch title and crossing tape ribbons.","author":"MONARC STUDIOS","license":"MONARC Packs License 1.0 (free to use in any video; not for resale as templates)","homepage":"https://volito.dev/docs/packs/promo","style":"candy","shared":"shared.js","items":["sale-badge","price-card","coupon","offer-countdown","features","cta","launch","ribbon"],"entries":[{"id":"sale-badge","name":"Sale badge","description":"A starburst badge slams in spinning — 50% OFF — then wobbles while a dashed ring turns inside it.","category":"cta","tags":["promo","sale","offer","badge","starburst","discount","percent","off","sticker"],"placement":"overlay","duration":{"default":3,"min":1,"max":15},"aspects":["16:9","9:16","1:1","4:5"],"props":{"big":{"type":"text","label":"Big","default":"50%","maxLength":6},"small":{"type":"text","label":"Small","default":"Off","maxLength":12},"x":{"type":"number","label":"Centre across (%)","default":78,"min":0,"max":100,"step":1},"y":{"type":"number","label":"Centre down (%)","default":30,"min":0,"max":100,"step":1},"size":{"type":"number","label":"Size","default":100,"min":40,"max":200,"step":5}},"parts":{"badge":"the starburst","words":"the words"},"beats":{"in":0.1},"style":"candy","pack":"promo","ref":"promo/sale-badge","code":"// Promo pieces: offers, prices, codes and calls to action, in the brand's surface (bold styles get ink\n// outlines and hard shadows).\n/** True when the brand draws ink outlines and hard shadows. */\nfunction boldSurface($) { return $.style.surface === \"bold\"; }\n/** A filled shape path with the brand's outline and hard shadow when it has them. */\nfunction shapeFill($, d, fill) {\n  const k = $.k;\n  if (!boldSurface($)) return $.path(d, { fill: fill, shadow: \"md\" });\n  return '<g transform=\"translate(' + 8 * k + \" \" + 8 * k + ')\">' + $.path(d, { fill: $.c.ink }) + \"</g>\" + $.path(d, { fill: fill, stroke: $.c.ink, sw: 4 * k });\n}\n/** Money: \"$1,299\" or \"1,299 €\" (currency after when it is a word or ends with a space). */\nfunction money(cur, n, dec) {\n  const v = Number(n).toLocaleString(\"en-US\", { minimumFractionDigits: dec, maximumFractionDigits: dec });\n  return /^[A-Za-z]{2,}$/.test(cur) ? v + \" \" + cur : cur + v;\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const A = safeArea($), M = motion($, t);\n  const tin = $.beat(\"in\", 0.1);\n  let s = \"\";\n  const R = Math.min(W, H) * 0.17 * p.size / 100, cx = Math.min(W - R * 1.1, Math.max(R * 1.1, W * p.x / 100)), cy = Math.min(H - R * 1.1, Math.max(R * 1.1, H * p.y / 100));\n  let d = \"\";\n  for (let i = 0; i < 36; i++) { const a = (i / 36) * 6.2832, r = i % 2 ? R * 0.86 : R; d += (i ? \"L\" : \"M\") + (cx + Math.cos(a) * r) + \" \" + (cy + Math.sin(a) * r); }\n  const wob = Math.sin((t - tin) * 3) * 4, spin = (t - tin) * 18;\n  let badge = shapeFill($, d + \"Z\", c.accent);\n  badge += $.circle(cx, cy, R * 0.7, { stroke: $.on(c.accent), sw: 3 * k, dash: 10 * k + \" \" + 8 * k, transform: \"rotate(\" + spin + \" \" + cx + \" \" + cy + \")\", op: 0.6 });\n  const big = $.case(p.big), small = $.case(p.small);\n  const bf = fitFont($, big, \"display\", R * 0.66, R * 1.3, { ls: -0.04 }), sf = fitFont($, small, \"display\", R * 0.3, R * 1.1, { ls: 0.02 });\n  const words = $.text(big, Object.assign({}, bf, { x: cx, y: cy - bf.size * 0.7, anchor: \"middle\", fill: $.on(c.accent) })) + (small ? $.text(small, Object.assign({}, sf, { x: cx, y: cy + bf.size * 0.32, anchor: \"middle\", fill: $.on(c.accent) })) : \"\");\n  const box = { x: cx - R, y: cy - R, w: 2 * R, h: 2 * R };\n  s += '<g transform=\"rotate(' + wob + \" \" + cx + \" \" + cy + ')\">' + M.part(\"badge\", { at: tin, in: \"spin\", box: box }, badge) + M.part(\"words\", { at: tin + 0.3, in: \"pop\", box: box }, words) + \"</g>\";\n  return s;\n}\n","libs":[]},{"id":"price-card","name":"Price card","description":"A product card: the old price is struck through, the new price counts down to it, a saving chip pops and the button gets pressed.","category":"cta","tags":["promo","sale","offer","price","product","card","discount","shop","ecommerce"],"placement":"overlay","duration":{"default":5,"min":2,"max":20},"aspects":["16:9","9:16","1:1","4:5"],"props":{"kicker":{"type":"text","label":"Kicker","default":"Weekend deal","maxLength":24},"name":{"type":"text","label":"Product","default":"Studio headphones","maxLength":32},"was":{"type":"number","label":"Was","default":199,"min":0,"max":10000000},"now":{"type":"number","label":"Now","default":149,"min":0,"max":10000000},"currency":{"type":"text","label":"Currency","default":"$","maxLength":4},"decimals":{"type":"number","label":"Decimals","default":0,"min":0,"max":2,"step":1},"cta":{"type":"text","label":"Button","default":"Shop now","maxLength":16},"added":{"type":"text","label":"After the click","default":"Added","maxLength":16},"position":{"type":"choice","label":"Position","options":["center","left","right"],"default":"center"}},"parts":{"card":"the card","price":"the prices","save":"the saving chip","button":"the button and cursor"},"beats":{"in":0.1},"style":"candy","pack":"promo","ref":"promo/price-card","code":"// Promo pieces: offers, prices, codes and calls to action, in the brand's surface (bold styles get ink\n// outlines and hard shadows).\n/** True when the brand draws ink outlines and hard shadows. */\nfunction boldSurface($) { return $.style.surface === \"bold\"; }\n/** A filled shape path with the brand's outline and hard shadow when it has them. */\nfunction shapeFill($, d, fill) {\n  const k = $.k;\n  if (!boldSurface($)) return $.path(d, { fill: fill, shadow: \"md\" });\n  return '<g transform=\"translate(' + 8 * k + \" \" + 8 * k + ')\">' + $.path(d, { fill: $.c.ink }) + \"</g>\" + $.path(d, { fill: fill, stroke: $.c.ink, sw: 4 * k });\n}\n/** Money: \"$1,299\" or \"1,299 €\" (currency after when it is a word or ends with a space). */\nfunction money(cur, n, dec) {\n  const v = Number(n).toLocaleString(\"en-US\", { minimumFractionDigits: dec, maximumFractionDigits: dec });\n  return /^[A-Za-z]{2,}$/.test(cur) ? v + \" \" + cur : cur + v;\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const A = safeArea($), M = motion($, t);\n  const tin = $.beat(\"in\", 0.1);\n  let s = \"\";\n  const cw = Math.min(A.w * (A.wide ? 0.42 : 0.9), 760 * k), pad = 44 * k;\n  const nf = Object.assign({}, $.font(\"display\"), { size: 58 * k, ls: -0.02 });\n  const nameLines = $.wrap($.case(p.name), cw - pad * 2, nf).slice(0, 2);\n  const pf = Object.assign({}, $.font(\"display\"), { size: 120 * k, ls: -0.03 }), of = Object.assign({}, $.font(\"text\"), { size: 44 * k });\n  const ch = pad * 2 + 40 * k + 20 * k + nameLines.length * nf.size * 1.08 + 24 * k + pf.size * 1.05 + 30 * k + 96 * k;\n  const cx = p.position === \"left\" ? A.x : p.position === \"right\" ? A.x + A.w - cw : W / 2 - cw / 2, cy = H / 2 - ch / 2;\n  let y = cy + pad;\n  let card = $.card(cx, cy, cw, ch, { t: t });\n  card += $.chip($.case(p.kicker), cx + pad, y, { size: 26 * k, fill: c.accent2, color: $.on(c.accent2) });\n  y += 60 * k;\n  nameLines.forEach(function (l, i) { card += $.text(l, Object.assign({}, nf, { x: cx + pad, y: y + i * nf.size * 1.08, fill: c.fg })); });\n  y += nameLines.length * nf.size * 1.08 + 24 * k;\n  s += M.part(\"card\", { at: tin, in: \"rise\", distance: 40, box: { x: cx, y: cy, w: cw, h: ch } }, card);\n  // The price counts down from the old one to the new.\n  const tc = tin + 0.7, q = $.E.out3($.seg(t, tc, tc + 0.9));\n  const nowV = $.lerp(p.was, p.now, q), nowS = money(p.currency, p.decimals > 0 ? nowV : Math.round(nowV), p.decimals);\n  const wasS = money(p.currency, p.was, p.decimals), ww = $.measure(wasS, of);\n  let price = $.text(nowS, Object.assign({}, pf, { x: cx + pad, y: y, fill: c.fg }));\n  const px = cx + pad + $.measure(money(p.currency, p.now, p.decimals), pf) + 26 * k, py = y + pf.size * 0.5 - of.size * 0.5;\n  price += $.text(wasS, Object.assign({}, of, { x: px, y: py, fill: c.mute }));\n  price += $.strike(t, tc, px - 4 * k, py + of.size * 0.55, px + ww + 4 * k, py + of.size * 0.5, { color: c.red, w: 6 * k });\n  s += M.part(\"price\", { at: tin + 0.3, in: \"fade\" }, price);\n  const save = p.was > 0 ? Math.round((1 - p.now / p.was) * 100) : 0;\n  if (save > 0) {\n    // The saving sits in the card's top-right corner, clear of both prices.\n    const st = \"-\" + save + \"%\", sz = $.chip.size(st, { size: 34 * k }), sx = cx + cw - pad - sz.w, sy = cy + pad - 4 * k;\n    s += M.part(\"save\", { at: tc + 0.9, in: \"pop\", box: { x: sx, y: sy, w: sz.w, h: sz.h } }, $.chip(st, sx, sy, { size: 34 * k, fill: c.accent, color: $.on(c.accent) }));\n  }\n  y += pf.size * 1.05 + 30 * k;\n  const press = tc + 1.9;\n  let btn = $.button(t, $.case(p.cta), cx + pad, y, { size: 38 * k, fill: c.accent, icon: \"cart\", pressAt: press, done: $.case(p.added || \"Added\"), anchor: \"start\" });\n  const bx = cx + pad + 160 * k, by = y + 40 * k;\n  const glide = $.E.inOut3($.seg(t, press - 0.8, press - 0.05));\n  btn += $.cursor($.lerp(W * 0.9, bx, glide), $.lerp(H * 1.05, by, glide), { press: $.bump(t, press, 0.05, 0.2), ripple: $.seg(t, press, press + 0.5) });\n  s += M.part(\"button\", { at: tin + 0.5, in: \"rise\", distance: 20, box: { x: cx + pad, y: y, w: cw - 2 * pad, h: 96 * k } }, btn);\n  return s;\n}\n","libs":[]},{"id":"coupon","name":"Coupon","description":"A ticket with a perforated tear line tears in: the deal on the stub, the code in a dashed box — then a click copies it.","category":"cta","tags":["promo","sale","offer","coupon","code","voucher","ticket","promo code","discount"],"placement":"overlay","duration":{"default":4,"min":1.5,"max":20},"aspects":["16:9","9:16","1:1","4:5"],"props":{"deal":{"type":"text","label":"Deal","default":"20% off","maxLength":12},"note":{"type":"text","label":"Note","default":"your first order","maxLength":32},"code":{"type":"text","label":"Code","default":"SAVE20","maxLength":16},"copied":{"type":"text","label":"After the click","default":"Copied","maxLength":12}},"parts":{"ticket":"the ticket","code":"the code box","copied":"the copied chip"},"beats":{"in":0.1},"style":"candy","pack":"promo","ref":"promo/coupon","code":"// Promo pieces: offers, prices, codes and calls to action, in the brand's surface (bold styles get ink\n// outlines and hard shadows).\n/** True when the brand draws ink outlines and hard shadows. */\nfunction boldSurface($) { return $.style.surface === \"bold\"; }\n/** A filled shape path with the brand's outline and hard shadow when it has them. */\nfunction shapeFill($, d, fill) {\n  const k = $.k;\n  if (!boldSurface($)) return $.path(d, { fill: fill, shadow: \"md\" });\n  return '<g transform=\"translate(' + 8 * k + \" \" + 8 * k + ')\">' + $.path(d, { fill: $.c.ink }) + \"</g>\" + $.path(d, { fill: fill, stroke: $.c.ink, sw: 4 * k });\n}\n/** Money: \"$1,299\" or \"1,299 €\" (currency after when it is a word or ends with a space). */\nfunction money(cur, n, dec) {\n  const v = Number(n).toLocaleString(\"en-US\", { minimumFractionDigits: dec, maximumFractionDigits: dec });\n  return /^[A-Za-z]{2,}$/.test(cur) ? v + \" \" + cur : cur + v;\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const A = safeArea($), M = motion($, t);\n  const tin = $.beat(\"in\", 0.1);\n  let s = \"\";\n  const tw = Math.min(A.w * 0.94, 1200 * k), th = Math.min(A.h * 0.42, tw * 0.36), x = W / 2 - tw / 2, y = H / 2 - th / 2;\n  const stub = tw * 0.42, n = 22 * k, r = Math.min(24 * k, th * 0.08);\n  // The ticket: rounded corners, a half-round notch top and bottom at the tear line.\n  const sx = x + stub;\n  const d = \"M\" + (x + r) + \" \" + y + \"H\" + (sx - n) + \"A\" + n + \" \" + n + \" 0 0 0 \" + (sx + n) + \" \" + y + \"H\" + (x + tw - r) + \"Q\" + (x + tw) + \" \" + y + \" \" + (x + tw) + \" \" + (y + r) + \"V\" + (y + th - r) + \"Q\" + (x + tw) + \" \" + (y + th) + \" \" + (x + tw - r) + \" \" + (y + th) + \"H\" + (sx + n) + \"A\" + n + \" \" + n + \" 0 0 0 \" + (sx - n) + \" \" + (y + th) + \"H\" + (x + r) + \"Q\" + x + \" \" + (y + th) + \" \" + x + \" \" + (y + th - r) + \"V\" + (y + r) + \"Q\" + x + \" \" + y + \" \" + (x + r) + \" \" + y + \"Z\";\n  let tk = shapeFill($, d, c.surface) + $.clip(x, y, stub, th, 0, $.path(d, { fill: c.accent }));\n  tk += $.line(sx, y + n + 8 * k, sx, y + th - n - 8 * k, { stroke: $.rgba(c.fg, 0.35), sw: 3 * k, dash: 10 * k + \" \" + 10 * k, cap: \"butt\" });\n  const deal = $.case(p.deal), df = fitFont($, deal, \"display\", th * 0.36, stub * 0.84, { ls: -0.03 });\n  const nf = fitFont($, p.note, \"text\", Math.max(26 * k, th * 0.1), stub * 0.84, {});\n  tk += $.text(deal, Object.assign({}, df, { x: x + stub / 2, y: y + th / 2 - df.size * 0.72, anchor: \"middle\", fill: $.on(c.accent) }));\n  if (p.note) tk += $.text(p.note, Object.assign({}, nf, { x: x + stub / 2, y: y + th / 2 + df.size * 0.38, anchor: \"middle\", fill: $.rgba($.on(c.accent), 0.8) }));\n  s += M.part(\"ticket\", { at: tin, in: \"stretch\", box: { x: x, y: y, w: tw, h: th } }, tk);\n  // The code in a dashed box on the right.\n  const code = String(p.code || \"\").toUpperCase(), cf = fitFont($, code, \"mono\", th * 0.22, (tw - stub) * 0.7, { ls: 0.08 });\n  const lf = Object.assign({}, $.font(\"text\"), { size: Math.max(24 * k, th * 0.085) });\n  const rx = sx + (tw - stub) / 2, bw = $.measure(code, cf) + 70 * k, bh = cf.size * 1.7, by = y + th / 2 - bh / 2 + lf.size * 0.6;\n  const copyAt = tin + 1.5;\n  const lit = t >= copyAt;\n  let cd = $.text(\"Use code\", Object.assign({}, lf, { x: rx, y: by - lf.size * 1.6, anchor: \"middle\", fill: c.mute }));\n  cd += $.rect(rx - bw / 2, by, bw, bh, { r: 12 * k, fill: lit ? $.rgba(c.accent, 0.14) : \"none\", stroke: c.accent, sw: 4 * k, dash: 14 * k + \" \" + 9 * k });\n  cd += $.text(code, Object.assign({}, cf, { x: rx, y: by + (bh - cf.size) / 2 - cf.size * 0.06, anchor: \"middle\", fill: c.fg }));\n  const glide = $.E.inOut3($.seg(t, copyAt - 0.7, copyAt - 0.05));\n  cd += $.cursor($.lerp(x + tw + 60 * k, rx + bw * 0.2, glide), $.lerp(y + th + 80 * k, by + bh * 0.6, glide), { press: $.bump(t, copyAt, 0.05, 0.2), ripple: $.seg(t, copyAt, copyAt + 0.5) });\n  s += M.part(\"code\", { at: tin + 0.4, in: \"fade\" }, cd);\n  if (p.copied) {\n    const ct = $.case(p.copied), co = { size: 28 * k, fill: c.green, color: $.on(c.green), anchor: \"middle\", icon: \"check\" }, cz = $.chip.size(ct, co);\n    s += M.part(\"copied\", { at: copyAt + 0.1, in: \"pop\", box: { x: rx - cz.w / 2, y: by + bh + 18 * k, w: cz.w, h: cz.h } }, $.chip(ct, rx, by + bh + 18 * k, co));\n  }\n  return s;\n}\n","libs":[]},{"id":"offer-countdown","name":"Offer countdown","description":"Offer ends in — days, hours, minutes and seconds in tiles, the seconds rolling down live. Sits low in the frame.","category":"timer","tags":["promo","sale","offer","countdown","timer","deadline","urgency","ends","limited"],"placement":"overlay","duration":{"default":5,"min":2,"max":60},"aspects":["16:9","9:16","1:1","4:5"],"props":{"label":{"type":"text","label":"Label","default":"Offer ends in","maxLength":28},"hours":{"type":"number","label":"Hours left","default":26,"min":0,"max":9999},"minutes":{"type":"number","label":"And minutes","default":41,"min":0,"max":59,"step":1},"days":{"type":"boolean","label":"Show days","default":true}},"parts":{"panel":"the panel","digits":"the digit tiles"},"beats":{"in":0.1},"style":"candy","pack":"promo","ref":"promo/offer-countdown","code":"// Promo pieces: offers, prices, codes and calls to action, in the brand's surface (bold styles get ink\n// outlines and hard shadows).\n/** True when the brand draws ink outlines and hard shadows. */\nfunction boldSurface($) { return $.style.surface === \"bold\"; }\n/** A filled shape path with the brand's outline and hard shadow when it has them. */\nfunction shapeFill($, d, fill) {\n  const k = $.k;\n  if (!boldSurface($)) return $.path(d, { fill: fill, shadow: \"md\" });\n  return '<g transform=\"translate(' + 8 * k + \" \" + 8 * k + ')\">' + $.path(d, { fill: $.c.ink }) + \"</g>\" + $.path(d, { fill: fill, stroke: $.c.ink, sw: 4 * k });\n}\n/** Money: \"$1,299\" or \"1,299 €\" (currency after when it is a word or ends with a space). */\nfunction money(cur, n, dec) {\n  const v = Number(n).toLocaleString(\"en-US\", { minimumFractionDigits: dec, maximumFractionDigits: dec });\n  return /^[A-Za-z]{2,}$/.test(cur) ? v + \" \" + cur : cur + v;\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const A = safeArea($), M = motion($, t);\n  const tin = $.beat(\"in\", 0.1);\n  let s = \"\";\n  const start = (p.hours * 3600 + p.minutes * 60 + 17) - Math.max(0, t - tin);\n  const left = Math.max(0, start);\n  const parts = p.days ? [[Math.floor(left / 86400), \"days\"], [Math.floor(left / 3600) % 24, \"hrs\"], [Math.floor(left / 60) % 60, \"min\"], [Math.floor(left) % 60, \"sec\"]] : [[Math.floor(left / 3600), \"hrs\"], [Math.floor(left / 60) % 60, \"min\"], [Math.floor(left) % 60, \"sec\"]];\n  const n = parts.length, lab = $.case(p.label);\n  const tileW = Math.min(A.w * 0.9 / (n * 1.25), 190 * k), tileH = tileW * 1.05, gap = tileW * 0.25;\n  const lf = Object.assign({}, $.font(\"display\"), { size: tileW * 0.3 }), df = Object.assign({}, $.font(\"mono\"), { size: tileW * 0.58, weight: 700 }), uf = Object.assign({}, $.font(\"text\"), { size: tileW * 0.16 });\n  const rowW = n * tileW + (n - 1) * gap, pw = Math.max(rowW, $.measure(lab, lf)) + 80 * k, ph = 40 * k + lf.size * 1.3 + tileH + uf.size * 1.8 + 30 * k;\n  const px = W / 2 - pw / 2, py = A.y + A.h - ph;\n  s += M.part(\"panel\", { at: tin, in: \"rise\", distance: 40, box: { x: px, y: py, w: pw, h: ph } }, $.card(px, py, pw, ph, { t: t }) + $.text(lab, Object.assign({}, lf, { x: W / 2, y: py + 34 * k, anchor: \"middle\", fill: c.fg })));\n  const ty = py + 34 * k + lf.size * 1.3;\n  let digits = \"\";\n  parts.forEach(function (q, i) {\n    const x = W / 2 - rowW / 2 + i * (tileW + gap), last = i === n - 1;\n    const v = String(q[0]).padStart(2, \"0\");\n    // The seconds roll: the new value drops in over the first quarter of each second.\n    const fr = (start % 1 + 1) % 1, roll = last ? $.E.out3($.clamp((1 - fr) / 0.25)) : 1;\n    let inner = $.text(v, Object.assign({}, df, { x: x + tileW / 2, y: ty + (tileH - df.size) / 2 - df.size * 0.08 - (1 - roll) * tileH * 0.5, anchor: \"middle\", fill: last ? $.on(c.accent) : c.fg, op: roll }));\n    digits += $.rect(x, ty, tileW, tileH, { r: 14 * k, fill: last ? c.accent : $.rgba(c.fg, 0.08) }) + $.clip(x, ty, tileW, tileH, 14 * k, inner);\n    digits += $.text(q[1], Object.assign({}, uf, { x: x + tileW / 2, y: ty + tileH + uf.size * 0.5, anchor: \"middle\", fill: c.mute }));\n    if (i < n - 1) digits += $.text(\":\", Object.assign({}, df, { x: x + tileW + gap / 2, y: ty + (tileH - df.size) / 2 - df.size * 0.1, anchor: \"middle\", fill: c.mute }));\n  });\n  s += M.part(\"digits\", { at: tin + 0.25, in: \"rise\", split: \"none\", distance: 20, box: { x: W / 2 - rowW / 2, y: ty, w: rowW, h: tileH } }, digits);\n  return s;\n}\n","libs":[]},{"id":"features","name":"Feature list","description":"A title, then features tick in one by one — a check draws in its circle and the line slides across.","category":"list","tags":["promo","sale","offer","features","checklist","benefits","list","ticks","included"],"placement":"full","duration":{"default":5,"min":2,"max":30},"aspects":["16:9","9:16","1:1","4:5"],"props":{"title":{"type":"text","label":"Title","default":"Everything you need","maxLength":40},"features":{"type":"list","label":"Features","of":"text","default":["Unlimited projects","4K exports","Brand styles","Team sharing"],"maxItems":6}},"parts":{"ground":"the ground","title":"the title","rows":"the feature rows"},"beats":{"in":0.1},"style":"candy","pack":"promo","ref":"promo/features","code":"// Promo pieces: offers, prices, codes and calls to action, in the brand's surface (bold styles get ink\n// outlines and hard shadows).\n/** True when the brand draws ink outlines and hard shadows. */\nfunction boldSurface($) { return $.style.surface === \"bold\"; }\n/** A filled shape path with the brand's outline and hard shadow when it has them. */\nfunction shapeFill($, d, fill) {\n  const k = $.k;\n  if (!boldSurface($)) return $.path(d, { fill: fill, shadow: \"md\" });\n  return '<g transform=\"translate(' + 8 * k + \" \" + 8 * k + ')\">' + $.path(d, { fill: $.c.ink }) + \"</g>\" + $.path(d, { fill: fill, stroke: $.c.ink, sw: 4 * k });\n}\n/** Money: \"$1,299\" or \"1,299 €\" (currency after when it is a word or ends with a space). */\nfunction money(cur, n, dec) {\n  const v = Number(n).toLocaleString(\"en-US\", { minimumFractionDigits: dec, maximumFractionDigits: dec });\n  return /^[A-Za-z]{2,}$/.test(cur) ? v + \" \" + cur : cur + v;\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const A = safeArea($), M = motion($, t);\n  const tin = $.beat(\"in\", 0.1);\n  let s = \"\";\n  s += M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: W, h: H } }));\n  const rows = (p.features || []).filter(Boolean).slice(0, 6);\n  const tf = fitLines($, $.case(p.title), Object.assign({}, $.font(\"display\"), { ls: -0.03 }), A.w * 0.9, Math.min(120 * k, A.h * 0.16), 50 * k, 2);\n  const rf = Object.assign({}, $.font(\"text\"), { size: Math.min(56 * k, (A.h * 0.62) / Math.max(1, rows.length) / 1.9), weight: 600 });\n  const rowH = rf.size * 1.9, listW = Math.min(A.w * 0.9, widest($, rows, rf) + rf.size * 2.2);\n  const total = tf.lines.length * tf.size * 1.05 + 50 * k + rows.length * rowH;\n  let y = H / 2 - total / 2;\n  tf.lines.forEach(function (l, i) { s += M.text(\"title\", Object.assign({ at: tin + i * 0.1, in: \"rise\" }, tf.font, { x: W / 2, y: y + i * tf.size * 1.05, anchor: \"middle\", fill: c.fg }), l); });\n  y += tf.lines.length * tf.size * 1.05 + 50 * k;\n  const x0 = W / 2 - listW / 2, r = rf.size * 0.62;\n  rows.forEach(function (row, i) {\n    const a = tin + 0.5 + i * 0.32, cy = y + i * rowH + rowH / 2;\n    const ck = $.E.out3($.seg(t, a + 0.15, a + 0.45));\n    let g = $.circle(x0 + r, cy, r, { fill: c.accent }) + (ck > 0 ? $.path(\"M\" + (x0 + r * 0.55) + \" \" + cy + \"L\" + (x0 + r * 0.88) + \" \" + (cy + r * 0.32) + \"L\" + (x0 + r * 1.48) + \" \" + (cy - r * 0.36), { stroke: $.on(c.accent), sw: r * 0.24, draw: ck }) : \"\");\n    g += $.text(row, Object.assign({}, rf, { x: x0 + r * 2 + rf.size * 0.5, y: cy - rf.size * 0.56, fill: c.fg, maxW: listW - r * 3 }));\n    if (i < rows.length - 1) g += $.rect(x0, y + (i + 1) * rowH - 1 * k, listW, 2 * k, { fill: c.line });\n    s += M.part(\"rows\", { at: a, in: \"slide\", from: \"left\", distance: 30, box: { x: x0, y: cy - rowH / 2, w: listW, h: rowH } }, g);\n  });\n  return s;\n}\n","libs":[]},{"id":"cta","name":"Call to action","description":"A short line, then a big button — a cursor glides on, presses it with a ripple, and it flips to the done label.","category":"cta","tags":["promo","sale","offer","cta","button","click","shop now","sign up","download","link in bio"],"placement":"overlay","duration":{"default":4,"min":1.5,"max":15},"aspects":["16:9","9:16","1:1","4:5"],"props":{"line":{"type":"text","label":"Line","default":"Ready when you are","maxLength":40,"description":"empty for none"},"button":{"type":"text","label":"Button","default":"Get started","maxLength":20},"done":{"type":"text","label":"After the click","default":"You're in","maxLength":20},"icon":{"type":"icon","label":"Icon","default":"arrowRight"},"position":{"type":"choice","label":"Position","options":["center","bottom"],"default":"center"}},"parts":{"line":"the line","button":"the button","cursor":"the cursor"},"beats":{"in":0.1},"style":"candy","pack":"promo","ref":"promo/cta","code":"// Promo pieces: offers, prices, codes and calls to action, in the brand's surface (bold styles get ink\n// outlines and hard shadows).\n/** True when the brand draws ink outlines and hard shadows. */\nfunction boldSurface($) { return $.style.surface === \"bold\"; }\n/** A filled shape path with the brand's outline and hard shadow when it has them. */\nfunction shapeFill($, d, fill) {\n  const k = $.k;\n  if (!boldSurface($)) return $.path(d, { fill: fill, shadow: \"md\" });\n  return '<g transform=\"translate(' + 8 * k + \" \" + 8 * k + ')\">' + $.path(d, { fill: $.c.ink }) + \"</g>\" + $.path(d, { fill: fill, stroke: $.c.ink, sw: 4 * k });\n}\n/** Money: \"$1,299\" or \"1,299 €\" (currency after when it is a word or ends with a space). */\nfunction money(cur, n, dec) {\n  const v = Number(n).toLocaleString(\"en-US\", { minimumFractionDigits: dec, maximumFractionDigits: dec });\n  return /^[A-Za-z]{2,}$/.test(cur) ? v + \" \" + cur : cur + v;\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const A = safeArea($), M = motion($, t);\n  const tin = $.beat(\"in\", 0.1);\n  let s = \"\";\n  const size = Math.min(64 * k, A.w / 14);\n  const by = p.position === \"bottom\" ? A.y + A.h - size * 2.4 : H / 2 + (p.line ? size * 0.4 : -size);\n  if (p.line) {\n    const lf = fitFont($, $.case(p.line), \"display\", size * 1.5, A.w * 0.9, { ls: -0.03 });\n    s += M.text(\"line\", Object.assign({ at: tin, in: \"rise\", split: \"words\" }, lf, { x: W / 2, y: by - lf.size * 1.6, anchor: \"middle\", fill: c.fg }), $.case(p.line));\n  }\n  const press = tin + 1.5;\n  s += M.part(\"button\", { at: tin + 0.3, in: \"pop\", box: { x: W / 2 - size * 4, y: by, w: size * 8, h: size * 2 } }, $.button(t, $.case(p.button), W / 2, by, { size: size, fill: c.accent, icon: p.icon || \"arrowRight\", pressAt: press, done: $.case(p.done || p.button), anchor: \"middle\" }));\n  const glide = $.E.inOut3($.seg(t, press - 0.75, press - 0.05)), bx = W / 2 + size * 1.2, byy = by + size * 1.1;\n  s += M.part(\"cursor\", { at: press - 0.8, in: \"fade\", dur: 0.2 }, $.cursor($.lerp(W * 0.86, bx, glide), $.lerp(H * 0.98, byy, glide), { press: $.bump(t, press, 0.05, 0.2), ripple: $.seg(t, press, press + 0.5), scale: 2.6 * k }));\n  return s;\n}\n","libs":[]},{"id":"launch","name":"Launch","description":"A launch title: a NEW sticker spins on, the product name lands huge over the brand's pattern, then the tagline and when it is out.","category":"title","tags":["promo","sale","offer","launch","new","product","reveal","announcement","drop"],"placement":"full","duration":{"default":5,"min":2,"max":20},"aspects":["16:9","9:16","1:1","4:5"],"props":{"badge":{"type":"text","label":"Sticker","default":"New","maxLength":8},"name":{"type":"text","label":"Name","default":"Aero Pro","maxLength":28},"tagline":{"type":"text","label":"Tagline","default":"Lighter than anything we've made","maxLength":64},"when":{"type":"text","label":"When","default":"Available now","maxLength":28,"description":"empty for none"}},"parts":{"ground":"the ground","badge":"the sticker","name":"the name","tagline":"the tagline","when":"the availability chip"},"beats":{"in":0.1},"style":"candy","pack":"promo","ref":"promo/launch","code":"// Promo pieces: offers, prices, codes and calls to action, in the brand's surface (bold styles get ink\n// outlines and hard shadows).\n/** True when the brand draws ink outlines and hard shadows. */\nfunction boldSurface($) { return $.style.surface === \"bold\"; }\n/** A filled shape path with the brand's outline and hard shadow when it has them. */\nfunction shapeFill($, d, fill) {\n  const k = $.k;\n  if (!boldSurface($)) return $.path(d, { fill: fill, shadow: \"md\" });\n  return '<g transform=\"translate(' + 8 * k + \" \" + 8 * k + ')\">' + $.path(d, { fill: $.c.ink }) + \"</g>\" + $.path(d, { fill: fill, stroke: $.c.ink, sw: 4 * k });\n}\n/** Money: \"$1,299\" or \"1,299 €\" (currency after when it is a word or ends with a space). */\nfunction money(cur, n, dec) {\n  const v = Number(n).toLocaleString(\"en-US\", { minimumFractionDigits: dec, maximumFractionDigits: dec });\n  return /^[A-Za-z]{2,}$/.test(cur) ? v + \" \" + cur : cur + v;\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const A = safeArea($), M = motion($, t);\n  const tin = $.beat(\"in\", 0.1);\n  let s = \"\";\n  s += M.part(\"ground\", { in: \"none\", out: \"fade\", vary: false }, $.panel(t, { box: { x: 0, y: 0, w: W, h: H } }));\n  const fit = fitLines($, $.case(p.name), Object.assign({}, $.font(\"display\"), { ls: -0.045 }), A.w * 0.94, Math.min(A.h * 0.36, 300 * k), 80 * k, 2);\n  const tf = Object.assign({}, $.font(\"text\"), { size: Math.max(30 * k, Math.min(48 * k, fit.size * 0.2)) });\n  const tl = p.tagline ? $.wrap(p.tagline, A.w * 0.8, tf).slice(0, 2) : [];\n  const total = fit.lines.length * fit.size * 0.98 + (tl.length ? 34 * k + tl.length * tf.size * 1.3 : 0) + (p.when ? 90 * k : 0);\n  let y = H / 2 - total / 2;\n  const w = widest($, fit.lines, fit.font);\n  fit.lines.forEach(function (l, i) { s += M.text(\"name\", Object.assign({ at: tin + 0.35 + i * 0.12, in: \"push\", split: \"letters\", stagger: 0.03 }, fit.font, { x: W / 2, y: y + i * fit.size * 0.98, anchor: \"middle\", fill: c.fg }), l); });\n  // The sticker sits on the name's top-right corner.\n  const R = Math.max(56 * k, fit.size * 0.42), bx = Math.min(W - R - 20 * k, W / 2 + w / 2 + R * 0.2), by = Math.max(R + 20 * k, y - R * 0.1);\n  const bt = $.case(p.badge), bf = fitFont($, bt, \"display\", R * 0.6, R * 1.5, { ls: 0.02 });\n  s += M.part(\"badge\", { at: tin, in: \"spin\", box: { x: bx - R, y: by - R, w: 2 * R, h: 2 * R } }, '<g transform=\"rotate(-14 ' + bx + \" \" + by + ')\">' + $.circle(bx, by, R, { fill: c.accent2, stroke: boldSurface($) ? c.ink : undefined, sw: 4 * k, shadow: boldSurface($) ? \"hard\" : \"md\" }) + $.text(bt, Object.assign({}, bf, { x: bx, y: by - bf.size * 0.56, anchor: \"middle\", fill: $.on(c.accent2) })) + \"</g>\");\n  y += fit.lines.length * fit.size * 0.98 + 34 * k;\n  if (tl.length) s += M.text(\"tagline\", Object.assign({ at: tin + 1.0, in: \"rise\", split: \"lines\", distance: 16 }, tf, { x: W / 2, y: y, anchor: \"middle\", fill: c.mute, lh: 1.3, maxW: A.w * 0.8 }), tl.join(\"\\n\"));\n  y += tl.length * tf.size * 1.3 + 30 * k;\n  if (p.when) {\n    const wt = $.case(p.when), sz = $.chip.size(wt, { size: 30 * k, dot: true });\n    s += M.part(\"when\", { at: tin + 1.3, in: \"pop\", box: { x: W / 2 - sz.w / 2, y: y, w: sz.w, h: sz.h } }, $.chip(wt, W / 2, y, { size: 30 * k, dot: true, anchor: \"middle\", fill: c.accent, color: $.on(c.accent) }));\n  }\n  return s;\n}\n","libs":[]},{"id":"ribbon","name":"Tape ribbons","description":"Two tapes cross the frame like caution tape, their words scrolling — FREE SHIPPING • LIMITED DROP — over your footage.","category":"banner","tags":["promo","sale","offer","ribbon","tape","banner","caution","scrolling","free shipping"],"placement":"overlay","duration":{"default":5,"min":2,"max":30},"aspects":["16:9","9:16","1:1","4:5"],"props":{"text":{"type":"text","label":"First tape","default":"Free shipping","maxLength":28},"text2":{"type":"text","label":"Second tape","default":"Limited drop","maxLength":28,"description":"empty for one tape"},"speed":{"type":"number","label":"Speed","default":1,"min":0.2,"max":3,"step":0.1}},"parts":{"tapes":"the tapes"},"beats":{"in":0.1},"style":"candy","pack":"promo","ref":"promo/ribbon","code":"// Promo pieces: offers, prices, codes and calls to action, in the brand's surface (bold styles get ink\n// outlines and hard shadows).\n/** True when the brand draws ink outlines and hard shadows. */\nfunction boldSurface($) { return $.style.surface === \"bold\"; }\n/** A filled shape path with the brand's outline and hard shadow when it has them. */\nfunction shapeFill($, d, fill) {\n  const k = $.k;\n  if (!boldSurface($)) return $.path(d, { fill: fill, shadow: \"md\" });\n  return '<g transform=\"translate(' + 8 * k + \" \" + 8 * k + ')\">' + $.path(d, { fill: $.c.ink }) + \"</g>\" + $.path(d, { fill: fill, stroke: $.c.ink, sw: 4 * k });\n}\n/** Money: \"$1,299\" or \"1,299 €\" (currency after when it is a word or ends with a space). */\nfunction money(cur, n, dec) {\n  const v = Number(n).toLocaleString(\"en-US\", { minimumFractionDigits: dec, maximumFractionDigits: dec });\n  return /^[A-Za-z]{2,}$/.test(cur) ? v + \" \" + cur : cur + v;\n}\n\nfunction frame(t, $) {\n  const p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  const A = safeArea($), M = motion($, t);\n  const tin = $.beat(\"in\", 0.1);\n  let s = \"\";\n  const D = Math.hypot(W, H), th = Math.min(W, H) * 0.1;\n  const tapes = [[p.text, -9, c.accent, 0.42], [p.text2, 7, c.accent2, 0.62]].filter(function (q) { return q[0]; });\n  tapes.forEach(function (q, i) {\n    const str = $.case(q[0]), f = Object.assign({}, $.font(\"display\"), { size: th * 0.5, ls: 0.02 });\n    const sep = \"  •  \", unit = $.measure(str + sep, f), dir = i ? -1 : 1, off = (((t * 160 * k * p.speed * dir) % unit) + unit) % unit;\n    let row = $.rect(-D / 2, -th / 2, D, th, { fill: q[2] }) + $.rect(-D / 2, -th / 2 + 6 * k, D, 3 * k, { fill: $.rgba($.on(q[2]), 0.5) }) + $.rect(-D / 2, th / 2 - 9 * k, D, 3 * k, { fill: $.rgba($.on(q[2]), 0.5) });\n    for (let x = -D / 2 - unit + off; x < D / 2 + unit; x += unit) row += $.text(str + sep, Object.assign({}, f, { within: { x: -1e5, y: -1e5, w: 2e5, h: 2e5 }, x: x, y: -f.size * 0.56, fill: $.on(q[2]) }));\n    const cy = H * q[3];\n    s += M.part(\"tapes\", { at: tin + i * 0.2, in: \"wipe\", from: i ? \"right\" : \"left\", dur: 0.5, box: { x: 0, y: cy - th, w: W, h: th * 2 } }, '<g transform=\"translate(' + W / 2 + \" \" + cy + \") rotate(\" + q[1] + ')\">' + row + \"</g>\");\n  });\n  return s;\n}\n","libs":[]}]},{"format":1,"id":"ui","name":"MONARC UI","version":"1.0.0","description":"Product micro-interactions that play live in a page and render to video: a button that hovers, presses, loads and lands on a check, a toggle, a like with a count, a loader that succeeds or fails, and a notification bell. Each declares its states, so <volito-player> plays them on hover and click with no setup.","author":"MONARC STUDIOS","license":"MONARC Packs License 1.0 (free to use in any video; not for resale as templates)","homepage":"https://volito.dev/docs/packs/ui","style":"studio","shared":"shared.js","items":["button","toggle","like","loader","bell"],"entries":[{"id":"button","name":"Button","description":"A product button with every state: it springs in, breathes while idle, lifts on hover, presses, spins while it loads and lands on a check with the done label. In a page, <volito-player> plays each state on hover and click.","category":"cta","tags":["ui","button","hover","press","loading","success","interactive","micro-interaction","product","web"],"placement":"overlay","duration":{"default":4.6,"min":4.6,"max":4.6},"aspects":["16:9","9:16","1:1","4:5"],"props":{"label":{"type":"text","label":"Label","default":"Get started","maxLength":22},"done":{"type":"text","label":"Done label","default":"You're in","maxLength":22},"icon":{"type":"icon","label":"Icon","default":"arrowRight"}},"beats":{"idle":0.6,"hoverIn":1.6,"hoverOut":1.9,"press":2.2,"loading":2.6,"done":3.6},"states":{"enter":{"from":"start","to":"idle","play":"once","next":"idle"},"idle":{"from":"idle","to":"hoverIn","play":"loop"},"hover":{"from":"hoverIn","to":"hoverOut","play":"once"},"unhover":{"from":"hoverOut","to":"press","play":"once","next":"idle"},"press":{"from":"press","to":"loading","play":"once","next":"loading"},"loading":{"from":"loading","to":"done","play":"loop"},"done":{"from":"done","to":"end","play":"once"}},"on":"pointerenter(idle|unhover): hover; pointerleave(hover): unhover; click(idle|hover|unhover): press","style":"studio","pack":"ui","ref":"ui/button","code":"// UI pieces: product micro-interactions whose timeline is cut into states at named beats (enter, idle,\n// hover, press, loading, done…). Loops are periodic so a looped state repeats without a seam, and every\n// state ends on a pose the next one starts from.\n\n/** 0→1 through a loop of [a, b): the frame at b is the frame at a. */\nfunction loopP(t, a, b) {\n  var L = b - a;\n  if (L <= 0) return 0;\n  var x = (t - a) % L;\n  return (x < 0 ? x + L : x) / L;\n}\n\n/** A size as a share of the frame's short side, so a control reads the same at every aspect. */\nfunction unit($, share) {\n  return Math.min($.W, $.H) * share;\n}\n\n/** Fixed-precision number for markup. */\nfunction n2(v) {\n  return (Math.round(v * 100) / 100).toString();\n}\n\n/** A rounded box in the brand's surface: bold brands get an ink outline and a hard shadow. */\nfunction uiBox($, x, y, w, h, r, fill, shadow) {\n  var k = $.k;\n  if ($.style.surface === \"bold\") return $.rect(x + 6 * k, y + 6 * k, w, h, { r: r, fill: $.c.ink }) + $.rect(x, y, w, h, { r: r, fill: fill, stroke: $.c.ink, sw: 3.5 * k });\n  return $.rect(x, y, w, h, { r: r, fill: fill, shadow: $.style.surface === \"flat\" ? null : shadow || \"md\" });\n}\n\n/** A spinner arc at (cx, cy) turned `turn` (0–1) of the way round. */\nfunction spinnerArc(cx, cy, r, turn, color, sw, op) {\n  return '<g transform=\"rotate(' + n2(turn * 360) + \" \" + n2(cx) + \" \" + n2(cy) + ')\"' + (op != null && op < 1 ? ' opacity=\"' + n2(op) + '\"' : \"\") + '><circle cx=\"' + n2(cx) + '\" cy=\"' + n2(cy) + '\" r=\"' + n2(r) + '\" fill=\"none\" stroke=\"' + color + '\" stroke-width=\"' + n2(sw) + '\" stroke-linecap=\"round\" pathLength=\"100\" stroke-dasharray=\"30 70\"/></g>';\n}\n\n/** A check mark centred at (cx, cy), `s` wide, drawn on as p goes 0→1. */\nfunction checkMark($, cx, cy, s, p, color, sw) {\n  if (p <= 0) return \"\";\n  var d = \"M\" + n2(cx - s * 0.5) + \" \" + n2(cy + s * 0.02) + \" L\" + n2(cx - s * 0.14) + \" \" + n2(cy + s * 0.36) + \" L\" + n2(cx + s * 0.52) + \" \" + n2(cy - s * 0.34);\n  return $.path(d, { stroke: color, sw: sw, draw: Math.min(1, p) });\n}\n\n/** A cross centred at (cx, cy), `s` wide, drawn on as p goes 0→1. */\nfunction crossMark($, cx, cy, s, p, color, sw) {\n  if (p <= 0) return \"\";\n  var h = s * 0.4;\n  var a = $.path(\"M\" + n2(cx - h) + \" \" + n2(cy - h) + \" L\" + n2(cx + h) + \" \" + n2(cy + h), { stroke: color, sw: sw, draw: Math.min(1, p * 2) });\n  var b = p > 0.5 ? $.path(\"M\" + n2(cx + h) + \" \" + n2(cy - h) + \" L\" + n2(cx - h) + \" \" + n2(cy + h), { stroke: color, sw: sw, draw: Math.min(1, (p - 0.5) * 2) }) : \"\";\n  return a + b;\n}\n\n/** A group scaled about (ox, oy) and moved by (dx, dy), at an opacity. */\nfunction posed(markup, ox, oy, s, dx, dy, op) {\n  return '<g transform=\"translate(' + n2(dx || 0) + \" \" + n2(dy || 0) + \") translate(\" + n2(ox) + \" \" + n2(oy) + \") scale(\" + n2(s) + \") translate(\" + n2(-ox) + \" \" + n2(-oy) + ')\"' + (op != null && op < 1 ? ' opacity=\"' + n2(Math.max(0, op)) + '\"' : \"\") + \">\" + markup + \"</g>\";\n}\n\nfunction frame(t, $) {\n  var p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  var bIdle = $.beat(\"idle\", 0.6), bHi = $.beat(\"hoverIn\", 1.6), bHo = $.beat(\"hoverOut\", 1.9), bPress = $.beat(\"press\", 2.2), bLoad = $.beat(\"loading\", 2.6), bDone = $.beat(\"done\", 3.6);\n  var tf = $.font(\"text\");\n  var size = unit($, 0.07);\n  var font = { size: size, weight: Math.max(700, tf.weight), family: tf.family };\n  var label = $.case(p.label), done = $.case(p.done || p.label);\n  var iconS = size * 1.0, gap = size * 0.55, padX = size * 1.25, h = size * 2.4;\n  var lw = Math.max($.measure(label, font), $.measure(done, font) + iconS + gap);\n  var w = padX * 2 + lw + (p.icon ? iconS + gap : 0);\n  var cx = W / 2, cy = H / 2;\n  var r = Math.min(h / 2, Math.max(8 * k, $.style.radius));\n\n  // Each stretch of the timeline is a state: enter, idle (loops), hover, unhover, press, loading (loops), done.\n  var appear = $.E.outBack($.seg(t, 0, bIdle * 0.85));\n  var breathe = t >= bIdle && t < bHi ? Math.sin(loopP(t, bIdle, bHi) * Math.PI * 2) : 0;\n  var hover = t < bHi ? 0 : t < bHo ? $.E.out3($.seg(t, bHi, bHi + 0.22)) : t < bPress ? 1 - $.E.inOut2($.seg(t, bHo, bHo + 0.25)) : 0;\n  var press = $.bump(t, bPress + 0.02, 0.06, 0.2);\n  var collapse = t < bDone ? $.E.inOut3($.seg(t, bPress + 0.14, bLoad)) : 1 - $.E.out3($.seg(t, bDone, bDone + 0.35));\n  var curW = $.lerp(w, h, collapse);\n  var curR = $.lerp(r, h / 2, collapse);\n  var lift = (hover * 6 + breathe * 1.5) * k;\n  var scale = (0.6 + 0.4 * appear) * (1 - 0.06 * press) * (1 + 0.03 * hover);\n  var fill = $.mix(c.accent, c.fg, 0.1 * hover);\n  var fg = $.on(c.accent);\n  var x = cx - curW / 2, y = cy - h / 2;\n\n  var s = \"\";\n  // The press ripple, outside the scaled button.\n  var rp = $.seg(t, bPress, bPress + 0.55);\n  if (rp > 0 && rp < 1) s += $.rect(cx - w / 2 - 26 * rp * k, y - 26 * rp * k, w + 52 * rp * k, h + 52 * rp * k, { r: r + 26 * rp * k, stroke: c.accent, sw: 5 * k * (1 - rp), op: 1 - rp });\n\n  var b = uiBox($, x, y, curW, h, curR, fill, hover > 0.5 ? \"lg\" : \"md\");\n  // The label and icon, until the button collapses into a circle.\n  var labelOp = t < bDone ? 1 - $.seg(collapse, 0, 0.35) : 0;\n  if (labelOp > 0) {\n    var tx = cx - w / 2 + padX;\n    b += $.text(label, Object.assign({}, font, { x: tx, y: cy - size * 0.55, fill: fg, op: labelOp }));\n    if (p.icon) b += $.icon(p.icon, cx + w / 2 - padX - iconS + hover * 5 * k, cy - iconS / 2, iconS, { color: fg, sw: 2.4, op: labelOp });\n  }\n  // Loading: a spinner turning once a second, seamless across the loop.\n  var spinOp = t >= bPress && t < bDone + 0.15 ? $.seg(t, bLoad - 0.15, bLoad) * (1 - $.seg(t, bDone, bDone + 0.12)) : 0;\n  if (spinOp > 0) b += spinnerArc(cx, cy, h * 0.27, t >= bLoad ? loopP(t, bLoad, bDone) * Math.round(bDone - bLoad) : 0, fg, size * 0.22, spinOp);\n  // Done: the check draws on, then the done label slides in as the button opens out again.\n  if (t >= bDone) {\n    var open = $.E.out3($.seg(t, bDone, bDone + 0.35));\n    var ck = $.seg(t, bDone + 0.08, bDone + 0.42);\n    var ckX = $.lerp(cx, cx - w / 2 + padX + iconS / 2, open);\n    b += checkMark($, ckX, cy, iconS * 0.9, ck, fg, size * 0.2);\n    var dOp = $.seg(t, bDone + 0.22, bDone + 0.5);\n    if (dOp > 0) b += $.text(done, Object.assign({}, font, { x: cx - w / 2 + padX + iconS + gap + (1 - dOp) * 10 * k, y: cy - size * 0.55, fill: fg, op: dOp }));\n  }\n  s += posed(b, cx, cy, scale, 0, -lift, Math.min(1, appear * 1.4));\n  return s;\n}\n","libs":[]},{"id":"toggle","name":"Toggle","description":"A settings switch with its label: the knob stretches as it slides, the track fills with the accent, and it slides back. In a page, a click turns it on and off.","category":"cta","tags":["ui","toggle","switch","settings","on off","interactive","micro-interaction","product","web"],"placement":"overlay","duration":{"default":2.4,"min":2.4,"max":2.4},"aspects":["16:9","9:16","1:1","4:5"],"props":{"label":{"type":"text","label":"Label","default":"Notifications","maxLength":24},"hint":{"type":"text","label":"Hint","default":"Get a ping when a render finishes","maxLength":44,"description":"empty for none"}},"beats":{"off":0.4,"turnOn":0.7,"turnOff":1.7},"states":{"enter":{"from":"start","to":"off","play":"once","next":"off"},"off":{"from":"off","play":"hold"},"on":{"from":"turnOn","to":"turnOff","play":"once"},"turning-off":{"from":"turnOff","to":"end","play":"once","next":"off"}},"on":"click(off|turning-off): on; click(on): turning-off","style":"studio","pack":"ui","ref":"ui/toggle","code":"// UI pieces: product micro-interactions whose timeline is cut into states at named beats (enter, idle,\n// hover, press, loading, done…). Loops are periodic so a looped state repeats without a seam, and every\n// state ends on a pose the next one starts from.\n\n/** 0→1 through a loop of [a, b): the frame at b is the frame at a. */\nfunction loopP(t, a, b) {\n  var L = b - a;\n  if (L <= 0) return 0;\n  var x = (t - a) % L;\n  return (x < 0 ? x + L : x) / L;\n}\n\n/** A size as a share of the frame's short side, so a control reads the same at every aspect. */\nfunction unit($, share) {\n  return Math.min($.W, $.H) * share;\n}\n\n/** Fixed-precision number for markup. */\nfunction n2(v) {\n  return (Math.round(v * 100) / 100).toString();\n}\n\n/** A rounded box in the brand's surface: bold brands get an ink outline and a hard shadow. */\nfunction uiBox($, x, y, w, h, r, fill, shadow) {\n  var k = $.k;\n  if ($.style.surface === \"bold\") return $.rect(x + 6 * k, y + 6 * k, w, h, { r: r, fill: $.c.ink }) + $.rect(x, y, w, h, { r: r, fill: fill, stroke: $.c.ink, sw: 3.5 * k });\n  return $.rect(x, y, w, h, { r: r, fill: fill, shadow: $.style.surface === \"flat\" ? null : shadow || \"md\" });\n}\n\n/** A spinner arc at (cx, cy) turned `turn` (0–1) of the way round. */\nfunction spinnerArc(cx, cy, r, turn, color, sw, op) {\n  return '<g transform=\"rotate(' + n2(turn * 360) + \" \" + n2(cx) + \" \" + n2(cy) + ')\"' + (op != null && op < 1 ? ' opacity=\"' + n2(op) + '\"' : \"\") + '><circle cx=\"' + n2(cx) + '\" cy=\"' + n2(cy) + '\" r=\"' + n2(r) + '\" fill=\"none\" stroke=\"' + color + '\" stroke-width=\"' + n2(sw) + '\" stroke-linecap=\"round\" pathLength=\"100\" stroke-dasharray=\"30 70\"/></g>';\n}\n\n/** A check mark centred at (cx, cy), `s` wide, drawn on as p goes 0→1. */\nfunction checkMark($, cx, cy, s, p, color, sw) {\n  if (p <= 0) return \"\";\n  var d = \"M\" + n2(cx - s * 0.5) + \" \" + n2(cy + s * 0.02) + \" L\" + n2(cx - s * 0.14) + \" \" + n2(cy + s * 0.36) + \" L\" + n2(cx + s * 0.52) + \" \" + n2(cy - s * 0.34);\n  return $.path(d, { stroke: color, sw: sw, draw: Math.min(1, p) });\n}\n\n/** A cross centred at (cx, cy), `s` wide, drawn on as p goes 0→1. */\nfunction crossMark($, cx, cy, s, p, color, sw) {\n  if (p <= 0) return \"\";\n  var h = s * 0.4;\n  var a = $.path(\"M\" + n2(cx - h) + \" \" + n2(cy - h) + \" L\" + n2(cx + h) + \" \" + n2(cy + h), { stroke: color, sw: sw, draw: Math.min(1, p * 2) });\n  var b = p > 0.5 ? $.path(\"M\" + n2(cx + h) + \" \" + n2(cy - h) + \" L\" + n2(cx - h) + \" \" + n2(cy + h), { stroke: color, sw: sw, draw: Math.min(1, (p - 0.5) * 2) }) : \"\";\n  return a + b;\n}\n\n/** A group scaled about (ox, oy) and moved by (dx, dy), at an opacity. */\nfunction posed(markup, ox, oy, s, dx, dy, op) {\n  return '<g transform=\"translate(' + n2(dx || 0) + \" \" + n2(dy || 0) + \") translate(\" + n2(ox) + \" \" + n2(oy) + \") scale(\" + n2(s) + \") translate(\" + n2(-ox) + \" \" + n2(-oy) + ')\"' + (op != null && op < 1 ? ' opacity=\"' + n2(Math.max(0, op)) + '\"' : \"\") + \">\" + markup + \"</g>\";\n}\n\nfunction frame(t, $) {\n  var p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  var bOff = $.beat(\"off\", 0.4), bOn = $.beat(\"turnOn\", 0.7), bBack = $.beat(\"turnOff\", 1.7);\n  var tf = $.font(\"text\");\n  var size = unit($, 0.06);\n  var th = size * 1.9, tw = th * 1.8, kr = th * 0.5 - 5 * k;\n  var label = $.case(p.label), hint = p.hint ? p.hint : \"\";\n  var lf = { size: size, weight: Math.max(650, tf.weight), family: tf.family };\n  var hf = { size: size * 0.62, weight: 500, family: tf.family };\n  var textW = Math.max($.measure(label, lf), hint ? $.measure(hint, hf) : 0);\n  var gap = size * 1.4, total = textW + gap + tw;\n  var x0 = W / 2 - total / 2, tx = x0 + textW + gap, ty = H / 2 - th / 2;\n\n  var appear = $.E.outBack($.seg(t, 0, bOff * 0.9));\n  // How far on: slides on at turnOn, back off at turnOff; the knob stretches while it moves.\n  var on = t < bOn ? 0 : t < bBack ? $.E.inOut3($.seg(t, bOn, bOn + 0.38)) : 1 - $.E.inOut3($.seg(t, bBack, bBack + 0.38));\n  var moving = t < bBack ? $.bump(t, bOn + 0.05, 0.12, 0.25) : $.bump(t, bBack + 0.05, 0.12, 0.25);\n  var track = $.mix($.mix(c.surface, c.fg, 0.22), c.accent, on);\n  var padX = size * 1.0, padY = size * 0.8, ch = Math.max(th, hint ? size * 1.9 : size) + padY * 2;\n  var s = uiBox($, x0 - padX, H / 2 - ch / 2, total + padX * 2, ch, Math.min(ch / 2, Math.max(10 * k, $.style.radius)), c.surface, \"md\");\n  s += $.text(label, Object.assign({}, lf, { x: x0, y: H / 2 - (hint ? size * 1.05 : size * 0.55), fill: c.fg }));\n  if (hint) s += $.text(hint, Object.assign({}, hf, { x: x0, y: H / 2 + size * 0.25, fill: $.mix(c.fg, c.surface, 0.4) }));\n  var g = uiBox($, tx, ty, tw, th, th / 2, track, null);\n  var kx = $.lerp(tx + th / 2, tx + tw - th / 2, on);\n  var stretch = 1 + 0.35 * moving;\n  g += '<ellipse cx=\"' + n2(kx) + '\" cy=\"' + n2(H / 2) + '\" rx=\"' + n2(kr * stretch) + '\" ry=\"' + n2(kr * (1 - 0.08 * moving)) + '\" fill=\"' + $.on(c.accent) + '\"/>';\n  if (on > 0.6) g += checkMark($, kx, H / 2, kr * 0.9, (on - 0.6) / 0.4, c.accent, size * 0.16);\n  s += g;\n  return posed(s, W / 2, H / 2, 0.85 + 0.15 * appear, 0, (1 - appear) * 20 * k, Math.min(1, appear * 1.5));\n}\n","libs":[]},{"id":"like","name":"Like","description":"A heart and its count: it breathes while idle, pops full with a burst when liked as the count rolls up by one, and empties again when unliked. In a page, a click likes and unlikes.","category":"social","tags":["ui","like","heart","favourite","count","interactive","micro-interaction","social","web"],"placement":"overlay","duration":{"default":3.2,"min":3.2,"max":3.2},"aspects":["16:9","9:16","1:1","4:5"],"props":{"count":{"type":"number","label":"Likes before","default":128,"min":0,"max":9999999,"step":1}},"beats":{"idle":0.5,"like":1.5,"unlike":2.6},"states":{"enter":{"from":"start","to":"idle","play":"once","next":"idle"},"idle":{"from":"idle","to":"like","play":"loop"},"like":{"from":"like","to":"unlike","play":"once"},"unlike":{"from":"unlike","to":"end","play":"once","next":"idle"}},"on":"click(idle|unlike): like; click(like): unlike","style":"studio","pack":"ui","ref":"ui/like","code":"// UI pieces: product micro-interactions whose timeline is cut into states at named beats (enter, idle,\n// hover, press, loading, done…). Loops are periodic so a looped state repeats without a seam, and every\n// state ends on a pose the next one starts from.\n\n/** 0→1 through a loop of [a, b): the frame at b is the frame at a. */\nfunction loopP(t, a, b) {\n  var L = b - a;\n  if (L <= 0) return 0;\n  var x = (t - a) % L;\n  return (x < 0 ? x + L : x) / L;\n}\n\n/** A size as a share of the frame's short side, so a control reads the same at every aspect. */\nfunction unit($, share) {\n  return Math.min($.W, $.H) * share;\n}\n\n/** Fixed-precision number for markup. */\nfunction n2(v) {\n  return (Math.round(v * 100) / 100).toString();\n}\n\n/** A rounded box in the brand's surface: bold brands get an ink outline and a hard shadow. */\nfunction uiBox($, x, y, w, h, r, fill, shadow) {\n  var k = $.k;\n  if ($.style.surface === \"bold\") return $.rect(x + 6 * k, y + 6 * k, w, h, { r: r, fill: $.c.ink }) + $.rect(x, y, w, h, { r: r, fill: fill, stroke: $.c.ink, sw: 3.5 * k });\n  return $.rect(x, y, w, h, { r: r, fill: fill, shadow: $.style.surface === \"flat\" ? null : shadow || \"md\" });\n}\n\n/** A spinner arc at (cx, cy) turned `turn` (0–1) of the way round. */\nfunction spinnerArc(cx, cy, r, turn, color, sw, op) {\n  return '<g transform=\"rotate(' + n2(turn * 360) + \" \" + n2(cx) + \" \" + n2(cy) + ')\"' + (op != null && op < 1 ? ' opacity=\"' + n2(op) + '\"' : \"\") + '><circle cx=\"' + n2(cx) + '\" cy=\"' + n2(cy) + '\" r=\"' + n2(r) + '\" fill=\"none\" stroke=\"' + color + '\" stroke-width=\"' + n2(sw) + '\" stroke-linecap=\"round\" pathLength=\"100\" stroke-dasharray=\"30 70\"/></g>';\n}\n\n/** A check mark centred at (cx, cy), `s` wide, drawn on as p goes 0→1. */\nfunction checkMark($, cx, cy, s, p, color, sw) {\n  if (p <= 0) return \"\";\n  var d = \"M\" + n2(cx - s * 0.5) + \" \" + n2(cy + s * 0.02) + \" L\" + n2(cx - s * 0.14) + \" \" + n2(cy + s * 0.36) + \" L\" + n2(cx + s * 0.52) + \" \" + n2(cy - s * 0.34);\n  return $.path(d, { stroke: color, sw: sw, draw: Math.min(1, p) });\n}\n\n/** A cross centred at (cx, cy), `s` wide, drawn on as p goes 0→1. */\nfunction crossMark($, cx, cy, s, p, color, sw) {\n  if (p <= 0) return \"\";\n  var h = s * 0.4;\n  var a = $.path(\"M\" + n2(cx - h) + \" \" + n2(cy - h) + \" L\" + n2(cx + h) + \" \" + n2(cy + h), { stroke: color, sw: sw, draw: Math.min(1, p * 2) });\n  var b = p > 0.5 ? $.path(\"M\" + n2(cx + h) + \" \" + n2(cy - h) + \" L\" + n2(cx - h) + \" \" + n2(cy + h), { stroke: color, sw: sw, draw: Math.min(1, (p - 0.5) * 2) }) : \"\";\n  return a + b;\n}\n\n/** A group scaled about (ox, oy) and moved by (dx, dy), at an opacity. */\nfunction posed(markup, ox, oy, s, dx, dy, op) {\n  return '<g transform=\"translate(' + n2(dx || 0) + \" \" + n2(dy || 0) + \") translate(\" + n2(ox) + \" \" + n2(oy) + \") scale(\" + n2(s) + \") translate(\" + n2(-ox) + \" \" + n2(-oy) + ')\"' + (op != null && op < 1 ? ' opacity=\"' + n2(Math.max(0, op)) + '\"' : \"\") + \">\" + markup + \"</g>\";\n}\n\nfunction frame(t, $) {\n  var p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  var bIdle = $.beat(\"idle\", 0.5), bLike = $.beat(\"like\", 1.5), bUn = $.beat(\"unlike\", 2.6);\n  var tf = $.font(\"text\");\n  var hs = unit($, 0.17);\n  var base = Math.max(0, Math.round(Number(p.count) || 0));\n  var cf = { size: hs * 0.5, weight: Math.max(700, tf.weight), family: tf.family };\n  var after = $.count(base + 1), before = $.count(base);\n  var cw = Math.max($.measure(after, cf), $.measure(before, cf));\n  var gap = hs * 0.3, total = hs + gap + cw;\n  var hx = W / 2 - total / 2, hy = H / 2 - hs / 2, cx = hx + hs / 2, cy = H / 2;\n\n  var appear = $.E.outBack($.seg(t, 0, bIdle * 0.9));\n  var breathe = t >= bIdle && t < bLike ? Math.sin(loopP(t, bIdle, bLike) * Math.PI * 2) : 0;\n  var liked = t < bLike ? 0 : t < bUn ? 1 : 1 - $.E.inOut2($.seg(t, bUn, bUn + 0.3));\n  var popS = t >= bLike && t < bUn ? 1 + 0.45 * $.bump(t, bLike, 0.1, 0.35) : 1;\n  var squash = t >= bUn ? $.bump(t, bUn, 0.08, 0.25) : 0;\n  var pad = hs * 0.32, ph = hs + pad * 2;\n  var s = uiBox($, hx - pad * 1.2, cy - ph / 2, total + pad * 2.6, ph, ph / 2, c.surface, \"md\");\n  if (t >= bLike && t < bUn) s += $.burst(t, bLike + 0.05, cx, cy, { n: 12, r: hs * 1.2, colors: [c.accent, c.accent2], size: hs * 0.08, dur: 0.7 });\n  // The heart: an outline that fills with the accent when liked.\n  var scale = (0.7 + 0.3 * appear) * (1 + 0.04 * breathe) * (liked > 0 && t < bUn ? popS : 1) * (1 - 0.15 * squash);\n  var heart = $.icon(\"heart\", hx, hy, hs, { color: $.mix(c.fg, c.accent, liked), sw: 2.2 });\n  if (liked > 0.01) heart += '<g opacity=\"' + n2(liked) + '\">' + $.icon(\"heart\", hx, hy, hs, { color: c.accent, fill: true }) + \"</g>\";\n  s += posed(heart, cx, cy, scale, 0, 0, Math.min(1, appear * 1.5));\n  // The count rolls: the old number slides up and out as the new one slides in.\n  var roll = t < bLike ? 0 : t < bUn ? $.E.out3($.seg(t, bLike + 0.05, bLike + 0.4)) : 1 - $.E.out3($.seg(t, bUn, bUn + 0.35));\n  var tx = hx + hs + gap, ty = cy - cf.size * 0.55, dy = cf.size * 0.9;\n  var clip = $.uid();\n  $.def('<clipPath id=\"' + clip + '\"><rect x=\"' + n2(tx - 4 * k) + '\" y=\"' + n2(cy - cf.size * 0.8) + '\" width=\"' + n2(cw + 8 * k) + '\" height=\"' + n2(cf.size * 1.6) + '\"/></clipPath>');\n  var nums = $.text(before, Object.assign({}, cf, { x: tx, y: ty - dy * roll, fill: c.fg, op: Math.max(0, 1 - roll * 2) })) + $.text(after, Object.assign({}, cf, { x: tx, y: ty + dy * (1 - roll), fill: liked > 0.5 ? c.accent : c.fg, op: Math.max(0, roll * 2 - 1) }));\n  s += '<g clip-path=\"url(#' + clip + ')\"' + (appear < 1 ? ' opacity=\"' + n2(Math.min(1, appear * 1.5)) + '\"' : \"\") + \">\" + nums + \"</g>\";\n  return s;\n}\n","libs":[]},{"id":"loader","name":"Loader","description":"A status card that waits, then ends one of two ways: a spinner turns while the line says what is happening, then it closes into a ring with a check, or turns into a cross with a shake. In a page, your code sets the state when the work finishes.","category":"callout","tags":["ui","loader","spinner","loading","progress","success","error","status","interactive","micro-interaction","web"],"placement":"overlay","duration":{"default":3.6,"min":3.6,"max":3.6},"aspects":["16:9","9:16","1:1","4:5"],"props":{"loading":{"type":"text","label":"While it works","default":"Uploading your video","maxLength":32},"success":{"type":"text","label":"When it works","default":"Uploaded","maxLength":32},"failure":{"type":"text","label":"When it fails","default":"Upload failed — try again","maxLength":32}},"beats":{"loading":0.4,"success":1.4,"failure":2.5},"states":{"enter":{"from":"start","to":"loading","play":"once","next":"loading"},"loading":{"from":"loading","to":"success","play":"loop"},"success":{"from":"success","to":"failure","play":"once"},"failure":{"from":"failure","to":"end","play":"once"}},"on":"click(success|failure): loading","style":"studio","pack":"ui","ref":"ui/loader","code":"// UI pieces: product micro-interactions whose timeline is cut into states at named beats (enter, idle,\n// hover, press, loading, done…). Loops are periodic so a looped state repeats without a seam, and every\n// state ends on a pose the next one starts from.\n\n/** 0→1 through a loop of [a, b): the frame at b is the frame at a. */\nfunction loopP(t, a, b) {\n  var L = b - a;\n  if (L <= 0) return 0;\n  var x = (t - a) % L;\n  return (x < 0 ? x + L : x) / L;\n}\n\n/** A size as a share of the frame's short side, so a control reads the same at every aspect. */\nfunction unit($, share) {\n  return Math.min($.W, $.H) * share;\n}\n\n/** Fixed-precision number for markup. */\nfunction n2(v) {\n  return (Math.round(v * 100) / 100).toString();\n}\n\n/** A rounded box in the brand's surface: bold brands get an ink outline and a hard shadow. */\nfunction uiBox($, x, y, w, h, r, fill, shadow) {\n  var k = $.k;\n  if ($.style.surface === \"bold\") return $.rect(x + 6 * k, y + 6 * k, w, h, { r: r, fill: $.c.ink }) + $.rect(x, y, w, h, { r: r, fill: fill, stroke: $.c.ink, sw: 3.5 * k });\n  return $.rect(x, y, w, h, { r: r, fill: fill, shadow: $.style.surface === \"flat\" ? null : shadow || \"md\" });\n}\n\n/** A spinner arc at (cx, cy) turned `turn` (0–1) of the way round. */\nfunction spinnerArc(cx, cy, r, turn, color, sw, op) {\n  return '<g transform=\"rotate(' + n2(turn * 360) + \" \" + n2(cx) + \" \" + n2(cy) + ')\"' + (op != null && op < 1 ? ' opacity=\"' + n2(op) + '\"' : \"\") + '><circle cx=\"' + n2(cx) + '\" cy=\"' + n2(cy) + '\" r=\"' + n2(r) + '\" fill=\"none\" stroke=\"' + color + '\" stroke-width=\"' + n2(sw) + '\" stroke-linecap=\"round\" pathLength=\"100\" stroke-dasharray=\"30 70\"/></g>';\n}\n\n/** A check mark centred at (cx, cy), `s` wide, drawn on as p goes 0→1. */\nfunction checkMark($, cx, cy, s, p, color, sw) {\n  if (p <= 0) return \"\";\n  var d = \"M\" + n2(cx - s * 0.5) + \" \" + n2(cy + s * 0.02) + \" L\" + n2(cx - s * 0.14) + \" \" + n2(cy + s * 0.36) + \" L\" + n2(cx + s * 0.52) + \" \" + n2(cy - s * 0.34);\n  return $.path(d, { stroke: color, sw: sw, draw: Math.min(1, p) });\n}\n\n/** A cross centred at (cx, cy), `s` wide, drawn on as p goes 0→1. */\nfunction crossMark($, cx, cy, s, p, color, sw) {\n  if (p <= 0) return \"\";\n  var h = s * 0.4;\n  var a = $.path(\"M\" + n2(cx - h) + \" \" + n2(cy - h) + \" L\" + n2(cx + h) + \" \" + n2(cy + h), { stroke: color, sw: sw, draw: Math.min(1, p * 2) });\n  var b = p > 0.5 ? $.path(\"M\" + n2(cx + h) + \" \" + n2(cy - h) + \" L\" + n2(cx - h) + \" \" + n2(cy + h), { stroke: color, sw: sw, draw: Math.min(1, (p - 0.5) * 2) }) : \"\";\n  return a + b;\n}\n\n/** A group scaled about (ox, oy) and moved by (dx, dy), at an opacity. */\nfunction posed(markup, ox, oy, s, dx, dy, op) {\n  return '<g transform=\"translate(' + n2(dx || 0) + \" \" + n2(dy || 0) + \") translate(\" + n2(ox) + \" \" + n2(oy) + \") scale(\" + n2(s) + \") translate(\" + n2(-ox) + \" \" + n2(-oy) + ')\"' + (op != null && op < 1 ? ' opacity=\"' + n2(Math.max(0, op)) + '\"' : \"\") + \">\" + markup + \"</g>\";\n}\n\nfunction frame(t, $) {\n  var p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  var bLoad = $.beat(\"loading\", 0.4), bOk = $.beat(\"success\", 1.4), bFail = $.beat(\"failure\", 2.5);\n  var tf = $.font(\"text\");\n  var size = unit($, 0.055);\n  var font = { size: size, weight: Math.max(600, tf.weight), family: tf.family };\n  var lines = [$.case(p.loading), $.case(p.success), $.case(p.failure)];\n  var iconR = size * 0.85, padX = size * 1.1, padY = size * 0.85, gap = size * 0.75;\n  var tw = Math.max($.measure(lines[0], font), $.measure(lines[1], font), $.measure(lines[2], font));\n  var w = padX * 2 + iconR * 2 + gap + tw, h = padY * 2 + iconR * 2;\n  var x = W / 2 - w / 2, y = H / 2 - h / 2, icx = x + padX + iconR, icy = H / 2, tx = icx + iconR + gap;\n\n  var appear = $.E.outBack($.seg(t, 0, bLoad * 0.9));\n  var phase = t < bOk ? 0 : t < bFail ? 1 : 2;\n  var ok = phase === 1 ? $.E.out3($.seg(t, bOk, bOk + 0.4)) : 0;\n  var bad = phase === 2 ? $.E.out3($.seg(t, bFail, bFail + 0.4)) : 0;\n  var shake = phase === 2 ? $.shake(t, bFail + 0.15, 9 * k, 0.4)[0] : 0;\n  var tone = phase === 2 ? c.red || c.accent2 : c.accent;\n  var s = uiBox($, x, y, w, h, Math.min(h / 2, Math.max(10 * k, $.style.radius)), c.surface, \"md\");\n  // The icon: a spinner while it works (one turn a second, seamless), then a full ring with a check or a cross.\n  var sw = size * 0.2;\n  if (phase === 0) s += '<circle cx=\"' + n2(icx) + '\" cy=\"' + n2(icy) + '\" r=\"' + n2(iconR) + '\" fill=\"none\" stroke=\"' + $.mix(c.surface, c.fg, 0.12) + '\" stroke-width=\"' + n2(sw) + '\"/>' + spinnerArc(icx, icy, iconR, t >= bLoad ? loopP(t, bLoad, bOk) * Math.round(bOk - bLoad) : 0, tone, sw, 1);\n  else {\n    var done = phase === 1 ? ok : bad;\n    s += $.ring(icx, icy, iconR, done, { color: tone, sw: sw, bg: $.mix(c.surface, c.fg, 0.12) });\n    if (phase === 1) s += checkMark($, icx, icy, iconR * 1.0, $.seg(t, bOk + 0.15, bOk + 0.5), tone, sw);\n    else s += crossMark($, icx, icy, iconR * 1.0, $.seg(t, bFail + 0.15, bFail + 0.5), tone, sw);\n  }\n  // The line: the new one rises in as the old one fades.\n  var swap = phase === 0 ? 1 : $.E.out3($.seg(t, phase === 1 ? bOk + 0.1 : bFail + 0.1, (phase === 1 ? bOk : bFail) + 0.45));\n  if (phase > 0 && swap < 1) s += $.text(lines[phase - 1], Object.assign({}, font, { x: tx, y: icy - size * 0.55 - swap * size * 0.6, fill: c.fg, op: 1 - swap }));\n  s += $.text(lines[phase], Object.assign({}, font, { x: tx, y: icy - size * 0.55 + (1 - swap) * size * 0.6, fill: c.fg, op: swap }));\n  return posed(s, W / 2, H / 2, 0.85 + 0.15 * appear, shake, (1 - appear) * 20 * k, Math.min(1, appear * 1.5));\n}\n","libs":[]},{"id":"bell","name":"Notification bell","description":"A bell that rings: it swings on its hook, a badge pops in with the count, and it clears when read. In a page, hovering rings it and a click marks it read.","category":"social","tags":["ui","notification","bell","badge","alert","inbox","interactive","micro-interaction","web"],"placement":"overlay","duration":{"default":3,"min":3,"max":3},"aspects":["16:9","9:16","1:1","4:5"],"props":{"count":{"type":"number","label":"New","default":3,"min":1,"max":99,"step":1}},"beats":{"idle":0.5,"ring":1.5,"clear":2.5},"states":{"enter":{"from":"start","to":"idle","play":"once","next":"idle"},"idle":{"from":"idle","to":"ring","play":"loop"},"ring":{"from":"ring","to":"clear","play":"once"},"clear":{"from":"clear","to":"end","play":"once","next":"idle"}},"on":"pointerenter(idle): ring; click(ring): clear","style":"studio","pack":"ui","ref":"ui/bell","code":"// UI pieces: product micro-interactions whose timeline is cut into states at named beats (enter, idle,\n// hover, press, loading, done…). Loops are periodic so a looped state repeats without a seam, and every\n// state ends on a pose the next one starts from.\n\n/** 0→1 through a loop of [a, b): the frame at b is the frame at a. */\nfunction loopP(t, a, b) {\n  var L = b - a;\n  if (L <= 0) return 0;\n  var x = (t - a) % L;\n  return (x < 0 ? x + L : x) / L;\n}\n\n/** A size as a share of the frame's short side, so a control reads the same at every aspect. */\nfunction unit($, share) {\n  return Math.min($.W, $.H) * share;\n}\n\n/** Fixed-precision number for markup. */\nfunction n2(v) {\n  return (Math.round(v * 100) / 100).toString();\n}\n\n/** A rounded box in the brand's surface: bold brands get an ink outline and a hard shadow. */\nfunction uiBox($, x, y, w, h, r, fill, shadow) {\n  var k = $.k;\n  if ($.style.surface === \"bold\") return $.rect(x + 6 * k, y + 6 * k, w, h, { r: r, fill: $.c.ink }) + $.rect(x, y, w, h, { r: r, fill: fill, stroke: $.c.ink, sw: 3.5 * k });\n  return $.rect(x, y, w, h, { r: r, fill: fill, shadow: $.style.surface === \"flat\" ? null : shadow || \"md\" });\n}\n\n/** A spinner arc at (cx, cy) turned `turn` (0–1) of the way round. */\nfunction spinnerArc(cx, cy, r, turn, color, sw, op) {\n  return '<g transform=\"rotate(' + n2(turn * 360) + \" \" + n2(cx) + \" \" + n2(cy) + ')\"' + (op != null && op < 1 ? ' opacity=\"' + n2(op) + '\"' : \"\") + '><circle cx=\"' + n2(cx) + '\" cy=\"' + n2(cy) + '\" r=\"' + n2(r) + '\" fill=\"none\" stroke=\"' + color + '\" stroke-width=\"' + n2(sw) + '\" stroke-linecap=\"round\" pathLength=\"100\" stroke-dasharray=\"30 70\"/></g>';\n}\n\n/** A check mark centred at (cx, cy), `s` wide, drawn on as p goes 0→1. */\nfunction checkMark($, cx, cy, s, p, color, sw) {\n  if (p <= 0) return \"\";\n  var d = \"M\" + n2(cx - s * 0.5) + \" \" + n2(cy + s * 0.02) + \" L\" + n2(cx - s * 0.14) + \" \" + n2(cy + s * 0.36) + \" L\" + n2(cx + s * 0.52) + \" \" + n2(cy - s * 0.34);\n  return $.path(d, { stroke: color, sw: sw, draw: Math.min(1, p) });\n}\n\n/** A cross centred at (cx, cy), `s` wide, drawn on as p goes 0→1. */\nfunction crossMark($, cx, cy, s, p, color, sw) {\n  if (p <= 0) return \"\";\n  var h = s * 0.4;\n  var a = $.path(\"M\" + n2(cx - h) + \" \" + n2(cy - h) + \" L\" + n2(cx + h) + \" \" + n2(cy + h), { stroke: color, sw: sw, draw: Math.min(1, p * 2) });\n  var b = p > 0.5 ? $.path(\"M\" + n2(cx + h) + \" \" + n2(cy - h) + \" L\" + n2(cx - h) + \" \" + n2(cy + h), { stroke: color, sw: sw, draw: Math.min(1, (p - 0.5) * 2) }) : \"\";\n  return a + b;\n}\n\n/** A group scaled about (ox, oy) and moved by (dx, dy), at an opacity. */\nfunction posed(markup, ox, oy, s, dx, dy, op) {\n  return '<g transform=\"translate(' + n2(dx || 0) + \" \" + n2(dy || 0) + \") translate(\" + n2(ox) + \" \" + n2(oy) + \") scale(\" + n2(s) + \") translate(\" + n2(-ox) + \" \" + n2(-oy) + ')\"' + (op != null && op < 1 ? ' opacity=\"' + n2(Math.max(0, op)) + '\"' : \"\") + \">\" + markup + \"</g>\";\n}\n\nfunction frame(t, $) {\n  var p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  var bIdle = $.beat(\"idle\", 0.5), bRing = $.beat(\"ring\", 1.5), bClear = $.beat(\"clear\", 2.5);\n  var tf = $.font(\"text\");\n  var bs = unit($, 0.18);\n  var cx = W / 2, cy = H / 2, bx = cx - bs / 2, by = cy - bs / 2;\n  var appear = $.E.outBack($.seg(t, 0, bIdle * 0.9));\n  var sway = t >= bIdle && t < bRing ? Math.sin(loopP(t, bIdle, bRing) * Math.PI * 2) * 2.5 : 0;\n  // Ringing: a damped swing about the hook at the top.\n  var rt = t - bRing;\n  var swing = t >= bRing && t < bClear ? 22 * Math.exp(-rt * 3.2) * Math.sin(rt * 26) : 0;\n  var disc = bs * 1.7;\n  var s = uiBox($, cx - disc / 2, cy - disc / 2, disc, disc, disc / 2, c.surface, \"md\");\n  s += '<g transform=\"rotate(' + n2(sway + swing) + \" \" + n2(cx) + \" \" + n2(by + bs * 0.08) + ')\">' + $.icon(\"bell\", bx, by, bs, { color: c.fg, sw: 2.2 }) + \"</g>\";\n  // The badge pops in when it rings and shrinks away when read.\n  var show = t < bRing ? 0 : t < bClear ? $.pop(t, bRing + 0.08, 2.6, 6) : 1 - $.E.in3($.seg(t, bClear, bClear + 0.3));\n  if (show > 0.01) {\n    var n = String(Math.max(1, Math.round(Number(p.count) || 1)));\n    var r = bs * 0.24, bcx = cx + bs * 0.32, bcy = by + bs * 0.18;\n    var badge = $.circle(bcx, bcy, r, { fill: c.accent, stroke: c.surface, sw: 4 * k }) + $.text(n, { x: bcx, y: bcy - r * 0.62, size: r * 1.15, weight: Math.max(700, tf.weight), family: tf.family, fill: $.on(c.accent), anchor: \"middle\" });\n    s += posed(badge, bcx, bcy, show, 0, 0, Math.min(1, show * 2));\n  }\n  return posed(s, cx, cy, 0.7 + 0.3 * appear, 0, 0, Math.min(1, appear * 1.5));\n}\n","libs":[]}]},{"format":1,"id":"effects","name":"MONARC Effects","version":"1.0.0","description":"Looks made from SVG filters, so they render the same in Node and the browser with no GPU: film grain, a light leak, a frosted glass card, a chromatic split title, a liquid warp title and a neon sign, in the brand's colours.","author":"MONARC STUDIOS","license":"MONARC Packs License 1.0 (free to use in any video; not for resale as templates)","homepage":"https://volito.dev/docs/packs/effects","style":"cobalt","shared":"shared.js","items":["grain","light-leak","glass","chromatic","warp","neon"],"entries":[{"id":"grain","name":"Film grain","description":"Moving film grain and a soft vignette over footage: the grain re-rolls on every film frame, so it reads as real stock, not a still texture.","category":"scene","tags":["effect","film grain","grain","noise","vignette","film look","texture","overlay"],"placement":"overlay","duration":{"default":6,"min":1,"max":60},"aspects":["16:9","9:16","1:1","4:5"],"props":{"amount":{"type":"number","label":"Grain","default":0.35,"min":0,"max":1,"step":0.05},"size":{"type":"choice","label":"Grain size","options":["fine","medium","coarse"],"default":"medium"},"vignette":{"type":"number","label":"Vignette","default":0.4,"min":0,"max":1,"step":0.05}},"beats":{},"style":"cobalt","pack":"effects","ref":"effects/grain","code":"// Effects: looks built from SVG filters (turbulence, displacement, blur, colour matrices), which resvg and\n// browsers both draw, so a render and the live player match. Every filter is a pure function of t.\n\n/** Fixed-precision number for markup. */\nfunction f2(v) {\n  return (Math.round(v * 100) / 100).toString();\n}\n\n/** A glow filter (a blur of the shape under the shape itself); returns its id. */\nfunction glowFilter($, std, strength) {\n  var id = $.uid();\n  var copies = \"\";\n  for (var i = 0; i < (strength || 2); i++) copies += '<feMergeNode in=\"b\"/>';\n  $.def('<filter id=\"' + id + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur in=\"SourceGraphic\" stdDeviation=\"' + f2(std) + '\" result=\"b\"/><feMerge>' + copies + '<feMergeNode in=\"SourceGraphic\"/></feMerge></filter>');\n  return id;\n}\n\n/** A title sized to fit the safe area's width, centred: { font, x, y, lines }. */\nfunction fitTitle($, str, share, maxSize) {\n  var A = safeArea($);\n  var f = fitFont($, str, \"display\", Math.min(maxSize, $.W * share), A.w * 0.9, { ls: -0.03 });\n  return { font: f, x: $.W / 2, y: $.H / 2 - f.size * 0.56 };\n}\n\nfunction frame(t, $) {\n  var p = $.data, W = $.W, H = $.H;\n  var amount = $.clamp(Number(p.amount)), vig = $.clamp(Number(p.vignette));\n  var freq = { fine: 0.95, medium: 0.7, coarse: 0.45 }[p.size] || 0.7;\n  var s = \"\";\n  if (amount > 0) {\n    // Grey noise, pushed light and dark around the middle, at a low opacity: specks lighter and darker than the\n    // picture under them. A new seed on every film frame (24 a second) moves it the way film stock does.\n    var seed = Math.floor($.step(t, 24) * 24) % 997;\n    var id = $.uid();\n    $.def('<filter id=\"' + id + '\" x=\"0\" y=\"0\" width=\"100%\" height=\"100%\" color-interpolation-filters=\"sRGB\"><feTurbulence type=\"fractalNoise\" baseFrequency=\"' + f2(freq / Math.max(1, $.k)) + '\" numOctaves=\"2\" seed=\"' + seed + '\" result=\"n\"/><feColorMatrix in=\"n\" type=\"matrix\" values=\"2.4 0 0 0 -0.7  2.4 0 0 0 -0.7  2.4 0 0 0 -0.7  0 0 0 0 ' + f2(amount * 0.42) + '\"/></filter>');\n    s += '<rect x=\"0\" y=\"0\" width=\"' + W + '\" height=\"' + H + '\" filter=\"url(#' + id + ')\"/>';\n  }\n  if (vig > 0) {\n    var g = $.uid();\n    $.def('<radialGradient id=\"' + g + '\" cx=\"50%\" cy=\"50%\" r=\"72%\"><stop offset=\"55%\" stop-color=\"#000\" stop-opacity=\"0\"/><stop offset=\"100%\" stop-color=\"#000\" stop-opacity=\"' + f2(vig * 0.85) + '\"/></radialGradient>');\n    s += '<rect x=\"0\" y=\"0\" width=\"' + W + '\" height=\"' + H + '\" fill=\"url(#' + g + ')\"/>';\n  }\n  return s;\n}\n","libs":[]},{"id":"light-leak","name":"Light leak","description":"Warm light bleeding in from the edge of the frame and drifting across it, in the brand's colours: for openings, transitions and a film feel over footage.","category":"scene","tags":["effect","light leak","film","glow","flare","warm","overlay","transition"],"placement":"overlay","duration":{"default":4,"min":1,"max":30},"aspects":["16:9","9:16","1:1","4:5"],"props":{"from":{"type":"choice","label":"From","options":["left","right","top","corner"],"default":"left"},"strength":{"type":"number","label":"Strength","default":0.7,"min":0.1,"max":1,"step":0.05}},"beats":{},"style":"cobalt","pack":"effects","ref":"effects/light-leak","code":"// Effects: looks built from SVG filters (turbulence, displacement, blur, colour matrices), which resvg and\n// browsers both draw, so a render and the live player match. Every filter is a pure function of t.\n\n/** Fixed-precision number for markup. */\nfunction f2(v) {\n  return (Math.round(v * 100) / 100).toString();\n}\n\n/** A glow filter (a blur of the shape under the shape itself); returns its id. */\nfunction glowFilter($, std, strength) {\n  var id = $.uid();\n  var copies = \"\";\n  for (var i = 0; i < (strength || 2); i++) copies += '<feMergeNode in=\"b\"/>';\n  $.def('<filter id=\"' + id + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur in=\"SourceGraphic\" stdDeviation=\"' + f2(std) + '\" result=\"b\"/><feMerge>' + copies + '<feMergeNode in=\"SourceGraphic\"/></feMerge></filter>');\n  return id;\n}\n\n/** A title sized to fit the safe area's width, centred: { font, x, y, lines }. */\nfunction fitTitle($, str, share, maxSize) {\n  var A = safeArea($);\n  var f = fitFont($, str, \"display\", Math.min(maxSize, $.W * share), A.w * 0.9, { ls: -0.03 });\n  return { font: f, x: $.W / 2, y: $.H / 2 - f.size * 0.56 };\n}\n\nfunction frame(t, $) {\n  var p = $.data, c = $.c, W = $.W, H = $.H, D = $.dur;\n  var strength = $.clamp(Number(p.strength) || 0.7, 0.1, 1);\n  // It swells in, drifts, and fades out by the end.\n  var life = $.E.out3($.seg(t, 0, Math.min(1, D * 0.3))) * (1 - $.E.in2($.seg(t, D - Math.min(1, D * 0.3), D)));\n  var u = D > 0 ? t / D : 0;\n  var origin = { left: [0, 0.45], right: [1, 0.55], top: [0.5, 0], corner: [0, 0] }[p.from] || [0, 0.45];\n  var dir = { left: [1, 0.15], right: [-1, -0.1], top: [0.1, 1], corner: [0.8, 0.6] }[p.from] || [1, 0.15];\n  var blobs = [\n    { c: c.accent, r: 0.75, d: 0.18, a: 0.55 },\n    { c: c.accent2 || c.accent, r: 0.5, d: 0.32, a: 0.45 },\n    { c: $.mix(c.accent2 || c.accent, \"#FFFFFF\", 0.55), r: 0.3, d: 0.45, a: 0.5 },\n  ];\n  var s = \"\";\n  var L = Math.max(W, H);\n  for (var i = 0; i < blobs.length; i++) {\n    var b = blobs[i], id = $.uid();\n    var x = (origin[0] + dir[0] * b.d * u) * W + Math.sin(t * 0.7 + i * 2.1) * L * 0.03;\n    var y = (origin[1] + dir[1] * b.d * u) * H + Math.cos(t * 0.5 + i * 1.3) * L * 0.03;\n    $.def('<radialGradient id=\"' + id + '\"><stop offset=\"0%\" stop-color=\"' + b.c + '\" stop-opacity=\"' + f2(b.a * strength * life) + '\"/><stop offset=\"60%\" stop-color=\"' + b.c + '\" stop-opacity=\"' + f2(b.a * 0.35 * strength * life) + '\"/><stop offset=\"100%\" stop-color=\"' + b.c + '\" stop-opacity=\"0\"/></radialGradient>');\n    s += '<circle cx=\"' + f2(x) + '\" cy=\"' + f2(y) + '\" r=\"' + f2(L * b.r) + '\" fill=\"url(#' + id + ')\"/>';\n  }\n  return s;\n}\n","libs":[]},{"id":"glass","name":"Glass card","description":"A frosted glass card over soft moving colour: the ground blurs through the glass, a highlight runs along its edge, and the title sets on it.","category":"title","tags":["effect","glass","glassmorphism","frosted","blur","title","card","gradient"],"placement":"full","duration":{"default":5,"min":2,"max":20},"aspects":["16:9","9:16","1:1","4:5"],"props":{"kicker":{"type":"text","label":"Kicker","default":"New","maxLength":20,"description":"empty for none"},"title":{"type":"text","label":"Title","default":"Made for agents","maxLength":28},"subtitle":{"type":"text","label":"Subtitle","default":"Motion graphics as code","maxLength":44,"description":"empty for none"}},"beats":{"in":0.2},"style":"cobalt","pack":"effects","ref":"effects/glass","code":"// Effects: looks built from SVG filters (turbulence, displacement, blur, colour matrices), which resvg and\n// browsers both draw, so a render and the live player match. Every filter is a pure function of t.\n\n/** Fixed-precision number for markup. */\nfunction f2(v) {\n  return (Math.round(v * 100) / 100).toString();\n}\n\n/** A glow filter (a blur of the shape under the shape itself); returns its id. */\nfunction glowFilter($, std, strength) {\n  var id = $.uid();\n  var copies = \"\";\n  for (var i = 0; i < (strength || 2); i++) copies += '<feMergeNode in=\"b\"/>';\n  $.def('<filter id=\"' + id + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur in=\"SourceGraphic\" stdDeviation=\"' + f2(std) + '\" result=\"b\"/><feMerge>' + copies + '<feMergeNode in=\"SourceGraphic\"/></feMerge></filter>');\n  return id;\n}\n\n/** A title sized to fit the safe area's width, centred: { font, x, y, lines }. */\nfunction fitTitle($, str, share, maxSize) {\n  var A = safeArea($);\n  var f = fitFont($, str, \"display\", Math.min(maxSize, $.W * share), A.w * 0.9, { ls: -0.03 });\n  return { font: f, x: $.W / 2, y: $.H / 2 - f.size * 0.56 };\n}\n\nfunction frame(t, $) {\n  var p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  var tin = $.beat(\"in\", 0.2);\n  var L = Math.min(W, H);\n  // The ground: the brand's ground with three soft colour fields drifting on slow loops.\n  var fields = [\n    { col: c.accent, x: 0.3, y: 0.35, r: 0.42, sp: 0.21 },\n    { col: c.accent2 || c.accent, x: 0.72, y: 0.62, r: 0.36, sp: 0.17 },\n    { col: $.mix(c.accent, c.accent2 || c.accent, 0.5), x: 0.55, y: 0.2, r: 0.28, sp: 0.27 },\n  ];\n  var blobs = \"\";\n  for (var i = 0; i < fields.length; i++) {\n    var f = fields[i];\n    var x = (f.x + Math.sin(t * f.sp * 2 + i) * 0.06) * W, y = (f.y + Math.cos(t * f.sp * 2 + i * 1.7) * 0.06) * H;\n    blobs += '<circle cx=\"' + f2(x) + '\" cy=\"' + f2(y) + '\" r=\"' + f2(L * f.r) + '\" fill=\"' + f.col + '\"/>';\n  }\n  var soft = $.uid();\n  $.def('<filter id=\"' + soft + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + f2(L * 0.08) + '\"/></filter>');\n  var s = '<rect x=\"0\" y=\"0\" width=\"' + W + '\" height=\"' + H + '\" fill=\"' + c.bg + '\"/><g filter=\"url(#' + soft + ')\">' + blobs + \"</g>\";\n\n  // The glass: the same fields blurred harder, clipped to the card, a frost over them and a lit edge.\n  var A = safeArea($);\n  var title = $.case(p.title), sub = p.subtitle ? $.case(p.subtitle) : \"\", kick = p.kicker ? $.case(p.kicker) : \"\";\n  var tf = fitFont($, title, \"display\", L * 0.11, A.w * 0.72, { ls: -0.03 });\n  var sf = { size: tf.size * 0.32, weight: 500, family: $.font(\"text\").family };\n  var cw = Math.min(A.w, Math.max($.measure(title, tf), sub ? $.measure(sub, sf) : 0) + tf.size * 1.6);\n  var ch = tf.size * (sub ? 2.4 : 1.8) + (kick ? tf.size * 0.7 : 0);\n  var rise = $.E.out3($.seg(t, tin, tin + 0.7));\n  var cx = W / 2 - cw / 2, cy = H / 2 - ch / 2 + (1 - rise) * 40 * k;\n  var r = Math.min(ch * 0.25, Math.max(14 * k, $.style.radius));\n  var clip = $.uid(), frost = $.uid(), edge = $.uid();\n  $.def('<clipPath id=\"' + clip + '\"><rect x=\"' + f2(cx) + '\" y=\"' + f2(cy) + '\" width=\"' + f2(cw) + '\" height=\"' + f2(ch) + '\" rx=\"' + f2(r) + '\"/></clipPath>');\n  $.def('<filter id=\"' + frost + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur stdDeviation=\"' + f2(L * 0.14) + '\"/></filter>');\n  var sweep = $.seg(t, tin + 0.4, tin + 1.6);\n  $.def('<linearGradient id=\"' + edge + '\" x1=\"0\" y1=\"0\" x2=\"1\" y2=\"1\"><stop offset=\"' + f2(Math.max(0, sweep - 0.25)) + '\" stop-color=\"#FFFFFF\" stop-opacity=\"0.15\"/><stop offset=\"' + f2(sweep) + '\" stop-color=\"#FFFFFF\" stop-opacity=\"0.85\"/><stop offset=\"' + f2(Math.min(1, sweep + 0.25)) + '\" stop-color=\"#FFFFFF\" stop-opacity=\"0.15\"/></linearGradient>');\n  var glass = '<g clip-path=\"url(#' + clip + ')\"><rect x=\"0\" y=\"0\" width=\"' + W + '\" height=\"' + H + '\" fill=\"' + c.bg + '\"/><g filter=\"url(#' + frost + ')\">' + blobs + '</g><rect x=\"' + f2(cx) + '\" y=\"' + f2(cy) + '\" width=\"' + f2(cw) + '\" height=\"' + f2(ch) + '\" fill=\"#FFFFFF\" fill-opacity=\"0.16\"/></g>';\n  glass += '<rect x=\"' + f2(cx) + '\" y=\"' + f2(cy) + '\" width=\"' + f2(cw) + '\" height=\"' + f2(ch) + '\" rx=\"' + f2(r) + '\" fill=\"none\" stroke=\"url(#' + edge + ')\" stroke-width=\"' + f2(2.5 * k) + '\"/>';\n  var ty = cy + ch / 2 - (sub ? tf.size * 0.85 : tf.size * 0.56) + (kick ? tf.size * 0.35 : 0);\n  if (kick) glass += $.chip(kick, W / 2, cy + tf.size * 0.35, { size: tf.size * 0.26, anchor: \"middle\", fill: c.accent, color: $.on(c.accent) });\n  glass += $.text(title, Object.assign({}, tf, { x: W / 2, y: ty, anchor: \"middle\", fill: c.fg }));\n  if (sub) glass += $.text(sub, Object.assign({}, sf, { x: W / 2, y: ty + tf.size * 1.15, anchor: \"middle\", fill: c.fg, op: 0.82 }));\n  s += '<g opacity=\"' + f2(Math.min(1, rise * 1.4)) + '\">' + glass + \"</g>\";\n  return s;\n}\n","libs":[]},{"id":"chromatic","name":"Chromatic split","description":"A title whose colour channels arrive apart and snap together, then split again on a hit: a lens-fringe look in the brand's two accents.","category":"text","tags":["effect","chromatic aberration","rgb split","glitch","title","text effect","fringe"],"placement":"overlay","duration":{"default":4,"min":1.5,"max":20},"aspects":["16:9","9:16","1:1","4:5"],"props":{"title":{"type":"text","label":"Title","default":"Signal found","maxLength":24},"hits":{"type":"number","label":"Split again (times)","default":2,"min":0,"max":6,"step":1}},"beats":{"in":0.1},"style":"cobalt","pack":"effects","ref":"effects/chromatic","code":"// Effects: looks built from SVG filters (turbulence, displacement, blur, colour matrices), which resvg and\n// browsers both draw, so a render and the live player match. Every filter is a pure function of t.\n\n/** Fixed-precision number for markup. */\nfunction f2(v) {\n  return (Math.round(v * 100) / 100).toString();\n}\n\n/** A glow filter (a blur of the shape under the shape itself); returns its id. */\nfunction glowFilter($, std, strength) {\n  var id = $.uid();\n  var copies = \"\";\n  for (var i = 0; i < (strength || 2); i++) copies += '<feMergeNode in=\"b\"/>';\n  $.def('<filter id=\"' + id + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur in=\"SourceGraphic\" stdDeviation=\"' + f2(std) + '\" result=\"b\"/><feMerge>' + copies + '<feMergeNode in=\"SourceGraphic\"/></feMerge></filter>');\n  return id;\n}\n\n/** A title sized to fit the safe area's width, centred: { font, x, y, lines }. */\nfunction fitTitle($, str, share, maxSize) {\n  var A = safeArea($);\n  var f = fitFont($, str, \"display\", Math.min(maxSize, $.W * share), A.w * 0.9, { ls: -0.03 });\n  return { font: f, x: $.W / 2, y: $.H / 2 - f.size * 0.56 };\n}\n\nfunction frame(t, $) {\n  var p = $.data, c = $.c, k = $.k, D = $.dur;\n  var tin = $.beat(\"in\", 0.1);\n  var str = $.case(p.title);\n  var T = fitTitle($, str, 0.14, 220 * k);\n  var settle = $.E.out3($.seg(t, tin, tin + 0.6));\n  var split = (1 - settle) * T.font.size * 0.35;\n  // Hits: the channels jolt apart and back, spread evenly through the hold.\n  var n = Math.max(0, Math.round(Number(p.hits) || 0));\n  for (var i = 1; i <= n; i++) split += $.bump(t, tin + 0.8 + ((D - tin - 1.2) * i) / (n + 1), 0.04, 0.22) * T.font.size * 0.12;\n  var op = $.fadeIn(t, tin, 0.15) * $.fadeOut(t, D, 0.3);\n  var base = Object.assign({}, T.font, { y: T.y, anchor: \"middle\" });\n  var s = $.text(str, Object.assign({}, base, { x: T.x - split, fill: c.accent, op: 0.9 * op }));\n  s += $.text(str, Object.assign({}, base, { x: T.x + split, fill: c.accent2 || c.accent, op: 0.9 * op }));\n  s += $.text(str, Object.assign({}, base, { x: T.x, fill: c.fg, op: op }));\n  return s;\n}\n","libs":[]},{"id":"warp","name":"Liquid warp","description":"A title that settles out of a liquid ripple: rolling noise bends the letters, eases off until they read clean, then keeps a slow shimmer.","category":"text","tags":["effect","liquid","warp","distortion","ripple","title","text effect","heat haze"],"placement":"overlay","duration":{"default":4,"min":1.5,"max":20},"aspects":["16:9","9:16","1:1","4:5"],"props":{"title":{"type":"text","label":"Title","default":"Fluid by design","maxLength":24},"shimmer":{"type":"boolean","label":"Keep a shimmer","default":true}},"beats":{"in":0.1},"style":"cobalt","pack":"effects","ref":"effects/warp","code":"// Effects: looks built from SVG filters (turbulence, displacement, blur, colour matrices), which resvg and\n// browsers both draw, so a render and the live player match. Every filter is a pure function of t.\n\n/** Fixed-precision number for markup. */\nfunction f2(v) {\n  return (Math.round(v * 100) / 100).toString();\n}\n\n/** A glow filter (a blur of the shape under the shape itself); returns its id. */\nfunction glowFilter($, std, strength) {\n  var id = $.uid();\n  var copies = \"\";\n  for (var i = 0; i < (strength || 2); i++) copies += '<feMergeNode in=\"b\"/>';\n  $.def('<filter id=\"' + id + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur in=\"SourceGraphic\" stdDeviation=\"' + f2(std) + '\" result=\"b\"/><feMerge>' + copies + '<feMergeNode in=\"SourceGraphic\"/></feMerge></filter>');\n  return id;\n}\n\n/** A title sized to fit the safe area's width, centred: { font, x, y, lines }. */\nfunction fitTitle($, str, share, maxSize) {\n  var A = safeArea($);\n  var f = fitFont($, str, \"display\", Math.min(maxSize, $.W * share), A.w * 0.9, { ls: -0.03 });\n  return { font: f, x: $.W / 2, y: $.H / 2 - f.size * 0.56 };\n}\n\nfunction frame(t, $) {\n  var p = $.data, c = $.c, k = $.k, D = $.dur;\n  var tin = $.beat(\"in\", 0.1);\n  var str = $.case(p.title);\n  var T = fitTitle($, str, 0.13, 200 * k);\n  var settle = $.E.out3($.seg(t, tin, tin + 1.1));\n  var amp = (1 - settle) * T.font.size * 0.5 + (p.shimmer ? T.font.size * 0.025 : 0);\n  var id = $.uid();\n  // The noise drifts with time (its offset moves), so the ripple rolls rather than flickers.\n  $.def('<filter id=\"' + id + '\" x=\"-20%\" y=\"-60%\" width=\"140%\" height=\"220%\"><feTurbulence type=\"turbulence\" baseFrequency=\"' + f2(0.012 / Math.max(0.5, k)) + \" \" + f2(0.045 / Math.max(0.5, k)) + '\" numOctaves=\"2\" seed=\"7\" result=\"n\"/><feOffset in=\"n\" dx=\"' + f2(t * 60 * k) + '\" dy=\"0\" result=\"m\"/><feDisplacementMap in=\"SourceGraphic\" in2=\"m\" scale=\"' + f2(amp) + '\" xChannelSelector=\"R\" yChannelSelector=\"G\"/></filter>');\n  var op = $.fadeIn(t, tin, 0.3) * $.fadeOut(t, D, 0.3);\n  return '<g filter=\"url(#' + id + ')\">' + $.text(str, Object.assign({}, T.font, { x: T.x, y: T.y, anchor: \"middle\", fill: c.fg, op: op })) + \"</g>\";\n}\n","libs":[]},{"id":"neon","name":"Neon sign","description":"A neon sign that flickers on: glass tubes of light in the accent with a glow around them, a buzz of flicker as it strikes, then a steady hum.","category":"title","tags":["effect","neon","glow","sign","flicker","night","title","retro"],"placement":"full","duration":{"default":4,"min":2,"max":20},"aspects":["16:9","9:16","1:1","4:5"],"props":{"title":{"type":"text","label":"Sign","default":"Open late","maxLength":20},"subtitle":{"type":"text","label":"Under it","default":"since 2026","maxLength":30,"description":"empty for none"}},"beats":{"on":0.3},"style":"cobalt","pack":"effects","ref":"effects/neon","code":"// Effects: looks built from SVG filters (turbulence, displacement, blur, colour matrices), which resvg and\n// browsers both draw, so a render and the live player match. Every filter is a pure function of t.\n\n/** Fixed-precision number for markup. */\nfunction f2(v) {\n  return (Math.round(v * 100) / 100).toString();\n}\n\n/** A glow filter (a blur of the shape under the shape itself); returns its id. */\nfunction glowFilter($, std, strength) {\n  var id = $.uid();\n  var copies = \"\";\n  for (var i = 0; i < (strength || 2); i++) copies += '<feMergeNode in=\"b\"/>';\n  $.def('<filter id=\"' + id + '\" x=\"-50%\" y=\"-50%\" width=\"200%\" height=\"200%\"><feGaussianBlur in=\"SourceGraphic\" stdDeviation=\"' + f2(std) + '\" result=\"b\"/><feMerge>' + copies + '<feMergeNode in=\"SourceGraphic\"/></feMerge></filter>');\n  return id;\n}\n\n/** A title sized to fit the safe area's width, centred: { font, x, y, lines }. */\nfunction fitTitle($, str, share, maxSize) {\n  var A = safeArea($);\n  var f = fitFont($, str, \"display\", Math.min(maxSize, $.W * share), A.w * 0.9, { ls: -0.03 });\n  return { font: f, x: $.W / 2, y: $.H / 2 - f.size * 0.56 };\n}\n\nfunction frame(t, $) {\n  var p = $.data, c = $.c, k = $.k, W = $.W, H = $.H;\n  var on = $.beat(\"on\", 0.3);\n  var str = $.case(p.title), sub = p.subtitle ? $.case(p.subtitle) : \"\";\n  var T = fitTitle($, str, 0.14, 210 * k);\n  var tube = c.accent, tube2 = c.accent2 || c.accent;\n  // Strikes on with a few hard flickers, then hums (a slight shimmer of brightness).\n  var flick = [0, 0.06, 0.1, 0.16, 0.22, 0.3, 0.34];\n  var lit = t < on ? 0 : 1;\n  for (var i = 0; i < flick.length; i += 2) if (t >= on + flick[i] && t < on + flick[i + 1]) lit = 0.15;\n  var hum = 0.93 + 0.07 * Math.sin(t * 47) * Math.sin(t * 13);\n  var b = lit * (t > on + 0.4 ? hum : 1);\n  var glow = glowFilter($, T.font.size * 0.12, 2);\n  var dark = $.mix(c.ink || \"#000000\", \"#000000\", 0.35);\n  var s = '<rect x=\"0\" y=\"0\" width=\"' + W + '\" height=\"' + H + '\" fill=\"' + dark + '\"/>';\n  // The unlit tubes stay faintly visible; the lit ones glow.\n  var base = Object.assign({}, T.font, { x: T.x, y: T.y - (sub ? T.font.size * 0.3 : 0), anchor: \"middle\", fill: \"none\", stroke: $.mix(dark, tube, 0.25), sw: T.font.size * 0.045 });\n  s += $.text(str, base);\n  if (b > 0) s += '<g filter=\"url(#' + glow + ')\" opacity=\"' + f2(b) + '\">' + $.text(str, Object.assign({}, base, { stroke: tube })) + $.text(str, Object.assign({}, base, { stroke: $.mix(tube, \"#FFFFFF\", 0.6), sw: T.font.size * 0.015 })) + \"</g>\";\n  if (sub) {\n    var sf = { size: T.font.size * 0.3, weight: 600, family: $.font(\"text\").family };\n    var sb = $.seg(t, on + 0.5, on + 0.6) * hum;\n    var sy = T.y + T.font.size * 1.0;\n    s += $.text(sub, Object.assign({}, sf, { x: T.x, y: sy, anchor: \"middle\", fill: \"none\", stroke: $.mix(dark, tube2, 0.25), sw: sf.size * 0.05 }));\n    if (sb > 0) s += '<g filter=\"url(#' + glow + ')\" opacity=\"' + f2(sb) + '\">' + $.text(sub, Object.assign({}, sf, { x: T.x, y: sy, anchor: \"middle\", fill: \"none\", stroke: tube2, sw: sf.size * 0.06 })) + \"</g>\";\n  }\n  return s;\n}\n","libs":[]}]}],"libs":{"world":"// ———————— Volito world ————————\nvar MM_WORLD = [[\"Afghanistan\",4,66.1,33.9,\"qh@kV_@FOEE@EGK?EIGGIB@DE@@NGF]Oa@@CDbAJHFEPHHAFDDR?GJJDFJ?JFDT?@DN?HJ@NXFLABDJCRB^IQQ@MNAF]IIHCMe@SDOACIYGCOQCAGOD\"],[\"Albania\",8,20,41.1,\"cLoX?BDB@DFHNM@GCSBIGIABEAGFATGD\"],[\"Algeria\",12,2.6,28.2,\"lDcP?[_@Oe@GEIWGAMIAIGYCCGDCF_@FMSIUC_@MaAEIBSGi@BBHCPDNJJAN]VMt@@x@FFKL?HGHICOHGJbAd@x@f@ZFT@@KRGDIpEgC\"],[\"Angola\",24,17.5,-12.3,\"cG~AFBBLB@DOMKGF uFxBUCy@?KXKPSEIBGOKAACI?@FW?CR?VEF?VYCG?AD?`@h@??~@YZb@Fn@ALIrA@JGLATF@KKm@IYQUAM?KN]EKHa@HKAC\"],[\"Antarctica\",10,31.2,-77.3,\"l]vo@g@C]BYHGJCP~@JnAFn@?XGCGi@EQEm@_@ lh@dq@wA@YOSFJRtAAVEJE dm@pk@c@AEIAWIIQAKFSVGPHNl@DX?IGl@DNE?GUG |~@|k@KCkAFUAKHzA?RCHG rkA`m@CGq@FWC\\\\JZARE pnA|l@MEs@L`AG heBbp@KGe@Ba@JEHd@@XEXM noB|s@QKc@F]G[F]Gy@C{@LqDNy@EuABo@DkBKCGnCENGt@CCIMMBIlAQ}AA]DcAKOGFGt@IrBEv@UDU[FaACOF_@AiAO]A?GFGEEYCKDu@Gu@AiAM]@[A[Bu@Cw@BqB?k@IYBm@I_@RSEWFq@Fu@EuAFIGVMVAJGJU{ADSDGF[?mAGSBYAQMOFUBYAOFiAFWOSF[Aa@JYAi@ImAGQCKEEIBGV]?GAGOOCEDQGGu@YGGWKS?cAWOAKBFFZHNCN@ZHHD@F?FID^Db@TBFQL_@H[\\\\EFAPMV@HVLXBTLjBPNFbC@EFs@HKDTD`@AZD@NWDCFWFk@@cBPaBFgAL]N{AQkAKq@?yAFKG[Es@A_ESTM?EtBBDGAOIC}@GaAS_CKcBSc@KEGTCGGMG}@MYQMCW?IFW??GIGU@EFW@q@EU?IFUEkBO[KOBSAYNWCGGSCY?GFOGcAAk@BUJyACaBOOEUQSBGDQBS?[JUEEGg@Ii@Eo@MQ@_@KQ?QCAGQEu@Ge@BOBAH[JW@]JS?a@Kw@Hg@?OT?B@Hb@JAFW?BFRNODW@UCQOWKOOiAGOOMGiAUoAAMCIMONQ@yA?_@Dy@ES@}@_@[Fg@Pe@@i@E]Ke@Cg@NU?e@HW@UA[KQAg@Bc@Ac@Bg@GoACEQKDCFMNQBoBCo@@MDBFKDk@HiAHU?MEq@No@BIFe@JqBB_AJMD@Fb@^X@JFXBd@TJL@PWTe@@GHfBFPHBHPLYFIFi@LgBPIHy@BQFw@CoAH\"],[\"Argentina\",32,-65.1,-35.2,\"zi@z_@OVYL[BFHR?HE^??m@ ~b@zQHb@?RDD@TYPBLMF@HPX\\\\Hf@BTACJBNCHJDTBPGFDARMDIGEJPDNJF\\\\P?NJDLSLSBFPVJJVPFFFETKJhAGFK?ML?DE@UOGEM@IIOEY@KIC@GHCEGFGDUICBYIe@KEDS?QMM?OKS?QDCFa@KU@QEQMSMKDGCE?a@UIES@EOQYDKLGOW?g@d@O@k@VCFR\\\\i@FOAQOCQICIJ?N^Rn@l@\"],[\"Armenia\",51,45,40.2,\"a\\\\gWF@FOF?^OAGBG[AKFBBIFDDQF?L\"],[\"Australia\",36,134.4,-25.6,\"i{AnXK@AVFD@NDELLPAJO@MJO?Ia@FYE ymAbSd@NHP\\\\@PCZBJHD?RJZA^O?KMEAUNk@?IFSFGBOLWIFFQOL?IN[GY@KGMALGMi@UM?e@M_@EY]AQMQGPICFIEIIBAMOQIC?EG@?CSEYTW@BKIOGC@EGKKEI@QC?INEKCWLOBEAKDWEIHLPD?AFLRADw@b@ODCDQDMEGSG[BYES@EG[GEEHAJC@AFEHARELMEON@FCPAHE@EP@HELo@Z@BKLGRGCGHCCCTg@b@CN?VINLdAHRLFLXPh@@RJFT?PFTNZKAIHBPLz@OJKFYHGRAEIBMHLPBKKAKGI@MNPJDFNNG?KTUCCz@Ub@@n@LRA\"],[\"Austria\",40,14.1,47.6,\"sIa]@FJ?CBHNP?HDj@GBERB@@^EAGEAIDAE_@AOB?OEAGIMDSGMDIAIB?H\"],[\"Azerbaijan\",31,47.7,40.3,\"_\\\\eYE@MJG?OKSVI@EBN@FVDB?HD?JIGIDE^N?MPGEEHGCCJGCCYDAAHIEC y[eWJCLOQBGN\"],[\"Bahamas\",44,-77.9,24.5,\"jp@wOW??BTB@G ro@{OOFBLBAAIHI zo@wNE?GP?HD@BKFECK\"],[\"Bangladesh\",50,90.3,23.8,\"}x@wL?LFCANJ]FKP?AFDJHC@@LCH]CKJECGKELKGKOFG?ALq@DHPF?DJIHCKC?K^\"],[\"Belarus\",112,28,53.5,\"sPcb@SDCBIASBAHBDSN@BMBEBFDR?IRP@DB?HhBKb@D?GDIKCDWQ?UGEIOG@Ga@K\"],[\"Belgium\",56,4.6,50.7,\"{Bw^BLB?@JPIH@VQF?BEq@GWL\"],[\"Belize\",84,-88.7,17.2,\"tv@cJACC@EKGBDb@HJD?Ae@\"],[\"Benin\",204,2.3,9.6,\"u@}BNBDMAm@BC@IJMAIEACGIAKKE?OHCRBFABNRBJ?j@\"],[\"Bhutan\",64,90.5,27.4,\"ix@kPGD@L^ALBPG?CMMIEYDGB\"],[\"Bolivia\",68,-64.6,-16.7,\"lj@zEW?EE[OYA@b@UPU@GFUHK?KDCXB?EVg@?BJAFKDCJHVAJDB?EPIt@BHPF`@@CV?FNJMXENPLBRq@GSJG@MHMKUFOCG@EGI?_@CEN_@\"],[\"Bosnia and Herz.\",70,17.8,44.2,\"sJsYPGVSNOEGEBCCI?o@DDHIF@FNF@J\"],[\"Botswana\",72,23.8,-22.1,\"kQxLZLPNJRJ@BNLBN?PGHBBHRLL@BEAKJSDA?{@S??eAm@GGFKGUCQ^UTG@ERO@KH\"],[\"Brazil\",76,-53.1,-10.8,\"j`@bTBKGIJMb@UF?ROJ@o@m@_@S?OHKHBEa@FCLBF_@LEFBRC?[DIEC@KIWBKJE@GCKf@?DWC?BYJEJ?TIFGTATQAc@X@ZNDDj@??]NJP?FKJ?CIJKFQEC?GKE@IEOi@SQ?I_ABKFE?MMAAGJA?Ke@?GEGPCCKJOAAEWGAGME?CPA@YFEC?]DECc@IGG@EI?CB@FEBEFDFBNGRKHI?WKY@?MQ?SFEEGDI?EGQa@G?Ol@IBALNNEDc@@?ROKy@RIHBJWEe@H]?]NYTMDQ?GFIb@F`@d@f@Zf@D@DLAd@Fj@DFBXRX@RPHBJT?\\\\FNFTDTNPR@LAJF\\\\LHTb@^VFRLJ\"],[\"Brunei\",96,114.9,4.7,\"egAkBBTF?BDHIYQ\"],[\"Bulgaria\",100,25.2,42.8,\"eMsZCFu@@_@I[HJFDLEJPARD?HP@LGNDL?@MHECKGEHG@GEC\"],[\"Burkina Faso\",854,-1.8,12.3,\"jBoE@KEG?EOOAMEEIBGCUUQGKACBM??PKL?FWD?LDDH@BFD@jA?AZLEH?DDTO\"],[\"Burundi\",108,29.9,-3.4,\"aRn@ERRTH??WDIK@EKK@\"],[\"Cambodia\",116,104.9,12.7,\"c_AsFDWMOYCQ@OFIMQFCL@V`@LGJR@PFNAP_@\"],[\"Cameroon\",120,12.6,5.7,\"aHaGGL?XKPX?BHIHIBGTP\\\\?\\\\QZID?X^I|AACOFKHCFKESGQEAKIGAKHKGIYKGOi@OOCIFG?EEA\"],[\"Canada\",124,-101.6,57.7,\"vkAs]v@[b@GJQCKXIBOVM?III?K`@M`@c@\\\\QJIf@P\\\\URER??{DyANi@M]@}@MMFMCEIM@_@P[MANWCGEW@mANo@@YHZHc@BgAESJUIRGKGi@Ac@NUAa@H}@CBKQC_@F?PKOO?ISl@SAUWMY@SH[TPHe@B?RYOWLDLSLSMMQAUy@BYHAHNJMH@Hd@LZ@TE\\\\b@VLZ@NF@JTBXNRTFN@V]@Q`@[Ce@Fa@Pm@Lu@B@NERMT]ROEIUH]LK_@GUMKM@MLQVMWUHQD]MEu@FQCi@PEFc@??PEVQBMJ]KSUKG_A`AFLk@T_@DMDGNMBGDAVZL`@DVP`@@j@Cp@@NLXFr@l@OC_@Yi@O]AQHPJKd@YHa@CSU?LMFVJ~@RVNLA@Qc@Ot@BLK?[FELBDCNLLVLB@DbA?^RDFd@?H@EJXHTBTHLES_@Ha@RIACFABGH@DIdA[PD\\\\ENBt@KDABKF??FfP? ns@af@OGY??BVHL?BE zp@ol@TIAGGAm@@c@J?DdA? dq@ye@GEKBFJFABG ny@{m@JFr@EII[CYJ ry@mo@n@ABCg@?MB@@ n{@gp@WFBD\\\\DNEHG?Gc@@ `v@om@tAGHURGh@ATGGGg@@UDg@?QDDFc@Hw@@_@CiA?SFEFLBZDXC`BB pdA{o@[BDDd@DZEOG[A jdAgp@YBv@B?CSEI@ va@a_@V\\\\MEKBDDQDGESFDLMCIVHNTACOBCVPJ?MIRCz@?@EKGFCOKS_@MKOEI?BD ls@ug@m@J?HOAMFPD\\\\EJGn@RFIX@OIIa@M?CHICKD fp@gl@SGgARAHe@ESLo@FQFQPb@Hm@J_@D]P]?DL`@VVG^SX@@Jw@XKRDLjASq@ZAFt@Gh@KVIEEx@S?Dx@BPGMMmACDEGIWSJO\\\\Ih@EMCTMP?NGHDb@BrCMNGSGZ?DSOQSGs@ENJOLQOs@Ia@TBJg@E `z@im@oAB\\\\PVBTNTAJO?IIISC xkAqn@kA[y@A@LNFvAJZC |qAw`@SAFTQNF?\\\\W@OIB |`Aqp@}AHUP~@Gl@ASEVC@G dlAi]H@`@GDGNEBETCDI?Es@HQNSFGJ |jAom@mABi@LhARXL?Fr@HHGl@Kg@c@RKcAA jbAkn@e@CCHJHbA@p@H\\\\?BEi@IxABZC[SQEw@Fc@Ja@?ZQQGS@OP paAul@UFQ`@eAR@F^@KFDDfAGz@FtADJIXEP@VOaBGd@CfA@HGm@G^?`@E]Uu@KSBHFm@EYHWIQFOPIGLSQAS@ v}@ml@TMUGWBc@CEDRH_@FBP`@FPALEp@O?Ei@@ raA_m@Y?OBPJ\\\\MEA p|@}n@OF?JHL^@TC?I^?@MU?]E[?AA ~z@kq@MESAFCm@AYJaAFOJWFZDb@LlA?TIAEOEd@?TEJIMG fx@er@{AG]GW@UDOK}@CgA?y@AkCBmAF?BrBPk@?nAP`@Nt@Fz@@[@LBOHp@JHFZDCBa@??Bt@Jr@Ex@BbAC@Ic@CHMMAs@HXM`@COGc@CEEZGFKeAB_@GrB?b@Gf@MBE ~m@ci@LDT?BIGIQCOD@J d{@mj@MFLDXEN@ZG_@Me@J hg@e^_@BUF?Bb@EPI ~f@k\\\\EHa@@HFF?VGDEGE\"],[\"Central African Rep.\",140,20.4,6.5,\"cPgB`@APFFCTHHAFL\\\\EZMRN?LZCJHHP@MHEP[?O?MOWOACEGDYGQM@GY?QIMUIGMAAFKJ@T_@V?DUREJMDCF\"],[\"Chad\",148,18.6,15.3,\"{MgKAnAPALZCBDDAFFNG?CPGD?DL@HFLTPHX?AFPLXFFEBDN@AEFUHCHICIY?JQ?YFMAGJA?KHGIYWQAYKq@NQB_@SI}CjA\"],[\"Chile\",152,-71.7,-37.3,\"zi@z_@?l@_@?DFNDx@Il@UZWgAXIIEMSGM@ nj@~IILALKFFRSp@MCADDRTH?`@BDEFLJLRDPAPJTG`@EB?PJR?NLL?PERJDHd@CXHBETGFDFIBAFHBAJDXHNAHDLNFATEDM??LGJaADP?XJBPF@VGl@WFMEKHK@a@GSUO\\\\EQOEa@WFIi@LEDXJCKaAIKFi@GAc@}AD]EO@WKYO{BD}@ICEI\"],[\"China\",156,103.9,36.6,\"mcAkJNE@QIGa@ECFFFDJNH eq@oY@IMEPa@q@KMc@g@DIIASO?OMGACLQH[FMNFVEFs@DWJKBIPIJ_ABYCSB]JW?IDUI_@G]?WG[OHOKK_@DSI]GMMMEm@?AEPMNGNFRCJBBGWc@WF[K?IQSIG?IHEOIWA[Am@L[h@GPa@FWLGP]?QG_@EPXDTLRVCPDEPBVH??HJKFJ\\\\FAHP?FELLd@VZDNFRDIIBGOKJIf@RJJR@HHKJOB?HOBUMQFM?AJZBHJPHHLSHGRY`@?NJBCJKDF^J@n@fAv@`@T@JHFEJFZHRBFTH?DMEGx@GFGAKPCHGNJb@@RFATH?BKLBVIEOJCBSTBAWSO?_@HCDK`@AGGHMLHNEh@ZN@FCXEHDLL@OJBl@E^KDIJCRKNEFBn@YBUM@?IFIAOTW^GDOPM@SJCF@BQEEBCSIMCSBGMYAEG_@ICE\"],[\"Colombia\",170,-73.1,3.9,\"xh@YBBFQFDd@??JK@@FL@?LGDCJH~@HIDAMSNGJ?FAHBNAJSZWD@PKD@NADGVK@EGA@IEEIAOUFECKBQCCBQFIAIG@CEDKACK@MMGACWKIK?ACO@_@WG?EDBDLBPTFXI@GL?RGFe@?MPa@CGBFP@LIVHHKJEP\"],[\"Congo\",178,15.1,-0.8,\"qJeA@JHVDh@@DTRFR?NZ\\\\FC@EH?DFLILJNSOKFKSGAIIHQ@GW@MHKIWDCL@DIAIW@_@HAKIQKI[B\"],[\"Costa Rica\",188,-84.2,10,\"pr@_EF@?FCBDLHCBEAGRIFIADBBJE@CAMBAEEMBI?IDCCEHQN\"],[\"Côte d'Ivoire\",384,-5.6,7.6,\"~CkEW@AEIACFKEUNEEI?MDCZFNBTGP@F`@CVBd@JAYROCG@IAEA??KIEHWAECA\"],[\"Croatia\",191,16.6,45,\"kIa\\\\SHQBEAKLFDFC^AH?BBDCDFg@b@QFB@n@SNMCAHG?GJADFDE?GM?CAM@?CGAAGOE\"],[\"Cuba\",192,-79,21.6,\"lr@oMa@@QDGFSAc@XQB@DO?MF@BLBv@?MIFEJ?FEBKJ?VGXCFCGCRALHF?BDP?WQ]G\"],[\"Cyprus\",196,33,34.9,\"mS}TY@RFHABGG?\"],[\"Czechia\",203,15.3,49.8,\"kH}^IDM@?DIBCEM@?FO?IHLBDFN@@B`@GRFb@QDOi@OM?\"],[\"Dem. Rep. Congo\",180,23.6,-2.8,\"iQxAEf@KJIVFA^DDLEFDj@QJECATNALQNCBIJDNAFIT??ENAXB?WDG?WBSV?AGH?@BJ@FNHCRDJQJYx@?TB@ECACMGCE@EGI?ADGB[]?OGSUSAEEi@IWAYSO[L]DGMI@UIMBCEGAa@@KNG@YEEFOL?VG@LJJRDd@DDDVEH?V\"],[\"Denmark\",208,9.3,56.2,\"eEka@JBNCFI?SGKQAWI@HBDADI@BFDANLEH wFab@EHJNTK@G]E\"],[\"Djibouti\",262,42.5,11.8,\"oYyFG?ECCD?FJDGBDJBCN?@IMQ\"],[\"Dominican Rep.\",214,-70.5,18.9,\"xk@gJ?EBECCAU_@DCDK?@DI?IFFFHCN?HBBCD@FLDC?C\"],[\"Ecuador\",218,-78.4,-1.5,\"bn@BCLDLTRVFLP@JJHFINA@EEE@GIMBIFHJICC@QECKY@GWKWJEFO@EAQJ\"],[\"Egypt\",818,29.8,26.5,\"aVwLlF??oCDOEMBGGIY?o@LWKQAMBEHCG_@DICK`@L`@DBNOL]@@c@hA_@j@B@?L[T\"],[\"El Salvador\",222,-88.9,13.7,\"zv@_HQJGCGBBLJA^IMM\"],[\"Eq. Guinea\",226,10.4,1.6,\"_Em@a@??Vb@@BCEU\"],[\"Eritrea\",232,38.7,15.4,\"wU_H@GKk@EEMAIKQh@e@Ze@b@DBF?\\\\_@PGL?BCJBJIDNVC\"],[\"Estonia\",233,25.8,58.6,\"oPed@ABLJEPFDN?XIPBAKD@LE@Ks@Gi@@\"],[\"eSwatini\",748,31.4,-26.5,\"aStOBHJ@JK?EEMO@EP\"],[\"Ethiopia\",231,39.6,8.7,\"{\\\\_Dv@z@X?PLL@BDL?FGPHDF\\\\CVOL?FG?IHCJUHCJQJAEKI?Ik@GEKWKIIc@WBEOKHKCCBM?QFMJORLPAHO?CBBDUZ_AVQ?\"],[\"Falkland Is.\",238,-59.4,-51.7,\"fe@j_@WIOBMGOFDFZDFGPHHI\"],[\"Fiji\",242,178,-17.8,\"inB|IECED@JL@HA@IGEG@ noB`ICA@HZH@G[I\"],[\"Finland\",246,26.2,64.5,\"{Pej@BL_@LPNUTLPQLFJ]LFHz@d@b@@bAJJKRECSFQGKQK{@Y@GZIDG@]x@WKEWJYASDSIIM]GYFFL\"],[\"Fr. S. Antarctic Lands\",260,69.5,-49.3,\"aj@j]MDQBABDF^@CW\"],[\"France\",250,2.3,46.6,\"h_@sAP`@DFH?FEDDFCIYFK@MIQUFUNAF {B}]ID[BHL@LDBHAABNJ?FIAEF?DEFFDELKBBFPJd@E\\\\D@JVBTIFBb@GFGIKCg@b@_@\\\\G@MYCa@DDWQHm@OEOQCCDG?WPIAUJEA mDsYMGCNFNFCDMCE\"],[\"Gabon\",266,11.7,-0.6,\"aFm@a@?ERMAEBHVIJALFVPAHI@HRFGJNJ`@e@JSMi@c@A?W\"],[\"Gambia\",270,-15.4,13.5,\"lIoGU?IEGDMCCDHBNCNFPAAG\"],[\"Georgia\",268,43.5,42.2,\"_XcZACm@F]JKASBEFKBDBIHJ@NE^DRIR@AIBK\\\\O\"],[\"Germany\",276,10.3,51.1,\"yGs`@EJDBEFEH@FGJL?h@NENUJFHD@?NNC^@@DHEZCBBN?AMIMZCHE?GBCCMBUK?CEEQBGCCO?CBMIDSOBKC?FSB?F_@I_@L\"],[\"Ghana\",288,-1.2,7.9,\"?{E?DGHA\\\\CFBPAHIR|@VPEAGFQCUGODw@y@?\"],[\"Greece\",300,22.6,39.3,\"mOaU@D\\\\@?CVCCGID_@?@BKA kMyXM?OEMFQA?IIBDLB@VAVBMJT@HIBBEJIHFBSN?HPCEHJ@GPL?NGJ]PYGIAEEC?Cg@I\"],[\"Greenland\",304,-41.5,74.8,\"f\\\\sr@cAKeA?YEeF?}BNd@FvD@IBeAA}@De@EOFTHsCM{@BKFzAR~@@m@@f@X?VWJ\\\\@`@De@HCPT?YPj@@WFFDv@BYL?Ff@GHD[BYJEN`@Bf@QEJVJqA@jB^pAFRFZRj@LbAHPL?LHJ^NGNRb@Z@\\\\Qf@?PILS`@WHMBQZQGMLGSW]EGGCOr@JVE@MGI{@Br@QR@NEUQr@i@XG?Gt@KnC?x@Q{AGrACl@GAGuCSGGt@EQGcAM]AFIkBG}@?UDs@IuBLn@IAG\"],[\"Guatemala\",320,-90.4,15.7,\"rx@aHAK@CIOW?AETQI??Ie@?@d@SBRJAFDDLLTCRK\"],[\"Guinea\",324,-11.1,10.4,\"pG{FWDSA?DIAOFUIMPBJE??DEDB@@DIVHD?JF?BFD?BCAGHKLB@SHMN?HBNPZY@IFEGISE?O\"],[\"Guinea-Bissau\",624,-15.1,12,\"lIwFWCc@??NRDFHJIF?JQ\"],[\"Guyana\",328,-59,4.8,\"hb@e@N?VJH?JIFSCOEGDGDCAGBCH?LOEC@IQEDG?GOKMFKL?HG?SNBPJD@LGNE?CHKP\"],[\"Haiti\",332,-72.7,18.9,\"xk@iK?PBBCD?Db@CFBJEAGa@DGEHG?GJACE]B\"],[\"Honduras\",340,-86.6,14.8,\"|r@kHXFHCPRDCBBH?AHJDBEDAAGBAJ@PKEE@GYOS@QCS?KBYN\"],[\"Hungary\",348,19.4,47.2,\"yLg]IBADJBTZZ@TFPCZQEKBCK?AGQDm@IKEUDCA\"],[\"Iceland\",352,-18.8,65.1,\"`Hqh@BLULXLjAPpAISGj@Ic@C?Eh@CKM_@A]L]KYD_@Ia@?\"],[\"India\",356,79.6,22.9,\"y{@uPAFDB?JLCXLAJJN?HHNNC?TBDAFHDJ_@B?BJHIEKG?IQp@E@MF?NGFJMJJDBFKDBJI\\\\@Fd@B?NHHZLVV`@X?Fd@LDNEh@FP?`@J@FNEDPDFJFDPQPo@N[He@P[L_ABk@ZJLAXWGGDETQKMk@?BOHI@OLGUUW@s@}@?MQKNIN]IG_@BUCQOUV@NGH?HLACTo@XJHFP_AZ[BKHw@D?]KCARQFMC_@@AMFEOAi@[ODMIILFFU@\"],[\"Indonesia\",360,114,-0.3,\"cwAr@?`CPORCBDX?GOMEDUHOd@QN?\\\\SDHF@BONIUEM?@E\\\\?FKPCFI[CIE_@FGf@UJOUUKQ?_@LUB cmApDAHLLND@CIQSG {rAhC@OGMCD?HHL uhAqAJPMP@HUPV@DL?PPL@RF\\\\@ETFFKLAHEVFFI\\\\A@YHEHO@QAQKMALMHMCK@KIIAQDOCI[GGGUg@B {oAv@UDENNIh@?CKY? inAjALCBGSACFDD }nAk@AJK@AF@NHA@LGHD@FKDYCOEG ykAQW?SMABNRLBRCp@BBLQPKGc@G?HHCFJPFSXBDQV?JJDFEIORFBEAGLKASLDAr@JBFEEQBSF?DMGMKm@CGQMODW@ ejAlEXMQCQJ@BF@ yjAhD_@G@J\\\\BZA?GOCKD }hAfDKACFn@DGIIAEEGD ybA~BAFe@@EGc@FGL]BWJTFTIf@?h@KT@b@GBIP?MSW@WF g`ATIVM@ILBV@^T?NQXOV[Xi@NQL_@PMHOb@a@@Ik@D}@|@U?OLKNMHFNSF\"],[\"Iran\",364,54.3,32.5,\"k]uQJK?ID?AMHOVILSEMIG@KLEVg@CEDUMEMNKBg@QEDFHKHE?ELQBMH]B]EACQCMKc@?UHO@WPM?APLd@IBHHG\\\\O@ALPPQTQFAPGBAFZJFTpAKFYHAb@JVEROREZk@H@LEDF\"],[\"Iraq\",368,43.8,33,\"oWcSFWk@SGW@O]SU@GDICKVMDAJHFDLMRWHIN@LE??HKJXCLRd@Av@g@\\\\MVE\"],[\"Ireland\",372,-8,53.2,\"zBu`@CLNPb@JZAOUHSi@WCHBH[B\"],[\"Israel\",376,35,31.5,\"iUmSBDDABJC@BB@BIA?FH^Ja@EEI_@MC@J\"],[\"Italy\",380,12.2,43.5,\"oEi\\\\M@AASCCD[D@HCFNAND?NEHQHINUNO?EDDBq@TABBFJINAFJMF@HH@HNF@CQCCLSb@WLA`@ULIDSXI\\\\LCGJCDMGEDG?EIBI?IGCBI?CGM@IC?G gHyVMADNADBHt@SCIk@? mDqXIEKL@XH?FDFE?WBKI@\"],[\"Jamaica\",388,-77.3,18.1,\"no@qJM@IBCDRBJCHEAEK?\"],[\"Japan\",392,136.9,36,\"uwAoWPR?TFNCHHLXFb@@ZTLE?Ob@BTHV?SNL^JHHGCQJEFMSEIKSKMKg@EUBSc@MHg@[MWBWGKUCIZ?N kyAoZMGCTZDPR^MHTT?BSIMUAKk@UT]H wqA}SIKKBGIMDCDJJFEHBDJJE?I\"],[\"Jordan\",400,36.8,31.2,\"eUgSCEUFg@UGVj@LSRHHN@JLTC@AI_@AY\"],[\"Kazakhstan\",398,67.3,48.2,\"su@w]NLN?@RHHf@ELb@p@JQ`@LDAHVIdA?ZGJB@J`@EJ@BFd@PHLD?DGXABOH?ASVMx@BPQr@Wr@JAhAH?LMLEVBFDCKBGVEFQJE?ES@?MQCO@CQBMR@NCh@JHCAIJKN?NMIOBCOUSJAMi@U]?cATUG_@AWJEE[?EI^MQGBESELMGEkAGICq@GQGc@BETSEYF@HS?q@SFDYNk@p@KIYJ]EIBIJMBGFYAKJ\"],[\"Kenya\",404,37.8,0.6,\"oW|AZS@KjAi@?UUc@Ha@HMYWIB?HGFM?WN]BEGQIGFM?PT?hAKNLFDFD@L`@FF\"],[\"Kosovo\",0,20.9,42.6,\"{KeY@EFGIKCASJBHRD\"],[\"Kuwait\",414,47.6,29.3,\"_]wQGZL@BIPAMSM@\"],[\"Kyrgyzstan\",417,74.6,41.5,\"kk@mYCGKAa@DAKKC[FeA?WHBD^HDFX@FLRC`@LCBDDd@@XER@AKSBGEO@YOVILDNGQMDA\"],[\"Laos\",418,103.7,18.4,\"cbA{GPGHLNGEGAQNO@SLONABDJ@DCRJ@QEUL?@KFEUSABI?@UICS`@W?GPJDDDWJ]h@MLELBR\"],[\"Latvia\",428,24.8,56.8,\"aP}b@IDGR`@JRIHA@Ct@@TD?OIKQGONO?COQCYHO?\"],[\"Lebanon\",422,35.9,33.9,\"kUySLBGOIMG?CFHFDH\"],[\"Lesotho\",426,28.2,-29.6,\"cQbQEDHNH@BDD@NMUSGEIF\"],[\"Liberia\",430,-9.4,6.4,\"fDyC?NBFSN@XXGn@g@EKQSGAIJ@FCBE?CGE?\"],[\"Libya\",434,18,27,\"sNwL?f@V??F|CkAb@PHI^IFKNIHBFI?IJMGG?_@CQFYIC@Q]SAMWDW@YFIPm@JUHSMDOEGOIKA[BEFa@DEDFHCFDLEN?nC\"],[\"Lithuania\",440,23.9,55.3,\"qOwa@AFNFDHTFP?DEHAAK\\\\EBOUEu@AABI@SH\"],[\"Luxembourg\",442,6,49.8,\"wBi^CB?FH?AKC?\"],[\"Macedonia\",807,21.7,41.6,\"_MmYIDALf@HFE@GEK_@C\"],[\"Madagascar\",450,46.7,-19.4,\"}]xFKTAVEF@HBDDKBDCN@FDB@Pl@hC`@LZKLk@?MAOIAKc@HU?QGWe@G[WEI@GG@IMAKEIEH\"],[\"Malawi\",454,34.2,-13.2,\"oSvDQBKNEXDNEXE?GDGLAXHBDLJKA_@FCD@TOEQEGBOGUDOFI\"],[\"Malaysia\",458,114.7,3.5,\"q}@aCCAOFAHKAEGQLGJAh@GDEVL@h@_@@IJK@QFIAMBI uhAqAf@CFTFFHZNBPEH@JHJALBLI@MMDOCCO_@GW[IHCEG?CUMMIOG?IHAF]J@FL?CHLF\"],[\"Mali\",466,-3.5,17.3,\"dFwF@MDEDWGCAKSDSCCCoA?CMBARmD_@?eCvAEHSFAJUA?l@JL@Jj@DFDX@BCJ@PFTTFBHCDD@LNN?DDFAJJDBGH@@DVADE?ED?CKLQTHNGH@?E\"],[\"Mauritania\",478,-10.3,20.2,\"tIcLEEmA?B]CISA@q@aA??]kAn@^?SlDC@BLnA?BBRBRE@JFBV[VKRBHAFF@KEICQD]AINQ\"],[\"Mexico\",484,-102.6,23.9,\"dhAiSo@C@BkAVw@??Ia@?]VIRKDODKOO?MF]d@IT]JGAJ^Bd@Mb@Yb@UBGH{@OKGIa@c@I[?EB?HJHBLC@FVDCF?DJBA@Bd@??HH?UP@DV?HNAB@J`@[NEd@HrA]TM^GFGTKHKBIEA@ECE?EL[n@o@PG@CAKTOBMJ?RURc@AG`@K@FCV[ZCFA?CJWTQd@?HI?OP?BJFDKd@Y@YVO@BPIJKI?GG?ILMJE^y@\"],[\"Moldova\",498,28.4,47.2,\"sOc]QEWFGD?DEBKPT??@LN@G?QVYDA\"],[\"Mongolia\",496,102.9,46.8,\"{u@y]SCcAYQDS?MHq@DQMFKUSUFi@DCN[Fk@ES@_@Rk@Bk@G]MUFWAVb@CFKCSBOGOFQL@Dl@?LDLL\\\\FRH^EJJINZNVF\\\\?^FTHHEV?\\\\KRCXB~@CHKHQJCVKr@EDGGWLOZGPIBM\"],[\"Montenegro\",499,19.3,42.8,\"qKsYD@@CFHADRKEMIEUJBD\"],[\"Morocco\",504,-8.4,29.9,\"j@_UGLG^EBBFXBHFH@@LVFDHd@F^N@`@J?DB`@?DND@HVZTDZJNj@@?IMO?EEMQOEUGMKEMSIGQAi@c@BYI[KKc@QSa@M?KHSA[B\"],[\"Mozambique\",508,35.5,-17.2,\"sTdFMAWDSAEEK?UGOKCHAbACHLZJJh@Pr@j@@LINCNC?BZCB@FHFf@PFDAFC@@HL?DWAULi@SWIOAgA^OP?@U{@OKHEAGB@^KJEMIC@YFMFED?DYEO\"],[\"Myanmar\",104,96.5,21,\"q}@wKTJL@HTFB]j@FPDBQXARGPRf@@OEMFKAUFIHq@FOb@VVEEYBQNWCEJCLO@OGB?MIE@GCE?UOBIO?IKO@KYMMB?KEC@GK?EJIB?^RN@VUCCRKBDNWHMCAFTR\"],[\"N. Cyprus\",0,33.6,35.3,\"mS}TCEO?QELHA@X?\"],[\"Namibia\",516,17.2,-22.1,\"mKnN?hAPHH?TCBGFEHHLOFMN_A@c@PWNe@NQ@OUGM@KFsAAMHo@@s@KUD\\\\LFGl@F?dAR??z@\"],[\"Nepal\",524,84,28.2,\"av@mP?\\\\P?d@EJIZC~@[GQSMo@TEH_@Jm@D\"],[\"Netherlands\",528,5.5,52.3,\"iCm`@CFDPBDJ?CTVMRBL?IEQ][GO?\"],[\"New Caledonia\",540,165.5,-21.3,\"sfBdLYTFBHEZSNSI?YR\"],[\"New Zealand\",554,170.5,-44,\"qmB`XPVNFHGIODKTGAGMEA[FQVYHMG?KHMDEPMRAMGDCN[FKGI@H\\\\NADDAH@B aiBfZ[UIMGEAIMIGNOGCF?H\\\\^GHN?NDP`@XLP?JETABEIOYS[I\"],[\"Nicaragua\",558,-85,12.8,\"hs@yEBBNGB@LC@@f@c@ACCBGAEE@II?CCEBQSIBYGBDAFDL?VFRCH\"],[\"Niger\",562,9.3,17.3,\"iHiMC^OPJp@@XVPHXIF?JK@FNB?LUB?NJXGP@JFH@VIHBH?FGRGRBDBFP@PNID?DD?MVE?GJM?QKAGEk@EAKKM?m@[Gy@g@cAe@_@HIHOG\"],[\"Nigeria\",566,8,9.5,\"u@}B?k@CKOS@CCGBKAYMUSCSFGFI?ICWHIAKGQAYFOKC?MTKFBHNNNh@JFHXJFJIF@JHD@L^b@JNAPYLM^?\"],[\"North Korea\",408,127.2,40.1,\"spAoYCBFALL?LTLTF@JSJBBT@FHH@HCFB@CHCIK@AEKTIOMUIMMGDQ?@I]GGKKJ\"],[\"Norway\",578,14.2,64.5,\"mHyp@GE]AyATp@FHNPBHNV?h@KQE\\\\Ef@QNOw@EID[? mRoj@p@HGMXG\\\\FHLRHREX@VKVD@Ld@CDJP?~@~@EFDFRALRAXKHDVXVJKf@VZBXIFUDm@QKu@Qe@SuAaAwAg@k@I_@@]Oe@?c@C}@LXDUJ cPaq@\\\\Jx@@x@CBCZATG}@E[BSEyAJ mNuo@j@Hb@EMCJGi@CGF[B\"],[\"Oman\",512,56.1,20.6,\"oa@eMEWICBCAKI?GLKDYDMPGB?BXd@HABD@XH?JDDLJ?FD?FHDJATDTm@{@SMg@HM eb@aOBGEEA@BJ\"],[\"Pakistan\",586,69.4,30,\"so@eUPNTB^CHFO\\\\OHPJ?Lr@|@VATTMFANIHCNj@?JLNCDOLM`BDGU[K@GFC@QPGPU_@HSCKBCEM@YGAOIKO?AEU?GE?KGKKEFKS?EE@GIIDQIGcAKMFEN_@F\"],[\"Palestine\",275,35.3,31.9,\"cUuRH@ACCCBACKE@@P\"],[\"Panama\",591,-80.1,8.5,\"jo@mD@BEJBDFA@HDEBKCCPMH@BDJD@BIJPDBK@BDABGTC?BBAEMBC?GGAEF?BI?EB]IEEK?QFMH\"],[\"Papua New Guinea\",598,144.3,-6.6,\"cwAr@gAXWRCJ_@JEHP@CLQJKTK??FMBDBUF@DL@BEd@EXWJOXIZJANND^C?aC k~AhAGDAHDDFS`@UCCYLGH q}ArBTHH?ZKAE[@EIAHK?OM@KKACB?JDJH@BB w_BhBWV@DD@FGHKBOCAAD\"],[\"Paraguay\",600,-58.4,-23.2,\"jc@rKEH?ZSBGCMDG^MCGBHr@PNN@h@GS]BGj@WNAd@a@Ga@IQu@CQH?D\"],[\"Peru\",604,-74.4,-9.2,\"tj@tAP?h@RDNAHJD?FDBGPKJBHK?GJQ?OK?\\\\G@KAO^BD?^FHADBFGNP^HBRI@Gd@Sr@c@FOAEdA}B\\\\UGGHSEMOKAFDDADO@GHKIAKMQWGUSEMBMEA[VKRO@ICG@K?OFLRE@IH\"],[\"Philippines\",608,121.5,15.8,\"ojA}FHOQ@EDBPHI skAgECEAKK?BJOQ@PRXJMCE _nAgDAVDPFSFHENDFVKDMEIJGDFHALJBEGOWMEHOECIM?@OOHCP aiAyDXPc@g@ISCNVX mkAkJ@FELBNJFBNCNS?YH@JEB@HNIFKBFLKPBHEAGECDE@FLS?SGFA_@EQK?KDEEAD gkAcF@IKDK??FTN?S mmAsFEVNEEPHD?MDABKK@?GJOQ@ED\"],[\"Poland\",616,19.3,52.1,\"uMu`@EVJBOb@@DH@PNEHVIV@JBHGFBHKN??GLABDHC?ELAHEFKAGDIDGECDKeAUSBADoA@I@ED\"],[\"Portugal\",620,-8.1,39.6,\"rDeYMGEHYAEHHDBZF@GJDJGDHL?DFDHCH@A[HABEAMGGCSBU\"],[\"Puerto Rico\",630,-66.5,18.2,\"lh@qJMDBBZ@?IAAO?\"],[\"Qatar\",634,51.2,25.3,\"w^oN@MEIEAED?JBJD?DC\"],[\"Romania\",642,24.9,45.9,\"sPm[[B?DHBDABVZI^Ht@AFKCCBAFBHE@GJC@EJGOCU[SGY@IDa@IE@WX?PAF\"],[\"Russia\",643,99.9,61.7,\"sy@sq@i@Eg@Jm@PDPh@@v@C`@GNMXCs@M g_Aqp@s@JDFpBFe@Y]A wuAqn@u@?gAHNNhA?^Bf@MIKYC s{Aan@q@BTF^Ad@GEEc@B mvA{l@QGYA[FADhAC _[kq@eACCD]G]BlALTCKGl@? eM}`@z@ACI[E]D@J m`@cm@o@ODIoBWaAAa@Gg@AMFLDbCP|@R|@d@CPe@NjAADGb@E@ISC@Kg@OPC ixAq`@GR@P]v@\\\\EJZQT?LNKJLBO?i@CMA[JQA[QIFGICCL wpAkYBMI?CWDQQEWBMSEUQY^DPF\\\\?FQVM`@GFQZi@NG\\\\Er@BNHID?HHFPR?HZJVGV@TG\\\\Lj@Fj@C^SRAj@DZGBOh@ETGTRGJPLp@ELIR?PEbAXZDJKX@FGLCHKHC\\\\DXKJHj@q@XOGEp@RR?AIXGRDDUb@CPFp@FHBjAFFDMLRDCDPF_@LDHZ?DDVK^@TFbAU\\\\?h@T@LRKNTCBHNOLO?KJ@HIBHJPBRRQP@LUVNJF?LKPEDGRCJ@\\\\Kl@G@BXQVGNIMCOOHG[G?EPBAGIEQACEBKGI?Eh@EJINBVGFKNA@EECJIf@BHSS?GEDCLCACROCE@IRCH@BCREFSHEGEDQMK@CUIRI{@e@GI\\\\MGKPMMQTUQO^MCMeAOa@Ju@BkAVOF?LTH^DxAOLB_@LA\\\\i@HAGJIMEo@JOEJMm@SQ@SDIMNKIKLKw@FIHV@?JMD_@CCKoB[O@RJW@MEe@A]GUJWKTKKGy@FcBZMIRI@EVAGGJO?Ec@QMQMCu@BCJRNMDELBZUJHLd@ZU@GEUEEIQIJIGMTABIOSXOc@MBMIAIJFRU@HMa@Gi@?c@JPQ@Sa@E{A?NKWKU?g@Is@CECu@AOBk@Ie@?CISGo@Ga@DZDm@@CHSCy@?k@HOFBHxATu@FQCILIE_@C_ABCHsA@AMiA@_@JIJJFWN_@FSS_@Fa@Ee@FMEa@@NQ[GsDJQJs@NoACg@@OF@NWD[CgA@e@Ac@PWENMIG}@Di@Ay@HmAVs@R@JMDDOu@Bg@PRH^@@PFBP?h@KBIRAV@HECGVBIHJFXHXAQHKNIDAFDDb@ChANz@ZFFZMr@NHGPHZCDLVRAFUB@ZR@FNGF`@JFT\\\\BDTZPFMRiAI[QK?G_@EeAk@c@OO]V@JPr@TNYr@Fp@^OLhAD?M\\\\CXHz@A`ADlCtA_@@IJSBKIU@[TAPNR@VF^\\\\ZFL`Ax@ZJJ?JIZL@F noBuk@SA[DTFn@@BEYG {Sw[ECSBOJSABFTBXHJCCGREUKBA\"],[\"Rwanda\",646,29.9,-2,\"_RTGJ?JP?DJJAEWEEC@KE\"],[\"S. Sudan\",728,30.2,7.3,\"gReANMDGXDFANWLEDKTS?EVOKEK[MAMNE@EAM?CBS??CICAEGCOJKASY@KBEK??CI?@NALIFE^BDH?DJK@IHAFIBKTd@`@\\\\BLCFD\"],[\"Saudi Arabia\",682,44.5,24.1,\"{TkQUBKMOAIIRSk@MWD]Lw@f@w@BCHMAGP[R?TKNEBE?K^{@HCCILLf@z@Rx@FPFLTHBDG\\\\AZ@FEDHAFFFHWHEJQDOLMFALS?YJURKJYb@q@H?GY\"],[\"Senegal\",686,-14.5,14.4,\"lIoGFOHEGCU_@I@SCWJWZEVEDALR@LEl@?VB@OQ@OGOBICBELBFEHDT?\"],[\"Serbia\",688,20.8,44.2,\"wJu[OEK@KFADKBAFIDGCC@BBCBDBAFIFFDBDA@PDCIRKHHTMEAAGHGEIF?GEJM\"],[\"Sierra Leone\",694,-11.8,8.5,\"fGqDOQICO?ILAREAPRDJRIHIDU\"],[\"Slovakia\",703,19.5,48.7,\"cMu]LNTEJDl@HPE@GCEOAMKMAIFKCWASF\"],[\"Slovenia\",705,14.9,46.1,\"sGa\\\\O@_@IGFND@FF@?BLAB@L?CABGAI\"],[\"Solomon Is.\",90,159.1,-7.9,\"idBpEEDL?FKOD adB~DBBLQBKE?OX qcBdERADCAGMBIH wbB~CEH`@UCAWL eaBjCGDJAFG?CKF\"],[\"Somalia\",706,45.7,4.8,\"_Y`@JO?iAU[MAQMY?mAyA?e@k@K@ZFZf@lAf@n@dAr@\\\\f@\"],[\"Somaliland\",0,46.2,9.8,\"q]cF?d@T\\\\P?~@WT[IQSTq@Gm@K\"],[\"South Africa\",710,25.1,-29,\"eIzPIIGDCFUBI?QI?iAE@KR@JCDMASMCIICQFO?MCCOKAKSQO[Mc@BMh@@ZNADL?DKJKACIM?Jh@PJV`@d@`@LF^H@BJAHBTCR@d@HJFH?FEDAHG?@@OFMEC?Md@}@ cQbQHGFDTROLEACEIAIODE\"],[\"South Korea\",410,127.8,36.4,\"{mAqVIAGIUACCOVEJ?VFHPBNFP@@ICOHSOCLO\"],[\"Spain\",724,-3.6,40.3,\"tCeV?EIMFEEKFKGAC[IEDIX@DILF?MFG[MiADo@?GFc@FGCUHWC?JPLXBT`@GJJFBLLBLLl@?RNHAJQRC\"],[\"Sri Lanka\",144,80.7,7.7,\"cr@uCBRXHFOB[G_@MHSb@\"],[\"Sudan\",729,29.9,16,\"kNcDTOAUJK@MFEBQF?GO@GEEBCM[Q@@wAW??g@mF?ERBBARGVQJHJL@DDJj@AF@NFRJHJVFDDR?PD_@HG@MAOH??BJ?CDAJRXJ@NKFB@DHB?BR?BCL?D@DALOL@JZJDMF\"],[\"Suriname\",740,-55.9,4.1,\"`a@m@JCP??LHAJQBID?FOAMKECQWBACO?SBHPALGJHX\"],[\"Sweden\",752,16.6,62.8,\"{Eyc@YWEWJI@YMSS@EGDG_A_AQ?EKe@BAMK?y@VA\\\\EF`@DRLCJfA`@LZMLSHPTTDF`@HPVAJNV?DQ^s@\"],[\"Switzerland\",756,8.1,46.8,\"_Eu\\\\@FQB?FHBLABFH?BCHFH?NKH@?GOK@C_@ACCUD\"],[\"Syria\",760,38.5,35,\"iUmSGUIGBGF?@OOQ?IGBUEYBWG_@AHJHBANFVrAh@TG\"],[\"Taiwan\",158,121,23.7,\"ckAgNTn@HO@OKQOOIDBJ\"],[\"Tajikistan\",762,71,38.6,\"ki@eVKUBMNCEIO@OW[EBHADG?FDRC@JSAYDe@ACPGAKBAR`@A\\\\NFGAODAAEHCFFDHJ?DFDANDDA\"],[\"Tanzania\",834,34.8,-6.3,\"eTRkAh@AJ[RHVAJKDB`@O^EBNJz@NVEL@DYJOPCh@QHWJKDg@I?SUDSEA?KFKeAA\"],[\"Thailand\",764,101,15,\"w`A}GPAXBLNEVPGP?COP?@TNl@?NM?Kb@IHKBIHDFJ@@INGB@FEBIRUBJBII_@Sg@FQ@SPYECGQ\\\\k@GCIUMAUKGDAJM?DTAPSKEBKACEO@MNARON@PDF\"],[\"Timor-Leste\",626,126,-8.8,\"cmApDACOEUAE@j@R@I\"],[\"Togo\",768,1,8.4,\"Q{E@HKLAHCB@l@ELNBHS@ICQBG@]FI?EQ?\"],[\"Trinidad and Tobago\",780,-61.3,10.4,\"pe@wEOA?NT?EE?G\"],[\"Tunisia\",788,9.5,34.2,\"}D}QFc@\\\\W@OKKEOBQCIUIMB?HOGABHH?HEB@PJHAHK?CHGB@L\\\\RAPHB\"],[\"Turkey\",792,35.4,39,\"_[gVHBFETAh@DVFXCTDFC?HJHDIGGJ@PCLJ\\\\@NITABFLBRKT?JSLIIOJIUQ]AGMe@@WKWEa@?}@Ti@AWISASHCF@FUJLDETBDKN iOcYSEQ@AFQDBBV@VNDKCAEMHC\"],[\"Turkmenistan\",795,59.3,39.1,\"y_@cYGEWCMDML_@?BIa@S[JANGBSAGBIRc@Vm@P?JNE@FPBBNXFBHN@RE@QL?VQNATIb@?LJPBBMCUNEEML?CQSDOGLKDINB@NDM\"],[\"Uganda\",800,32.4,1.3,\"eTR|@?RFBAEe@KSMKFA?WGEMB]CKIILI`@Tb@?T\"],[\"Ukraine\",804,31.2,49.1,\"{Rq_@g@CKHDBADO@GJWFOCKHi@D?DFHCJBDP@HD@FN@JDR@ND?JXIDB`@E?GR@TXFCHBHCMO?AU?JQDC?EFEVGr@NHEXAFB@EHCIMC@DIQOIAAEHQc@EiBJ?IECQA\"],[\"United Arab Emirates\",784,54.2,23.9,\"g_@cNCA?DOC[@i@g@CFANH?@JCBHB?FHRz@IFW\"],[\"United Kingdom\",826,-2.7,53.9,\"zBu`@ZCCIBIQASJHL |@k`@CKLKVCDEGGDEJH?SJIGSOOg@?TRi@ABNPPS@SXM@Q^WB@JJDGHPHX?`@DHCJHPALDJC]SQC^CDGUEJICM]@\"],[\"United States of America\",840,-99.1,39.5,\"vkAs]gP??GG?CJE@u@JOC]DQEeAZEHIACFG@@BSHI`@R\\\\GFkAWDKIAe@?EG_@ScA?AEMCMWOMEBMCGD?ZOP|@TLN?JEHG?@EEB@Dx@FND]CEBZDL??CDDE@BLLN@EHEGL?FNVCMHGBO@FCJLAMD?PE@EXLNRDLJH?HF@DTLRTBLCLO\\\\?HGT?TDJD@HA@GFEVg@CMRYFCRHXOf@?VBGPB@FAFBL?LKP@LC\\\\Dn@`@DF?LENF@\\\\KHU\\\\e@LGN?JNNEJEHS\\\\W`@??Hv@?jAWACn@BBIJKHA@Eh@K@KRSN[?CVWBOHICO?QDOGQCc@B[JYAC]FIPEEH_@ b`BqKKJPJBCBKCKIB n`B_LFBDEM@ vaBqLEFF?BEEA rbB{L@DDCGA `gBwd@O@AFJ@XGSC v~Agc@K@IDd@PHEBI]I bwAqj@?zDS?SD]Tg@QKH]Pa@b@a@L?JHH\\\\MBQXOJQp@AVEh@St@KZ@|@QTBCLhANBIISUEDEXJLJZLMHPNh@LDF\\\\HDHVFL?v@P`@D@C}@YWAIG[KSKAMKKVDDCJFJIDDFIRFJ?@KCEJGXB\\\\M@KLGGKOIEI]?OGO?MEBGHCMG`@BDDLE\\\\BZEFGXKgAQO?@Hi@?NKVG`@QZGKIc@?WIEISGy@KQ@]K]DOFGCa@@@B_@BSA_BHYCs@H hjB{f@{@HNDVGP@BAAG\"],[\"Uruguay\",858,-56,-32.8,\"~b@zQKASNG?c@TKLFHCJFJTJNCH@PIL@JKOiA\"],[\"Uzbekistan\",860,63.2,41.7,\"_b@yX@iAs@Ks@VQPy@CWL@RI?CNY@EFE?IMe@QE@PLOFMEWHXNVA@ECIZDNVNADHOBCLJTXE?Kl@Qb@WHSFCR@FC@OZK`@RCHT?\"],[\"Vanuatu\",548,166.9,-15.2,\"ogB|HKJD@DG?E ggBxHBUIDCNH?\"],[\"Venezuela\",862,-66.2,7.2,\"|d@gBADFFb@HDB\\\\EB?GDAXQ@?BLD@FVF@DN@JKDQJKIIHWAMGQFC`@BLQd@?FG?SFMHAGYQUMC@DH@EF?JHHGNG?EOFE?OWG@GGGELM@MH?Dg@?IFO@KE?Co@ANDEHO?OHANI?GBNJ?FEFPDAHDBMN\"],[\"Vietnam\",704,106.3,16.7,\"e`AqEQGSAFKa@MAWBMCSDMLM\\\\i@VKEEKEFQV?Ra@ICc@AOKIFQB@JGFSBXPNPBLa@n@QJKNGd@@`@f@Xf@b@FKEMNK\"],[\"W. Sahara\",732,-12.1,24.3,\"lDiP?b@`A?Ap@R@BHC\\\\lA?DDAGk@AKOE[[UIWEAEOa@?ECK??KA?\"],[\"Yemen\",887,47.5,15.9,\"o_@{JUl@LDBNr@PPLN?HFb@DJJ\\\\@DKAKL[CA?SGG@GEIGD[A]@EFICMUQGy@G\"],[\"Zambia\",894,27.7,-13.4,\"eRdDi@PGHENFTCNDFDPIDz@NALN@JF@FD@\\\\Z`@CJELANBX[?_Ai@?@g@G@?DU?GHO@KECHOBMPO@@UDBPKEk@DGEMEAa@A\"],[\"Zimbabwe\",716,29.8,-18.9,\"oR|Lb@CJINADSFATUP_@a@B][EAAGKGOA?FQ?_@N@fAHNRV\"]];\nvar MM_WORLD_ALIASES = {\"us\":\"United States of America\",\"usa\":\"United States of America\",\"united states\":\"United States of America\",\"america\":\"United States of America\",\"uk\":\"United Kingdom\",\"great britain\":\"United Kingdom\",\"britain\":\"United Kingdom\",\"england\":\"United Kingdom\",\"uae\":\"United Arab Emirates\",\"south korea\":\"South Korea\",\"korea\":\"South Korea\",\"north korea\":\"North Korea\",\"drc\":\"Dem. Rep. Congo\",\"dr congo\":\"Dem. Rep. Congo\",\"democratic republic of the congo\":\"Dem. Rep. Congo\",\"republic of the congo\":\"Congo\",\"czech republic\":\"Czechia\",\"dominican republic\":\"Dominican Rep.\",\"ivory coast\":\"Côte d'Ivoire\",\"cote d'ivoire\":\"Côte d'Ivoire\",\"eswatini\":\"eSwatini\",\"swaziland\":\"eSwatini\",\"east timor\":\"Timor-Leste\",\"north macedonia\":\"Macedonia\",\"bosnia\":\"Bosnia and Herz.\",\"bosnia and herzegovina\":\"Bosnia and Herz.\",\"western sahara\":\"W. Sahara\",\"central african republic\":\"Central African Rep.\",\"south sudan\":\"S. Sudan\",\"equatorial guinea\":\"Eq. Guinea\",\"solomon islands\":\"Solomon Is.\",\"falkland islands\":\"Falkland Is.\",\"holland\":\"Netherlands\",\"burma\":\"Myanmar\",\"persia\":\"Iran\",\"viet nam\":\"Vietnam\",\"lao pdr\":\"Laos\"};\nvar MM_WORLD_CACHE = null;\nfunction mmWorldDecode(s) {\n  var pts = [], i = 0, x = 0, y = 0;\n  while (i < s.length) {\n    var v = 0, sh = 0, b;\n    do { b = s.charCodeAt(i++) - 63; v |= (b & 31) << sh; sh += 5; } while (b >= 32);\n    x += v & 1 ? ~(v >> 1) : v >> 1;\n    v = 0; sh = 0;\n    do { b = s.charCodeAt(i++) - 63; v |= (b & 31) << sh; sh += 5; } while (b >= 32);\n    y += v & 1 ? ~(v >> 1) : v >> 1;\n    pts.push([x / 10, y / 10]);\n  }\n  return pts;\n}\n/** Every country: {name, id (ISO 3166 numeric), c: [lon, lat] label point, rings: [[[lon, lat], …], …]}. */\nfunction worldShapes() {\n  if (!MM_WORLD_CACHE) MM_WORLD_CACHE = MM_WORLD.map(function (r) { return { name: r[0], id: r[1], c: [r[2], r[3]], rings: r[4].split(\" \").map(mmWorldDecode) }; });\n  return MM_WORLD_CACHE;\n}\n/** A country by name (\"India\"), a common short name (\"UK\", \"USA\") or ISO numeric id (356). */\nfunction worldFind(q) {\n  var all = worldShapes(), s = String(q == null ? \"\" : q).trim().toLowerCase();\n  if (!s) return null;\n  var target = MM_WORLD_ALIASES[s] ? MM_WORLD_ALIASES[s].toLowerCase() : s;\n  for (var i = 0; i < all.length; i++) if (all[i].name.toLowerCase() === target || String(all[i].id) === s) return all[i];\n  for (var j = 0; j < all.length; j++) if (all[j].name.toLowerCase().indexOf(target) === 0) return all[j];\n  return null;\n}\n/** Equal Earth: [lon, lat] degrees → unit coordinates (x ±2.71, y ±1.32, y up). */\nfunction mmEqualEarth(lon, lat) {\n  var A1 = 1.340264, A2 = -0.081106, A3 = 0.000893, A4 = 0.003796, l = lon * Math.PI / 180, p = lat * Math.PI / 180;\n  var th = Math.asin(Math.sqrt(3) / 2 * Math.sin(p)), t2 = th * th, t6 = t2 * t2 * t2;\n  return [2 * Math.sqrt(3) * l * Math.cos(th) / (3 * (9 * A4 * t6 * t2 + 7 * A3 * t6 + 3 * A2 * t2 + A1)), A4 * t6 * t2 * th + A3 * t6 * th + A2 * t2 * th + A1 * th];\n}\n/**\n * A flat world map fitted into a box: o = {x, y, w, h, center: [lon, lat], zoom (1 = whole world),\n * antarctica (false)}. Returns project(lon, lat) → [x, y], path(country or name) → SVG path data,\n * outline (the map's edge as path data, to clip or fill the sea), clipId(), and the countries.\n */\nfunction worldMap($, o) {\n  o = o || {};\n  var bx = o.x || 0, by = o.y || 0, bw = o.w || $.W, bh = o.h || $.H;\n  var center = o.center || [0, 0], zoom = o.zoom || 1;\n  var fit = Math.min(bw / (2 * 2.7066), bh / (2 * 1.3173)) * zoom;\n  var cy0 = mmEqualEarth(0, center[1])[1];\n  function rotLon(lon) { var l = lon - center[0]; while (l > 180) l -= 360; while (l < -180) l += 360; return l; }\n  function P(lon, lat) { var u = mmEqualEarth(lon, lat); return [bx + bw / 2 + u[0] * fit, by + bh / 2 - (u[1] - cy0) * fit]; }\n  var api = { countries: worldShapes().filter(function (c) { return o.antarctica || c.name !== \"Antarctica\"; }) };\n  api.project = function (lon, lat) { return P(rotLon(lon), lat); };\n  api.ring = function (ring) {\n    var out = [], prev = null;\n    for (var i = 0; i < ring.length; i++) {\n      var l = rotLon(ring[i][0]);\n      if (prev !== null) { while (l - prev > 180) l -= 360; while (prev - l > 180) l += 360; }\n      prev = l;\n      out.push(P(l, ring[i][1]));\n    }\n    return out;\n  };\n  api.path = function (c) {\n    if (typeof c === \"string\" || typeof c === \"number\") c = worldFind(c);\n    if (!c) return \"\";\n    return c.rings.map(function (r) { return \"M\" + api.ring(r).map(function (p) { return $.fx(p[0]) + \" \" + $.fx(p[1]); }).join(\"L\") + \"Z\"; }).join(\"\");\n  };\n  var edge = [];\n  for (var lat = -90; lat <= 90; lat += 3) edge.push(P(180, lat));\n  for (var lat2 = 90; lat2 >= -90; lat2 -= 3) edge.push(P(-180, lat2));\n  api.outline = \"M\" + edge.map(function (p) { return $.fx(p[0]) + \" \" + $.fx(p[1]); }).join(\"L\") + \"Z\";\n  var clip = null;\n  api.clipId = function () { if (!clip) { clip = $.uid(\"mmw\"); $.def('<clipPath id=\"' + clip + '\"><path d=\"' + api.outline + '\"/></clipPath>'); } return clip; };\n  return api;\n}\n/** [lon, lat] on a sphere of radius r turned by spin (deg) and tilted by tilt: a 3D point for scene3d. */\nfunction worldOnSphere(lon, lat, r, spin, tilt) {\n  var l = (lon + (spin || 0)) * Math.PI / 180, p = lat * Math.PI / 180;\n  var x = r * Math.cos(p) * Math.sin(l), y = -r * Math.sin(p), z = -r * Math.cos(p) * Math.cos(l);\n  var t = (tilt || 0) * Math.PI / 180, c = Math.cos(t), s = Math.sin(t);\n  return [x, y * c - z * s, y * s + z * c];\n}\n/**\n * Countries on a globe in a scene3d: o = {r, center [x, y, z], spin, tilt, color, colorOf(country) → colour,\n * lift (how far above the sea, 0.004)}. Faces beyond the horizon are clipped away.\n */\nfunction worldGlobe(S, o) {\n  o = o || {};\n  var r = o.r || 300, C = o.center || [0, 0, 0], lift = 1 + (o.lift != null ? o.lift : 0.004);\n  var view = [S.camPos[0] - C[0], S.camPos[1] - C[1], S.camPos[2] - C[2]];\n  var vl = Math.sqrt(view[0] * view[0] + view[1] * view[1] + view[2] * view[2]) || 1;\n  var vn = [view[0] / vl, view[1] / vl, view[2] / vl];\n  // The visible cap ends where the line from the camera grazes the sphere, a little short of the centre plane.\n  var cut = (r * r) / vl;\n  var plane = [C[0] + vn[0] * cut, C[1] + vn[1] * cut, C[2] + vn[2] * cut];\n  var faces = [];\n  worldShapes().forEach(function (c) {\n    if (c.name === \"Antarctica\" && !o.antarctica) return;\n    var col = o.colorOf ? o.colorOf(c) : o.color || \"#3a4150\";\n    if (!col) return;\n    c.rings.forEach(function (ring) {\n      var pts = ring.map(function (p) { var q = worldOnSphere(p[0], p[1], r * lift, o.spin, o.tilt); return [q[0] + C[0], q[1] + C[1], q[2] + C[2]]; });\n      var vis = S.clip(pts, vn, plane);\n      if (vis.length < 3) return;\n      var m = [0, 0, 0];\n      vis.forEach(function (q) { m[0] += q[0] - C[0]; m[1] += q[1] - C[1]; m[2] += q[2] - C[2]; });\n      var ml = Math.sqrt(m[0] * m[0] + m[1] * m[1] + m[2] * m[2]) || 1;\n      faces.push({ loops: [vis], n: [m[0] / ml, m[1] / ml, m[2] / ml], color: col, role: \"surface\" });\n    });\n  });\n  S.add({ faces: faces }, { twoSided: true, spec: 0.12 });\n  return S;\n}\n"},"libFiles":{"three-gl":"dist/libs/three-gl.js","three":"dist/libs/three.js"}}