|
| 1 | +use winit::{ |
| 2 | + event::{Event, WindowEvent}, |
| 3 | + event_loop::{ControlFlow, EventLoop}, |
| 4 | + window::WindowBuilder, |
| 5 | +}; |
| 6 | + |
| 7 | +pub fn main() { |
| 8 | + let event_loop = EventLoop::new(); |
| 9 | + |
| 10 | + let window = WindowBuilder::new() |
| 11 | + .with_title("A fantastic window!") |
| 12 | + .build(&event_loop) |
| 13 | + .unwrap(); |
| 14 | + |
| 15 | + #[cfg(feature = "web-sys")] |
| 16 | + { |
| 17 | + use winit::platform::web::WindowExtWebSys; |
| 18 | + |
| 19 | + let canvas = window.canvas(); |
| 20 | + |
| 21 | + let window = web_sys::window().unwrap(); |
| 22 | + let document = window.document().unwrap(); |
| 23 | + let body = document.body().unwrap(); |
| 24 | + |
| 25 | + body.append_child(&canvas) |
| 26 | + .expect("Append canvas to HTML body"); |
| 27 | + } |
| 28 | + |
| 29 | + #[cfg(feature = "stdweb")] |
| 30 | + { |
| 31 | + use std_web::web::INode; |
| 32 | + use winit::platform::web::WindowExtStdweb; |
| 33 | + |
| 34 | + let canvas = window.canvas(); |
| 35 | + |
| 36 | + let document = std_web::web::document(); |
| 37 | + let body: std_web::web::Node = document.body().expect("Get HTML body").into(); |
| 38 | + |
| 39 | + body.append_child(&canvas); |
| 40 | + } |
| 41 | + |
| 42 | + event_loop.run(move |event, _, control_flow| { |
| 43 | + #[cfg(feature = "web-sys")] |
| 44 | + log::debug!("{:?}", event); |
| 45 | + |
| 46 | + #[cfg(feature = "stdweb")] |
| 47 | + std_web::console!(log, "%s", format!("{:?}", event)); |
| 48 | + |
| 49 | + match event { |
| 50 | + Event::WindowEvent { |
| 51 | + event: WindowEvent::CloseRequested, |
| 52 | + window_id, |
| 53 | + } if window_id == window.id() => *control_flow = ControlFlow::Exit, |
| 54 | + Event::MainEventsCleared => { |
| 55 | + window.request_redraw(); |
| 56 | + } |
| 57 | + _ => *control_flow = ControlFlow::Wait, |
| 58 | + } |
| 59 | + }); |
| 60 | +} |
| 61 | + |
| 62 | +#[cfg(feature = "web-sys")] |
| 63 | +mod wasm { |
| 64 | + use wasm_bindgen::prelude::*; |
| 65 | + |
| 66 | + #[wasm_bindgen(start)] |
| 67 | + pub fn run() { |
| 68 | + console_log::init_with_level(log::Level::Debug); |
| 69 | + |
| 70 | + super::main(); |
| 71 | + } |
| 72 | +} |
0 commit comments