Skip to content

Commit

Permalink
fix: new Image texture on Canvas (#470)
Browse files Browse the repository at this point in the history
  • Loading branch information
wouterlucas authored Dec 11, 2024
2 parents 900853c + 381296a commit 9653518
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/core/renderers/canvas/CanvasCoreTexture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');

Expand All @@ -94,7 +100,7 @@ export class CanvasCoreTexture extends CoreContextTexture {
}

protected tintTexture(
source: ImageBitmap | HTMLCanvasElement,
source: ImageBitmap | HTMLCanvasElement | HTMLImageElement,
color: string,
) {
const { width, height } = source;
Expand Down Expand Up @@ -132,8 +138,8 @@ 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 };
Expand Down
4 changes: 4 additions & 0 deletions src/core/textures/ImageTexture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand Down

0 comments on commit 9653518

Please sign in to comment.