Skip to content

🎥 Make videos programmatically with React

13.8 KB
Raw Blame
import {burlap} from '@remotion/effects/burlap';
import React, {useEffect, useLayoutEffect, useRef, useState} from 'react';
import {
	AbsoluteFill,
	Easing,
	Interactive,
	interpolate,
	interpolateColors,
	Solid,
	useCurrentFrame,
	useDelayRender,
	useVideoConfig,
} from 'remotion';

const springCodeBefore = `const progress =
	spring({fps, frame, durationInFrames: 20, config: {damping: 200}}) -
	spring({
		fps,
		frame,
		delay: durationInFrames - 20,
		durationInFrames: 20,
		config: {damping: 200},
	});

return (
	<Sequence from={30}>
		<Sequence from={-15}>
			<Video
				src="https://remotion.media/video.mp4"
				style={{
					translate: interpolate(progress, [0, 1], [0, 200]),
				}}
			/>
		</Sequence>
	</Sequence>
);`;

const combinedCodeAfter = `return (
	<Video
		src="https://remotion.media/video.mp4"
		trimBefore={15}
		from={30}
		style={{
			translate: interpolate(
				frame,
				[0, 20, durationInFrames - 20, durationInFrames],
				[0, 200, 200, 0],
				{
					easing: Easing.spring({damping: 200}),
				},
			),
		}}
	/>
);`;

type TokenKind =
	| 'whitespace'
	| 'keyword'
	| 'function'
	| 'number'
	| 'string'
	| 'tag'
	| 'attribute'
	| 'variable'
	| 'identifier'
	| 'operator'
	| 'punctuation'
	| 'comment';

type CodeToken = {
	readonly id: string;
	readonly value: string;
	readonly kind: TokenKind;
	readonly color: string;
	readonly visibleIndex: number | null;
};

type VisibleToken = CodeToken & {
	readonly visibleIndex: number;
};

type TokenizedCode = {
	readonly stream: CodeToken[];
	readonly visible: VisibleToken[];
};

type TokenPosition = {
	readonly x: number;
	readonly y: number;
};

type CodeLayouts = {
	readonly before: TokenPosition[];
	readonly after: TokenPosition[];
};

type CodeChangeData = {
	readonly before: TokenizedCode;
	readonly after: TokenizedCode;
	readonly afterIndexByBeforeIndex: Map<number, number>;
	readonly matchedAfterIndexes: Set<number>;
};

const getTokenColor = (kind: TokenKind) => {
	if (kind === 'keyword' || kind === 'operator') {
		return '#cf222e';
	}

	if (kind === 'function') {
		return '#8250df';
	}

	if (kind === 'number' || kind === 'variable' || kind === 'attribute') {
		return '#0550ae';
	}

	if (kind === 'string') {
		return '#0a3069';
	}

	if (kind === 'tag') {
		return '#116329';
	}

	if (kind === 'comment') {
		return '#6e7781';
	}

	if (kind === 'identifier') {
		return '#24292f';
	}

	return '#24292f';
};

