Skip to content

🎥 Make videos programmatically with React

simplify

Jonny Burgeraa91b22cd35b
diff --git a/packages/gif/.eslintrc b/packages/gif/.eslintrc
index 2e095939f8..c1f331a684 100644
--- a/packages/gif/.eslintrc
+++ b/packages/gif/.eslintrc
@@ -1,6 +1,7 @@
{
"extends": "@jonny",
"rules": {
- "ts-exports/unused-exports": "off"
+ "ts-exports/unused-exports": "off",
+ "react/require-default-props": "off"
}
}
diff --git a/packages/gif/src/canvas.tsx b/packages/gif/src/canvas.tsx
index a83cee6a89..b1f4d3bed5 100644
--- a/packages/gif/src/canvas.tsx
+++ b/packages/gif/src/canvas.tsx
@@ -1,6 +1,10 @@
-import type {ForwardedRef} from 'react';
-import {forwardRef, useEffect, useLayoutEffect, useRef} from 'react';
-import {createCanvasSingleton} from './react-tools';
+import {
+ forwardRef,
+ useEffect,
+ useImperativeHandle,
+ useRef,
+ useState,
+} from 'react';
const calcArgs = (
fit: 'fill' | 'contain' | 'cover',
@@ -69,25 +73,7 @@ const calcArgs = (
}
};
-const combine =
- (
- ...refs: (
- | React.MutableRefObject<unknown>
- | Function
- | ForwardedRef<unknown>
- )[]
- ) =>
- (value: unknown) => {
- refs.forEach((ref) => {
- if (typeof ref === 'function') {
- ref(value);
- } else if (ref !== null && ref !== undefined) {
- ref.current = value;
- }
- });
- };
-
-const useCanvasSingleton = createCanvasSingleton(() => {
+const makeCanvas = () => {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
@@ -95,51 +81,33 @@ const useCanvasSingleton = createCanvasSingleton(() => {
canvas.height = 0;
return ctx;
-});
+};
-export const Canvas = forwardRef(
- (
- {
- index,
- frames,
- width,
- height,
- fit,
- className,
- style,
- }: {
- index: number;
- frames: ImageData[];
- width: number;
- height: number;
- fit: 'fill' | 'contain' | 'cover';
- // eslint-disable-next-line react/require-default-props
- className?: string;
- // eslint-disable-next-line react/require-default-props
- style?: React.CSSProperties;
- },
- ref
- ) => {
- const canvasRef = useRef<HTMLCanvasElement>();
- const ctx = useRef<CanvasRenderingContext2D | null>();
- const tempCtx = useCanvasSingleton();
+type Props = {
+ index: number;
+ frames: ImageData[];
+ width: number;
+ height: number;
+ fit: 'fill' | 'contain' | 'cover';
+ className?: string;
+ style?: React.CSSProperties;
+};
- useLayoutEffect(() => {
- if (canvasRef.current) {
- ctx.current = canvasRef.current.getContext('2d');
- }
- }, [canvasRef]);
+export const Canvas = forwardRef(
+ ({index, frames, width, height, fit, className, style}: Props, ref) => {
+ const canvasRef = useRef<HTMLCanvasElement>(null);
+ const [tempCtx] = useState(() => {
+ return makeCanvas();
+ });
- useLayoutEffect(() => {
- if (canvasRef.current) {
- canvasRef.current.width = width;
- canvasRef.current.height = height;
- }
- }, [canvasRef, width, height]);
+ useImperativeHandle(ref, () => {
+ return canvasRef.current as HTMLCanvasElement;
+ });
useEffect(() => {
const imageData = frames[index];
- if (imageData && tempCtx) {
+ const ctx = canvasRef.current?.getContext('2d');
+ if (imageData && tempCtx && ctx) {
if (
tempCtx.canvas.width < imageData.width ||
tempCtx.canvas.height < imageData.height
@@ -149,12 +117,12 @@ export const Canvas = forwardRef(
}
if (width > 0 && height > 0) {
- ctx.current?.clearRect(0, 0, width, height);
+ ctx.clearRect(0, 0, width, height);
tempCtx.clearRect(0, 0, tempCtx.canvas.width, tempCtx.canvas.height);
}
tempCtx.putImageData(imageData, 0, 0);
- ctx.current?.drawImage(
+ ctx.drawImage(
tempCtx.canvas,
...calcArgs(fit, imageData, {width, height})
);
@@ -163,9 +131,11 @@ export const Canvas = forwardRef(
return (
<canvas
- ref={combine(canvasRef, ref)}
+ ref={canvasRef}
className={className}
style={style}
+ width={width}
+ height={height}
/>
);
}
diff --git a/packages/gif/src/react-tools.ts b/packages/gif/src/react-tools.ts
index 00bb8e23f4..3f17728a32 100644
--- a/packages/gif/src/react-tools.ts
+++ b/packages/gif/src/react-tools.ts
@@ -1,50 +1,7 @@
-import {useLayoutEffect} from 'react';
-
import {generate, parse} from './parse-generate';
import type {GifState} from './props';
import {makeWorker} from './worker';
-type Ref = {
- instance?: CanvasRenderingContext2D | null;
- timeout?: NodeJS.Timeout;
- usageCount?: number;
-};
-
-export const createCanvasSingleton = (
- constructor: () => CanvasRenderingContext2D | null
-): (() => CanvasRenderingContext2D | null) => {
- const ref: Ref = {};
- return () => {
- if (!ref.instance) {
- ref.instance = constructor();
- }
-
- useLayoutEffect(() => {
- if (ref.timeout) {
- clearTimeout(ref.timeout);
- delete ref.timeout;
- } else {
- ref.usageCount = (ref.usageCount || 0) + 1;
- }
-
- return () => {
- ref.timeout = setTimeout(() => {
- if (ref.usageCount !== undefined) {
- ref.usageCount -= 1;
- }
-
- if (ref.usageCount === 0) {
- delete ref.instance;
- delete ref.timeout;
- }
- });
- };
- }, []);
-
- return ref.instance;
- };
-};
-
export const parseGif = async ({
src,
controller,