-
Notifications
You must be signed in to change notification settings - Fork 722
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
[text] Text measurement problems in browsers #375
Comments
@sto3psl Awesome analysis! The discrepancies between the measuring types seems relatively low (fractional pixel typically) (I only looked in Chrome and Safari briefly) although I don't know if using a non-SVG based measurement could cause any problems with rendering inconsistencies, but with such a low variance, it's probably worth the speed improvement (especially getting it off the main thread). I don't have a lot of extra time nowadays, but could try to assist as much as possible. |
|
That's interesting. I didn't know that transforms affect the measured size of text in SVG since they don't affect Thanks for the insights! That's super helpful. |
Well maybe it was a misunderstanding, but I am currently working on something where the user can basically author a graph, e.g. place nodes and edges, put labels on them, and zoom the whole thing in out via scale transform on a rather-top-level |
I'm not sure if I follow. Could you make a short video of the zoom behaviour? Basic zooming with |
@hshoff I added some speed measurement to https://beta.observablehq.com/@sto3psl/text-measurement-in-the-browser. It now displays how fast every line got measured and how long it took to measure all lines. For individual lines the difference isn't that big of a deal but if you start measuring a lot of lines it adds up. Here you can see that Chrome 70Firefox 62I think it would be pretty reasonable to select a sane default and if the user needs a different measurement method we can give them an option in If I have some time later this week I might make some more benchmarks and profile with the chrome dev tools. Than we can also better consider the time used for paint and layout. |
Working a lot with data visualisation and building responsive charts at my company, it came to my attention that in some cases browsers don't measure text correctly and it isn't the fastest thing in general. This got me to start investigating what methods are available to measure text in browsers and compare their speed and accuracy.
I compared these methods:
Element.getBoundingClientRect()
SVGTextContentElement.getComputedTextLength()
CanvasRenderingContext2D.measureText()
To make it short:
ctx.measureText
seems to be consistently the fastest andgetBoundingClientRect
the slowestI didn't try Edge yet or mobile browsers.
I will investigate further and extend my examples. The question is if someone would like to help with that and if it is of interest to you, to improve the reliability and speed of
@vx/text
andgetStringWidth
. Currently vx usesgetComputedTextLength
and from my first investigations I think it could be worth to switch toctx.measureText
. There are also other benefits. For examplectx.measureText
doesn't need an element in the DOM (still needs to be created) and it can be done off the main thread with Offscreen Canvas in the future.Let me know what you think ✌️.
The text was updated successfully, but these errors were encountered: