@thi.ng/ramp
Version:
Extensible keyframe interpolation/tweening of arbitrary, nested types
55 lines (54 loc) • 1.35 kB
JavaScript
import { unconstrained } from "./domain.js";
import { __samples } from "./utils.js";
const group = (children, opts) => new Group(children, opts);
class Group {
constructor(children, opts) {
this.children = children;
this.childEntries = Object.entries(this.children);
this.domain = opts?.domain || unconstrained;
}
children;
domain;
childEntries;
at(t) {
t = this.domain(t, ...this.timeBounds());
return this.childEntries.reduce(
(acc, [id, ramp]) => {
acc[id] = ramp.at(t);
return acc;
},
{}
);
}
samples(n = 100, start, end) {
return __samples(this, n, start, end);
}
bounds() {
return this.childEntries.reduce(
(acc, [id, ramp]) => {
const bounds = ramp.bounds();
acc.minT = Math.min(acc.minT, bounds.minT);
acc.maxT = Math.max(acc.maxT, bounds.maxT);
acc.min[id] = bounds.min;
acc.max[id] = bounds.max;
return acc;
},
{ minT: Infinity, maxT: -Infinity, min: {}, max: {} }
);
}
timeBounds() {
return this.childEntries.reduce(
(acc, [_, ramp]) => {
const bounds = ramp.timeBounds();
acc[0] = Math.min(acc[0], bounds[0]);
acc[1] = Math.max(acc[1], bounds[1]);
return acc;
},
[Infinity, -Infinity]
);
}
}
export {
Group,
group
};