Skip to content

πŸŽ₯ Make videos programmatically with React

hmm

Jonny Burgerae8ebc319029
diff --git a/packages/core/src/HtmlInCanvas.tsx b/packages/core/src/HtmlInCanvas.tsx
index 22bb1fc2b6..dca572aad2 100644
--- a/packages/core/src/HtmlInCanvas.tsx
+++ b/packages/core/src/HtmlInCanvas.tsx
@@ -164,8 +164,6 @@ declare global {
export type HtmlInCanvasComposeParams = {
readonly canvas: HTMLCanvasElement;
- readonly width: number;
- readonly height: number;
readonly element: HTMLDivElement;
};
@@ -291,8 +289,6 @@ const HtmlInCanvasInner: React.FC<
const handler = onPaintRef.current ?? defaultOnPaint;
await handler({
canvas,
- width: canvas.width,
- height: canvas.height,
element,
});
diff --git a/packages/example/src/HtmlInCanvas/compose-async-bitmap.tsx b/packages/example/src/HtmlInCanvas/compose-async-bitmap.tsx
index f771bae29f..ae61ad9fab 100644
--- a/packages/example/src/HtmlInCanvas/compose-async-bitmap.tsx
+++ b/packages/example/src/HtmlInCanvas/compose-async-bitmap.tsx
@@ -7,20 +7,21 @@ export const HtmlInCanvasComposeAsyncBitmap: React.FC = () => {
const {width, height} = useVideoConfig();
return (
- <AbsoluteFill style={{backgroundColor: 'black'}}>
+ <AbsoluteFill style={{backgroundColor: 'red'}}>
<HtmlInCanvas
width={width}
height={height}
- onPaint={async ({canvas: source, width: w, height: h}) => {
- const bitmap = await createImageBitmap(source);
+ onPaint={async ({canvas, element}) => {
+ const ctx = canvas.getContext('2d');
+ if (!ctx) {
+ return;
+ }
+ ctx.reset();
+ ctx.drawElementImage(element, 0, 0);
+ const bitmap = await createImageBitmap(canvas);
try {
- const ctx = source.getContext('2d');
- if (!ctx) {
- return;
- }
-
- ctx.clearRect(0, 0, w, h);
- ctx.drawImage(bitmap, 0, 0, w, h);
+ ctx.reset();
+ ctx.drawImage(bitmap, 0, 0, width, height);
} finally {
bitmap.close();
}
diff --git a/packages/example/src/HtmlInCanvas/compose-then-effects.tsx b/packages/example/src/HtmlInCanvas/compose-then-effects.tsx
index 2659875a8b..b1fc0127f0 100644
--- a/packages/example/src/HtmlInCanvas/compose-then-effects.tsx
+++ b/packages/example/src/HtmlInCanvas/compose-then-effects.tsx
@@ -31,18 +31,18 @@ export const HtmlInCanvasComposeThenEffects: React.FC = () => {
<HtmlInCanvas
width={width}
height={height}
- onPaint={({canvas: source, width: w, height: h}) => {
- const ctx = source.getContext('2d');
+ onPaint={({canvas}) => {
+ const ctx = canvas.getContext('2d');
if (!ctx) {
return;
}
- ctx.clearRect(0, 0, w, h);
+ ctx.clearRect(0, 0, width, height);
ctx.save();
- ctx.translate(w / 2, h / 2);
+ ctx.translate(width / 2, height / 2);
ctx.scale(scale, scale);
- ctx.translate(-w / 2, -h / 2);
- ctx.drawImage(source, 0, 0, w, h);
+ ctx.translate(-width / 2, -height / 2);
+ ctx.drawImage(canvas, 0, 0, width, height);
ctx.restore();
}}
_experimentalEffects={[blur({radius: blurRadius})]}
diff --git a/packages/example/src/HtmlInCanvas/compose-webgl.tsx b/packages/example/src/HtmlInCanvas/compose-webgl.tsx
index 4005ddca47..71f5d86438 100644
--- a/packages/example/src/HtmlInCanvas/compose-webgl.tsx
+++ b/packages/example/src/HtmlInCanvas/compose-webgl.tsx
@@ -135,26 +135,21 @@ export const HtmlInCanvasComposeWebGL: React.FC = () => {
useEffect(() => () => disposeGpu(gpuRef), []);
- const onCompose = useCallback(
- ({
- canvas: source,
- target,
- width: w,
- height: h,
- }: HtmlInCanvasComposeParams) => {
+ const onPaint = useCallback(
+ ({canvas}: HtmlInCanvasComposeParams) => {
const c = Math.cos(rotation);
const s = Math.sin(rotation);
const mat = new Float32Array([c, -s, 0, s, c, 0, 0, 0, 1]);
- const gpu = ensureGpu(target, gpuRef);
+ const gpu = ensureGpu(canvas, gpuRef);
if (!gpu) {
- const ctx = target.getContext('2d');
+ const ctx = canvas.getContext('2d');
if (!ctx) {
return;
}
- ctx.clearRect(0, 0, w, h);
- ctx.drawImage(source, 0, 0, w, h);
+ ctx.clearRect(0, 0, width, height);
+ ctx.drawImage(canvas, 0, 0, width, height);
return;
}
@@ -170,7 +165,7 @@ export const HtmlInCanvasComposeWebGL: React.FC = () => {
gl.RGBA,
gl.RGBA,
gl.UNSIGNED_BYTE,
- source as TexImageSource,
+ canvas as TexImageSource,
);
if (gpu.uTex) {
@@ -189,7 +184,7 @@ export const HtmlInCanvasComposeWebGL: React.FC = () => {
return (
<AbsoluteFill style={{backgroundColor: 'black'}}>
- <HtmlInCanvas width={width} height={height} onPaint={onCompose}>
+ <HtmlInCanvas width={width} height={height} onPaint={onPaint}>
<HtmlInCanvasScene />
</HtmlInCanvas>
</AbsoluteFill>
diff --git a/packages/example/src/HtmlInCanvas/effects-demo.tsx b/packages/example/src/HtmlInCanvas/effects-demo.tsx
index 5046e72640..a542bec9f4 100644
--- a/packages/example/src/HtmlInCanvas/effects-demo.tsx
+++ b/packages/example/src/HtmlInCanvas/effects-demo.tsx
@@ -19,7 +19,7 @@ export const HtmlInCanvasEffectsDemo: React.FC = () => {
<HtmlInCanvas
width={size}
height={size}
- onPaint={({canvas, element, width: w, height: h}) => {
+ onPaint={({canvas, element}) => {
const ctx = canvas.getContext('2d', {willReadFrequently: true});
if (!ctx) {
throw new Error(
@@ -31,9 +31,20 @@ export const HtmlInCanvasEffectsDemo: React.FC = () => {
// what makes the effect impossible in pure HTML/CSS β€” we need
// actual sampled pixel data from the rendered subtree.
ctx.reset();
- const transform = ctx.drawElementImage(element, 0, 0, w, h);
+ const transform = ctx.drawElementImage(
+ element,
+ 0,
+ 0,
+ canvas.width,
+ canvas.height,
+ );
element.style.transform = transform.toString();
- const {data: pixels} = ctx.getImageData(0, 0, w, h);
+ const {data: pixels} = ctx.getImageData(
+ 0,
+ 0,
+ canvas.width,
+ canvas.height,
+ );
// 2. Repaint as a halftone dot grid with per-channel offsets:
// each cell samples a region's average color, then draws three
@@ -44,22 +55,23 @@ export const HtmlInCanvasEffectsDemo: React.FC = () => {
const cell = Math.max(2, Math.round(cellSize));
const maxR = (cell / 2) * Math.SQRT2;
const offset = cell * 0.18;
+ const {width, height} = canvas;
ctx.globalCompositeOperation = 'lighter';
- for (let y = 0; y < h; y += cell) {
- for (let x = 0; x < w; x += cell) {
+ for (let y = 0; y < height; y += cell) {
+ for (let x = 0; x < width; x += cell) {
let r = 0;
let g = 0;
let b = 0;
let a = 0;
let count = 0;
- const yEnd = Math.min(y + cell, h);
- const xEnd = Math.min(x + cell, w);
+ const yEnd = Math.min(y + cell, height);
+ const xEnd = Math.min(x + cell, width);
for (let py = y; py < yEnd; py += 2) {
for (let px = x; px < xEnd; px += 2) {
- const i = (py * w + px) * 4;
+ const i = (py * width + px) * 4;
r += pixels[i];
g += pixels[i + 1];
b += pixels[i + 2];