Skip to content

Commit b41547c

Browse files
committed
perf(core): redraw a parked scene timeline once instead of every frame
1 parent de2758e commit b41547c

3 files changed

Lines changed: 46 additions & 3 deletions

File tree

‎packages/core/src/runtime/adapters/gsap.steps.test.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,41 @@ describe("seekGsapTimeline", () => {
549549
expect(film.word.style.visibility).toBe("hidden");
550550
});
551551

552+
it("redraws a timeline parked at one time once, not on every seek", () => {
553+
const film = hiddenAtZero();
554+
const real = film.timeline;
555+
let moves = 0;
556+
const counted = {
557+
...real,
558+
time: () => real.time(),
559+
totalTime: (time?: number, suppressEvents?: boolean) => {
560+
if (time === undefined) return real.totalTime!();
561+
moves++;
562+
return real.totalTime!(time, suppressEvents);
563+
},
564+
getChildren: real.getChildren!.bind(real),
565+
} as RuntimeTimelineLike;
566+
seekGsapTimeline(counted, 0, false);
567+
const firstSeekMoves = moves;
568+
seekGsapTimeline(counted, 0, false);
569+
seekGsapTimeline(counted, 0, false);
570+
expect(firstSeekMoves).toBeGreaterThan(1);
571+
expect(moves - firstSeekMoves).toBe(2);
572+
});
573+
574+
it("redraws again after the timeline is cleared and rebuilt", () => {
575+
const film = hiddenAtZero();
576+
seekGsapTimeline(film.timeline, 0, false);
577+
const rebuilt = document.body.appendChild(document.createElement("span"));
578+
const gsapTimeline = film.timeline as unknown as gsap.core.Timeline;
579+
gsapTimeline
580+
.clear()
581+
.set(rebuilt, { visibility: "hidden" }, 0)
582+
.set(rebuilt, { visibility: "visible" }, 0.5);
583+
seekGsapTimeline(film.timeline, 0, false);
584+
expect(rebuilt.style.visibility).toBe("hidden");
585+
});
586+
552587
it("fires a call at that time once across the next seeks", () => {
553588
const film = hiddenAtZero();
554589
seekGsapTimeline(film.timeline, 0, false);

‎packages/core/src/runtime/adapters/gsap.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ export function rerenderGsapTimelineAt(
4949
}
5050
}
5151

52+
const childrenAtLastInPlaceRedraw = new WeakMap<RuntimeTimelineLike, unknown[]>();
53+
5254
export function seekGsapTimeline(
5355
timeline: RuntimeTimelineLike,
5456
t: number,
@@ -58,9 +60,15 @@ export function seekGsapTimeline(
5860
timeline.seek(t, suppressEvents);
5961
return false;
6062
}
61-
const gsapSkipsRenderInPlace = timeline.time() === t;
63+
const gsapSkipsRenderInPlace = Number(timeline.totalTime()) === t;
6264
timeline.totalTime(t, suppressEvents);
6365
if (!gsapSkipsRenderInPlace) return false;
66+
const children = timeline.getChildren?.(false, true, true) ?? [];
67+
const redrawn = childrenAtLastInPlaceRedraw.get(timeline);
68+
const unchangedSinceRedraw =
69+
redrawn?.length === children.length && children.every((child, i) => child === redrawn[i]);
70+
if (unchangedSinceRedraw) return false;
71+
childrenAtLastInPlaceRedraw.set(timeline, children);
6472
rerenderGsapTimelineAt(
6573
{
6674
totalTime: timeline.totalTime.bind(timeline),

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3119,7 +3119,7 @@ describe("initSandboxRuntimeModular", () => {
31193119
const rootTimeline = createMockTimeline(10);
31203120
const originalTotalTime = rootTimeline.totalTime;
31213121
rootTimeline.totalTime = (time: number, suppressEvents?: boolean) => {
3122-
seekCalls.push({ time, suppressEvents });
3122+
if (time !== undefined) seekCalls.push({ time, suppressEvents });
31233123
return originalTotalTime?.(time, suppressEvents);
31243124
};
31253125

@@ -3155,7 +3155,7 @@ describe("initSandboxRuntimeModular", () => {
31553155
const rootTimeline = createMockTimeline(10);
31563156
const originalTotalTime = rootTimeline.totalTime;
31573157
rootTimeline.totalTime = (time: number, suppressEvents?: boolean) => {
3158-
seekCalls.push({ time, suppressEvents });
3158+
if (time !== undefined) seekCalls.push({ time, suppressEvents });
31593159
return originalTotalTime?.(time, suppressEvents);
31603160
};
31613161
Object.assign(rootTimeline, {

0 commit comments

Comments
 (0)