Skip to content

🎥 Make videos programmatically with React

videooncanvas

Jonny Burger617a71bfd896
diff --git a/packages/example/src/RemoteVideo/index.tsx b/packages/example/src/RemoteVideo/index.tsx
index b0fe420b60..19df74b84d 100644
--- a/packages/example/src/RemoteVideo/index.tsx
+++ b/packages/example/src/RemoteVideo/index.tsx
@@ -1,12 +1,8 @@
-import {useRef} from 'react';
import {interpolate, Video} from 'remotion';
const RemoteVideo: React.FC = () => {
- const ref = useRef<HTMLVideoElement>(null);
-
return (
<Video
- ref={ref}
volume={(f) =>
interpolate(f, [0, 500], [1, 0], {extrapolateRight: 'clamp'})
}
diff --git a/packages/example/src/Video.tsx b/packages/example/src/Video.tsx
index 9d39492472..871d4bae92 100644
--- a/packages/example/src/Video.tsx
+++ b/packages/example/src/Video.tsx
@@ -13,6 +13,7 @@ import {SeriesTesting} from './StaggerTesting';
import {StaticDemo} from './StaticServer';
import {TenFrameTester} from './TenFrameTester';
import ThreeBasic from './ThreeBasic';
+import {VideoOnCanvas} from './VideoOnCanvas';
import {VideoSpeed} from './VideoSpeed';
import {VideoTesting} from './VideoTesting';
@@ -301,6 +302,14 @@ export const Index: React.FC = () => {
fps={30}
durationInFrames={100}
/>
+ <Composition
+ id="video-on-canvas"
+ component={VideoOnCanvas}
+ width={1920}
+ height={1080}
+ fps={30}
+ durationInFrames={100}
+ />
<Still id="Orb" component={OrbScene} width={2000} height={2000} />
<Still
id="static-demo"
diff --git a/packages/example/src/VideoOnCanvas/index.tsx b/packages/example/src/VideoOnCanvas/index.tsx
new file mode 100644
index 0000000000..be669e0861
--- /dev/null
+++ b/packages/example/src/VideoOnCanvas/index.tsx
@@ -0,0 +1,52 @@
+import {useVideoConfig, Video} from 'remotion';
+import {useCallback, useEffect, useRef} from 'react';
+import React from 'react';
+import {AbsoluteFill} from 'remotion';
+
+export const VideoOnCanvas: React.FC = () => {
+ const video = useRef<HTMLVideoElement>(null);
+ const canvas = useRef<HTMLCanvasElement>(null);
+ const {width, height} = useVideoConfig();
+
+ const onVideoFrame = useCallback(() => {
+ if (
+ !canvas.current ||
+ !video.current ||
+ !video.current.requestVideoFrameCallback
+ ) {
+ return;
+ }
+ const context = canvas.current.getContext('2d');
+
+ if (!context) {
+ return;
+ }
+
+ context.filter = 'grayscale(100%)';
+ context.drawImage(video.current, 0, 0, width, height);
+ video.current.requestVideoFrameCallback(() => onVideoFrame());
+ }, [height, width]);
+
+ useEffect(() => {
+ if (!video.current || !video.current.requestVideoFrameCallback) {
+ return;
+ }
+ video.current.requestVideoFrameCallback(() => onVideoFrame());
+ }, [onVideoFrame]);
+
+ return (
+ <AbsoluteFill>
+ <AbsoluteFill>
+ <Video
+ ref={video}
+ style={{opacity: 0}}
+ startFrom={300}
+ src="http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"
+ />
+ </AbsoluteFill>
+ <AbsoluteFill>
+ <canvas ref={canvas} width={width} height={height} />
+ </AbsoluteFill>
+ </AbsoluteFill>
+ );
+};