Visualize Your Ideas With Code
fix(2d): fix text alignment (#1061)
Jacobdd066c6d6f05
diff --git a/packages/2d/src/lib/components/TxtLeaf.ts b/packages/2d/src/lib/components/TxtLeaf.tsindex 83707da3..f760556b 100644--- a/packages/2d/src/lib/components/TxtLeaf.ts+++ b/packages/2d/src/lib/components/TxtLeaf.ts@@ -65,9 +65,11 @@ export class TxtLeaf extends Shape {this.applyStyle(context);this.applyText(context);context.font = this.styles.font;+ context.textBaseline = 'bottom';if ('letterSpacing' in context) {context.letterSpacing = `${this.letterSpacing()}px`;}+ const fontOffset = context.measureText('').fontBoundingBoxAscent;const parentRect = this.element.getBoundingClientRect();const {width, height} = this.size();@@ -83,7 +85,7 @@ export class TxtLeaf extends Shape {const rangeRect = range.getBoundingClientRect();const x = width / -2 + rangeRect.left - parentRect.left;- const y = height / -2 + rangeRect.top - parentRect.top;+ const y = height / -2 + rangeRect.top - parentRect.top + fontOffset;if (lineRect.y === y) {lineRect.width += rangeRect.width;@@ -106,9 +108,7 @@ export class TxtLeaf extends Shape {text: string,box: BBox,) {- const y = box.y + box.height / 2;- context.save();- context.textBaseline = 'middle';+ const y = box.y;text = text.replace(/\s+/g, ' ');if (this.lineWidth() <= 0) {@@ -120,8 +120,6 @@ export class TxtLeaf extends Shape {context.fillText(text, box.x, y);context.strokeText(text, box.x, y);}-- context.restore();}protected override getCacheBBox(): BBox {