2019-12-04 04:16:06 +11:00
|
|
|
use winit::{
|
|
|
|
event::{Event, WindowEvent},
|
|
|
|
event_loop::{ControlFlow, EventLoop},
|
|
|
|
window::WindowBuilder,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub fn main() {
|
|
|
|
let event_loop = EventLoop::new();
|
|
|
|
|
|
|
|
let window = WindowBuilder::new()
|
|
|
|
.with_title("A fantastic window!")
|
|
|
|
.build(&event_loop)
|
|
|
|
.unwrap();
|
|
|
|
|
2021-05-25 03:06:21 +10:00
|
|
|
#[cfg(target_arch = "wasm32")]
|
2019-12-04 04:16:06 +11:00
|
|
|
{
|
|
|
|
use winit::platform::web::WindowExtWebSys;
|
|
|
|
|
|
|
|
let canvas = window.canvas();
|
|
|
|
|
|
|
|
let window = web_sys::window().unwrap();
|
|
|
|
let document = window.document().unwrap();
|
|
|
|
let body = document.body().unwrap();
|
|
|
|
|
|
|
|
body.append_child(&canvas)
|
|
|
|
.expect("Append canvas to HTML body");
|
|
|
|
}
|
|
|
|
|
|
|
|
event_loop.run(move |event, _, control_flow| {
|
2020-01-05 18:12:03 +11:00
|
|
|
*control_flow = ControlFlow::Wait;
|
|
|
|
|
2021-05-25 03:06:21 +10:00
|
|
|
#[cfg(target_arch = "wasm32")]
|
2019-12-04 04:16:06 +11:00
|
|
|
log::debug!("{:?}", event);
|
|
|
|
|
|
|
|
match event {
|
|
|
|
Event::WindowEvent {
|
|
|
|
event: WindowEvent::CloseRequested,
|
|
|
|
window_id,
|
|
|
|
} if window_id == window.id() => *control_flow = ControlFlow::Exit,
|
|
|
|
Event::MainEventsCleared => {
|
|
|
|
window.request_redraw();
|
|
|
|
}
|
2020-01-05 18:12:03 +11:00
|
|
|
_ => (),
|
2019-12-04 04:16:06 +11:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-05-25 03:06:21 +10:00
|
|
|
#[cfg(target_arch = "wasm32")]
|
2019-12-04 04:16:06 +11:00
|
|
|
mod wasm {
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
|
|
|
|
#[wasm_bindgen(start)]
|
|
|
|
pub fn run() {
|
|
|
|
console_log::init_with_level(log::Level::Debug);
|
|
|
|
|
|
|
|
super::main();
|
|
|
|
}
|
|
|
|
}
|