const tokenizeCode = (code: string): TokenizedCode => {
	const stream: CodeToken[] = [];
	const visible: VisibleToken[] = [];
	let cursor = 0;

	while (cursor < code.length) {
		const remaining = code.slice(cursor);
		const whitespace = remaining.match(/^\s+/)?.[0];
		const comment = remaining.match(/^(?:\/\/[^\n]*|\/\*[\s\S]*?\*\/)/)?.[0];
		const string = remaining.match(
			/^(?:'(?:\\.|[^'\\])*'|"(?:\\.|[^"\\])*"|`(?:\\.|[^`\\])*`)/,
		)?.[0];
		const number = remaining.match(/^\d+(?:\.\d+)?/)?.[0];
		const identifier = remaining.match(/^[A-Za-z_$][\w$]*/)?.[0];
		const operator = remaining.match(
			/^(?:=>|===|!==|==|!=|<=|>=|\+\+|--|&&|\|\||\?\?|[+\-*%=!&|?])/,
		)?.[0];
		const punctuation = remaining.match(/^[{}()[\].,;:<>/]/)?.[0];

		let value = remaining[0];
		let kind: TokenKind = 'punctuation';

		if (whitespace) {
			value = whitespace;
			kind = 'whitespace';
		} else if (comment) {
			value = comment;
			kind = 'comment';
		} else if (string) {
			value = string;
			kind = 'string';
		} else if (number) {
			value = number;
			kind = 'number';
		} else if (identifier) {
			value = identifier;
			if (['const', 'return'].includes(identifier)) {
				kind = 'keyword';
			} else if (['spring', 'interpolate'].includes(identifier)) {
				kind = 'function';
			} else if (['div', 'Sequence', 'Video'].includes(identifier)) {
				kind = 'tag';
			} else if (['style', 'src', 'trimBefore', 'from'].includes(identifier)) {
				kind = 'attribute';
			} else if (identifier === 'progress') {
				kind = 'variable';
			} else {
				kind = 'identifier';
			}
		} else if (operator) {
			value = operator;
			kind = 'operator';
		} else if (punctuation) {
			value = punctuation;
		}

		if (kind === 'whitespace') {
			stream.push({
				id: `${cursor}-${value}`,
				value,
				kind,
				color: getTokenColor(kind),
				visibleIndex: null,
			});
		} else {
			const token: VisibleToken = {
				id: `${cursor}-${value}`,
				value,
				kind,
				color: getTokenColor(kind),
				visibleIndex: visible.length,
			};
			stream.push(token);
			visible.push(token);
		}

		cursor += value.length;
	}

	return {stream, visible};
};

const matchTokens = (
	before: VisibleToken[],
	after: VisibleToken[],
): [number, number][] => {
	const semanticTokenScore = before.length + after.length;
	const scores = Array.from({length: before.length + 1}, () =>
		Array<number>(after.length + 1).fill(0),
	);

	for (let row = before.length - 1; row >= 0; row--) {
		for (let column = after.length - 1; column >= 0; column--) {
			const tokensMatch =
				before[row].kind === after[column].kind &&
				before[row].value === after[column].value;
			// Keep meaningful tokens moving even when matching more punctuation would
			// produce a longer sequence.
			const matchScore =
				before[row].kind === 'punctuation' || before[row].kind === 'operator'
					? 1
					: semanticTokenScore;
			scores[row][column] = tokensMatch
				? scores[row + 1][column + 1] + matchScore
				: Math.max(scores[row + 1][column], scores[row][column + 1]);
		}
	}

	const matches: [number, number][] = [];
	let beforeIndex = 0;
	let afterIndex = 0;
	while (beforeIndex < before.length && afterIndex < after.length) {
		const tokensMatch =
			before[beforeIndex].kind === after[afterIndex].kind &&
			before[beforeIndex].value === after[afterIndex].value;
		if (tokensMatch) {
			matches.push([beforeIndex, afterIndex]);
			beforeIndex++;
			afterIndex++;
		} else if (
			scores[beforeIndex + 1][afterIndex] >= scores[beforeIndex][afterIndex + 1]
		) {
			beforeIndex++;
		} else {
			afterIndex++;
		}
	}

	return matches;
};

const renderTokenStream = (tokens: CodeToken[], prefix: string) => {
	return tokens.map((token) => {
		if (token.visibleIndex === null) {
			return (
				<React.Fragment key={`${prefix}-${token.id}`}>
					{token.value}
				</React.Fragment>
			);
		}

		return (
			<span
				key={`${prefix}-${token.id}`}
				data-token-index={token.visibleIndex}
				style={{color: token.color}}
			>
				{token.value}
			</span>
		);
	});
};

const prepareCodeChange = (before: string, after: string): CodeChangeData => {
	const beforeCode = tokenizeCode(before);
	const afterCode = tokenizeCode(after);
	const tokenMatches = matchTokens(beforeCode.visible, afterCode.visible);

	return {
		before: beforeCode,
		after: afterCode,
		afterIndexByBeforeIndex: new Map(tokenMatches),
		matchedAfterIndexes: new Set(
			tokenMatches.map(([, afterIndex]) => afterIndex),
		),
	};
};

