Skip to content
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

Option to give existing canvas element as winit window #515

Merged
merged 1 commit into from
Sep 21, 2020
Merged

Option to give existing canvas element as winit window #515

merged 1 commit into from
Sep 21, 2020

Conversation

smokku
Copy link
Member

@smokku smokku commented Sep 19, 2020

This PR extends WindowDescriptor resource with canvas field.
This is a Document.querySelector() string allowing pointing winit to an existing HTMLCanvasElement to use.

This allows embedding Bevy game in an existing web page.

@Moxinilian Moxinilian added C-Feature A new feature, making something new possible O-Web Specific to web (WASM) builds A-Windowing Platform-agnostic interface layer to run your app in labels Sep 19, 2020
@cart
Copy link
Member

cart commented Sep 19, 2020

Ooh does this mean we might be able to have multiple canvases that behave as different "bevy windows"?

@smokku
Copy link
Member Author

smokku commented Sep 20, 2020

We are able to do so already. Every Bevy/winit window creates a separate <canvas> HTML element.

This PR gives an option to use pre-existing canvas element instead of creating and attaching one.

@cart
Copy link
Member

cart commented Sep 21, 2020

Looks good to me. Its worth noting that I don't see an error/panic like I would expect to when the custom canvas doesnt exist.

@cart cart merged commit dd6f0b5 into bevyengine:master Sep 21, 2020
@smokku
Copy link
Member Author

smokku commented Sep 22, 2020

You need to configure it. If it is not configured, canvas element is is created and attached to HTML.

Works for me.

diff --git a/examples/wasm/index.html b/examples/wasm/index.html
index 33f18ca2..f10014d8 100644
--- a/examples/wasm/index.html
+++ b/examples/wasm/index.html
@@ -21,7 +21,7 @@
     </style>
   </head>
   <script type="module">
-    import init from './target/headless_wasm.js'
+    import init from './target/winit_wasm.js'
     init()
   </script>
 </html>
diff --git a/examples/wasm/winit_wasm.rs b/examples/wasm/winit_wasm.rs
index 7759d141..c3ab5568 100644
--- a/examples/wasm/winit_wasm.rs
+++ b/examples/wasm/winit_wasm.rs
@@ -16,6 +16,7 @@ fn main() {
         .add_resource(WindowDescriptor {
             width: 300,
             height: 300,
+            canvas: Some("#nonexistant".to_string()),
             ..Default::default()
         })
         .add_default_plugins()

gives:
Zrzut ekranu z 2020-09-22 09-19-36

@smokku smokku deleted the wasm branch September 22, 2020 07:27
mrk-its pushed a commit to mrk-its/bevy that referenced this pull request Oct 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Windowing Platform-agnostic interface layer to run your app in C-Feature A new feature, making something new possible O-Web Specific to web (WASM) builds
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants