Skip to content

Write HTML. Render video. Built for agents.

fix(core): a call on the playhead fires once when the preview redraws it (#4981)

Miguel Ángela8dcdd109a88
diff --git a/packages/core/src/runtime/adapters/gsap.steps.test.ts b/packages/core/src/runtime/adapters/gsap.steps.test.ts
index 778c10f228..50bbb3ea74 100644
--- a/packages/core/src/runtime/adapters/gsap.steps.test.ts
+++ b/packages/core/src/runtime/adapters/gsap.steps.test.ts
@@ -1,7 +1,7 @@
// @vitest-environment happy-dom
import gsap from "gsap";
import { describe, expect, it } from "vitest";
-import { createGsapAdapter } from "./gsap";
+import { createGsapAdapter, rerenderGsapTimelineAt } from "./gsap";
import type { RuntimeTimelineLike } from "../types";
// Every 0.1 s: hide all three frames, then show one, as frame-by-frame films do.
@@ -214,3 +214,52 @@ describe("gsap adapter at a tween's start", () => {
expect(o.x).toBeCloseTo(x, 6);
});
});
+
+describe("re-rendering onto a call at the playhead", () => {
+ type Film = {
+ build: (timeline: gsap.core.Timeline, fire: () => void) => void;
+ at: number;
+ to: number;
+ };
+ const nestedAt = (callAt: number, repeat = 0, speed = 1): Film["build"] => {
+ return (timeline, fire) => {
+ const scene = gsap.timeline({ repeat }).to({ y: 0 }, { y: 1, duration: 4 });
+ scene.call(fire, [], callAt).timeScale(speed);
+ timeline.add(scene, 1);
+ };
+ };
+ const films: Record<string, Film> = {
+ "a call on the playhead": { build: (tl, fire) => void tl.call(fire, [], 2), at: 2, to: 3 },
+ "a call at 0 on a fresh timeline": {
+ build: (tl, fire) => void tl.call(fire, [], 0),
+ at: 0,
+ to: 1,
+ },
+ "a reversed call": {
+ build: (tl, fire) => void tl.call(fire, [], 2).getChildren().at(-1)!.reversed(true),
+ at: 2,
+ to: 3,
+ },
+ "a call in a nested timeline": { build: nestedAt(1), at: 2, to: 3 },
+ "a call in a repeating nested timeline": { build: nestedAt(2, 1), at: 7, to: 8 },
+ "a call in a nested timeline at double speed": { build: nestedAt(1, 0, 2), at: 1.5, to: 2 },
+ };
+
+ it.each(Object.keys(films).flatMap((film) => [false, true].map((silent) => ({ film, silent }))))(
+ "fires $film as often as without the redraw (arrived silently: $silent)",
+ ({ film, silent }) => {
+ const { build, at, to } = films[film]!;
+ const fires = (redraw: boolean) => {
+ let fired = 0;
+ const timeline = gsap.timeline({ paused: true }).to({ x: 0 }, { x: 1, duration: 10 });
+ build(timeline, () => void fired++);
+ timeline.totalTime(at, silent);
+ if (redraw) rerenderGsapTimelineAt(timeline, at);
+ timeline.totalTime(to, false);
+ return fired;
+ };
+ expect(fires(false)).toBeGreaterThan(0);
+ expect(fires(true)).toBe(fires(false));
+ },
+ );
+});
diff --git a/packages/core/src/runtime/adapters/gsap.ts b/packages/core/src/runtime/adapters/gsap.ts
index 3af49a71e4..734c284396 100644
--- a/packages/core/src/runtime/adapters/gsap.ts
+++ b/packages/core/src/runtime/adapters/gsap.ts
@@ -15,9 +15,36 @@ export function rerenderGsapTimelineAt(
},
t: number,
): void {
+ const children = timeline.getChildren?.(false, true, true) ?? [];
+ const firedStates = callTweensIn(timeline).map(
+ (call) => [call, call.ratio, call._zTime] as const,
+ );
timeline.totalTime(t >= 0.001 ? t - 0.001 : t + 0.001, true);
- primeKeyframedTweensStartingAt(timeline.getChildren?.(false, true, true) ?? [], t);
+ primeKeyframedTweensStartingAt(children, t);
timeline.totalTime(t, true);
+ for (const [call, ratio, zTime] of firedStates) Object.assign(call, { ratio, _zTime: zTime });
+}
+
+type GsapCallInternals = { ratio: number; _zTime?: number };
+
+export const GSAP_CALLBACK_NAMES = [
+ "onStart",
+ "onUpdate",
+ "onComplete",
+ "onReverseComplete",
+ "onRepeat",
+];
+
+function callTweensIn(timeline: {
+ getChildren?: RuntimeTimelineLike["getChildren"];
+}): GsapCallInternals[] {
+ return (timeline.getChildren?.(true, true, false) ?? []).filter((child) => {
+ const tween = child as { totalDuration?: () => number; vars?: Record<string, unknown> };
+ return (
+ tween.totalDuration?.() === 0 &&
+ GSAP_CALLBACK_NAMES.some((name) => typeof tween.vars?.[name] === "function")
+ );
+ }) as unknown as GsapCallInternals[];
}
type GsapAnimation = {
diff --git a/packages/core/src/runtime/init.test.ts b/packages/core/src/runtime/init.test.ts
index ca9f1bc738..d028f82be7 100644
--- a/packages/core/src/runtime/init.test.ts
+++ b/packages/core/src/runtime/init.test.ts
@@ -2920,6 +2920,23 @@ describe("initSandboxRuntimeModular", () => {
expect(fired).toHaveBeenCalledTimes(1);
});
+ it("fires a call on the playhead once when a readiness pass runs between seeks", () => {
+ vi.useFakeTimers();
+ document.body.innerHTML = `<div data-composition-id="main" data-root="true" data-start="0" data-duration="10" data-width="1920" data-height="1080"></div>`;
+ const fired = vi.fn();
+ const main = gsap.timeline({ paused: true }).to({ x: 0 }, { x: 1, duration: 10 });
+ main.call(fired, [], 2);
+ window.__timelines = { main };
+ initSandboxRuntimeModular();
+
+ window.__player?.renderSeek(2);
+ vi.runOnlyPendingTimers();
+ window.__player?.renderSeek(3);
+
+ expect(fired).toHaveBeenCalledTimes(1);
+ vi.useRealTimers();
+ });
+
it("shows pip video at global start time even when host composition starts late", () => {
// Regression: resolveStartForElement used to add the host composition's start on top of
// the video's own data-start, causing double-offset. A pip video with data-start="45.40"
diff --git a/packages/core/src/runtime/init.ts b/packages/core/src/runtime/init.ts
index 8e0dcb37f9..e22fae957d 100644
--- a/packages/core/src/runtime/init.ts
+++ b/packages/core/src/runtime/init.ts
@@ -8,7 +8,7 @@ import { STUDIO_PREVIEW_LAZY_ATTR, STUDIO_PREVIEW_UPCOMING_ATTR } from "../studi
import { initRuntimeAnalytics, emitAnalyticsEvent } from "./analytics";
import { injectCompositionCssVariables } from "./getVariables";
import { createCssAdapter } from "./adapters/css";
-import { createGsapAdapter, rerenderGsapTimelineAt } from "./adapters/gsap";
+import { createGsapAdapter, GSAP_CALLBACK_NAMES, rerenderGsapTimelineAt } from "./adapters/gsap";
import { createAnimeJsAdapter } from "./adapters/animejs";
import { createLottieAdapter } from "./adapters/lottie";
import { createThreeAdapter } from "./adapters/three";
@@ -4251,14 +4251,6 @@ export function initSandboxRuntimeModular(): void {
const isObjectRecord = (value: unknown): value is Record<string, unknown> =>
typeof value === "object" && value !== null;
- const GSAP_CALLBACK_NAMES = [
- "onStart",
- "onUpdate",
- "onComplete",
- "onReverseComplete",
- "onRepeat",
- ];
-
const readGsapDuration = (child: Record<string, unknown>, property: string): number | null => {
const getter = child[property];
if (typeof getter !== "function") return null;