Fix wasm32
deployments not displaying anything
#2574
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This fixes a bug where
web
deployments of aniced
application would silently fail, not displaying anything.The problem can be tested by using
trunk serve
in any of the examples that support it.It seems to have started after #2469, where changes were introduced to allow an application to be started without an window. Although an window is not necessarily shown at the start, it's still created silently, and this is necessary for the acquisition of the graphics device and it's initialization, after that the window is discarded and only created again when requested by the
runtime
(or quickly after that on "non-deamon" applications).On the
wasm32
path of the code, the window contains theHTMLCanvasElement
, which in turn, has thegetContext
method and contains theWebGLRenderingContext
which is necessary for properly displaying graphics on thecanvas
.By discarding the window, we end up discarding the
HTMLCanvasElement
which is associated with the context and other graphics dependencies for proper rendering; creating a new window without using the correct associatedcanvas
element which was used to initialize the context, then makes thecanvas
graphically display nothing.There could multiple solutions to this, but the simplest seems to be to simply retain the
canvas
element, and re-use it to create thewindow
through thewith_canvas
window attribute.This PR changes the
wasm32
path to store theweb_sys::HTMLCanvasElement
and use it on thewith_canvas
method when creating an window, I've also added logging for the window attributes to the relevant code, which could be helpful in future debugging of similar problems.As for possible problems with this, the code doesn't make any guarantees for other windows, it'll certainly silently fail when asked to create new windows, since those are basically
canvas
elements onwasm32
that are not properly created or maintained. Although I would add that this was already the case before the PR.Closes #2480.