Skip to content

Write HTML. Render video. Built for agents.

fix(core): the preview redraw leaves animations after the playhead as the seek drew them (#4997)

Miguel Ángel805cf60b036b
diff --git a/packages/core/src/runtime/adapters/gsap.steps.test.ts b/packages/core/src/runtime/adapters/gsap.steps.test.ts
index 50bbb3ea74..68ae7a3813 100644
--- a/packages/core/src/runtime/adapters/gsap.steps.test.ts
+++ b/packages/core/src/runtime/adapters/gsap.steps.test.ts
@@ -243,23 +243,152 @@ describe("re-rendering onto a call at the playhead", () => {
"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 },
+ "a call in an otherwise empty nested timeline": {
+ build: (tl, fire) => void tl.add(gsap.timeline().call(fire), 2),
+ at: 2,
+ to: 3,
+ },
};
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) => {
+ const fires = (redraw: boolean, arriveSilently = silent) => {
let fired = 0;
const timeline = gsap.timeline({ paused: true }).to({ x: 0 }, { x: 1, duration: 10 });
build(timeline, () => void fired++);
- timeline.totalTime(at, silent);
+ timeline.totalTime(at, arriveSilently);
if (redraw) rerenderGsapTimelineAt(timeline, at);
timeline.totalTime(to, false);
return fired;
};
- expect(fires(false)).toBeGreaterThan(0);
+ // GSAP itself never fires a call in an otherwise empty nested timeline arrived at silently.
+ expect(fires(false, false)).toBeGreaterThan(0);
expect(fires(true)).toBe(fires(false));
},
);
});
+
+describe("re-rendering leaves the rest as the seek left it", () => {
+ const shapes: Record<string, (timeline: gsap.core.Timeline, o: { x: number }) => void> = {
+ "a reversed set": (tl, o) => void tl.set(o, { x: 100 }, 2).getChildren().at(-1)!.reversed(true),
+ "an immediateRender set": (tl, o) => void tl.set(o, { x: 100, immediateRender: true }, 2),
+ "a reversed set in a nested timeline playing over the playhead": (tl, o) => {
+ const scene = gsap.timeline().to({ y: 0 }, { y: 1, duration: 4 });
+ scene.set(o, { x: 100 }, 1.5).getChildren().at(-1)!.reversed(true);
+ tl.add(scene, 0.5);
+ },
+ "a reversed nested timeline": (tl, o) => {
+ const scene = gsap.timeline().to(o, { x: 100, duration: 1 });
+ tl.add(scene, 2);
+ scene.reversed(true);
+ },
+ };
+
+ it.each(
+ Object.keys(shapes).flatMap((shape) => [false, true].map((silent) => ({ shape, silent }))),
+ )("draws $shape as without the redraw (arrived silently: $silent)", ({ shape, silent }) => {
+ const draws = (redraw: boolean) => {
+ const o = { x: 0 };
+ const timeline = gsap.timeline({ paused: true }).to({ y: 0 }, { y: 1, duration: 10 });
+ shapes[shape]!(timeline, o);
+ timeline.totalTime(1, silent);
+ if (redraw) rerenderGsapTimelineAt(timeline, 1);
+ const atPlayhead = o.x;
+ timeline.totalTime(1.01, false);
+ return [atPlayhead, o.x];
+ };
+ expect(draws(true)).toEqual(draws(false));
+ });
+
+ it("keeps two sets on the playhead in authored order on the next seek", () => {
+ const o = { x: 0 };
+ const timeline = gsap.timeline({ paused: true }).to({ y: 0 }, { y: 1, duration: 10 });
+ // Only the second set has a callback, so restoring callback tweens alone breaks the order.
+ timeline.set(o, { x: 50 }, 2).set(o, { x: 100, onComplete: () => {} }, 2);
+ timeline.totalTime(2, false);
+ rerenderGsapTimelineAt(timeline, 2);
+ timeline.totalTime(2.01, false);
+ expect(o.x).toBe(100);
+ });
+
+ it("undraws a scene's opening set when stepping back off the scene's start", () => {
+ const draws = (redraw: boolean) => {
+ const o = { x: 0 };
+ const timeline = gsap.timeline({ paused: true }).to({ y: 0 }, { y: 1, duration: 10 });
+ timeline.add(gsap.timeline().set(o, { x: 100 }, 0).to({ z: 0 }, { z: 1, duration: 2 }), 2);
+ timeline.totalTime(3, false);
+ timeline.totalTime(2, false);
+ if (redraw) rerenderGsapTimelineAt(timeline, 2);
+ timeline.totalTime(1.99, false);
+ return o.x;
+ };
+ expect(draws(true)).toBe(draws(false));
+ });
+
+ it("fires a pause in a reversed repeating scene as often as without the redraw", () => {
+ const fires = (redraw: boolean) => {
+ let fired = 0;
+ const timeline = gsap.timeline({ paused: true }).to({ y: 0 }, { y: 1, duration: 10 });
+ const scene = gsap.timeline({ repeat: 2, yoyo: true }).to({ z: 0 }, { z: 1, duration: 1 });
+ scene.addPause(0, () => void fired++).timeScale(3);
+ timeline.add(scene, 1);
+ scene.reversed(true);
+ timeline.progress(0.0001, true).totalTime(0, false);
+ timeline.totalTime(3, true);
+ if (redraw) rerenderGsapTimelineAt(timeline, 3);
+ timeline.totalTime(2.99, false);
+ return fired;
+ };
+ expect(fires(true)).toBe(fires(false));
+ });
+
+ it("renders every frame of style-5-prod's paused typed intro as without the redraw", () => {
+ const frames = (redraw: boolean) => {
+ const timeline = gsap.timeline({ paused: true }).to({ y: 0 }, { y: 1, duration: 10 });
+ const scene = gsap.timeline({ paused: true });
+ const typeLine = (text: string, start: number) => {
+ const chars = text.split("").map(() => ({ opacity: 0 }));
+ const cursor = { opacity: 0 };
+ scene.set(cursor, { opacity: 1 }, start);
+ chars.forEach((char, i) =>
+ scene.to(char, { opacity: 1, duration: 0.01 }, start + i * 0.05),
+ );
+ const end = start + chars.length * 0.05;
+ scene
+ .to(cursor, { opacity: 0.25, duration: 0.1 }, end)
+ .to(cursor, { opacity: 1, duration: 0.1 }, end + 0.1);
+ scene.set(cursor, { opacity: 0 }, end + 0.2);
+ return { chars, cursor, end: end + 0.5 };
+ };
+ const line1 = typeLine("SYSTEM BOOTING...", 0.2);
+ const line2 = typeLine("EDITOR AGENT v1.0", line1.end);
+ const container = { opacity: 0 };
+ scene.fromTo(container, { opacity: 0.92 }, { opacity: 1, duration: 0.6 }, 0);
+ timeline.add(scene, 0);
+ const drawn: number[][] = [];
+ for (let frame = 0; frame < 90; frame++) {
+ scene.paused(false);
+ timeline.totalTime(frame / 30, false);
+ if (redraw) rerenderGsapTimelineAt(timeline, frame / 30);
+ scene.paused(true);
+ drawn.push(
+ [...line1.chars, line1.cursor, ...line2.chars, line2.cursor, container].map(
+ (o) => o.opacity,
+ ),
+ );
+ }
+ return drawn;
+ };
+ expect(frames(true)).toEqual(frames(false));
+ });
+
+ it("keeps the length of a timeline that grew after its last render", () => {
+ const timeline = gsap.timeline({ paused: true }).to({ y: 0 }, { y: 1, duration: 10 });
+ timeline.totalTime(1, true);
+ timeline.set({ x: 0 }, { x: 100 }, 20);
+ rerenderGsapTimelineAt(timeline, 1);
+ expect(timeline.duration()).toBe(20);
+ });
+});
diff --git a/packages/core/src/runtime/adapters/gsap.ts b/packages/core/src/runtime/adapters/gsap.ts
index 734c284396..15d9e46f7c 100644
--- a/packages/core/src/runtime/adapters/gsap.ts
+++ b/packages/core/src/runtime/adapters/gsap.ts
@@ -11,40 +11,82 @@ type GsapAdapterDeps = {
export function rerenderGsapTimelineAt(
timeline: {
totalTime: (time: number, suppressEvents?: boolean) => unknown;
+ totalDuration?: () => number;
getChildren?: RuntimeTimelineLike["getChildren"];
},
t: number,
): void {
+ timeline.totalDuration?.();
const children = timeline.getChildren?.(false, true, true) ?? [];
- const firedStates = callTweensIn(timeline).map(
- (call) => [call, call.ratio, call._zTime] as const,
+ const marked = childrenWithLandingMarksIn(timeline).map(
+ (child) => [child, child.ratio, child._zTime, child._act] as const,
);
- timeline.totalTime(t >= 0.001 ? t - 0.001 : t + 0.001, true);
- primeKeyframedTweensStartingAt(children, t);
- timeline.totalTime(t, true);
- for (const [call, ratio, zTime] of firedStates) Object.assign(call, { ratio, _zTime: zTime });
+ const skipped = childrenStartingAfter(children, t).map((child) => [child, child._ts] as const);
+ const parents = new Set<GsapParent>();
+ for (const [child] of skipped) {
+ for (let parent = child.parent; parent && !parents.has(parent); parent = parent.parent)
+ parents.add(parent);
+ }
+ const lengths = [...parents].map(
+ (parent) => [parent, parent._dur, parent._tDur, parent._end, parent._dirty] as const,
+ );
+ for (const [child] of skipped) child._ts = 0;
+ try {
+ timeline.totalTime(t >= 0.001 ? t - 0.001 : t + 0.001, true);
+ primeKeyframedTweensStartingAt(children, t);
+ timeline.totalTime(t, true);
+ } finally {
+ for (const [child, timeScale] of skipped) child._ts = timeScale;
+ for (const [parent, dur, tDur, end, dirty] of lengths) {
+ Object.assign(parent, { _dur: dur, _tDur: tDur, _end: end, _dirty: dirty });
+ }
+ }
+ for (const [child, ratio, zTime, active] of marked) {
+ child.ratio = ratio;
+ child._zTime = zTime;
+ child._act = active;
+ }
}
-type GsapCallInternals = { ratio: number; _zTime?: number };
+type GsapParent = {
+ _dur: number;
+ _tDur: number;
+ _end: number;
+ _dirty: number;
+ parent?: GsapParent | null;
+};
+
+type GsapChild = Pick<GsapAnimation, "startTime" | "getChildren"> & {
+ _ts: number;
+ parent?: GsapParent | null;
+ endTime: () => number;
+ time: () => number;
+};
+
+const PLAYHEAD_FLOAT_NOISE = 1e-6;
+
+function childrenStartingAfter(
+ children: unknown[],
+ time: number,
+ found: GsapChild[] = [],
+): GsapChild[] {
+ for (const child of children as GsapChild[]) {
+ if (child.startTime() > time + PLAYHEAD_FLOAT_NOISE) found.push(child);
+ else if (child.getChildren && child.endTime() >= time)
+ childrenStartingAfter(child.getChildren(false, true, true), child.time(), found);
+ }
+ return found;
+}
-export const GSAP_CALLBACK_NAMES = [
- "onStart",
- "onUpdate",
- "onComplete",
- "onReverseComplete",
- "onRepeat",
-];
+type GsapLandingMarks = { ratio: number; _zTime?: number; _act?: number };
-function callTweensIn(timeline: {
+function childrenWithLandingMarksIn(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[];
+}): GsapLandingMarks[] {
+ return (timeline.getChildren?.(true, true, true) ?? []).filter((child) => {
+ const animation = child as { totalDuration?: () => number; getChildren?: unknown };
+ return typeof animation.getChildren === "function" || animation.totalDuration?.() === 0;
+ }) as unknown as GsapLandingMarks[];
}
type GsapAnimation = {
@@ -98,6 +140,7 @@ export function createGsapAdapter(deps: GsapAdapterDeps): RuntimeDeterministicAd
rerenderGsapTimelineAt(
{
totalTime: timeline.totalTime.bind(timeline),
+ totalDuration: timeline.totalDuration?.bind(timeline),
getChildren: timeline.getChildren?.bind(timeline),
},
safeTime,
diff --git a/packages/core/src/runtime/init.ts b/packages/core/src/runtime/init.ts
index 4b3716c33d..1f5095b9b2 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, GSAP_CALLBACK_NAMES, rerenderGsapTimelineAt } from "./adapters/gsap";
+import { createGsapAdapter, rerenderGsapTimelineAt } from "./adapters/gsap";
import { createAnimeJsAdapter } from "./adapters/animejs";
import { createLottieAdapter } from "./adapters/lottie";
import { createThreeAdapter } from "./adapters/three";
@@ -4174,6 +4174,14 @@ 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;
@@ -4248,11 +4256,10 @@ export function initSandboxRuntimeModular(): void {
// timeline's full extent so it holds the final computed frame instead.
// Adapters still receive the raw `t` (their media may run longer).
// totalDuration() includes repeats; Infinity (infinite repeat) → no clamp.
- const tlWithTotal = tl as RuntimeTimelineLike & { totalDuration?: () => number };
let tlSeekTime = t;
- if (typeof tlWithTotal.totalDuration === "function") {
+ if (typeof tl.totalDuration === "function") {
try {
- const total = Number(tlWithTotal.totalDuration());
+ const total = Number(tl.totalDuration());
if (Number.isFinite(total) && total > 0 && t > total) {
tlSeekTime = total;
}
@@ -4266,7 +4273,11 @@ export function initSandboxRuntimeModular(): void {
if (!suppressEvents && !hasZeroDurationCallbackTween(tl)) {
// The first seek is the only eventful one; the re-render only refreshes styles.
rerenderGsapTimelineAt(
- { totalTime: tl.totalTime.bind(tl), getChildren: tl.getChildren?.bind(tl) },
+ {
+ totalTime: tl.totalTime.bind(tl),
+ totalDuration: tl.totalDuration?.bind(tl),
+ getChildren: tl.getChildren?.bind(tl),
+ },
tlSeekTime,
);
}
diff --git a/packages/core/src/runtime/types.ts b/packages/core/src/runtime/types.ts
index 1c38578372..25a16a2b19 100644
--- a/packages/core/src/runtime/types.ts
+++ b/packages/core/src/runtime/types.ts
@@ -314,6 +314,7 @@ export type RuntimeTimelineLike = {
pause: () => void;
seek: (timeSeconds?: number, suppressEvents?: boolean) => unknown;
totalTime?: (timeSeconds?: number, suppressEvents?: boolean) => unknown;
+ totalDuration?: () => number;
progress?: (value?: number, suppressEvents?: boolean) => unknown;
time: () => number;
duration: () => number;