🎥 Make videos programmatically with React
`remotion`: Add preview fallback for unsupported `<HtmlInCanvas>`
JonnyBurger1e4872889fad
diff --git a/packages/core/src/HtmlInCanvas.tsx b/packages/core/src/HtmlInCanvas.tsxindex 549f418761..80a60450b4 100644--- a/packages/core/src/HtmlInCanvas.tsx+++ b/packages/core/src/HtmlInCanvas.tsx@@ -18,6 +18,7 @@ import {} from './effects/use-memoized-effects.js';import {addSequenceStackTraces} from './enable-sequence-stack-traces.js';import {Freeze} from './freeze.js';+import {HtmlInCanvasFallback} from './HtmlInCanvasFallback.js';import type {InteractiveBaseProps,InteractiveCropProps,@@ -471,6 +472,19 @@ type HtmlInCanvasPaintState = {const HtmlInCanvasAncestorContext =createContext<HtmlInCanvasPaintState | null>(null);+export const useHtmlInCanvasUnsupportedMessage = (): string | null => {+ const ancestorPaintState = useContext(HtmlInCanvasAncestorContext);+ if (!isHtmlInCanvasSupported()) {+ return getHtmlInCanvasUnsupportedMessage();+ }++ if (ancestorPaintState && !isHtmlInCanvasNestingSupported()) {+ return `Nested <HtmlInCanvas> components require Chrome ${MINIMUM_CHROME_VERSION_FOR_NESTED_HTML_IN_CANVAS} or newer with HTML-in-canvas enabled.`;+ }++ return null;+};+type HtmlInCanvasContentProps = {readonly width: number;readonly height: number;@@ -1114,6 +1128,9 @@ const HtmlInCanvasInner = forwardRef<ref,) => {const {isRendering} = useRemotionEnvironment();+ const unsupportedMessage = useHtmlInCanvasUnsupportedMessage();+ assertHtmlInCanvasDimensions(width, height);+ resolveHtmlInCanvasPixelDensity(pixelDensity);const Content = isRendering? HtmlInCanvasContentForRendering: HtmlInCanvasContent;@@ -1181,22 +1198,30 @@ const HtmlInCanvasInner = forwardRef<_remotionInternalIsPremounting={premountingActive}_remotionInternalIsPostmounting={postmountingActive}>- <Content- ref={setCanvasRef}- renderFrame={null}- width={width}- height={height}- effects={effects}- effectsOutputSize={effectsOutputSize ?? null}- onPaint={onPaint}- onInit={onInit}- pixelDensity={pixelDensity}- canvasSiblings={_remotionInternalCanvasSiblings ?? null}- controls={controls}- style={croppedStyle ?? undefined}- >- {children}- </Content>+ {!isRendering && unsupportedMessage !== null ? (+ <HtmlInCanvasFallback style={{width, height, ...croppedStyle}}>+ <DefaultPremountContext.Provider value={0}>+ <div style={{width, height}}>{children}</div>+ </DefaultPremountContext.Provider>+ </HtmlInCanvasFallback>+ ) : (+ <Content+ ref={setCanvasRef}+ renderFrame={null}+ width={width}+ height={height}+ effects={effects}+ effectsOutputSize={effectsOutputSize ?? null}+ onPaint={onPaint}+ onInit={onInit}+ pixelDensity={pixelDensity}+ canvasSiblings={_remotionInternalCanvasSiblings ?? null}+ controls={controls}+ style={croppedStyle ?? undefined}+ >+ {children}+ </Content>+ )}</Sequence></Freeze>);diff --git a/packages/core/src/HtmlInCanvasFallback.tsx b/packages/core/src/HtmlInCanvasFallback.tsxnew file mode 100644index 0000000000..b7ffeff2f4--- /dev/null+++ b/packages/core/src/HtmlInCanvasFallback.tsx@@ -0,0 +1,52 @@+import React from 'react';++export const HtmlInCanvasFallback: React.FC<{+ readonly children: React.ReactNode;+ readonly style: React.CSSProperties;+}> = ({children, style}) => {+ return (+ <div style={{position: 'relative', ...style}}>+ {children}+ <div+ className="css-reset"+ style={{+ position: 'absolute',+ top: 8,+ right: 8,+ zIndex: 1000,+ maxWidth: 'calc(100% - 16px)',+ padding: '8px 10px',+ borderRadius: 6,+ border: '1px solid #7f3540',+ backgroundColor: '#2b171c',+ boxShadow: '0 2px 8px #0003',+ pointerEvents: 'none',+ textAlign: 'left',+ }}+ >+ <div+ style={{+ fontFamily: 'Arial, Helvetica, sans-serif',+ fontSize: 12,+ fontWeight: 600,+ lineHeight: 1.5,+ color: '#ffabb5',+ }}+ >+ HTML-in-canvas is not available+ </div>+ <div+ style={{+ fontFamily: 'Arial, Helvetica, sans-serif',+ fontSize: 11,+ fontWeight: 400,+ lineHeight: 1.5,+ color: '#e8bdc3',+ }}+ >+ Effects are not visible+ </div>+ </div>+ </div>+ );+};diff --git a/packages/core/src/internals.ts b/packages/core/src/internals.tsindex c682ddbc92..4a36e7029f 100644--- a/packages/core/src/internals.ts+++ b/packages/core/src/internals.ts@@ -106,6 +106,8 @@ import {REMOTION_STUDIO_CONTAINER_ELEMENT,} from './get-preview-dom-element.js';import {getRemotionEnvironment} from './get-remotion-environment.js';+import {useHtmlInCanvasUnsupportedMessage} from './HtmlInCanvas.js';+import {HtmlInCanvasFallback} from './HtmlInCanvasFallback.js';import {getInputPropsOverride,setInputPropsOverride,@@ -353,6 +355,8 @@ const compositionSelectorRef = createRef<{// Mark them as Internals so use don't assume this is public// API and are less likely to use itexport const Internals = {+ HtmlInCanvasFallback,+ useHtmlInCanvasUnsupportedMessage,OptimisticSequenceDeletion,usePendingSequenceDeletions,createElementSourceProxy,diff --git a/packages/core/src/test/html-in-canvas.test.tsx b/packages/core/src/test/html-in-canvas.test.tsxindex 6a24524bd9..12bc12447f 100644--- a/packages/core/src/test/html-in-canvas.test.tsx+++ b/packages/core/src/test/html-in-canvas.test.tsx@@ -296,10 +296,10 @@ test('<HtmlInCanvas> exposes crop controls', () => {expect(htmlInCanvasSchema.cropBottom.keyframable).toBe(true);});-test('<HtmlInCanvas> throws when nested', () => {+test('<HtmlInCanvas> throws when nested during rendering', () => {expect(() =>render(- <SequenceTestWrapper onRegisterSequence={() => undefined}>+ <SequenceTestWrapper isRendering onRegisterSequence={() => undefined}><HtmlInCanvas width={120} height={80}><HtmlInCanvas width={60} height={40}><div>Nested</div>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.mdxindex 1dbb24df7d..e85d08274e 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@@ -14,6 +14,8 @@ crumb: '@remotion/motion-blur'`<HtmlInCanvasMotionBlur>` is the state-of-the-art recommended way to achieve motion blur in Remotion.It requires [HTML-in-canvas](/docs/html-in-canvas#enabling-the-flag-in-chrome) to be enabled during preview. Rendering needs no configuration.+From <AvailableFrom v="4.0.538" inline />, if HTML-in-canvas is unavailable during preview, the component displays its children at the current frame without motion blur and shows an "Effects are not visible" warning. Rendering still fails when HTML-in-canvas is unavailable.+```tsx twoslash title="MyComposition.tsx"import {HtmlInCanvasMotionBlur,diff --git a/packages/docs/docs/remotion/html-in-canvas.mdx b/packages/docs/docs/remotion/html-in-canvas.mdxindex 12725c434a..cdeec2b984 100644--- a/packages/docs/docs/remotion/html-in-canvas.mdx+++ b/packages/docs/docs/remotion/html-in-canvas.mdx@@ -28,10 +28,13 @@ export const MyComp: React.FC = () => {Return value: _boolean_-If HTML-in-canvas is not available, the component throws a fatal error.Use `HtmlInCanvas.isSupported()` to check if HTML-in-canvas is supported.Buggy implementations such as the one in Chrome 147 are not considered supported.+From <AvailableFrom v="4.0.538" inline />, if HTML-in-canvas is unavailable during preview, the component displays its children without canvas effects and shows a warning in its top-right corner. Dimensions, styles, cropping, and sequence timing are preserved. The `onInit` and `onPaint` callbacks are not called, and the canvas ref remains `null`.++Rendering still throws a fatal error when HTML-in-canvas is unavailable. Before v4.0.538, previews also threw an error.+```ts twoslash title="Check if HTML-in-canvas is supported"import {HtmlInCanvas} from 'remotion';@@ -46,7 +49,7 @@ if (HtmlInCanvas.isSupported()) {Return value: _boolean_-Returns `true` when HTML-in-canvas is enabled and the browser supports nesting. Use it before nesting `<HtmlInCanvas>` components. Older versions throw an error when components are nested.+Returns `true` when HTML-in-canvas is enabled and the browser supports nesting. During rendering, unsupported nesting throws an error. From <AvailableFrom v="4.0.538" inline />, unsupported nested components display their children without canvas effects during preview, with a warning for each affected component.From Remotion <AvailableFrom v="4.0.534" inline />, the bundled server-side browser is Chromium 157.0.8080.0, so this method also returns `true` during server-side rendering.diff --git a/packages/docs/docs/transitions/make-html-in-canvas-presentation.mdx b/packages/docs/docs/transitions/make-html-in-canvas-presentation.mdxindex 3096071ef6..6652f87847 100644--- a/packages/docs/docs/transitions/make-html-in-canvas-presentation.mdx+++ b/packages/docs/docs/transitions/make-html-in-canvas-presentation.mdx@@ -234,6 +234,8 @@ const myPresentation = makeHtmlInCanvasPresentation(myShader);:::warningHTML-in-canvas requires [Google Chrome](https://www.google.com/chrome/) with `chrome://flags/#canvas-draw-element` enabled.++From <AvailableFrom v="4.0.538" inline />, previews without HTML-in-canvas show an "Effects are not visible" warning and cut between scenes halfway through the transition. The transition duration is preserved. Rendering still fails when HTML-in-canvas is unavailable.:::## Argumentsdiff --git a/packages/example/src/HtmlInCanvas/fallback.tsx b/packages/example/src/HtmlInCanvas/fallback.tsxnew file mode 100644index 0000000000..79d915113c--- /dev/null+++ b/packages/example/src/HtmlInCanvas/fallback.tsx@@ -0,0 +1,134 @@+import {blur} from '@remotion/effects/blur';+import {HtmlInCanvasMotionBlur} from '@remotion/motion-blur';+import {linearTiming, TransitionSeries} from '@remotion/transitions';+import {zoomBlur} from '@remotion/transitions/zoom-blur';+import React from 'react';+import {AbsoluteFill, HtmlInCanvas, useCurrentFrame} from 'remotion';++const MovingCard: React.FC = () => {+ const frame = useCurrentFrame();+ return (+ <div+ style={{+ position: 'absolute',+ left: 120 + Math.sin(frame / 15) * 80,+ top: 115,+ width: 130,+ height: 130,+ borderRadius: 24,+ background: 'linear-gradient(135deg, #fbbf24, #f472b6)',+ transform: `rotate(${frame * 3}deg)`,+ display: 'grid',+ placeItems: 'center',+ fontSize: 38,+ fontWeight: 700,+ color: '#211629',+ }}+ >+ DOM+ </div>+ );+};++export const HtmlInCanvasFallbackExample: React.FC = () => {+ const frame = useCurrentFrame();+ return (+ <AbsoluteFill+ style={{+ background: '#10141e',+ color: '#f1f5f9',+ fontFamily: 'Arial, Helvetica, sans-serif',+ padding: 40,+ }}+ >+ <div style={{fontSize: 34, fontWeight: 700}}>HTML-in-canvas fallback</div>+ <div style={{fontSize: 18, color: '#94a3b8', marginTop: 12}}>+ Preview in a browser without HTML-in-canvas · Frame {frame}+ </div>+ <div style={{display: 'flex', gap: 24, marginTop: 42}}>+ <div>+ <div style={{fontSize: 20, marginBottom: 16}}>Nested components</div>+ <HtmlInCanvas+ width={384}+ height={430}+ effects={[blur({radius: 6})]}+ style={{background: '#1e293b', borderRadius: 12}}+ >+ <div style={{padding: '100px 24px 24px'}}>+ <div style={{fontSize: 24, marginBottom: 20}}>Outer content</div>+ <HtmlInCanvas+ width={336}+ height={240}+ effects={[blur({radius: 4})]}+ style={{background: '#31435e', borderRadius: 10}}+ >+ <div style={{padding: '110px 24px', fontSize: 24}}>+ Inner content+ </div>+ </HtmlInCanvas>+ </div>+ </HtmlInCanvas>+ </div>+ <div>+ <div style={{fontSize: 20, marginBottom: 16}}>Motion blur</div>+ <div+ style={{+ position: 'relative',+ width: 384,+ height: 430,+ background: '#1e293b',+ borderRadius: 12,+ }}+ >+ <HtmlInCanvasMotionBlur width={384} height={430}>+ <MovingCard />+ </HtmlInCanvasMotionBlur>+ </div>+ </div>+ <div>+ <div style={{fontSize: 20, marginBottom: 16}}>Shader transition</div>+ <div+ style={{+ position: 'relative',+ width: 384,+ height: 430,+ borderRadius: 12,+ overflow: 'hidden',+ }}+ >+ <TransitionSeries>+ <TransitionSeries.Sequence durationInFrames={120}>+ <AbsoluteFill+ style={{+ background: '#164e63',+ justifyContent: 'center',+ alignItems: 'center',+ fontSize: 36,+ }}+ >+ Scene A+ </AbsoluteFill>+ </TransitionSeries.Sequence>+ <TransitionSeries.Transition+ presentation={zoomBlur({})}+ timing={linearTiming({durationInFrames: 60})}+ />+ <TransitionSeries.Sequence durationInFrames={120}>+ <AbsoluteFill+ style={{+ background: '#5b214e',+ justifyContent: 'center',+ alignItems: 'center',+ fontSize: 36,+ }}+ >+ Scene B+ </AbsoluteFill>+ </TransitionSeries.Sequence>+ </TransitionSeries>+ </div>+ </div>+ </div>+ </AbsoluteFill>+ );+};diff --git a/packages/example/src/Root.tsx b/packages/example/src/Root.tsxindex fbac78830d..2edf15341f 100644--- a/packages/example/src/Root.tsx+++ b/packages/example/src/Root.tsx@@ -115,6 +115,7 @@ import {ZoomInOutTransitionDoc,ZoomInOutTransitionDocThumb,} from './HtmlInCanvas';+import {HtmlInCanvasFallbackExample} from './HtmlInCanvas/fallback';import {HugeImage} from './HugeImage';import {HugePayload, hugePayloadSchema} from './HugePayload';import {IntrinsicDurationLoopTestbed} from './IntrinsicDurationLoopTestbed';@@ -1273,6 +1274,14 @@ export const Index: React.FC = () => {durationInFrames={100}/><Folder name="html-in-canvas">+ <Composition+ id="html-in-canvas-fallback"+ component={HtmlInCanvasFallbackExample}+ fps={30}+ height={720}+ width={1280}+ durationInFrames={180}+ /><Compositionid="html-in-canvas-nested"component={HtmlInCanvasNested}diff --git a/packages/motion-blur/src/HtmlInCanvasMotionBlur.tsx b/packages/motion-blur/src/HtmlInCanvasMotionBlur.tsxindex f91e78173b..92b51f46c1 100644--- a/packages/motion-blur/src/HtmlInCanvasMotionBlur.tsx+++ b/packages/motion-blur/src/HtmlInCanvasMotionBlur.tsx@@ -215,22 +215,26 @@ const HtmlInCanvasMotionBlurInner: React.FC<[actualSamples],);- const sampleElements = Array.from({length: actualSamples}, (_, index) => {- return (- <MotionBlurSample- key={index}- width={width}- height={height}- index={index}- count={actualSamples}- shutterInFrames={shutterInFrames}- firstFrame={firstFrame}- lastFrame={lastFrame}- >- {children}- </MotionBlurSample>- );- });+ const passThrough = Internals.useHtmlInCanvasUnsupportedMessage() !== null;+ const sampleElements = Array.from(+ {length: passThrough ? 0 : actualSamples},+ (_, index) => {+ return (+ <MotionBlurSample+ key={index}+ width={width}+ height={height}+ index={index}+ count={actualSamples}+ shutterInFrames={shutterInFrames}+ firstFrame={firstFrame}+ lastFrame={lastFrame}+ >+ {children}+ </MotionBlurSample>+ );+ },+ );// The root wrapper holds the first sample. The rest must be canvas children.// Reuse HtmlInCanvas's Sequence for the motion blur controls.@@ -255,7 +259,7 @@ const HtmlInCanvasMotionBlurInner: React.FC<onPaint={onPaint}_remotionInternalCanvasSiblings={sampleElements.slice(1)}>- {sampleElements[0]}+ {passThrough ? children : sampleElements[0]}</HtmlInCanvas>);};diff --git a/packages/transitions/src/html-in-canvas-presentation.tsx b/packages/transitions/src/html-in-canvas-presentation.tsxindex f25c915638..cae3bbd2b9 100644--- a/packages/transitions/src/html-in-canvas-presentation.tsx+++ b/packages/transitions/src/html-in-canvas-presentation.tsx@@ -5,6 +5,7 @@ import {HtmlInCanvas,Internals,useDelayRender,+ useRemotionEnvironment,type EffectsProp,type EffectsOutputSize,} from 'remotion';@@ -14,7 +15,7 @@ import type {TransitionPresentationComponentProps,} from './types';-export const HtmlInCanvasPresentation = <+const HtmlInCanvasPresentationWithSupport = <TPassedProps extends Record<string, unknown>,>({children,@@ -29,13 +30,9 @@ export const HtmlInCanvasPresentation = <bothEnteringAndExiting,}: TransitionPresentationComponentProps<TPassedProps> & {readonly shader: HtmlInCanvasShader<TPassedProps>;- readonly effects?: EffectsProp;+ readonly effects: EffectsProp | null;readonly effectsOutputSize: EffectsOutputSize | null;}) => {- if (!HtmlInCanvas.isSupported()) {- throw new Error(getHtmlInCanvasUnsupportedMessage());- }-const canvasRef = useRef<HTMLCanvasElement>(null);const outputCanvasRef = useRef<HTMLCanvasElement>(null);const canvasSubtreeStyle: React.CSSProperties = useMemo(() => {@@ -313,6 +310,41 @@ export const HtmlInCanvasPresentation = <);};+export const HtmlInCanvasPresentation = <+ TPassedProps extends Record<string, unknown>,+>(+ props: TransitionPresentationComponentProps<TPassedProps> & {+ readonly shader: HtmlInCanvasShader<TPassedProps>;+ readonly effects: EffectsProp | null;+ readonly effectsOutputSize: EffectsOutputSize | null;+ },+) => {+ const {isRendering} = useRemotionEnvironment();+ if (!HtmlInCanvas.isSupported()) {+ if (isRendering) {+ throw new Error(getHtmlInCanvasUnsupportedMessage());+ }++ const visible =+ props.presentationDirection === 'entering'+ ? props.presentationProgress >= 0.5+ : props.presentationProgress < 0.5;+ return (+ <Internals.HtmlInCanvasFallback+ style={{+ position: 'absolute',+ inset: 0,+ visibility: visible ? 'visible' : 'hidden',+ }}+ >+ {props.children}+ </Internals.HtmlInCanvasFallback>+ );+ }++ return <HtmlInCanvasPresentationWithSupport {...props} />;+};+export type HtmlInCanvasShaderDrawParams<Props> = {prevImage: OffscreenCanvas | null;nextImage: OffscreenCanvas | null;@@ -350,7 +382,7 @@ export const makeHtmlInCanvasPresentation = <<HtmlInCanvasPresentationshader={shader}passedProps={restPassedProps as TPassedProps}- effects={effects}+ effects={effects ?? null}effectsOutputSize={effectsOutputSize ?? null}{...otherProps}/>