Estimate Text Layout using an OffscreenCanvas#664
Estimate Text Layout using an OffscreenCanvas#664jwagner wants to merge 1 commit intoplotters-rs:masterfrom
Conversation
243a1e6 to
52ce834
Compare
|
Sounds reasonable to me, but I'm not proficient enough to tell which approach is preferable. In the end, both solution seem like a hack to me, and the question is which hack has the better trade-offs. I'd like to hear another opinion on this matter, but unfortunately I'm the only somewhat active maintainer currently. |
|
Thanks a lot for getting back to me and keeping the project afloat @AaronErhardt. If browser compatibility is the main concern I could also just add a fallback to a regular (non-offscreen) canvas. That way there is (essentially) only one code path and the dom hackery can go. Canvas is supported in practically every browser that supports wasm. |
|
@jwagner Sounds like the stable option... If you would go forward with adding the fallback, we could go forward with merging :) |
|
@philocalyst I'm happy to add the fallback as soon as someone with merge permissions confirms that this is what they want. |
|
Sure, man, I'm just helping out around the repo and know this would make it more maintainable and more likely to be considered. Do with that what you will...
▰▰▰▰▰
Miles Wirht 🙃
… From: Jonas Wagner ***@***.***>
Sent: 27 January 2026 14:35
To: plotters-rs/plotters ***@***.***>
Cc: Miles Wirht ***@***.***>, Mention ***@***.***>
Subject: Re: [plotters-rs/plotters] Estimate Text Layout using an OffscreenCanvas (PR #664)
jwagner left a comment (plotters-rs/plotters#664)
@philocalyst I'm happy to add the fallback as soon as someone with merge permissions confirms that this is what they want.
--
Reply to this email directly or view it on GitHub:
#664 (comment)
You are receiving this because you were mentioned.
Message ID: ***@***.***>
|
I would merge it :) |
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.