Skip to content

Commit 19a8d44

Browse files
fix(core): scenes stop resetting during playback when the root has no animation of its own (#5693)
1 parent 1714f7c commit 19a8d44

2 files changed

Lines changed: 41 additions & 3 deletions

File tree

‎packages/core/src/runtime/init.subCompositionPlacement.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,29 @@ describe("runtime sub-composition placement", () => {
199199
expect(window.__player?.getDuration()).toBe(9);
200200
});
201201

202+
it("keeps the scene in one root after a tween first renders, when the root's own timeline is empty", async () => {
203+
const runFrames = stubFrames();
204+
document.body.innerHTML =
205+
`<div data-composition-id="main" data-root="true" data-duration="10">` +
206+
`<div id="host" class="clip" data-composition-id="scene" data-start="1" data-duration="9">` +
207+
`<div id="s"></div></div></div>`;
208+
const scene = gsap
209+
.timeline({ paused: true })
210+
.fromTo("#s", { x: -100 }, { x: 0, duration: 0.5, ease: "none" }, 0.5)
211+
.to({}, { duration: 9 }, 0);
212+
const root = gsap.timeline({ paused: true });
213+
window.__timelines = { main: root, scene } as unknown as Record<string, RuntimeTimelineLike>;
214+
initSandboxRuntimeModular();
215+
window.__player?.seek(1.2);
216+
const bound = scene.parent;
217+
218+
window.__player?.seek(3);
219+
await runFrames(6);
220+
221+
expect(scene.parent).toBe(bound);
222+
expect(shownX()).toBe(0);
223+
});
224+
202225
it("binds a root timeline that registers after the runtime started on its children", async () => {
203226
const runFrames = stubFrames();
204227
loadWithoutRootTimeline(`<div id="r"></div>`);

‎packages/core/src/runtime/timelineDuration.ts‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
11
import { swallow } from "./diagnostics";
22
import type { RuntimeTimelineLike } from "./types";
33

4-
/** A registered timeline's finite duration(); a partial, throwing or non-numeric entry reads as null. */
4+
type ChildTimeline = {
5+
paused?: () => boolean;
6+
startTime?: () => number;
7+
totalDuration?: () => number;
8+
timeScale?: () => number;
9+
};
10+
11+
const pausedChildEnd = (child: ChildTimeline): number =>
12+
child.paused?.() === true
13+
? (child.startTime?.() ?? 0) +
14+
(child.totalDuration?.() ?? 0) / Math.abs(child.timeScale?.() || 1)
15+
: 0;
16+
17+
/** A timeline's finite length, counting paused child timelines; a partial or throwing entry reads as null. */
518
export function readTimelineDurationSeconds(
6-
timeline: Pick<RuntimeTimelineLike, "duration"> | null | undefined,
19+
timeline: Pick<RuntimeTimelineLike, "duration" | "getChildren"> | null | undefined,
720
): number | null {
821
try {
922
if (typeof timeline?.duration !== "function") return null;
10-
const seconds = Number(timeline.duration());
23+
const held: unknown = timeline.getChildren?.(false, false, true);
24+
const children = Array.isArray(held) ? (held as ChildTimeline[]) : [];
25+
const seconds = Math.max(Number(timeline.duration()), ...children.map(pausedChildEnd));
1126
return Number.isFinite(seconds) ? seconds : null;
1227
} catch (err) {
1328
swallow("runtime.timelineDuration", err);

0 commit comments

Comments
 (0)