@algorisys/tinyfly 0.82.0 → 0.87.0

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 (71) hide show
  1. package/README.md +13 -4
  2. package/bin/tinyfly.mjs +44 -6
  3. package/lib/addons/adapters.js +411 -248
  4. package/lib/addons/characters.js +1652 -1470
  5. package/lib/addons/dom-adapter-Cphr2HZn.js +372 -0
  6. package/lib/addons/gsap-compat.js +197 -187
  7. package/lib/addons/load-scene-vp14_Jj-.js +324 -0
  8. package/lib/addons/maps.js +1 -1
  9. package/lib/addons/scene-3d-webgl.js +240 -0
  10. package/lib/addons/scene-3d.js +306 -0
  11. package/lib/addons/shading-o5T_v9DT.js +89 -0
  12. package/lib/addons/{sketch-CA8ZXhxI.js → sketch-CYPUbk3Q.js} +2 -2
  13. package/lib/browser/tinyfly-scene-3d-webgl.iife.js +116 -0
  14. package/lib/browser/tinyfly-scene-3d.iife.js +3 -0
  15. package/lib/browser/tinyfly.iife.js +4 -4
  16. package/lib/browser/tinyfly.js +4333 -3726
  17. package/lib/browser/tinyfly.umd.js +4 -4
  18. package/lib/cli/tools.js +137 -22
  19. package/lib/embed/tinyfly-embed.iife.js +4 -4
  20. package/lib/embed/tinyfly-embed.js +921 -833
  21. package/lib/engine/tinyfly-engine.js +1022 -776
  22. package/lib/engine/tinyfly-engine.umd.cjs +2 -2
  23. package/lib/headless/headless.js +410 -8
  24. package/lib/player/tinyfly-player.es.js +459 -371
  25. package/lib/player/tinyfly-player.iife.js +2 -2
  26. package/lib/player/tinyfly-player.umd.js +2 -2
  27. package/lib/types/adapters/canvas/bloom.d.ts +33 -0
  28. package/lib/types/adapters/canvas/canvas-adapter.d.ts +36 -0
  29. package/lib/types/adapters/canvas/index.d.ts +2 -0
  30. package/lib/types/adapters/canvas/offscreen.d.ts +13 -0
  31. package/lib/types/adapters/transform-3d.d.ts +99 -0
  32. package/lib/types/adapters/webgl/webgl-adapter.d.ts +17 -0
  33. package/lib/types/browser/index.d.ts +2 -0
  34. package/lib/types/characters/character.d.ts +20 -1
  35. package/lib/types/characters/index.d.ts +2 -0
  36. package/lib/types/characters/rig/body-plan.d.ts +3 -1
  37. package/lib/types/characters/rig/skeleton.d.ts +57 -0
  38. package/lib/types/characters/scene-objects.d.ts +2 -0
  39. package/lib/types/compat/gsap/vars.d.ts +8 -0
  40. package/lib/types/engine/core/bake.d.ts +3 -1
  41. package/lib/types/engine/index.d.ts +1 -0
  42. package/lib/types/engine/interpolation/interpolators.d.ts +10 -3
  43. package/lib/types/engine/math/index.d.ts +11 -0
  44. package/lib/types/engine/math/mat4.d.ts +37 -0
  45. package/lib/types/engine/math/quat.d.ts +39 -0
  46. package/lib/types/engine/math/vec3.d.ts +16 -0
  47. package/lib/types/engine/types.d.ts +23 -2
  48. package/lib/types/headless/index.d.ts +1 -0
  49. package/lib/types/headless/loop-check.d.ts +41 -0
  50. package/lib/types/headless/node-video.d.ts +8 -0
  51. package/lib/types/headless/video-scene.d.ts +7 -1
  52. package/lib/types/scene-3d/camera.d.ts +27 -0
  53. package/lib/types/scene-3d/canvas-2d-renderer.d.ts +21 -0
  54. package/lib/types/scene-3d/geometry/extrude.d.ts +17 -0
  55. package/lib/types/scene-3d/geometry/mesh.d.ts +36 -0
  56. package/lib/types/scene-3d/geometry/primitives.d.ts +16 -0
  57. package/lib/types/scene-3d/geometry/triangulate.d.ts +24 -0
  58. package/lib/types/scene-3d/index.d.ts +20 -0
  59. package/lib/types/scene-3d/load-scene.d.ts +31 -0
  60. package/lib/types/scene-3d/object-kind.d.ts +54 -0
  61. package/lib/types/scene-3d/resolve-scene.d.ts +122 -0
  62. package/lib/types/scene-3d/scene-3d-adapter.d.ts +46 -0
  63. package/lib/types/scene-3d/scene-types.d.ts +192 -0
  64. package/lib/types/scene-3d/shading.d.ts +20 -0
  65. package/lib/types/scene-3d/validate-scene.d.ts +11 -0
  66. package/lib/types/scene-3d/webgl/gl-data.d.ts +29 -0
  67. package/lib/types/scene-3d/webgl/index.d.ts +16 -0
  68. package/lib/types/scene-3d/webgl/shaders.d.ts +29 -0
  69. package/lib/types/scene-3d/webgl/webgl2-renderer.d.ts +33 -0
  70. package/package.json +10 -2
  71. package/lib/addons/dom-adapter-CrNwoomd.js +0 -243
