18 lines
724 B
JavaScript
18 lines
724 B
JavaScript
export function decay({ velocity = 0, from = 0, power = 0.8, timeConstant = 350, restDelta = 0.5, modifyTarget, }) {
|
|
const state = { done: false, value: from };
|
|
let amplitude = power * velocity;
|
|
const ideal = from + amplitude;
|
|
const target = modifyTarget === undefined ? ideal : modifyTarget(ideal);
|
|
if (target !== ideal)
|
|
amplitude = target - from;
|
|
return {
|
|
next: (t) => {
|
|
const delta = -amplitude * Math.exp(-t / timeConstant);
|
|
state.done = !(delta > restDelta || delta < -restDelta);
|
|
state.value = state.done ? target : target + delta;
|
|
return state;
|
|
},
|
|
flipTarget: () => { },
|
|
};
|
|
}
|
|
//# sourceMappingURL=decay.js.map
|