diff --git a/nexrad-browser/Cargo.toml b/nexrad-browser/Cargo.toml index 389d2fa..9d91aa4 100644 --- a/nexrad-browser/Cargo.toml +++ b/nexrad-browser/Cargo.toml @@ -22,6 +22,7 @@ wasm-logger = "0.2" serde-wasm-bindgen = "0.6" chrono = "0.4" itertools = "0.11" +winit = "0.29" [dependencies.js-sys] version = "0.3" diff --git a/nexrad-browser/src/command.rs b/nexrad-browser/src/command.rs index 3c55c1f..141d5a8 100644 --- a/nexrad-browser/src/command.rs +++ b/nexrad-browser/src/command.rs @@ -1,7 +1,6 @@ use web_sys::FileReader; use crate::loadar2; use crate::mode::Mode; -use crate::rendering::render; use crate::scope::ScopeState; pub fn should_newline(state: &mut ScopeState) -> bool { @@ -16,7 +15,6 @@ pub fn exec(state: &mut ScopeState, command: String) { } else if command == "CLF RELOAD" { state.command_buf = "SYSTEM PROCESSING".to_string(); state.command_buf_response_mode = true; - render(state).expect("rerender failed"); loadar2("file"); } else if command.starts_with("MODE SET") { if tokens.len() < 3 { diff --git a/nexrad-browser/src/lib.rs b/nexrad-browser/src/lib.rs index 75fec4a..98b0fa6 100644 --- a/nexrad-browser/src/lib.rs +++ b/nexrad-browser/src/lib.rs @@ -1,5 +1,4 @@ pub mod utils; -pub mod rendering; pub mod mode; pub mod scope; pub mod equirectangular; @@ -14,6 +13,10 @@ use js_sys::Uint8Array; use log::info; use wasm_bindgen::prelude::*; use web_sys::{CanvasRenderingContext2d, HtmlCanvasElement}; +use winit::dpi::PhysicalSize; +use winit::event_loop::EventLoop; +use winit::window::WindowBuilder; +use winit::platform::web::WindowExtWebSys; use crate::command::{exec, should_newline}; use crate::mode::Mode; use crate::mode::Mode::Reflectivity; @@ -33,15 +36,26 @@ pub fn __nxrd_browser_init() -> AbiScopeState { wasm_logger::init(wasm_logger::Config::new(log::Level::Debug)); utils::set_panic_hook(); + info!("wgpu setup"); + + let event_loop = EventLoop::new().expect("event loop creation failed"); + let window = WindowBuilder::new().build(&event_loop).unwrap(); + + window.set_min_inner_size(Some(PhysicalSize::new(450, 400))); + + web_sys::window() + .and_then(|win| win.document()) + .and_then(|doc| { + let dst = doc.get_element_by_id("wasm-example")?; + let canvas = web_sys::Element::from(window.canvas().unwrap()); + dst.append_child(&canvas).ok()?; + Some(()) + }) + .expect("Couldn't append canvas to document body."); + info!("initializing the scope"); - info!("finding canvas element"); - let document = web_sys::window().expect("window should exist").document().expect("document should exist"); - let canvas = document.get_element_by_id("canvas").expect("canvas element should exist"); - - let canvas: web_sys::HtmlCanvasElement = canvas.dyn_into::().map_err(|_| ()).expect("canvas is not a canvas"); - let context = canvas.get_context("2d").expect("failed to get canvas rendering context").expect("canvas rendering context doesn't exist").dyn_into::().map_err(|_| ()).expect("2d context is not a 2d context"); info!("finding form input element"); @@ -51,10 +65,6 @@ pub fn __nxrd_browser_init() -> AbiScopeState { let scope_state = ScopeState { ar2: None, scope_mode: Mode::RadarInoperative, - render_state: RenderState { - canvas, - context - }, file_input: file, lat: 30.48500, // jacksonville long: -81.70200, // jacksonville @@ -72,12 +82,14 @@ pub fn __nxrd_browser_init() -> AbiScopeState { AbiScopeState(scope_state) } - +/* #[wasm_bindgen] pub fn render_abi(state: &mut AbiScopeState) { - rendering::render(&mut state.0).unwrap() + old_rendering::render(&mut state.0).unwrap() } + */ + #[wasm_bindgen] pub fn load_ar2(buf: &JsValue, scope: &mut AbiScopeState) { let array = Uint8Array::new(buf); diff --git a/nexrad-browser/src/rendering.rs b/nexrad-browser/src/old_rendering.rs similarity index 100% rename from nexrad-browser/src/rendering.rs rename to nexrad-browser/src/old_rendering.rs diff --git a/nexrad-browser/src/scope.rs b/nexrad-browser/src/scope.rs index bc95670..99bca68 100644 --- a/nexrad-browser/src/scope.rs +++ b/nexrad-browser/src/scope.rs @@ -5,7 +5,6 @@ use crate::mode::Mode; pub struct ScopeState { pub ar2: Option, pub scope_mode: Mode, - pub render_state: RenderState, pub file_input: HtmlInputElement, pub lat: f64, pub long: f64, diff --git a/nexrad-browser/www/index.html b/nexrad-browser/www/index.html index 8a40f6e..4ecbdfb 100644 --- a/nexrad-browser/www/index.html +++ b/nexrad-browser/www/index.html @@ -16,10 +16,6 @@ - - Something went wrong while loading the canvas. Sorry :/ - - diff --git a/nexrad-browser/www/index.js b/nexrad-browser/www/index.js index 45df9d2..c513c51 100644 --- a/nexrad-browser/www/index.js +++ b/nexrad-browser/www/index.js @@ -1,7 +1,7 @@ import * as wasm from "./wasm/nexrad_browser.js"; await wasm.default(); let global_context = wasm.__nxrd_browser_init(); - +/* function rescaleCanvas(canvas, ctx) { var dpr = window.devicePixelRatio || 1; // Get the size of the canvas in CSS pixels. @@ -40,4 +40,6 @@ document.getElementById("file").onchange = () => { let render = () => { wasm.render_abi(global_context); requestAnimationFrame(render); } -requestAnimationFrame(render); \ No newline at end of file +requestAnimationFrame(render); + + */ \ No newline at end of file