Skip to content

Commit

Permalink
fix: Support for color texture in Canvas renderer, align with latest …
Browse files Browse the repository at this point in the history
…texture changes
  • Loading branch information
wouterlucas committed Dec 18, 2024
1 parent 4af99df commit 7a0fafb
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
5 changes: 1 addition & 4 deletions src/core/Stage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,7 @@ export class Stage {
this.renderer = new renderEngine(rendererOptions);
const renderMode = this.renderer.mode || 'webgl';

if (renderMode === 'webgl') {
this.createDefaultTexture();
}

this.createDefaultTexture();
this.defShaderCtr = this.renderer.getDefShaderCtr();
setPremultiplyMode(renderMode);

Expand Down
24 changes: 17 additions & 7 deletions src/core/renderers/canvas/CanvasCoreRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import type { CoreNode } from '../../CoreNode.js';
import type { CoreShaderManager } from '../../CoreShaderManager.js';
import { getRgbaComponents, type RGBA } from '../../lib/utils.js';
import { SubTexture } from '../../textures/SubTexture.js';
import type { Texture } from '../../textures/Texture.js';
import { TextureType, type Texture } from '../../textures/Texture.js';
import type { CoreContextTexture } from '../CoreContextTexture.js';
import {
CoreRenderer,
Expand All @@ -43,6 +43,7 @@ import {
type IParsedColor,
} from './internal/ColorUtils.js';
import { UnsupportedShader } from './shaders/UnsupportedShader.js';
import { assertTruthy } from '../../../utils.js';

export class CanvasCoreRenderer extends CoreRenderer {
private context: CanvasRenderingContext2D;
Expand Down Expand Up @@ -118,6 +119,17 @@ export class CanvasCoreRenderer extends CoreRenderer {
| { x: number; y: number; width: number; height: number }
| undefined;

const textureType = texture?.type;
assertTruthy(textureType, 'Texture type is not defined');

// The Canvas2D renderer only supports image and color textures
if (
textureType !== TextureType.image &&
textureType !== TextureType.color
) {
return;
}

if (texture) {
if (texture instanceof SubTexture) {
frame = texture.props;
Expand All @@ -126,11 +138,9 @@ export class CanvasCoreRenderer extends CoreRenderer {

ctxTexture = texture.ctxTexture as CanvasCoreTexture;
if (texture.state === 'freed') {
// we're going to batch the texture loading so we don't have to wait for
// ctxTexture.load();
return;
}
if (texture.state !== 'loaded' || !ctxTexture.hasImage()) {
if (texture.state !== 'loaded') {
return;
}
}
Expand Down Expand Up @@ -175,7 +185,7 @@ export class CanvasCoreRenderer extends CoreRenderer {
ctx.clip(path);
}

if (ctxTexture) {
if (textureType === TextureType.image && ctxTexture) {
const image = ctxTexture.getImage(color);
ctx.globalAlpha = color.a ?? alpha;
if (frame) {
Expand All @@ -199,7 +209,7 @@ export class CanvasCoreRenderer extends CoreRenderer {
}
}
ctx.globalAlpha = 1;
} else if (hasGradient) {
} else if (textureType === TextureType.color && hasGradient) {
let endX: number = tx;
let endY: number = ty;
let endColor: IParsedColor;
Expand All @@ -219,7 +229,7 @@ export class CanvasCoreRenderer extends CoreRenderer {
gradient.addColorStop(1, formatRgba(endColor));
ctx.fillStyle = gradient;
ctx.fillRect(tx, ty, width, height);
} else {
} else if (textureType === TextureType.color) {
ctx.fillStyle = formatRgba(color);
ctx.fillRect(tx, ty, width, height);
}
Expand Down
3 changes: 0 additions & 3 deletions src/core/renderers/canvas/CanvasCoreTexture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ export class CanvasCoreTexture extends CoreContextTexture {
| undefined;

load(): void {
if (this.textureSource.state !== 'freed') {
return;
}
this.textureSource.setCoreCtxState('loading');

this.onLoadRequest()
Expand Down

0 comments on commit 7a0fafb

Please sign in to comment.