UNPKG

@thi.ng/ramp

Version:

Extensible keyframe interpolation/tweening of arbitrary, nested types

31 lines (30 loc) 937 B
import { easeInOut5 } from "@thi.ng/math/easing"; import { norm } from "@thi.ng/math/fit"; import { mix } from "@thi.ng/math/mix"; import { Ramp } from "./ramp.js"; const easing = (stops, opts) => new Ramp(EASING_N(opts?.easing), stops, opts); const EASING_N = (easing2 = easeInOut5) => { return { min: (acc, x) => Math.min(acc ?? Infinity, x), max: (acc, x) => Math.max(acc ?? -Infinity, x), at: (stops, i, t) => { const a = stops[i]; const b = stops[i + 1]; return mix(a[1], b[1], easing2(norm(t, a[0], b[0]))); } }; }; const EASING_V = (vec, easing2 = easeInOut5) => ({ min: (acc, x) => vec.min(acc, acc || vec.vecOf(Infinity), x), max: (acc, x) => vec.max(acc, acc || vec.vecOf(-Infinity), x), at: (stops, i, t) => { const a = stops[i]; const b = stops[i + 1]; return vec.mixN([], a[1], b[1], easing2(norm(t, a[0], b[0]))); } }); export { EASING_N, EASING_V, easing };