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

Don't round font sizes #926

Merged
merged 1 commit into from
Dec 1, 2024
Merged

Don't round font sizes #926

merged 1 commit into from
Dec 1, 2024

Conversation

jaredkhan
Copy link
Collaborator

Fixes #899

This is a pretty big improvement for basically no effort so maybe go with this for now.
(See video demos on the issue #899)

There's still a bit of jitteriness of multi-line text but I think that's more of a limitation of the canvas API: It doesn't seem to let you write text whose baseline isn't on a pixel boundary.

@jaredkhan jaredkhan requested a review from hyanwong December 1, 2024 22:39
Copy link
Member

@hyanwong hyanwong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Nice that it's so easy. I wonder why we rounded in the first place?

@hyanwong hyanwong merged commit 30dad8c into OneZoom:main Dec 1, 2024
1 check passed
@jaredkhan
Copy link
Collaborator Author

Yeah it is a bit curious. I did have half a think about it:

I found this StackOverflow from 11 years ago talking about how browsers seemed to round font sizes in canvas:
https://stackoverflow.com/questions/15430763/fractional-font-sizes-html5-canvas

and in particular they produced this graphic of incrementing font sizes by 0.1px and the resulting output
image

but when I tried to produce a similar graphic in 2024 I didn't find such agressive rounding, though there was still some in Firefox

image

left-to-right: Firefox, Safari, Chrome.

I also found that it does seem to be way better on Firefox to scale text using the ctx.scale rather than using the fontsize.
In this graphic, the stack on the left is using font size, the stack on the right is using ctx.scale. The rectangles are boxes drawn with the result of a measureText call on that text. So you can see that the text size rounding is gone and the measuretext is more accurate when using ctx.scale.

image

but that's a bit more of a faff to change for less of a return, hence suggesting this PR in the end.

So perhaps the rounding was based on previous typical behaviour of canvas that's no longer so typical?
Or perhaps it was just an assumption that whole numbers were required.
Or perhaps I'm missing something else entirely.

@jaredkhan jaredkhan deleted the fix-text-jitter branch December 4, 2024 19:26
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.

Text Jitters
2 participants