@bydiorama/motion 0.0.0-stage → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (354) hide show
  1. package/CREDITS.md +53 -0
  2. package/LICENSE +21 -0
  3. package/README.md +137 -2
  4. package/catalog.json +4697 -0
  5. package/dist/npm/brief/brief.d.ts +28 -0
  6. package/dist/npm/brief/brief.js +159 -0
  7. package/dist/npm/brief/types.d.ts +75 -0
  8. package/dist/npm/brief/types.js +2 -0
  9. package/dist/npm/browser/motion.js +1039 -0
  10. package/dist/npm/browser/page.js +4821 -0
  11. package/dist/npm/browser/wizard.js +1511 -0
  12. package/dist/npm/build/design.d.ts +54 -0
  13. package/dist/npm/build/design.js +91 -0
  14. package/dist/npm/build/index.d.ts +39 -0
  15. package/dist/npm/build/index.js +66 -0
  16. package/dist/npm/build/iso.d.ts +41 -0
  17. package/dist/npm/build/iso.js +71 -0
  18. package/dist/npm/catalog.d.ts +24 -0
  19. package/dist/npm/catalog.js +23 -0
  20. package/dist/npm/cli/browser.d.ts +26 -0
  21. package/dist/npm/cli/browser.js +66 -0
  22. package/dist/npm/cli/capture.d.ts +26 -0
  23. package/dist/npm/cli/capture.js +94 -0
  24. package/dist/npm/cli/files.d.ts +14 -0
  25. package/dist/npm/cli/files.js +87 -0
  26. package/dist/npm/cli/frames.d.ts +27 -0
  27. package/dist/npm/cli/frames.js +125 -0
  28. package/dist/npm/cli/guard.d.ts +23 -0
  29. package/dist/npm/cli/guard.js +158 -0
  30. package/dist/npm/cli/library.d.ts +2 -0
  31. package/dist/npm/cli/library.js +55 -0
  32. package/dist/npm/cli/main.d.ts +17 -0
  33. package/dist/npm/cli/main.js +964 -0
  34. package/dist/npm/cli/motion.d.ts +2 -0
  35. package/dist/npm/cli/motion.js +6 -0
  36. package/dist/npm/cli/record.d.ts +36 -0
  37. package/dist/npm/cli/record.js +142 -0
  38. package/dist/npm/cli/review.d.ts +45 -0
  39. package/dist/npm/cli/review.js +139 -0
  40. package/dist/npm/cli/schema.d.ts +852 -0
  41. package/dist/npm/cli/schema.js +120 -0
  42. package/dist/npm/cli/server.d.ts +17 -0
  43. package/dist/npm/cli/server.js +135 -0
  44. package/dist/npm/core/ease.d.ts +39 -0
  45. package/dist/npm/core/ease.js +176 -0
  46. package/dist/npm/core/frame.d.ts +52 -0
  47. package/dist/npm/core/frame.js +60 -0
  48. package/dist/npm/core/shapes.d.ts +8 -0
  49. package/dist/npm/core/shapes.js +88 -0
  50. package/dist/npm/core/stagger.d.ts +19 -0
  51. package/dist/npm/core/stagger.js +77 -0
  52. package/dist/npm/core/tap.d.ts +19 -0
  53. package/dist/npm/core/tap.js +42 -0
  54. package/dist/npm/core/track.d.ts +4 -0
  55. package/dist/npm/core/track.js +23 -0
  56. package/dist/npm/dom/mount.d.ts +127 -0
  57. package/dist/npm/dom/mount.js +950 -0
  58. package/dist/npm/dom/page.d.ts +37 -0
  59. package/dist/npm/dom/page.js +77 -0
  60. package/dist/npm/dom/soundtrack.d.ts +13 -0
  61. package/dist/npm/dom/soundtrack.js +37 -0
  62. package/dist/npm/dom/stage.css.d.ts +1 -0
  63. package/dist/npm/dom/stage.css.js +44 -0
  64. package/dist/npm/dom/tokens.d.ts +10 -0
  65. package/dist/npm/dom/tokens.js +38 -0
  66. package/dist/npm/dom/ui-themes.css.d.ts +8 -0
  67. package/dist/npm/dom/ui-themes.css.js +9 -0
  68. package/dist/npm/film/beats.d.ts +64 -0
  69. package/dist/npm/film/beats.js +282 -0
  70. package/dist/npm/film/pace.d.ts +94 -0
  71. package/dist/npm/film/pace.js +396 -0
  72. package/dist/npm/film/resolve.d.ts +16 -0
  73. package/dist/npm/film/resolve.js +997 -0
  74. package/dist/npm/film/tempo.d.ts +19 -0
  75. package/dist/npm/film/tempo.js +34 -0
  76. package/dist/npm/film/tune.d.ts +17 -0
  77. package/dist/npm/film/tune.js +68 -0
  78. package/dist/npm/film/types.d.ts +416 -0
  79. package/dist/npm/film/types.js +16 -0
  80. package/dist/npm/index.d.ts +18 -0
  81. package/dist/npm/index.js +11 -0
  82. package/dist/npm/primitives/blur.d.ts +14 -0
  83. package/dist/npm/primitives/blur.js +59 -0
  84. package/dist/npm/primitives/canvas.d.ts +5 -0
  85. package/dist/npm/primitives/canvas.js +13 -0
  86. package/dist/npm/primitives/el/box.d.ts +2 -0
  87. package/dist/npm/primitives/el/box.js +24 -0
  88. package/dist/npm/primitives/el/frames.d.ts +15 -0
  89. package/dist/npm/primitives/el/frames.js +71 -0
  90. package/dist/npm/primitives/el/html.d.ts +2 -0
  91. package/dist/npm/primitives/el/html.js +14 -0
  92. package/dist/npm/primitives/el/image.d.ts +2 -0
  93. package/dist/npm/primitives/el/image.js +43 -0
  94. package/dist/npm/primitives/el/slot.d.ts +2 -0
  95. package/dist/npm/primitives/el/slot.js +25 -0
  96. package/dist/npm/primitives/el/text.d.ts +2 -0
  97. package/dist/npm/primitives/el/text.js +13 -0
  98. package/dist/npm/primitives/el/video.d.ts +6 -0
  99. package/dist/npm/primitives/el/video.js +179 -0
  100. package/dist/npm/primitives/fx/counter-roll.d.ts +2 -0
  101. package/dist/npm/primitives/fx/counter-roll.js +32 -0
  102. package/dist/npm/primitives/fx/depth-drift.d.ts +2 -0
  103. package/dist/npm/primitives/fx/depth-drift.js +44 -0
  104. package/dist/npm/primitives/fx/grid-ripple.d.ts +2 -0
  105. package/dist/npm/primitives/fx/grid-ripple.js +59 -0
  106. package/dist/npm/primitives/fx/odometer.d.ts +2 -0
  107. package/dist/npm/primitives/fx/odometer.js +80 -0
  108. package/dist/npm/primitives/fx/pan-across.d.ts +17 -0
  109. package/dist/npm/primitives/fx/pan-across.js +76 -0
  110. package/dist/npm/primitives/fx/path-draw.d.ts +2 -0
  111. package/dist/npm/primitives/fx/path-draw.js +105 -0
  112. package/dist/npm/primitives/fx/pin-drop.d.ts +2 -0
  113. package/dist/npm/primitives/fx/pin-drop.js +92 -0
  114. package/dist/npm/primitives/fx/shape-morph.d.ts +2 -0
  115. package/dist/npm/primitives/fx/shape-morph.js +65 -0
  116. package/dist/npm/primitives/fx/slow-push.d.ts +13 -0
  117. package/dist/npm/primitives/fx/slow-push.js +56 -0
  118. package/dist/npm/primitives/fx/stagger-fill.d.ts +2 -0
  119. package/dist/npm/primitives/fx/stagger-fill.js +33 -0
  120. package/dist/npm/primitives/index.d.ts +4 -0
  121. package/dist/npm/primitives/index.js +78 -0
  122. package/dist/npm/primitives/rig/browser-window.d.ts +2 -0
  123. package/dist/npm/primitives/rig/browser-window.js +142 -0
  124. package/dist/npm/primitives/rig/camera.d.ts +36 -0
  125. package/dist/npm/primitives/rig/camera.js +81 -0
  126. package/dist/npm/primitives/rig/footage-plane.d.ts +27 -0
  127. package/dist/npm/primitives/rig/footage-plane.js +257 -0
  128. package/dist/npm/primitives/rig/layer-stack.d.ts +39 -0
  129. package/dist/npm/primitives/rig/layer-stack.js +276 -0
  130. package/dist/npm/primitives/rig/orbit-ring.d.ts +2 -0
  131. package/dist/npm/primitives/rig/orbit-ring.js +59 -0
  132. package/dist/npm/primitives/rig/tab-stack.d.ts +9 -0
  133. package/dist/npm/primitives/rig/tab-stack.js +143 -0
  134. package/dist/npm/primitives/scene/bar-chart.d.ts +2 -0
  135. package/dist/npm/primitives/scene/bar-chart.js +83 -0
  136. package/dist/npm/primitives/scene/bento-grid.d.ts +49 -0
  137. package/dist/npm/primitives/scene/bento-grid.js +293 -0
  138. package/dist/npm/primitives/scene/grid-wall.d.ts +18 -0
  139. package/dist/npm/primitives/scene/grid-wall.js +110 -0
  140. package/dist/npm/primitives/scene/line-chart.d.ts +2 -0
  141. package/dist/npm/primitives/scene/line-chart.js +121 -0
  142. package/dist/npm/primitives/scene/logo-reveal.d.ts +2 -0
  143. package/dist/npm/primitives/scene/logo-reveal.js +117 -0
  144. package/dist/npm/primitives/scene/marquee-wall.d.ts +2 -0
  145. package/dist/npm/primitives/scene/marquee-wall.js +82 -0
  146. package/dist/npm/primitives/scene/post-carousel.d.ts +16 -0
  147. package/dist/npm/primitives/scene/post-carousel.js +195 -0
  148. package/dist/npm/primitives/scene/strip-cycle.d.ts +49 -0
  149. package/dist/npm/primitives/scene/strip-cycle.js +237 -0
  150. package/dist/npm/primitives/split.d.ts +34 -0
  151. package/dist/npm/primitives/split.js +105 -0
  152. package/dist/npm/primitives/texture/beat-field.d.ts +13 -0
  153. package/dist/npm/primitives/texture/beat-field.js +113 -0
  154. package/dist/npm/primitives/texture/gradient-drift.d.ts +2 -0
  155. package/dist/npm/primitives/texture/gradient-drift.js +56 -0
  156. package/dist/npm/primitives/texture/grain.d.ts +2 -0
  157. package/dist/npm/primitives/texture/grain.js +54 -0
  158. package/dist/npm/primitives/texture/halftone-field.d.ts +2 -0
  159. package/dist/npm/primitives/texture/halftone-field.js +36 -0
  160. package/dist/npm/primitives/texture/signal-field.d.ts +4 -0
  161. package/dist/npm/primitives/texture/signal-field.js +107 -0
  162. package/dist/npm/primitives/texture/vignette.d.ts +2 -0
  163. package/dist/npm/primitives/texture/vignette.js +23 -0
  164. package/dist/npm/primitives/transition/band-sweep.d.ts +22 -0
  165. package/dist/npm/primitives/transition/band-sweep.js +111 -0
  166. package/dist/npm/primitives/transition/beat-cut.d.ts +2 -0
  167. package/dist/npm/primitives/transition/beat-cut.js +10 -0
  168. package/dist/npm/primitives/transition/blur-dissolve.d.ts +2 -0
  169. package/dist/npm/primitives/transition/blur-dissolve.js +28 -0
  170. package/dist/npm/primitives/transition/camera-travel.d.ts +2 -0
  171. package/dist/npm/primitives/transition/camera-travel.js +69 -0
  172. package/dist/npm/primitives/transition/cover-reveal.d.ts +2 -0
  173. package/dist/npm/primitives/transition/cover-reveal.js +52 -0
  174. package/dist/npm/primitives/transition/curtain.d.ts +2 -0
  175. package/dist/npm/primitives/transition/curtain.js +40 -0
  176. package/dist/npm/primitives/transition/dive-in.d.ts +2 -0
  177. package/dist/npm/primitives/transition/dive-in.js +52 -0
  178. package/dist/npm/primitives/transition/fly-past.d.ts +2 -0
  179. package/dist/npm/primitives/transition/fly-past.js +87 -0
  180. package/dist/npm/primitives/transition/iris.d.ts +2 -0
  181. package/dist/npm/primitives/transition/iris.js +38 -0
  182. package/dist/npm/primitives/transition/overlay.d.ts +4 -0
  183. package/dist/npm/primitives/transition/overlay.js +24 -0
  184. package/dist/npm/primitives/transition/push.d.ts +2 -0
  185. package/dist/npm/primitives/transition/push.js +35 -0
  186. package/dist/npm/primitives/transition/signal-wipe.d.ts +2 -0
  187. package/dist/npm/primitives/transition/signal-wipe.js +67 -0
  188. package/dist/npm/primitives/transition/slant-wipe.d.ts +2 -0
  189. package/dist/npm/primitives/transition/slant-wipe.js +30 -0
  190. package/dist/npm/primitives/transition/stack-over.d.ts +2 -0
  191. package/dist/npm/primitives/transition/stack-over.js +38 -0
  192. package/dist/npm/primitives/transition/step-wipe.d.ts +2 -0
  193. package/dist/npm/primitives/transition/step-wipe.js +29 -0
  194. package/dist/npm/primitives/transition/strip-wipe.d.ts +40 -0
  195. package/dist/npm/primitives/transition/strip-wipe.js +121 -0
  196. package/dist/npm/primitives/transition/whip-pan.d.ts +2 -0
  197. package/dist/npm/primitives/transition/whip-pan.js +45 -0
  198. package/dist/npm/primitives/type/block-reveal.d.ts +2 -0
  199. package/dist/npm/primitives/type/block-reveal.js +61 -0
  200. package/dist/npm/primitives/type/char-cascade.d.ts +2 -0
  201. package/dist/npm/primitives/type/char-cascade.js +45 -0
  202. package/dist/npm/primitives/type/echo.d.ts +2 -0
  203. package/dist/npm/primitives/type/echo.js +57 -0
  204. package/dist/npm/primitives/type/fill-text.d.ts +14 -0
  205. package/dist/npm/primitives/type/fill-text.js +82 -0
  206. package/dist/npm/primitives/type/mask-reveal.d.ts +2 -0
  207. package/dist/npm/primitives/type/mask-reveal.js +58 -0
  208. package/dist/npm/primitives/type/scramble.d.ts +2 -0
  209. package/dist/npm/primitives/type/scramble.js +34 -0
  210. package/dist/npm/primitives/type/scribble-highlight.d.ts +2 -0
  211. package/dist/npm/primitives/type/scribble-highlight.js +84 -0
  212. package/dist/npm/primitives/type/shimmer.d.ts +2 -0
  213. package/dist/npm/primitives/type/shimmer.js +35 -0
  214. package/dist/npm/primitives/type/tracking-in.d.ts +2 -0
  215. package/dist/npm/primitives/type/tracking-in.js +37 -0
  216. package/dist/npm/primitives/type/typed-title.d.ts +2 -0
  217. package/dist/npm/primitives/type/typed-title.js +33 -0
  218. package/dist/npm/primitives/type/word-build.d.ts +2 -0
  219. package/dist/npm/primitives/type/word-build.js +44 -0
  220. package/dist/npm/primitives/type/word-roller.d.ts +2 -0
  221. package/dist/npm/primitives/type/word-roller.js +80 -0
  222. package/dist/npm/primitives/types.d.ts +183 -0
  223. package/dist/npm/primitives/types.js +5 -0
  224. package/dist/npm/primitives/ui/cursor-click.d.ts +4 -0
  225. package/dist/npm/primitives/ui/cursor-click.js +68 -0
  226. package/dist/npm/primitives/ui/page-scroll.d.ts +17 -0
  227. package/dist/npm/primitives/ui/page-scroll.js +98 -0
  228. package/dist/npm/review/fft.d.ts +4 -0
  229. package/dist/npm/review/fft.js +71 -0
  230. package/dist/npm/review/measure.d.ts +170 -0
  231. package/dist/npm/review/measure.js +577 -0
  232. package/dist/npm/tokens.d.ts +50 -0
  233. package/dist/npm/tokens.js +49 -0
  234. package/dist/npm/version.d.ts +1 -0
  235. package/dist/npm/version.js +1 -0
  236. package/examples/builder.film.mjs +18 -0
  237. package/films/feature-announcement.film.json +70 -0
  238. package/library/narratives/app-demo.md +64 -0
  239. package/library/narratives/brand-identity.md +78 -0
  240. package/library/narratives/capability-through-type.md +53 -0
  241. package/library/narratives/data-story.md +59 -0
  242. package/library/narratives/explainer.md +64 -0
  243. package/library/narratives/feature-announcement.md +68 -0
  244. package/library/narratives/infinite-loop.md +28 -0
  245. package/library/narratives/platform-launch.md +81 -0
  246. package/library/narratives/social-teaser.md +45 -0
  247. package/library/narratives/title-sequence.md +55 -0
  248. package/library/narratives/website-launch.md +65 -0
  249. package/library/primitives/fx/card-arrangements.md +26 -0
  250. package/library/primitives/fx/counter-roll.md +27 -0
  251. package/library/primitives/fx/depth-drift.md +32 -0
  252. package/library/primitives/fx/grid-ripple.md +58 -0
  253. package/library/primitives/fx/letter-interleave.md +26 -0
  254. package/library/primitives/fx/marquee-highlight.md +27 -0
  255. package/library/primitives/fx/mosaic-fill.md +23 -0
  256. package/library/primitives/fx/odometer.md +44 -0
  257. package/library/primitives/fx/pan-across.md +63 -0
  258. package/library/primitives/fx/path-draw.md +54 -0
  259. package/library/primitives/fx/pin-drop.md +51 -0
  260. package/library/primitives/fx/radial-burst.md +33 -0
  261. package/library/primitives/fx/shape-morph.md +45 -0
  262. package/library/primitives/fx/slow-push.md +63 -0
  263. package/library/primitives/fx/stagger-fill.md +28 -0
  264. package/library/primitives/rig/browser-window.md +113 -0
  265. package/library/primitives/rig/flip-ticker.md +26 -0
  266. package/library/primitives/rig/footage-plane.md +90 -0
  267. package/library/primitives/rig/layer-stack.md +168 -0
  268. package/library/primitives/rig/orbit-ring.md +43 -0
  269. package/library/primitives/rig/rolodex.md +31 -0
  270. package/library/primitives/rig/tab-stack.md +116 -0
  271. package/library/primitives/rig/tumbling-object.md +27 -0
  272. package/library/primitives/scene/bar-chart.md +49 -0
  273. package/library/primitives/scene/bento-grid.md +74 -0
  274. package/library/primitives/scene/end-card.md +28 -0
  275. package/library/primitives/scene/grid-wall.md +61 -0
  276. package/library/primitives/scene/hero-object.md +33 -0
  277. package/library/primitives/scene/kinetic-headline.md +34 -0
  278. package/library/primitives/scene/line-chart.md +47 -0
  279. package/library/primitives/scene/logo-reveal.md +53 -0
  280. package/library/primitives/scene/marquee-wall.md +33 -0
  281. package/library/primitives/scene/mosaic.md +28 -0
  282. package/library/primitives/scene/post-carousel.md +68 -0
  283. package/library/primitives/scene/specimen-grid.md +28 -0
  284. package/library/primitives/scene/stat-with-evidence.md +34 -0
  285. package/library/primitives/scene/strip-cycle.md +122 -0
  286. package/library/primitives/scene/zoomed-ui.md +29 -0
  287. package/library/primitives/texture/ascii.md +22 -0
  288. package/library/primitives/texture/beat-field.md +85 -0
  289. package/library/primitives/texture/depth-fade.md +23 -0
  290. package/library/primitives/texture/dither.md +27 -0
  291. package/library/primitives/texture/gradient-drift.md +50 -0
  292. package/library/primitives/texture/grain.md +39 -0
  293. package/library/primitives/texture/halftone-field.md +26 -0
  294. package/library/primitives/texture/signal-field.md +61 -0
  295. package/library/primitives/texture/vignette.md +53 -0
  296. package/library/primitives/transition/band-sweep.md +63 -0
  297. package/library/primitives/transition/beat-cut.md +27 -0
  298. package/library/primitives/transition/blur-dissolve.md +36 -0
  299. package/library/primitives/transition/camera-travel.md +57 -0
  300. package/library/primitives/transition/collapse-to-anchor.md +27 -0
  301. package/library/primitives/transition/cover-reveal.md +47 -0
  302. package/library/primitives/transition/curtain.md +42 -0
  303. package/library/primitives/transition/dive-in.md +51 -0
  304. package/library/primitives/transition/fly-past.md +51 -0
  305. package/library/primitives/transition/iris.md +49 -0
  306. package/library/primitives/transition/layout-locked-swap.md +28 -0
  307. package/library/primitives/transition/light-dark-flip.md +27 -0
  308. package/library/primitives/transition/push.md +47 -0
  309. package/library/primitives/transition/signal-wipe.md +52 -0
  310. package/library/primitives/transition/slant-wipe.md +44 -0
  311. package/library/primitives/transition/stack-over.md +51 -0
  312. package/library/primitives/transition/step-wipe.md +32 -0
  313. package/library/primitives/transition/strip-wipe.md +80 -0
  314. package/library/primitives/transition/torn-edge.md +26 -0
  315. package/library/primitives/transition/whip-pan.md +47 -0
  316. package/library/primitives/transition/zoom-through-letter.md +27 -0
  317. package/library/primitives/type/accent-number.md +22 -0
  318. package/library/primitives/type/block-reveal.md +41 -0
  319. package/library/primitives/type/char-cascade.md +65 -0
  320. package/library/primitives/type/echo.md +44 -0
  321. package/library/primitives/type/fill-text.md +63 -0
  322. package/library/primitives/type/focus-blur.md +22 -0
  323. package/library/primitives/type/inline-chips.md +27 -0
  324. package/library/primitives/type/mask-reveal.md +65 -0
  325. package/library/primitives/type/parting.md +23 -0
  326. package/library/primitives/type/per-word-restyle.md +28 -0
  327. package/library/primitives/type/scramble.md +26 -0
  328. package/library/primitives/type/scribble-highlight.md +47 -0
  329. package/library/primitives/type/shimmer.md +39 -0
  330. package/library/primitives/type/split-flap.md +26 -0
  331. package/library/primitives/type/style-multiply.md +23 -0
  332. package/library/primitives/type/tracking-in.md +40 -0
  333. package/library/primitives/type/typed-title.md +27 -0
  334. package/library/primitives/type/word-build.md +37 -0
  335. package/library/primitives/type/word-roller.md +52 -0
  336. package/library/primitives/type/writing-ticker.md +27 -0
  337. package/library/primitives/ui/collab-cursors.md +27 -0
  338. package/library/primitives/ui/cursor-click.md +61 -0
  339. package/library/primitives/ui/page-scroll.md +58 -0
  340. package/library/primitives/ui/prompt-typing.md +29 -0
  341. package/library/primitives/ui/streamed-result.md +23 -0
  342. package/library/primitives/ui/tour-hold.md +27 -0
  343. package/library/styles/brand-system.md +49 -0
  344. package/library/styles/clean-explainer.md +42 -0
  345. package/library/styles/dark-showcase.md +48 -0
  346. package/library/styles/editorial-type.md +37 -0
  347. package/library/styles/kinetic-poster.md +54 -0
  348. package/library/styles/product-ui.md +44 -0
  349. package/library/styles/studio-loop.md +41 -0
  350. package/library.json +1467 -0
  351. package/package.json +64 -4
  352. package/schema/film.schema.json +1316 -0
  353. package/showcase/vendor/DIALKIT-LICENSE +21 -0
  354. package/src/studio/theme/fonts/OFL.txt +94 -0
@@ -0,0 +1,950 @@
1
+ // Draws a film into a page. Everything on screen is written by seek(t) from the
2
+ // film time alone, so frames can be drawn in any order and always come out the same.
3
+ import { clamp, progress, resolveEase } from "../core/ease.js";
4
+ import { track } from "../core/track.js";
5
+ import { frameAt } from "../core/frame.js";
6
+ import { pressAt, ringAt } from "../core/tap.js";
7
+ import { resolveFilm } from "../film/resolve.js";
8
+ import { applyTune } from "../film/tune.js";
9
+ import { directionalBlur, streak } from "../primitives/blur.js";
10
+ import { PRIMITIVES, TRANSITIONS } from "../primitives/index.js";
11
+ import { merge } from "../tokens.js";
12
+ import { STAGE_CSS } from "./stage.css.js";
13
+ import { ASPEKTA_URL, UI_THEMES } from "./ui-themes.css.js";
14
+ import { readUiTokens } from "./tokens.js";
15
+ export class FilmError extends Error {
16
+ issues;
17
+ constructor(issues) {
18
+ super(`The film has ${issues.filter((i) => i.level === 'error').length} error(s):\n` +
19
+ issues.filter((i) => i.level === 'error').map((i) => ` ${i.path}: ${i.message}${i.hint ? ` ${i.hint}` : ''}`).join('\n'));
20
+ this.issues = issues;
21
+ }
22
+ }
23
+ const SURFACE = new Set(['base', 'surface', 'sunken', 'accent', 'inverse']);
24
+ const ANCHOR = {
25
+ center: '-50% -50%', 'top-left': '0 0', top: '-50% 0', 'top-right': '-100% 0', left: '0 -50%', right: '-100% -50%',
26
+ 'bottom-left': '0 -100%', bottom: '-50% -100%', 'bottom-right': '-100% -100%',
27
+ };
28
+ const FLEX = { start: 'flex-start', center: 'center', end: 'flex-end', between: 'space-between' };
29
+ /** How wide a placed layer can be, in % of the stage, before its anchor pushes it off stage. */
30
+ export function placedRoom(place) {
31
+ const a = place.anchor ?? 'center', x = Math.min(100, Math.max(0, place.x));
32
+ const room = a.endsWith('left') ? 100 - x : a.endsWith('right') ? x : 2 * Math.min(x, 100 - x);
33
+ // Never narrower than a fifth of the stage: a layer anchored at the edge still needs a line.
34
+ return Math.max(20, room);
35
+ }
36
+ function ensureCss(doc) {
37
+ if (doc.getElementById('m-stage-css'))
38
+ return;
39
+ const style = doc.createElement('style');
40
+ style.id = 'm-stage-css';
41
+ style.textContent = STAGE_CSS;
42
+ doc.head.appendChild(style);
43
+ }
44
+ /**
45
+ * A bundled theme for the stage (`theme.ui`), once per page: its --ui-*
46
+ * tokens scoped to stages that ask for it, and Aspekta, embedded so stills and
47
+ * renders never fall back to another face.
48
+ */
49
+ function ensureUiTheme(doc, name) {
50
+ const id = `m-ui-theme-${name}`;
51
+ if (doc.getElementById(id))
52
+ return;
53
+ const style = doc.createElement('style');
54
+ style.id = id;
55
+ const face = doc.getElementById('m-ui-font') ? '' : `@font-face { font-family: "Aspekta"; font-weight: 100 900; font-style: normal; font-display: block; src: url("${ASPEKTA_URL}") format("woff2"); }\n`;
56
+ style.textContent = face + UI_THEMES[name];
57
+ if (face) {
58
+ const mark = doc.createElement('meta');
59
+ mark.id = 'm-ui-font';
60
+ doc.head.appendChild(mark);
61
+ }
62
+ doc.head.appendChild(style);
63
+ }
64
+ function samplers(tracks, defaultEase, overrides) {
65
+ const out = {};
66
+ for (const [prop, keys] of Object.entries(tracks ?? {}))
67
+ out[prop] = track(keys, defaultEase, overrides);
68
+ return out;
69
+ }
70
+ const rest = () => ({ x: 0, y: 0, scale: 1, rotate: 0, opacity: 1, blur: 0, rotateX: 0, rotateY: 0, skew: 0, yShare: 0, clip: [0, 0, 0, 0] });
71
+ /**
72
+ * The side a wipe clips, as an index into Pose.clip (top, right, bottom, left). The edge
73
+ * travels in the named direction: wipe-right uncovers from the left edge rightwards, so on
74
+ * the way in the clip sits on the right and shrinks; on the way out it grows from the left.
75
+ */
76
+ export function wipeSide(move, leaving) {
77
+ const sides = {
78
+ 'wipe-up': [0, 2], 'wipe-down': [2, 0], 'wipe-left': [3, 1], 'wipe-right': [1, 3],
79
+ };
80
+ return sides[move][leaving ? 1 : 0];
81
+ }
82
+ function applyMove(p, m, k, leaving) {
83
+ // `gone` is how far from rest: 1 before an entrance starts and after an exit ends.
84
+ // The overshoot of a springy ease is kept (gone < 0), so things can pass their mark.
85
+ const gone = leaving ? k : 1 - k;
86
+ const g = Math.max(0, Math.min(1, gone));
87
+ const amount = m.amount ?? 0;
88
+ const dir = leaving ? -1 : 1;
89
+ const fades = !m.move.startsWith('mask-') && !m.move.startsWith('wipe') && m.move !== 'slot-up';
90
+ const rad = ((m.angle ?? 0) * Math.PI) / 180, ux = Math.cos(rad), uy = Math.sin(rad);
91
+ if (m.feather !== undefined && g > 0)
92
+ p.feather = m.feather;
93
+ if (fades)
94
+ p.opacity *= m.move === 'pop' || m.move === 'swing' || m.move === 'pop-check' ? 1 - Math.min(1, g * 2.5) : 1 - g;
95
+ switch (m.move) {
96
+ case 'rise':
97
+ p.y += dir * gone * amount;
98
+ break;
99
+ case 'drop':
100
+ p.y -= dir * gone * amount;
101
+ break;
102
+ case 'slide-left':
103
+ p.x += dir * gone * amount;
104
+ break;
105
+ case 'slide-right':
106
+ p.x -= dir * gone * amount;
107
+ break;
108
+ case 'scale':
109
+ case 'pop':
110
+ p.scale *= 1 - gone * amount;
111
+ break;
112
+ case 'zoom':
113
+ p.scale *= 1 + gone * amount;
114
+ break;
115
+ case 'blur':
116
+ p.blur += g * amount;
117
+ break;
118
+ case 'focus':
119
+ p.blur += g * amount * 0.5;
120
+ p.y += dir * gone * amount;
121
+ break;
122
+ // A mask keeps going the way it came: in from below, out through the top.
123
+ case 'mask-up':
124
+ p.yShare += gone * (leaving ? -1 : 1);
125
+ break;
126
+ case 'mask-down':
127
+ p.yShare += gone * (leaving ? 1 : -1);
128
+ break;
129
+ // A wipe's edge travels in the named direction, uncovering or covering.
130
+ case 'wipe-up':
131
+ case 'wipe-down':
132
+ case 'wipe-left':
133
+ case 'wipe-right': {
134
+ const side = wipeSide(m.move, leaving);
135
+ p.clip[side] = Math.max(p.clip[side], g);
136
+ break;
137
+ }
138
+ // Along any angle: the edge, and the layer by `amount`, travel that way, in and out.
139
+ case 'wipe': {
140
+ p.cut = { angle: m.angle ?? 0, hidden: g, leaving };
141
+ p.x -= dir * gone * amount * ux;
142
+ p.y -= dir * gone * amount * uy;
143
+ break;
144
+ }
145
+ case 'slide':
146
+ p.x -= dir * gone * amount * ux;
147
+ p.y -= dir * gone * amount * uy;
148
+ break;
149
+ case 'swing':
150
+ p.rotateX += dir * gone * amount;
151
+ break;
152
+ // A check arriving: small, turned back and soft, then past its mark and home.
153
+ case 'pop-check':
154
+ p.scale *= 1 - gone * amount;
155
+ p.rotate -= gone * 80;
156
+ p.blur += g * 4;
157
+ break;
158
+ // Into its slot from below, out of it through the top: the box stays where it is.
159
+ case 'slot-up': {
160
+ const d = gone * amount * (leaving ? -1 : 1);
161
+ p.y += d;
162
+ p.slot = (p.slot ?? 0) + d;
163
+ break;
164
+ }
165
+ default: break;
166
+ }
167
+ }
168
+ function writePose(el, p, depth, smear = '') {
169
+ const flat = p.rotateX === 0 && p.rotateY === 0;
170
+ const still = p.x === 0 && p.y === 0 && p.scale === 1 && p.rotate === 0 && p.skew === 0 && p.yShare === 0 && !p.slot && flat;
171
+ const parts = [];
172
+ if (!flat)
173
+ parts.push(`perspective(${depth}px)`);
174
+ // A framing camera turns in depth about its anchor, so the framed point stays where it is.
175
+ const pivot = !flat && p.pivot ? p.pivot : undefined;
176
+ if (pivot)
177
+ parts.push(`translate(${pivot[0]}px, ${pivot[1]}px) rotateX(${p.rotateX}deg) rotateY(${p.rotateY}deg) translate(${-pivot[0]}px, ${-pivot[1]}px)`);
178
+ // A mask travels its own height plus the room above and below it, so the
179
+ // line is fully out of the window before the move starts.
180
+ if (p.yShare !== 0)
181
+ parts.push(`translateY(calc(${p.yShare * 100}% + ${p.yShare * 2} * ${MASK_PAD}))`);
182
+ parts.push(`translate(${p.x}px, ${p.y}px)`);
183
+ if (!flat && !pivot)
184
+ parts.push(`rotateX(${p.rotateX}deg) rotateY(${p.rotateY}deg)`);
185
+ parts.push(`rotate(${p.rotate}deg)`);
186
+ if (p.skew !== 0)
187
+ parts.push(`skewX(${p.skew}deg)`);
188
+ parts.push(`scale(${p.scale})`);
189
+ el.style.transform = still ? '' : parts.join(' ');
190
+ el.style.opacity = p.opacity >= 1 ? '' : String(Math.max(0, p.opacity));
191
+ el.style.filter = [smear, p.blur > 0.01 ? `blur(${p.blur}px)` : ''].filter(Boolean).join(' ');
192
+ // A feathered mask or wipe fades its edge out with a mask image instead of cutting it.
193
+ const soft = p.feather !== undefined && !p.cut && !p.slot && (Math.abs(p.yShare) > 1e-6 || p.clip.some((v) => v > 1e-4));
194
+ if (soft) {
195
+ const cs = getComputedStyle(el), fs = parseFloat(cs.fontSize) || 16, padVar = cs.getPropertyValue('--m-mask-pad').trim();
196
+ const pad = padVar.endsWith('px') ? parseFloat(padVar) : (padVar.endsWith('em') ? parseFloat(padVar) : 0.3) * fs;
197
+ const lh = parseFloat(cs.lineHeight) || fs * 1.2;
198
+ const m = featherMask(p, el.offsetWidth, el.offsetHeight, pad, p.feather === true ? lh / 10 : p.feather);
199
+ el.style.clipPath = '';
200
+ el.style.maskImage = m.image;
201
+ el.style.maskSize = m.size;
202
+ el.style.maskPosition = m.position;
203
+ el.style.maskRepeat = 'no-repeat';
204
+ el.style.maskComposite = 'intersect';
205
+ el.style.setProperty('mask-clip', 'no-clip');
206
+ return;
207
+ }
208
+ if (el.style.maskImage)
209
+ for (const k of ['mask-image', 'mask-size', 'mask-position', 'mask-repeat', 'mask-composite', 'mask-clip'])
210
+ el.style.removeProperty(k);
211
+ // An angled wipe cuts in the layer's own px, so it needs the box's size (untransformed).
212
+ el.style.clipPath = clipFor(p, p.cut && p.cut.hidden > 0 ? [el.offsetWidth, el.offsetHeight] : undefined);
213
+ }
214
+ /**
215
+ * The mask for a feathered mask or wipe on a w × h box: the same window `clipFor` cuts, with
216
+ * each edge fading out over `f` px beyond it rather than cut, so a line rises out of a soft
217
+ * edge. A wipe's edge travels its share of the box plus the feather, so it starts and ends
218
+ * fully off the box: nothing changes the frame the move ends and the mask comes off. The
219
+ * image spans well past the box so overhangs, accents and italics are not cut.
220
+ */
221
+ export function featherMask(p, w, h, pad, f) {
222
+ const X = Math.max(w, h) + pad + f, [t, r, b, l] = p.clip;
223
+ let top = -X + f, bottom = h + X - f, left = -X + f, right = w + X - f;
224
+ if (Math.abs(p.yShare) > 1e-6) {
225
+ // The mask's window, fixed on stage, in the layer's own px as the layer travels through it.
226
+ const travel = p.yShare * (h + 2 * pad);
227
+ top = -pad - travel;
228
+ bottom = h + pad - travel;
229
+ }
230
+ // A wipe's covered share, with the ramp carried past the box at either end of the move.
231
+ if (t > 1e-4)
232
+ top = Math.max(top, t * (h + f));
233
+ if (b > 1e-4)
234
+ bottom = Math.min(bottom, h - b * (h + f));
235
+ if (l > 1e-4)
236
+ left = Math.max(left, l * (w + f));
237
+ if (r > 1e-4)
238
+ right = Math.min(right, w - r * (w + f));
239
+ const px = (v) => `${(v + X).toFixed(2)}px`;
240
+ const ramp = (dir, a, z) => z <= a ? `linear-gradient(${dir}, transparent, transparent)` : `linear-gradient(${dir}, transparent ${px(a - f)}, #000 ${px(a)}, #000 ${px(z)}, transparent ${px(z + f)})`;
241
+ return {
242
+ image: `${ramp('to bottom', top, bottom)}, ${ramp('to right', left, right)}`,
243
+ size: `${(w + 2 * X).toFixed(2)}px ${(h + 2 * X).toFixed(2)}px`,
244
+ position: `${(-X).toFixed(2)}px ${(-X).toFixed(2)}px`,
245
+ };
246
+ }
247
+ /**
248
+ * Room a mask leaves above and below the layer's box, for ascenders, accents,
249
+ * descenders and old-style figures that reach past the line. A stylesheet can
250
+ * change it with `--m-mask-pad` on the layer.
251
+ */
252
+ const MASK_PAD = 'var(--m-mask-pad, 0.3em)';
253
+ /**
254
+ * The most a placed layer without its own width may run: on a still stage, the room its
255
+ * anchor leaves; on a canvas the camera travels, no limit, since a rule or a picture
256
+ * there is meant to run past the stage's edge.
257
+ */
258
+ export const placedMaxWidth = (place, canvas, stageWidth) => canvas ? 'none' : `${(placedRoom(place) / 100) * stageWidth}px`;
259
+ /** Where an artboard sits on the stage: its scale, and the stage px of its top-left corner. */
260
+ export function artboardFit(stage, board) {
261
+ const [W, H] = stage, sw = W / board.width, sh = H / board.height;
262
+ const fit = board.fit ?? 'cover';
263
+ const scale = fit === 'contain' ? Math.min(sw, sh) : fit === 'width' ? sw : fit === 'height' ? sh : Math.max(sw, sh);
264
+ const [ax, ay] = board.align ?? [50, 50];
265
+ return { scale, x: ((W - board.width * scale) * ax) / 100 || 0, y: ((H - board.height * scale) * ay) / 100 || 0 };
266
+ }
267
+ /**
268
+ * What an angled wipe leaves showing of a w × h box, as a polygon in the box's px: the box,
269
+ * less any straight wipes (`inset`, shares of top, right, bottom, left), cut by a line square
270
+ * to `angle`. Entering, the line runs from the box's trailing corner to its leading one and
271
+ * shows what it has passed; leaving, it hides what it has passed. `hidden` is the share of
272
+ * the sweep still to come, or already gone.
273
+ */
274
+ export function cutPolygon(w, h, inset, cut) {
275
+ const [t, r, b, l] = inset;
276
+ const rad = (cut.angle * Math.PI) / 180, ux = Math.cos(rad), uy = Math.sin(rad);
277
+ const s = (x, y) => x * ux + y * uy;
278
+ const proj = [s(0, 0), s(w, 0), s(w, h), s(0, h)];
279
+ const lo = Math.min(...proj), hi = Math.max(...proj);
280
+ const c = lo + (cut.leaving ? cut.hidden : 1 - cut.hidden) * (hi - lo);
281
+ // Signed distance past the line: kept where it is <= 0.
282
+ // Points within a hair of the line count as on it, so a corner on the line is kept once.
283
+ const side = (x, y) => { const d = cut.leaving ? c - s(x, y) : s(x, y) - c; return Math.abs(d) < 1e-6 ? 0 : d; };
284
+ const box = [[l * w, t * h], [(1 - r) * w, t * h], [(1 - r) * w, (1 - b) * h], [l * w, (1 - b) * h]];
285
+ const out = [];
286
+ for (let i = 0; i < box.length; i++) {
287
+ const a = box[i], z = box[(i + 1) % box.length], da = side(a[0], a[1]), dz = side(z[0], z[1]);
288
+ if (da <= 0)
289
+ out.push(a);
290
+ if ((da < 0 && dz > 0) || (da > 0 && dz < 0)) {
291
+ const k = da / (da - dz);
292
+ out.push([a[0] + (z[0] - a[0]) * k, a[1] + (z[1] - a[1]) * k]);
293
+ }
294
+ }
295
+ // A corner or an edge alone shows nothing.
296
+ return out.length < 3 ? [] : out;
297
+ }
298
+ /** The clip-path for a pose's wipes and masks, or '' when nothing is clipped. `size` is the box, needed for an angled wipe. */
299
+ export function clipFor(p, size) {
300
+ const [t, r, b, l] = p.clip;
301
+ const pct = (v) => `${(v * 100).toFixed(3)}%`;
302
+ if (p.cut && p.cut.hidden > 0 && size && Math.abs(p.yShare) < 1e-6) {
303
+ const pts = cutPolygon(size[0], size[1], p.clip, p.cut);
304
+ return pts.length < 3 ? 'polygon(0 0, 0 0, 0 0)' : `polygon(${pts.map(([x, y]) => `${x.toFixed(2)}px ${y.toFixed(2)}px`).join(', ')})`;
305
+ }
306
+ const slot = p.slot ?? 0;
307
+ if (Math.abs(p.yShare) < 1e-6 && Math.abs(slot) > 1e-3) {
308
+ // A slot move: the layer has travelled `slot` px; what passed the box's edge is cut off.
309
+ const px = (wipe, by) => (by > 0 ? (wipe > 0.0001 ? `max(${pct(wipe)}, ${by.toFixed(2)}px)` : `${by.toFixed(2)}px`) : pct(wipe));
310
+ return `inset(${px(t, -slot)} ${pct(r)} ${px(b, slot)} ${pct(l)})`;
311
+ }
312
+ if (Math.abs(p.yShare) < 1e-6) {
313
+ // Wipes clip at the box itself: their edge is the move.
314
+ return t + r + b + l > 0.0001 ? `inset(${pct(t)} ${pct(r)} ${pct(b)} ${pct(l)})` : '';
315
+ }
316
+ // A mask is a window from MASK_PAD above the box to MASK_PAD below it, fixed
317
+ // on stage. The layer travels through it, so in the layer's own space the
318
+ // window moves back by the travel. Sides are left open, so italics and
319
+ // overhangs are not cut. When the move ends the clip is removed: everything
320
+ // inside the padding was already showing, so nothing jumps.
321
+ const y = Math.abs(p.yShare), travel = `(${(y * 100).toFixed(3)}% + ${(y * 2).toFixed(4)} * ${MASK_PAD})`;
322
+ const near = `calc(${travel} - ${MASK_PAD})`, far = `calc(-1 * ${MASK_PAD})`;
323
+ const [mTop, mBottom] = p.yShare > 0 ? [far, near] : [near, far];
324
+ const edge = (wipe, mask) => (wipe > 0.0001 ? `max(${pct(wipe)}, ${mask})` : mask);
325
+ const side = (wipe) => (wipe > 0.0001 ? pct(wipe) : '-100%');
326
+ return `inset(${edge(t, mTop)} ${side(r)} ${edge(b, mBottom)} ${side(l)})`;
327
+ }
328
+ /** Seconds a layout glide takes, and its curve: quick away, a long settle. */
329
+ const GLIDE = 0.45;
330
+ const glideEase = resolveEase([0.22, 1, 0.36, 1]);
331
+ /** A layer's shutter: `true` is the film-standard half frame, a number is the share of a frame. */
332
+ const shutterOf = (v) => (v === true ? 0.5 : typeof v === 'number' && Number.isFinite(v) ? clamp(v, 0, 1) : 0);
333
+ /** White or near-black, whichever has more contrast against the colour. */
334
+ function readableOn(colour, probe) {
335
+ const canvas = probe.ownerDocument.createElement('canvas');
336
+ canvas.width = canvas.height = 1;
337
+ const c = canvas.getContext('2d', { willReadFrequently: true });
338
+ if (!c)
339
+ return '#FFFFFF';
340
+ c.fillStyle = colour;
341
+ c.fillRect(0, 0, 1, 1);
342
+ const [r = 0, g = 0, b = 0] = c.getImageData(0, 0, 1, 1).data;
343
+ const lin = (v) => { const x = v / 255; return x <= 0.04045 ? x / 12.92 : Math.pow((x + 0.055) / 1.055, 2.4); };
344
+ const L = 0.2126 * lin(r) + 0.7152 * lin(g) + 0.0722 * lin(b);
345
+ // Contrast against white is (1.05)/(L+0.05); against near-black about (L+0.05)/0.06.
346
+ return 1.05 / (L + 0.05) >= (L + 0.05) / 0.06 ? '#FFFFFF' : '#14161A';
347
+ }
348
+ export function mount(host, source, options = {}) {
349
+ const doc = host.ownerDocument;
350
+ ensureCss(doc);
351
+ const { film: tuned, orphans } = applyTune(source, options.tune);
352
+ // A ui theme on the page supplies the interface timings and curves; the film's own tokens still win.
353
+ const ui = readUiTokens(host);
354
+ const film = { ...tuned, tokens: merge(ui.tokens, tuned.tokens) };
355
+ const res = resolveFilm(film);
356
+ if (!res.timeline)
357
+ throw new FilmError(res.issues);
358
+ const { timeline, tokens } = res;
359
+ const [W, H] = timeline.size;
360
+ const stage = doc.createElement('div');
361
+ stage.className = 'm-stage';
362
+ stage.style.width = `${W}px`;
363
+ stage.style.height = `${H}px`;
364
+ stage.style.setProperty('--m-u', `${W / 100}px`);
365
+ if (film.theme?.ui) {
366
+ ensureUiTheme(doc, film.theme.ui);
367
+ stage.dataset.uiTheme = film.theme.ui;
368
+ // The theme's tokens follow the colour scheme; pin it, so a render never follows the machine's.
369
+ stage.style.colorScheme = film.theme.scheme ?? 'light';
370
+ }
371
+ if (film.theme?.scheme)
372
+ stage.style.colorScheme = film.theme.scheme;
373
+ if (film.theme?.accent) {
374
+ const accent = film.theme.accent;
375
+ stage.style.setProperty('--m-bg-accent', accent);
376
+ stage.style.setProperty('--m-accent', accent);
377
+ stage.style.setProperty('--m-text-on-accent', film.theme.onAccent ?? readableOn(accent, stage));
378
+ }
379
+ for (const [k, v] of Object.entries(film.theme?.vars ?? {}))
380
+ stage.style.setProperty(k, v);
381
+ if (film.title) {
382
+ stage.setAttribute('role', 'img');
383
+ stage.setAttribute('aria-label', film.title);
384
+ }
385
+ const ctx = {
386
+ tokens, seed: film.seed ?? 1, size: [W, H], fps: timeline.fps, easeOverrides: ui.ease, slots: options.slots ?? {},
387
+ ease: (spec) => resolveEase(spec, ui.ease),
388
+ ...(timeline.tempo ? { beat: 60 / timeline.tempo.bpm } : {}),
389
+ cameraScale: () => drawingScale,
390
+ cameraPose: () => drawingPose,
391
+ };
392
+ let drawingPose = { scale: 1, rotateX: 0, rotateY: 0 };
393
+ /** A layer's smaller copies of its footage, pulled as "<key>@1", "<key>@2", …, largest first. */
394
+ const mipsOf = (key) => {
395
+ const out = [];
396
+ for (let k = 1; options.frames?.[`${key}@${k}`]; k++)
397
+ out.push(options.frames[`${key}@${k}`]);
398
+ return out;
399
+ };
400
+ /** The camera scale of the scene being drawn, set before its layers seek. */
401
+ let drawingScale = 1;
402
+ // The stage must be in the page before primitives build: some of them measure text.
403
+ host.appendChild(stage);
404
+ let observer;
405
+ if (options.fixed)
406
+ host.style.cssText += `;width:${W}px;height:${H}px`;
407
+ else {
408
+ host.style.aspectRatio = `${W} / ${H}`;
409
+ host.style.overflow = 'hidden';
410
+ const fit = () => { stage.style.transform = `scale(${host.clientWidth / W})`; };
411
+ fit();
412
+ observer = new ResizeObserver(fit);
413
+ observer.observe(host);
414
+ }
415
+ const scenes = timeline.scenes.map((spec, i) => {
416
+ const s = spec.source;
417
+ const el = doc.createElement('section');
418
+ el.className = 'm-scene';
419
+ el.dataset.scene = spec.id;
420
+ el.style.zIndex = String(i + 1);
421
+ if (s.scheme)
422
+ el.style.colorScheme = s.scheme;
423
+ const bg = s.background ?? 'base';
424
+ el.style.background = SURFACE.has(bg) ? `var(--m-bg-${bg})` : bg;
425
+ if (bg === 'inverse')
426
+ el.style.color = 'var(--m-text-inverse)';
427
+ if (bg === 'accent')
428
+ el.style.color = 'var(--m-text-on-accent)';
429
+ const cam = doc.createElement('div');
430
+ cam.className = 'm-cam';
431
+ const lay = s.layout ?? {};
432
+ cam.style.alignItems = FLEX[lay.align ?? 'center'];
433
+ cam.style.justifyContent = FLEX[lay.justify ?? 'center'];
434
+ cam.style.textAlign = lay.align === 'start' ? 'left' : lay.align === 'end' ? 'right' : 'center';
435
+ if (lay.gap !== undefined)
436
+ cam.style.gap = `${lay.gap}px`;
437
+ if (lay.padding !== undefined)
438
+ cam.style.padding = `${lay.padding}px`;
439
+ el.appendChild(cam);
440
+ stage.appendChild(el);
441
+ // A scene whose camera moves is a canvas the camera travels over.
442
+ const frame = s.artboard ?? { width: W, height: H };
443
+ const fit = artboardFit([W, H], frame);
444
+ let board;
445
+ const canvas = !!s.follow || !!spec.camera || ['x', 'y', 'scale', 'rotate', 'rotateX', 'rotateY'].some((k) => (s.tracks?.[k]?.length ?? 0) > 1);
446
+ const layers = spec.layers.map((layer) => {
447
+ const le = doc.createElement('div');
448
+ le.className = 'm-layer';
449
+ le.dataset.layer = layer.id;
450
+ const st = layer.source.style ?? {};
451
+ le.dataset.size = st.size ?? 'title';
452
+ if (st.color)
453
+ le.style.color = st.color === 'accent' ? 'var(--m-accent)' : `var(--m-text-${st.color})`;
454
+ if (st.font)
455
+ le.style.fontFamily = `var(--m-font-${st.font})`;
456
+ if (st.weight !== undefined)
457
+ le.style.fontWeight = String(st.weight);
458
+ if (st.align)
459
+ le.style.textAlign = st.align;
460
+ const place = layer.source.place;
461
+ const onBoard = place?.unit === 'px';
462
+ // style.width is a share of the stage, or on an artboard, of the artboard.
463
+ if (st.width !== undefined)
464
+ le.style.maxWidth = `${(st.width / 100) * (onBoard ? frame.width : W)}px`;
465
+ if (place) {
466
+ le.style.position = 'absolute';
467
+ le.style.left = `${place.x}${onBoard ? 'px' : '%'}`;
468
+ le.style.top = `${place.y}${onBoard ? 'px' : '%'}`;
469
+ le.style.translate = ANCHOR[place.anchor ?? 'center'];
470
+ // An absolute box shrinks to the room right of its `left`, so a centred
471
+ // headline would wrap at half the stage. Size it to its content instead,
472
+ // up to style.width, or else up to the room its anchor leaves on stage.
473
+ // Not on a canvas, though: where the scene's camera moves, layers lie beyond the
474
+ // stage on purpose, and the stage's edge is no limit on how wide they are.
475
+ le.style.width = 'max-content';
476
+ // On an artboard the design sets the sizes, not the stage.
477
+ if (st.width === undefined)
478
+ le.style.maxWidth = onBoard ? 'none' : placedMaxWidth(place, canvas, W);
479
+ }
480
+ for (const [k, v] of Object.entries(st.css ?? {}))
481
+ le.style.setProperty(k, v);
482
+ // Layers placed in px sit on the artboard. A run of them shares one; a layer on the
483
+ // stage in between starts a new one, so they stack in the order they are written.
484
+ if (onBoard) {
485
+ if (!board || cam.lastElementChild !== board) {
486
+ board = doc.createElement('div');
487
+ board.className = 'm-artboard';
488
+ board.style.cssText = `position:absolute;left:${fit.x}px;top:${fit.y}px;width:${frame.width}px;height:${frame.height}px;transform-origin:0 0;transform:scale(${fit.scale})`;
489
+ cam.appendChild(board);
490
+ }
491
+ board.appendChild(le);
492
+ }
493
+ else if (layer.source.screen) {
494
+ // Fixed to the screen: a sibling of the camera, never moved by it. Written before every
495
+ // camera layer it lies under the camera (a fixed backdrop); otherwise over it (a vignette).
496
+ le.style.position = le.style.position || 'absolute';
497
+ if (!place)
498
+ le.style.inset = '0';
499
+ le.classList.add('m-screen');
500
+ if (cam.childElementCount === 0)
501
+ el.insertBefore(le, cam);
502
+ else
503
+ el.appendChild(le);
504
+ }
505
+ else
506
+ cam.appendChild(le);
507
+ const frames = options.frames?.[`${spec.id}/${layer.id}`];
508
+ const instance = PRIMITIVES[layer.use].build(le, layer.props, layer.params, { ...ctx, ...(frames ? { frames } : {}), filmStart: spec.start + layer.start, boxOf: (id) => boxOf(spec.id, id), mips: mipsOf(`${spec.id}/${layer.id}`) });
509
+ return {
510
+ spec: layer, el: le, instance, tracks: samplers(layer.source.tracks, tokens.ease_move, ui.ease),
511
+ ease: { ...(layer.enter ? { enter: ctx.ease(layer.enter.ease) } : {}), ...(layer.exit ? { exit: ctx.ease(layer.exit.ease) } : {}) },
512
+ shutter: shutterOf(layer.source.motionBlur), unit: onBoard ? fit.scale : 1,
513
+ // A tap on a mark of footage is already pressed in the footage: only its ring is drawn.
514
+ ...(layer.taps?.some((t) => !t.mark) ? { taps: layer.taps.filter((t) => !t.mark).map((t) => t.t) } : {}),
515
+ };
516
+ });
517
+ // Taps' rings, one per layer they are drawn around.
518
+ const rings = [];
519
+ for (const l of layers)
520
+ for (const tap of l.spec.taps ?? []) {
521
+ const target = tap.ring === null ? undefined : layers.find((o) => o.spec.id === tap.ring);
522
+ if (!target)
523
+ continue;
524
+ let ring = rings.find((r) => r.target === target && r.mark === tap.mark);
525
+ if (!ring) {
526
+ const re = doc.createElement('div');
527
+ re.className = 'm-ring';
528
+ re.style.cssText = `position:absolute;pointer-events:none;box-sizing:border-box;border:${1.5 / target.unit}px solid var(--m-accent);opacity:0`;
529
+ target.el.parentElement.appendChild(re);
530
+ rings.push((ring = { target, el: re, taps: [], ...(tap.mark ? { mark: tap.mark } : {}) }));
531
+ }
532
+ ring.taps.push(tap.t);
533
+ }
534
+ const live = { spec, el, cam, layers, tracks: samplers(s.tracks, tokens.ease_move, ui.ease), handover: false, homes: new Map(),
535
+ depth: s.perspective ?? W * 1.2, shutter: shutterOf(s.motionBlur), rings };
536
+ reflow(live);
537
+ aim(live);
538
+ shape(live);
539
+ const f = s.follow, target = f ? layers.find((l) => l.spec.id === f.layer) : undefined;
540
+ if (f && target?.instance.tip) {
541
+ // Where the layer's box sits in the camera's world, measured once while nothing is moved.
542
+ const k = stage.getBoundingClientRect().width / W || 1;
543
+ const a = target.el.getBoundingClientRect(), c = cam.getBoundingClientRect();
544
+ live.follow = {
545
+ layer: target, origin: [(a.left - c.left) / k, (a.top - c.top) / k],
546
+ at: [((f.at?.[0] ?? 50) / 100) * W, ((f.at?.[1] ?? 50) / 100) * H], lag: f.lag ?? 0, blend: f.blend ?? 0.6,
547
+ // A layer on an artboard draws its tip, and travels, in the design's pixels.
548
+ scale: target.spec.source.place?.unit === 'px' ? fit.scale : 1,
549
+ };
550
+ }
551
+ return live;
552
+ });
553
+ const last = scenes[scenes.length - 1];
554
+ /**
555
+ * Aims a scene's framing camera. Every layer's box is measured where layout put it, with
556
+ * nothing moved: no camera, no enter move, no hand-over. So the camera aims at where a thing
557
+ * sits, never at where its animation happens to carry it, and a layer not yet in, or already
558
+ * gone, is framed where it will be or was (hidden layers keep their room). Layout does not
559
+ * change after build, so this is done once, and again when the fonts have loaded.
560
+ */
561
+ function aim(scene) {
562
+ const keys = scene.spec.camera;
563
+ if (!keys)
564
+ return;
565
+ const world = unmoved(scene, () => {
566
+ const c = scene.cam.getBoundingClientRect(), k = c.width / W || 1;
567
+ return new Map(scene.layers.map((l) => {
568
+ const r = l.el.getBoundingClientRect();
569
+ return [l.spec.id, { x: (r.left - c.left) / k, y: (r.top - c.top) / k, w: r.width / k, h: r.height / k }];
570
+ }));
571
+ });
572
+ scene.frame = keys.map((key) => {
573
+ let box = typeof key.frame === 'string' ? world.get(key.frame.split('#')[0]) : undefined;
574
+ if (box && typeof key.frame === 'string' && key.frame.includes('#')) {
575
+ // A mark in a footage plane's picture, where it is at the key's moment.
576
+ const [lid, mark] = key.frame.split('#');
577
+ const layer = scene.layers.find((l) => l.spec.id === lid), m = layer?.instance.markAt?.(mark, key.t - layer.spec.start);
578
+ if (m)
579
+ box = { x: box.x + m[0], y: box.y + m[1], w: m[2], h: m[3] };
580
+ }
581
+ const target = box
582
+ ? [box.x + key.point[0] * box.w + key.offset[0], box.y + key.point[1] * box.h + key.offset[1]]
583
+ : [(key.frame[0] / 100) * W + key.offset[0], (key.frame[1] / 100) * H + key.offset[1]];
584
+ return {
585
+ t: key.t, target, zoom: key.zoom, anchor: [(key.anchor[0] / 100) * W, (key.anchor[1] / 100) * H], tilt: key.tilt, rotate: key.rotate,
586
+ ease: ctx.ease(key.ease ?? tokens.ease_move), drift: key.drift,
587
+ };
588
+ });
589
+ }
590
+ /**
591
+ * Measures each tap ring's target where layout put it, with nothing moved, and finds its
592
+ * shape: the first element down a single-child chain with rounded corners (a button inside
593
+ * its layer's box), so the ring follows the control, not the box around it.
594
+ */
595
+ function shape(scene) {
596
+ if (!scene.rings.length)
597
+ return;
598
+ unmoved(scene, () => {
599
+ for (const ring of scene.rings) {
600
+ const parent = ring.el.parentElement, pr = parent.getBoundingClientRect(), k = pr.width / (parent.offsetWidth || 1) || 1;
601
+ let e = ring.target.el;
602
+ const radius = (x) => parseFloat(getComputedStyle(x).borderTopLeftRadius) || 0;
603
+ while (!ring.mark && radius(e) === 0 && e.children.length === 1)
604
+ e = e.firstElementChild;
605
+ if (radius(e) === 0)
606
+ e = ring.target.el;
607
+ const r = e.getBoundingClientRect(), w = r.width / k, h = r.height / k;
608
+ ring.box = { x: (r.left - pr.left) / k, y: (r.top - pr.top) / k, w, h, radius: Math.min(radius(e), Math.min(w, h) / 2) };
609
+ }
610
+ });
611
+ }
612
+ /**
613
+ * Layout glides. A glide layer takes no room until it starts and gives it up when its exit
614
+ * starts. The page stays laid out with every layer in place, so nothing re-wraps; instead each
615
+ * layout the scene passes through is measured once here (glide layers taken out with
616
+ * display: none), and the layers are moved between them by transforms over GLIDE seconds.
617
+ */
618
+ function reflow(scene) {
619
+ const glides = scene.layers.filter((l) => l.spec.source.reflow === 'glide' && !l.spec.source.place && !l.spec.source.screen);
620
+ if (!glides.length) {
621
+ delete scene.glide;
622
+ return;
623
+ }
624
+ const events = glides.flatMap((g) => [
625
+ { t: g.spec.start, g, on: true },
626
+ ...(g.spec.exit ? [{ t: g.spec.exit.start, g, on: false }] : []),
627
+ ]).sort((a, b) => a.t - b.t);
628
+ scene.glide = unmoved(scene, () => {
629
+ const display = new Map(scene.layers.map((l) => [l, l.el.style.display]));
630
+ const c = () => scene.cam.getBoundingClientRect();
631
+ const measure = (present) => {
632
+ for (const g of glides)
633
+ g.el.style.display = present.has(g) ? display.get(g) : 'none';
634
+ const cr = c(), k = cr.width / W || 1;
635
+ return new Map(scene.layers.map((l) => {
636
+ const r = l.el.getBoundingClientRect();
637
+ return [l, [(r.left - cr.left) / k, (r.top - cr.top) / k]];
638
+ }));
639
+ };
640
+ const diff = (a, b) => new Map(scene.layers.map((l) => [l, [(a.get(l)[0] - b.get(l)[0]) / l.unit, (a.get(l)[1] - b.get(l)[1]) / l.unit]]));
641
+ const full = measure(new Set(glides));
642
+ const present = new Set();
643
+ let prev = measure(present);
644
+ const base = diff(prev, full);
645
+ const steps = events.map((e) => {
646
+ if (e.on)
647
+ present.add(e.g);
648
+ else
649
+ present.delete(e.g);
650
+ const next = measure(present), delta = diff(next, prev);
651
+ prev = next;
652
+ return { t: e.t, delta };
653
+ });
654
+ for (const g of glides)
655
+ g.el.style.display = display.get(g);
656
+ return { base, steps };
657
+ });
658
+ }
659
+ /** Layout boxes other layers asked for, by scene and layer id; cleared when the fonts load. */
660
+ const boxes = new Map();
661
+ function boxOf(sceneId, id) {
662
+ const key = `${sceneId}/${id}`;
663
+ if (boxes.has(key))
664
+ return boxes.get(key);
665
+ const scene = scenes.find((x) => x.spec.id === sceneId), layer = scene?.layers.find((l) => l.spec.id === id);
666
+ const box = scene && layer ? unmoved(scene, () => {
667
+ const c = scene.cam.getBoundingClientRect(), k = c.width / W || 1, r = layer.el.getBoundingClientRect();
668
+ return { x: (r.left - c.left) / k, y: (r.top - c.top) / k, w: r.width / k, h: r.height / k };
669
+ }) : undefined;
670
+ boxes.set(key, box);
671
+ return box;
672
+ }
673
+ /** Runs `measure` with the scene, its camera and its layers unmoved and the scene laid out. */
674
+ function unmoved(scene, measure) {
675
+ const moved = [scene.el, scene.cam, ...scene.layers.map((l) => l.el)];
676
+ const was = moved.map((e) => [e.style.transform, e.style.display]);
677
+ for (const e of moved)
678
+ e.style.transform = 'none';
679
+ scene.el.style.display = '';
680
+ try {
681
+ return measure();
682
+ }
683
+ finally {
684
+ moved.forEach((e, i) => { e.style.transform = was[i][0]; e.style.display = was[i][1]; });
685
+ }
686
+ }
687
+ let lastSeek = 0;
688
+ function seek(time) {
689
+ const t = clamp(time, 0, timeline.duration);
690
+ lastSeek = t;
691
+ // First take off every finished hand-over's styles, including on scenes not on
692
+ // screen. It must come before this frame's hand-overs are drawn: a scene can be
693
+ // the incoming side of one transition and the outgoing side of the next, and
694
+ // clearing the old one afterwards would wipe what the new one just wrote.
695
+ scenes.forEach((scene, i) => {
696
+ const next = scenes[i + 1];
697
+ if (!scene.handover || !next || !scene.spec.out)
698
+ return;
699
+ if (t >= next.spec.start && t < scene.spec.end && scene.spec.out.duration > 0)
700
+ return;
701
+ const tr = TRANSITIONS[scene.spec.out.use];
702
+ tr.clear(scene.el);
703
+ tr.clear(next.el);
704
+ scene.handover = false;
705
+ });
706
+ scenes.forEach((scene, i) => {
707
+ const { spec } = scene;
708
+ const on = t >= spec.start && (t < spec.end || scene === last);
709
+ scene.el.style.visibility = on ? 'visible' : 'hidden';
710
+ // Off-screen scenes leave rendering entirely: some content (blend modes, SVG
711
+ // filters) still paints under visibility: hidden. Layout was measured at build.
712
+ scene.el.style.display = on ? '' : 'none';
713
+ if (!on)
714
+ return;
715
+ const local = t - spec.start;
716
+ const cam = camAt(scene, local);
717
+ // Motion blur on the camera: the frame's travel over half a frame either side, along the
718
+ // pan, plus the zoom's spread at a third of the way to the corner, which a round blur stands in for.
719
+ let camSmear = '';
720
+ if (scene.shutter > 0) {
721
+ const h = 0.5 / timeline.fps, a = camAt(scene, local - h), b = camAt(scene, local + h);
722
+ const spread = Math.abs(Math.log(b.scale / a.scale)) * Math.hypot(W, H) / 6;
723
+ camSmear = directionalBlur(scene.cam, Math.hypot(streak(b.x - a.x, scene.shutter), streak(spread, scene.shutter)), Math.hypot(streak(b.y - a.y, scene.shutter), streak(spread, scene.shutter)));
724
+ }
725
+ writePose(scene.cam, cam, scene.depth, camSmear);
726
+ const posed = new Map();
727
+ drawingScale = Math.abs(cam.scale) || 1;
728
+ drawingPose = { scale: drawingScale, rotateX: cam.rotateX, rotateY: cam.rotateY };
729
+ for (const layer of scene.layers) {
730
+ const l = layer.spec;
731
+ const lt = local - l.start;
732
+ const gone = lt < 0 || (l.exit !== undefined && local >= l.exit.start + l.exit.duration);
733
+ layer.el.style.visibility = gone ? 'hidden' : 'inherit';
734
+ if (gone)
735
+ continue;
736
+ layer.instance.seek(lt);
737
+ const pose = poseAt(layer, local, scene, cam);
738
+ // Motion blur: the layer's speed from the poses half a frame either side,
739
+ // smeared along its path only, and gone the moment it stops.
740
+ let smear = '';
741
+ if (layer.shutter > 0) {
742
+ const h = 0.5 / timeline.fps, a = poseAt(layer, local - h, scene), b = poseAt(layer, local + h, scene);
743
+ smear = directionalBlur(layer.el, streak(b.x - a.x, layer.shutter), streak(b.y - a.y, layer.shutter));
744
+ }
745
+ writePose(layer.el, pose, W * 0.8, smear);
746
+ // Inside a zoomed camera, a part without its own compositor layer is painted at whole CSS px
747
+ // of the camera's space, so a slow move steps by the zoom (4 px at 4×). While it moves there,
748
+ // it gets a layer of its own and lands where its time puts it; at rest it is painted flat again.
749
+ const prev = drawingScale >= 1.25 ? poseAt(layer, local - 1 / timeline.fps, scene, cam) : undefined;
750
+ const moving = !!prev && (Math.abs(prev.x - pose.x) > 1e-3 || Math.abs(prev.y - pose.y) > 1e-3 || Math.abs(prev.scale - pose.scale) > 1e-5);
751
+ if (moving)
752
+ layer.el.style.setProperty('will-change', 'transform', 'important');
753
+ else
754
+ layer.el.style.removeProperty('will-change');
755
+ posed.set(layer, pose);
756
+ }
757
+ // Rings leave their control's layout box, moved with it but never scaled by its press.
758
+ for (const ring of scene.rings) {
759
+ const r = ringAt(ring.taps, local), p = posed.get(ring.target);
760
+ let b = ring.box;
761
+ if (b && ring.mark) {
762
+ // A mark: where it is in the footage now, inside the layer's box.
763
+ const m = ring.target.instance.markAt?.(ring.mark, local - ring.target.spec.start);
764
+ b = m ? { x: b.x + m[0], y: b.y + m[1], w: m[2], h: m[3], radius: Math.min(m[3] / 2, 14) } : undefined;
765
+ }
766
+ if (!r || !b || !p) {
767
+ ring.el.style.opacity = '0';
768
+ continue;
769
+ }
770
+ const g = r.grow / ring.target.unit, st = ring.el.style;
771
+ st.left = `${b.x + p.x - g}px`;
772
+ st.top = `${b.y + p.y - g}px`;
773
+ st.width = `${b.w + 2 * g}px`;
774
+ st.height = `${b.h + 2 * g}px`;
775
+ st.borderRadius = `${b.radius + g}px`;
776
+ st.opacity = String(r.opacity * p.opacity);
777
+ }
778
+ // Hand-over: this scene is the outgoing one while the next has already started.
779
+ const next = scenes[i + 1];
780
+ const out = spec.out;
781
+ if (out && next) {
782
+ const active = t >= next.spec.start && t < spec.end && out.duration > 0;
783
+ const tr = TRANSITIONS[out.use];
784
+ // Finished hand-overs were cleared at the top of seek.
785
+ if (active) {
786
+ tr.apply(scene.el, next.el, (t - next.spec.start) / out.duration, out.params, ctx);
787
+ scene.handover = true;
788
+ }
789
+ }
790
+ });
791
+ }
792
+ /**
793
+ * Moves the camera so the followed tip sits at its mark. The camera turns and
794
+ * scales about the stage centre, so the translation that puts a world point P
795
+ * at A is A − C − R·S·(P − C). Lag averages the tip over the last `lag`
796
+ * seconds, which trails it smoothly; blend eases over from the scene's own
797
+ * x and y tracks, starting when the layer does.
798
+ */
799
+ function follow(cam, f, local) {
800
+ const l = f.layer, lt = local - l.spec.start;
801
+ const tipAt = (t) => l.instance.tip(Math.max(0, t));
802
+ let p = tipAt(lt);
803
+ if (!p)
804
+ return;
805
+ if (f.lag > 0) {
806
+ const n = 12;
807
+ let sx = 0, sy = 0, m = 0;
808
+ for (let i = 0; i <= n; i++) {
809
+ const q = tipAt(lt - (f.lag * i) / n);
810
+ if (q) {
811
+ sx += q[0];
812
+ sy += q[1];
813
+ m++;
814
+ }
815
+ }
816
+ if (m)
817
+ p = [sx / m, sy / m];
818
+ }
819
+ // The layer's own travel (its enter move, its tracks) carries the tip with it.
820
+ const own = poseAt(l, local);
821
+ const P = [f.origin[0] + f.scale * (p[0] + own.x), f.origin[1] + f.scale * (p[1] + own.y)];
822
+ const C = [W / 2, H / 2], r = (cam.rotate * Math.PI) / 180, s = cam.scale;
823
+ const dx = P[0] - C[0], dy = P[1] - C[1];
824
+ const tx = f.at[0] - C[0] - s * (Math.cos(r) * dx - Math.sin(r) * dy);
825
+ const ty = f.at[1] - C[1] - s * (Math.sin(r) * dx + Math.cos(r) * dy);
826
+ const w = f.blend > 0 ? ctx.ease('smooth')(clamp(lt / f.blend, 0, 1)) : lt >= 0 ? 1 : 0;
827
+ cam.x += (tx - cam.x) * w;
828
+ cam.y += (ty - cam.y) * w;
829
+ }
830
+ /** The scene's camera at `local` seconds: its tracks, then the follow. */
831
+ function camAt(scene, local) {
832
+ const cam = rest();
833
+ if (scene.frame) {
834
+ const f = frameAt(scene.frame, local, [W, H]);
835
+ Object.assign(cam, { x: f.x, y: f.y, scale: f.scale, rotate: f.rotate, rotateX: f.rotateX, rotateY: f.rotateY, pivot: f.pivot });
836
+ }
837
+ sample(cam, scene.tracks, local);
838
+ if (scene.follow)
839
+ follow(cam, scene.follow, local);
840
+ return cam;
841
+ }
842
+ /**
843
+ * Where a layer's enter, exit and tracks put it at `local` seconds into its scene, and with
844
+ * the scene, its parallax: a layer at depth d moves d times the camera's travel since its
845
+ * home time, so inside the camera it moves (d − 1) times it, in its own px.
846
+ */
847
+ function poseAt(layer, local, scene, cam) {
848
+ const l = layer.spec, pose = rest();
849
+ if (l.enter && layer.ease.enter)
850
+ applyMove(pose, l.enter, progress(local, l.enter.start, l.enter.duration, layer.ease.enter), false);
851
+ if (l.exit && layer.ease.exit && local >= l.exit.start)
852
+ applyMove(pose, l.exit, progress(local, l.exit.start, l.exit.duration, layer.ease.exit), true);
853
+ sample(pose, layer.tracks, local - l.start);
854
+ if (layer.taps)
855
+ pose.scale *= pressAt(layer.taps, local);
856
+ if (scene?.glide) {
857
+ const g = scene.glide, b = g.base.get(layer);
858
+ let dx = b[0], dy = b[1];
859
+ for (const step of g.steps) {
860
+ if (local <= step.t)
861
+ break;
862
+ const k = progress(local, step.t, GLIDE, glideEase), d = step.delta.get(layer);
863
+ dx += d[0] * k;
864
+ dy += d[1] * k;
865
+ }
866
+ pose.x += dx;
867
+ pose.y += dy;
868
+ }
869
+ const px = l.parallax;
870
+ if (scene && px && px.depth !== 1) {
871
+ const now = cam ?? camAt(scene, local);
872
+ let home = scene.homes.get(px.home);
873
+ if (!home)
874
+ scene.homes.set(px.home, (home = camAt(scene, px.home)));
875
+ const k = (px.depth - 1) / (layer.unit * (now.scale || 1));
876
+ pose.x += k * (now.x - home.x);
877
+ pose.y += k * (now.y - home.y);
878
+ }
879
+ return pose;
880
+ }
881
+ function sample(pose, s, t) {
882
+ if (s.x)
883
+ pose.x += s.x(t);
884
+ if (s.y)
885
+ pose.y += s.y(t);
886
+ if (s.scale)
887
+ pose.scale *= s.scale(t);
888
+ if (s.rotate)
889
+ pose.rotate += s.rotate(t);
890
+ if (s.opacity)
891
+ pose.opacity *= s.opacity(t);
892
+ if (s.blur)
893
+ pose.blur += s.blur(t);
894
+ if (s.rotateX)
895
+ pose.rotateX += s.rotateX(t);
896
+ if (s.rotateY)
897
+ pose.rotateY += s.rotateY(t);
898
+ if (s.skew)
899
+ pose.skew += s.skew(t);
900
+ }
901
+ function audit(t) {
902
+ seek(t);
903
+ const frame = stage.getBoundingClientRect();
904
+ const k = frame.width > 0 ? W / frame.width : 1;
905
+ const boxes = [];
906
+ // A transition moves whole scenes off stage on purpose, so scenes in a hand-over are not audited.
907
+ const moving = new Set();
908
+ scenes.forEach((s, i) => { if (s.handover)
909
+ moving.add(i).add(i + 1); });
910
+ for (const [i, scene] of scenes.entries()) {
911
+ if (scene.el.style.visibility === 'hidden' || moving.has(i))
912
+ continue;
913
+ for (const layer of scene.layers) {
914
+ if (layer.el.style.visibility === 'hidden')
915
+ continue;
916
+ // Full-bleed layers (textures, fields) may bleed past the edge by design.
917
+ const bleed = layer.el.offsetWidth >= W - 1 && layer.el.offsetHeight >= H - 1;
918
+ const r = layer.el.getBoundingClientRect();
919
+ const x = (r.left - frame.left) * k, y = (r.top - frame.top) * k, width = r.width * k, height = r.height * k;
920
+ const round = (n) => Math.round(n * 10) / 10;
921
+ boxes.push({
922
+ scene: scene.spec.id, layer: layer.spec.id, use: layer.spec.use,
923
+ x: round(x), y: round(y), width: round(width), height: round(height),
924
+ off: bleed ? { left: 0, top: 0, right: 0, bottom: 0 } : { left: round(Math.max(0, -x)), top: round(Math.max(0, -y)), right: round(Math.max(0, x + width - W)), bottom: round(Math.max(0, y + height - H)) },
925
+ empty: width < 1 || height < 1,
926
+ });
927
+ }
928
+ }
929
+ return boxes;
930
+ }
931
+ // Images a layer fills in later (a footage plane's copies) have no source yet: nothing to wait for.
932
+ const images = [...stage.querySelectorAll('img')].filter((img) => img.getAttribute('src'));
933
+ const videos = [...stage.querySelectorAll('video')];
934
+ const ready = Promise.all([
935
+ doc.fonts.ready,
936
+ ...images.map((img) => img.decode().catch(() => { throw new Error(`Image did not load: ${img.getAttribute('src')}`); })),
937
+ ...videos.map((v) => (v.readyState >= 2 ? Promise.resolve() : new Promise((resolve, reject) => {
938
+ v.addEventListener('loadeddata', () => resolve(), { once: true });
939
+ v.addEventListener('error', () => reject(new Error(`Video did not load: ${v.getAttribute('src')}`)), { once: true });
940
+ }))),
941
+ ]).then(() => undefined);
942
+ // Fonts change where text sits: aim the framing cameras again once they are in.
943
+ void ready.then(() => { boxes.clear(); scenes.forEach((s) => { reflow(s); aim(s); shape(s); }); seek(lastSeek); }).catch(() => undefined);
944
+ const settled = () => Promise.all(scenes.flatMap((s) => s.layers.map((l) => l.instance.pending?.()))).then(() => undefined);
945
+ seek(0);
946
+ return {
947
+ film, timeline, tokens, issues: res.issues, orphans, duration: timeline.duration, fps: timeline.fps, stage, ready, seek, settled, audit,
948
+ destroy() { observer?.disconnect(); stage.remove(); },
949
+ };
950
+ }