@@ -1,4 +1,4 @@
1
- import { parsePath, createRandom, hashSeed, deserializeTimeline, planNarration } from "@algorisys/tinyfly";
1
+ import { parsePath, createRandom, hashSeed, mat4, quat, deserializeTimeline, planNarration } from "@algorisys/tinyfly";
2
2
  import { spawn } from "node:child_process";
3
3
  import { mkdir, writeFile } from "node:fs/promises";
4
4
  import { resolve, join, dirname } from "node:path";
@@ -268,6 +268,147 @@ function smoothPath(ctx, points) {
268
268
  const last = points[points.length - 1];
269
269
  ctx.lineTo(last.x, last.y);
270
270
  }
271
+ function canvasLike(ctx, width, height) {
272
+ if (typeof OffscreenCanvas !== "undefined") return new OffscreenCanvas(width, height);
273
+ const source = ctx.canvas;
274
+ if (source?.ownerDocument) {
275
+ const canvas = source.ownerDocument.createElement("canvas");
276
+ canvas.width = width;
277
+ canvas.height = height;
278
+ return canvas;
279
+ }
280
+ try {
281
+ return source?.constructor ? new source.constructor(width, height) : null;
282
+ } catch {
283
+ return null;
284
+ }
285
+ }
286
+ const DEG = Math.PI / 180;
287
+ function has3dTransform(t) {
288
+ return t.z !== void 0 || t.rotateX !== void 0 || t.rotateY !== void 0 || t.perspective !== void 0 || Array.isArray(t.quaternion) && t.quaternion.length === 4;
289
+ }
290
+ function perspectiveMatrix(distance) {
291
+ const m = mat4.identity();
292
+ if (distance > 0) m[11] = -1 / distance;
293
+ return m;
294
+ }
295
+ function skewMatrix(skewX, skewY) {
296
+ const m = mat4.identity();
297
+ m[4] = Math.tan(skewX * DEG);
298
+ m[1] = Math.tan(skewY * DEG);
299
+ return m;
300
+ }
301
+ const turn = (axis, degrees) => degrees ? mat4.fromQuat(quat.fromAxisAngle(axis, degrees)) : null;
302
+ function elementMatrix(t, offset, pivot) {
303
+ const parts = [
304
+ mat4.translation([pivot.x, pivot.y, 0]),
305
+ t.perspective !== void 0 ? perspectiveMatrix(t.perspective) : null,
306
+ mat4.translation([offset.x, offset.y, t.z ?? 0]),
307
+ turn([0, 0, 1], t.rotate),
308
+ turn([1, 0, 0], t.rotateX),
309
+ turn([0, 1, 0], t.rotateY),
310
+ Array.isArray(t.quaternion) && t.quaternion.length === 4 ? mat4.fromQuat(quat.normalize(t.quaternion)) : null,
311
+ t.scale !== void 0 ? mat4.scaling([t.scale, t.scale, 1]) : mat4.scaling([t.scaleX ?? 1, t.scaleY ?? 1, 1]),
312
+ t.skewX !== void 0 || t.skewY !== void 0 ? skewMatrix(t.skewX ?? 0, t.skewY ?? 0) : null,
313
+ mat4.translation([-pivot.x, -pivot.y, 0])
314
+ ];
315
+ return parts.reduce((m, part) => part ? mat4.multiply(m, part) : m, mat4.identity());
316
+ }
317
+ function isAffine(m) {
318
+ return m[3] === 0 && m[7] === 0 && m[11] === 0 && m[15] === 1;
319
+ }
320
+ function affinePart(m) {
321
+ return [m[0], m[1], m[4], m[5], m[12], m[13]];
322
+ }
323
+ function isBackFacing(m) {
324
+ const inverse = mat4.invert(m);
325
+ return inverse !== null && inverse[10] < 0;
326
+ }
327
+ function projectPoint(m, x, y) {
328
+ const w = m[3] * x + m[7] * y + m[15];
329
+ if (w <= 1e-6) return null;
330
+ return { x: (m[0] * x + m[4] * y + m[12]) / w, y: (m[1] * x + m[5] * y + m[13]) / w };
331
+ }
332
+ function triangleTransform(from, to) {
333
+ const [s0, s1, s2] = from;
334
+ const [d0, d1, d2] = to;
335
+ const sx1 = s1.x - s0.x;
336
+ const sy1 = s1.y - s0.y;
337
+ const sx2 = s2.x - s0.x;
338
+ const sy2 = s2.y - s0.y;
339
+ const det = sx1 * sy2 - sx2 * sy1;
340
+ if (Math.abs(det) < 1e-9) return null;
341
+ const dx1 = d1.x - d0.x;
342
+ const dy1 = d1.y - d0.y;
343
+ const dx2 = d2.x - d0.x;
344
+ const dy2 = d2.y - d0.y;
345
+ const a = (dx1 * sy2 - dx2 * sy1) / det;
346
+ const b = (dy1 * sy2 - dy2 * sy1) / det;
347
+ const c = (dx2 * sx1 - dx1 * sx2) / det;
348
+ const d = (dy2 * sx1 - dy1 * sx2) / det;
349
+ return [a, b, c, d, d0.x - a * s0.x - c * s0.y, d0.y - b * s0.x - d * s0.y];
350
+ }
351
+ function meshDivisions(m, bounds) {
352
+ const corners = [
353
+ projectPoint(m, bounds.x, bounds.y),
354
+ projectPoint(m, bounds.x + bounds.width, bounds.y),
355
+ projectPoint(m, bounds.x, bounds.y + bounds.height),
356
+ projectPoint(m, bounds.x + bounds.width, bounds.y + bounds.height)
357
+ ];
358
+ if (corners.some((c2) => c2 === null)) return 16;
359
+ const [a, b, c, d] = corners;
360
+ const size = Math.max(Math.hypot(d.x - a.x, d.y - a.y), Math.hypot(c.x - b.x, c.y - b.y));
361
+ return Math.min(16, Math.max(2, Math.round(size / 40)));
362
+ }
363
+ function perspectiveMesh(m, bounds, divisions = meshDivisions(m, bounds)) {
364
+ const n = Math.max(1, Math.round(divisions));
365
+ const grid = [];
366
+ for (let row = 0; row <= n; row++) {
367
+ const line = [];
368
+ for (let col = 0; col <= n; col++) {
369
+ const source = { x: bounds.x + bounds.width * col / n, y: bounds.y + bounds.height * row / n };
370
+ line.push({ source, target: projectPoint(m, source.x, source.y) });
371
+ }
372
+ grid.push(line);
373
+ }
374
+ const triangles = [];
375
+ const add = (p, q, r) => {
376
+ if (p.target && q.target && r.target) triangles.push({ source: [p.source, q.source, r.source], target: [p.target, q.target, r.target] });
377
+ };
378
+ for (let row = 0; row < n; row++) {
379
+ for (let col = 0; col < n; col++) {
380
+ const tl = grid[row][col];
381
+ const tr = grid[row][col + 1];
382
+ const bl = grid[row + 1][col];
383
+ const br = grid[row + 1][col + 1];
384
+ add(tl, tr, br);
385
+ add(tl, br, bl);
386
+ }
387
+ }
388
+ return triangles;
389
+ }
390
+ function growTriangle(points, amount) {
391
+ const [a, b, c] = points;
392
+ const sign = (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x) >= 0 ? 1 : -1;
393
+ const outward = (p, q) => {
394
+ const dx = q.x - p.x;
395
+ const dy = q.y - p.y;
396
+ const len = Math.hypot(dx, dy) || 1;
397
+ return { x: sign * dy / len, y: -sign * dx / len };
398
+ };
399
+ return points.map((p, i) => {
400
+ const prev = points[(i + 2) % 3];
401
+ const next = points[(i + 1) % 3];
402
+ const n1 = outward(prev, p);
403
+ const n2 = outward(p, next);
404
+ const bx = n1.x + n2.x;
405
+ const by = n1.y + n2.y;
406
+ const half = Math.hypot(bx, by) / 2 || 1;
407
+ const reach = Math.min(amount / half, amount * 4);
408
+ const len = Math.hypot(bx, by) || 1;
409
+ return { x: p.x + bx / len * reach, y: p.y + by / len * reach };
410
+ });
411
+ }
271
412
  class CanvasAdapter {
272
413
  targets = /* @__PURE__ */ new Map();
273
414
  targetOrder = [];
@@ -278,6 +419,8 @@ class CanvasAdapter {
278
419
  aliases = /* @__PURE__ */ new Map();
279
420
  // Time of the last applied state, passed to custom draw functions
280
421
  time = 0;
422
+ /** Offscreen layer for see-through targets in perspective (see `drawInPerspective`) */
423
+ layer = null;
281
424
  /**
282
425
  * Register a canvas drawing target.
283
426
  * @param id - The ID to register the target under
@@ -325,7 +468,8 @@ class CanvasAdapter {
325
468
  fill: "fillStyle",
326
469
  stroke: "strokeStyle",
327
470
  strokeWidth: "lineWidth",
328
- motionPathRotate: "rotate"
471
+ motionPathRotate: "rotate",
472
+ rotateZ: "rotate"
329
473
  };
330
474
  // Properties that are position offsets (applied via ctx.translate)
331
475
  static OFFSET_PROPERTIES = /* @__PURE__ */ new Set(["x", "y", "motionPathX", "motionPathY"]);
@@ -348,7 +492,7 @@ class CanvasAdapter {
348
492
  } else if (property === "y" || property === "motionPathY") {
349
493
  offsets.y = value;
350
494
  }
351
- } else if (target.type === "custom" && target.props && property in target.props) {
495
+ } else if (target.type === "custom" && target.props && (property in target.props || target.acceptsProp?.(property))) {
352
496
  target.props[property] = value;
353
497
  } else {
354
498
  const targetProperty = CanvasAdapter.PROPERTY_MAP[property] ?? property;
@@ -381,12 +525,27 @@ class CanvasAdapter {
381
525
  */
382
526
  renderTarget(ctx, target, offset) {
383
527
  ctx.save();
384
- if (offset.x !== 0 || offset.y !== 0) {
385
- ctx.translate(offset.x, offset.y);
386
- }
387
528
  if (target.opacity !== void 0) {
388
529
  ctx.globalAlpha = target.opacity;
389
530
  }
531
+ if (has3dTransform(target)) {
532
+ const matrix = elementMatrix(target, offset, { x: this.getPivotX(target), y: this.getPivotY(target) });
533
+ if (target.backfaceVisibility === "hidden" && isBackFacing(matrix)) {
534
+ ctx.restore();
535
+ return;
536
+ }
537
+ if (isAffine(matrix)) {
538
+ ctx.transform(...affinePart(matrix));
539
+ this.drawContent(ctx, target);
540
+ } else {
541
+ this.drawInPerspective(ctx, target, matrix);
542
+ }
543
+ ctx.restore();
544
+ return;
545
+ }
546
+ if (offset.x !== 0 || offset.y !== 0) {
547
+ ctx.translate(offset.x, offset.y);
548
+ }
390
549
  const hasTransform = target.rotate !== void 0 || target.rotateX !== void 0 || target.rotateY !== void 0 || target.scale !== void 0 || target.scaleX !== void 0 || target.scaleY !== void 0 || target.skewX !== void 0 || target.skewY !== void 0;
391
550
  if (hasTransform) {
392
551
  const centerX = this.getPivotX(target);
@@ -415,6 +574,105 @@ class CanvasAdapter {
415
574
  }
416
575
  ctx.translate(-centerX, -centerY);
417
576
  }
577
+ this.drawContent(ctx, target);
578
+ ctx.restore();
579
+ }
580
+ /**
581
+ * Draw a target in perspective: each triangle of a mesh over its bounds is
582
+ * clipped on the canvas and drawn with the 2D transform that carries it
583
+ * there. The triangles overlap by a pixel so no anti-aliased seam shows.
584
+ */
585
+ drawInPerspective(ctx, target, matrix) {
586
+ const alpha = ctx.globalAlpha;
587
+ const layer = alpha < 1 ? this.perspectiveLayer(ctx) : null;
588
+ if (layer) {
589
+ layer.ctx.setTransform(ctx.getTransform());
590
+ this.drawMesh(layer.ctx, target, matrix);
591
+ ctx.save();
592
+ ctx.setTransform(1, 0, 0, 1, 0, 0);
593
+ ctx.drawImage(layer.canvas, 0, 0);
594
+ ctx.restore();
595
+ return;
596
+ }
597
+ this.drawMesh(ctx, target, matrix);
598
+ }
599
+ drawMesh(ctx, target, matrix) {
600
+ const bounds = this.contentBounds(ctx, target);
601
+ for (const triangle of perspectiveMesh(matrix, bounds)) {
602
+ const transform = triangleTransform(triangle.source, triangle.target);
603
+ if (!transform) continue;
604
+ const [p, q, r] = growTriangle(triangle.target, 1);
605
+ ctx.save();
606
+ ctx.beginPath();
607
+ ctx.moveTo(p.x, p.y);
608
+ ctx.lineTo(q.x, q.y);
609
+ ctx.lineTo(r.x, r.y);
610
+ ctx.closePath();
611
+ ctx.clip();
612
+ ctx.transform(...transform);
613
+ this.drawContent(ctx, target);
614
+ ctx.restore();
615
+ }
616
+ }
617
+ /** A cleared offscreen canvas the size of `ctx`'s, reused frame to frame; null where none can be made. */
618
+ perspectiveLayer(ctx) {
619
+ const width = ctx.canvas?.width ?? 0;
620
+ const height = ctx.canvas?.height ?? 0;
621
+ if (!(width > 0 && height > 0)) return null;
622
+ if (!this.layer || this.layer.width !== width || this.layer.height !== height) {
623
+ const canvas = canvasLike(ctx, width, height);
624
+ const layerCtx2 = canvas?.getContext("2d");
625
+ this.layer = canvas && layerCtx2 ? { canvas, ctx: layerCtx2, width, height } : null;
626
+ if (!this.layer) return null;
627
+ }
628
+ const layerCtx = this.layer.ctx;
629
+ layerCtx.setTransform(1, 0, 0, 1, 0, 0);
630
+ layerCtx.globalAlpha = 1;
631
+ layerCtx.clearRect(0, 0, width, height);
632
+ return this.layer;
633
+ }
634
+ /**
635
+ * Everything a target paints, generously: its box, measured text, a path's
636
+ * control points, plus room for its stroke, blur and glow. The perspective
637
+ * mesh covers this, so nothing it draws falls outside.
638
+ */
639
+ contentBounds(ctx, target) {
640
+ let box = this.getTargetBounds(target);
641
+ if (target.type === "text") {
642
+ const fontSize = target.fontSize ?? 16;
643
+ ctx.save();
644
+ ctx.font = `${target.fontWeight ?? 400} ${fontSize}px ${target.fontFamily ?? "sans-serif"}`;
645
+ const width = ctx.measureText(target.text).width;
646
+ ctx.restore();
647
+ const align = target.textAlign ?? "left";
648
+ const left = align === "center" ? target.x - width / 2 : align === "right" ? target.x - width : target.x;
649
+ const top = (target.textBaseline ?? "top") === "top" ? target.y : target.y - fontSize;
650
+ box = { x: left, y: top, width, height: fontSize * 1.4 };
651
+ } else if (target.type === "path") {
652
+ const xs = [];
653
+ const ys = [];
654
+ for (const segment of parsePath(target.d).segments) {
655
+ xs.push(segment.startX, segment.endX);
656
+ ys.push(segment.startY, segment.endY);
657
+ for (let i = 0; i + 1 < segment.points.length; i += 2) {
658
+ xs.push(segment.points[i]);
659
+ ys.push(segment.points[i + 1]);
660
+ }
661
+ }
662
+ if (xs.length > 0) {
663
+ const minX = Math.min(...xs);
664
+ const minY = Math.min(...ys);
665
+ box = { x: target.x + minX, y: target.y + minY, width: Math.max(...xs) - minX, height: Math.max(...ys) - minY };
666
+ }
667
+ }
668
+ const pad = (target.lineWidth ?? 1) + 2 + 3 * Math.max(target.blur ?? 0, target.glow ?? 0, target.shadowBlur ?? 0) + Math.max(Math.abs(target.shadowX ?? 0), Math.abs(target.shadowY ?? 0));
669
+ return { x: box.x - pad, y: box.y - pad, width: box.width + 2 * pad, height: box.height + 2 * pad };
670
+ }
671
+ /**
672
+ * Paint a target in the current transform: its fill and stroke styles,
673
+ * filter and clip, then its shape.
674
+ */
675
+ drawContent(ctx, target) {
418
676
  if (target.fillStyle) {
419
677
  ctx.fillStyle = this.resolveFillStyle(ctx, target);
420
678
  }
@@ -461,7 +719,6 @@ class CanvasAdapter {
461
719
  this.renderCustom(ctx, target);
462
720
  break;
463
721
  }
464
- ctx.restore();
465
722
  }
466
723
  /**
467
724
  * Resolve a fill style to a canvas-compatible value.
@@ -861,6 +1118,85 @@ class CanvasAdapter {
861
1118
  });
862
1119
  }
863
1120
  }
1121
+ function brightPass(data, threshold) {
1122
+ const pixels = data.length / 4;
1123
+ const out = new Float32Array(pixels * 3);
1124
+ const t = Math.min(0.999, Math.max(0, threshold));
1125
+ for (let i = 0; i < pixels; i++) {
1126
+ const r = data[i * 4] / 255;
1127
+ const g = data[i * 4 + 1] / 255;
1128
+ const b = data[i * 4 + 2] / 255;
1129
+ const a = data[i * 4 + 3] / 255;
1130
+ const peak = Math.max(r, g, b);
1131
+ if (peak <= t) continue;
1132
+ const k = (peak - t) / (1 - t) * a / peak;
1133
+ out[i * 3] = r * k;
1134
+ out[i * 3 + 1] = g * k;
1135
+ out[i * 3 + 2] = b * k;
1136
+ }
1137
+ return out;
1138
+ }
1139
+ function boxPass(source, width, height, radius, horizontal) {
1140
+ const out = new Float32Array(source.length);
1141
+ const lines = horizontal ? height : width;
1142
+ const length = horizontal ? width : height;
1143
+ const at = (line, i) => (horizontal ? line * width + i : i * width + line) * 3;
1144
+ const span = radius * 2 + 1;
1145
+ for (let line = 0; line < lines; line++) {
1146
+ for (let c = 0; c < 3; c++) {
1147
+ let sum = 0;
1148
+ for (let i = -radius; i <= radius; i++) sum += source[at(line, Math.min(length - 1, Math.max(0, i))) + c];
1149
+ for (let i = 0; i < length; i++) {
1150
+ out[at(line, i) + c] = sum / span;
1151
+ const leaving = source[at(line, Math.max(0, i - radius)) + c];
1152
+ const entering = source[at(line, Math.min(length - 1, i + radius + 1)) + c];
1153
+ sum += entering - leaving;
1154
+ }
1155
+ }
1156
+ }
1157
+ return out;
1158
+ }
1159
+ function boxBlur(source, width, height, radius) {
1160
+ const r = Math.max(1, Math.round(radius / Math.sqrt(3)));
1161
+ let out = source;
1162
+ for (let pass = 0; pass < 3; pass++) {
1163
+ out = boxPass(out, width, height, r, true);
1164
+ out = boxPass(out, width, height, r, false);
1165
+ }
1166
+ return out;
1167
+ }
1168
+ function applyBloom(ctx, options = {}) {
1169
+ const canvas = ctx.canvas;
1170
+ const W = canvas.width;
1171
+ const H = canvas.height;
1172
+ if (!(W > 0 && H > 0)) return;
1173
+ const factor = Math.max(1, Math.round(options.downsample ?? 4));
1174
+ const w = Math.max(1, Math.ceil(W / factor));
1175
+ const h = Math.max(1, Math.ceil(H / factor));
1176
+ const small = canvasLike(ctx, w, h);
1177
+ const sctx = small?.getContext("2d");
1178
+ if (!small || !sctx) return;
1179
+ sctx.imageSmoothingEnabled = true;
1180
+ sctx.drawImage(ctx.canvas, 0, 0, w, h);
1181
+ const bright = brightPass(sctx.getImageData(0, 0, w, h).data, options.threshold ?? 0.55);
1182
+ const radius = (options.radius ?? Math.max(W, H) * 0.02) / factor;
1183
+ const glow = boxBlur(bright, w, h, radius);
1184
+ const wide = boxBlur(bright, w, h, radius * 3);
1185
+ const halo = options.halo ?? 0.6;
1186
+ const image = sctx.createImageData(w, h);
1187
+ for (let i = 0; i < w * h; i++) {
1188
+ for (let c = 0; c < 3; c++) image.data[i * 4 + c] = Math.round(Math.min(1, glow[i * 3 + c] + wide[i * 3 + c] * halo) * 255);
1189
+ image.data[i * 4 + 3] = 255;
1190
+ }
1191
+ sctx.putImageData(image, 0, 0);
1192
+ ctx.save();
1193
+ ctx.setTransform(1, 0, 0, 1, 0, 0);
1194
+ ctx.globalCompositeOperation = "lighter";
1195
+ ctx.globalAlpha = Math.max(0, options.strength ?? 0.9);
1196
+ ctx.imageSmoothingEnabled = true;
1197
+ ctx.drawImage(small, 0, 0, W, H);
1198
+ ctx.restore();
1199
+ }
864
1200
  class FrameRenderer {
865
1201
  scene;
866
1202
  fps;
@@ -910,7 +1246,7 @@ class FrameRenderer {
910
1246
  if (captions.length > 0) {
911
1247
  return captions.map((cue, index) => ({ id: cue.id ?? `line-${index}`, time: (cue.start + cue.end) / 2 }));
912
1248
  }
913
- const markers = [...this.scene.timeline?.config.markers ?? []].sort((a, b) => a.time - b.time);
1249
+ const markers = [...this.scene.timeline?.config?.markers ?? []].sort((a, b) => a.time - b.time);
914
1250
  if (markers.length === 0) return [{ id: "middle", time: this.duration / 2 }];
915
1251
  return markers.map((marker, index) => {
916
1252
  const end = index + 1 < markers.length ? markers[index + 1].time : this.duration;
@@ -937,6 +1273,10 @@ class FrameRenderer {
937
1273
  this.adapter.render(ctx);
938
1274
  if (this.scene.draw) this.drawIsolated(ctx, this.scene.draw, frame);
939
1275
  ctx.restore();
1276
+ if (this.scene.bloom) {
1277
+ const options = this.scene.bloom === true ? {} : this.scene.bloom;
1278
+ applyBloom(ctx, options.radius !== void 0 ? { ...options, radius: options.radius * this.scale } : options);
1279
+ }
940
1280
  }
941
1281
  /** Run scene code without letting its context changes leak into the rest. */
942
1282
  drawIsolated(ctx, draw, frame) {
@@ -1037,6 +1377,48 @@ function sceneCaptions(scene, language) {
1037
1377
  if (scene.timeline) return captionCuesFromTimeline(scene.timeline, { language });
1038
1378
  return [];
1039
1379
  }
1380
+ function psnr(a, b) {
1381
+ if (a.length !== b.length) throw new Error("psnr: the images differ in size");
1382
+ let sum = 0;
1383
+ let count = 0;
1384
+ for (let i = 0; i < a.length; i += 4) {
1385
+ for (let c = 0; c < 3; c++) {
1386
+ const d = a[i + c] - b[i + c];
1387
+ sum += d * d;
1388
+ }
1389
+ count += 3;
1390
+ }
1391
+ if (sum === 0) return Infinity;
1392
+ return 10 * Math.log10(255 * 255 / (sum / count));
1393
+ }
1394
+ const CLOSED_DB = 40;
1395
+ const SEAM_SLACK_DB = 3;
1396
+ const db = (value) => Number.isFinite(value) ? `${value.toFixed(2)} dB` : "identical";
1397
+ function loopReport(frames) {
1398
+ const closure = psnr(frames.wrapped, frames.first);
1399
+ const seam = psnr(frames.last, frames.first);
1400
+ const steps = [psnr(frames.first, frames.second), psnr(frames.beforeLast, frames.last)];
1401
+ const worst = Math.min(...steps);
1402
+ if (closure >= CLOSED_DB) {
1403
+ return { closure, seam, steps, loops: true, verdict: `closes: the frame at the duration matches the first (${db(closure)}), so the seam is an ordinary step` };
1404
+ }
1405
+ if (seam >= worst - SEAM_SLACK_DB) {
1406
+ return {
1407
+ closure,
1408
+ seam,
1409
+ steps,
1410
+ loops: true,
1411
+ verdict: `seamless: the last → first step (${db(seam)}) is like an ordinary step (${db(worst)}), though the motion does not repeat exactly (${db(closure)})`
1412
+ };
1413
+ }
1414
+ return {
1415
+ closure,
1416
+ seam,
1417
+ steps,
1418
+ loops: false,
1419
+ verdict: `does not loop: the last → first step (${db(seam)}) differs much more than an ordinary step (${db(worst)}); make every motion repeat over the duration`
1420
+ };
1421
+ }
1040
1422
  let canvasModule;
1041
1423
  function loadCanvas() {
1042
1424
  canvasModule ??= import("@napi-rs/canvas").then(
@@ -1110,6 +1492,21 @@ async function renderStills(scene, options) {
1110
1492
  }
1111
1493
  return files;
1112
1494
  }
1495
+ async function checkLoop(scene, options = {}) {
1496
+ const { renderer, ctx } = await prepare(scene, options);
1497
+ const at = (time, index) => {
1498
+ renderer.render(ctx, time, index);
1499
+ return ctx.getImageData(0, 0, renderer.width, renderer.height).data.slice();
1500
+ };
1501
+ const last = renderer.frameCount - 1;
1502
+ return loopReport({
1503
+ first: at(0, 0),
1504
+ second: at(renderer.frameTime(1), 1),
1505
+ beforeLast: at(renderer.frameTime(Math.max(0, last - 1)), Math.max(0, last - 1)),
1506
+ last: at(renderer.frameTime(last), last),
1507
+ wrapped: at(renderer.duration, last + 1)
1508
+ });
1509
+ }
1113
1510
  function safeName(id) {
1114
1511
  return id.replace(/[^a-zA-Z0-9_-]+/g, "-") || "frame";
1115
1512
  }
@@ -1227,10 +1624,13 @@ function decodeAudio(file, options = {}) {
1227
1624
  });
1228
1625
  }
1229
1626
  export {
1627
+ CLOSED_DB,
1230
1628
  DEFAULT_FPS,
1231
1629
  FrameRenderer,
1630
+ SEAM_SLACK_DB,
1232
1631
  assembleNarration,
1233
1632
  captionCuesFromTimeline,
1633
+ checkLoop,
1234
1634
  decodeAudio,
1235
1635
  encodeWav,
1236
1636
  ffmpegArgs,
@@ -1238,6 +1638,8 @@ export {
1238
1638
  ffmpegExitError,
1239
1639
  ffmpegStartError,
1240
1640
  frameCount,
1641
+ loopReport,
1642
+ psnr,
1241
1643
  renderStills,
1242
1644
  renderVideo,
1243
1645
  sceneCaptions,