Skip to content

Commit 13d7b8d

Browse files
committed
fix(studio): reading an unplayed tween restores each layer's whole style and GSAP state
1 parent b87e8ee commit 13d7b8d

2 files changed

Lines changed: 27 additions & 23 deletions

File tree

‎packages/studio/src/hooks/gsapParsedTween.ts‎

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -101,19 +101,14 @@ export function withLiveTiming(anim: GsapAnimation, tween: ParsedTween | null):
101101
};
102102
}
103103

104-
const TRANSFORM = [
105-
...["x", "y", "z", "xPercent", "yPercent", "rotation", "rotationX", "rotationY"],
106-
...["skewX", "skewY", "scaleX", "scaleY", "transformPerspective"],
107-
];
108-
109104
/** GSAP parses a to() tween only when the playhead first passes it, and a soft reload resets that.
110105
* Play its timeline from the tween's start (so earlier tweens set its start value) to its end and
111-
* back, its channels cleared first; then put back every layer's live value the seek redrew. */
106+
* back, its channels cleared first; then restore every layer's inline style and GSAP cache. */
112107
function parseUnplayed(win: GsapWindow, element: Element, tween: ParsedTween, props: string[]) {
113108
const seek = !isParsed(tween) && seekable(win, tween);
114109
if (!seek) return;
115110
const { parent, gsap } = seek;
116-
const live = liveValues(gsap, tween, element, props);
111+
const before = layerStates(tween, element);
117112
const now = parent.time();
118113
const start = tween.startTime?.() ?? 0;
119114
gsap.set(element, { clearProps: props.join(",") });
@@ -124,7 +119,7 @@ function parseUnplayed(win: GsapWindow, element: Element, tween: ParsedTween, pr
124119
parent.seek(start + (tween.duration?.() ?? 0), true);
125120
} finally {
126121
parent.seek(now, true);
127-
putBack(gsap, live);
122+
restore(before);
128123
}
129124
}
130125

@@ -148,23 +143,26 @@ function seekable(win: GsapWindow, tween: ParsedTween) {
148143
};
149144
}
150145

151-
// The channels a gesture draws live with gsap.set, on any layer the seek may redraw.
152-
const DRAFT_CHANNELS = [...TRANSFORM, "width", "height"];
146+
type Layer = Element & { _gsap?: Record<string, unknown> };
153147

154-
function liveValues(gsap: GsapTools, tween: ParsedTween, element: Element, props: string[]) {
155-
const layers = new Set<Element>([element]);
148+
function layerStates(tween: ParsedTween, element: Element) {
149+
const layers = new Set<Layer>([element]);
156150
for (const child of tween.parent?.getChildren?.(true, true, false) ?? [])
157151
for (const target of child.targets?.() ?? []) layers.add(target);
158-
return [...layers].map((layer) => {
159-
const channels = new Set([...DRAFT_CHANNELS, ...(layer === element ? props : [])]);
160-
return [layer, [...channels].map((p) => [p, gsap.getProperty(layer, p)] as const)] as const;
161-
});
152+
return [...layers].map((layer) => ({
153+
layer,
154+
style: layer.getAttribute("style"),
155+
cache: layer._gsap && { ...layer._gsap },
156+
}));
162157
}
163158

164-
function putBack(gsap: GsapTools, live: ReturnType<typeof liveValues>) {
165-
for (const [layer, values] of live) {
166-
const moved = values.filter(([p, v]) => v != null && gsap.getProperty(layer, p) !== v);
167-
if (moved.length > 0) gsap.set(layer, Object.fromEntries(moved));
159+
function restore(states: ReturnType<typeof layerStates>) {
160+
for (const { layer, style, cache } of states) {
161+
if (style === null) layer.removeAttribute("style");
162+
else layer.setAttribute("style", style);
163+
if (!cache || !layer._gsap) continue;
164+
for (const key of Object.keys(layer._gsap)) if (!(key in cache)) delete layer._gsap[key];
165+
Object.assign(layer._gsap, cache);
168166
}
169167
}
170168

‎packages/studio/src/hooks/gsapValueAtPlayhead.realGsap.test.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,19 @@ it("keeps a layer's other transform values while reading an unplayed tween", ()
128128
"keyframes: { '0%': { opacity: 0 }, '100%': { opacity: 1, x: 300 } }, duration: 1",
129129
).replace("}, 0);", "}, 2);");
130130
const { timeline, iframe } = play(src, 0.5);
131-
gsap.set(box, { x: 77, skewX: 23, rotationX: 19, xPercent: 12 });
131+
gsap.set(box, {
132+
x: 77,
133+
skewX: 23,
134+
rotationX: 19,
135+
xPercent: 12,
136+
transformOrigin: "20px 30px 40px",
137+
});
132138

133139
findParsedTween(iframe, box, parseGsapScriptAcorn(src).animations[0]!);
134140
timeline.kill();
135141

136-
const values = ["x", "skewX", "rotationX", "xPercent"].map((p) => gsap.getProperty(box, p));
137-
expect(values).toEqual([77, 23, 19, 12]);
142+
const values = ["x", "skewX", "rotationX", "xPercent", "transformOrigin"];
143+
expect(values.map((p) => gsap.getProperty(box, p))).toEqual([77, 23, 19, 12, "20px 30px 40px"]);
138144
});
139145

140146
it("names only an ease GSAP built in, and refuses a custom function rather than guess", () => {

0 commit comments

Comments
 (0)