type: "png" | "jpeg" | "webp";
interface TransferOptions {
canvas: HTMLCanvasElement;
interface TransfferedImage {
function transferImage(options: TransferOptions): Promise<TransferredImage> {
const { canvas, video, output } = options;
const height = video.videoHeight;
const width = video.videoWidth;
return new Promise<TransferredImage>((resolve, reject) => {
const ctx = canvas.getContext("2d");
reject(new Error("Context2D is not defined"));
ctx.drawImage(video, 0, 0, width, height);
const type = `image/${output.type}`;
const q = output.quality || 1;
function toBlob(blob: Blob | null) {
resolve({ blob, width, height, type });
reject(new Error("Failed to create blob from canvas"));
canvas.toBlob(toBlob, type, q);