Skip to content

🎥 Make videos programmatically with React

continueRender in finally: shader draw exceptions must not leak the delayRender handle

BarriBarri20aceb44a97ba8
diff --git a/packages/transitions/src/html-in-canvas-presentation.tsx b/packages/transitions/src/html-in-canvas-presentation.tsx
index c977c0c8cd..5784da61ff 100644
--- a/packages/transitions/src/html-in-canvas-presentation.tsx
+++ b/packages/transitions/src/html-in-canvas-presentation.tsx
@@ -113,53 +113,54 @@ export const HtmlInCanvasPresentation = <
}
const handle = delayRender('onPaint');
- const clearOutput = () => {
- const context = outputCanvas.getContext('2d');
- if (!context) {
- throw new Error('Failed to create output canvas context');
+ try {
+ const clearOutput = () => {
+ const context = outputCanvas.getContext('2d');
+ if (!context) {
+ throw new Error('Failed to create output canvas context');
+ }
+
+ context.clearRect(0, 0, outputCanvas.width, outputCanvas.height);
+ };
+
+ if (!prevImage && !nextImage) {
+ instance.clear();
+ clearOutput();
+ return;
}
- context.clearRect(0, 0, outputCanvas.width, outputCanvas.height);
- };
-
- if (!prevImage && !nextImage) {
- instance.clear();
- clearOutput();
- continueRender(handle);
- return;
- }
+ const width = prevImage?.width ?? nextImage?.width ?? 0;
+ const height = prevImage?.height ?? nextImage?.height ?? 0;
- const width = prevImage?.width ?? nextImage?.width ?? 0;
- const height = prevImage?.height ?? nextImage?.height ?? 0;
+ if (width === 0 || height === 0) {
+ instance.clear();
+ clearOutput();
+ return;
+ }
- if (width === 0 || height === 0) {
- instance.clear();
- clearOutput();
+ shaderCanvas.width = width;
+ shaderCanvas.height = height;
+
+ instance.draw({
+ prevImage,
+ nextImage,
+ width,
+ height,
+ time: progress,
+ passedProps: passedPropsRef.current,
+ });
+
+ await Internals.runEffectChain({
+ state: chainState.get(width, height)!,
+ source: shaderCanvas,
+ effects: effectsRef.current ?? [],
+ width,
+ height,
+ output: outputCanvas,
+ });
+ } finally {
continueRender(handle);
- return;
}
-
- shaderCanvas.width = width;
- shaderCanvas.height = height;
-
- instance.draw({
- prevImage,
- nextImage,
- width,
- height,
- time: progress,
- passedProps: passedPropsRef.current,
- });
-
- await Internals.runEffectChain({
- state: chainState.get(width, height)!,
- source: shaderCanvas,
- effects: effectsRef.current ?? [],
- width,
- height,
- output: outputCanvas,
- });
- continueRender(handle);
},
[chainState, continueRender, delayRender, instance, shaderCanvas],
);