2020-04-06 03:19:02 +00:00
|
|
|
[package]
|
|
|
|
name = "bevy_winit"
|
2022-04-17 23:04:52 +00:00
|
|
|
version = "0.8.0-dev"
|
2021-10-27 00:12:14 +00:00
|
|
|
edition = "2021"
|
2020-08-10 00:24:27 +00:00
|
|
|
description = "A winit window and input backend for Bevy Engine"
|
|
|
|
homepage = "https://bevyengine.org"
|
|
|
|
repository = "https://github.com/bevyengine/bevy"
|
2021-07-23 21:11:51 +00:00
|
|
|
license = "MIT OR Apache-2.0"
|
2020-08-10 00:24:27 +00:00
|
|
|
keywords = ["bevy"]
|
2020-04-06 03:19:02 +00:00
|
|
|
|
2020-08-25 00:06:08 +00:00
|
|
|
[features]
|
|
|
|
wayland = ["winit/wayland"]
|
|
|
|
x11 = ["winit/x11"]
|
|
|
|
|
2020-04-06 03:19:02 +00:00
|
|
|
[dependencies]
|
2020-08-10 00:39:28 +00:00
|
|
|
# bevy
|
2022-04-17 23:04:52 +00:00
|
|
|
bevy_app = { path = "../bevy_app", version = "0.8.0-dev" }
|
|
|
|
bevy_ecs = { path = "../bevy_ecs", version = "0.8.0-dev" }
|
|
|
|
bevy_input = { path = "../bevy_input", version = "0.8.0-dev" }
|
|
|
|
bevy_math = { path = "../bevy_math", version = "0.8.0-dev" }
|
|
|
|
bevy_window = { path = "../bevy_window", version = "0.8.0-dev" }
|
|
|
|
bevy_utils = { path = "../bevy_utils", version = "0.8.0-dev" }
|
2020-04-06 03:19:02 +00:00
|
|
|
|
2020-08-10 00:39:28 +00:00
|
|
|
# other
|
2021-12-09 20:14:00 +00:00
|
|
|
winit = { version = "0.26.0", default-features = false }
|
2021-09-10 18:46:16 +00:00
|
|
|
approx = { version = "0.5.0", default-features = false }
|
2021-12-09 20:14:00 +00:00
|
|
|
raw-window-handle = "0.4.2"
|
2020-09-16 20:40:32 +00:00
|
|
|
|
|
|
|
[target.'cfg(target_arch = "wasm32")'.dependencies]
|
2021-12-09 20:14:00 +00:00
|
|
|
winit = { version = "0.26.0", default-features = false }
|
2020-09-21 23:12:34 +00:00
|
|
|
wasm-bindgen = { version = "0.2" }
|
2020-09-19 03:11:26 +00:00
|
|
|
web-sys = "0.3"
|
Optionally resize Window canvas element to fit parent element (#4726)
Currently Bevy's web canvases are "fixed size". They are manually set to specific dimensions. This might be fine for some games and website layouts, but for sites with flexible layouts, or games that want to "fill" the browser window, Bevy doesn't provide the tools needed to make this easy out of the box.
There are third party plugins like [bevy-web-resizer](https://github.com/frewsxcv/bevy-web-resizer/) that listen for window resizes, take the new dimensions, and resize the winit window accordingly. However this only covers a subset of cases and this is common enough functionality that it should be baked into Bevy.
A significant motivating use case here is the [Bevy WASM Examples page](https://bevyengine.org/examples/). This scales the canvas to fit smaller windows (such as mobile). But this approach both breaks winit's mouse events and removes pixel-perfect rendering (which means we might be rendering too many or too few pixels). https://github.com/bevyengine/bevy-website/issues/371
In an ideal world, winit would support this behavior out of the box. But unfortunately that seems blocked for now: https://github.com/rust-windowing/winit/pull/2074. And it builds on the ResizeObserver api, which isn't supported in all browsers yet (and is only supported in very new versions of the popular browsers).
While we wait for a complete winit solution, I've added a `fit_canvas_to_parent` option to WindowDescriptor / Window, which when enabled will listen for window resizes and resize the Bevy canvas/window to fit its parent element. This enables users to scale bevy canvases using arbitrary CSS, by "inheriting" their parents' size. Note that the wrapper element _is_ required because winit overrides the canvas sizing with absolute values on each resize.
There is one limitation worth calling out here: while the majority of canvas resizes will be triggered by window resizes, modifying element layout at runtime (css animations, javascript-driven element changes, dev-tool-injected changes, etc) will not be detected here. I'm not aware of a good / efficient event-driven way to do this outside of the ResizeObserver api. In practice, window-resize-driven canvas resizing should cover the majority of use cases. Users that want to actively poll for element resizes can just do that (or we can build another feature and let people choose based on their specific needs).
I also took the chance to make a couple of minor tweaks:
* Made the `canvas` window setting available on all platforms. Users shouldn't need to deal with cargo feature selection to support web scenarios. We can just ignore the value on non-web platforms. I added documentation that explains this.
* Removed the redundant "initial create windows" handler. With the addition of the code in this pr, the code duplication was untenable.
This enables a number of patterns:
## Easy "fullscreen window" mode for the default canvas
The "parent element" defaults to the `<body>` element.
```rust
app
.insert_resource(WindowDescriptor {
fit_canvas_to_parent: true,
..default()
})
```
And CSS:
```css
html, body {
margin: 0;
height: 100%;
}
```
## Fit custom canvas to "wrapper" parent element
```rust
app
.insert_resource(WindowDescriptor {
fit_canvas_to_parent: true,
canvas: Some("#bevy".to_string()),
..default()
})
```
And the HTML:
```html
<div style="width: 50%; height: 100%">
<canvas id="bevy"></canvas>
</div>
```
2022-05-20 23:13:48 +00:00
|
|
|
crossbeam-channel = "0.5"
|
2021-02-22 04:15:50 +00:00
|
|
|
|
|
|
|
[package.metadata.docs.rs]
|
|
|
|
features = ["x11"]
|