Files
hyperframes/packages/studio/src/hooks/gsapRuntimeMotionPath.ts
T
Miguel Ángel db12b3e022 fix(studio): place GSAP step lists and duration-less tweens where GSAP plays them (#5024)
* fix(studio): place GSAP step lists and duration-less tweens where GSAP plays them

* refactor(studio): drop narrated comments and unnest the extend clamp

* fix(studio): match keyframes in time, resolve partial step timing, start readout from prior tween

* fix(studio): time step-list keyframes from the parts' last end and format

* refactor(studio): trim comments to the package's comment share

* fix(parsers): apply timeline defaults as GSAP does to step lists and motion paths

* fix(studio): keep GSAP's ease for motion paths with keyframes and read finished tweens

* refactor(studio): read a live motion path in its own step

* refactor(parsers): leave step-list timing to its one owner and skip timeline defaults there

* refactor(studio): move the live motion-path read into its own module

* test(parsers): pin an unresolved length for a step list stretched by a variable duration

* fix(studio): time keyframes linearly under an outer ease that overshoots or steps

* fix(studio): time a converted expression-duration tween from its live tween, and pin no rewrite
2026-10-05 02:44:49 +00:00

48 lines
2.0 KiB
TypeScript

import { buildArcPath, type ArcPathConfig } from "@hyperframes/core/gsap-parser-acorn";
import { MOTION_PATH_RUN_EASE } from "../utils/gsapKeyframeEases";
import type { ReadTween } from "./gsapRuntimeKeyframes";
function isXY(p: unknown): p is { x: number; y: number } {
return !!p && typeof (p as any).x === "number" && typeof (p as any).y === "number";
}
/** Coordinates + curviness from a live `vars.motionPath` value (object or array form), or null. */
function coordsFromMotionPath(mp: unknown): {
coords: Array<{ x: number; y: number }>;
curviness: number;
autoRotate: boolean | number;
isCubic: boolean;
} | null {
if (!mp || typeof mp !== "object") return null;
const obj = mp as Record<string, unknown>;
const pathVal = Array.isArray(mp) ? mp : obj.path;
if (!Array.isArray(pathVal)) return null;
const coords = pathVal.filter(isXY).map((p) => ({ x: p.x, y: p.y }));
if (coords.length < 2) return null;
const curviness = typeof obj.curviness === "number" ? obj.curviness : 1;
const autoRotate = typeof obj.autoRotate === "number" ? obj.autoRotate : obj.autoRotate === true;
return { coords, curviness, autoRotate, isCubic: obj.type === "cubic" };
}
/** Build an arcPath config from a live `vars.motionPath` value. */
export function arcPathFromMotionPathValue(mp: unknown): ArcPathConfig | undefined {
const parsed = coordsFromMotionPath(mp);
if (!parsed) return undefined;
return buildArcPath(parsed.coords, parsed.curviness, parsed.autoRotate, parsed.isCubic)?.arcPath;
}
export function readMotionPathTween(
vars: Record<string, unknown>,
runEase?: string,
): ReadTween | null {
const mp = coordsFromMotionPath(vars.motionPath);
const shape = mp && buildArcPath(mp.coords, mp.curviness, mp.autoRotate, mp.isCubic);
if (!shape) return null;
const n = shape.waypoints.length;
const keyframes = shape.waypoints.map((wp, i) => ({
percentage: n > 1 ? Math.round((i / (n - 1)) * 100) : 0,
properties: { x: wp.x, y: wp.y },
}));
return { keyframes, arcPath: shape.arcPath, runEase: runEase ?? MOTION_PATH_RUN_EASE };
}