21 lines
680 B
JavaScript
Raw Normal View History

2024-01-29 09:26:07 +08:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
2024-08-02 18:19:39 +08:00
exports.tween = tween;
const easeOut = (t) => 1 - Math.pow((1 - t), 5);
2024-01-29 09:26:07 +08:00
function tween(props) {
const { from, to, duration, onUpdate, onFinish } = props;
2024-08-02 18:19:39 +08:00
const startTime = performance.now();
2024-01-29 09:26:07 +08:00
const tick = () => {
const current = performance.now();
const elapsedTime = Math.min(current - startTime, duration);
const currentValue = from + (to - from) * easeOut(elapsedTime / duration);
if (elapsedTime === duration) {
onFinish();
return;
}
onUpdate(currentValue);
requestAnimationFrame(tick);
};
tick();
}