-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Fix fit_canvas_to_parent #11278
Fix fit_canvas_to_parent #11278
Conversation
c37cb7e
to
f0dd8b1
Compare
/// **Warning**: this will not behave as expected for parents that set their size according to the size of their | ||
/// children. This creates a "feedback loop" that will result in the canvas growing on each resize. When using this | ||
/// feature, ensure the parent's size is not affected by its children. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not too sure if this comment is still 100% correct
crates/bevy_winit/src/system.rs
Outdated
let canvas: HtmlCanvasElement = web_sys::window() | ||
.unwrap() | ||
.document() | ||
.unwrap() | ||
.query_selector(&format!("canvas[data-raw-handle=\"{}\"]", handle.id)) | ||
.unwrap() | ||
.unwrap() | ||
.unchecked_into(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is that too many unwraps ? We could use expects like https://github.com/gfx-rs/wgpu/blob/9f82504d8a3b6e234599b19cc35c299bcec8b138/wgpu-hal/src/gles/web.rs#L117
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fortunately you don't need this whole block (and the corresponding crate features in web-sys
), you can just use WindowExtWebSys::canvas()
, which only needs one unwrap()
that checks if you are on the main thread, which Bevy always is.
transparent: false, | ||
focused: true, | ||
window_level: Default::default(), | ||
- fit_canvas_to_parent: false, | ||
+ fit_canvas_to_parent: true, | ||
prevent_default_event_handling: true, | ||
- canvas: None, | ||
+ canvas: Some("#bevy".to_string()), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will also not be needed if you use WindowExtWebSys::canvas()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the insight!
You may be right about this specific example, but I think it's still ok to use that as a recommended way to use bevy, so the ability to target a specific canvas is in bevy user space opposed to the lower level winit functions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For clarity, this "canvas" selector allows to declare the canvas at a specific place rather than relying on winit creating it
2a0b14e
to
f168ae0
Compare
// Tells wasm to resize the window according to the available canvas | ||
fit_canvas_to_parent: true, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// Tells wasm to resize the window according to the available canvas | |
fit_canvas_to_parent: true, | |
// Tells wasm to set the CSS width and height properties to 100% for the target canvas. | |
// This can be useful to support window resizing. | |
fit_canvas_to_parent: true, |
Maybe an improvement ?
No, wgpu only sets the html attributes I'd strongly prefer it if we could avoid having either winit or bevy modify the canvas style at all. That way, styling on the web page would just work without having to use edit: Commenting out the calls to |
I tested this PR and it works on my machine ™️ (firefox/windows): Thanks for correcting me on my assumption that wgpu sets the CSS! I want to believe you're right and winit does it, still I'm not sure where ?
Does it override the style ? I don't think that's the case since winit 0.29, from this PR, when I resize the window, only the canvas attribute width/height changes, not the style. I'm not sure resizing is what you were referring to with "request a resolution", maybe scale factor is a risk too ? If you mean user explicitly using API to set the resolution, I think it's intentional enough a user wouldn't be surprised the CSS is "modified" (but still could use docs, and support to set it to % rather than pixels). In other case, if CSS gets overridden implicitly after window creation, isn't it a bug 🤔 ? I think it shouldn't even be modified during window creation but I'm not sure. |
Winit doesn't touch Please let me know if Winit has any issue on that front, but from what I'm reading it seems to work correctly. |
winit is only adding the style attributes when explicitly requested in my testing, so that's all good.
Yes, that's what I was referring to. Allowing the resolution to be a percentage sounds like a better alternative to |
Thanks for the precision! So our culprit is https://github.com/Vrixyz/bevy/blob/f168ae0451e693232591e0b72ddb78726dfcb22c/crates/bevy_winit/src/winit_windows.rs#L85 ; not too far from my change. There's a bunch of called to
No bug within winit from my understanding so far :) |
@Vrixyz can you resolve merge conflicts so I can merge this in? |
I can do that ~sunday |
Implemented suggestions from reviewers: a simpler fit_canvas_to_parent leads to an explicit CSS setting to the canvas. It has do be set after wgpu creation due to wgpu overriding the canvas width/height: https://github.com/gfx-rs/wgpu/blob/4400a5847080d1164bdca93a90622414963ed9f3/examples/src/utils.rs#L68-L74
f168ae0
to
106b8d3
Compare
Follow up to bevyengine#11057 Implemented suggestions from reviewers from: a simpler fit_canvas_to_parent leads to an explicit CSS setting to the canvas. From my understanding, it has do be set after wgpu creation due to wgpu overriding the canvas width/height: https://github.com/gfx-rs/wgpu/blob/4400a5847080d1164bdca93a90622414963ed9f3/examples/src/utils.rs#L68-L74 # Changelog - Re-enable a `fit_canvas_to_parent`, it's removal from bevyengine#11057 was problematic. Still, its inner working is more simple than before: bevy doesn't handle its resizing, winit does. ## Migration Guide - Cancels the migration from bevyengine#11057
Follow up to bevyengine#11057 Implemented suggestions from reviewers from: a simpler fit_canvas_to_parent leads to an explicit CSS setting to the canvas. From my understanding, it has do be set after wgpu creation due to wgpu overriding the canvas width/height: https://github.com/gfx-rs/wgpu/blob/4400a5847080d1164bdca93a90622414963ed9f3/examples/src/utils.rs#L68-L74 # Changelog - Re-enable a `fit_canvas_to_parent`, it's removal from bevyengine#11057 was problematic. Still, its inner working is more simple than before: bevy doesn't handle its resizing, winit does. ## Migration Guide - Cancels the migration from bevyengine#11057
Follow up to bevyengine#11057 Implemented suggestions from reviewers from: a simpler fit_canvas_to_parent leads to an explicit CSS setting to the canvas. From my understanding, it has do be set after wgpu creation due to wgpu overriding the canvas width/height: https://github.com/gfx-rs/wgpu/blob/4400a5847080d1164bdca93a90622414963ed9f3/examples/src/utils.rs#L68-L74 # Changelog - Re-enable a `fit_canvas_to_parent`, it's removal from bevyengine#11057 was problematic. Still, its inner working is more simple than before: bevy doesn't handle its resizing, winit does. ## Migration Guide - Cancels the migration from bevyengine#11057
Follow up to bevyengine#11057 Implemented suggestions from reviewers from: a simpler fit_canvas_to_parent leads to an explicit CSS setting to the canvas. From my understanding, it has do be set after wgpu creation due to wgpu overriding the canvas width/height: https://github.com/gfx-rs/wgpu/blob/4400a5847080d1164bdca93a90622414963ed9f3/examples/src/utils.rs#L68-L74 # Changelog - Re-enable a `fit_canvas_to_parent`, it's removal from bevyengine#11057 was problematic. Still, its inner working is more simple than before: bevy doesn't handle its resizing, winit does. ## Migration Guide - Cancels the migration from bevyengine#11057
Follow up to #11057
Implemented suggestions from reviewers from: a simpler fit_canvas_to_parent leads to an explicit CSS setting to the canvas.
From my understanding, it has do be set after wgpu creation due to wgpu overriding the canvas width/height: https://github.com/gfx-rs/wgpu/blob/4400a5847080d1164bdca93a90622414963ed9f3/examples/src/utils.rs#L68-L74
Changelog
fit_canvas_to_parent
, it's removal from Remove CanvasParentResizePlugin #11057 was problematic. Still, its inner working is more simple than before: bevy doesn't handle its resizing, winit does.Migration Guide