Estimate Text Layout using an OffscreenCanvas #664
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I tried to use plotters in a web worker and ran into a panic in estimate_layout. The reason is fairly obvious, there is no window or document inside of a worker context.
To get plotters to work inside of a web worker I hacked up some code to measure the text size using an OffscreenCanvas.
There are some benefits and drawbacks to this.
I think it should be more resilient than creating a span in the dom and measuring offsetWidth/Height because it is not affected by CSS.
It should be more performant than adding a dom node and measuring the offset properties which cause layout reflows. This is just an educated guess. Didn't benchmark it - takes this claim with a pinch of salt.
I think the main drawback is compatibility. While it is considered to be baseline now, browser support isn't ideal.
There are several ways I see forward. This approach could be used as a fallback when window isn't available or visa versa.
I think it's also an option to just fall back to a really crude approximation along the lines of height = size, width = size * text.len().
Anyway would be glad to get some feedback on this from the maintainers.