engine.getRenderingCanvasClientRect returning null for some reason

In this PG, which uses the htmlmesh BJS extension (BabylonJS-Extensions/HtmlMesh at master · ericwood73/BabylonJS-Extensions · GitHub. Note my fork is referenced because it contains code that illustrates/mitigates the problem). If the PG is loaded or reloaded there is no issue. But if the play button is pushed, then you will start seeing console warnings that the canvas rect has become null. If you follow the link next to one of the warnings and place a breakpoint on the call to scene.getEngine().getRenderingCanvasClientRect() and step into it, you will indeed see that the _renderingCanvas is null on the engine instance. However, if you place breakpoints in each of the 4 places that _renderingCanvas is set in ThinEngine, reload and hit play, you will see that the only time the _renderingCanvas is set to null is on the call to dispose() and that call is followed by calls that set the _renderingCanvas to a non-null value. I suspect some sort of async issue here, but I can’t find it in my code. Can anyone see what I am doing wrong?

Actually, engine.getRenderingCanvasClientRect() is not null:


Maybe your previous instance of HtmlMeshRenderer is still running and it is the one that generates the error? Don’t you have some cleanup to perform before the PG is restarted (you should do it in scene.onDisposeObservable)?

Here’s a simplified version of the PG that still exhibits the problem, for easier debugging: https://playground.babylonjs.com/#B17TC7#25

Yep. You are right. I need to dispose of the HtmlMeshRenderer when the scene is disposed. Is it best practice to subscribe to the scene disposed observable and have the renderer dispose itself, or expect the calling code to handle disposing?

I guess it depends on your use case, but in the Playground it’s better to subscribe to the scene disposed observable.