stevenpetryk / mafs

React components for interactive math
https://mafs.dev
MIT License
3.24k stars 90 forks source link

Flash of 1px wide canvas on initial render #113

Closed Mesoptier closed 1 year ago

Mesoptier commented 1 year ago

Initial render: the <svg> is rendered with width={1}, causing it to appear very stretched for a single frame. mafs stretched canvas

On the next frame: the useResizeObserver hook returns an actual value for the width and the canvas renders correctly. mafs canvas

See https://react-mnnsnw.stackblitz.io. In this example I render a whole bunch of points to slow the rendering down and emulate a heavy visualization, such that the issue is more easily visible.

Besides looking a little bit janky, this can also cause significant performance issues for page loads, since the <Coordinates.Cartesian> component might render tens of thousands of lines and labels. This performance issue is the actual problem I'm facing in my project.


Potential solutions

stevenpetryk commented 1 year ago

Huh yeah, I suppose this is kind of a janky solution. I'll explore the things you shared! I think both bullet points make total sense and sound easy to implement.

I'll have to take a look at AutoSizer :)

Mesoptier commented 1 year ago

Looking at it again, I don't think I would actually recommend AutoSizer anymore. It hasn't been really maintained in a few years now and doesn't even use resize observers under the hood.