feat: AnimationController for scrollToContent
Signed-off-by: Mark Tolmacs <mark@lazycat.hu>
This commit is contained in:
@@ -204,135 +204,6 @@ export const easeOut = (k: number) => {
|
||||
return 1 - Math.pow(1 - k, 4);
|
||||
};
|
||||
|
||||
const easeOutInterpolate = (from: number, to: number, progress: number) => {
|
||||
return (to - from) * easeOut(progress) + from;
|
||||
};
|
||||
|
||||
/**
|
||||
* Animates values from `fromValues` to `toValues` using the requestAnimationFrame API.
|
||||
* Executes the `onStep` callback on each step with the interpolated values.
|
||||
* Returns a function that can be called to cancel the animation.
|
||||
*
|
||||
* @example
|
||||
* // Example usage:
|
||||
* const fromValues = { x: 0, y: 0 };
|
||||
* const toValues = { x: 100, y: 200 };
|
||||
* const onStep = ({x, y}) => {
|
||||
* setState(x, y)
|
||||
* };
|
||||
* const onCancel = () => {
|
||||
* console.log("Animation canceled");
|
||||
* };
|
||||
*
|
||||
* const cancelAnimation = easeToValuesRAF({
|
||||
* fromValues,
|
||||
* toValues,
|
||||
* onStep,
|
||||
* onCancel,
|
||||
* });
|
||||
*
|
||||
* // To cancel the animation:
|
||||
* cancelAnimation();
|
||||
*/
|
||||
export const easeToValuesRAF = <
|
||||
T extends Record<keyof T, number>,
|
||||
K extends keyof T,
|
||||
>({
|
||||
fromValues,
|
||||
toValues,
|
||||
onStep,
|
||||
duration = 250,
|
||||
interpolateValue,
|
||||
onStart,
|
||||
onEnd,
|
||||
onCancel,
|
||||
}: {
|
||||
fromValues: T;
|
||||
toValues: T;
|
||||
/**
|
||||
* Interpolate a single value.
|
||||
* Return undefined to be handled by the default interpolator.
|
||||
*/
|
||||
interpolateValue?: (
|
||||
fromValue: number,
|
||||
toValue: number,
|
||||
/** no easing applied */
|
||||
progress: number,
|
||||
key: K,
|
||||
) => number | undefined;
|
||||
onStep: (values: T) => void;
|
||||
duration?: number;
|
||||
onStart?: () => void;
|
||||
onEnd?: () => void;
|
||||
onCancel?: () => void;
|
||||
}) => {
|
||||
let canceled = false;
|
||||
let frameId = 0;
|
||||
let startTime: number;
|
||||
|
||||
function step(timestamp: number) {
|
||||
if (canceled) {
|
||||
return;
|
||||
}
|
||||
if (startTime === undefined) {
|
||||
startTime = timestamp;
|
||||
onStart?.();
|
||||
}
|
||||
|
||||
const elapsed = Math.min(timestamp - startTime, duration);
|
||||
const factor = easeOut(elapsed / duration);
|
||||
|
||||
const newValues = {} as T;
|
||||
|
||||
Object.keys(fromValues).forEach((key) => {
|
||||
const _key = key as keyof T;
|
||||
const result = ((toValues[_key] - fromValues[_key]) * factor +
|
||||
fromValues[_key]) as T[keyof T];
|
||||
newValues[_key] = result;
|
||||
});
|
||||
|
||||
onStep(newValues);
|
||||
|
||||
if (elapsed < duration) {
|
||||
const progress = elapsed / duration;
|
||||
|
||||
const newValues = {} as T;
|
||||
|
||||
Object.keys(fromValues).forEach((key) => {
|
||||
const _key = key as K;
|
||||
const startValue = fromValues[_key];
|
||||
const endValue = toValues[_key];
|
||||
|
||||
let result;
|
||||
|
||||
result = interpolateValue
|
||||
? interpolateValue(startValue, endValue, progress, _key)
|
||||
: easeOutInterpolate(startValue, endValue, progress);
|
||||
|
||||
if (result == null) {
|
||||
result = easeOutInterpolate(startValue, endValue, progress);
|
||||
}
|
||||
|
||||
newValues[_key] = result as T[K];
|
||||
});
|
||||
onStep(newValues);
|
||||
|
||||
frameId = window.requestAnimationFrame(step);
|
||||
} else {
|
||||
onStep(toValues);
|
||||
onEnd?.();
|
||||
}
|
||||
}
|
||||
|
||||
frameId = window.requestAnimationFrame(step);
|
||||
|
||||
return () => {
|
||||
onCancel?.();
|
||||
canceled = true;
|
||||
window.cancelAnimationFrame(frameId);
|
||||
};
|
||||
};
|
||||
|
||||
// https://github.com/lodash/lodash/blob/es/chunk.js
|
||||
export const chunk = <T extends any>(
|
||||
array: readonly T[],
|
||||
|
||||
Reference in New Issue
Block a user