Skip to content

Commit 9653518

Browse files
authored
fix: new Image texture on Canvas (#470)
2 parents 900853c + 381296a commit 9653518

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

src/core/renderers/canvas/CanvasCoreTexture.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ import { CoreContextTexture } from '../CoreContextTexture.js';
2323
import { formatRgba, type IParsedColor } from './internal/ColorUtils.js';
2424

2525
export class CanvasCoreTexture extends CoreContextTexture {
26-
protected image: ImageBitmap | HTMLCanvasElement | undefined;
26+
protected image:
27+
| ImageBitmap
28+
| HTMLCanvasElement
29+
| HTMLImageElement
30+
| undefined;
2731
protected tintCache:
2832
| {
2933
key: string;
@@ -68,7 +72,9 @@ export class CanvasCoreTexture extends CoreContextTexture {
6872
return this.image !== undefined;
6973
}
7074

71-
getImage(color: IParsedColor): ImageBitmap | HTMLCanvasElement {
75+
getImage(
76+
color: IParsedColor,
77+
): ImageBitmap | HTMLCanvasElement | HTMLImageElement {
7278
const image = this.image;
7379
assertTruthy(image, 'Attempt to get unloaded image texture');
7480

@@ -94,7 +100,7 @@ export class CanvasCoreTexture extends CoreContextTexture {
94100
}
95101

96102
protected tintTexture(
97-
source: ImageBitmap | HTMLCanvasElement,
103+
source: ImageBitmap | HTMLCanvasElement | HTMLImageElement,
98104
color: string,
99105
) {
100106
const { width, height } = source;
@@ -132,8 +138,8 @@ export class CanvasCoreTexture extends CoreContextTexture {
132138
this.image = canvas;
133139
return { width: data.width, height: data.height };
134140
} else if (
135-
typeof ImageBitmap !== 'undefined' &&
136-
data instanceof ImageBitmap
141+
(typeof ImageBitmap !== 'undefined' && data instanceof ImageBitmap) ||
142+
data instanceof HTMLImageElement
137143
) {
138144
this.image = data;
139145
return { width: data.width, height: data.height };

src/core/textures/ImageTexture.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ export class ImageTexture extends Texture {
135135
async loadImageFallback(src: string, hasAlpha: boolean) {
136136
const img = new Image();
137137

138+
if (!src.startsWith('data:')) {
139+
img.crossOrigin = 'Anonymous';
140+
}
141+
138142
return new Promise<{ data: HTMLImageElement; premultiplyAlpha: boolean }>(
139143
(resolve) => {
140144
img.onload = () => {

0 commit comments

Comments
 (0)