Skip to content

πŸŽ₯ Make videos programmatically with React

alright

JonnyBurger17b0875fb9ea
diff --git a/.claude/commands/update-chrome-binaries-test-region.md b/.claude/commands/update-chrome-binaries-test-region.md
index 5259fcd8bc..ae320ec086 100644
--- a/.claude/commands/update-chrome-binaries-test-region.md
+++ b/.claude/commands/update-chrome-binaries-test-region.md
@@ -10,4 +10,6 @@
- `packages/docs/docs/lambda/runtime.mdx`: prepend a new row to the "Chrome" version table for the next Remotion release. Determine the next version with `cat packages/core/package.json | grep '"version"'` and increment the patch.
- `packages/docs/docs/miscellaneous/chrome-headless-shell.mdx`: prepend a new row to the version table, and update the example version string in the "Version tracking" section.
- `packages/docs/docs/renderer/ensure-browser.mdx`: update both `version: '<old>'` occurrences in the example code blocks.
+- Run the Docker matrix tests in `packages/dockerfiles/` (`./run.sh`) against the new Chrome binary. Each Dockerfile renders two compositions from the `BrowserTestRoot`: `browser-test` (Three.js / WebGL / codec smoke test) and `html-in-canvas` (experimental `drawElementImage()` API β€” falls back to a "not supported" frame if the flag/version is unavailable). Outputs land in `packages/dockerfiles/out/<platform>.mp4` and `packages/dockerfiles/out/<platform>-html-in-canvas.mp4`. Make sure Docker Desktop is running first.
+- If a new composition is added to the Docker test matrix, register it in `packages/example/src/BrowserTestRoot.tsx` (so it is included in the bundle built from `src/browser-test-entry.ts`) and add a corresponding `RUN npx remotion render /usr/app/bundle <id> /usr/app/<filename>.mp4` line + `docker cp` extraction in `run.sh`.
- Do not proceed to publishing all regions until the test region has been verified end-to-end.
diff --git a/packages/dockerfiles/Dockerfile.al2023 b/packages/dockerfiles/Dockerfile.al2023
index 3b0162d07c..c06395738a 100644
--- a/packages/dockerfiles/Dockerfile.al2023
+++ b/packages/dockerfiles/Dockerfile.al2023
@@ -31,3 +31,4 @@ COPY bundle/ /usr/app/bundle/
RUN npx remotion compositions /usr/app/bundle
RUN npx remotion render /usr/app/bundle browser-test /usr/app/out.mp4
+RUN npx remotion render /usr/app/bundle html-in-canvas /usr/app/out-html-in-canvas.mp4
diff --git a/packages/dockerfiles/Dockerfile.debian b/packages/dockerfiles/Dockerfile.debian
index cd81486259..b3fffc6a23 100644
--- a/packages/dockerfiles/Dockerfile.debian
+++ b/packages/dockerfiles/Dockerfile.debian
@@ -31,3 +31,4 @@ COPY bundle/ /usr/app/bundle/
RUN npx remotion compositions /usr/app/bundle
RUN npx remotion render /usr/app/bundle browser-test /usr/app/out.mp4
+RUN npx remotion render /usr/app/bundle html-in-canvas /usr/app/out-html-in-canvas.mp4
diff --git a/packages/dockerfiles/Dockerfile.ubuntu22 b/packages/dockerfiles/Dockerfile.ubuntu22
index 477b60713b..317db988d3 100644
--- a/packages/dockerfiles/Dockerfile.ubuntu22
+++ b/packages/dockerfiles/Dockerfile.ubuntu22
@@ -29,4 +29,5 @@ RUN node ensure.mjs
COPY bundle/ /usr/app/bundle/
RUN npx remotion compositions /usr/app/bundle
-RUN npx remotion render /usr/app/bundle browser-test /usr/app/out.mp4 --log=verbose
\ No newline at end of file
+RUN npx remotion render /usr/app/bundle browser-test /usr/app/out.mp4 --log=verbose
+RUN npx remotion render /usr/app/bundle html-in-canvas /usr/app/out-html-in-canvas.mp4 --log=verbose
\ No newline at end of file
diff --git a/packages/dockerfiles/Dockerfile.ubuntu24 b/packages/dockerfiles/Dockerfile.ubuntu24
index d280976b4d..9c3882f7d6 100644
--- a/packages/dockerfiles/Dockerfile.ubuntu24
+++ b/packages/dockerfiles/Dockerfile.ubuntu24
@@ -29,4 +29,5 @@ RUN node ensure.mjs
COPY bundle/ /usr/app/bundle/
RUN npx remotion compositions /usr/app/bundle
-RUN npx remotion render /usr/app/bundle browser-test /usr/app/out.mp4 --log=verbose
\ No newline at end of file
+RUN npx remotion render /usr/app/bundle browser-test /usr/app/out.mp4 --log=verbose
+RUN npx remotion render /usr/app/bundle html-in-canvas /usr/app/out-html-in-canvas.mp4 --log=verbose
\ No newline at end of file
diff --git a/packages/dockerfiles/README.md b/packages/dockerfiles/README.md
index 517ef69b18..9baab479d7 100644
--- a/packages/dockerfiles/README.md
+++ b/packages/dockerfiles/README.md
@@ -12,15 +12,15 @@ This will:
1. Build the browser-test bundle from `packages/example` (using `src/browser-test-entry.ts`)
2. Build Docker images for each platform (ubuntu24, ubuntu22, debian, nix)
-3. Each Docker build runs `npx remotion compositions` and `npx remotion render`
+3. Each Docker build runs `npx remotion compositions` and renders **two** compositions: `browser-test` and `html-in-canvas`
4. Extract the rendered videos to `out/` directory
-Output videos:
+Output videos (per platform β€” `<platform>.mp4` and `<platform>-html-in-canvas.mp4`):
-- `out/ubuntu24.mp4`
-- `out/ubuntu22.mp4`
-- `out/debian.mp4`
-- `out/nix.mp4`
+- `out/ubuntu24.mp4`, `out/ubuntu24-html-in-canvas.mp4`
+- `out/ubuntu22.mp4`, `out/ubuntu22-html-in-canvas.mp4`
+- `out/debian.mp4`, `out/debian-html-in-canvas.mp4`
+- `out/nix.mp4`, `out/nix-html-in-canvas.mp4`
## browser-test composition
@@ -30,4 +30,8 @@ Located in `packages/example/src/BrowserTest/index.tsx`, it tests:
- Video playback with different codecs (H.264, H.265)
- WebGL availability
+## html-in-canvas composition
+
+Located in `packages/example/src/HtmlInCanvas/index.tsx`, it tests the experimental [WICG html-in-canvas](https://github.com/WICG/html-in-canvas) `CanvasRenderingContext2D.drawElementImage()` API. If the API is unavailable in the bundled Chrome (e.g. without `chrome://flags/#canvas-draw-element` or Chrome Canary), the composition renders a fallback "not supported" frame instead of failing.
+
The bundle is built from `packages/example` and copied into each Docker container during build.
diff --git a/packages/dockerfiles/run.sh b/packages/dockerfiles/run.sh
index c150b5b365..cb7337e42f 100644
--- a/packages/dockerfiles/run.sh
+++ b/packages/dockerfiles/run.sh
@@ -18,6 +18,8 @@ mkdir -p out
# Function to build and extract video
# Usage: build_and_extract <dockerfile> <tag> <output_name> [platform]
# platform is optional, e.g., "linux/amd64" for x86 emulation
+# Extracts both the browser-test video (out/<output_name>) and the
+# html-in-canvas video (out/<output_name without .mp4>-html-in-canvas.mp4).
build_and_extract() {
local dockerfile=$1
local tag=$2
@@ -34,11 +36,12 @@ build_and_extract() {
docker build $platform_flag --file $dockerfile -t $tag .
- echo "Extracting video from $tag..."
+ echo "Extracting videos from $tag..."
container_id=$(docker create $platform_flag $tag)
docker cp $container_id:/usr/app/out.mp4 ./out/$output_name
+ docker cp $container_id:/usr/app/out-html-in-canvas.mp4 ./out/${output_name%.mp4}-html-in-canvas.mp4
docker rm $container_id
- echo "Video saved to out/$output_name"
+ echo "Videos saved to out/$output_name and out/${output_name%.mp4}-html-in-canvas.mp4"
}
# x86 (amd64) emulation builds - useful for testing on ARM machines
diff --git a/packages/example/src/BrowserTestRoot.tsx b/packages/example/src/BrowserTestRoot.tsx
index cc101a6768..842cd01fb1 100644
--- a/packages/example/src/BrowserTestRoot.tsx
+++ b/packages/example/src/BrowserTestRoot.tsx
@@ -1,16 +1,27 @@
import React from 'react';
import {Composition} from 'remotion';
import {BrowserTest} from './BrowserTest';
+import {HtmlInCanvasDemo} from './HtmlInCanvas';
export const BrowserTestRoot: React.FC = () => {
return (
- <Composition
- id="browser-test"
- component={BrowserTest}
- width={1280}
- height={720}
- fps={30}
- durationInFrames={2 * 60 * 30}
- />
+ <>
+ <Composition
+ id="browser-test"
+ component={BrowserTest}
+ width={1280}
+ height={720}
+ fps={30}
+ durationInFrames={2 * 60 * 30}
+ />
+ <Composition
+ id="html-in-canvas"
+ component={HtmlInCanvasDemo}
+ fps={30}
+ height={1080}
+ width={1920}
+ durationInFrames={120}
+ />
+ </>
);
};
diff --git a/packages/example/src/HtmlInCanvas/index.tsx b/packages/example/src/HtmlInCanvas/index.tsx
index a299903776..f3085eb39f 100644
--- a/packages/example/src/HtmlInCanvas/index.tsx
+++ b/packages/example/src/HtmlInCanvas/index.tsx
@@ -1,10 +1,9 @@
-import React, {useEffect, useRef, useState} from 'react';
+import React, {useEffect, useRef} from 'react';
import {
AbsoluteFill,
cancelRender,
continueRender,
delayRender,
- interpolate,
useCurrentFrame,
useVideoConfig,
} from 'remotion';
@@ -12,10 +11,8 @@ import {
// Type augmentation for the WICG html-in-canvas proposal:
// https://github.com/WICG/html-in-canvas
//
-// In Chrome, requires enabling chrome://flags/#canvas-draw-element.
-// canvas.requestPaint() is currently only available in Chrome Canary; on
-// regular Chrome we fall back to a double-requestAnimationFrame to give the
-// browser a chance to lay out and paint after React commits.
+// Requires Chrome Canary with chrome://flags/#canvas-draw-element enabled, so
+// that both drawElementImage() and canvas.requestPaint() are available.
//
// The current Chromium implementation requires the element passed to
// drawElementImage() to be an immediate child of the canvas, and the canvas
@@ -33,7 +30,7 @@ type Canvas2DWithDrawElement = CanvasRenderingContext2D & {
type HTMLCanvasWithLayoutSubtree = HTMLCanvasElement & {
layoutSubtree?: boolean;
- requestPaint?: () => void;
+ requestPaint: () => void;
};
const isHtmlInCanvasSupported = () => {
@@ -41,10 +38,14 @@ const isHtmlInCanvasSupported = () => {
return false;
}
- const ctx = document
- .createElement('canvas')
- .getContext('2d') as Canvas2DWithDrawElement | null;
- return typeof ctx?.drawElementImage === 'function';
+ const canvas = document.createElement(
+ 'canvas',
+ ) as HTMLCanvasWithLayoutSubtree;
+ const ctx = canvas.getContext('2d') as Canvas2DWithDrawElement | null;
+ return (
+ typeof ctx?.drawElementImage === 'function' &&
+ typeof canvas.requestPaint === 'function'
+ );
};
const Scene: React.FC = () => {
@@ -173,17 +174,16 @@ export const HtmlInCanvasDemo: React.FC = () => {
const sceneRef = useRef<HTMLDivElement | null>(null);
const outputCanvasRef = useRef<HTMLCanvasElement | null>(null);
- const [supported, setSupported] = useState<boolean | null>(null);
-
- useEffect(() => {
- setSupported(isHtmlInCanvasSupported());
- }, []);
-
// Mark the source canvas as a layoutSubtree root once on mount so its
// child <div ref={sceneRef}> participates in layout/paint and can be
// captured via drawElementImage().
useEffect(() => {
- if (!supported) {
+ if (!isHtmlInCanvasSupported()) {
+ cancelRender(
+ new Error(
+ 'HTML in Canvas is not supported. Open this page in Chrome Canary with chrome://flags/#canvas-draw-element enabled.',
+ ),
+ );
return;
}
@@ -194,13 +194,9 @@ export const HtmlInCanvasDemo: React.FC = () => {
}
sourceCanvas.layoutSubtree = true;
- }, [supported]);
+ }, []);
useEffect(() => {
- if (!supported) {
- return;
- }
-
const sourceCanvas =
sourceCanvasRef.current as HTMLCanvasWithLayoutSubtree | null;
const sceneEl = sceneRef.current;
@@ -210,7 +206,7 @@ export const HtmlInCanvasDemo: React.FC = () => {
}
const ctx = sourceCanvas.getContext('2d') as Canvas2DWithDrawElement | null;
- if (!ctx || typeof ctx.drawElementImage !== 'function') {
+ if (!ctx) {
return;
}
@@ -243,90 +239,25 @@ export const HtmlInCanvasDemo: React.FC = () => {
}
};
- if (typeof sourceCanvas.requestPaint === 'function') {
- const onPaint = () => {
- sourceCanvas.removeEventListener('paint', onPaint);
- capture();
- };
-
- sourceCanvas.addEventListener('paint', onPaint);
- sourceCanvas.requestPaint();
-
- return () => {
- cancelled = true;
- sourceCanvas.removeEventListener('paint', onPaint);
- if (!resolved) {
- continueRender(handle);
- }
- };
- }
+ const onPaint = () => {
+ sourceCanvas.removeEventListener('paint', onPaint);
+ capture();
+ };
- const rafIds: number[] = [];
- rafIds.push(
- requestAnimationFrame(() => {
- rafIds.push(requestAnimationFrame(capture));
- }),
- );
+ sourceCanvas.addEventListener('paint', onPaint);
+ sourceCanvas.requestPaint();
return () => {
cancelled = true;
- for (const id of rafIds) {
- cancelAnimationFrame(id);
- }
-
+ sourceCanvas.removeEventListener('paint', onPaint);
if (!resolved) {
continueRender(handle);
}
};
- }, [frame, width, height, supported]);
-
- if (supported === false) {
- return (
- <AbsoluteFill
- style={{
- backgroundColor: '#111',
- color: 'white',
- alignItems: 'center',
- justifyContent: 'center',
- padding: 80,
- fontFamily:
- '-apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif',
- textAlign: 'center',
- }}
- >
- <div style={{fontSize: 64, fontWeight: 800, marginBottom: 24}}>
- HTML in Canvas not supported
- </div>
- <div
- style={{
- fontSize: 32,
- lineHeight: 1.4,
- maxWidth: 1200,
- opacity: 0.85,
- }}
- >
- This demo uses the experimental{' '}
- <code>CanvasRenderingContext2D.drawElementImage()</code> API
- (WICG/html-in-canvas). Enable{' '}
- <code>chrome://flags/#canvas-draw-element</code> in Chrome (or Chrome
- Canary for full support including <code>canvas.requestPaint()</code>)
- and reload to view it.
- </div>
- </AbsoluteFill>
- );
- }
+ }, [frame, width, height]);
return (
<AbsoluteFill style={{backgroundColor: 'black'}}>
- {/*
- * The source canvas hosts the scene as an immediate child. Its
- * `layoutSubtree` property (set imperatively after mount) tells
- * Chromium to lay out and paint the children. We then capture them
- * via drawElementImage() and draw the result as the canvas's bitmap.
- *
- * Note: drawElementImage() currently requires the element to be an
- * immediate child of the canvas being drawn onto.
- */}
<canvas
ref={sourceCanvasRef}
width={width}