Skip to content

Commit 0d5e395

Browse files
fix(studio): keyframed animations keep their ease through every Studio edit (#5051)
* fix(parsers): dragging a keyframe keeps the keyframes' ease * fix(parsers): splitting a keyframed tween into groups keeps its eases * fix(studio): keyframed tweens keep their eases when Studio rewrites them Extending a keyframed tween to the playhead and merging a gesture recording into it now carry the tween's eases and each keyframe's own ease. The parsers no longer report a tween-level easeEach, which GSAP ignores, as the keyframes' ease. * fix(studio): a gesture merged into a tween writes rounded keyframe percentages * fix(studio): a gesture merged into an eased tween plays at the times it was recorded Merged keyframes are placed through the tween's run ease, as the extend path already does, and recorded constant-speed segments stay linear. * fix(studio): the ease card describes a keyframed tween by its keyframes and eases The summary line read 'no properties yet using a none curve' for every keyframed tween. It now names the keyframed properties, the keyframe count, the segment ease and the run ease that GSAP plays. * fix(studio): a gesture merged over a strong ease-out keeps its keyframes and the ones before it The merge keeps or replaces old keyframes by when they play, and rounds percentages only when the run ease does not warp time. * fix(studio): the ease card labels an unset segment with the ease GSAP plays * fix(studio): the ease card header shows the segment ease its rows play The header shows the one ease every segment plays, or Mixed, and a run ease other than none gets its own line. * fix(parsers): keyframe percentages are written as decimal keys the parsers read back A percentage below 1e-6 printed as '1.28e-7%', which both parsers reject. One plainPercentKey owner now writes every percentage key in both writers and the unroller. * fix(studio): the ease card's segment rows show rounded percentages
1 parent 6c353d8 commit 0d5e395

22 files changed

Lines changed: 516 additions & 53 deletions

‎packages/parsers/src/gsapParser.test.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1607,9 +1607,8 @@ describe("native GSAP keyframes parsing", () => {
16071607
const script = `
16081608
const tl = gsap.timeline({ paused: true });
16091609
tl.to("#hero", {
1610-
keyframes: { "0%": { x: 0 }, "50%": { x: 100, ease: "back.out(1.7)" }, "100%": { x: 200 } },
1610+
keyframes: { "0%": { x: 0 }, "50%": { x: 100, ease: "back.out(1.7)" }, "100%": { x: 200 }, easeEach: "power2.out" },
16111611
ease: "none",
1612-
easeEach: "power2.out",
16131612
duration: 5
16141613
}, 0);
16151614
`;
@@ -1618,7 +1617,7 @@ describe("native GSAP keyframes parsing", () => {
16181617

16191618
// Tween-level ease
16201619
expect(anim.ease).toBe("none");
1621-
// easeEach on keyframes data (set from tween-level)
1620+
// easeEach inside the keyframes object, the only place GSAP reads it
16221621
expect(anim.keyframes!.easeEach).toBe("power2.out");
16231622
// Per-keyframe ease
16241623
expect(anim.keyframes!.keyframes[1].ease).toBe("back.out(1.7)");

‎packages/parsers/src/gsapParser.ts‎

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
resolveConversionProps,
2424
mergePercentageKeyframes,
2525
authorsKeyframes,
26+
plainPercentKey,
2627
} from "./gsapSerialize";
2728

2829
export type {
@@ -1074,11 +1075,6 @@ function tweenCallToAnimation(
10741075
}
10751076
}
10761077

1077-
// Apply tween-level easeEach to keyframes data.
1078-
if (keyframesData && typeof vars.easeEach === "string") {
1079-
keyframesData.easeEach = vars.easeEach as string;
1080-
}
1081-
10821078
// When motionPath is present, reconstruct x/y as keyframe waypoints.
10831079
if (motionPathResult) {
10841080
const { waypoints } = motionPathResult;
@@ -2089,7 +2085,7 @@ function buildKeyframeObjectCode(
20892085
const props = keyframePropsToCode(kf);
20902086
if (kf.ease) props.push(`ease: ${JSON.stringify(kf.ease)}`);
20912087
if (kf.auto) props.push(`_auto: 1`);
2092-
return `${JSON.stringify(`${kf.percentage}%`)}: { ${props.join(", ")} }`;
2088+
return `${JSON.stringify(plainPercentKey(kf.percentage))}: { ${props.join(", ")} }`;
20932089
});
20942090
if (options?.easeEach) entries.push(`easeEach: ${JSON.stringify(options.easeEach)}`);
20952091
return `{ ${entries.join(", ")} }`;
@@ -2251,7 +2247,7 @@ function convertArrayKeyframesToObjectNode(varsArg: AstNode, scope: ScopeBinding
22512247
el.properties = (el.properties ?? []).filter(
22522248
(property: AstNode) => !isObjectProperty(property) || propKeyName(property) !== "duration",
22532249
);
2254-
return `${JSON.stringify(`${timing.percentages[i]}%`)}: ${recast.print(el).code}`;
2250+
return `${JSON.stringify(plainPercentKey(timing.percentages[i]!))}: ${recast.print(el).code}`;
22552251
});
22562252
prop.value = parseExpr(`{ ${entries.join(", ")}, easeEach: "none" }`);
22572253
return prop.value;
@@ -2290,7 +2286,7 @@ function locateKeyframeCtx(script: string, animationId: string, percentage: numb
22902286
if (!loc) return null;
22912287
const kfNode = findKeyframesObjectNode(loc.target.call.varsArg);
22922288
if (!kfNode) return null;
2293-
return { loc, kfNode, pctKey: `${percentage}%` };
2289+
return { loc, kfNode, pctKey: plainPercentKey(percentage) };
22942290
}
22952291

22962292
/**
@@ -2323,7 +2319,7 @@ export function addKeyframeToScript(
23232319
kfNode = findKeyframesObjectNode(loc.target.call.varsArg);
23242320
if (!kfNode) return script;
23252321
}
2326-
const pctKey = `${percentage}%`;
2322+
const pctKey = plainPercentKey(percentage);
23272323

23282324
const newValueNode = buildKeyframeValueNode(properties, ease);
23292325

@@ -2525,11 +2521,16 @@ export function moveKeyframeInScript(
25252521
entries.push({ pct: toPercentage, value: movedValue });
25262522
entries.sort((a, b) => a.pct - b.pct);
25272523

2528-
kfNode.properties = entries.map((e) => {
2529-
const p = parseExpr(`{ ${JSON.stringify(`${e.pct}%`)}: {} }`).properties[0];
2530-
p.value = e.value;
2531-
return p;
2532-
});
2524+
const pctProps = new Set(filterPercentageProps(kfNode));
2525+
const kept = (kfNode.properties ?? []).filter((p: AstNode) => !pctProps.has(p));
2526+
kfNode.properties = [
2527+
...entries.map((e) => {
2528+
const p = parseExpr(`{ ${JSON.stringify(plainPercentKey(e.pct))}: {} }`).properties[0];
2529+
p.value = e.value;
2530+
return p;
2531+
}),
2532+
...kept,
2533+
];
25332534
return recast.print(loc.parsed.ast).code;
25342535
}
25352536

@@ -2566,7 +2567,7 @@ export function resizeKeyframedTweenInScript(
25662567
seen.add(match.prop);
25672568
// Replace only the key node; the value node (incl. _auto + per-keyframe ease)
25682569
// stays verbatim. easeEach is a sibling non-percentage prop, left untouched.
2569-
match.prop.key = parseExpr(`{ ${JSON.stringify(`${to}%`)}: 0 }`).properties[0].key;
2570+
match.prop.key = parseExpr(`{ ${JSON.stringify(plainPercentKey(to))}: 0 }`).properties[0].key;
25702571
}
25712572

25722573
applyUpdatesToCall(loc.target.call, {
@@ -3370,7 +3371,8 @@ export function splitIntoPropertyGroups(
33703371
typeof anim.position === "number" ? anim.position : 0,
33713372
anim.duration ?? 0.5,
33723373
groupKeyframes,
3373-
anim.keyframes.easeEach ?? anim.ease,
3374+
anim.keyframes.ease ?? anim.ease,
3375+
anim.keyframes.easeEach,
33743376
);
33753377
result = addResult.script;
33763378
} else {

‎packages/parsers/src/gsapParserAcorn.full.test.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -945,9 +945,8 @@ describe("native GSAP keyframes parsing", () => {
945945
const script = `
946946
const tl = gsap.timeline({ paused: true });
947947
tl.to("#hero", {
948-
keyframes: { "0%": { x: 0 }, "50%": { x: 100, ease: "back.out(1.7)" }, "100%": { x: 200 } },
948+
keyframes: { "0%": { x: 0 }, "50%": { x: 100, ease: "back.out(1.7)" }, "100%": { x: 200 }, easeEach: "power2.out" },
949949
ease: "none",
950-
easeEach: "power2.out",
951950
duration: 5
952951
}, 0);
953952
`;

‎packages/parsers/src/gsapParserAcorn.ts‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,10 +1326,6 @@ function tweenCallToAnimation(
13261326
}
13271327
}
13281328

1329-
if (keyframesData && typeof vars.easeEach === "string") {
1330-
keyframesData.easeEach = vars.easeEach as string;
1331-
}
1332-
13331329
if (motionPathResult) {
13341330
const { waypoints } = motionPathResult;
13351331
if (!keyframesData) {

‎packages/parsers/src/gsapSerialize.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,12 @@ export function serializeValue(value: unknown): string {
322322
return String(value);
323323
}
324324

325+
export function plainPercentKey(percentage: number): string {
326+
const text =
327+
Math.abs(percentage) < 1e-6 ? percentage.toFixed(20).replace(/\.?0+$/, "") : String(percentage);
328+
return `${text}%`;
329+
}
330+
325331
export function safeJsKey(key: string): string {
326332
return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(key) ? key : JSON.stringify(key);
327333
}

‎packages/parsers/src/gsapUnroll.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ import * as acorn from "acorn";
1818
import * as acornWalk from "acorn-walk";
1919
import MagicString from "magic-string";
2020
import type { GsapAnimation } from "./gsapSerialize.js";
21-
import { serializeValue as valueToCode, safeJsKey as safeKey } from "./gsapSerialize.js";
21+
import {
22+
serializeValue as valueToCode,
23+
safeJsKey as safeKey,
24+
plainPercentKey,
25+
} from "./gsapSerialize.js";
2226
import { parseGsapScriptAcorn } from "./gsapParserAcorn.js";
2327
import { isFunctionNode, isTimelineRooted } from "./gsapInline.js";
2428

@@ -43,7 +47,7 @@ function keyframesEntry(anim: GsapAnimation): string {
4347
const kfs = (anim.keyframes?.keyframes ?? []).map((k) => {
4448
const body = propEntries(k.properties);
4549
if (k.ease) body.push(`ease: ${valueToCode(k.ease)}`);
46-
return `"${k.percentage}%": { ${body.join(", ")} }`;
50+
return `${JSON.stringify(plainPercentKey(k.percentage))}: { ${body.join(", ")} }`;
4751
});
4852
if (anim.keyframes?.easeEach) kfs.push(`easeEach: ${valueToCode(anim.keyframes.easeEach)}`);
4953
return `keyframes: { ${kfs.join(", ")} }`;

‎packages/parsers/src/gsapWriter.parity.test.ts‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ import {
6262
retimeClipTweensInScript,
6363
type ClipTweenRetime,
6464
dedupePositionWritesInScript as dedupePosAcorn,
65+
replaceTweenWithKeyframesInScript as replaceTweenWithKeyframesAcorn,
6566
} from "./gsapWriterAcorn.js";
6667

6768
function acornId(script: string): string {
@@ -1454,6 +1455,94 @@ describe("parity: moveKeyframeInScript (recast vs acorn)", () => {
14541455
});
14551456
});
14561457

1458+
describe("a keyframe percentage below 1e-6", () => {
1459+
const tiny = 1.2860082304526747e-7;
1460+
const readBack = (out: string) =>
1461+
parseGsapScriptAcorn(out).animations[0]!.keyframes?.keyframes.map((kf) => kf.percentage);
1462+
1463+
const steps = [
1464+
{ percentage: 0, properties: { x: 0 } },
1465+
{ percentage: tiny, properties: { x: 1 } },
1466+
{ percentage: 100, properties: { x: 10 } },
1467+
];
1468+
1469+
it.each([
1470+
["recast", addWithKfRecast],
1471+
["acorn", addWithKfAcorn],
1472+
])("is written as a decimal key apart from 0% (%s)", (_name, add) => {
1473+
const base = `const tl = gsap.timeline({ paused: true });`;
1474+
const out = add(base, "#a", 0, 2, steps).script;
1475+
expect(out).not.toMatch(/\de-\d/);
1476+
expect(readBack(out)).toEqual([0, expect.closeTo(tiny, 12), 100]);
1477+
});
1478+
1479+
it("stays apart from 0% when replace-with-keyframes writes both", () => {
1480+
const script = `const tl = gsap.timeline({ paused: true });
1481+
tl.to("#a", { duration: 2, x: 10, ease: "power4.in" }, 0);`;
1482+
const out = replaceTweenWithKeyframesAcorn(script, acornId(script), {
1483+
targetSelector: "#a",
1484+
position: 0,
1485+
duration: 2,
1486+
ease: "power4.in",
1487+
keyframes: steps,
1488+
})!;
1489+
expect(readBack(out)).toEqual([0, expect.closeTo(tiny, 12), 100]);
1490+
});
1491+
});
1492+
1493+
describe("a tween-level easeEach, which GSAP ignores", () => {
1494+
const script = `const tl = gsap.timeline({ paused: true });
1495+
tl.to("#a", { duration: 1, keyframes: { "0%": { x: 0 }, "100%": { x: 10 } }, easeEach: "power4.in" }, 0);`;
1496+
it.each([
1497+
["recast", parseGsapScript],
1498+
["acorn", parseGsapScriptAcorn],
1499+
])("is not reported as the keyframes' ease (%s)", (_name, parse) => {
1500+
expect(parse(script).animations[0]!.keyframes?.easeEach).toBeUndefined();
1501+
});
1502+
});
1503+
1504+
describe("a keyframe drag keeps the keyframes' eases", () => {
1505+
const writers = [
1506+
["recast", updateAnimRecast, moveKeyframeRecast],
1507+
["acorn", updateAnimAcorn, moveKeyframeAcorn],
1508+
] as const;
1509+
1510+
for (const [writer, update, move] of writers) {
1511+
it(`${writer}: an ease set with Studio's ease control survives a diamond drag`, () => {
1512+
const script = `const tl = gsap.timeline({ paused: true });
1513+
tl.to("#a", { duration: 2, keyframes: { "0%": { x: 0 }, "50%": { x: 5 }, "100%": { x: 10 } } }, 0);`;
1514+
const id = acornId(script);
1515+
const eased = update(script, id, { easeEach: "power2.out", resetKeyframeEases: true });
1516+
expect(parseGsapScriptAcorn(eased).animations[0]!.keyframes?.easeEach).toBe("power2.out");
1517+
1518+
const kf = parseGsapScriptAcorn(move(eased, id, 50, 60)).animations[0]!.keyframes!;
1519+
expect(kf.keyframes.map((k) => k.percentage)).toEqual([0, 60, 100]);
1520+
expect(kf.easeEach).toBe("power2.out");
1521+
});
1522+
1523+
it(`${writer}: splitting into property groups keeps the keyframes' eases`, () => {
1524+
const script = `const tl = gsap.timeline({ paused: true });
1525+
tl.to("#a", { duration: 2, keyframes: { "0%": { x: 0, opacity: 0 }, "100%": { x: 10, opacity: 1 }, ease: "sine.inOut", easeEach: "power2.out" } }, 0);`;
1526+
const splitGroups = writer === "acorn" ? splitGroupsAcorn : splitGroupsRecast;
1527+
const groups = parseGsapScriptAcorn(splitGroups(script, acornId(script)).script).animations;
1528+
expect(groups).toHaveLength(2);
1529+
for (const group of groups) {
1530+
expect(group.keyframes?.easeEach).toBe("power2.out");
1531+
expect(group.keyframes?.ease ?? group.ease).toBe("sine.inOut");
1532+
}
1533+
});
1534+
1535+
it(`${writer}: an ease authored on the keyframes survives a diamond drag`, () => {
1536+
const script = `const tl = gsap.timeline({ paused: true });
1537+
tl.to("#a", { duration: 2, keyframes: { "0%": { x: 0 }, "50%": { x: 5 }, "100%": { x: 10 }, ease: "sine.inOut" } }, 0);`;
1538+
const kf = parseGsapScriptAcorn(move(script, acornId(script), 50, 60)).animations[0]!
1539+
.keyframes!;
1540+
expect(kf.keyframes.map((k) => k.percentage)).toEqual([0, 60, 100]);
1541+
expect(kf.ease).toBe("sine.inOut");
1542+
});
1543+
}
1544+
});
1545+
14571546
// Regression: array-form `keyframes: [...]` has no explicit percentages, so
14581547
// locateWithKeyframes/findKeyframesObjectNode (which only match the object
14591548
// form) resolved to nothing and the move silently no-op'd — Studio's "Move to

‎packages/parsers/src/gsapWriterAcorn.ts‎

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
extractArcWaypoints,
2020
buildMotionPathObjectCode,
2121
mergePercentageKeyframes,
22+
plainPercentKey,
2223
} from "./gsapSerialize.js";
2324
import {
2425
parseGsapScriptAcornForWrite,
@@ -1225,7 +1226,7 @@ function convertArrayKeyframesToObject(script: string, target: Node): string {
12251226
if (!timing) return script;
12261227
const entries = els.map((el, i) => {
12271228
const { duration: _duration, ...record } = records[i]!;
1228-
return `${JSON.stringify(`${timing.percentages[i]}%`)}: ${recordToCode(record)}`;
1229+
return `${JSON.stringify(plainPercentKey(timing.percentages[i]!))}: ${recordToCode(record)}`;
12291230
});
12301231
const ms = new MagicString(script);
12311232
ms.overwrite(
@@ -1328,7 +1329,13 @@ export function addKeyframeToScript(
13281329
ms.overwrite(existing.prop.value.start, existing.prop.value.end, recordToCode(targetRecord));
13291330
}
13301331
} else {
1331-
insertNewKeyframe(ms, kfNode, percentage, `${percentage}%`, recordToCode(targetRecord));
1332+
insertNewKeyframe(
1333+
ms,
1334+
kfNode,
1335+
percentage,
1336+
plainPercentKey(percentage),
1337+
recordToCode(targetRecord),
1338+
);
13321339
}
13331340
for (const [prop, rec] of [...endpointOverwrites, ...backfillOverwrites]) {
13341341
ms.overwrite(prop.value.start, prop.value.end, recordToCode(rec));
@@ -1507,9 +1514,14 @@ export function moveKeyframeInScript(
15071514
entries.push({ pct: toPercentage, record: valueNodeToRecord(match.prop.value, src) });
15081515
entries.sort((a, b) => a.pct - b.pct);
15091516

1510-
const body = entries
1511-
.map((e) => `${JSON.stringify(`${e.pct}%`)}: ${recordToCode(e.record)}`)
1512-
.join(", ");
1517+
const pctProps = new Set(percentagePropsOf(kfNode));
1518+
const kept = (kfNode.properties ?? [])
1519+
.filter((p: Node) => !pctProps.has(p))
1520+
.map((p: Node) => src.slice(p.start, p.end));
1521+
const body = [
1522+
...entries.map((e) => `${JSON.stringify(plainPercentKey(e.pct))}: ${recordToCode(e.record)}`),
1523+
...kept,
1524+
].join(", ");
15131525
const ms = new MagicString(src);
15141526
ms.overwrite(kfNode.start, kfNode.end, `{ ${body} }`);
15151527
return ms.toString();
@@ -1554,7 +1566,7 @@ export function resizeKeyframedTweenInScript(
15541566

15551567
const ms = new MagicString(src);
15561568
for (const { keyNode, to } of edits) {
1557-
ms.overwrite(keyNode.start, keyNode.end, JSON.stringify(`${to}%`));
1569+
ms.overwrite(keyNode.start, keyNode.end, JSON.stringify(plainPercentKey(to)));
15581570
}
15591571
overwritePosition(ms, target.call, newPosition);
15601572
// Resizing is an explicit duration-authoring gesture. Promote GSAP's implicit
@@ -1753,7 +1765,7 @@ function buildKeyframeObjectCode(
17531765
const props = Object.entries(kf.properties).map(([k, v]) => `${safeKey(k)}: ${valueToCode(v)}`);
17541766
if (kf.ease) props.push(`ease: ${valueToCode(kf.ease)}`);
17551767
if (kf.auto) props.push(`_auto: 1`);
1756-
return `${JSON.stringify(`${kf.percentage}%`)}: { ${props.join(", ")} }`;
1768+
return `${JSON.stringify(plainPercentKey(kf.percentage))}: { ${props.join(", ")} }`;
17571769
});
17581770
if (easeEach) entries.push(`easeEach: ${valueToCode(easeEach)}`);
17591771
return `{ ${entries.join(", ")} }`;
@@ -1995,7 +2007,8 @@ function addGroupAnimToScript(
19952007
pos,
19962008
anim.duration ?? 0.5,
19972009
groupKeyframes,
1998-
anim.keyframes.easeEach ?? anim.ease,
2010+
anim.keyframes.ease ?? anim.ease,
2011+
anim.keyframes.easeEach,
19992012
);
20002013
}
20012014
const groupProperties = filterGroupProperties(anim.properties, propSet);

0 commit comments

Comments
 (0)