Skip to content

🎥 Make videos programmatically with React

cool

JonnyBurger70cac3160ad7
diff --git a/packages/core/src/canvas-effects/Solid.tsx b/packages/core/src/canvas-effects/Solid.tsx
index 5e4b95a8c8..61ee00d419 100644
--- a/packages/core/src/canvas-effects/Solid.tsx
+++ b/packages/core/src/canvas-effects/Solid.tsx
@@ -43,7 +43,7 @@ export const Solid: React.FC<SolidProps> = ({
return canvas;
}, []);
- const chainState = useEffectChainState(width, height);
+ const chainState = useEffectChainState();
// Fill source and run effect chain on every frame / color change.
useEffect(() => {
@@ -89,7 +89,7 @@ export const Solid: React.FC<SolidProps> = ({
ctx.fillRect(0, 0, 1, 1);
runEffectChain({
- state: chainState.current!,
+ state: chainState.get(width, height)!,
source: sourceCanvas,
effects: experimentalEffects,
output: outputCanvas,
diff --git a/packages/core/src/canvas-effects/run-effect-chain.ts b/packages/core/src/canvas-effects/run-effect-chain.ts
index 180c8ee20b..facfa4d322 100644
--- a/packages/core/src/canvas-effects/run-effect-chain.ts
+++ b/packages/core/src/canvas-effects/run-effect-chain.ts
@@ -165,8 +165,6 @@ export const runEffectChain = async ({
throw new Error('Failed to acquire 2D context for output canvas');
}
- console.log({lastTarget: lastTarget.width, output: output.width});
-
outCtx.clearRect(0, 0, width, height);
outCtx.drawImage(lastTarget, 0, 0, width, height);
return true;
diff --git a/packages/core/src/canvas-effects/use-effect-chain-state.ts b/packages/core/src/canvas-effects/use-effect-chain-state.ts
index 2d03f9e5b3..aaa5fe1c38 100644
--- a/packages/core/src/canvas-effects/use-effect-chain-state.ts
+++ b/packages/core/src/canvas-effects/use-effect-chain-state.ts
@@ -1,38 +1,16 @@
-import type {RefObject} from 'react';
-import {useEffect, useRef} from 'react';
-import {ENABLE_EFFECTS} from '../enable-effects.js';
+import {useEffect, useMemo, useRef} from 'react';
import type {EffectChainState} from './run-effect-chain.js';
import {
cleanupEffectChainState,
createEffectChainState,
} from './run-effect-chain.js';
-export const useEffectChainState = (
- width: number,
- height: number,
-): RefObject<EffectChainState | null> | null => {
+export const useEffectChainState = (): {
+ get: (width: number, height: number) => EffectChainState | null;
+} => {
const chainStateRef = useRef<EffectChainState | null>(null);
const sizeRef = useRef<{width: number; height: number} | null>(null);
- if (ENABLE_EFFECTS) {
- if (
- !sizeRef.current ||
- sizeRef.current.width !== width ||
- sizeRef.current.height !== height
- ) {
- if (chainStateRef.current) {
- cleanupEffectChainState(chainStateRef.current);
- }
-
- chainStateRef.current = createEffectChainState(width, height);
- sizeRef.current = {width, height};
- }
- } else if (chainStateRef.current) {
- cleanupEffectChainState(chainStateRef.current);
- chainStateRef.current = null;
- sizeRef.current = null;
- }
-
useEffect(() => {
return () => {
if (chainStateRef.current) {
@@ -41,9 +19,25 @@ export const useEffectChainState = (
};
}, []);
- if (!ENABLE_EFFECTS) {
- return null;
- }
+ return useMemo(
+ () => ({
+ get: (width, height) => {
+ if (
+ !sizeRef.current ||
+ sizeRef.current.width !== width ||
+ sizeRef.current.height !== height
+ ) {
+ if (chainStateRef.current) {
+ cleanupEffectChainState(chainStateRef.current);
+ }
+
+ chainStateRef.current = createEffectChainState(width, height);
+ sizeRef.current = {width, height};
+ }
- return chainStateRef;
+ return chainStateRef.current;
+ },
+ }),
+ [],
+ );
};
diff --git a/packages/example/src/HtmlInCanvas/compose-webgpu.tsx b/packages/example/src/HtmlInCanvas/compose-webgpu.tsx
index fe861be9f4..4aa58be82d 100644
--- a/packages/example/src/HtmlInCanvas/compose-webgpu.tsx
+++ b/packages/example/src/HtmlInCanvas/compose-webgpu.tsx
@@ -12,6 +12,7 @@ import {
// matching the convention in `packages/core/src/canvas-effects/gpu-device.ts`.
type Gpu = {
requestAdapter(): Promise<GpuAdapter | null>;
+ getPreferredCanvasFormat(): string;
};
type GpuAdapter = {requestDevice(): Promise<GpuDevice>};
type GpuTextureView = unknown;
@@ -57,8 +58,6 @@ type GpuCanvasContext = {
getCurrentTexture(): GpuTexture;
};
-const PRESENTATION_FORMAT = 'rgba8unorm';
-
const WGSL = /* wgsl */ `
struct VsOut {
@builtin(position) pos: vec4f,
@@ -147,9 +146,13 @@ export const HtmlInCanvasComposeWebGPU: React.FC = () => {
throw new Error('WebGPU context unavailable on OffscreenCanvas');
}
+ // Use the device's preferred swap-chain format (typically `bgra8unorm`)
+ // to avoid an extra format-conversion copy on present.
+ const presentationFormat = gpu.getPreferredCanvasFormat();
+
context.configure({
device,
- format: PRESENTATION_FORMAT,
+ format: presentationFormat,
alphaMode: 'premultiplied',
});
@@ -161,7 +164,7 @@ export const HtmlInCanvasComposeWebGPU: React.FC = () => {
fragment: {
module,
entryPoint: 'fs',
- targets: [{format: PRESENTATION_FORMAT}],
+ targets: [{format: presentationFormat}],
},
primitive: {topology: 'triangle-list'},
});
diff --git a/packages/media/src/media-player.ts b/packages/media/src/media-player.ts
index 96ff09e16b..e3ef67557b 100644
--- a/packages/media/src/media-player.ts
+++ b/packages/media/src/media-player.ts
@@ -70,7 +70,11 @@ export class MediaPlayer {
null;
private getEffects: () => EffectsProp;
- private getEffectChainState: () => EffectChainState | null;
+ private getEffectChainState: (
+ width: number,
+ height: number,
+ ) => EffectChainState | null;
+
private getCurrentFrame: () => number;
private initializationPromise: Promise<MediaPlayerInitResult> | null = null;
@@ -129,7 +133,10 @@ export class MediaPlayer {
credentials: RequestCredentials | undefined;
tagType: 'audio' | 'video';
getEffects: () => EffectsProp;
- getEffectChainState: () => EffectChainState | null;
+ getEffectChainState: (
+ width: number,
+ height: number,
+ ) => EffectChainState | null;
getCurrentFrame: () => number;
}) {
this.canvas = canvas ?? null;
diff --git a/packages/media/src/video-iterator-manager.ts b/packages/media/src/video-iterator-manager.ts
index c48001672d..7d3a5a63bf 100644
--- a/packages/media/src/video-iterator-manager.ts
+++ b/packages/media/src/video-iterator-manager.ts
@@ -38,7 +38,10 @@ export const videoIteratorManager = async ({
getStartTime: () => number;
getIsLooping: () => boolean;
getEffects: () => EffectsProp;
- getEffectChainState: () => EffectChainState | null;
+ getEffectChainState: (
+ width: number,
+ height: number,
+ ) => EffectChainState | null;
getCurrentFrame: () => number;
}) => {
let videoIteratorsCreated = 0;
@@ -67,7 +70,7 @@ export const videoIteratorManager = async ({
const drawFrame = async (frame: WrappedCanvas): Promise<void> => {
if (context && canvas) {
const effects = getEffects();
- const chainState = getEffectChainState();
+ const chainState = getEffectChainState(canvas.width, canvas.height);
if (
ENABLE_EFFECTS &&
effects.length > 0 &&
diff --git a/packages/media/src/video/video-for-preview.tsx b/packages/media/src/video/video-for-preview.tsx
index 34d01e441e..3196e80995 100644
--- a/packages/media/src/video/video-for-preview.tsx
+++ b/packages/media/src/video/video-for-preview.tsx
@@ -151,10 +151,7 @@ const VideoForPreviewAssertedShowing: React.FC<
warnAboutTooHighVolume(userPreferredVolume);
- const effectChainState = useEffectChainState(
- videoConfig.width,
- videoConfig.height,
- );
+ const effectChainState = useEffectChainState();
const experimentalEffectsRef = useRef(_experimentalEffects);
experimentalEffectsRef.current = _experimentalEffects;
const memoizedEffects = useMemoizedEffects(
@@ -326,7 +323,8 @@ const VideoForPreviewAssertedShowing: React.FC<
tagType: 'video',
getEffects: () =>
ENABLE_EFFECTS ? experimentalEffectsRef.current : [],
- getEffectChainState: () => effectChainStateRef.current?.current!,
+ getEffectChainState: (width, height) =>
+ effectChainStateRef.current?.get(width, height)!,
getCurrentFrame: () => frameRef.current,
});