Closed AlmightyLks closed 1 year ago
Adding to this: I just noticed, the Pan is a simple Point; Which confuses me even more
Hello,
Hope this helps.
Me trying to explain with a drawing 😂
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?
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
Is there a way to make it select a node programmatically? 😆
Yes! Diagram.SelectModel(yourNode, unselectOthers: true)
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? 😄
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:
I can provide an example code once I get on my computer.
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
doesn't position it as good however 😄
On the side, to not undermine your efforts: I appreciate your patience and help!
Oh! I found a mistake on my side!
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
I am still open to hear about the centering code snippet, as I did not manage to do it myself, admittedly 😅
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);
}
Yes! That does the job! Thanks a lot! 😄
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