From e942b149c607843d96c10811a8c99d546a09d5d9 Mon Sep 17 00:00:00 2001 From: Mat Groves Date: Wed, 1 May 2024 12:59:18 +0100 Subject: [PATCH] fix bounds calculation with padding --- src/scene/text/Text.ts | 4 ++-- tests/renderering/text/Text.tests.ts | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/scene/text/Text.ts b/src/scene/text/Text.ts index 0e302758ad..795f1a708a 100644 --- a/src/scene/text/Text.ts +++ b/src/scene/text/Text.ts @@ -64,8 +64,8 @@ export class Text const { width, height } = canvasMeasurement; bounds.minX = (-anchor._x * width) - padding; - bounds.maxX = bounds.minX + width; + bounds.maxX = bounds.minX + width + (padding * 2); bounds.minY = (-anchor._y * height) - padding; - bounds.maxY = bounds.minY + height; + bounds.maxY = bounds.minY + height + (padding * 2); } } diff --git a/tests/renderering/text/Text.tests.ts b/tests/renderering/text/Text.tests.ts index dc7be61123..c7b2260fd4 100644 --- a/tests/renderering/text/Text.tests.ts +++ b/tests/renderering/text/Text.tests.ts @@ -251,4 +251,16 @@ describe('Text', () => expect(text.height).toEqual(100); }); }); + + it('should measure bounds of text correctly when padding is set', () => + { + const textNoPadding = new Text({ text: 'HI', style: { padding: 0 } }); + const text = new Text({ text: 'HI', style: { padding: 10 } }); + + const boundsNoPadding = textNoPadding.getBounds(); + const bounds = text.getBounds(); + + expect(boundsNoPadding.width).toBeLessThan(bounds.width + 20); + expect(boundsNoPadding.height).toBeLessThan(bounds.height + 20); + }); });