Files
hyperframes/packages/parsers/src/springEase.ts
T
Miguel Ángel cdf9c817e1 refactor: extract @hyperframes/parsers from core (#1755)
## Summary

Extracts the GSAP parser/writer suite, HTML parser, hf-ids, spring-ease, and the shared composition data types out of `@hyperframes/core/src/parsers/` into a new, independently-publishable **`@hyperframes/parsers`** package.

This is the foundation of the [#1749](https://github.com/heygen-com/hyperframes/issues/1749) effort: make HyperFrames' parsing/linting/validation usable as plain libraries in a Node app, without shelling out to the CLI. Parsers is the standalone base every other extracted package builds on.

**Part 1 of 3** — splits #1754 into independently-reviewable pieces. Parts 2 (lint) and 3 (studio-server) stack on this branch.

## What moves

| | |
|---|---|
| Source moved out of core | **~9,900 LOC** (`src/parsers/` → `packages/parsers/src/`) |
| Total lines removed from core (incl. tests + goldens) | ~19,600 |
| Files relocated | 39 |
| Tests carried over | **660 passing** (5 skipped, 3 todo) |

The big movers: `gsapParser` / `gsapParserAcorn` (the recast + acorn dual parsers), `gsapWriterAcorn`, `gsapSerialize`, `gsapUnroll`, `htmlParser`, `hfIds`, `springEase`, `stableIds`, plus the `__goldens__` corpus.

## Bundle footprint of the new package

| Artifact | Size |
|---|---|
| `dist/` (unpacked) | 1.7 MB |
| npm tarball (packed) | 409 KB |
| `dist/index.js` | 90 KB (**~21 KB gzipped**) |
| Heaviest entries | `gsapWriterAcorn.js` 93 KB · `gsapParser.js` 91 KB |

Most of the weight is the GSAP AST machinery (recast/babel/acorn). It's tree-shakeable via subpath entries (`@hyperframes/parsers/hf-ids`, `/gsap-constants`, etc.) so a consumer that only needs `hf-ids` (2 KB) doesn't pull the parsers.

## How `@hyperframes/core` changes

The interesting part: **core sheds its entire AST toolchain.**

| core `dependencies` | before | after |
|---|---|---|
| count | 9 | 6 |
| removed | — | `@babel/parser`, `acorn`, `acorn-walk`, `magic-string`, `recast` |
| added | — | `@hyperframes/parsers`, `linkedom` |

Before this PR, importing `@hyperframes/core` at all dragged in babel + recast + acorn just to construct types. Now those live behind `@hyperframes/parsers`, and a consumer that only wants core's runtime/compiler types never resolves the parser stack. Core keeps thin `@deprecated` re-export stubs at the old subpaths (`@hyperframes/core/gsap-parser`, `/gsap-constants`, …) so nothing downstream breaks.

## Design notes

- **`"bun"` export condition before `"node"`** in every package export. Bun resolves the TypeScript source directly (no pre-built `dist/`), while Node/tsx/Docker contexts fall through to `"node"` → `dist/`. This keeps the dev loop zero-build while published artifacts stay Node-consumable.
- `@hyperframes/parsers` is **standalone** — zero `@hyperframes/*` dependencies — so it can be the base of the stack.

## Test plan

- [x] `bun run --filter @hyperframes/parsers test` — 660 tests pass
- [x] `bun run --filter @hyperframes/sdk test` — 382 tests pass
- [x] `bun run build` — full monorepo build succeeds
- [x] Fallow audit passes on CI
2026-06-27 00:46:26 -04:00

89 lines
3.1 KiB
TypeScript

/**
* Damped harmonic oscillator solver for GSAP CustomEase spring curves.
*
* Generates an SVG path data string compatible with `CustomEase.create(id, data)`.
* The solver supports underdamped (bouncy), critically damped, and overdamped
* spring configurations. Output is normalized to x ∈ [0,1] with y starting at 0
* and settling to 1.
*/
export interface SpringPreset {
name: string;
label: string;
mass: number;
stiffness: number;
damping: number;
}
export const SPRING_PRESETS: SpringPreset[] = [
{ name: "spring-gentle", label: "Gentle", mass: 1, stiffness: 100, damping: 15 },
{ name: "spring-bouncy", label: "Bouncy", mass: 1, stiffness: 180, damping: 12 },
{ name: "spring-stiff", label: "Stiff", mass: 1, stiffness: 300, damping: 20 },
{ name: "spring-wobbly", label: "Wobbly", mass: 1, stiffness: 120, damping: 8 },
{ name: "spring-heavy", label: "Heavy", mass: 3, stiffness: 200, damping: 20 },
];
/**
* Solve a damped harmonic oscillator and return a GSAP CustomEase data string.
*
* The output is an SVG path (`M0,0 L... L...`) that CustomEase.create() accepts.
* The curve is normalized so x spans [0,1] and the spring settles at y = 1.
*
* @param mass - Spring mass (> 0)
* @param stiffness - Spring stiffness constant (> 0)
* @param damping - Damping coefficient (> 0)
* @param steps - Number of sample points (default 120)
*/
export function generateSpringEaseData(
mass: number,
stiffness: number,
damping: number,
steps = 120,
): string {
const w0 = Math.sqrt(stiffness / mass);
const zeta = damping / (2 * Math.sqrt(stiffness * mass));
// Determine simulation duration: time until oscillation settles within threshold of 1.0.
// Underdamped: ~5 time constants. Critically/overdamped: characteristic decay time.
let settleDuration: number;
if (zeta < 1) {
settleDuration = Math.min(5 / (zeta * w0), 10);
} else {
const decayRate = zeta * w0 - w0 * Math.sqrt(zeta * zeta - 1);
settleDuration = Math.min(4 / Math.max(decayRate, 0.01), 10);
}
const simDuration = Math.max(settleDuration, 1);
const segments: string[] = ["M0,0"];
for (let i = 1; i <= steps; i++) {
const t = i / steps;
const simT = t * simDuration;
let value: number;
if (zeta < 1) {
// Underdamped — oscillates before settling
const wd = w0 * Math.sqrt(1 - zeta * zeta);
value =
1 -
Math.exp(-zeta * w0 * simT) *
(Math.cos(wd * simT) + ((zeta * w0) / wd) * Math.sin(wd * simT));
} else if (zeta === 1) {
// Critically damped — fastest approach without oscillation
value = 1 - (1 + w0 * simT) * Math.exp(-w0 * simT);
} else {
// Overdamped — slow exponential approach
const s1 = -w0 * (zeta - Math.sqrt(zeta * zeta - 1));
const s2 = -w0 * (zeta + Math.sqrt(zeta * zeta - 1));
value = 1 + (s1 * Math.exp(s2 * simT) - s2 * Math.exp(s1 * simT)) / (s2 - s1);
}
segments.push(`${t.toFixed(4)},${value.toFixed(4)}`);
}
// Force exact endpoint
segments[segments.length - 1] = "1,1";
return `${segments[0]} L${segments.slice(1).join(" ")}`;
}