Skip to content

Commit

Permalink
remove OffscreenCanvas for canvas-scan for better compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
rwv committed Nov 17, 2023
1 parent a831e72 commit 5db2f17
Showing 1 changed file with 18 additions and 32 deletions.
50 changes: 18 additions & 32 deletions src/utils/scan-renderer/canvas-scan/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,24 @@ export class CanvasScanner implements ScanRenderer {
throw new Error("Aborted");
}

if ("OffscreenCanvas" in window) {
// TODO: use web worker
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const canvas = new OffscreenCanvas(10, 10);
await scanCanvas(canvas, image, this.config);
const blob = await canvas.convertToBlob({
type: this.config.output_format,
});
const height = canvas.height;
const width = canvas.width;
return { blob, height, width };
} else {
const canvas = document.createElement("canvas");
await scanCanvas(canvas, image, this.config);
if (options?.signal?.aborted) {
throw new Error("Aborted");
}

const blob = await new Promise<Blob>((resolve, reject) =>
canvas.toBlob((blob) => {
if (blob) {
resolve(blob);
} else {
reject(new Error("Canvas to Blob failed"));
}
}, this.config.output_format)
);
const height = canvas.height;
const width = canvas.width;
canvas.remove();
return { blob, height, width };
const canvas = document.createElement("canvas");
await scanCanvas(canvas, image, this.config);
if (options?.signal?.aborted) {
throw new Error("Aborted");
}

const blob = await new Promise<Blob>((resolve, reject) =>
canvas.toBlob((blob) => {
if (blob) {
resolve(blob);
} else {
reject(new Error("Canvas to Blob failed"));
}
}, this.config.output_format)
);
const height = canvas.height;
const width = canvas.width;
canvas.remove();
return { blob, height, width };
}
}

0 comments on commit 5db2f17

Please sign in to comment.