summaryrefslogtreecommitdiff
path: root/crates/gipfel-web/src/bridge.rs
diff options
context:
space:
mode:
authorJohannes Hofmann <Johannes.Hofmann@gmx.de>2026-10-27 10:48:03 +0100
committerJohannes Hofmann <Johannes.Hofmann@gmx.de>2026-10-27 10:48:03 +0100
commit2ac8e530b8fb9ca4fa073e1e25a4230a83d99c5d (patch)
tree573b6745d9c7d8d3cb884c955009f6deaadedd87 /crates/gipfel-web/src/bridge.rs
parent236fcded5e7302c4b99f4626b4e8cbc35896ceee (diff)
web: W1 scaffold — Yew app with image display, pan and zoom
First milestone of wasm-plan.md: - new crate gipfel-web: Yew 0.21 (csr), canvas 2D viewport - file input + drag-and-drop; browser-native decode via createImageBitmap (faster than image-crate decode in wasm, GPU-backed blitting) - pan (pointer drag with capture, touch-ready) and cursor-anchored wheel zoom, DPR-aware canvas sizing, fit-to-window - dark sidebar shell: open/fit buttons, status line - Trunk.toml + www/ (index.html, style.css); README with dev setup Verified: cargo check clean (native and wasm32-unknown-unknown), release wasm builds (5.3 MB pre-wasm-opt), all 38 native tests pass. Runtime behavior to be confirmed with trunk serve on a desktop browser.
Diffstat (limited to 'crates/gipfel-web/src/bridge.rs')
-rw-r--r--crates/gipfel-web/src/bridge.rs45
1 files changed, 45 insertions, 0 deletions
diff --git a/crates/gipfel-web/src/bridge.rs b/crates/gipfel-web/src/bridge.rs
new file mode 100644
index 0000000..1353279
--- /dev/null
+++ b/crates/gipfel-web/src/bridge.rs
@@ -0,0 +1,45 @@
+//! Small JS helpers: window access, browser-native image decode, blob
+//! reads, canvas context — the hand-written JS boundary of the app.
+
+use js_sys::Uint8Array;
+use wasm_bindgen::JsCast;
+use wasm_bindgen_futures::JsFuture;
+use web_sys::{Blob, CanvasRenderingContext2d, HtmlCanvasElement, ImageBitmap, Window};
+
+pub fn window() -> Window {
+ web_sys::window().expect("no global window")
+}
+
+/// Decode image bytes into a browser-native (GPU-backed) bitmap.
+/// The browser does the JPEG decode and keeps the pixels where canvas
+/// blitting is fast — cheaper than any pure-Rust path in wasm.
+pub async fn decode_bitmap(bytes: &[u8]) -> Result<ImageBitmap, String> {
+ let array = js_sys::Array::new();
+ array.push(&Uint8Array::from(bytes).buffer());
+ let blob = Blob::new_with_u8_array_sequence(&array).map_err(|_| "blob failed".to_string())?;
+ let promise = window()
+ .create_image_bitmap_with_blob(&blob)
+ .map_err(|_| "createImageBitmap failed".to_string())?;
+ let value = JsFuture::from(promise)
+ .await
+ .map_err(|e| format!("decode failed: {e:?}"))?;
+ value
+ .dyn_into::<ImageBitmap>()
+ .map_err(|_| "decode produced non-bitmap".to_string())
+}
+
+/// Read a File/Blob fully into memory.
+pub async fn read_blob(blob: &Blob) -> Result<Vec<u8>, String> {
+ let buf = JsFuture::from(blob.array_buffer())
+ .await
+ .map_err(|e| format!("read failed: {e:?}"))?;
+ Ok(Uint8Array::new(&buf).to_vec())
+}
+
+pub fn ctx2d(canvas: &HtmlCanvasElement) -> Option<CanvasRenderingContext2d> {
+ canvas
+ .get_context("2d")
+ .ok()
+ .flatten()
+ .and_then(|c| c.dyn_into::<CanvasRenderingContext2d>().ok())
+}