Check if HTMLCanvasElement exists (i.e. we are not running in a webworker) (#22153)

This fixes #22152


### Description
Tensor.fromImage fails in a webworker context, because HTMLCanvasElement
does not exist:

> HTMLCanvasElement is not defined



### Motivation and Context
This fixes #22152

---------

Co-authored-by: Yulong Wang <7679871+fs-eire@users.noreply.github.com>
This commit is contained in:
Claude 2024-09-25 20:52:52 +02:00 committed by GitHub
parent a47254eaef
commit 3494f80e83
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -152,7 +152,7 @@ export const tensorFromImage = async (
}
};
const createCanvasContext = (canvas: HTMLCanvasElement | OffscreenCanvas) => {
if (canvas instanceof HTMLCanvasElement) {
if (typeof HTMLCanvasElement !== 'undefined' && canvas instanceof HTMLCanvasElement) {
return canvas.getContext('2d');
} else if (canvas instanceof OffscreenCanvas) {
return canvas.getContext('2d') as OffscreenCanvasRenderingContext2D;