const combinedCodeChange = prepareCodeChange(
	springCodeBefore,
	combinedCodeAfter,
);

const CodeChange: React.FC<{readonly codeChange: CodeChangeData}> = ({
	codeChange,
}) => {
	const frame = useCurrentFrame();
	const posterizedFrame = Math.floor(frame / 3) * 3;
	const {height} = useVideoConfig();
	const beforeMeasurementRef = useRef<HTMLPreElement>(null);
	const afterMeasurementRef = useRef<HTMLPreElement>(null);
	const [layouts, setLayouts] = useState<CodeLayouts | null>(null);
	const {delayRender, continueRender} = useDelayRender();
	const [handle] = useState(() =>
		delayRender('Measuring code token positions'),
	);
	const hasContinued = useRef(false);

	useLayoutEffect(() => {
		if (!beforeMeasurementRef.current || !afterMeasurementRef.current) {
			return;
		}

		const verticalOffset =
			(height - beforeMeasurementRef.current.offsetHeight) / 2;

		const measure = (
			pre: HTMLPreElement,
			tokenCount: number,
		): TokenPosition[] => {
			const preBounds = pre.getBoundingClientRect();
			const measurementScaleX = preBounds.width / pre.offsetWidth;
			const measurementScaleY = preBounds.height / pre.offsetHeight;
			const positions = Array.from({length: tokenCount}, () => ({x: 0, y: 0}));
			const elements = pre.querySelectorAll<HTMLElement>('[data-token-index]');

			elements.forEach((element) => {
				const index = Number(element.dataset.tokenIndex);
				const bounds = element.getBoundingClientRect();
				positions[index] = {
					x: (bounds.left - preBounds.left) / measurementScaleX,
					y:
						verticalOffset +
						(bounds.top - preBounds.top) / measurementScaleY,
				};
			});

			return positions;
		};

		setLayouts({
			before: measure(
				beforeMeasurementRef.current,
				codeChange.before.visible.length,
			),
			after: measure(
				afterMeasurementRef.current,
				codeChange.after.visible.length,
			),
		});
	}, [codeChange, height]);

	useEffect(() => {
		if (layouts && !hasContinued.current) {
			hasContinued.current = true;
			continueRender(handle);
		}
	}, [continueRender, handle, layouts]);

	return (
		<Interactive.Div
			name="Code"
			style={{
				position: 'absolute',
				left: 148,
				top: 0,
				right: 148,
				bottom: 0,
				fontFamily:
					'SFMono-Regular, Consolas, Liberation Mono, Menlo, monospace',
				fontSize: 34,
				lineHeight: 1.35,
				tabSize: 2,
			}}
		>
			<pre
				ref={beforeMeasurementRef}
				aria-hidden="true"
				style={{
					position: 'absolute',
					left: 0,
					top: 0,
					width: 'max-content',
					margin: 0,
					font: 'inherit',
					lineHeight: 'inherit',
					whiteSpace: 'pre',
					visibility: 'hidden',
					pointerEvents: 'none',
				}}
			>
				{renderTokenStream(codeChange.before.stream, 'before')}
			</pre>
			<pre
				ref={afterMeasurementRef}
				aria-hidden="true"
				style={{
					position: 'absolute',
					left: 0,
					top: 0,
					width: 'max-content',
					margin: 0,
					font: 'inherit',
					lineHeight: 'inherit',
					whiteSpace: 'pre',
					visibility: 'hidden',
					pointerEvents: 'none',
				}}
			>
				{renderTokenStream(codeChange.after.stream, 'after')}
			</pre>

			{layouts
				? codeChange.before.visible.map((token, beforeIndex) => {
						const beforePosition = layouts.before[beforeIndex];
						const afterIndex =
							codeChange.afterIndexByBeforeIndex.get(beforeIndex);

						if (afterIndex === undefined) {
							const exitAngle = beforeIndex * Math.PI * (3 - Math.sqrt(5));
							const exitDistance = 2400;
							return (
								<span
									key={`removed-${token.id}`}
									style={{
										position: 'absolute',
										left: 0,
										top: 0,
										display: 'inline-block',
										whiteSpace: 'pre',
										color: token.color,
										rotate: `${interpolate(
											posterizedFrame,
											[90, 108],
											[0, beforeIndex % 2 === 0 ? 720 : -720],
											{
												easing: Easing.bezier(0.4, 0, 1, 1),
												extrapolateLeft: 'clamp',
												extrapolateRight: 'clamp',
											},
										)}deg`,
										translate: `${interpolate(
											posterizedFrame,
											[90, 108],
											[
												beforePosition.x,
												beforePosition.x + Math.cos(exitAngle) * exitDistance,
											],
											{
												easing: Easing.bezier(0.4, 0, 1, 1),
												extrapolateLeft: 'clamp',
												extrapolateRight: 'clamp',
											},
										)}px ${interpolate(
											posterizedFrame,
											[90, 108],
											[
												beforePosition.y,
												beforePosition.y + Math.sin(exitAngle) * exitDistance,
											],
											{
												easing: Easing.bezier(0.4, 0, 1, 1),
												extrapolateLeft: 'clamp',
												extrapolateRight: 'clamp',
											},
										)}px`,
									}}
								>
									{token.value}
								</span>
							);
						}

						const afterToken = codeChange.after.visible[afterIndex];
						const afterPosition = layouts.after[afterIndex];
						return (
							<span
								key={`matched-${token.id}-${afterToken.id}`}
								style={{
									position: 'absolute',
									left: 0,
									top: 0,
									display: 'inline-block',
									whiteSpace: 'pre',
									color:
										token.color === afterToken.color
											? token.color
											: interpolateColors(
												frame,
												[90, 108],
												[token.color, afterToken.color],
											),
									translate: `${interpolate(
										posterizedFrame,
										[90, 108],
										[beforePosition.x, afterPosition.x],
										{
											easing: Easing.spring({damping: 200}),
											extrapolateLeft: 'clamp',
											extrapolateRight: 'clamp',
										},
									)}px ${interpolate(
										posterizedFrame,
										[90, 108],
										[beforePosition.y, afterPosition.y],
										{
											easing: Easing.spring({damping: 200}),
											extrapolateLeft: 'clamp',
											extrapolateRight: 'clamp',
										},
									)}px`,
								}}
							>
								{token.value}
							</span>
						);
					})
				: null}

			{layouts
				? codeChange.after.visible.map((token, afterIndex) => {
						if (codeChange.matchedAfterIndexes.has(afterIndex)) {
							return null;
						}

						const afterPosition = layouts.after[afterIndex];
						return (
							<span
								key={`added-${token.id}`}
								style={{
									position: 'absolute',
									left: 0,
									top: 0,
									display: 'inline-block',
									whiteSpace: 'pre',
									color: token.color,
									opacity: interpolate(frame, [108, 126], [0, 1], {
										easing: Easing.bezier(0, 0, 0.2, 1),
										extrapolateLeft: 'clamp',
										extrapolateRight: 'clamp',
									}),
									translate: `${afterPosition.x}px ${afterPosition.y}px`,
								}}
							>
								{token.value}
							</span>
						);
					})
				: null}
		</Interactive.Div>
	);
};

export const Skills2CodeChange: React.FC = () => {
	return (
		<AbsoluteFill style={{backgroundColor: '#f5fafb'}}>
			<Solid
				width={1920}
				height={1920}
				color="#f5fafb"
				style={{position: 'absolute'}}
				effects={[
					burlap({
						size: 10,
						roughness: 0,
						color: '#e9e9e9',
					}),
				]}
			/>
			<CodeChange codeChange={combinedCodeChange} />
		</AbsoluteFill>
	);
};