#events #channel #winit #js #window #resized

js_resized_event_channel

js resized event channel

8 releases

0.1.9 Oct 12, 2024
0.1.8 Oct 12, 2024

#380 in GUI

Download history 501/week @ 2024-10-08 102/week @ 2024-10-15

603 downloads per month

MIT license

5KB


use js_resized_event_channel::JsResizeEventChannel;
use winit::{dpi::PhysicalSize, event_loop::EventLoop, window::WindowAttributes};

fn main() {
    let web_window = web_sys::window().unwrap();
    let event_loop: EventLoop<()> = EventLoop::builder().build().unwrap();
    let window = event_loop
        .create_window(WindowAttributes::default())
        .unwrap();
    let resize_event_channel = JsResizeEventChannel::init(
        &window,
        web_window
            .document()
            .unwrap()
            .get_element_by_id("container")
            .unwrap(),
    );
    event_loop
        .run(|_, _| {
            if resize_event_channel.try_recv_resized_event() {
                let _ = window.request_inner_size(PhysicalSize {
                    width: web_window.inner_width().unwrap().as_f64().unwrap() as u32 * 2,
                    height: web_window.inner_height().unwrap().as_f64().unwrap() as u32 * 2,
                });
            }
        })
        .unwrap();
}

Dependencies

~3–17MB
~243K SLoC