Fix wasm window compilation, initial state, and scaling - #1086
Open
umitsukidev wants to merge 4 commits into
Open
Fix wasm window compilation, initial state, and scaling#1086umitsukidev wants to merge 4 commits into
umitsukidev wants to merge 4 commits into
Conversation
Bevy 0.19 implements WindowResolution conversion for (u32, u32), so floating-point literals prevent nannou from compiling for wasm32.
On wasm, creating the primary window reuses the bootstrap window entity, so world-first reads returned its stale resolution before deferred commands were applied. Track the creation frame and prefer pending state only for that frame, then fall back to the live window.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Motivation
nannou 0.20.0 does not compile for
wasm32-unknown-unknown.After fixing the compilation failure locally, I noticed that the application
reacted to mouse input at a different position from the visible cursor.
app.window_rect()returned the 1024×768 bootstrap size insidefn model(),even though the application requested a 1920×1080 window.
Changes
1. Restore wasm compilation
In a downstream application using nannou 0.20.0, the error can be reproduced
with:
Bevy 0.19 does not convert a floating-point tuple into
WindowResolution.The wasm bootstrap dimensions now use integers:
2. Return the requested window state during creation
On wasm, nannou reuses the existing bootstrap window entity and applies the
requested configuration through deferred commands.
App::with_windowpreviouslyread the existing component first, so it returned the old 1024×768 state instead
of the pending configuration.
Pending window state now records its creation frame. It takes precedence during
that frame, then later reads return to the live window component so resize,
focus, and cursor state cannot become stale.
3. Preserve logical window size on high-DPI displays
On wasm, a new window builder started with a scale factor of 1.0 instead of
inheriting the scale factor already reported by the bootstrap window. On a
Retina display,
.size(1200, 900)therefore produced a 1200×900 physicalcanvas that appeared as 600×450 CSS pixels.
The wasm window builder now inherits the bootstrap window's scale factor before
applying logical dimensions. This keeps
.size(...)in logical points whilethe canvas uses the appropriate physical resolution for the display.
Testing
The following targeted checks pass:
The
eguifeature is enabled for the targeted wasm check becausebevy_eguienables getrandom's
wasm_jsbackend. Without it, compilation stops ingetrandombefore reaching nannou's wasm window code. This feature is used onlyto ensure that the check reaches the code changed by this PR; changing the
workspace's getrandom configuration is outside the scope of this PR.
The full example runner was also executed on macOS:
All examples completed successfully except
laser_frame_stream_gui, whichpanicked during startup with
no camera found for window. The affected exampleand egui camera lookup are unchanged by this PR.
I also tested a 1920×1080 downstream nannou application with Trunk. Window
coordinates, nannou mouse input, and egui input remained aligned.
I also tested
.size(1200, 900)in a downstream application with Trunk on aRetina display. The canvas used the requested logical size, and nannou mouse
input and egui input remained aligned.
I also tried checking the entire workspace with
--target wasm32-unknown-unknown, but it currently fails while compilingnannou_webcamandnannou_videodue to errors unrelated to this PR.I therefore used the targeted
-p nannoucheck above to test these changes.