silphius/app/util/delta.js
2024-07-04 09:09:37 -05:00

33 lines
828 B
JavaScript

import TickingPromise from '@/util/ticking-promise.js';
export default function delta(object, properties) {
const deltas = {};
for (const key in properties) {
const property = properties[key];
const delta = {
duration: Infinity,
elapsed: 0,
...property,
};
deltas[key] = delta;
}
let stop;
const promise = new TickingPromise(
(resolve) => {
stop = resolve;
},
(elapsed, resolve) => {
for (const key in deltas) {
deltas[key].elapsed += elapsed;
if (deltas[key].elapsed >= deltas[key].duration) {
object[key] += deltas[key].delta * (deltas[key].duration - deltas[key].elapsed);
resolve();
return;
}
object[key] += deltas[key].delta * elapsed;
}
},
);
return {stop, deltas, promise};
}