Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,11 @@
"cross-env": "^7.0.3",
"dotenv": "^16.5.0",
"drizzle-kit": "^0.31.4",
"patch-package": "^8.0.1",
"postcss": "^8",
"sharp": "^0.34.5",
"tailwindcss": "^4.2.1",
"typescript": "^5.8.3",
"wrangler": "^4.77.0"
}
}
}
177 changes: 90 additions & 87 deletions apps/web/src/services/renderer/canvas-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,105 +5,108 @@ import { wasmCompositor } from "./compositor/wasm-compositor";
import { resolveRenderTree } from "./resolve";

export type CanvasRendererParams = {
width: number;
height: number;
fps: FrameRate;
width: number;
height: number;
fps: FrameRate;
};

export class CanvasRenderer {
canvas: OffscreenCanvas | HTMLCanvasElement;
context: OffscreenCanvasRenderingContext2D | CanvasRenderingContext2D;
width: number;
height: number;
fps: FrameRate;
canvas: OffscreenCanvas | HTMLCanvasElement;
context: OffscreenCanvasRenderingContext2D | CanvasRenderingContext2D;
width: number;
height: number;
fps: FrameRate;

constructor({ width, height, fps }: CanvasRendererParams) {
this.width = width;
this.height = height;
this.fps = fps;
constructor({ width, height, fps }: CanvasRendererParams) {
this.width = width;
this.height = height;
this.fps = fps;

try {
this.canvas = new OffscreenCanvas(width, height);
} catch {
this.canvas = document.createElement("canvas");
this.canvas.width = width;
this.canvas.height = height;
}
try {
this.canvas = new OffscreenCanvas(width, height);
} catch {
this.canvas = document.createElement("canvas");
this.canvas.width = width;
this.canvas.height = height;
}

const context = this.canvas.getContext("2d");
if (!context) {
throw new Error("Failed to get canvas context");
}
const context = this.canvas.getContext("2d");
if (!context) {
throw new Error("Failed to get canvas context");
}

this.context = context as
| OffscreenCanvasRenderingContext2D
| CanvasRenderingContext2D;
}
this.context = context as
| OffscreenCanvasRenderingContext2D
| CanvasRenderingContext2D;
}

getOutputCanvas(): HTMLCanvasElement {
wasmCompositor.ensureInitialized({
width: this.width,
height: this.height,
});
return wasmCompositor.getCanvas();
}
getOutputCanvas(): HTMLCanvasElement | null {
wasmCompositor.ensureInitialized({
width: this.width,
height: this.height,
});
return wasmCompositor.getCanvas();
}
Comment on lines +43 to +49
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
rg -nP '\bgetOutputCanvas\s*\(' -C3

Repository: OpenCut-app/OpenCut

Length of output: 1266


🏁 Script executed:

#!/bin/bash
# Check the full context around scene-exporter.ts line 100
cat -n apps/web/src/services/renderer/scene-exporter.ts | sed -n '85,115p'

Repository: OpenCut-app/OpenCut

Length of output: 1386


🏁 Script executed:

#!/bin/bash
# Find CanvasSource type definition and constructor signature
rg -n "class CanvasSource|interface CanvasSource|type CanvasSource" -A5

Repository: OpenCut-app/OpenCut

Length of output: 45


🏁 Script executed:

#!/bin/bash
# Check the full implementation of getOutputCanvas
cat -n apps/web/src/services/renderer/canvas-renderer.ts | sed -n '38,55p'

Repository: OpenCut-app/OpenCut

Length of output: 837


🏁 Script executed:

#!/bin/bash
# Check wasmCompositor.getCanvas() return type
rg -n "getCanvas\s*\(" -B2 -A3 apps/web/src/services/renderer/

Repository: OpenCut-app/OpenCut

Length of output: 1680


🏁 Script executed:

#!/bin/bash
# Find CanvasSource definition - could be imported from a library or defined locally
rg "CanvasSource" -B2 -A8 apps/web/src/services/renderer/scene-exporter.ts | head -30

Repository: OpenCut-app/OpenCut

Length of output: 663


🏁 Script executed:

#!/bin/bash
# Search for CanvasSource import/definition across the codebase
rg "class CanvasSource|from.*CanvasSource" -B1 -A1

Repository: OpenCut-app/OpenCut

Length of output: 45


🏁 Script executed:

#!/bin/bash
# Check mediabunny version and any type definitions
cat -n package.json | grep -i mediabunny -B2 -A2

Repository: OpenCut-app/OpenCut

Length of output: 45


🏁 Script executed:

#!/bin/bash
# Search for all uses of getOutputCanvas in the codebase
rg "getOutputCanvas" -B3 -A3

Repository: OpenCut-app/OpenCut

Length of output: 1220


🏁 Script executed:

#!/bin/bash
# Check if there's a try-catch around the export that might handle this
cat -n apps/web/src/services/renderer/scene-exporter.ts | sed -n '75,120p'

Repository: OpenCut-app/OpenCut

Length of output: 1746


Handle null return from getOutputCanvas() in export path.

getOutputCanvas() returns HTMLCanvasElement | null and can return null when GPU is unavailable (see wasm-compositor.ts:74-75). However, scene-exporter.ts:100 passes the result directly to new CanvasSource(...) without a null check. When GPU is unavailable, this will fail during export without a user-friendly error message.

Add a null check in the export method before instantiating CanvasSource, and either:

