Skip to content

🎥 Make videos programmatically with React

remotion: HtmlInCanvas runtime validation; HtmlInCanvasPrivacy example

JonnyBurger1ce54f1742e1
diff --git a/packages/core/src/HtmlInCanvas.tsx b/packages/core/src/HtmlInCanvas.tsx
index ff485667b1..cd85e03cc4 100644
--- a/packages/core/src/HtmlInCanvas.tsx
+++ b/packages/core/src/HtmlInCanvas.tsx
@@ -191,10 +191,27 @@ export type HtmlInCanvasOnInitCleanup = () => void;
export type HtmlInCanvasOnInit = (
params: HtmlInCanvasComposeParams,
-) =>
- | void
- | HtmlInCanvasOnInitCleanup
- | Promise<void | HtmlInCanvasOnInitCleanup>;
+) => HtmlInCanvasOnInitCleanup | Promise<HtmlInCanvasOnInitCleanup>;
+
+function assertHtmlInCanvasDimensions(width: unknown, height: unknown): void {
+ if (typeof width !== 'number' || typeof height !== 'number') {
+ throw new Error(
+ `HtmlInCanvas: \`width\` and \`height\` must be numbers. Received width=${String(width)}, height=${String(height)}.`,
+ );
+ }
+
+ if (!Number.isInteger(width) || width <= 0) {
+ throw new Error(
+ `HtmlInCanvas: \`width\` must be a positive integer. Received: ${String(width)}.`,
+ );
+ }
+
+ if (!Number.isInteger(height) || height <= 0) {
+ throw new Error(
+ `HtmlInCanvas: \`height\` must be a positive integer. Received: ${String(height)}.`,
+ );
+ }
+}
const defaultOnPaint: HtmlInCanvasOnPaint = ({
canvas,
@@ -272,6 +289,8 @@ const HtmlInCanvasInner: React.FC<
durationInFrames,
...sequenceProps
}) => {
+ assertHtmlInCanvasDimensions(width, height);
+
const {durationInFrames: videoDuration} = useVideoConfig();
const resolvedDuration = durationInFrames ?? videoDuration;
@@ -318,12 +337,19 @@ const HtmlInCanvasInner: React.FC<
// uploads (e.g. `copyElementImageToTexture`) failing with
// "No context found for ElementImage" on the very first paint.
const initImage = canvas2dRef.current?.captureElementImage(element);
- const cleanup = await onInitRef.current?.({
- canvas: offscreenCanvas,
- element,
- elementImage: initImage!,
- });
- if (typeof cleanup === 'function') {
+ const onInit = onInitRef.current;
+ if (onInit) {
+ const cleanup = await onInit({
+ canvas: offscreenCanvas,
+ element,
+ elementImage: initImage!,
+ });
+ if (typeof cleanup !== 'function') {
+ throw new Error(
+ 'HtmlInCanvas: when `onInit` is provided, it must return a cleanup function, or a Promise that resolves to one.',
+ );
+ }
+
if (unmountedRef.current) {
cleanup();
} else {
diff --git a/packages/example/src/HtmlInCanvas/index.tsx b/packages/example/src/HtmlInCanvas/index.tsx
index ee9820dec3..1eb5f0bd13 100644
--- a/packages/example/src/HtmlInCanvas/index.tsx
+++ b/packages/example/src/HtmlInCanvas/index.tsx
@@ -7,5 +7,6 @@ export {
HtmlInCanvasChangingSize,
HtmlInCanvasChangingSize as HtmlInCanvasDemo,
} from './changing-size';
+export {HtmlInCanvasPrivacy} from './privacy';
export {HtmlInCanvasReactSvg} from './react-svg';
export {HtmlInCanvasScene} from './scene';
diff --git a/packages/example/src/HtmlInCanvas/privacy.tsx b/packages/example/src/HtmlInCanvas/privacy.tsx
new file mode 100644
index 0000000000..027b8da2c1
--- /dev/null
+++ b/packages/example/src/HtmlInCanvas/privacy.tsx
@@ -0,0 +1,147 @@
+import React from 'react';
+import {AbsoluteFill, HtmlInCanvas} from 'remotion';
+
+// Demonstrates the "Privacy-preserving painting" rules from
+// https://github.com/WICG/html-in-canvas#privacy-preserving-painting
+//
+// Each row contains content that the spec says must NOT leak into the
+// painted snapshot:
+// • Cross-origin iframe content
+// • Cross-origin <img> without CORS
+// • :visited link styling
+// • Spellcheck markers
+// • System colors (forced-colors / theme)
+//
+// Right column (HtmlInCanvas paint) should show those bits redacted/blank;
+// left column (plain DOM) shows the full content for comparison.
+
+const Section: React.FC<{
+ readonly title: string;
+ readonly children: React.ReactNode;
+}> = ({title, children}) => (
+ <div style={{marginBottom: 14}}>
+ <div
+ style={{
+ fontSize: 12,
+ color: '#666',
+ marginBottom: 4,
+ fontFamily: 'sans-serif',
+ }}
+ >
+ {title}
+ </div>
+ {children}
+ </div>
+);
+
+const VISITED_LINK_CSS = `
+ .privacy-link { color: #0000ee; }
+ .privacy-link:visited { color: #ff00ff; }
+`;
+
+const SensitiveContent: React.FC = () => (
+ <div
+ style={{
+ fontFamily: 'sans-serif',
+ fontSize: 16,
+ color: 'black',
+ padding: 20,
+ backgroundColor: 'white',
+ width: 560,
+ }}
+ >
+ <style>{VISITED_LINK_CSS}</style>
+
+ <Section title="Cross-origin <iframe> (should paint blank)">
+ <iframe
+ title="cross-origin"
+ src="https://example.com"
+ width={500}
+ height={120}
+ style={{border: '1px solid #888'}}
+ />
+ </Section>
+
+ <Section title="Cross-origin <img> without CORS (should paint blank)">
+ <img
+ src="https://upload.wikimedia.org/wikipedia/commons/thumb/4/47/PNG_transparency_demonstration_1.png/280px-PNG_transparency_demonstration_1.png"
+ alt="cross-origin"
+ width={200}
+ />
+ </Section>
+
+ <Section title=":visited link (visited color must not leak)">
+ <a className="privacy-link" href="https://www.google.com">
+ google.com
+ </a>{' '}
+ ·{' '}
+ <a
+ className="privacy-link"
+ href="https://this-is-almost-certainly-unvisited.example/abc123"
+ >
+ unvisited.example
+ </a>
+ </Section>
+
+ <Section title="Spellcheck markers (no red squigglies in canvas)">
+ <input
+ type="text"
+ defaultValue="Speeling errrors should not show in canvas"
+ spellCheck
+ style={{width: 460, padding: 6, fontSize: 14}}
+ />
+ </Section>
+
+ <Section title="System color (color: ButtonText)">
+ <span style={{color: 'ButtonText', fontWeight: 'bold'}}>
+ Rendered with the SystemColor keyword
+ </span>
+ </Section>
+ </div>
+);
+
+export const HtmlInCanvasPrivacy: React.FC = () => {
+ return (
+ <AbsoluteFill
+ style={{
+ backgroundColor: '#eee',
+ padding: 30,
+ flexDirection: 'row',
+ gap: 24,
+ fontFamily: 'sans-serif',
+ color: 'black',
+ }}
+ >
+ <div style={{flex: 1}}>
+ <h2 style={{margin: '0 0 12px 0'}}>Live DOM (reference)</h2>
+ <SensitiveContent />
+ </div>
+
+ <div style={{flex: 1}}>
+ <h2 style={{margin: '0 0 12px 0'}}>HtmlInCanvas paint (redacted)</h2>
+ <HtmlInCanvas
+ width={600}
+ height={760}
+ style={{border: '1px dashed #888'}}
+ onPaint={({canvas, element, elementImage}) => {
+ const ctx = canvas.getContext('2d');
+ if (!ctx) {
+ throw new Error('Failed to acquire 2D context');
+ }
+
+ ctx.reset();
+ ctx.drawElementImage(elementImage, 0, 0);
+
+ // Push the live DOM off-screen so the canvas pixels (which
+ // have the redactions applied) are visible. CSS transforms
+ // on canvas children don't re-trigger the `paint` event, so
+ // this is safe to do inside the handler.
+ element.style.transform = 'translate(-99999px, 0)';
+ }}
+ >
+ <SensitiveContent />
+ </HtmlInCanvas>
+ </div>
+ </AbsoluteFill>
+ );
+};
diff --git a/packages/example/src/NewVideo.tsx b/packages/example/src/NewVideo.tsx
index 7e30727493..c2cd974eaf 100644
--- a/packages/example/src/NewVideo.tsx
+++ b/packages/example/src/NewVideo.tsx
@@ -22,9 +22,7 @@ export const calculateMetadataFn: CalculateMetadataFunction<
export const Component = () => {
return (
<>
- (
<Video src={src} debugOverlay _experimentalEffects={[halftone()]} />
- )( )
</>
);
};
diff --git a/packages/example/src/Root.tsx b/packages/example/src/Root.tsx
index e0f689828d..4a1f2f7da3 100644
--- a/packages/example/src/Root.tsx
+++ b/packages/example/src/Root.tsx
@@ -50,6 +50,7 @@ import {
HtmlInCanvasComposeWebGLCrt,
HtmlInCanvasComposeWebGPU,
HtmlInCanvasDemo,
+ HtmlInCanvasPrivacy,
HtmlInCanvasReactSvg,
} from './HtmlInCanvas';
import {HugeImage} from './HugeImage';
@@ -934,6 +935,14 @@ export const Index: React.FC = () => {
width={1280}
durationInFrames={60}
/>
+ <Composition
+ id="html-in-canvas-privacy"
+ component={HtmlInCanvasPrivacy}
+ fps={30}
+ height={900}
+ width={1400}
+ durationInFrames={60}
+ />
<Composition
id="html-in-canvas-react-svg"
component={HtmlInCanvasReactSvg}