Skip to content

🎥 Make videos programmatically with React

@remotion/studio: Add HtmlInCanvasMotionBlur to Wrap menu

Jonny Burgere92686e6661a
diff --git a/packages/codemods/src/wrap-jsx-node.ts b/packages/codemods/src/wrap-jsx-node.ts
index d878e9afd3..547e371a0b 100644
--- a/packages/codemods/src/wrap-jsx-node.ts
+++ b/packages/codemods/src/wrap-jsx-node.ts
@@ -21,7 +21,11 @@ import {
import {getEndOfLine, getIndentationUnit, getLineIndent} from './source-style';
import {stripParenthesizedExtra} from './strip-parenthesized-extra';
-type JsxWrapper = 'AbsoluteFill' | 'Sequence' | 'HtmlInCanvas';
+type JsxWrapper =
+ | 'AbsoluteFill'
+ | 'Sequence'
+ | 'HtmlInCanvas'
+ | 'HtmlInCanvasMotionBlur';
export const canWrapJsxNode = ({
input,
@@ -36,11 +40,12 @@ export const canWrapJsxNode = ({
return {canWrap: false, canWrapHtmlInCanvas: false};
}
- const htmlInCanvasNames = new Set(['HtmlInCanvas']);
+ const htmlInCanvasNames = new Set(['HtmlInCanvas', 'HtmlInCanvasMotionBlur']);
for (const statement of ast.program.body) {
if (
statement.type !== 'ImportDeclaration' ||
- statement.source.value !== 'remotion'
+ (statement.source.value !== 'remotion' &&
+ statement.source.value !== '@remotion/motion-blur')
) {
continue;
}
@@ -48,9 +53,12 @@ export const canWrapJsxNode = ({
for (const specifier of statement.specifiers ?? []) {
if (
specifier.type === 'ImportSpecifier' &&
- getImportedName(specifier) === 'HtmlInCanvas'
+ (getImportedName(specifier) === 'HtmlInCanvas' ||
+ getImportedName(specifier) === 'HtmlInCanvasMotionBlur')
) {
- htmlInCanvasNames.add(specifier.local?.name ?? 'HtmlInCanvas');
+ htmlInCanvasNames.add(
+ specifier.local?.name ?? getImportedName(specifier),
+ );
}
}
}
@@ -104,21 +112,23 @@ export const wrapJsxNode = <Project extends CodemodProject>({
const filePath = findProjectFile({project, filePath: node.filePath});
const input = project.files[filePath];
const eligibility = canWrapJsxNode({input, nodePath: node.nodePath});
+ const requiresHtmlInCanvas =
+ wrapper === 'HtmlInCanvas' || wrapper === 'HtmlInCanvasMotionBlur';
if (
!eligibility.canWrap ||
- (wrapper === 'HtmlInCanvas' && !eligibility.canWrapHtmlInCanvas)
+ (requiresHtmlInCanvas && !eligibility.canWrapHtmlInCanvas)
) {
throw new Error('This JSX element cannot be wrapped');
}
if (
- wrapper === 'HtmlInCanvas' &&
+ requiresHtmlInCanvas &&
(!Number.isInteger(width) ||
width <= 0 ||
!Number.isInteger(height) ||
height <= 0)
) {
- throw new Error('HtmlInCanvas requires positive integer dimensions');
+ throw new Error(`${wrapper} requires positive integer dimensions`);
}
const ast = parseAst(input);
@@ -250,23 +260,25 @@ export const wrapJsxNode = <Project extends CodemodProject>({
ast,
importedName: wrapper,
localName: suggestedName,
- sourcePath: 'remotion',
+ sourcePath:
+ wrapper === 'HtmlInCanvasMotionBlur'
+ ? '@remotion/motion-blur'
+ : 'remotion',
});
const b = recast.types.builders;
const name = b.jsxIdentifier(localName);
- const attributes =
- wrapper === 'HtmlInCanvas'
- ? [
- b.jsxAttribute(
- b.jsxIdentifier('width'),
- b.jsxExpressionContainer(b.numericLiteral(width)),
- ),
- b.jsxAttribute(
- b.jsxIdentifier('height'),
- b.jsxExpressionContainer(b.numericLiteral(height)),
- ),
- ]
- : [];
+ const attributes = requiresHtmlInCanvas
+ ? [
+ b.jsxAttribute(
+ b.jsxIdentifier('width'),
+ b.jsxExpressionContainer(b.numericLiteral(width)),
+ ),
+ b.jsxAttribute(
+ b.jsxIdentifier('height'),
+ b.jsxExpressionContainer(b.numericLiteral(height)),
+ ),
+ ]
+ : [];
const opening = b.jsxOpeningElement(name, attributes, false);
const openingLines = printJsxOpeningElement({
openingElement: opening,
diff --git a/packages/motion-blur/src/HtmlInCanvasMotionBlur.tsx b/packages/motion-blur/src/HtmlInCanvasMotionBlur.tsx
index fd2d16a68e..63760d04f3 100644
--- a/packages/motion-blur/src/HtmlInCanvasMotionBlur.tsx
+++ b/packages/motion-blur/src/HtmlInCanvasMotionBlur.tsx
@@ -2,16 +2,16 @@ import React, {useCallback} from 'react';
import {
Freeze,
HtmlInCanvas,
- type HtmlInCanvasProps,
+ Interactive,
+ type InteractiveBaseProps,
+ type InteractivitySchema,
type HtmlInCanvasOnPaint,
+ type SequenceControls,
useCurrentFrame,
useVideoConfig,
} from 'remotion';
-export type HtmlInCanvasMotionBlurProps = Pick<
- HtmlInCanvasProps,
- 'from' | 'durationInFrames' | 'trimBefore' | 'playbackRate'
-> & {
+export type HtmlInCanvasMotionBlurProps = InteractiveBaseProps & {
readonly children: React.ReactNode;
readonly width: number;
readonly height: number;
@@ -19,6 +19,30 @@ export type HtmlInCanvasMotionBlurProps = Pick<
readonly samples?: number;
};
+const htmlInCanvasMotionBlurSchema = {
+ ...Interactive.baseSchema,
+ shutterAngle: {
+ type: 'number',
+ min: 0,
+ max: 360,
+ step: 1,
+ default: 180,
+ description: 'Shutter angle',
+ hiddenFromList: false,
+ },
+ samples: {
+ type: 'number',
+ min: 1,
+ max: 64,
+ step: 1,
+ integer: true,
+ default: 8,
+ description: 'Samples',
+ hiddenFromList: false,
+ keyframable: false,
+ },
+} as const satisfies InteractivitySchema;
+
type MotionBlurSampleProps = {
readonly children: React.ReactNode;
readonly width: number;
@@ -68,13 +92,11 @@ const MotionBlurSample: React.FC<MotionBlurSampleProps> = ({
);
};
-/**
- * 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.
- */
-export const HtmlInCanvasMotionBlur: React.FC<HtmlInCanvasMotionBlurProps> = ({
+const HtmlInCanvasMotionBlurInner: React.FC<
+ HtmlInCanvasMotionBlurProps & {
+ readonly controls: SequenceControls | null | undefined;
+ }
+> = ({
children,
width,
height,
@@ -84,6 +106,11 @@ export const HtmlInCanvasMotionBlur: React.FC<HtmlInCanvasMotionBlurProps> = ({
durationInFrames,
trimBefore,
playbackRate,
+ freeze,
+ hidden,
+ name,
+ showInTimeline,
+ controls,
}) => {
const {durationInFrames: compositionDurationInFrames} = useVideoConfig();
@@ -180,6 +207,7 @@ export const HtmlInCanvasMotionBlur: React.FC<HtmlInCanvasMotionBlurProps> = ({
});
// The root wrapper holds the first sample. The rest must be canvas children.
+ // Reuse HtmlInCanvas's Sequence for the motion blur controls.
return (
<HtmlInCanvas
width={width}
@@ -188,7 +216,11 @@ export const HtmlInCanvasMotionBlur: React.FC<HtmlInCanvasMotionBlurProps> = ({
durationInFrames={durationInFrames}
trimBefore={trimBefore}
playbackRate={playbackRate}
- name="<HtmlInCanvasMotionBlur>"
+ freeze={freeze}
+ hidden={hidden}
+ name={name ?? '<HtmlInCanvasMotionBlur>'}
+ showInTimeline={showInTimeline}
+ {...{controls}}
onPaint={onPaint}
_remotionInternalCanvasSiblings={sampleElements.slice(1)}
>
@@ -196,3 +228,19 @@ export const HtmlInCanvasMotionBlur: React.FC<HtmlInCanvasMotionBlurProps> = ({
</HtmlInCanvas>
);
};
+
+/**
+ * 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.
+ */
+export const HtmlInCanvasMotionBlur = Interactive.withSchema({
+ Component: HtmlInCanvasMotionBlurInner,
+ componentName: '<HtmlInCanvasMotionBlur>',
+ componentIdentity: 'dev.remotion.motionBlur.HtmlInCanvasMotionBlur',
+ schema: htmlInCanvasMotionBlurSchema,
+ supportsEffects: false,
+});
+
+HtmlInCanvasMotionBlur.displayName = 'HtmlInCanvasMotionBlur';
diff --git a/packages/studio-server/src/preview-server/routes/wrap-jsx-node.ts b/packages/studio-server/src/preview-server/routes/wrap-jsx-node.ts
index bf00132acb..846ca2fc72 100644
--- a/packages/studio-server/src/preview-server/routes/wrap-jsx-node.ts
+++ b/packages/studio-server/src/preview-server/routes/wrap-jsx-node.ts
@@ -47,7 +47,8 @@ export const wrapJsxNodeHandler: ApiHandler<
if (
!eligibility.canWrap ||
- (wrapper === 'HtmlInCanvas' && !eligibility.canWrapHtmlInCanvas)
+ ((wrapper === 'HtmlInCanvas' || wrapper === 'HtmlInCanvasMotionBlur') &&
+ !eligibility.canWrapHtmlInCanvas)
) {
throw new Error('This JSX element cannot be wrapped');
}
diff --git a/packages/studio-shared/src/api-requests.ts b/packages/studio-shared/src/api-requests.ts
index b4268ac8c3..d603c5c792 100644
--- a/packages/studio-shared/src/api-requests.ts
+++ b/packages/studio-shared/src/api-requests.ts
@@ -891,7 +891,11 @@ export type DuplicateJsxNodeResponse =
stack: string;
};
-export type JsxWrapper = 'AbsoluteFill' | 'Sequence' | 'HtmlInCanvas';
+export type JsxWrapper =
+ | 'AbsoluteFill'
+ | 'Sequence'
+ | 'HtmlInCanvas'
+ | 'HtmlInCanvasMotionBlur';
export type WrapJsxNodeRequest = {
fileName: string;
diff --git a/packages/studio/src/components/InspectorPanel/SequenceWrapAction.tsx b/packages/studio/src/components/InspectorPanel/SequenceWrapAction.tsx
index 15e87c7171..348fbdbc88 100644
--- a/packages/studio/src/components/InspectorPanel/SequenceWrapAction.tsx
+++ b/packages/studio/src/components/InspectorPanel/SequenceWrapAction.tsx
@@ -4,6 +4,7 @@ import {isHtmlInCanvasSupported, useVideoConfig} from 'remotion';
import {LIGHT_TEXT} from '../../helpers/colors';
import type {SequenceNodePathInfo} from '../../helpers/get-timeline-sequence-sort-key';
import type {TimelineTrackData} from '../../helpers/get-timeline-sequence-sort-key';
+import {installRequiredPackages} from '../../helpers/install-required-package';
import {CaretDown} from '../../icons/caret';
import {WrapIcon} from '../../icons/wrap';
import {INSPECTOR_PANEL_HORIZONTAL_PADDING} from '../InspectorPanelLayout';
@@ -16,7 +17,12 @@ import {
largeInspectorActionIconStyle,
} from './common';
-const wrapperNames: JsxWrapper[] = ['AbsoluteFill', 'Sequence', 'HtmlInCanvas'];
+const wrapperNames: JsxWrapper[] = [
+ 'AbsoluteFill',
+ 'Sequence',
+ 'HtmlInCanvas',
+ 'HtmlInCanvasMotionBlur',
+];
const buttonStyle: React.CSSProperties = {
borderRadius: 4,
@@ -99,7 +105,7 @@ export const SequenceWrapAction: React.FC<{
}, [nodePathKey, sequence, sourceActionsDisabled]);
const onWrap = useCallback(
- (wrapper: JsxWrapper) => {
+ async (wrapper: JsxWrapper) => {
if (busy || sourceActionsDisabled || !eligibility?.canWrap) {
return;
}
@@ -108,22 +114,28 @@ export const SequenceWrapAction: React.FC<{
const nodePath = JSON.parse(
nodePathKey,
) as SequenceNodePathInfo['sequenceSubscriptionKey'];
- wrapJsxNode({
- fileName: nodePath.absolutePath,
- nodePath: nodePath.nodePath,
- wrapper,
- width,
- height,
- })
- .then((result) => {
- if (!result.success) {
- showNotification(result.reason, 4000);
- }
- })
- .catch((error) => {
- showNotification((error as Error).message, 4000);
- })
- .finally(() => setBusy(false));
+ try {
+ if (wrapper === 'HtmlInCanvasMotionBlur') {
+ await installRequiredPackages([
+ {name: '@remotion/motion-blur', version: null},
+ ]);
+ }
+
+ const result = await wrapJsxNode({
+ fileName: nodePath.absolutePath,
+ nodePath: nodePath.nodePath,
+ wrapper,
+ width,
+ height,
+ });
+ if (!result.success) {
+ showNotification(result.reason, 4000);
+ }
+ } catch (error) {
+ showNotification((error as Error).message, 4000);
+ } finally {
+ setBusy(false);
+ }
},
[busy, eligibility, height, nodePathKey, sourceActionsDisabled, width],
);
@@ -142,7 +154,8 @@ export const SequenceWrapAction: React.FC<{
quickSwitcherLabel: null,
disabled:
busy ||
- (wrapper === 'HtmlInCanvas' &&
+ ((wrapper === 'HtmlInCanvas' ||
+ wrapper === 'HtmlInCanvasMotionBlur') &&
(!eligibility?.canWrapHtmlInCanvas || !isHtmlInCanvasSupported())),
})),
[busy, eligibility, onWrap],