Skip to content

πŸŽ₯ Make videos programmatically with React

remotion: Add `HtmlInCanvas.isNestingSupported()` for nested canvases

JonnyBurger2225bad55d39
diff --git a/packages/codex-plugin/skills/remotion-best-practices/remotion-markup/html-in-canvas.md b/packages/codex-plugin/skills/remotion-best-practices/remotion-markup/html-in-canvas.md
index 33e2f15140..d6eda80a54 100644
--- a/packages/codex-plugin/skills/remotion-best-practices/remotion-markup/html-in-canvas.md
+++ b/packages/codex-plugin/skills/remotion-best-practices/remotion-markup/html-in-canvas.md
@@ -7,7 +7,7 @@ Give the user a notice.
## Nesting
-Do not nest `<HtmlInCanvas>` components. Remotion rejects nesting because Chrome does not reliably render nested HTML-in-canvas subtrees.
+Nest `<HtmlInCanvas>` components only when `HtmlInCanvas.isNestingSupported()` returns `true` (Chrome 157 or newer with HTML-in-canvas enabled). Older browsers reject nesting.
## Enabling WebGL during renders
diff --git a/packages/codex-plugin/skills/remotion-markup/html-in-canvas.md b/packages/codex-plugin/skills/remotion-markup/html-in-canvas.md
index 33e2f15140..d6eda80a54 100644
--- a/packages/codex-plugin/skills/remotion-markup/html-in-canvas.md
+++ b/packages/codex-plugin/skills/remotion-markup/html-in-canvas.md
@@ -7,7 +7,7 @@ Give the user a notice.
## Nesting
-Do not nest `<HtmlInCanvas>` components. Remotion rejects nesting because Chrome does not reliably render nested HTML-in-canvas subtrees.
+Nest `<HtmlInCanvas>` components only when `HtmlInCanvas.isNestingSupported()` returns `true` (Chrome 157 or newer with HTML-in-canvas enabled). Older browsers reject nesting.
## Enabling WebGL during renders
diff --git a/packages/core/src/HtmlInCanvas.tsx b/packages/core/src/HtmlInCanvas.tsx
index f48e11f5e0..55de76973f 100644
--- a/packages/core/src/HtmlInCanvas.tsx
+++ b/packages/core/src/HtmlInCanvas.tsx
@@ -292,6 +292,22 @@ export const isHtmlInCanvasSupported = (): boolean => {
return cachedSupport;
};
+const MINIMUM_CHROME_VERSION_FOR_NESTED_HTML_IN_CANVAS = 157;
+
+export const isHtmlInCanvasNestingSupported = (): boolean => {
+ if (!isHtmlInCanvasSupported() || typeof navigator === 'undefined') {
+ return false;
+ }
+
+ const chromeVersion = navigator.userAgent.match(
+ /(?:Chrome|Chromium)\/(\d+)/,
+ )?.[1];
+ return (
+ chromeVersion !== undefined &&
+ Number(chromeVersion) >= MINIMUM_CHROME_VERSION_FOR_NESTED_HTML_IN_CANVAS
+ );
+};
+
/** Generic fallback for consumers that cannot inspect the current browser. */
export const HTML_IN_CANVAS_UNSUPPORTED_MESSAGE =
'HTML in Canvas requires Chrome 149 or newer with Canvas Draw Element enabled at chrome://flags/#canvas-draw-element.';
@@ -480,9 +496,9 @@ const HtmlInCanvasContent = forwardRef<
HtmlInCanvasAncestorContext,
);
assertHtmlInCanvasDimensions(width, height);
- if (isInsideAncestorHtmlInCanvas) {
+ if (isInsideAncestorHtmlInCanvas && !isHtmlInCanvasNestingSupported()) {
throw new Error(
- '<HtmlInCanvas> components cannot be nested. Chrome does not reliably render nested HTML-in-canvas subtrees. Consider merging the effects into one <HtmlInCanvas> if you can.',
+ `Nested <HtmlInCanvas> components require Chrome ${MINIMUM_CHROME_VERSION_FOR_NESTED_HTML_IN_CANVAS} or newer with HTML-in-canvas enabled.`,
);
}
@@ -960,10 +976,12 @@ const HtmlInCanvasWrapped = withInteractivitySchema({
export const HtmlInCanvas = Object.assign(HtmlInCanvasWrapped, {
isSupported: isHtmlInCanvasSupported,
+ isNestingSupported: isHtmlInCanvasNestingSupported,
}) as React.ForwardRefExoticComponent<
HtmlInCanvasProps & React.RefAttributes<HTMLCanvasElement>
> & {
readonly isSupported: typeof isHtmlInCanvasSupported;
+ readonly isNestingSupported: typeof isHtmlInCanvasNestingSupported;
};
HtmlInCanvas.displayName = 'HtmlInCanvas';
diff --git a/packages/core/src/test/html-in-canvas.test.tsx b/packages/core/src/test/html-in-canvas.test.tsx
index 9bd446a698..4e1b127799 100644
--- a/packages/core/src/test/html-in-canvas.test.tsx
+++ b/packages/core/src/test/html-in-canvas.test.tsx
@@ -336,7 +336,7 @@ test('<HtmlInCanvas> throws when nested', () => {
</HtmlInCanvas>
</SequenceTestWrapper>,
),
- ).toThrow('<HtmlInCanvas> components cannot be nested.');
+ ).toThrow('Nested <HtmlInCanvas> components require Chrome 157 or newer');
});
test('<HtmlInCanvas> keeps refs current when the canvas remounts', async () => {
diff --git a/packages/docs/docs/client-side-rendering/html-in-canvas.mdx b/packages/docs/docs/client-side-rendering/html-in-canvas.mdx
index f8ce8e32ab..f374083a82 100644
--- a/packages/docs/docs/client-side-rendering/html-in-canvas.mdx
+++ b/packages/docs/docs/client-side-rendering/html-in-canvas.mdx
@@ -17,7 +17,7 @@ On supported Chromium-based browsers, [`@remotion/web-renderer`](/docs/web-rende
Use a recent version of Google Chrome or another Chromium-based browser with `chrome://flags/#canvas-draw-element` enabled. Current Chrome stable releases ship `canvas.requestPaint()` together with [`drawElementImage`](https://github.com/WICG/html-in-canvas#2-drawelementimage-and-webglwebgpu-equivalents), which is required for correct frame capture. See [Compatibility](#compatibility) for details.
:::
-Enabling the option does not guarantee that this path runs. If the browser does not have HTML-in-canvas enabled, or if the composition contains an [`<HtmlInCanvas>`](/docs/remotion/html-in-canvas), it falls back to the [default frame capturing mechanism](/docs/client-side-rendering/how-it-works).
+Enabling the option does not guarantee that this path runs. If the browser does not have HTML-in-canvas enabled, or if the composition contains an [`<HtmlInCanvas>`](/docs/remotion/html-in-canvas) in Chrome earlier than 157, it falls back to the [default frame capturing mechanism](/docs/client-side-rendering/how-it-works).
## Enabling
@@ -67,7 +67,7 @@ If native capture starts but fails while capturing a frame, a warning is printed
Not using HTML-in-canvas: drawElementImage failed (...); falling back to the built-in DOM composer.
```
-If HTML-in-canvas is unavailable, disabled, or would require a nested capture, the built-in DOM composer is selected.
+If HTML-in-canvas is unavailable, disabled, or would require a nested capture in Chrome earlier than 157, the built-in DOM composer is selected.
## Compatibility
@@ -87,7 +87,7 @@ If HTML-in-canvas is unavailable, disabled, or would require a nested capture, t
HTML-in-canvas depends on Chromium flag `chrome://flags/#canvas-draw-element`, which must be enabled explicitly.
-Use a recent Chrome or Chromium version with the flag enabled for full support. Compositions containing [`<HtmlInCanvas>`](/docs/remotion/html-in-canvas) always use the built-in DOM composer because nested HTML-in-canvas captures are unsupported.
+Use a recent Chrome or Chromium version with the flag enabled for full support. Compositions containing [`<HtmlInCanvas>`](/docs/remotion/html-in-canvas) use native capture in Chrome 157 or newer. Earlier versions use the built-in DOM composer.
## See also
diff --git a/packages/docs/docs/client-side-rendering/limitations.mdx b/packages/docs/docs/client-side-rendering/limitations.mdx
index 4074df615c..d46564587d 100644
--- a/packages/docs/docs/client-side-rendering/limitations.mdx
+++ b/packages/docs/docs/client-side-rendering/limitations.mdx
@@ -130,7 +130,7 @@ The [`<Audio>`](/docs/media/audio) from `@remotion/media` is <a style={{color: '
[`<ThreeCanvas>`](/docs/three-canvas) from [`@remotion/three`](/docs/three) is <a style={{color: 'green'}}>supported</a>.
[`<SkiaCanvas>`](/docs/skia/skia-canvas) from [`@remotion/skia`](/docs/skia) is <a style={{color: 'green'}}>supported</a>.
[`<AnimatedImage>`](/docs/animatedimage) is <a style={{color: 'green'}}>supported</a> when rendering in Chrome.
-[`<HtmlInCanvas>`](/docs/remotion/html-in-canvas) is <a style={{color: 'green'}}>supported</a> when the component itself is supported, including its [`onPaint`](/docs/remotion/html-in-canvas#onpaint) and [`onInit`](/docs/remotion/html-in-canvas#oninit) callbacks. The optional [HTML-in-canvas capture mode](/docs/client-side-rendering/html-in-canvas) falls back to the built-in DOM composer for such compositions to avoid nesting.<br />
+[`<HtmlInCanvas>`](/docs/remotion/html-in-canvas) is <a style={{color: 'green'}}>supported</a> when the component itself is supported, including its [`onPaint`](/docs/remotion/html-in-canvas#onpaint) and [`onInit`](/docs/remotion/html-in-canvas#oninit) callbacks. The optional [HTML-in-canvas capture mode](/docs/client-side-rendering/html-in-canvas) supports nested capture in Chrome 157 or newer and falls back to the built-in DOM composer in older versions.<br />
[`<OffthreadVideo>`](/docs/offthreadvideo) is <a style={{color: 'red'}}>not supported</a>.
β†’ Use [`<Video>`](/docs/media/video) instead.
[`<Html5Video>`](/docs/html5-video) is <a style={{color: 'red'}}>not supported</a>.
diff --git a/packages/docs/docs/html-in-canvas-guide.mdx b/packages/docs/docs/html-in-canvas-guide.mdx
index 86969dc125..b2d392ffde 100644
--- a/packages/docs/docs/html-in-canvas-guide.mdx
+++ b/packages/docs/docs/html-in-canvas-guide.mdx
@@ -100,7 +100,7 @@ You can also build your own β€” see [Making a custom HTML-in-canvas presentation
HTML-in-canvas is only available in Chrome 149 and later with the `chrome://flags/#canvas-draw-element` flag enabled.
The API is unstable - Chrome may change the API or even remove it in the future.
-Nesting [`<HtmlInCanvas>`](/docs/remotion/html-in-canvas) components is not supported and throws an error. If you need to combine multiple effects, merge them into a single `onPaint` callback.
+Nesting [`<HtmlInCanvas>`](/docs/remotion/html-in-canvas) components requires Chrome 157 or newer with HTML-in-canvas enabled. See [Nested HTML-in-canvas](#nested-html-in-canvas).
## Rendering
@@ -126,7 +126,7 @@ If your machine has no GPU, we recommend `--gl=swangle` instead (default on Lamb
## AI agents
-Remotion publishes [Agent Skills](/docs/ai/skills) that teach AI agents like Claude Code, Codex and Cursor how to use [`<HtmlInCanvas>`](/docs/remotion/html-in-canvas) correctly β€” including the [`onInit`](/docs/remotion/html-in-canvas#oninit) / [`onPaint`](/docs/remotion/html-in-canvas#onpaint) lifecycle, the [`--gl=angle` flag](#rendering) and the [no-nesting rule](#limitations).
+Remotion publishes [Agent Skills](/docs/ai/skills) that teach AI agents like Claude Code, Codex and Cursor how to use [`<HtmlInCanvas>`](/docs/remotion/html-in-canvas) correctly β€” including the [`onInit`](/docs/remotion/html-in-canvas#oninit) / [`onPaint`](/docs/remotion/html-in-canvas#onpaint) lifecycle, the [`--gl=angle` flag](#rendering) and [nesting support](#nested-html-in-canvas).
Install them with:
@@ -138,11 +138,11 @@ npx skills add remotion-dev/skills
Not directly related, but [`@remotion/web-renderer`](/docs/client-side-rendering/html-in-canvas) can also use this technology to capture frames.
-This is opt-in because the mode cannot capture compositions containing [`<HtmlInCanvas>`](/docs/remotion/html-in-canvas): that would create a nested HTML-in-canvas subtree.
+This is opt-in. In Chrome versions earlier than 157, compositions containing [`<HtmlInCanvas>`](/docs/remotion/html-in-canvas) use the built-in DOM composer because native capture would create a nested HTML-in-canvas subtree.
## Nested HTML-in-canvas
-Nesting `<HtmlInCanvas>` components is unsupported in preview, client-side rendering, and server-side rendering.
+Nesting `<HtmlInCanvas>` components is supported in Chrome 157 or newer with HTML-in-canvas enabled. Use [`HtmlInCanvas.isNestingSupported()`](/docs/remotion/html-in-canvas#htmlincanvasisnestingsupported) to check at runtime. Older versions throw an error.
## See also
diff --git a/packages/docs/docs/motion-blur/html-in-canvas-motion-blur.mdx b/packages/docs/docs/motion-blur/html-in-canvas-motion-blur.mdx
index d47ef13222..22e4a7aa49 100644
--- a/packages/docs/docs/motion-blur/html-in-canvas-motion-blur.mdx
+++ b/packages/docs/docs/motion-blur/html-in-canvas-motion-blur.mdx
@@ -109,7 +109,7 @@ Samples outside the composition are clamped to its first or last frame.
## Nested HTML-in-canvas caveat
-Nested [`<HtmlInCanvas>`](/docs/remotion/html-in-canvas) components are currently unsupported, so children of `<HtmlInCanvasMotionBlur>` cannot contain another `<HtmlInCanvas>` component or vice versa.
+Children of `<HtmlInCanvasMotionBlur>` can contain another [`<HtmlInCanvas>`](/docs/remotion/html-in-canvas) component, or vice versa, in Chrome 157 or newer with HTML-in-canvas enabled. Check [`HtmlInCanvas.isNestingSupported()`](/docs/remotion/html-in-canvas#htmlincanvasisnestingsupported) before nesting them.
## Compatibility
diff --git a/packages/docs/docs/remotion/html-in-canvas.mdx b/packages/docs/docs/remotion/html-in-canvas.mdx
index b074147a00..ea23443962 100644
--- a/packages/docs/docs/remotion/html-in-canvas.mdx
+++ b/packages/docs/docs/remotion/html-in-canvas.mdx
@@ -42,6 +42,12 @@ if (HtmlInCanvas.isSupported()) {
}
```
+## `HtmlInCanvas.isNestingSupported()`<AvailableFrom v="4.0.531" />
+
+Return value: _boolean_
+
+Returns `true` when HTML-in-canvas is enabled in Chrome or a Chromium-based browser version 157 or newer. Use it before nesting `<HtmlInCanvas>` components. Older versions throw an error when components are nested.
+
## Using WebGL and WebGPU during renders
See [Using WebGL and WebGPU during renders](/docs/webgl).
@@ -840,7 +846,7 @@ const onPaint: HtmlInCanvasOnPaint = async ({canvas, elementImage}) => {
## Nested HTML-in-canvas
-Do not nest `<HtmlInCanvas>` inside another `<HtmlInCanvas>`. Nesting is unsupported and throws an error. If you need to combine multiple effects, merge them into one `<HtmlInCanvas>` if possible.
+You can nest `<HtmlInCanvas>` components in Chrome 157 or newer when HTML-in-canvas is enabled. Check [`HtmlInCanvas.isNestingSupported()`](#htmlincanvasisnestingsupported) before rendering nested components. In older versions, nesting throws an error.
## Compatibility
diff --git a/packages/motion-blur/src/HtmlInCanvasMotionBlur.tsx b/packages/motion-blur/src/HtmlInCanvasMotionBlur.tsx
index 3deb3be501..f91e78173b 100644
--- a/packages/motion-blur/src/HtmlInCanvasMotionBlur.tsx
+++ b/packages/motion-blur/src/HtmlInCanvasMotionBlur.tsx
@@ -264,7 +264,7 @@ const HtmlInCanvasMotionBlurInner: React.FC<
* Experimental motion blur that averages separately captured HTML-in-canvas
* snapshots of the children at fractional frames. Preview requires Chrome with
* the experimental HTML-in-canvas flag enabled. Nested HtmlInCanvas components
- * are currently unsupported.
+ * require Chrome 157 or newer.
*/
export const HtmlInCanvasMotionBlur = Interactive.withSchema<
typeof htmlInCanvasMotionBlurSchema,
diff --git a/packages/skills/skills/remotion-markup/html-in-canvas.md b/packages/skills/skills/remotion-markup/html-in-canvas.md
index 33e2f15140..d6eda80a54 100644
--- a/packages/skills/skills/remotion-markup/html-in-canvas.md
+++ b/packages/skills/skills/remotion-markup/html-in-canvas.md
@@ -7,7 +7,7 @@ Give the user a notice.
## Nesting
-Do not nest `<HtmlInCanvas>` components. Remotion rejects nesting because Chrome does not reliably render nested HTML-in-canvas subtrees.
+Nest `<HtmlInCanvas>` components only when `HtmlInCanvas.isNestingSupported()` returns `true` (Chrome 157 or newer with HTML-in-canvas enabled). Older browsers reject nesting.
## Enabling WebGL during renders
diff --git a/packages/skills/skills/remotion-markup/motion-blur.md b/packages/skills/skills/remotion-markup/motion-blur.md
index c5dd84cfb9..a81a23088f 100644
--- a/packages/skills/skills/remotion-markup/motion-blur.md
+++ b/packages/skills/skills/remotion-markup/motion-blur.md
@@ -55,6 +55,6 @@ export const HtmlInCanvasBlur = () => {
For live preview, enable HTML-in-canvas in Chrome 149 or later with `chrome://flags/#canvas-draw-element`.
No configuration for rendering is necessary.
-Do not nest `<HtmlInCanvas>` or `<HtmlInCanvasMotionBlur>` components; nested HTML-in-canvas is unsupported.
+Nest `<HtmlInCanvas>` or `<HtmlInCanvasMotionBlur>` components only when `HtmlInCanvas.isNestingSupported()` returns `true` (Chrome 157 or newer with HTML-in-canvas enabled).
If HTML-in-canvas is unavailable, see the [motion blur guide](https://www.remotion.dev/docs/motion-blur) for other options. See the [`<HtmlInCanvasMotionBlur>` reference](https://www.remotion.dev/docs/motion-blur/html-in-canvas-motion-blur) for API details.
diff --git a/packages/web-renderer/src/take-screenshot.ts b/packages/web-renderer/src/take-screenshot.ts
index 0fd9389921..df83608f4c 100644
--- a/packages/web-renderer/src/take-screenshot.ts
+++ b/packages/web-renderer/src/take-screenshot.ts
@@ -1,5 +1,5 @@
import type {LogLevel} from 'remotion';
-import {Internals} from 'remotion';
+import {HtmlInCanvas, Internals} from 'remotion';
import {compose} from './compose';
import {containsUrlMaskImage} from './drawing/mask-image';
import type {HtmlInCanvasContext} from './html-in-canvas';
@@ -50,11 +50,14 @@ export const createLayer = async ({
'URL masks are loaded by the built-in DOM composer to guarantee deterministic rendering.',
shouldWarn: false,
});
- } else if (containsLayoutSubtreeCanvas(element)) {
+ } else if (
+ containsLayoutSubtreeCanvas(element) &&
+ !HtmlInCanvas.isNestingSupported()
+ ) {
onHtmlInCanvasLayerOutcome({
native: false,
reason:
- 'The composition contains an <HtmlInCanvas> element. Nested HTML-in-canvas capture is unsupported, so the built-in DOM composer is used.',
+ 'The composition contains an <HtmlInCanvas> element. Nested HTML-in-canvas capture requires Chrome 157 or newer, so the built-in DOM composer is used.',
shouldWarn: false,
});
} else {
diff --git a/packages/web-renderer/src/test/html-in-canvas.test.tsx b/packages/web-renderer/src/test/html-in-canvas.test.tsx
index 2b2adaa541..2aa4fea226 100644
--- a/packages/web-renderer/src/test/html-in-canvas.test.tsx
+++ b/packages/web-renderer/src/test/html-in-canvas.test.tsx
@@ -167,7 +167,7 @@ test('does not create a nested HTML-in-canvas capture', async () => {
expect(outcome).toEqual({
native: false,
reason:
- 'The composition contains an <HtmlInCanvas> element. Nested HTML-in-canvas capture is unsupported, so the built-in DOM composer is used.',
+ 'The composition contains an <HtmlInCanvas> element. Nested HTML-in-canvas capture requires Chrome 157 or newer, so the built-in DOM composer is used.',
shouldWarn: false,
});
} finally {