Skip to content

🎥 Make videos programmatically with React

remotion: Remove fit prop from <Img>, use style.objectFit instead

Jonny Burgerc0b6c409c92f
diff --git a/packages/core/src/Img.tsx b/packages/core/src/Img.tsx
index c9c0235ec2..fab8600c37 100644
--- a/packages/core/src/Img.tsx
+++ b/packages/core/src/Img.tsx
@@ -51,7 +51,6 @@ export type ImgProps = NativeImgProps & {
readonly onImageFrame?: (imageElement: HTMLImageElement) => void;
readonly src: string;
readonly effects?: EffectsProp;
- readonly fit?: ImageFit;
readonly showInTimeline?: boolean;
readonly name?: string;
/**
@@ -74,7 +73,6 @@ type ImgContentProps = Omit<
| 'from'
| 'durationInFrames'
| 'effects'
- | 'fit'
>;
const ImgContent: React.FC<ImgContentProps> = ({
@@ -304,7 +302,7 @@ const ImgContent: React.FC<ImgContentProps> = ({
);
};
-type NativeImgInnerProps = Omit<ImgProps, 'effects' | 'fit'> & {
+type NativeImgInnerProps = Omit<ImgProps, 'effects'> & {
readonly _experimentalControls: SequenceControls | undefined;
};
@@ -349,6 +347,11 @@ const CanvasImageWithPrivateProps = CanvasImage as React.ComponentType<
}
>;
+export const imgSchema = {
+ ...sequenceVisualStyleSchema,
+ hidden: hiddenField,
+} as const satisfies SequenceSchema;
+
const imgCanvasFallbackIncompatibleProps = new Set([
'alt',
'crossOrigin',
@@ -408,6 +411,22 @@ const validateCanvasImageFallbackProps = ({
);
};
+const getFitFromObjectFit = (
+ style: React.CSSProperties | undefined,
+): ImageFit | undefined => {
+ const objectFit = style?.objectFit;
+
+ if (
+ objectFit === 'fill' ||
+ objectFit === 'contain' ||
+ objectFit === 'cover'
+ ) {
+ return objectFit;
+ }
+
+ return undefined;
+};
+
const ImgInner: React.FC<
ImgProps & {
readonly _experimentalControls: SequenceControls | undefined;
@@ -425,7 +444,6 @@ const ImgInner: React.FC<
_experimentalControls: controls,
width,
height,
- fit,
className,
style,
id,
@@ -475,13 +493,14 @@ const ImgInner: React.FC<
const canvasWidth = typeof width === 'number' ? width : undefined;
const canvasHeight = typeof height === 'number' ? height : undefined;
const canvasProps = props as CanvasImageCanvasProps;
+ const canvasFit = getFitFromObjectFit(style) ?? 'fill';
return (
<CanvasImageWithPrivateProps
src={src}
width={canvasWidth}
height={canvasHeight}
- fit={fit}
+ fit={canvasFit}
effects={effects}
className={className}
style={style}
@@ -505,21 +524,6 @@ const ImgInner: React.FC<
);
};
-const imgSchema = {
- fit: {
- type: 'enum',
- default: 'fill',
- description: 'Fit',
- variants: {
- fill: {},
- contain: {},
- cover: {},
- },
- },
- ...sequenceVisualStyleSchema,
- hidden: hiddenField,
-} as const satisfies SequenceSchema;
-
/*
* @description Works just like a regular HTML img tag. When you use the <Img> tag, Remotion will ensure that the image is loaded before rendering the frame.
* @see [Documentation](https://remotion.dev/docs/img)
diff --git a/packages/core/src/canvas-image/CanvasImage.tsx b/packages/core/src/canvas-image/CanvasImage.tsx
index 9ee7bedc23..a08b541e59 100644
--- a/packages/core/src/canvas-image/CanvasImage.tsx
+++ b/packages/core/src/canvas-image/CanvasImage.tsx
@@ -30,7 +30,7 @@ import {useDelayRender} from '../use-delay-render.js';
import {wrapInSchema} from '../wrap-in-schema.js';
import type {CanvasImageCanvasProps, CanvasImageProps} from './props.js';
-const canvasImageSchema = {
+export const canvasImageSchema = {
fit: {
type: 'enum',
default: 'fill',
diff --git a/packages/core/src/test/Img.test.tsx b/packages/core/src/test/Img.test.tsx
index 36dcfe1dd9..f437e68c2a 100644
--- a/packages/core/src/test/Img.test.tsx
+++ b/packages/core/src/test/Img.test.tsx
@@ -6,7 +6,7 @@ import type {
EffectDefinition,
EffectDescriptor,
} from '../effects/effect-types.js';
-import {Img} from '../Img.js';
+import {Img, imgSchema} from '../Img.js';
import type {RemotionEnvironment} from '../remotion-environment-context.js';
import {RemotionEnvironmentContext} from '../remotion-environment-context.js';
import {WrapSequenceContext} from './wrap-sequence-context.js';
@@ -190,6 +190,10 @@ test('Img with effects renders through the canvas image path', async () => {
expect(applyCalls[0].height).toBe(50);
});
+test('<Img> schema does not expose fit', () => {
+ expect(Object.keys(imgSchema)).not.toContain('fit');
+});
+
test('Img throws when native image props conflict with effects', () => {
expect(() =>
renderImg(
@@ -240,6 +244,24 @@ test('Img forwards canvas-compatible attributes when effects are passed', async
expect(drawImageCalls.some((call) => call[0] === 'clicked')).toBe(true);
});
+test('Img forwards objectFit to the canvas backend when effects are passed', async () => {
+ renderImg(
+ <Img
+ src={testImgUrl}
+ width={100}
+ height={100}
+ style={{objectFit: 'contain'}}
+ effects={[makeEffect()]}
+ />,
+ );
+
+ await waitFor(() => {
+ expect(drawImageCalls.length).toBeGreaterThanOrEqual(1);
+ });
+
+ expect(drawImageCalls[0].slice(1)).toEqual([0, 0, 200, 100, 0, 25, 100, 50]);
+});
+
test('Img throws when a ref is passed together with effects', () => {
const ref = React.createRef<HTMLImageElement>();
diff --git a/packages/docs/docs/img.mdx b/packages/docs/docs/img.mdx
index e3c42a9168..0b27de1eaa 100644
--- a/packages/docs/docs/img.mdx
+++ b/packages/docs/docs/img.mdx
@@ -65,12 +65,7 @@ Because the element becomes a `<canvas>`, native image props that require an `<i
The unsupported props include `ref`, `srcSet`, `sizes`, `loading`, `decoding`, `fetchPriority`, `useMap`, `crossOrigin`, `onLoad`, `onError`, `onImageFrame` and `alt`.
Canvas-compatible props such as `aria-*`, `data-*`, `role`, `title`, `tabIndex` and pointer or mouse event handlers are forwarded to the underlying `<canvas>`.
-
-### `fit?`<AvailableFrom v="4.0.468" />
-
-Controls how the image is drawn into the canvas when [`effects`](#effects) is a non-empty array.
-
-Can be `"fill"`, `"contain"` or `"cover"`. Default: `"fill"`.
+When [`effects`](#effects) is a non-empty array, `style.objectFit` controls how the image is drawn into the canvas. Supported values are `"fill"`, `"contain"` and `"cover"`.
### `onError`