Blazor-Diagrams / Blazor.Diagrams

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

GetBounds() is null. #199

Closed ChuckBerg closed 2 years ago

ChuckBerg commented 2 years ago

I'm trying to get the bounding box for each of my custom components so that I can dynamically adjust their positions at runtime. Here's my latest attempt.

private NodeModel myCustomNode;

protected override void OnInitialized()
{
    base.OnInitialized();
    diagram.RegisterModelComponent<myCustomModel, myCustomComponent>();

    myCustomNode = new myCustomModel(new Point(0, 0), name);
    ...
}

protected override void OnAfterRender(bool firstRender)
{
    base.OnAfterRender(firstRender);
    var boundingBox = myCustomNode.GetBounds();
    ...
}

Everything renders as I expect, but it's not laid out properly. Unfortunately, boundingBox is always null so I can't adjust positions the way I'd like. Any thoughts?

314159265meow commented 2 years ago

Hello,

I think the reason boundingBox is null is because the bounds of a node are calculated asynchronously via JavaScript, so when you call GetBounds() directly in OnAfterRender these values haven't been calculated yet.
You can test this if you add a button to your page and try to read the bounding box in the button event handler, there you will get the correct values.

Another way to get the bounds is to listen to the OnChanged or OnSizeChanged events of a node. These are called after the node was rendered and the size calculations are finished.

zHaytam commented 2 years ago

Hello,

Indeed, @314159265meow is right. Since dimensions and positions are calculated after the elements are rendered, they won't be available to you right away.

ChuckBerg commented 2 years ago

Thanks for the feedback, guys. Unfortunately, I had to get something working so I've taken a different approach and am not using Blazor.Diagrams anymore. I may switch back sometime later in the project.

Chuck

zHaytam commented 2 years ago

If it's possible, may I know what you used as the alternative? Thank you and good luck in your project!

ChuckBerg commented 2 years ago

I only need the ability to create static flowcharts, so CSS for placement of nodes, and LeaderLine for the connecting lines and arrows.

zHaytam commented 2 years ago

Okay! Good luck with your project.