mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 16:08:56 +00:00
* 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
81 lines
2.6 KiB
TypeScript
81 lines
2.6 KiB
TypeScript
import type { GsapAnimation } from "@hyperframes/core/gsap-parser";
|
|
import { GSAP_DEFAULT_DURATION } from "@hyperframes/parsers/gsap-parser";
|
|
import { progressAtTime, runEaseOf, timeAtProgress } from "./gsapKeyframeEases";
|
|
|
|
export function absoluteToPercentage(
|
|
time: number,
|
|
tweenStart: number,
|
|
tweenDuration: number,
|
|
): number {
|
|
if (tweenDuration <= 0) return 0;
|
|
const raw = ((time - tweenStart) / tweenDuration) * 100;
|
|
return Math.max(0, Math.min(100, Math.round(raw * 1000) / 1000));
|
|
}
|
|
|
|
export function percentageToAbsolute(
|
|
pct: number,
|
|
tweenStart: number,
|
|
tweenDuration: number,
|
|
): number {
|
|
return tweenStart + (pct / 100) * tweenDuration;
|
|
}
|
|
|
|
export function isTimeWithinTween(
|
|
time: number,
|
|
tweenStart: number,
|
|
tweenDuration: number,
|
|
): boolean {
|
|
return time >= tweenStart && time <= tweenStart + tweenDuration;
|
|
}
|
|
|
|
export function resolveTweenStart(animation: GsapAnimation): number | null {
|
|
if (animation.resolvedStart != null) return animation.resolvedStart;
|
|
if (typeof animation.position === "number") return animation.position;
|
|
const parsed = Number.parseFloat(animation.position as string);
|
|
if (!Number.isNaN(parsed)) return parsed;
|
|
return null;
|
|
}
|
|
|
|
export function resolveTweenDuration(animation: GsapAnimation): number {
|
|
return animation.duration ?? GSAP_DEFAULT_DURATION;
|
|
}
|
|
|
|
export function findTweenAtTime(
|
|
time: number,
|
|
animations: GsapAnimation[],
|
|
selector: string,
|
|
): GsapAnimation | null {
|
|
for (const anim of animations) {
|
|
if (!matchesSelector(anim.targetSelector, selector)) continue;
|
|
const start = resolveTweenStart(anim);
|
|
if (start === null) continue;
|
|
const duration = resolveTweenDuration(anim);
|
|
if (isTimeWithinTween(time, start, duration)) return anim;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
export function absoluteToPercentageForAnimation(
|
|
time: number,
|
|
animation: GsapAnimation,
|
|
): number | null {
|
|
const start = resolveTweenStart(animation);
|
|
if (start === null) return null;
|
|
const duration = resolveTweenDuration(animation);
|
|
return progressAtTime(runEaseOf(animation), absoluteToPercentage(time, start, duration));
|
|
}
|
|
|
|
export function percentageToAbsoluteForAnimation(
|
|
pct: number,
|
|
animation: GsapAnimation,
|
|
): number | null {
|
|
const start = resolveTweenStart(animation);
|
|
if (start === null) return null;
|
|
const duration = resolveTweenDuration(animation);
|
|
return percentageToAbsolute(timeAtProgress(runEaseOf(animation), pct), start, duration);
|
|
}
|
|
|
|
function matchesSelector(tweenSelector: string, querySelector: string): boolean {
|
|
return tweenSelector.split(",").some((part) => part.trim() === querySelector);
|
|
}
|