Skip to content

🎥 Make videos programmatically with React

`remotion`: Prevent `<HtmlInCanvas>` from flashing unprocessed HTML before effects finish

JonnyBurger3e82836e148f
diff --git a/packages/core/src/HtmlInCanvas.tsx b/packages/core/src/HtmlInCanvas.tsx
index 53f000a711..8fc794cbfa 100644
--- a/packages/core/src/HtmlInCanvas.tsx
+++ b/packages/core/src/HtmlInCanvas.tsx
@@ -607,6 +607,10 @@ const HtmlInCanvasContent = forwardRef<
const initializedRef = useRef(false);
const onInitCleanupRef = useRef<HtmlInCanvasOnInitCleanup | null>(null);
const unmountedRef = useRef(false);
+ const effectScratchCanvasesRef = useRef<{
+ source: HTMLCanvasElement;
+ previousOutput: HTMLCanvasElement;
+ } | null>(null);
const paintingTargets = useRef(
new WeakMap<HtmlInCanvasPaintTarget, {repaint: boolean}>(),
);
@@ -747,6 +751,7 @@ const HtmlInCanvasContent = forwardRef<
throw error;
}
+ let effectSourceCanvas: HTMLCanvasElement | null = null;
try {
const currentOnPaint = onPaintRef.current;
if (currentOnPaint) {
@@ -765,6 +770,55 @@ const HtmlInCanvasContent = forwardRef<
if (paintResult) {
await paintResult;
}
+ } else if (
+ paintTarget instanceof HTMLCanvasElement &&
+ effectsRef.current.some(
+ (effect) => !(effect.params as {disabled?: boolean}).disabled,
+ )
+ ) {
+ // The element image can only be drawn into the visible layout
+ // canvas, but effects finish asynchronously (e.g. while the
+ // WebGPU device initializes). Move the unprocessed pixels to a
+ // scratch canvas and restore the previous output within this task,
+ // so the browser never presents the HTML without its effects.
+ effectScratchCanvasesRef.current ??= {
+ source: document.createElement('canvas'),
+ previousOutput: document.createElement('canvas'),
+ };
+ const {source: effectSource, previousOutput} =
+ effectScratchCanvasesRef.current;
+ resizePaintTarget({
+ target: effectSource,
+ width: canvasWidth,
+ height: canvasHeight,
+ });
+ resizePaintTarget({
+ target: previousOutput,
+ width: canvasWidth,
+ height: canvasHeight,
+ });
+ const effectSourceCtx = effectSource.getContext('2d');
+ const previousOutputCtx = previousOutput.getContext('2d');
+ const paintTargetCtx = paintTarget.getContext('2d');
+ if (!effectSourceCtx || !previousOutputCtx || !paintTargetCtx) {
+ throw new Error(
+ 'Failed to acquire 2D context for <HtmlInCanvas> canvas',
+ );
+ }
+
+ previousOutputCtx.reset();
+ previousOutputCtx.drawImage(paintTarget, 0, 0);
+ defaultOnPaint({
+ canvas: paintTarget,
+ element,
+ elementImage: elImage,
+ pixelDensity: resolvedPixelDensity,
+ });
+ effectSourceCtx.reset();
+ effectSourceCtx.drawImage(paintTarget, 0, 0);
+ paintTargetCtx.reset();
+ paintTargetCtx.drawImage(previousOutput, 0, 0);
+ effectSourceCanvas = effectSource;
} else {
defaultOnPaint({
canvas: paintTarget,
@@ -779,7 +833,7 @@ const HtmlInCanvasContent = forwardRef<
if (state) {
await runEffectChain({
state,
- source: paintTarget,
+ source: effectSourceCanvas ?? paintTarget,
effects: effectsRef.current,
output: paintTarget,
width: canvasWidth,