  1. Return null with a user-friendly error (export requires GPU), or
  2. Throw a descriptive error immediately for faster failure with clear messaging
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@apps/web/src/services/renderer/canvas-renderer.ts` around lines 43 - 49, The
export path currently assumes getOutputCanvas() returns a non-null
HTMLCanvasElement; add a null check before calling new CanvasSource(...) in the
export method where CanvasSource is instantiated (the code that calls
getOutputCanvas()). If getOutputCanvas() returns null (GPU unavailable via
wasmCompositor.getCanvas/getOutputCanvas), either return null with a clear
user-facing message like "Export failed: GPU unavailable — export requires GPU"
or throw a descriptive Error immediately to fail fast; update the code that
calls new CanvasSource(...) to handle the chosen behavior.


setSize({ width, height }: { width: number; height: number }) {
this.width = width;
this.height = height;
setSize({ width, height }: { width: number; height: number }) {
this.width = width;
this.height = height;

if (this.canvas instanceof OffscreenCanvas) {
this.canvas = new OffscreenCanvas(width, height);
} else {
this.canvas.width = width;
this.canvas.height = height;
}
if (this.canvas instanceof OffscreenCanvas) {
this.canvas = new OffscreenCanvas(width, height);
} else {
this.canvas.width = width;
this.canvas.height = height;
}

const context = this.canvas.getContext("2d");
if (!context) {
throw new Error("Failed to get canvas context");
}
this.context = context as
| OffscreenCanvasRenderingContext2D
| CanvasRenderingContext2D;
}
const context = this.canvas.getContext("2d");
if (!context) {
throw new Error("Failed to get canvas context");
}
this.context = context as
| OffscreenCanvasRenderingContext2D
| CanvasRenderingContext2D;
}

async render({ node, time }: { node: AnyBaseNode; time: number }) {
await resolveRenderTree({ node, renderer: this, time });
const { frame, textures } = await buildFrameDescriptor({
node,
renderer: this,
});
wasmCompositor.ensureInitialized({
width: this.width,
height: this.height,
});
wasmCompositor.syncTextures(textures);
wasmCompositor.render(frame);
}
async render({ node, time }: { node: AnyBaseNode; time: number }) {
await resolveRenderTree({ node, renderer: this, time });
const { frame, textures } = await buildFrameDescriptor({
node,
renderer: this,
});
wasmCompositor.ensureInitialized({
width: this.width,
height: this.height,
});
wasmCompositor.syncTextures(textures);
wasmCompositor.render(frame);
}

async renderToCanvas({
node,
time,
targetCanvas,
}: {
node: AnyBaseNode;
time: number;
targetCanvas: HTMLCanvasElement;
}) {
await this.render({ node, time });
async renderToCanvas({
node,
time,
targetCanvas,
}: {
node: AnyBaseNode;
time: number;
targetCanvas: HTMLCanvasElement;
}) {
await this.render({ node, time });

const ctx = targetCanvas.getContext("2d");
if (!ctx) {
throw new Error("Failed to get target canvas context");
}
const ctx = targetCanvas.getContext("2d");
if (!ctx) {
throw new Error("Failed to get target canvas context");
}

ctx.drawImage(
wasmCompositor.getCanvas(),
0,
0,
targetCanvas.width,
targetCanvas.height,
);
}
}
const compositorCanvas = wasmCompositor.getCanvas();
if (!compositorCanvas) return;

ctx.drawImage(
compositorCanvas,
0,
0,
targetCanvas.width,
targetCanvas.height,
);
}
}
Loading