From bfb4b84e28472ef6855502ca8efa3a3db5d6587d Mon Sep 17 00:00:00 2001 From: Mirko Pecora Date: Wed, 11 Dec 2024 17:20:19 +0100 Subject: [PATCH 1/2] fix: new Image texture on Canvas --- src/core/renderers/canvas/CanvasCoreTexture.ts | 16 +++++++++++++--- src/core/textures/ImageTexture.ts | 4 ++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/core/renderers/canvas/CanvasCoreTexture.ts b/src/core/renderers/canvas/CanvasCoreTexture.ts index 2831b678..22d24cc3 100644 --- a/src/core/renderers/canvas/CanvasCoreTexture.ts +++ b/src/core/renderers/canvas/CanvasCoreTexture.ts @@ -23,7 +23,11 @@ import { CoreContextTexture } from '../CoreContextTexture.js'; import { formatRgba, type IParsedColor } from './internal/ColorUtils.js'; export class CanvasCoreTexture extends CoreContextTexture { - protected image: ImageBitmap | HTMLCanvasElement | undefined; + protected image: + | ImageBitmap + | HTMLCanvasElement + | HTMLImageElement + | undefined; protected tintCache: | { key: string; @@ -68,7 +72,9 @@ export class CanvasCoreTexture extends CoreContextTexture { return this.image !== undefined; } - getImage(color: IParsedColor): ImageBitmap | HTMLCanvasElement { + getImage( + color: IParsedColor, + ): ImageBitmap | HTMLCanvasElement | HTMLImageElement { const image = this.image; assertTruthy(image, 'Attempt to get unloaded image texture'); @@ -94,7 +100,7 @@ export class CanvasCoreTexture extends CoreContextTexture { } protected tintTexture( - source: ImageBitmap | HTMLCanvasElement, + source: ImageBitmap | HTMLCanvasElement | HTMLImageElement, color: string, ) { const { width, height } = source; @@ -138,6 +144,10 @@ export class CanvasCoreTexture extends CoreContextTexture { this.image = data; return { width: data.width, height: data.height }; } + if (data instanceof HTMLImageElement) { + this.image = data; + return { width: data.width, height: data.height }; + } return { width: 0, height: 0 }; } } diff --git a/src/core/textures/ImageTexture.ts b/src/core/textures/ImageTexture.ts index 3196cca1..afc5c9e7 100644 --- a/src/core/textures/ImageTexture.ts +++ b/src/core/textures/ImageTexture.ts @@ -135,6 +135,10 @@ export class ImageTexture extends Texture { async loadImageFallback(src: string, hasAlpha: boolean) { const img = new Image(); + if (!src.startsWith('data:')) { + img.crossOrigin = 'Anonymous'; + } + return new Promise<{ data: HTMLImageElement; premultiplyAlpha: boolean }>( (resolve) => { img.onload = () => { From 381296a5acbb7b0fe87c436b1b1d30e4a28e09fe Mon Sep 17 00:00:00 2001 From: Mirko Pecora Date: Wed, 11 Dec 2024 17:25:18 +0100 Subject: [PATCH 2/2] fix: condition --- src/core/renderers/canvas/CanvasCoreTexture.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/core/renderers/canvas/CanvasCoreTexture.ts b/src/core/renderers/canvas/CanvasCoreTexture.ts index 22d24cc3..8957fc5f 100644 --- a/src/core/renderers/canvas/CanvasCoreTexture.ts +++ b/src/core/renderers/canvas/CanvasCoreTexture.ts @@ -138,16 +138,12 @@ export class CanvasCoreTexture extends CoreContextTexture { this.image = canvas; return { width: data.width, height: data.height }; } else if ( - typeof ImageBitmap !== 'undefined' && - data instanceof ImageBitmap + (typeof ImageBitmap !== 'undefined' && data instanceof ImageBitmap) || + data instanceof HTMLImageElement ) { this.image = data; return { width: data.width, height: data.height }; } - if (data instanceof HTMLImageElement) { - this.image = data; - return { width: data.width, height: data.height }; - } return { width: 0, height: 0 }; } }