Skip to content

Commit

Permalink
Migrate web demo to provide canvas directly to eframe::WebRunner
Browse files Browse the repository at this point in the history
  • Loading branch information
zakarumych committed Dec 19, 2024
1 parent f1bcf9b commit ffedc85
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion examples/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1116,15 +1116,24 @@ fn main() -> eframe::Result<()> {
)
}

#[cfg(target_arch = "wasm32")]
fn get_canvas_element() -> Option<web_sys::HtmlCanvasElement> {
let document = web_sys::window()?.document()?;
let canvas = document.get_element_by_id("egui_snarl_demo")?;
canvas.dyn_into::<web_sys::HtmlCanvasElement>().ok()
}

// When compiling to web using trunk:
#[cfg(target_arch = "wasm32")]
fn main() {
let canvas = get_canvas_element().expect("Failed to find canvas with id 'egui_snarl_demo'");

let web_options = eframe::WebOptions::default();

wasm_bindgen_futures::spawn_local(async {
eframe::WebRunner::new()
.start(
"egui_snarl_demo",
canvas,
web_options,
Box::new(|cx| Ok(Box::new(DemoApp::new(cx)))),
)
Expand Down

0 comments on commit ffedc85

Please sign in to comment.