From c2a3ddb1ec887537cd8ec7dd9e3f4a1bdc14d414 Mon Sep 17 00:00:00 2001 From: core Date: Sat, 4 Jan 2025 15:46:09 -0500 Subject: [PATCH] web client & more blending work --- .gitignore | 3 ++- Cargo.lock | 29 +++++++++++++++++++++ wxbox-tiler/src/sources/grib2.rs | 43 +++++++++++++++++++++++--------- wxbox_client/src/lib.rs | 18 ++++++++++--- wxbox_client_wasm/Cargo.toml | 6 +++++ wxbox_client_wasm/index.html | 33 ++++++++++++++++++++++++ wxbox_client_wasm/src/lib.rs | 14 ----------- wxbox_client_wasm/src/main.rs | 30 ++++++++++++++++++++++ wxbox_common/Cargo.toml | 2 +- 9 files changed, 147 insertions(+), 31 deletions(-) create mode 100644 wxbox_client_wasm/index.html delete mode 100644 wxbox_client_wasm/src/lib.rs create mode 100644 wxbox_client_wasm/src/main.rs diff --git a/.gitignore b/.gitignore index 5426951..278cb5d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ */target target node_modules -.cache \ No newline at end of file +.cache +wxbox_client_wasm/dist \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 5ebadcf..15d17e3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1051,6 +1051,16 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "console_error_panic_hook" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a06aeb73f470f66dcdbf7223caeebb85984942f22f1adb2a088cf9668146bbbc" +dependencies = [ + "cfg-if", + "wasm-bindgen", +] + [[package]] name = "convert_case" version = "0.4.0" @@ -5050,6 +5060,17 @@ version = "0.2.99" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "943aab3fdaaa029a6e0271b35ea10b72b943135afe9bffca82384098ad0e06a6" +[[package]] +name = "wasm-tracing" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2107431e9bd9fa9ff3c4e7d384563c849cbfae830866a74d59c3472942bd5af1" +dependencies = [ + "tracing", + "tracing-subscriber", + "wasm-bindgen", +] + [[package]] name = "wayland-backend" version = "0.3.7" @@ -5769,6 +5790,14 @@ dependencies = [ [[package]] name = "wxbox_client_wasm" version = "0.1.0" +dependencies = [ + "console_error_panic_hook", + "eframe", + "wasm-bindgen-futures", + "wasm-tracing", + "web-sys", + "wxbox_client", +] [[package]] name = "wxbox_common" diff --git a/wxbox-tiler/src/sources/grib2.rs b/wxbox-tiler/src/sources/grib2.rs index 3c0f55e..2781aed 100644 --- a/wxbox-tiler/src/sources/grib2.rs +++ b/wxbox-tiler/src/sources/grib2.rs @@ -178,6 +178,30 @@ impl Sub for ColorF64 { } } } +impl Sub for f64 { + type Output = ColorF64; + + fn sub(self, rhs: ColorF64) -> Self::Output { + ColorF64 { + red: rhs.red - self, + green: rhs.green - self, + blue: rhs.blue - self, + alpha: rhs.alpha - self + } + } +} +impl Mul for ColorF64 { + type Output = Self; + + fn mul(self, rhs: f64) -> Self::Output { + Self { + red: self.red * rhs, + blue: self.blue * rhs, + green: self.green * rhs, + alpha: self.alpha * rhs + } + } +} impl Mul for ColorF64 { type Output = ColorF64; @@ -197,20 +221,15 @@ pub fn merge(base: Pixmap, data: Pixmap, settings: &TileRequestOptions) -> Pixma for x in 0..256 { for y in 0..256 { - let mut lower: ColorF64 = base.get(x, y).into(); - lower.alpha = 1.0; - let mut upper: ColorF64 = data.get(x, y).into(); - //upper.alpha = settings.data_transparency; + let mut c_b: ColorF64 = base.get(x, y).into(); + let a_b = 1.0; + let mut c_s: ColorF64 = data.get(x, y).into(); + let a_s = settings.data_transparency; - if upper.red == 0.0 && upper.green == 0.0 && upper.blue == 0.0 && upper.alpha == 0.0 { - lower.alpha = 1.0; - new.set(x, y, lower.into()); - } else { - upper.alpha = 1.0; - new.set(x, y, upper.into()); - } + let mut co = c_s * a_s + c_b * a_b * (1.0 - a_s); + co.alpha = 1.0; -// new.set(x, y, ((ColorF64 { red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0 } - ColorF64 { red: 2.0, green: 2.0, blue: 2.0, alpha: 2.0 } * upper) * (lower * lower) + ColorF64 { red: 2.0, green: 2.0, blue: 2.0, alpha: 2.0 } * lower * upper).into()); + new.set(x, y, co.into()); } } diff --git a/wxbox_client/src/lib.rs b/wxbox_client/src/lib.rs index a765ff1..b3db134 100644 --- a/wxbox_client/src/lib.rs +++ b/wxbox_client/src/lib.rs @@ -25,7 +25,7 @@ impl TileSource for DynamicUrlSource { fn tile_url(&self, tile_id: TileId) -> String { format!( "{}/{}/{}/{}.png?settings={}", - var("TILER_BASE_URL").unwrap(), + env!("TILER_BASE_URL"), tile_id.zoom, tile_id.x, tile_id.y, self.url_query ) @@ -54,7 +54,14 @@ impl WxboxApp { data: "grib2/noaa_mrms_merged_composite_reflectivity_qc".to_string(), data_transparency: 0.9, }), - Default::default(), + HttpOptions { + cache: if cfg!(target_arch = "wasm32") { + None + } else { + Some(".cache".into()) + }, + user_agent: None, + }, ctx.clone() ), map_memory: MapMemory::default() @@ -85,11 +92,16 @@ impl eframe::App for WxboxApp { .collapsible(false) .resizable(false) .title_bar(false) - .anchor(Align2::LEFT_TOP, [10.0, 10.0]) + .anchor(Align2::RIGHT_BOTTOM, [-10.0, -10.0]) .show(ui.ctx(), |ui| { ui.horizontal(|ui| { ui.hyperlink_to(attribution.text, attribution.url); }) + }); + + Window::new("wxbox") + .show(ui.ctx(), |ui| { + ui.label("Welcome to wxbox!") }) }); } diff --git a/wxbox_client_wasm/Cargo.toml b/wxbox_client_wasm/Cargo.toml index 79f5ea5..c9efe49 100644 --- a/wxbox_client_wasm/Cargo.toml +++ b/wxbox_client_wasm/Cargo.toml @@ -4,3 +4,9 @@ version = "0.1.0" edition = "2021" [dependencies] +console_error_panic_hook = "0.1" +wasm-tracing = "1" +eframe = "0.30" +wxbox_client = { path = "../wxbox_client" } +wasm-bindgen-futures = "0.4" +web-sys = "0.3" \ No newline at end of file diff --git a/wxbox_client_wasm/index.html b/wxbox_client_wasm/index.html new file mode 100644 index 0000000..32888f4 --- /dev/null +++ b/wxbox_client_wasm/index.html @@ -0,0 +1,33 @@ + + + + wxbox + + + + + + + + + + \ No newline at end of file diff --git a/wxbox_client_wasm/src/lib.rs b/wxbox_client_wasm/src/lib.rs deleted file mode 100644 index b93cf3f..0000000 --- a/wxbox_client_wasm/src/lib.rs +++ /dev/null @@ -1,14 +0,0 @@ -pub fn add(left: u64, right: u64) -> u64 { - left + right -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn it_works() { - let result = add(2, 2); - assert_eq!(result, 4); - } -} diff --git a/wxbox_client_wasm/src/main.rs b/wxbox_client_wasm/src/main.rs new file mode 100644 index 0000000..382e0aa --- /dev/null +++ b/wxbox_client_wasm/src/main.rs @@ -0,0 +1,30 @@ +use wxbox_client::WxboxApp; +use web_sys::wasm_bindgen::JsCast; + +fn main() { + console_error_panic_hook::set_once(); + wasm_tracing::set_as_global_default(); + + let web_options = eframe::WebOptions::default(); + wasm_bindgen_futures::spawn_local(async { + let document = web_sys::window() + .expect("no window?") + .document() + .expect("no document?"); + + let canvas = document + .get_element_by_id("target") + .expect("failed to find target canvas for rendering") + .dyn_into::() + .expect("#target is not a canvas"); + + eframe::WebRunner::new() + .start( + canvas, + web_options, + Box::new(|cc| Ok(Box::new(WxboxApp::new(cc.egui_ctx.clone())))) + ) + .await + .expect("failed to start eframe") + }) +} \ No newline at end of file diff --git a/wxbox_common/Cargo.toml b/wxbox_common/Cargo.toml index d953d4b..3235e51 100644 --- a/wxbox_common/Cargo.toml +++ b/wxbox_common/Cargo.toml @@ -4,4 +4,4 @@ version = "0.1.0" edition = "2021" [dependencies] -serde = "1" \ No newline at end of file +serde = { version = "1", features = ["derive"] } \ No newline at end of file