Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling ZoomToFit function on OnInitialized throws nullreference exception #118

Closed
Wizdore opened this issue Jul 16, 2021 · 4 comments · Fixed by #119
Closed

Calling ZoomToFit function on OnInitialized throws nullreference exception #118

Wizdore opened this issue Jul 16, 2021 · 4 comments · Fixed by #119
Labels
bug Something isn't working fixed/done

Comments

@Wizdore
Copy link

Wizdore commented Jul 16, 2021

ZoomToFit function throws nullreference exception when its called from the components lifecycle function.
But it works when the function is called onbutton click or any dom events.
image

So I dont want the user to press a button everytime to fit the diagram on screen. Is there any workaround on this?

@zHaytam zHaytam added the bug Something isn't working label Jul 16, 2021
@zHaytam
Copy link
Collaborator

zHaytam commented Jul 16, 2021

Hello,
Do you have a simple example to show the issue?
ZoomToFit won't really work before the canvas is rendered, because the nodes don't have sizes yet (unless you set them manually).
Still, it shouldn't throw a NullReferenceException, so that needs to be fixed.

@Wizdore
Copy link
Author

Wizdore commented Jul 17, 2021

Hello,
Do you have a simple example to show the issue?
ZoomToFit won't really work before the canvas is rendered, because the nodes don't have sizes yet (unless you set them manually).
Still, it shouldn't throw a NullReferenceException, so that needs to be fixed.

<div class="diagramContainer" @ref="diagramContainer">
    <DiagramComp Height="_diagramDrawer.diagramHeight"
                 Width="_diagramDrawer.diagramWidth"
                 DiagramModel="_diagramDrawer.diagram"/>
</div>
@code{
    private DiagramDrawer _diagramDrawer;

    protected override void OnInitialized()
    {
        base.OnInitialized();

        _diagramDrawer = new(
            Config.visualConfig.Diagram["height"], 
            Config.visualConfig.Diagram["width"], 
            diagramConfig: Config.plantConfig
            );
        
        _diagramDrawer.diagram.ZoomToFit();

    }
}

Here DiagramDrawer is a class that lays out the diagram based on a json object (Config.plantConfig).
Calling ZoomToFit OnInitialized throws the null reference exception. I found out that its happening because the bounds are not found so for now I have solved it by calling the ZoomToFit in OnAfterRenderAsync and awaiting for the bounds to be calculated and calling StateHasChanged on first render to update the ui.

 protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await base.OnAfterRenderAsync(firstRender);
        await JSRuntime.GetBoundingClientRect(diagramContainer);
        _diagramDrawer.diagram.ZoomToFit(20);
        
        if (firstRender)
            StateHasChanged();
    }

@Wizdore
Copy link
Author

Wizdore commented Jul 17, 2021

I could not reproduce the error on the sample project of this repo. I'll dig deeper whats happening.

@zHaytam
Copy link
Collaborator

zHaytam commented Jul 19, 2021

I believe that I fixed the NullReferenceException part. It was throwing it because the nodes didn't have a Size yet, not because the bounds aren't here yet (that would be another exception).

Since the library needs to know the canvas container and the nodes sizes, calling ZoomToFit in OnInitialized will probably never work, unless you set them beforehand.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed/done
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants