silphius/app/util/transition.js

49 lines
1.2 KiB
JavaScript
Raw Normal View History

2024-07-22 01:31:52 -05:00
import {Ticker} from '@/util/promise.js';
2024-07-03 21:58:24 -05:00
2024-07-30 22:57:52 -05:00
import * as Easing from './easing';
2024-07-03 21:58:24 -05:00
export default function transition(object, properties) {
const transitions = {};
for (const key in properties) {
const property = properties[key];
const transition = {
elapsed: 0,
start: object[key],
...property,
};
if (!transition.easing) {
transition.easing = Easing.easeOutQuad;
}
else {
if ('string' === typeof transition.easing) {
transition.easing = Easing[transition.easing];
}
}
transitions[key] = transition;
}
let stop;
2024-07-22 01:31:52 -05:00
const promise = new Ticker(
2024-07-03 21:58:24 -05:00
(resolve) => {
stop = resolve;
},
(elapsed, resolve) => {
for (const key in transitions) {
const transition = transitions[key];
transition.elapsed += elapsed;
if (transition.elapsed >= transition.duration) {
2024-07-04 09:08:20 -05:00
object[key] = transition.start + transition.magnitude;
2024-07-03 21:58:24 -05:00
resolve();
return;
}
object[key] = transition.easing(
transition.elapsed,
transition.start,
transition.magnitude,
transition.duration,
);
}
},
);
2024-07-04 09:08:20 -05:00
return {stop, transitions, promise};
2024-07-03 21:58:24 -05:00
}