diff --git a/Cargo.lock b/Cargo.lock index a31bb14..8d1c9fd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2467,6 +2467,8 @@ dependencies = [ "log", "plate-tool-lib", "serde", + "wasm-bindgen-futures", + "web-sys", ] [[package]] diff --git a/plate-tool-eframe/.gitignore b/plate-tool-eframe/.gitignore new file mode 100644 index 0000000..4f96631 --- /dev/null +++ b/plate-tool-eframe/.gitignore @@ -0,0 +1,2 @@ +/target +/dist diff --git a/plate-tool-eframe/Cargo.toml b/plate-tool-eframe/Cargo.toml index 02bfef1..6665889 100644 --- a/plate-tool-eframe/Cargo.toml +++ b/plate-tool-eframe/Cargo.toml @@ -13,5 +13,17 @@ eframe = { version = "0.30", default-features = false, features = [ ]} log = "0.4" -env_logger = "0.11" serde = { version = "1.0", features = ["derive"] } + +[target.'cfg(not(target_arch = "wasm32"))'.dependencies] +env_logger = "0.11" + +[target.'cfg(target_arch = "wasm32")'.dependencies] +wasm-bindgen-futures = "0.4" +web-sys = "0.3" + +#[profile.release] +#opt-level = 2 + +#[profile.dev.package."*"] +#opt-level = 2 diff --git a/plate-tool-eframe/Trunk.toml b/plate-tool-eframe/Trunk.toml new file mode 100644 index 0000000..e0c8dfc --- /dev/null +++ b/plate-tool-eframe/Trunk.toml @@ -0,0 +1,3 @@ +[build] +target = "index.html" +release = true diff --git a/plate-tool-eframe/index.html b/plate-tool-eframe/index.html new file mode 100644 index 0000000..0a25d97 --- /dev/null +++ b/plate-tool-eframe/index.html @@ -0,0 +1,53 @@ + + + + + + Plate Tool + + + + + + diff --git a/plate-tool-eframe/src/main.rs b/plate-tool-eframe/src/main.rs index bec9a89..64cee4d 100644 --- a/plate-tool-eframe/src/main.rs +++ b/plate-tool-eframe/src/main.rs @@ -1,18 +1,48 @@ -use eframe::*; use eframe::egui; +use eframe::*; -fn main() -> eframe::Result{ +#[cfg(not(target_arch = "wasm32"))] +fn main() -> eframe::Result { env_logger::init(); log::info!("Shrimp!"); let native_options = eframe::NativeOptions { - viewport: egui::ViewportBuilder::default() - .with_title("Shrimp"), + viewport: egui::ViewportBuilder::default().with_title("Shrimp"), ..Default::default() }; eframe::run_native( "PlateToolEframe", native_options, - Box::new(|cc| Ok(Box::new(plate_tool_eframe::PlateToolEframe::new(cc)))) + Box::new(|cc| Ok(Box::new(plate_tool_eframe::PlateToolEframe::new(cc)))), ) } + +#[cfg(target_arch = "wasm32")] +fn main() { + use eframe::wasm_bindgen::JsCast as _; + + eframe::WebLogger::init(log::LevelFilter::Info).ok(); + + let web_options = eframe::WebOptions::default(); + + wasm_bindgen_futures::spawn_local(async { + let document = web_sys::window() + .expect("No window") + .document() + .expect("No document"); + + let canvas = document + .get_element_by_id("main_canvas") + .expect("Canvas id not found") + .dyn_into::() + .expect("Canvas was not a HtmlCanvasElement"); + + let start_result = eframe::WebRunner::new() + .start( + canvas, + web_options, + Box::new(|cc| Ok(Box::new(plate_tool_eframe::PlateToolEframe::new(cc)))), + ) + .await; + }); +}