@thi.ng/ramp
Version:
Extensible keyframe interpolation/tweening of arbitrary, nested types
27 lines (26 loc) • 736 B
JavaScript
import { fit, norm } from "@thi.ng/math/fit";
import { Ramp } from "./ramp.js";
const linear = (stops, opts) => new Ramp(LINEAR_N, stops, opts);
const LINEAR_N = {
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 fit(t, a[0], b[0], a[1], b[1]);
}
};
const LINEAR_V = (vec) => ({
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], norm(t, a[0], b[0]));
}
});
export {
LINEAR_N,
LINEAR_V,
linear
};