🎥 Make videos programmatically with React
import type {LogLevel} from 'remotion';
import type {TransformStyleCache} from './drawing/calculate-transforms';
import type {InternalState} from './internal-state';
import {createTreeWalkerCleanupAfterChildren} from './tree-walker-cleanup-after-children';
import {walkOverNode} from './walk-over-node';
import {skipToNextNonDescendant} from './walk-tree';
const getFilterFunction = (node: Node) => {
if (!(node instanceof Element)) {
// Must be a text node!
return NodeFilter.FILTER_ACCEPT;
}
// SVG does have children, but we process SVG elements in its
// entirety
if (node.parentElement instanceof SVGSVGElement) {
return NodeFilter.FILTER_REJECT;
}
// Canvas children are fallback/layout content. The canvas bitmap already
// contains their rendered output, including any <HtmlInCanvas> effects.
if (node.parentElement instanceof HTMLCanvasElement) {
return NodeFilter.FILTER_REJECT;
}
const computedStyle = getComputedStyle(node);
if (computedStyle.display === 'none') {
return NodeFilter.FILTER_REJECT;
}
return NodeFilter.FILTER_ACCEPT;
};
export const compose = async ({
element,
context,
logLevel,
parentRect,
internalState,
onlyBackgroundClipText,
scale,
waitForPageResponsiveness,
}: {
element: HTMLElement | SVGElement;
context: OffscreenCanvasRenderingContext2D;
logLevel: LogLevel;
parentRect: DOMRect;
internalState: InternalState;
onlyBackgroundClipText: boolean;
scale: number;
waitForPageResponsiveness: (() => Promise<void>) | null;
}) => {
const treeWalker = document.createTreeWalker(
element,
onlyBackgroundClipText
? NodeFilter.SHOW_TEXT
: NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT,
getFilterFunction,
);
const transformStyleCache: TransformStyleCache = new WeakMap();
// Skip to the first text node
if (onlyBackgroundClipText) {
treeWalker.nextNode();
if (!treeWalker.currentNode) {
return;
}
}
using treeWalkerClean = createTreeWalkerCleanupAfterChildren(treeWalker);
const {checkCleanUpAtBeginningOfIteration, addCleanup} = treeWalkerClean;
while (true) {
checkCleanUpAtBeginningOfIteration();
const val = await walkOverNode({
node: treeWalker.currentNode,
context,
logLevel,
parentRect,
internalState,
rootElement: element,
onlyBackgroundClipText,
scale,
waitForPageResponsiveness,
transformStyleCache,
});
if (val.type === 'skip-children') {
if (!skipToNextNonDescendant(treeWalker)) {
break;
}
} else {
if (val.cleanupAfterChildren) {
addCleanup(treeWalker.currentNode, val.cleanupAfterChildren);
}
if (!treeWalker.nextNode()) {
break;
}
}
if (waitForPageResponsiveness !== null) {
await waitForPageResponsiveness();
}
}
};