Skip to content

Commit 57a9f48

Browse files
fix: the error in text.bounds when there is no root canvas (galacean#2543)
1 parent 5ab9ce4 commit 57a9f48

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

packages/ui/src/component/advanced/Text.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,8 @@ export class Text extends UIRenderer implements ITextRenderer {
598598
this._text === "" ||
599599
this._fontSize === 0 ||
600600
(this.enableWrapping && size.x <= 0) ||
601-
(this.overflowMode === OverflowMode.Truncate && size.y <= 0)
601+
(this.overflowMode === OverflowMode.Truncate && size.y <= 0) ||
602+
!this._getRootCanvas()
602603
);
603604
}
604605

tests/src/ui/Text.test.ts

+20
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,26 @@ describe("Text", async () => {
4242
expect(label.enableWrapping).to.eq(true);
4343
});
4444

45+
it("get bounds", () => {
46+
const textWithoutCanvas = root.addComponent(Text);
47+
textWithoutCanvas.text = "hello world";
48+
const bounds = textWithoutCanvas.bounds;
49+
expect(bounds.min).to.deep.include({ x: -50, y: -50, z: 0 });
50+
expect(bounds.max).to.deep.include({ x: 50, y: 50, z: 0 });
51+
52+
const labelBounds = label.bounds;
53+
expect(labelBounds.min).to.deep.include({ x: -50, y: -50, z: 0 });
54+
expect(labelBounds.max).to.deep.include({ x: 50, y: 50, z: 0 });
55+
label.text = "hello world";
56+
const labelBounds2 = label.bounds;
57+
expect(labelBounds2.min).to.deep.include({ x: -50, y: -50, z: 0 });
58+
expect(labelBounds2.max).to.deep.include({ x: 50, y: 50, z: 0 });
59+
(<UITransform>label.entity.transform).size.x = 200;
60+
const labelBounds3 = label.bounds;
61+
expect(labelBounds3.min).to.deep.include({ x: -100, y: -50, z: 0 });
62+
expect(labelBounds3.max).to.deep.include({ x: 100, y: 50, z: 0 });
63+
});
64+
4565
it("emoji", () => {
4666
const textEntity = canvasEntity.createChild("text");
4767

0 commit comments

Comments
 (0)