diff --git a/js/web/lib/onnxjs/backends/webgl/webgl-context-factory.ts b/js/web/lib/onnxjs/backends/webgl/webgl-context-factory.ts index 4abc9cb7c2..e2b060955b 100644 --- a/js/web/lib/onnxjs/backends/webgl/webgl-context-factory.ts +++ b/js/web/lib/onnxjs/backends/webgl/webgl-context-factory.ts @@ -83,7 +83,16 @@ export function createNewWebGLContext(contextId?: 'webgl'|'webgl2'): WebGLContex throw new Error('WebGL is not supported'); } +// eslint-disable-next-line @typescript-eslint/naming-convention +declare let OffscreenCanvas: {new (width: number, height: number): HTMLCanvasElement}; + function createCanvas(): HTMLCanvasElement { + if (typeof document === 'undefined') { + if (typeof OffscreenCanvas === 'undefined') { + throw new TypeError('failed to create canvas: OffscreenCanvas is not supported'); + } + return new OffscreenCanvas(1, 1); + } const canvas: HTMLCanvasElement = document.createElement('canvas'); canvas.width = 1; canvas.height = 1;