Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve calculations for Text width to better align with CSS letter-spacing properties #1865

Merged
merged 7 commits into from
Dec 23, 2024

Conversation

Adam-Greenan
Copy link

This pull request includes a change to the Text class in the src/shapes/Text.ts file to adjust the width calculation by incorporating letter spacing.

  • src/shapes/Text.ts: Updated the width calculation in the Text class to include letterSpacing in the obj.width value.

Issue Description

While testing text alignment, I noticed a discrepancy between how Konva.Text and CSS handle letter spacing for center-aligned text. Specifically, CSS includes the letter-spacing after the last character when calculating the x-axis offset for center alignment, whereas Konva does not. This inconsistency results in text being slightly misaligned when using center alignment with letter spacing.

Proposed Fix

This pull request modifies Konva.Text to align its letter-spacing calculations with CSS behavior. The updated logic ensures that the x-axis offset for center-aligned text incorporates the letter-spacing value applied after the last character, matching CSS behavior precisely.

Screenshots

The yellow text in the screenshots is an HTML element styled to match the Konva text (black) exactly, with the same properties, text content, and letter-spacing applied. This provides a direct comparison between how the Konva text and a standard HTML element are rendered. The behavior shown is consistent across both screenshots.

In these examples letterSpacing is equal to 50% of the fontSize.

  • Original Konva Behavior vs. CSS: Highlights the misalignment issue.
    Screenshot 2024-12-11 at 14 03 37
    Screenshot 2024-12-11 at 14 04 26

  • Updated Konva Behavior (Post Fix): Demonstrates alignment consistency with CSS.
    Screenshot 2024-12-11 at 14 02 20

Notes

This is my first contribution to open source, and I welcome any feedback on improvements or omissions in this pull request. Thank you for taking the time to review this!

@lavrton
Copy link
Member

lavrton commented Dec 16, 2024

Can you please make a demo where you see a mismatch? I just tried this https://jsbin.com/rexuzetaxi/1/edit?js,output. Looks correct to me.

@Adam-Greenan
Copy link
Author

The issue arises specifically when both the textarea and the Konva Text element are horizontally aligned to the center.

https://jsbin.com/havebis/edit?html,css,js,output

Screenshot 2024-12-20 at 08 09 43

@lavrton lavrton merged commit a8395ca into konvajs:master Dec 23, 2024
0 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants