PAIR-code / megaplot

Apache License 2.0
19 stars 5 forks source link

Gracefully handle canvas resize #16

Closed jimbojw closed 2 years ago

jimbojw commented 2 years ago

Currently, if the canvas changes size, the visualization gets stretched out or squished. Instead, when the canvas is resized, the Scene offset and scale should be adjusted to preserve the aspect ratio of rendered Sprites.

This operation could be set to check for canvas size immediately before performing the draw call. If the canvas size has changed since the last call, then perform the offset/scale adjustment.

When performing the adjustment, there's a question as to which world coordinate to preserve. For example, it could be that the world origin maintains its position in pixel space relative to the top left of the canvas (or the center of the canvas). Consider what options might be appropriate and whether/how to expose them to the API user.

jimbojw commented 2 years ago

Approach plan: preserve effective scale, while adjusting to the new canvas size. This raises the question of which world point should remain stationary. A natural default would be the world origin, (0,0).

So for example, say the world origin is positioned in the center of the canvas before the resize event. Then after the resize event, we would expect the world origin to again be at the center of the canvas. If the world origin was at the bottom-left corner of the canvas, then it would be at the bottom-left of the canvas after resize.

Other reasonable defaults could be chosen. For example, one could default to preserving the center point. That is, whatever world coordinate was at the center of the canvas, that coordinate would be at the center again after resizing. Perhaps the resize API should be sufficiently configurable to allow these alternatives?

Preserving the origin's proportional place within the canvas and the effective scale seems to be a good enough default for most cases to begin with.

jimbojw commented 2 years ago

Implemented in #28