-
Notifications
You must be signed in to change notification settings - Fork 3.6k
feat(core): add deterministic keyframe ease runtime #2672
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { evaluateSpringEase, parseSpringBounce } from "./springEase"; | ||
|
|
||
| describe("single-parameter spring ease", () => { | ||
| it("parses and clamps the bounce parameter", () => { | ||
| expect(parseSpringBounce("spring(0.5)")).toBe(0.5); | ||
| expect(parseSpringBounce(" spring(2) ")).toBe(1); | ||
| expect(parseSpringBounce("spring(-1)")).toBe(0); | ||
| expect(parseSpringBounce("spring(nope)")).toBeNull(); | ||
| }); | ||
|
|
||
| it("starts at zero, overshoots, and settles exactly at one", () => { | ||
| const samples = Array.from({ length: 101 }, (_, index) => evaluateSpringEase(index / 100, 0.5)); | ||
| expect(samples[0]).toBe(0); | ||
| expect(samples.at(-1)).toBe(1); | ||
| expect(Math.max(...samples)).toBeGreaterThan(1); | ||
| }); | ||
|
|
||
| it("is deterministic and makes higher bounce values more oscillatory", () => { | ||
| const progress = Array.from({ length: 41 }, (_, index) => index / 40); | ||
| expect(progress.map((value) => evaluateSpringEase(value, 0.75))).toEqual( | ||
| progress.map((value) => evaluateSpringEase(value, 0.75)), | ||
| ); | ||
|
|
||
| const lowBounce = progress.map((value) => evaluateSpringEase(value, 0.25)); | ||
| const highBounce = progress.map((value) => evaluateSpringEase(value, 0.75)); | ||
| expect(Math.max(...highBounce)).toBeGreaterThan(Math.max(...lowBounce)); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,31 @@ | ||
| /** @deprecated Import from @hyperframes/parsers/spring-ease */ | ||
| // Preserve the legacy symbols (SpringPreset, SPRING_PRESETS, generateSpringEaseData) | ||
| // on the still-published ./spring-ease subpath. Dropping them is a breaking change | ||
| // for external importers; the canonical source stays @hyperframes/parsers/spring-ease. | ||
| export * from "@hyperframes/parsers/spring-ease"; | ||
|
|
||
| const SPRING_TOKEN = /^\s*spring\(\s*([+-]?(?:\d+(?:\.\d*)?|\.\d+))\s*\)\s*$/; | ||
|
|
||
| function clampBounce(bounce: number): number { | ||
| return Math.max(0, Math.min(1, bounce)); | ||
| } | ||
|
|
||
| /** Parse Studio's single-parameter spring token into a normalized bounce value. */ | ||
| export function parseSpringBounce(ease: string): number | null { | ||
| const match = SPRING_TOKEN.exec(ease); | ||
| if (!match) return null; | ||
| const bounce = Number(match[1]); | ||
| return Number.isFinite(bounce) ? clampBounce(bounce) : null; | ||
| } | ||
|
|
||
| /** Evaluate Studio's deterministic, endpoint-normalized damped-cosine spring. */ | ||
| export function evaluateSpringEase(progress: number, bounce: number): number { | ||
| if (!Number.isFinite(progress)) return progress; | ||
| if (progress <= 0) return 0; | ||
| if (progress >= 1) return 1; | ||
|
|
||
| const normalizedBounce = clampBounce(bounce); | ||
| const decay = 12 - normalizedBounce * 6; | ||
| const angularFrequency = Math.PI * 2 * (1 + normalizedBounce * 1.5); | ||
| const endpoint = 1 - Math.exp(-decay) * Math.cos(angularFrequency); | ||
| return (1 - Math.exp(-decay * progress) * Math.cos(angularFrequency * progress)) / endpoint; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| import { describe, expect, it, vi } from "vitest"; | ||
| import { installStudioCustomEase } from "./customEase"; | ||
|
|
||
| describe("installStudioCustomEase", () => { | ||
| it("resolves wiggle while preserving hold, spring, custom, and named eases", () => { | ||
| const namedEase = (progress: number) => progress * progress; | ||
| const originalParseEase = vi.fn(() => namedEase); | ||
| const gsap = { parseEase: originalParseEase }; | ||
|
|
||
| expect(installStudioCustomEase(gsap)).toBe(true); | ||
|
|
||
| const wiggleEase = gsap.parseEase("wiggle(6,easeInOut)"); | ||
| expect(wiggleEase).toBeTypeOf("function"); | ||
| const samples = Array.from({ length: 401 }, (_, index) => wiggleEase(index / 400)); | ||
| expect(samples[0]).toBe(0); | ||
| expect(samples.at(-1)).toBe(1); | ||
| expect(samples).toEqual(Array.from({ length: 401 }, (_, index) => wiggleEase(index / 400))); | ||
| const directions = samples | ||
| .slice(1) | ||
| .map((value, index) => Math.sign(value - samples[index]!)) | ||
| .filter((direction) => direction !== 0); | ||
| expect( | ||
| directions.filter((direction, index) => index > 0 && direction !== directions[index - 1]) | ||
| .length, | ||
| ).toBeGreaterThanOrEqual(8); | ||
|
|
||
| expect(gsap.parseEase("hold")(0.5)).toBe(0); | ||
| expect(gsap.parseEase("spring(0.5)")(0)).toBe(0); | ||
| expect(gsap.parseEase("custom(M0,0 C0.25,0.1 0.25,1 1,1)")(1)).toBe(1); | ||
| expect(Number.isNaN(gsap.parseEase("custom(M0,0 C0.25,0.1 0.25,1 1,1)")(Number.NaN))).toBe( | ||
| true, | ||
| ); | ||
| expect(gsap.parseEase("power2.out")).toBe(namedEase); | ||
| expect(originalParseEase).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it("registers custom eases in GSAP's internal ease map for keyframe-segment resolution", () => { | ||
| // GSAP resolves keyframe SEGMENT eases via its internal _parseEase/_easeMap, | ||
| // not the public parseEase — so the eases must be registered there too, else | ||
| // a custom ease inside `keyframes:{...}` resolves to undefined and throws | ||
| // "_ease is not a function" on first render. | ||
| const easeMap = new Map<string, ((progress: number) => number) & { config?: unknown }>(); | ||
| const gsap = { | ||
| parseEase: vi.fn((ease: unknown) => (typeof ease === "function" ? ease : null)), | ||
| registerEase: (name: string, ease: (progress: number) => number) => easeMap.set(name, ease), | ||
| }; | ||
|
|
||
| expect(installStudioCustomEase(gsap)).toBe(true); | ||
|
|
||
| // Bare hold registers directly and holds at 0 until the end. | ||
| expect(easeMap.get("hold")?.(0.5)).toBe(0); | ||
| expect(easeMap.get("hold")?.(1)).toBe(1); | ||
|
|
||
| // GSAP calls a configurable ease's `.config` with the parenthesized params | ||
| // comma-split (custom's bezier path splits into several parts). The config | ||
| // must rejoin them and resolve a real ease. | ||
| const springConfig = easeMap.get("spring")?.config as ( | ||
| ...p: unknown[] | ||
| ) => (n: number) => number; | ||
| expect(springConfig(0.5)(0)).toBe(0); | ||
|
|
||
| const customConfig = easeMap.get("custom")?.config as ( | ||
| ...p: unknown[] | ||
| ) => (n: number) => number; | ||
| // "custom(M0,0 C0.25,0.1 0.25,1 1,1)" → params split on "," by GSAP: | ||
| const customEase = customConfig("M0", "0 C0.25", "0.1 0.25", "1 1", 1); | ||
| expect(customEase(0)).toBe(0); | ||
| expect(customEase(1)).toBe(1); | ||
| }); | ||
|
|
||
| it("installs once per GSAP instance and reinstalls on a replacement instance", () => { | ||
| const firstParser = vi.fn(() => (progress: number) => progress); | ||
| const first = { parseEase: firstParser }; | ||
| expect(installStudioCustomEase(first)).toBe(true); | ||
| const installed = first.parseEase; | ||
| expect(installStudioCustomEase(first)).toBe(true); | ||
| expect(first.parseEase).toBe(installed); | ||
|
|
||
| const replacementParser = vi.fn(() => (progress: number) => progress); | ||
| const replacement = { parseEase: replacementParser }; | ||
| expect(installStudioCustomEase(replacement)).toBe(true); | ||
| expect(replacement.parseEase).not.toBe(replacementParser); | ||
| }); | ||
|
|
||
| it("uses the same GSAP fallback for malformed public and segment eases", () => { | ||
| const fallback = (progress: number) => progress * progress; | ||
| const easeMap = new Map<string, ConfigurableEase>(); | ||
| type ConfigurableEase = ((progress: number) => number) & { | ||
| config?: (...params: unknown[]) => (progress: number) => number; | ||
| }; | ||
| const gsap = { | ||
| parseEase: vi.fn(() => fallback), | ||
| registerEase: (name: string, ease: ConfigurableEase) => easeMap.set(name, ease), | ||
| }; | ||
| expect(installStudioCustomEase(gsap)).toBe(true); | ||
|
|
||
| expect(gsap.parseEase("spring(nope)")).toBe(fallback); | ||
| expect(easeMap.get("spring")?.config?.("nope")).toBe(fallback); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.