Blazor-Diagrams / Blazor.Diagrams

A fully customizable and extensible all-purpose diagrams library for Blazor
https://blazor-diagrams.zhaytam.com
MIT License
919 stars 176 forks source link

Diagram Container vs Pan(-el?) #269

Closed AlmightyLks closed 1 year ago

AlmightyLks commented 1 year ago

What's what here exactly? I am getting a little confused about their meaning, and I can't seem to find any docs explaining them

AlmightyLks commented 1 year ago

Adding to this: I just noticed, the Pan is a simple Point; Which confuses me even more

zHaytam commented 1 year ago

Hello,

Hope this helps.

zHaytam commented 1 year ago

image

Me trying to explain with a drawing 😂

AlmightyLks commented 1 year ago

I am not certain how I'd manage to move the Diagram to the position of a certain Node in the centre 😄 Is there a way of achieving that?

zHaytam commented 1 year ago

If your node is the only one selected, you can use Diagram.ZoomToFit. Otherwsise, there is a PR still in progress: https://github.com/Blazor-Diagrams/Blazor.Diagrams/pull/178

AlmightyLks commented 1 year ago

Is there a way to make it select a node programmatically? 😆

zHaytam commented 1 year ago

Yes! Diagram.SelectModel(yourNode, unselectOthers: true)

AlmightyLks commented 1 year ago

That does move the Pan to the node's top left position
The margin parameter of ZoomToFit allows for a margin. However that margin affects the zoom as well, zooming out by a lot in order to kinda get it to center the node Is there no way to position the Container so that the Node is in the center? 😄

zHaytam commented 1 year ago

If you want it in the center, that will require a bit more calculations. The container is not really used like that. Container is only used (mostly) internally to understand how to shif things.

If you want things to be in the middle, you will have to adjust the pan yourself by:

  1. Set the node position to 0,0 (or call ZoomToFit with margin 0)
  2. Setting Pan.X to HalfContainerWidth - HalfNodeWidth
  3. Setting Pan.Y to HalfContainerHeight - HalfNodeHeight

I can provide an example code once I get on my computer.

AlmightyLks commented 1 year ago
            Diagram.SelectModel(_rootNode, unselectOthers: true);
            Diagram.ZoomToFit();
            Diagram.SetPan(
                (Diagram.Container.Width / 2) - (_rootNode.Size.Width / 2),
                (Diagram.Container.Height / 2) - (_rootNode.Size.Height / 2)
                );

Seems fitting to what you described

image

doesn't position it as good however 😄

AlmightyLks commented 1 year ago

On the side, to not undermine your efforts: I appreciate your patience and help!

AlmightyLks commented 1 year ago

Oh! I found a mistake on my side!

zHaytam commented 1 year ago

There is a mistake on my side too, my idea would only work if your node is at 0,0. I can provide a working example tomorrow if you still don't have a solution

AlmightyLks commented 1 year ago

I am still open to hear about the centering code snippet, as I did not manage to do it myself, admittedly 😅

zHaytam commented 1 year ago

Hello!

Please try the following extension method:

public static void CenterOnNode(this Diagram diagram, NodeModel node, double margin = 0)
{
    diagram.SelectModel(node, unselectOthers: true);
    diagram.ZoomToFit(margin);

    var width = node.Size.Width * diagram.Zoom;
    var scaledMargin = margin * diagram.Zoom;
    var deltaX = (diagram.Container.Width / 2) - (width / 2) - scaledMargin;
    diagram.UpdatePan(deltaX, 0);
}
AlmightyLks commented 1 year ago

Yes! That does the job! Thanks a lot! 😄