Open History to see the last commit
1/** A synchronous, borrowed frame. The producer releases its source after present returns. */
2export interface DeviceFrameSink {
3 readonly present: (source: CanvasImageSource, width: number, height: number) => boolean;
4}
5
6export function createCanvasFrameSink(
7 canvas: HTMLCanvasElement,
8 onFrame?: (width: number, height: number) => void,
9): DeviceFrameSink {
10 return {
11 present(source, width, height) {
12 const context = canvas.getContext("2d");
13 if (!context) return false;
14 if (canvas.width !== width || canvas.height !== height) {
15 canvas.width = width;
16 canvas.height = height;
17 }
18 context.drawImage(source, 0, 0, width, height);
19 onFrame?.(width, height);
20 return true;
21 },
22 };
23}