From 6d003d2c983d5d0d9a45efa8425ecb19c8a2507f Mon Sep 17 00:00:00 2001 From: core Date: Tue, 7 Nov 2023 09:35:37 -0500 Subject: [PATCH] wgpu basics --- nexrad-browser/src/lib.rs | 31 +++++++++++++++++++++++++++---- nexrad-browser/www/index.html | 17 +++++++++++++++-- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/nexrad-browser/src/lib.rs b/nexrad-browser/src/lib.rs index 98b0fa6..0e95427 100644 --- a/nexrad-browser/src/lib.rs +++ b/nexrad-browser/src/lib.rs @@ -10,13 +10,16 @@ pub mod colors; use std::io::Cursor; use std::ops::{Deref, DerefMut}; use js_sys::Uint8Array; -use log::info; +use log::{debug, info}; use wasm_bindgen::prelude::*; use web_sys::{CanvasRenderingContext2d, HtmlCanvasElement}; use winit::dpi::PhysicalSize; -use winit::event_loop::EventLoop; +use winit::event::{ElementState, Event, WindowEvent}; +use winit::event::WindowEvent::KeyboardInput; +use winit::event_loop::{EventLoop, EventLoopWindowTarget}; +use winit::keyboard::KeyCode; use winit::window::WindowBuilder; -use winit::platform::web::WindowExtWebSys; +use winit::platform::web::{WindowExtWebSys, EventLoopExtWebSys}; use crate::command::{exec, should_newline}; use crate::mode::Mode; use crate::mode::Mode::Reflectivity; @@ -46,13 +49,33 @@ pub fn __nxrd_browser_init() -> AbiScopeState { web_sys::window() .and_then(|win| win.document()) .and_then(|doc| { - let dst = doc.get_element_by_id("wasm-example")?; + let dst = doc.get_element_by_id("canvas-container")?; + // If you see an error here, your IDE is not compiling for webassembly let canvas = web_sys::Element::from(window.canvas().unwrap()); dst.append_child(&canvas).ok()?; Some(()) }) .expect("Couldn't append canvas to document body."); + // If you see an error here, your IDE is not compiling for webassembly + event_loop.spawn(move |event: Event<_>, window: &EventLoopWindowTarget<_>| { + match event { + Event::WindowEvent { ref event, window_id} => { + match event { + KeyboardInput { event, .. } => { + debug!("{:?}", event.physical_key); + if event.physical_key == KeyCode::Escape { + window.exit(); + } + }, + WindowEvent::CloseRequested => { window.exit(); }, + _ => {} + } + }, + _ => {} + } + }); + info!("initializing the scope"); let document = web_sys::window().expect("window should exist").document().expect("document should exist"); diff --git a/nexrad-browser/www/index.html b/nexrad-browser/www/index.html index 4ecbdfb..15a94b3 100644 --- a/nexrad-browser/www/index.html +++ b/nexrad-browser/www/index.html @@ -1,8 +1,19 @@ - + + NEXRAD Browser + + + +
+