Skip to content

Commit 7a0fafb

Browse files
committed
fix: Support for color texture in Canvas renderer, align with latest texture changes
1 parent 4af99df commit 7a0fafb

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
lines changed

src/core/Stage.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,7 @@ export class Stage {
194194
this.renderer = new renderEngine(rendererOptions);
195195
const renderMode = this.renderer.mode || 'webgl';
196196

197-
if (renderMode === 'webgl') {
198-
this.createDefaultTexture();
199-
}
200-
197+
this.createDefaultTexture();
201198
this.defShaderCtr = this.renderer.getDefShaderCtr();
202199
setPremultiplyMode(renderMode);
203200

src/core/renderers/canvas/CanvasCoreRenderer.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import type { CoreNode } from '../../CoreNode.js';
2222
import type { CoreShaderManager } from '../../CoreShaderManager.js';
2323
import { getRgbaComponents, type RGBA } from '../../lib/utils.js';
2424
import { SubTexture } from '../../textures/SubTexture.js';
25-
import type { Texture } from '../../textures/Texture.js';
25+
import { TextureType, type Texture } from '../../textures/Texture.js';
2626
import type { CoreContextTexture } from '../CoreContextTexture.js';
2727
import {
2828
CoreRenderer,
@@ -43,6 +43,7 @@ import {
4343
type IParsedColor,
4444
} from './internal/ColorUtils.js';
4545
import { UnsupportedShader } from './shaders/UnsupportedShader.js';
46+
import { assertTruthy } from '../../../utils.js';
4647

4748
export class CanvasCoreRenderer extends CoreRenderer {
4849
private context: CanvasRenderingContext2D;
@@ -118,6 +119,17 @@ export class CanvasCoreRenderer extends CoreRenderer {
118119
| { x: number; y: number; width: number; height: number }
119120
| undefined;
120121

122+
const textureType = texture?.type;
123+
assertTruthy(textureType, 'Texture type is not defined');
124+
125+
// The Canvas2D renderer only supports image and color textures
126+
if (
127+
textureType !== TextureType.image &&
128+
textureType !== TextureType.color
129+
) {
130+
return;
131+
}
132+
121133
if (texture) {
122134
if (texture instanceof SubTexture) {
123135
frame = texture.props;
@@ -126,11 +138,9 @@ export class CanvasCoreRenderer extends CoreRenderer {
126138

127139
ctxTexture = texture.ctxTexture as CanvasCoreTexture;
128140
if (texture.state === 'freed') {
129-
// we're going to batch the texture loading so we don't have to wait for
130-
// ctxTexture.load();
131141
return;
132142
}
133-
if (texture.state !== 'loaded' || !ctxTexture.hasImage()) {
143+
if (texture.state !== 'loaded') {
134144
return;
135145
}
136146
}
@@ -175,7 +185,7 @@ export class CanvasCoreRenderer extends CoreRenderer {
175185
ctx.clip(path);
176186
}
177187

178-
if (ctxTexture) {
188+
if (textureType === TextureType.image && ctxTexture) {
179189
const image = ctxTexture.getImage(color);
180190
ctx.globalAlpha = color.a ?? alpha;
181191
if (frame) {
@@ -199,7 +209,7 @@ export class CanvasCoreRenderer extends CoreRenderer {
199209
}
200210
}
201211
ctx.globalAlpha = 1;
202-
} else if (hasGradient) {
212+
} else if (textureType === TextureType.color && hasGradient) {
203213
let endX: number = tx;
204214
let endY: number = ty;
205215
let endColor: IParsedColor;
@@ -219,7 +229,7 @@ export class CanvasCoreRenderer extends CoreRenderer {
219229
gradient.addColorStop(1, formatRgba(endColor));
220230
ctx.fillStyle = gradient;
221231
ctx.fillRect(tx, ty, width, height);
222-
} else {
232+
} else if (textureType === TextureType.color) {
223233
ctx.fillStyle = formatRgba(color);
224234
ctx.fillRect(tx, ty, width, height);
225235
}

src/core/renderers/canvas/CanvasCoreTexture.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ export class CanvasCoreTexture extends CoreContextTexture {
3636
| undefined;
3737

3838
load(): void {
39-
if (this.textureSource.state !== 'freed') {
40-
return;
41-
}
4239
this.textureSource.setCoreCtxState('loading');
4340

4441
this.onLoadRequest()

0 commit comments

Comments
 (0)