mirror of
https://github.com/Doodle3D/Doodle3D-Core.git
synced 2024-11-16 02:57:56 +01:00
27 lines
571 B
JavaScript
27 lines
571 B
JavaScript
import raf from 'raf';
|
|
|
|
export function tween(duration, callback) {
|
|
return new Promise(resolve => {
|
|
let elapsedTime = 0;
|
|
let lastTime = performance.now();
|
|
|
|
const step = () => {
|
|
const currentTime = performance.now();
|
|
const deltaTime = currentTime - lastTime;
|
|
lastTime = currentTime;
|
|
|
|
elapsedTime += deltaTime;
|
|
const progress = elapsedTime / duration;
|
|
|
|
if (progress >= 1.0) {
|
|
callback(1.0);
|
|
resolve();
|
|
} else {
|
|
callback(progress);
|
|
raf(step);
|
|
}
|
|
};
|
|
step(lastTime);
|
|
});
|
|
}
|