Skip to content

Commit 539a7ee

Browse files
committed
Invalidate canvas Page cache to redraw on changes
1 parent 4ac8560 commit 539a7ee

File tree

3 files changed

+49
-24
lines changed

3 files changed

+49
-24
lines changed

examples/tests/text-canvas.ts

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import type { ExampleSettings } from '../common/ExampleSettings.js';
22

3-
export async function automation(settings: ExampleSettings) {
4-
await test(settings);
5-
await settings.snapshot();
6-
}
3+
const Colors = {
4+
Black: 0x000000ff,
5+
Red: 0xff0000ff,
6+
Green: 0x00ff00ff,
7+
Blue: 0x0000ffff,
8+
Magenta: 0xff00ffff,
9+
Gray: 0x7f7f7fff,
10+
White: 0xffffffff,
11+
};
712

813
const randomIntBetween = (from: number, to: number) =>
914
Math.floor(Math.random() * (to - from + 1) + from);
@@ -33,13 +38,18 @@ export default async function test(settings: ExampleSettings) {
3338
x: Math.random() * 1900,
3439
y: Math.random() * 1080,
3540
text: 'CANVAS ' + t,
36-
color: 0x000000ff,
3741
fontFamily: 'sans-serif',
3842
parent: testRoot,
3943
fontSize: 80,
4044
});
4145

4246
nodes.push(node);
47+
48+
// pick random color from Colors
49+
node.color =
50+
Object.values(Colors)[
51+
randomIntBetween(0, Object.keys(Colors).length - 1)
52+
] || 0xff0000ff;
4353
};
4454

4555
const spawn = (amount = 100) => {
@@ -58,8 +68,18 @@ export default async function test(settings: ExampleSettings) {
5868
const move = () => {
5969
for (let i = 0; i < nodes.length; i++) {
6070
const node = nodes[i];
61-
node.x = randomIntBetween(0, 1900);
62-
node.y = randomIntBetween(0, 1080);
71+
node.x = randomIntBetween(0, 1600);
72+
node.y = randomIntBetween(0, 880);
73+
}
74+
};
75+
76+
const newColor = () => {
77+
for (let i = 0; i < nodes.length; i++) {
78+
const node = nodes[i];
79+
node.color =
80+
Object.values(Colors)[
81+
randomIntBetween(0, Object.keys(Colors).length - 1)
82+
] || 0x000000ff;
6383
}
6484
};
6585

@@ -72,6 +92,8 @@ export default async function test(settings: ExampleSettings) {
7292
move();
7393
} else if (event.key === 'ArrowRight') {
7494
move();
95+
} else if (event.key === '1') {
96+
newColor();
7597
}
7698
});
7799

src/core/text-rendering/renderers/CanvasTextRenderer.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,6 @@ function getFontCssString(props: TrProps): string {
7575
);
7676
}
7777

78-
function getFontCacheString(props: TrProps): string {
79-
const { fontFamily, fontStyle, fontWeight, fontStretch } = props;
80-
return [fontFamily, fontWeight, fontStyle, fontStretch].join(' ');
81-
}
82-
8378
export interface CanvasTextRendererState extends TextRendererState {
8479
props: TrProps;
8580

@@ -315,7 +310,7 @@ export class CanvasTextRenderer extends TextRenderer<CanvasTextRendererState> {
315310
let containedMaxLines = 0;
316311
let calcMaxLines = 0;
317312

318-
if (state.props.contain === 'both') {
313+
if (state.props.contain === 'both' || state.props.contain === 'width') {
319314
containedMaxLines = Math.floor(
320315
(state.props.height - state.props.offsetY) / state.props.lineHeight,
321316
);
@@ -584,6 +579,12 @@ export class CanvasTextRenderer extends TextRenderer<CanvasTextRendererState> {
584579
return;
585580
}
586581

582+
// if our canvasPage texture is still valid, return early
583+
if (state.canvasPage?.valid) {
584+
this.setStatus(state, 'loaded');
585+
return;
586+
}
587+
587588
if (state.canvasPage === undefined) {
588589
state.canvasPage = {
589590
texture: undefined,
@@ -841,11 +842,6 @@ export class CanvasTextRenderer extends TextRenderer<CanvasTextRendererState> {
841842
* @param state
842843
*/
843844
protected invalidateVisibleWindowCache(state: CanvasTextRendererState): void {
844-
// only needed for scrollable text
845-
if (state.isScrollable !== true) {
846-
return;
847-
}
848-
849845
state.visibleWindow.valid = false;
850846
this.setStatus(state, 'loading');
851847
this.scheduleUpdateState(state);
@@ -861,8 +857,19 @@ export class CanvasTextRenderer extends TextRenderer<CanvasTextRendererState> {
861857
* @param state
862858
*/
863859
private invalidateLayoutCache(state: CanvasTextRendererState): void {
860+
state.canvasPage?.texture?.free();
861+
862+
if (state.canvasPages) {
863+
state.canvasPages.forEach((pageInfo) => {
864+
pageInfo.texture?.free();
865+
});
866+
}
867+
868+
state.canvasPage = undefined;
869+
state.canvasPages = undefined;
864870
state.renderInfo = undefined;
865871
state.visibleWindow.valid = false;
872+
866873
this.setStatus(state, 'loading');
867874
this.scheduleUpdateState(state);
868875
}

src/core/textures/ImageTexture.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,18 +145,14 @@ export class ImageTexture extends Texture {
145145
}
146146

147147
override free(): void {
148-
const cacheKey = ImageTexture.makeCacheKey(this.props);
149-
if (cacheKey) {
150-
this.txManager.removeTextureCacheKeyFromCache(cacheKey);
151-
}
152-
153148
if (this.props.src instanceof ImageData) {
154149
// ImageData is a non-cacheable texture, so we need to free it manually
155150
const texture = this.txManager.getCtxTexture(this);
156151
texture?.free();
152+
153+
this.props.src = '';
157154
}
158155

159-
this.props.src = '';
160156
this.setState('freed');
161157
}
162158

0 commit comments

Comments
 (0)