Skip to content

Commit c4440df

Browse files
perf(core): scenes stop animating after their clip ends, so late playback keeps its frame rate (#5741)
* perf(core): scenes stop animating after their clip ends, so late playback keeps its frame rate A scene timeline registered on its own was re-seeked every frame up to its own length, not its clip's end. A scene whose animation outlasts its clip kept re-rendering while hidden; the runtime now rests it at the clip's end. * perf(core): name the clip end instead of commenting it * fix(core): rest only timed scenes in preview, leave render seeks as before * fix(core): a Studio-stamped scene keeps animating after the root grows past its stamp
1 parent 5e7ba0b commit c4440df

2 files changed

Lines changed: 111 additions & 3 deletions

File tree

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

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

202+
it("rests a scene's longer animation at its clip's end, where the clip hides", () => {
203+
document.body.innerHTML =
204+
`<div data-composition-id="main" data-root="true" data-duration="10">` +
205+
`<div id="host" class="clip" data-composition-id="scene" data-start="1" data-duration="2">` +
206+
`<div id="s"></div></div></div>`;
207+
let renders = 0;
208+
const scene = gsap
209+
.timeline({ paused: true })
210+
.to("#s", { x: 100, duration: 8, ease: "none", onUpdate: () => renders++ }, 0);
211+
const root = gsap.timeline({ paused: true }).to({}, { duration: 10 }, 0);
212+
window.__timelines = { main: root, scene } as unknown as Record<string, RuntimeTimelineLike>;
213+
initSandboxRuntimeModular();
214+
215+
window.__player?.seek(2);
216+
expect(shownX()).toBe(12.5);
217+
window.__player?.seek(5);
218+
expect(shownX()).toBe(25);
219+
expect(getComputedStyle(document.getElementById("host")!).visibility).toBe("hidden");
220+
renders = 0;
221+
window.__player?.seek(6);
222+
expect(renders).toBe(0);
223+
});
224+
225+
it("keeps a slow scene with no clip window animating while it shows", () => {
226+
document.body.innerHTML =
227+
`<div data-composition-id="main" data-root="true" data-duration="20">` +
228+
`<div id="host" data-composition-id="scene" data-playback-rate="0.5">` +
229+
`<div id="s"></div></div></div>`;
230+
const scene = gsap
231+
.timeline({ paused: true })
232+
.to("#s", { x: 100, duration: 8, ease: "none" }, 0);
233+
const root = gsap.timeline({ paused: true }).to({}, { duration: 20 }, 0);
234+
window.__timelines = { main: root, scene } as unknown as Record<string, RuntimeTimelineLike>;
235+
initSandboxRuntimeModular();
236+
237+
window.__player?.seek(6);
238+
window.__player?.seek(12);
239+
expect(shownX()).toBe(75);
240+
});
241+
242+
it("keeps a slow scene Studio stamped animating past its stamp, once the root has grown", () => {
243+
const parent = window.parent;
244+
Object.defineProperty(window, "parent", { configurable: true, value: {} });
245+
try {
246+
// What Studio leaves after a root extension: the host's discovery stamp still ends at 10 s.
247+
document.body.innerHTML =
248+
`<div data-composition-id="main" data-root="true" data-duration="20">` +
249+
`<div id="host" style="position: absolute" data-composition-id="scene" data-playback-rate="0.5"` +
250+
` data-start="0" data-duration="10" data-hf-autostamped="1"><div id="s"></div></div></div>`;
251+
const scene = gsap
252+
.timeline({ paused: true })
253+
.to("#s", { x: 100, duration: 8, ease: "none" }, 0);
254+
const root = gsap.timeline({ paused: true }).to({}, { duration: 20 }, 0);
255+
window.__timelines = { main: root, scene } as unknown as Record<string, RuntimeTimelineLike>;
256+
initSandboxRuntimeModular();
257+
258+
window.__player?.seek(6);
259+
window.__player?.seek(12);
260+
expect(shownX()).toBe(75);
261+
expect(getComputedStyle(document.getElementById("host")!).visibility).not.toBe("hidden");
262+
} finally {
263+
Object.defineProperty(window, "parent", { configurable: true, value: parent });
264+
}
265+
});
266+
267+
it("renders a scene past its clip's end where root propagation leaves it, replaying no callbacks", () => {
268+
document.body.innerHTML =
269+
`<div data-composition-id="main" data-root="true" data-duration="10">` +
270+
`<div id="host" class="clip" data-composition-id="scene" data-start="1" data-duration="2">` +
271+
`<div id="s"></div></div></div>`;
272+
let callbacks = 0;
273+
const scene = gsap.timeline({ paused: true }).to(
274+
"#s",
275+
{
276+
x: 100,
277+
duration: 8,
278+
ease: "none",
279+
onUpdate: () => callbacks++,
280+
onComplete: () => callbacks++,
281+
},
282+
0,
283+
);
284+
const root = gsap.timeline({ paused: true }).to({}, { duration: 10 }, 0);
285+
window.__timelines = { main: root, scene } as unknown as Record<string, RuntimeTimelineLike>;
286+
initSandboxRuntimeModular();
287+
288+
window.__player?.renderSeek(9.1);
289+
expect(shownX()).toBe(100);
290+
callbacks = 0;
291+
window.__player?.renderSeek(9.2);
292+
window.__player?.renderSeek(9.3, { suppressEvents: true });
293+
expect(callbacks).toBe(0);
294+
});
295+
202296
it("keeps the scene in one root after a tween first renders, when the root's own timeline is empty", async () => {
203297
const runFrames = stubFrames();
204298
document.body.innerHTML =

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,9 @@ function loadPreviewImagesIn(root: Element): void {
433433
export function initSandboxRuntimeModular(): void {
434434
const state = createRuntimeState();
435435
const stampsForStudio = window.parent !== window;
436+
// Studio's discovery stamps time a clip for the timeline only; nothing hides it at their end.
437+
const isDiscoveryStamped = (el: Element) =>
438+
stampsForStudio && el.hasAttribute(RUNTIME_STAMPED_ATTR);
436439
authoredMediaObserver?.disconnect();
437440
authoredMediaObserver = null;
438441
if (readSceneParts(document)) recordAuthoredMedia(document.documentElement);
@@ -2959,7 +2962,7 @@ export function initSandboxRuntimeModular(): void {
29592962
visibleAt(rawNode, currentTime) &&
29602963
clipChainVisibleAt(rawNode.parentElement, currentTime, visibleAt, rootComp);
29612964
visibilityDecided.set(rawNode, isVisibleNow);
2962-
const isRuntimeStamped = stampsForStudio && rawNode.hasAttribute(RUNTIME_STAMPED_ATTR);
2965+
const isRuntimeStamped = isDiscoveryStamped(rawNode);
29632966
if (!isRuntimeStamped) rawNode.style.visibility = isVisibleNow ? "visible" : "hidden";
29642967
rawNode.toggleAttribute(CLIP_HIDDEN_ATTR, !isRuntimeStamped && !isVisibleNow);
29652968
const upcoming =
@@ -4425,7 +4428,10 @@ export function initSandboxRuntimeModular(): void {
44254428
standaloneHosts.set(compositionId, node ?? { missingAt: compositionTimingRevision });
44264429
return node;
44274430
};
4428-
const seekStandaloneRegisteredTimelines = (timeSeconds: number, options?: RuntimeSeekOptions) => {
4431+
const seekStandaloneRegisteredTimelines = (
4432+
timeSeconds: number,
4433+
options?: RuntimeSeekOptions & { activateChildren?: boolean },
4434+
) => {
44294435
const timelines = (window.__timelines ?? {}) as Record<string, RuntimeTimelineLike | undefined>;
44304436
const rootCompositionId =
44314437
resolveRootCompositionElement()?.getAttribute("data-composition-id") ?? null;
@@ -4435,10 +4441,18 @@ export function initSandboxRuntimeModular(): void {
44354441
if (!node) continue;
44364442
const start = resolveStartForElement(node, 0);
44374443
if (!Number.isFinite(start)) continue;
4444+
// Rest only where the visibility pass hides the host and no rearmed root propagation moves it.
4445+
const hiddenFrom =
4446+
!options?.activateChildren && node.hasAttribute("data-start") && !isDiscoveryStamped(node)
4447+
? start + (resolveDurationForElement(node) ?? Infinity)
4448+
: Infinity;
44384449
const timelineDuration = getTimelineDurationSeconds(timeline);
44394450
const sourceTime =
44404451
readElementPlaybackStart(node) +
4441-
sourceTimeAt(readElementRateSpec(node), Math.max(0, timeSeconds - start));
4452+
sourceTimeAt(
4453+
readElementRateSpec(node),
4454+
Math.max(0, Math.min(timeSeconds, hiddenFrom) - start),
4455+
);
44424456
const localTime = Math.max(
44434457
0,
44444458
timelineDuration != null && timelineDuration > 0

0 commit comments

Comments
 (0)