Skip to content

🎥 Make videos programmatically with React

4.4 KB
Raw Blame
type AnimationNode = {
	lastTimestamp: number;
	toValue: number;
	current: number;
	velocity: number;
	prevPosition?: number;
};

export type SpringConfig = {
	damping: number;
	mass: number;
	stiffness: number;
	overshootClamping: boolean;
};

const defaultSpringConfig: SpringConfig = {
	damping: 10,
	mass: 1,
	stiffness: 100,
	overshootClamping: false,
};

const advanceCache: {[key: string]: AnimationNode} = {};

function advance({
	animation,
	now,
	config,
}: {
	animation: AnimationNode;
	now: number;
	config: SpringConfig;
}): AnimationNode {
	const {toValue, lastTimestamp, current, velocity} = animation;

	const deltaTime = Math.min(now - lastTimestamp, 64);

	if (config.damping <= 0) {
		throw new Error(
			'Spring damping must be greater than 0, otherwise the spring() animation will never end, causing an infinite loop.',
		);
	}

	const c = config.damping;
	const m = config.mass;
	const k = config.stiffness;

	const cacheKey = [
		toValue,
		lastTimestamp,
		current,
		velocity,
		c,
		m,
		k,
		now,
	].join('-');
	if (advanceCache[cacheKey]) {
		return advanceCache[cacheKey];
	}

	const v0 = -velocity;
	const x0 = toValue - current;

	const zeta = c / (2 * Math.sqrt(k * m)); // damping ratio
	const omega0 = Math.sqrt(k / m); // undamped angular frequency of the oscillator (rad/ms)
	const omega1 = omega0 * Math.sqrt(1 - zeta ** 2); // exponential decay

	const t = deltaTime / 1000;

	const sin1 = Math.sin(omega1 * t);
	const cos1 = Math.cos(omega1 * t);

	// under damped
	const underDampedEnvelope = Math.exp(-zeta * omega0 * t);
	const underDampedFrag1 =
		underDampedEnvelope *
		(sin1 * ((v0 + zeta * omega0 * x0) / omega1) + x0 * cos1);

	const underDampedPosition = toValue - underDampedFrag1;
	// This looks crazy -- it's actually just the derivative of the oscillation function
	const underDampedVelocity =
		zeta * omega0 * underDampedFrag1 -
		underDampedEnvelope *
			(cos1 * (v0 + zeta * omega0 * x0) - omega1 * x0 * sin1);

	// critically damped
	const criticallyDampedEnvelope = Math.exp(-omega0 * t);
	const criticallyDampedPosition =
		toValue - criticallyDampedEnvelope * (x0 + (v0 + omega0 * x0) * t);

	const criticallyDampedVelocity =
		criticallyDampedEnvelope *
		(v0 * (t * omega0 - 1) + t * x0 * omega0 * omega0);

	const animationNode: AnimationNode = {
		toValue,
		prevPosition: current,
		lastTimestamp: now,
		current: zeta < 1 ? underDampedPosition : criticallyDampedPosition,
		velocity: zeta < 1 ? underDampedVelocity : criticallyDampedVelocity,
	};
	advanceCache[cacheKey] = animationNode;
	return animationNode;
}

const calculationCache: {[key: string]: AnimationNode} = {};

export function springCalculation({
	frame,
	fps,
	config = {},
}: {
	frame: number;
	fps: number;
	config?: Partial<SpringConfig>;
}): AnimationNode {
	const from = 0;
	const to = 1;
	// An explicit `undefined` (e.g. a forwarded optional prop) must not
	// override the defaults, otherwise the physics turn into NaN
	const resolvedConfig: SpringConfig = {
		damping: config.damping ?? defaultSpringConfig.damping,
		mass: config.mass ?? defaultSpringConfig.mass,
		stiffness: config.stiffness ?? defaultSpringConfig.stiffness,
		overshootClamping:
			config.overshootClamping ?? defaultSpringConfig.overshootClamping,
	};
	const configKey = [
		fps,
		resolvedConfig.damping,
		resolvedConfig.mass,
		resolvedConfig.overshootClamping,
		resolvedConfig.stiffness,
	].join('-');
	const cacheKey = `${frame}-${configKey}`;
	if (calculationCache[cacheKey]) {
		return calculationCache[cacheKey];
	}

	const frameClamped = Math.max(0, frame);
	// Only resume from an integer frame: fractional steps change the simulation.
	const previousFrame = Math.ceil(frameClamped) - 1;
	const previousAnimation =
		previousFrame >= 0
			? calculationCache[`${previousFrame}-${configKey}`]
			: undefined;
	let animation: AnimationNode = previousAnimation ?? {
		lastTimestamp: 0,
		current: from,
		toValue: to,
		velocity: 0,
		prevPosition: 0,
	};
	const unevenRest = frameClamped % 1;
	const startFrame = previousAnimation ? previousFrame + 1 : 0;
	for (let f = startFrame; f <= Math.floor(frameClamped); f++) {
		const time = (f / fps) * 1000;
		animation = advance({
			animation,
			now: time,
			config: resolvedConfig,
		});
	}

	if (unevenRest > 0) {
		animation = advance({
			animation,
			now: (frameClamped / fps) * 1000,
			config: resolvedConfig,
		});
	}

	calculationCache[cacheKey] = animation;
	return animation;
}