Skip to content

🎥 Make videos programmatically with React

fix gif fill mode tests

Jonny Burger01625478030f
diff --git a/packages/example/src/GifTest/fill-modes.tsx b/packages/example/src/GifTest/fill-modes.tsx
new file mode 100644
index 0000000000..4d57c7e32b
--- /dev/null
+++ b/packages/example/src/GifTest/fill-modes.tsx
@@ -0,0 +1,47 @@
+import {Gif} from '@remotion/gif';
+import {AbsoluteFill, Sequence} from 'remotion';
+
+const GifTest: React.FC = () => {
+ return (
+ <AbsoluteFill
+ style={{
+ flex: 1,
+ flexDirection: 'row',
+ display: 'flex',
+ }}
+ >
+ <Sequence from={0} durationInFrames={30}>
+ <Gif
+ fit="contain"
+ style={{
+ width: 1920,
+ height: 1080,
+ }}
+ src="https://c.tenor.com/pCcL8OOdEBUAAAAC/gifs-away-gif.gif"
+ />
+ </Sequence>
+ <Sequence from={30} durationInFrames={30}>
+ <Gif
+ fit="cover"
+ style={{
+ width: 1920,
+ height: 1080,
+ }}
+ src="https://c.tenor.com/pCcL8OOdEBUAAAAC/gifs-away-gif.gif"
+ />
+ </Sequence>
+ <Sequence from={60} durationInFrames={30}>
+ <Gif
+ fit="fill"
+ style={{
+ width: 1920,
+ height: 1080,
+ }}
+ src="https://c.tenor.com/pCcL8OOdEBUAAAAC/gifs-away-gif.gif"
+ />
+ </Sequence>
+ </AbsoluteFill>
+ );
+};
+
+export default GifTest;
diff --git a/packages/example/src/Video.tsx b/packages/example/src/Video.tsx
index d77f65ed8c..ec95637e18 100644
--- a/packages/example/src/Video.tsx
+++ b/packages/example/src/Video.tsx
@@ -511,6 +511,14 @@ export const Index: React.FC = () => {
fps={30}
durationInFrames={150}
/>
+ <Composition
+ id="gif-fill-modes"
+ lazyComponent={() => import('./GifTest/fill-modes')}
+ width={1920}
+ height={1080}
+ fps={30}
+ durationInFrames={150}
+ />
</Folder>
</>
);
diff --git a/packages/gif/src/GifForDevelopment.tsx b/packages/gif/src/GifForDevelopment.tsx
index 236c5e0c2e..1baa8ed422 100644
--- a/packages/gif/src/GifForDevelopment.tsx
+++ b/packages/gif/src/GifForDevelopment.tsx
@@ -88,8 +88,8 @@ export const GifForDevelopment = forwardRef<
fit={fit}
index={index}
frames={state.frames}
- width={width ?? state.width}
- height={height ?? state.height}
+ width={width}
+ height={height}
{...props}
ref={ref}
/>
diff --git a/packages/gif/src/GifForRendering.tsx b/packages/gif/src/GifForRendering.tsx
index 18c7c46982..921bb7345d 100644
--- a/packages/gif/src/GifForRendering.tsx
+++ b/packages/gif/src/GifForRendering.tsx
@@ -85,8 +85,8 @@ export const GifForRendering = forwardRef<HTMLCanvasElement, RemotionGifProps>(
fit={fit}
index={index}
frames={state.frames}
- width={width ?? state.width}
- height={height ?? state.height}
+ width={width}
+ height={height}
{...props}
ref={ref}
/>
diff --git a/packages/gif/src/canvas.tsx b/packages/gif/src/canvas.tsx
index b1f4d3bed5..92c9be7a74 100644
--- a/packages/gif/src/canvas.tsx
+++ b/packages/gif/src/canvas.tsx
@@ -5,6 +5,7 @@ import {
useRef,
useState,
} from 'react';
+import {useElementSize} from './use-element-size';
const calcArgs = (
fit: 'fill' | 'contain' | 'cover',
@@ -18,7 +19,7 @@ const calcArgs = (
}
): [number, number, number, number, number, number, number, number] => {
switch (fit) {
- case 'fill':
+ case 'fill': {
return [
0,
0,
@@ -29,6 +30,7 @@ const calcArgs = (
canvasSize.width,
canvasSize.height,
];
+ }
case 'contain': {
const ratio = Math.min(
@@ -86,8 +88,8 @@ const makeCanvas = () => {
type Props = {
index: number;
frames: ImageData[];
- width: number;
- height: number;
+ width?: number;
+ height?: number;
fit: 'fill' | 'contain' | 'cover';
className?: string;
style?: React.CSSProperties;
@@ -100,11 +102,17 @@ export const Canvas = forwardRef(
return makeCanvas();
});
+ const size = useElementSize(canvasRef);
+
useImperativeHandle(ref, () => {
return canvasRef.current as HTMLCanvasElement;
});
useEffect(() => {
+ if (!size) {
+ return;
+ }
+
const imageData = frames[index];
const ctx = canvasRef.current?.getContext('2d');
if (imageData && tempCtx && ctx) {
@@ -116,26 +124,26 @@ export const Canvas = forwardRef(
tempCtx.canvas.height = imageData.height;
}
- if (width > 0 && height > 0) {
- ctx.clearRect(0, 0, width, height);
+ if (size.width > 0 && size.height > 0) {
+ ctx.clearRect(0, 0, size.width, size.height);
tempCtx.clearRect(0, 0, tempCtx.canvas.width, tempCtx.canvas.height);
}
tempCtx.putImageData(imageData, 0, 0);
ctx.drawImage(
tempCtx.canvas,
- ...calcArgs(fit, imageData, {width, height})
+ ...calcArgs(fit, imageData, {width: size.width, height: size.height})
);
}
- }, [index, frames, width, height, fit, tempCtx]);
+ }, [index, frames, fit, tempCtx, size]);
return (
<canvas
ref={canvasRef}
className={className}
style={style}
- width={width}
- height={height}
+ width={width ?? size?.width}
+ height={height ?? size?.height}
/>
);
}
diff --git a/packages/gif/src/use-element-size.ts b/packages/gif/src/use-element-size.ts
new file mode 100644
index 0000000000..0e15e4ecc7
--- /dev/null
+++ b/packages/gif/src/use-element-size.ts
@@ -0,0 +1,94 @@
+import {useCallback, useEffect, useMemo, useState} from 'react';
+
+export type Size = {
+ width: number;
+ height: number;
+};
+
+type ElementSizeForceUpdate = () => void;
+
+let elementSizeHooks: ElementSizeForceUpdate[] = [];
+
+export const updateAllElementsSizes = () => {
+ for (const listener of elementSizeHooks) {
+ listener();
+ }
+};
+
+export const useElementSize = (
+ ref: React.RefObject<HTMLElement>
+): Size | null => {
+ const [size, setSize] = useState<Size | null>(null);
+ const observer = useMemo(() => {
+ if (typeof ResizeObserver === 'undefined') {
+ return null;
+ }
+
+ return new ResizeObserver((entries) => {
+ // The contentRect returns the width without any `scale()`'s being applied. The height is wrong
+ const {contentRect} = entries[0];
+ // The clientRect returns the size with `scale()` being applied.
+ const newSize = entries[0].target.getClientRects();
+
+ if (!newSize || !newSize[0]) {
+ setSize(null);
+ return;
+ }
+
+ const probableCssParentScale = newSize[0].width / contentRect.width;
+
+ const width = newSize[0].width * (1 / probableCssParentScale);
+ const height = newSize[0].height * (1 / probableCssParentScale);
+
+ setSize({
+ width,
+ height,
+ });
+ });
+ }, []);
+
+ const updateSize = useCallback(() => {
+ if (!ref.current) {
+ return;
+ }
+
+ const rect = ref.current.getClientRects();
+ if (!rect[0]) {
+ setSize(null);
+ return;
+ }
+
+ setSize({
+ width: rect[0].width as number,
+ height: rect[0].height as number,
+ });
+ }, [ref]);
+
+ useEffect(() => {
+ if (!observer) {
+ return;
+ }
+
+ updateSize();
+ const {current} = ref;
+ if (ref.current) {
+ observer.observe(ref.current);
+ }
+
+ return (): void => {
+ if (current) {
+ observer.unobserve(current);
+ }
+ };
+ }, [observer, ref, updateSize]);
+
+ useEffect(() => {
+ elementSizeHooks.push(updateSize);
+
+ return () => {
+ elementSizeHooks = elementSizeHooks.filter((e) => e !== updateSize);
+ };
+ }, [updateSize]);
+
+ return size;
+};