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

Make Nannou run in WASM environment #811

Merged
merged 1 commit into from
Apr 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions nannou/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "nannou"
version ="0.18.0"
version = "0.18.0"
authors = ["mitchmindtree <mitchell.nordine@gmail.com>"]
description = "A Creative Coding Framework for Rust."
readme = "README.md"
Expand Down Expand Up @@ -30,6 +30,7 @@ serde_derive = "1"
serde_json = "1"
toml = "0.5"
walkdir = "2"
web-sys = { version = "0.3.55", optional = true }
wgpu_upstream = { version = "0.11.1", package = "wgpu" }
winit = "0.26"

Expand All @@ -38,4 +39,4 @@ default = ["notosans"]
# Enables SPIR-V support in the `wgpu` module.
spirv = ["nannou_wgpu/spirv"]
# Enables experimental WASM compilation for CI-use only
wasm-experimental = ["getrandom/js", "wgpu_upstream/webgl", "async-std/unstable"]
wasm-experimental = ["getrandom/js", "web-sys", "wgpu_upstream/webgl", "async-std/unstable"]
15 changes: 15 additions & 0 deletions nannou/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,21 @@ impl<'app> Builder<'app> {
window.build(window_target)?
};

#[cfg(target_arch = "wasm32")]
{
use winit::platform::web::WindowExtWebSys;
let canvas = window.canvas();

web_sys::window()
.expect("window")
.document()
.expect("document")
.body()
.expect("body")
.append_child(&canvas)
.expect("append_child");
}

// Build the wgpu surface.
let surface = unsafe { app.instance().create_surface(&window) };

Expand Down