excubo-ag / Blazor.Diagrams

https://excubo-ag.github.io/Blazor.Diagrams/
MIT License
136 stars 18 forks source link

Auto-Center after ZoomToFit #93

Closed Phloog closed 1 year ago

Phloog commented 1 year ago

Hi,

is there a way to make the diagram auto-center (in other words: pan/translate automatically so that the diagram's nodes are centered in the viewport) after calling ZoomToFit? Can I implement this by myself ("from the outside") or is it neccessary to add code to the Blazor.Diagrams lib?

Thanks, Tim

stefanloerwald commented 1 year ago

Hi Tim,

I'm not sure I understand the expected outcome here. Could you please provide screenshots of the result after ZoomToFit and of your expected layout?

Thanks Stefan

Phloog commented 1 year ago

Hi, sure!

The first screenshot is after zoom&fit, and the second is how I'd like it to be translated (the card encompasses the canvas fully).

2023-06-06 08_09_15-SB Central

2023-06-06 08_09_33-SB Central

So it'd need to determine the bounding box size of the zoomed full diagram, and the current size of the viewport, and then pan accordingly

stefanloerwald commented 1 year ago

I see! Thanks for clarifying this.

I don't think it will be possible to achieve this without modification of the library code, but that shouldn't be too difficult. Zoom to fit already does most of what you need, but the anchor for this operation is the top left corner, whereas you want it in the center of the screen.

If I remember the code correctly, it measures the most extreme points of all nodes and then adjusts pan and zoom to fit those, for which it needs the diagram size as well. Changing the anchor would mean adjusting the pan slightly, which isn't a big deal (essentially add a horizontal and vertical offset of half the difference between width/height and maximum difference between left-most&right-most/top-most&bottom-most points).

I'd be very happy to review a pull request for this. It could be a new method "ZoomToFitCenter" or maybe a parameter to the existing method (default should keep existing behaviour).

Would you be willing to implement this?

Thanks Stefan

Phloog commented 1 year ago

Sure, as soon as I find time I'll look into this!

Phloog commented 1 year ago

Hi Stefan,

after updating to 4.1.0, the method ZoomToFitCentered is still not available on Diagram.

stefanloerwald commented 1 year ago

Hi @Phloog, can you please try again with v4.1.5? Thanks.

Phloog commented 1 year ago

After updating to 4.1.5, the method is available, thank you! :-)