🎥 Make videos programmatically with React
cool
JonnyBurgerd44563f441dd
diff --git a/packages/example/src/HtmlInCanvas/complex-text.tsx b/packages/example/src/HtmlInCanvas/complex-text.tsxnew file mode 100644index 0000000000..011f27e514--- /dev/null+++ b/packages/example/src/HtmlInCanvas/complex-text.tsx@@ -0,0 +1,69 @@+import React from 'react';+import {AbsoluteFill, HtmlInCanvas, staticFile} from 'remotion';++// Port of https://github.com/WICG/html-in-canvas/blob/main/Examples/complex-text.html —+// demonstrates rasterizing rich text (RTL, vertical writing mode, emoji,+// inline `<img>` and inline `<svg>`) through `drawElementImage`, then+// applying a 2D-context rotation/translation to the bitmap.+export const HtmlInCanvasComplexText: React.FC = () => {+ return (+ <AbsoluteFill+ style={{+ justifyContent: 'center',+ alignItems: 'center',+ backgroundColor: 'white',+ }}+ >+ <HtmlInCanvas+ width={1276}+ height={636}+ style={{border: '2px solid blue'}}+ onPaint={({canvas, element, elementImage}) => {+ const ctx = canvas.getContext('2d');+ if (!ctx) {+ throw new Error('Failed to acquire 2D context');+ }++ ctx.reset();+ ctx.rotate((15 * Math.PI) / 180);+ ctx.translate(160, -40);+ const transform = ctx.drawElementImage(elementImage, 0, 0);+ element.style.transform = transform.toString();+ }}+ >+ <div+ style={{+ width: 1100,+ fontSize: 36,+ fontFamily: 'sans-serif',+ lineHeight: 1.4,+ color: 'black',+ }}+ >+ Hello from{' '}+ <a href="https://github.com/WICG/html-in-canvas">html-in-canvas</a>!+ <br />+ I'm multi-line, <b>formatted</b>, rotated text with emoji+ (😀), RTL text <span dir="rtl">من فارسی صحبت میکنم</span>,+ vertical text,+ <p style={{writingMode: 'vertical-rl'}}>这是垂直文本</p>+ an inline image (+ <img+ width={150}+ src={staticFile('1.jpg')}+ alt="inline"+ style={{verticalAlign: 'middle'}}+ />+ ), and{' '}+ <svg width={50} height={50} style={{verticalAlign: 'middle'}}>+ <circle cx={25} cy={25} r={20} fill="green" />+ <text x={25} y={30} fontSize={15} textAnchor="middle" fill="#fff">+ SVG+ </text>+ </svg>+ !+ </div>+ </HtmlInCanvas>+ </AbsoluteFill>+ );+};diff --git a/packages/example/src/HtmlInCanvas/compose-webgpu.tsx b/packages/example/src/HtmlInCanvas/compose-webgpu.tsxnew file mode 100644index 0000000000..fe861be9f4--- /dev/null+++ b/packages/example/src/HtmlInCanvas/compose-webgpu.tsx@@ -0,0 +1,308 @@+import React, {useCallback, useRef} from 'react';+import {+ AbsoluteFill,+ HtmlInCanvas,+ type HtmlInCanvasOnInit,+ type HtmlInCanvasOnPaint,+ useCurrentFrame,+ useVideoConfig,+} from 'remotion';++// Minimal WebGPU types — `@webgpu/types` is intentionally not a dependency,+// matching the convention in `packages/core/src/canvas-effects/gpu-device.ts`.+type Gpu = {+ requestAdapter(): Promise<GpuAdapter | null>;+};+type GpuAdapter = {requestDevice(): Promise<GpuDevice>};+type GpuTextureView = unknown;+type GpuTexture = {createView(): GpuTextureView; destroy(): void};+type GpuBuffer = {destroy(): void};+type GpuBindGroup = unknown;+type GpuPipeline = unknown;+type GpuSampler = unknown;+type GpuShaderModule = unknown;+type GpuDevice = {+ createShaderModule(d: {code: string}): GpuShaderModule;+ createRenderPipeline(d: unknown): GpuPipeline;+ createTexture(d: unknown): GpuTexture;+ createSampler(d?: unknown): GpuSampler;+ createBindGroup(d: unknown): GpuBindGroup;+ createBuffer(d: unknown): GpuBuffer;+ createCommandEncoder(): {+ beginRenderPass(d: unknown): {+ setPipeline(p: GpuPipeline): void;+ setBindGroup(i: number, b: GpuBindGroup): void;+ draw(n: number): void;+ end(): void;+ };+ finish(): unknown;+ };+ queue: {+ submit(c: unknown[]): void;+ writeBuffer(b: GpuBuffer, offset: number, data: BufferSource): void;+ copyElementImageToTexture(+ source: Element | ElementImage,+ width: number,+ height: number,+ destination: {texture: GpuTexture},+ ): void;+ };+};+type GpuCanvasContext = {+ configure(d: {+ device: GpuDevice;+ format: string;+ alphaMode: 'premultiplied' | 'opaque';+ }): void;+ getCurrentTexture(): GpuTexture;+};++const PRESENTATION_FORMAT = 'rgba8unorm';++const WGSL = /* wgsl */ `+struct VsOut {+ @builtin(position) pos: vec4f,+ @location(0) uv: vec2f,+};++@vertex+fn vs(@builtin(vertex_index) i: u32) -> VsOut {+ // Fullscreen triangle (slightly oversized — clipped to viewport).+ var p = array(vec2f(-1.0, -3.0), vec2f(-1.0, 1.0), vec2f(3.0, 1.0));+ var uv = array(vec2f(0.0, 2.0), vec2f(0.0, 0.0), vec2f(2.0, 0.0));+ var o: VsOut;+ o.pos = vec4f(p[i], 0.0, 1.0);+ o.uv = uv[i];+ return o;+}++struct U {+ time: f32,+ _pad: f32,+ resolution: vec2f,+};++@group(0) @binding(0) var samp: sampler;+@group(0) @binding(1) var tex: texture_2d<f32>;+@group(0) @binding(2) var<uniform> u: U;++@fragment+fn fs(in: VsOut) -> @location(0) vec4f {+ // Animate pixel cell size with a slow breathing motion.+ let cell = 6.0 + sin(u.time * 0.8) * 4.0;+ let snapped = floor(in.uv * u.resolution / cell) * cell / u.resolution;++ // Slight chromatic offset between channels — sampled from snapped centers.+ let off = vec2f(2.0, 0.0) / u.resolution;+ let r = textureSample(tex, samp, snapped + off).r;+ let g = textureSample(tex, samp, snapped).g;+ let b = textureSample(tex, samp, snapped - off).b;+ let a = textureSample(tex, samp, snapped).a;++ // Posterize to 5 levels per channel for a flatter, screenprint look.+ let levels = 5.0;+ let q = floor(vec3f(r, g, b) * levels) / (levels - 1.0);++ return vec4f(q, a);+}+`;++type GpuState = {+ device: GpuDevice;+ context: GpuCanvasContext;+ pipeline: GpuPipeline;+ sampler: GpuSampler;+ texture: GpuTexture;+ uniformBuffer: GpuBuffer;+ bindGroup: GpuBindGroup;+ width: number;+ height: number;+};++export const HtmlInCanvasComposeWebGPU: React.FC = () => {+ const frame = useCurrentFrame();+ const {width, height, fps} = useVideoConfig();+ const gpuRef = useRef<GpuState | null>(null);++ const time = frame / fps;++ const onInit: HtmlInCanvasOnInit = useCallback(async ({canvas}) => {+ if (typeof navigator === 'undefined' || !('gpu' in navigator)) {+ throw new Error('WebGPU is not available in this environment');+ }++ const gpu = (navigator as unknown as {gpu: Gpu}).gpu;+ const adapter = await gpu.requestAdapter();+ if (!adapter) {+ throw new Error('No WebGPU adapter available');+ }++ const device = await adapter.requestDevice();+ const context = (+ canvas as unknown as {+ getContext(id: 'webgpu'): GpuCanvasContext | null;+ }+ ).getContext('webgpu');+ if (!context) {+ throw new Error('WebGPU context unavailable on OffscreenCanvas');+ }++ context.configure({+ device,+ format: PRESENTATION_FORMAT,+ alphaMode: 'premultiplied',+ });++ const module = device.createShaderModule({code: WGSL});++ const pipeline = device.createRenderPipeline({+ layout: 'auto',+ vertex: {module, entryPoint: 'vs'},+ fragment: {+ module,+ entryPoint: 'fs',+ targets: [{format: PRESENTATION_FORMAT}],+ },+ primitive: {topology: 'triangle-list'},+ });++ const TextureUsage = (+ globalThis as unknown as {+ GPUTextureUsage: {+ COPY_DST: number;+ TEXTURE_BINDING: number;+ RENDER_ATTACHMENT: number;+ };+ }+ ).GPUTextureUsage;+ const BufferUsage = (+ globalThis as unknown as {+ GPUBufferUsage: {UNIFORM: number; COPY_DST: number};+ }+ ).GPUBufferUsage;++ const texture = device.createTexture({+ size: {width: canvas.width, height: canvas.height},+ format: 'rgba8unorm',+ usage:+ TextureUsage.COPY_DST |+ TextureUsage.TEXTURE_BINDING |+ TextureUsage.RENDER_ATTACHMENT,+ });++ const sampler = device.createSampler({+ magFilter: 'linear',+ minFilter: 'linear',+ addressModeU: 'clamp-to-edge',+ addressModeV: 'clamp-to-edge',+ });++ // 16 bytes: time (f32), pad (f32), resolution (vec2f).+ const uniformBuffer = device.createBuffer({+ size: 16,+ usage: BufferUsage.UNIFORM | BufferUsage.COPY_DST,+ });++ const bindGroup = device.createBindGroup({+ layout: (+ pipeline as unknown as {+ getBindGroupLayout(i: number): unknown;+ }+ ).getBindGroupLayout(0),+ entries: [+ {binding: 0, resource: sampler},+ {binding: 1, resource: texture.createView()},+ {binding: 2, resource: {buffer: uniformBuffer}},+ ],+ });++ gpuRef.current = {+ device,+ context,+ pipeline,+ sampler,+ texture,+ uniformBuffer,+ bindGroup,+ width: canvas.width,+ height: canvas.height,+ };++ return () => {+ texture.destroy();+ uniformBuffer.destroy();+ gpuRef.current = null;+ };+ }, []);++ const onPaint: HtmlInCanvasOnPaint = useCallback(+ ({elementImage}) => {+ const gpu = gpuRef.current;+ if (!gpu) {+ return;+ }++ const {device, context, pipeline, texture, bindGroup, uniformBuffer} =+ gpu;++ device.queue.copyElementImageToTexture(+ elementImage,+ gpu.width,+ gpu.height,+ {texture},+ );++ const uniforms = new Float32Array([time, 0, gpu.width, gpu.height]);+ device.queue.writeBuffer(uniformBuffer, 0, uniforms);++ const encoder = device.createCommandEncoder();+ const view = context.getCurrentTexture().createView();+ const pass = encoder.beginRenderPass({+ colorAttachments: [+ {+ view,+ clearValue: {r: 0, g: 0, b: 0, a: 0},+ loadOp: 'clear',+ storeOp: 'store',+ },+ ],+ });+ pass.setPipeline(pipeline);+ pass.setBindGroup(0, bindGroup);+ pass.draw(3);+ pass.end();+ device.queue.submit([encoder.finish()]);+ },+ [time],+ );++ return (+ <AbsoluteFill+ style={{+ justifyContent: 'center',+ alignItems: 'center',+ }}+ >+ <HtmlInCanvas+ width={width}+ height={height}+ onInit={onInit}+ onPaint={onPaint}+ >+ <AbsoluteFill+ style={{+ backgroundColor: 'white',+ color: 'black',+ justifyContent: 'center',+ alignItems: 'center',+ fontSize: 120,+ fontFamily: 'sans-serif',+ fontWeight: 'bold',+ }}+ >+ <h1>Hello, World!</h1>+ </AbsoluteFill>+ </HtmlInCanvas>+ </AbsoluteFill>+ );+};diff --git a/packages/example/src/HtmlInCanvas/index.tsx b/packages/example/src/HtmlInCanvas/index.tsxindex 5508c6b687..410c7cccae 100644--- a/packages/example/src/HtmlInCanvas/index.tsx+++ b/packages/example/src/HtmlInCanvas/index.tsx@@ -1,6 +1,8 @@+export {HtmlInCanvasComplexText} from './complex-text';export {HtmlInCanvasComposeAsyncBitmap} from './compose-async-bitmap';export {HtmlInCanvasComposeWebGL} from './compose-webgl';export {HtmlInCanvasComposeWebGLCrt} from './compose-webgl-crt';+export {HtmlInCanvasComposeWebGPU} from './compose-webgpu';export {HtmlInCanvasEffectsDemo,HtmlInCanvasEffectsDemo as HtmlInCanvasDemo,diff --git a/packages/example/src/NewVideo.tsx b/packages/example/src/NewVideo.tsxindex bceea004a0..5e48d9cfd0 100644--- a/packages/example/src/NewVideo.tsx+++ b/packages/example/src/NewVideo.tsx@@ -23,14 +23,7 @@ export const Component = () => {return (<>(- <Video- src={src}- debugOverlay- _experimentalEffects={[halftone(), tint({color: 'green', amount: 1})]}- style={{- translate: '-3px 434px',- }}- />+ <Video src={src} debugOverlay _experimentalEffects={[halftone()]} />)( )</>);diff --git a/packages/example/src/Root.tsx b/packages/example/src/Root.tsxindex 54ed34d558..f88209bd69 100644--- a/packages/example/src/Root.tsx+++ b/packages/example/src/Root.tsx@@ -44,9 +44,11 @@ import {HlsMediaVideoTrimmed,} from './Hls/HlsMediaVideo';import {+ HtmlInCanvasComplexText,HtmlInCanvasComposeAsyncBitmap,HtmlInCanvasComposeWebGL,HtmlInCanvasComposeWebGLCrt,+ HtmlInCanvasComposeWebGPU,HtmlInCanvasDemo,HtmlInCanvasReactSvg,} from './HtmlInCanvas';@@ -916,6 +918,22 @@ export const Index: React.FC = () => {width={1920}durationInFrames={120}/>+ <Composition+ id="html-in-canvas-compose-webgpu"+ component={HtmlInCanvasComposeWebGPU}+ fps={30}+ height={1080}+ width={1920}+ durationInFrames={120}+ />+ <Composition+ id="html-in-canvas-complex-text"+ component={HtmlInCanvasComplexText}+ fps={30}+ height={720}+ width={1280}+ durationInFrames={60}+ /><Compositionid="html-in-canvas-react-svg"component={HtmlInCanvasReactSvg}