From 30962b9ac786b99d554269fa85b11b19174797a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Mon, 30 Mar 2026 20:06:16 +0200 Subject: [PATCH] fix(runtime): extend GSAP timeline in-place instead of creating composite MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit createDurationFloorTimeline appended a tween AFTER the existing timeline, making it originalDuration + declaredDuration (e.g. 4.6+5.6= 10.2s) instead of just declaredDuration (5.6s). This caused regression tests to render the wrong number of frames. Now extends the original timeline in-place by placing a zero-duration no-op tween at the declared end position — GSAP then reports the correct duration with no composite overhead. Also adds a 0.5s minimum gap guard to avoid floating-point false positives on compositions where the GSAP timeline is already close to data-duration. --- packages/core/src/runtime/init.ts | 36 ++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/packages/core/src/runtime/init.ts b/packages/core/src/runtime/init.ts index 97b19296e8..dd83510a10 100644 --- a/packages/core/src/runtime/init.ts +++ b/packages/core/src/runtime/init.ts @@ -707,24 +707,40 @@ export function initSandboxRuntimeModular(): void { }; } } - // If the root composition declares an explicit data-duration that exceeds - // the captured GSAP timeline, pad the timeline so playback runs to the - // declared end rather than stopping when GSAP runs out. + // If the root composition declares an explicit data-duration that meaningfully + // exceeds the captured GSAP timeline, extend the timeline in-place by placing + // a zero-duration no-op tween at the declared end position. This makes + // timeline.duration() report the declared length without creating a composite + // (which would double-count the original duration). const rootDeclaredDurAttr = rootCompositionNode?.getAttribute("data-duration"); if (rootDeclaredDurAttr) { const rootDeclaredDur = parseFloat(rootDeclaredDurAttr); if ( isUsableTimelineDuration(rootDeclaredDur) && isUsableTimelineDuration(rootDurationSeconds) && - rootDeclaredDur > rootDurationSeconds + // Only pad when the gap is meaningful (>= 0.5s) to avoid floating-point + // false positives on compositions whose GSAP duration is already close + // to data-duration. + rootDeclaredDur >= rootDurationSeconds + 0.5 ) { - const paddedTimeline = createDurationFloorTimeline(rootDeclaredDur, rootTimeline); - const paddedDur = getTimelineDurationSeconds(paddedTimeline); - if (paddedTimeline && isUsableTimelineDuration(paddedDur)) { + const tlWithTo = rootTimeline as RuntimeTimelineLike & { + to?: (target: object, vars: { duration: number }, position: number) => unknown; + }; + if (typeof tlWithTo.to === "function") { + try { + // Placing a zero-duration tween AT rootDeclaredDur extends + // timeline.duration() to exactly rootDeclaredDur. + tlWithTo.to({}, { duration: 0 }, rootDeclaredDur); + } catch { + // keep runtime resilient + } + } + const newDur = getTimelineDurationSeconds(rootTimeline); + if (isUsableTimelineDuration(newDur)) { return { - timeline: paddedTimeline, + timeline: rootTimeline, selectedTimelineIds: [rootCompositionId], - selectedDurationSeconds: paddedDur, + selectedDurationSeconds: newDur, mediaDurationFloorSeconds, diagnostics: { code: "root_timeline_padded_to_declared_duration", @@ -732,7 +748,7 @@ export function initSandboxRuntimeModular(): void { rootCompositionId, rootDurationSeconds, rootDeclaredDur, - paddedDur, + newDur, }, }, };