Skip to content

Commit fff3636

Browse files
fix(core): exported scenes keep cards that fly in, instead of one frame of each (#5692)
* fix(core): exported scenes keep cards that fly in, instead of one frame of each Under the producer's GSAP stub every timeline is a stand-in. GSAP's bundle sets window.gsap twice, so the stub wrapped twice; and the runtime compared those stand-ins against GSAP's real children. Nesting a scene into the root never read back as nested, so the scene ended up parked under an abandoned composite at time 0, and each render seek's play() snapped it back to 0. Tweens whose from-values set opacity lost them after their first frame. The stub now wraps a GSAP instance once, and the runtime compares the timeline behind a stand-in. * fix(core): keep the stand-in note to one line
1 parent 4e6fcdb commit fff3636

5 files changed

Lines changed: 104 additions & 7 deletions

File tree

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

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,3 +399,64 @@ describe("runtime sub-composition placement", () => {
399399
expect(video("fade").volume).toBeCloseTo(0.5, 6);
400400
});
401401
});
402+
403+
// The producer's tween-batching stub registers stand-ins: the same methods on another object, the timeline behind it.
404+
function standIn(real: gsap.core.Timeline): RuntimeTimelineLike {
405+
const proxy: Record<string, unknown> = { __hfIsProxy: true, __hfReal: real };
406+
const unwrap = (arg: unknown) => (arg as { __hfReal?: unknown } | null)?.__hfReal ?? arg;
407+
for (let o: object | null = real; o && o !== Object.prototype; o = Object.getPrototypeOf(o)) {
408+
for (const key of Object.getOwnPropertyNames(o)) {
409+
const method = (real as unknown as Record<string, unknown>)[key];
410+
if (key === "constructor" || key in proxy || typeof method !== "function") continue;
411+
proxy[key] = (...args: unknown[]) => {
412+
const out = (method as (...a: unknown[]) => unknown).apply(real, args.map(unwrap));
413+
return out === real ? proxy : out;
414+
};
415+
}
416+
}
417+
return proxy as unknown as RuntimeTimelineLike;
418+
}
419+
420+
describe("runtime placement of timelines registered as stand-ins", () => {
421+
beforeEach(() => {
422+
resetRuntimeFixtureDom();
423+
window.gsap = gsap as unknown as typeof window.gsap;
424+
});
425+
426+
afterEach(() => {
427+
window.__hfRuntimeTeardown?.();
428+
vi.restoreAllMocks();
429+
delete window.__player;
430+
delete window.__playerReady;
431+
delete window.__hf;
432+
});
433+
434+
// A card flies in at 0.5 s, its from-vars showing it, hidden from 0 by a fromTo authored after it.
435+
function cardScene(card: Element | object) {
436+
const scene = gsap.timeline({ paused: true });
437+
scene.fromTo(card, { x: -600, opacity: 1 }, { x: 0, duration: 0.34 }, 0.5);
438+
scene.fromTo([card], { opacity: 0 }, { opacity: 0, duration: 0.01 }, 0);
439+
return scene;
440+
}
441+
442+
it("keeps a scene's fly-in on every rendered frame under an empty root, as playback does", () => {
443+
document.body.innerHTML =
444+
`<div data-composition-id="main" data-root="true" data-duration="2">` +
445+
`<div class="clip" data-composition-id="scene" data-start="0" data-duration="2">` +
446+
`<div id="card"></div></div></div>`;
447+
const card = document.getElementById("card")!;
448+
const scene = cardScene(card);
449+
window.__timelines = {
450+
main: standIn(gsap.timeline({ paused: true })),
451+
scene: standIn(scene),
452+
};
453+
initSandboxRuntimeModular();
454+
const played = { x: 0, opacity: 0 };
455+
const playedScene = cardScene(played);
456+
for (let frame = 0; frame <= 30; frame++) {
457+
playedScene.totalTime(frame / 30);
458+
window.__player?.renderSeek(frame / 30, { suppressEvents: true });
459+
expect(card.style.opacity, `frame ${frame}`).toBe(String(played.opacity));
460+
}
461+
});
462+
});

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,6 +1416,9 @@ export function initSandboxRuntimeModular(): void {
14161416

14171417
// Sub-composition timelines the runtime nested into the root: the host composition id, and where.
14181418
const autoNested = new WeakMap<object, { hostId: string; parent: unknown; at: number }>();
1419+
// The producer's GSAP stub registers stand-ins; GSAP's tree holds the timelines behind them.
1420+
const gsapTimelineOf = (timeline: object): object =>
1421+
(timeline as { __hfReal?: object }).__hfReal ?? timeline;
14191422
// The composite that holds the children of a root with no timeline, and how many it holds.
14201423
let missingRootComposite: { timeline: RuntimeTimelineLike; size: number } | null = null;
14211424

@@ -1471,16 +1474,20 @@ export function initSandboxRuntimeModular(): void {
14711474
): void => {
14721475
const at = resolveCompositionStartSeconds(candidate.compositionId);
14731476
parent.add(candidate.timeline, at);
1474-
autoNested.set(candidate.timeline, { hostId: candidate.compositionId, parent, at });
1477+
autoNested.set(gsapTimelineOf(candidate.timeline), {
1478+
hostId: candidate.compositionId,
1479+
parent,
1480+
at,
1481+
});
14751482
};
14761483
/** Placed by the runtime in `root` and still there; under another root a re-run script owns its place. */
14771484
const placedIn = (
14781485
root: RuntimeTimelineLike,
14791486
candidate: { compositionId: string; timeline: RuntimeTimelineLike },
14801487
) => {
1481-
const nested = candidate.timeline as RuntimeTimelineLike & RuntimeTimelineChildLike;
1488+
const nested = gsapTimelineOf(candidate.timeline) as RuntimeTimelineChildLike;
14821489
const placed = autoNested.get(nested);
1483-
return placed && placed.parent === root && nested.parent === (root as unknown)
1490+
return placed && placed.parent === root && nested.parent === gsapTimelineOf(root)
14841491
? placed
14851492
: null;
14861493
};
@@ -1551,7 +1558,9 @@ export function initSandboxRuntimeModular(): void {
15511558
if (!parent || typeof parent.getChildren !== "function") return [];
15521559
try {
15531560
const held = parent.getChildren(true, true, true);
1554-
return Array.isArray(held) ? candidates.filter((c) => held.includes(c.timeline)) : [];
1561+
return Array.isArray(held)
1562+
? candidates.filter((c) => held.includes(gsapTimelineOf(c.timeline)))
1563+
: [];
15551564
} catch {
15561565
return [];
15571566
}
@@ -1574,7 +1583,7 @@ export function initSandboxRuntimeModular(): void {
15741583
if (!Array.isArray(existingChildren)) return none;
15751584
const addedIds: string[] = [];
15761585
for (const candidate of candidates) {
1577-
if (existingChildren.includes(candidate.timeline)) {
1586+
if (existingChildren.includes(gsapTimelineOf(candidate.timeline))) {
15781587
followHostStart(rootTimeline, candidate);
15791588
continue;
15801589
}

‎packages/producer/src/generated/hf-early-stub-inline.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// AUTO-GENERATED by scripts/build-hf-early-stub.ts — do not edit
22
const HF_EARLY_STUB_IIFE: string =
3-
'"use strict";(()=>{var P=100,x=[],s=[],w=!1,f=!1,T=new Set;function p(n){let e=window.__HF_VIRTUAL_TIME__?.originalRequestAnimationFrame;return typeof e=="function"?e(n):requestAnimationFrame(n)}function b(n){let e=window.__HF_VIRTUAL_TIME__?.originalSetTimeout;if(typeof e=="function"){e(n,0);return}setTimeout(n,0)}function O(n){return n!==null&&typeof n=="object"&&"__hfIsProxy"in n?n.__hfReal:n}function m(n,e){n!=="add"&&v(e)}function h(n){if(n===null||typeof n!="object"||Array.isArray(n))return!1;let e=n;return"rotationX"in e||"rotationY"in e||"transformPerspective"in e}var d=new Set;function v(n){let e=n[0];if(e==null)return;let o=window;d.has(e)||(d.add(e),o.__hfAllTweenTargets=Array.from(d)),(h(n[1])||h(n[2]))&&(T.add(e),o.__hf3dTweenTargets=Array.from(T))}function _(n){let e=n.proxy.__hfReal,o=e[n.method];if(typeof o=="function"){let i=n.method==="add"?n.args.map(O):n.args;o.call(e,...i)}}function l(n,e,o){m(e,o);let i={proxy:n,method:e,args:o};return n.__hfQueue.push(i),s.push(i),R(),n}function y(n){let e=n.proxy.__hfQueue.indexOf(n);e>=0&&n.proxy.__hfQueue.splice(e,1)}function t(){for(;s.length>0;){let n=s.shift();n&&(y(n),_(n))}A()}function k(){f=!1,window.__hfTimelinesBuilding=!1;try{window.dispatchEvent(new CustomEvent("hf-timelines-built"))}catch{}}function A(){f||(f=!0,b(()=>{s.length===0?k():f=!1}))}function g(){w=!1;let n=s.splice(0,P);for(let e of n)y(e),_(e);s.length>0?(w=!0,p(g)):k()}function R(){w||(w=!0,window.__hfTimelinesBuilding=!0,p(g))}var S=new Set(["to","from","fromTo","set","add"]);function I(n,e){let o=e;for(;o!==null&&o!==Object.prototype;){for(let i of Object.getOwnPropertyNames(o)){if(i==="constructor"||i==="then"||i in n||S.has(i)||i.charAt(0)==="_")continue;let u=Object.getOwnPropertyDescriptor(o,i);if(!u||typeof u.value!="function")continue;let r=u.value;n[i]=function(...c){t();let a=r.call(e,...c);return a===e?n:a}}o=Object.getPrototypeOf(o)}}function G(n){let e={__hfReal:n,__hfQueue:[],__hfIsProxy:!0,to(...o){return l(e,"to",o)},from(...o){return l(e,"from",o)},fromTo(...o){return l(e,"fromTo",o)},set(...o){return l(e,"set",o)},add(...o){return l(e,"add",o)},pause(...o){return t(),n.pause(...o),e},play(...o){return t(),n.play(...o),e},seek(...o){return t(),n.seek(...o),e},totalTime(...o){return t(),o.length>0?(n.totalTime(...o),e):n.totalTime()},time(...o){return t(),o.length>0?(n.time(...o),e):n.time()},duration(...o){return t(),o.length>0?(n.duration(...o),e):n.duration()},getChildren(...o){t();let i=n.getChildren(...o);return Array.isArray(i)?i:[]},paused(...o){return t(),o.length>0?(n.paused(...o),e):n.paused()},timeScale(...o){return t(),o.length>0?(n.timeScale(...o),e):n.timeScale()},kill(){t(),n.kill()}};return I(e,n),x.push(e),e}if(typeof window<"u"){window.__hf||(window.__hf={}),window.__hfTimelinesBuilding=!1,window.__hfFlushSync=()=>{t(),s.length===0&&window.__hfTimelinesBuilding&&k()};let n=null;try{Object.defineProperty(window,"gsap",{configurable:!0,enumerable:!0,get(){return n},set(e){if(n=e,!e||typeof e.timeline!="function")return;let o=e.timeline.bind(e);e.timeline=u=>G(o(u));for(let u of["to","from","set"]){let r=e[u];if(typeof r!="function")continue;let c=r.bind(e);e[u]=(...a)=>(m(u,a),c(...a))}let i=e.fromTo;if(typeof i=="function"){let u=i.bind(e);e.fromTo=(...r)=>(m("fromTo",r),u(...r))}}})}catch{}}})();\n';
3+
'"use strict";(()=>{var x=100,b=[],s=[],c=!1,w=!1,h=new Set;function _(n){let o=window.__HF_VIRTUAL_TIME__?.originalRequestAnimationFrame;return typeof o=="function"?o(n):requestAnimationFrame(n)}function O(n){let o=window.__HF_VIRTUAL_TIME__?.originalSetTimeout;if(typeof o=="function"){o(n,0);return}setTimeout(n,0)}function v(n){return n!==null&&typeof n=="object"&&"__hfIsProxy"in n?n.__hfReal:n}function m(n,o){n!=="add"&&A(o)}function p(n){if(n===null||typeof n!="object"||Array.isArray(n))return!1;let o=n;return"rotationX"in o||"rotationY"in o||"transformPerspective"in o}var d=new Set;function A(n){let o=n[0];if(o==null)return;let e=window;d.has(o)||(d.add(o),e.__hfAllTweenTargets=Array.from(d)),(p(n[1])||p(n[2]))&&(h.add(o),e.__hf3dTweenTargets=Array.from(h))}function y(n){let o=n.proxy.__hfReal,e=o[n.method];if(typeof e=="function"){let i=n.method==="add"?n.args.map(v):n.args;e.call(o,...i)}}function l(n,o,e){m(o,e);let i={proxy:n,method:o,args:e};return n.__hfQueue.push(i),s.push(i),R(),n}function g(n){let o=n.proxy.__hfQueue.indexOf(n);o>=0&&n.proxy.__hfQueue.splice(o,1)}function t(){for(;s.length>0;){let n=s.shift();n&&(g(n),y(n))}S()}function k(){w=!1,window.__hfTimelinesBuilding=!1;try{window.dispatchEvent(new CustomEvent("hf-timelines-built"))}catch{}}function S(){w||(w=!0,O(()=>{s.length===0?k():w=!1}))}function P(){c=!1;let n=s.splice(0,x);for(let o of n)g(o),y(o);s.length>0?(c=!0,_(P)):k()}function R(){c||(c=!0,window.__hfTimelinesBuilding=!0,_(P))}var I=new Set(["to","from","fromTo","set","add"]);function G(n,o){let e=o;for(;e!==null&&e!==Object.prototype;){for(let i of Object.getOwnPropertyNames(e)){if(i==="constructor"||i==="then"||i in n||I.has(i)||i.charAt(0)==="_")continue;let a=Object.getOwnPropertyDescriptor(e,i);if(!a||typeof a.value!="function")continue;let u=a.value;n[i]=function(...r){t();let f=u.call(o,...r);return f===o?n:f}}e=Object.getPrototypeOf(e)}}function B(n){let o={__hfReal:n,__hfQueue:[],__hfIsProxy:!0,to(...e){return l(o,"to",e)},from(...e){return l(o,"from",e)},fromTo(...e){return l(o,"fromTo",e)},set(...e){return l(o,"set",e)},add(...e){return l(o,"add",e)},pause(...e){return t(),n.pause(...e),o},play(...e){return t(),n.play(...e),o},seek(...e){return t(),n.seek(...e),o},totalTime(...e){return t(),e.length>0?(n.totalTime(...e),o):n.totalTime()},time(...e){return t(),e.length>0?(n.time(...e),o):n.time()},duration(...e){return t(),e.length>0?(n.duration(...e),o):n.duration()},getChildren(...e){t();let i=n.getChildren(...e);return Array.isArray(i)?i:[]},paused(...e){return t(),e.length>0?(n.paused(...e),o):n.paused()},timeScale(...e){return t(),e.length>0?(n.timeScale(...e),o):n.timeScale()},kill(){t(),n.kill()}};return G(o,n),b.push(o),o}if(typeof window<"u"){window.__hf||(window.__hf={}),window.__hfTimelinesBuilding=!1,window.__hfFlushSync=()=>{t(),s.length===0&&window.__hfTimelinesBuilding&&k()};let n=null,o=new WeakSet;try{Object.defineProperty(window,"gsap",{configurable:!0,enumerable:!0,get(){return n},set(e){if(n=e,!e||typeof e.timeline!="function"||o.has(e))return;o.add(e);let i=e.timeline.bind(e);e.timeline=u=>B(i(u));for(let u of["to","from","set"]){let r=e[u];if(typeof r!="function")continue;let f=r.bind(e);e[u]=(...T)=>(m(u,T),f(...T))}let a=e.fromTo;if(typeof a=="function"){let u=a.bind(e);e.fromTo=(...r)=>(m("fromTo",r),u(...r))}}})}catch{}}})();\n';
44

55
/**
66
* Returns the pre-built HyperFrames early stub IIFE as a string constant.

‎packages/producer/src/services/fileServer.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,4 +1028,27 @@ describe("HF_EARLY_STUB + HF_BRIDGE_SCRIPT integration", () => {
10281028

10291029
expect(sandbox.window.__hf?.duration).toBe(13);
10301030
});
1031+
1032+
it("stands in for the GSAP timeline itself when the GSAP bundle sets window.gsap twice", () => {
1033+
class FakeTimeline {
1034+
parent: FakeTimeline | null = null;
1035+
add(child: FakeTimeline) {
1036+
child.parent = this;
1037+
return this;
1038+
}
1039+
}
1040+
const gsap = { timeline: () => new FakeTimeline() };
1041+
const win: Record<string, unknown> = {
1042+
__HF_VIRTUAL_TIME__: { originalRequestAnimationFrame: () => 0, originalSetTimeout: () => {} },
1043+
};
1044+
new Function("window", HF_EARLY_STUB)(win);
1045+
win.gsap = gsap;
1046+
win.gsap = gsap;
1047+
const parent = gsap.timeline() as unknown as { __hfReal: unknown; add: (c: unknown) => void };
1048+
const child = gsap.timeline() as unknown as { __hfReal: FakeTimeline };
1049+
parent.add(child);
1050+
(win.__hfFlushSync as () => void)();
1051+
expect(child.__hfReal).toBeInstanceOf(FakeTimeline);
1052+
expect(child.__hfReal.parent).toBe(parent.__hfReal);
1053+
});
10311054
});

‎packages/producer/stubs/hf-early-stub.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,9 @@ if (typeof window !== "undefined") {
463463
// `gsap.timeline()` before any user script calls it. GSAP is not yet
464464
// loaded when this stub runs — it loads via a <script> tag in the HTML body.
465465
let _realGsap: GsapInstance | null = null;
466+
// GSAP's browser bundle assigns window.gsap twice: wrapping again would stand a proxy in for a
467+
// proxy, and the runtime would compare GSAP's children against the outer one.
468+
const wrapped = new WeakSet<GsapInstance>();
466469
try {
467470
Object.defineProperty(window, "gsap", {
468471
configurable: true,
@@ -472,7 +475,8 @@ if (typeof window !== "undefined") {
472475
},
473476
set(g: GsapInstance): void {
474477
_realGsap = g;
475-
if (!g || typeof g.timeline !== "function") return;
478+
if (!g || typeof g.timeline !== "function" || wrapped.has(g)) return;
479+
wrapped.add(g);
476480
const origTimeline = g.timeline.bind(g) as (params?: unknown) => GsapTimeline;
477481
g.timeline = (params?: unknown): GsapTimeline => wrapTimeline(origTimeline(params));
478482
// Tween-target tracking for top-level gsap.to/from/set/fromTo calls

0 commit comments

Comments
 (0)