From 70784357c804d3411bb93fa6e0a74c9ffcd3d3ad Mon Sep 17 00:00:00 2001 From: Johannes Hofmann Date: Tue, 27 Oct 2026 12:06:13 +0100 Subject: web: W3 — compass strip, parameter sliders, collision-free labels MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - compass.rs: canvas port of the desktop Aligned compass strip — adaptive tick step, direction labels (red N), center triangle and azimuth readout, drag to rotate the view (Newton inverse projection, same solver as desktop; round-trip tests ported and passing) - sidebar sections like the desktop: Viewpoint (lat/lon/height number inputs), Orientation (azimuth/focal/pitch/roll sliders, desktop ranges), Projection (fit-distortion toggle), Display (visibility with the 0.11-r inversion, label size 8-28 px) - every control applies to the Panorama immediately (focal maps via scale = focal * w / 35, same formula as the metadata path) - marker labels: ported the desktop layout_label_y collision push-up with yellow leader lines (cross, vertical, horizontal), label font size live-adjustable - main area split: canvas + 56 px compass strip --- crates/gipfel-web/Cargo.toml | 1 + crates/gipfel-web/src/app.rs | 192 +++++++++++++++++-- crates/gipfel-web/src/canvas_view.rs | 158 ++++++++++++++-- crates/gipfel-web/src/compass.rs | 349 +++++++++++++++++++++++++++++++++++ crates/gipfel-web/src/main.rs | 2 + crates/gipfel-web/src/widgets.rs | 76 ++++++++ crates/gipfel-web/www/style.css | 54 +++++- 7 files changed, 792 insertions(+), 40 deletions(-) create mode 100644 crates/gipfel-web/src/compass.rs create mode 100644 crates/gipfel-web/src/widgets.rs (limited to 'crates') diff --git a/crates/gipfel-web/Cargo.toml b/crates/gipfel-web/Cargo.toml index 4d846c1..4cb78a7 100644 --- a/crates/gipfel-web/Cargo.toml +++ b/crates/gipfel-web/Cargo.toml @@ -35,6 +35,7 @@ features = [ "PointerEvent", "Request", "Response", + "TextMetrics", "Window", "WheelEvent", ] diff --git a/crates/gipfel-web/src/app.rs b/crates/gipfel-web/src/app.rs index f152dcc..0874e9a 100644 --- a/crates/gipfel-web/src/app.rs +++ b/crates/gipfel-web/src/app.rs @@ -15,7 +15,40 @@ use yew::prelude::*; use crate::bridge; use crate::canvas_view::{CanvasView, MarkerView, ViewCallbacks, ViewState}; +use crate::compass::{Compass, CompassData}; use crate::markers::{self, Marker}; +use crate::widgets::{NumItem, SliderItem}; + +/// sidebar-editable view parameters (degrees / meters / mm) +#[derive(Clone, Copy, PartialEq)] +struct UiParams { + lat: f64, + lon: f64, + height: f64, + az: f64, + focal: f64, + pitch: f64, + roll: f64, + /// visibility UI value: 0.11 - height_dist_ratio (right = more visible) + vis: f64, + label_font: f32, +} + +impl Default for UiParams { + fn default() -> Self { + UiParams { + lat: 0.0, + lon: 0.0, + height: 0.0, + az: 0.0, + focal: 50.0, + pitch: 0.0, + roll: 0.0, + vis: 0.04, + label_font: 12.0, + } + } +} fn gunzip(gz: &[u8]) -> Result, String> { let mut raw = Vec::new(); @@ -39,6 +72,8 @@ pub fn app() -> Html { let markers = use_state(Vec::::new); let fit_distortion = use_state(|| false); let hover = use_state(|| Option::::None); + let vp = use_state(UiParams::default); + let img_size = use_state(|| (0u32, 0u32)); // force a redraw when the window is resized so the canvas backing // store tracks its CSS size (listener lives as long as the app) @@ -134,9 +169,13 @@ pub fn app() -> Html { let pan = pan.clone(); let markers = markers.clone(); let fit_info = fit_info.clone(); + let vp = vp.clone(); + let img_size = img_size.clone(); Callback::from(move |(bytes, name): (Vec, String)| { let (bitmap, status, fit, pan, markers, fit_info) = (bitmap.clone(), status.clone(), fit.clone(), pan.clone(), markers.clone(), fit_info.clone()); + let vp = vp.clone(); + let img_size = img_size.clone(); spawn_local(async move { match bridge::decode_bitmap(&bytes).await { Ok(bmp) => { @@ -145,6 +184,22 @@ pub fn app() -> Html { md.load_image_from_bytes(&bytes); let have = apply_image_metadata(&mut pan.borrow_mut(), &md, w, h); let vis = pan.borrow().visible.len(); + let u = { + let p = pan.borrow(); + UiParams { + lat: p.get_view_lat(), + lon: p.get_view_long(), + height: p.get_view_height(), + az: p.get_center_angle().rem_euclid(360.0), + focal: (p.get_scale() * 35.0 / w as f64).clamp(1.0, 300.0), + pitch: p.get_pitch_angle(), + roll: p.get_roll_angle(), + vis: (0.11 - p.height_dist_ratio).clamp(0.01, 0.1), + label_font: vp.label_font, + } + }; + vp.set(u); + img_size.set((w, h)); let vp = if have { format!("viewpoint from image · {vis} peaks visible") } else { @@ -224,6 +279,73 @@ pub fn app() -> Html { }) }; + // sidebar parameter editing: update UI state and apply to the panorama + macro_rules! param_cb { + ($field:ident, $apply:expr) => {{ + let vp = vp.clone(); + let pan = pan.clone(); + Callback::from(move |v: f64| { + let mut u = *vp; + u.$field = v; + vp.set(u); + let f: &dyn Fn(&mut Panorama, f64) = &$apply; + f(&mut pan.borrow_mut(), v); + }) + }}; + } + let on_lat = param_cb!(lat, |p: &mut Panorama, v| p.set_view_lat(v)); + let on_lon = param_cb!(lon, |p, v| p.set_view_long(v)); + let on_height = param_cb!(height, |p, v| p.set_view_height(v)); + let on_az = param_cb!(az, |p, v| p.set_center_angle(v)); + let on_pitch = param_cb!(pitch, |p, v| p.set_pitch_angle(v)); + let on_roll = param_cb!(roll, |p, v| p.set_roll_angle(v)); + let on_vis = param_cb!(vis, |p, v| p.set_height_dist_ratio(0.11 - v)); + let on_focal = { + let vp = vp.clone(); + let pan = pan.clone(); + let img_size = img_size.clone(); + Callback::from(move |v: f64| { + let mut u = *vp; + u.focal = v; + vp.set(u); + let w = img_size.0 as f64; + if w > 0.0 { + pan.borrow_mut().set_scale(v * w / 35.0); + } + }) + }; + let on_font = { + let vp = vp.clone(); + Callback::from(move |v: f64| { + let mut u = *vp; + u.label_font = v as f32; + vp.set(u); + }) + }; + + // compass strip: shares the image transform, drag changes azimuth + let compass_data = { + let p = pan.borrow(); + CompassData { + ptype: p.projection_type, + parms: p.parms, + center_deg: p.get_center_angle(), + zoom: view.scale, + cx: view.ox + img_size.0 as f64 * view.scale / 2.0, + max_view_angle: p.max_view_angle, + } + }; + let compass_change = { + let pan = pan.clone(); + let vp = vp.clone(); + Callback::from(move |deg: f64| { + pan.borrow_mut().set_center_angle(deg); + let mut u = *vp; + u.az = deg.rem_euclid(360.0); + vp.set(u); + }) + }; + let callbacks = ViewCallbacks { pan: { let view = view.clone(); @@ -366,17 +488,41 @@ pub fn app() -> Html {

{ (*status).clone() }

{ (*db_status).clone() }

if bitmap.is_some() { - -

{ format!("{n_markers} markers · zoom {zoom_pct:.0} %") }

- if !fit_info.is_empty() { -

{ (*fit_info).clone() }

- } - if let Some(h) = hover.as_ref() { -

{ h.clone() }

- } + <> +
{ "Viewpoint" }
+ + + +
{ "Orientation" }
+ + + + +
{ "Projection" }
+ +
{ "Display" }
+ + +

{ format!("{n_markers} markers · zoom {zoom_pct:.0} %") }

+ if !fit_info.is_empty() { +

{ (*fit_info).clone() }

+ } + if let Some(h) = hover.as_ref() { +

{ h.clone() }

+ } + }

{ "Hover a green dot to see the peak · click to mark it · drag to adjust · right-click to remove." } @@ -386,15 +532,21 @@ pub fn app() -> Html {

- +
+ +
+ if bitmap.is_some() { + + }
} diff --git a/crates/gipfel-web/src/canvas_view.rs b/crates/gipfel-web/src/canvas_view.rs index d870029..ab72b7f 100644 --- a/crates/gipfel-web/src/canvas_view.rs +++ b/crates/gipfel-web/src/canvas_view.rs @@ -53,6 +53,8 @@ pub struct Props { pub markers: Vec, /// projected positions of all visible hills (small guide dots) pub candidates: Vec<(f64, f64)>, + /// marker label font size, screen px + pub label_font: f32, } #[derive(Clone, PartialEq)] @@ -85,9 +87,10 @@ pub fn canvas_view(props: &Props) -> Html { let canvas_ref = props.canvas_ref.clone(); let markers = props.markers.clone(); let candidates = props.candidates.clone(); + let label_font = props.label_font; use_effect_with( - (bitmap, view, props.tick, markers.clone(), candidates.clone()), - move |(bitmap, view, _tick, markers, candidates)| { + (bitmap, view, props.tick, markers.clone(), candidates.clone(), label_font), + move |(bitmap, view, _tick, markers, candidates, label_font)| { let Some(canvas) = canvas_ref.cast::() else { return; }; @@ -137,7 +140,7 @@ pub fn canvas_view(props: &Props) -> Html { continue; } ctx.begin_path(); - ctx.arc(px, py, 4.5, 0.0, std::f64::consts::TAU); + let _ = ctx.arc(px, py, 4.5, 0.0, std::f64::consts::TAU); ctx.fill(); } #[allow(deprecated)] @@ -148,31 +151,83 @@ pub fn canvas_view(props: &Props) -> Html { continue; } ctx.begin_path(); - ctx.arc(px, py, 2.5, 0.0, std::f64::consts::TAU); + let _ = ctx.arc(px, py, 2.5, 0.0, std::f64::consts::TAU); ctx.fill(); } - // markers: red cross + name - ctx.set_font("12px system-ui, sans-serif"); - for m in markers { - let (px, py) = (sx(m.x), sy(m.y)); + // markers: yellow leader lines, red crosses, collision-free + // labels (port of the desktop viewport drawing) + if !markers.is_empty() { + let label_font = *label_font; + let line_h = label_font * 1.3 + 2.0; + ctx.set_font(&format!("{label_font:.0}px system-ui, sans-serif")); + // layout runs in screen space, sorted by x + let mut order: Vec = (0..markers.len()) + .filter(|&i| { + markers[i].x.abs() * 2.0 <= bitmap.width() as f64 + && markers[i].y.abs() * 2.0 <= bitmap.height() as f64 + }) + .collect(); + order.sort_by(|&a, &b| sx(markers[a].x).total_cmp(&sx(markers[b].x))); + let xs: Vec = order.iter().map(|&i| sx(markers[i].x) as f32).collect(); + let ys: Vec = order.iter().map(|&i| sy(markers[i].y) as f32).collect(); + let widths: Vec = order + .iter() + .map(|&i| { + ctx.measure_text(&markers[i].name) + .map(|m| m.width() as f32) + .unwrap_or_else(|_| markers[i].name.len() as f32 * 7.0) + }) + .collect(); + let in_img = vec![true; order.len()]; + let entries = layout_label_y(&xs, &ys, &widths, &in_img, line_h); + + // leaders (yellow): cross, vertical, horizontal + #[allow(deprecated)] + ctx.set_stroke_style(&wasm_bindgen::JsValue::from_str("rgb(255, 230, 51)")); + ctx.set_line_width(1.0); + for (k, &i) in order.iter().enumerate() { + let (px, py) = (sx(markers[i].x), sy(markers[i].y)); + let (lx, ly) = entries[k]; + let label_line = py + ly as f64 - line_h as f64; + ctx.begin_path(); + ctx.move_to(px - 4.0, py); + ctx.line_to(px + 4.0, py); + ctx.move_to(px, py); + ctx.line_to(px, label_line); + ctx.move_to(px, label_line); + ctx.line_to(px + lx as f64, label_line); + ctx.stroke(); + } + // marker crosses (always red) #[allow(deprecated)] ctx.set_stroke_style(&wasm_bindgen::JsValue::from_str("rgb(255, 64, 64)")); ctx.set_line_width(1.5); - ctx.begin_path(); - ctx.move_to(px - 5.0, py); - ctx.line_to(px + 5.0, py); - ctx.move_to(px, py - 5.0); - ctx.line_to(px, py + 5.0); - ctx.stroke(); - if !m.name.is_empty() { - // dark shadow + white text stays readable on any photo + for &i in &order { + let (px, py) = (sx(markers[i].x), sy(markers[i].y)); + ctx.begin_path(); + ctx.move_to(px - 5.0, py); + ctx.line_to(px + 5.0, py); + ctx.move_to(px, py - 5.0); + ctx.line_to(px, py + 5.0); + ctx.stroke(); + } + // labels: white on dark shadow, right-aligned at leader end + for (k, &i) in order.iter().enumerate() { + if markers[i].name.is_empty() { + continue; + } + let (px, py) = (sx(markers[i].x), sy(markers[i].y)); + let (lx, ly) = entries[k]; + let label_line = py + ly as f64 - line_h as f64; + let tx = px + lx as f64 - widths[k] as f64; + let ty = label_line - 3.0; #[allow(deprecated)] - ctx.set_fill_style(&wasm_bindgen::JsValue::from_str("rgba(0, 0, 0, 0.8)")); - ctx.fill_text(&m.name, px + 7.0, py - 5.0).ok(); + ctx.set_fill_style(&wasm_bindgen::JsValue::from_str("rgba(0, 0, 0, 0.85)")); + ctx.fill_text(&markers[i].name, tx + 1.0, ty + 1.0).ok(); #[allow(deprecated)] ctx.set_fill_style(&wasm_bindgen::JsValue::from_str("white")); - ctx.fill_text(&m.name, px + 6.0, py - 6.0).ok(); + ctx.fill_text(&markers[i].name, tx, ty).ok(); } } }, @@ -356,3 +411,68 @@ pub fn canvas_view(props: &Props) -> Html { /> } } + +/// Port of the desktop `viewport::layout_label_y`: place labels right of +/// their crosses, pushing colliding ones up; iterate to a fixed point +/// (the C single pass can leave overlaps). Inputs must be sorted by x. +fn layout_label_y( + xs: &[f32], + ys: &[f32], + widths: &[f32], + in_img: &[bool], + line_h: f32, +) -> Vec<(f32, f32)> { + let n = xs.len(); + let mut label_x = vec![0.0f32; n]; + let mut label_y = vec![0.0f32; n]; + let cross = 4.0f32; + let overlap = |a1: f32, l1: f32, a2: f32, l2: f32| a1 <= a2 + l2 && a1 + l1 >= a2; + + for i in 0..n { + if !in_img[i] { + continue; + } + label_x[i] = widths[i] + 1.0; + label_y[i] = 0.0; + + let mut colliding: Vec = Vec::new(); + for j in (0..i).rev() { + if !in_img[j] { + continue; + } + if overlap(xs[i], label_x[i], xs[j] - cross, label_x[j]) { + colliding.push(j); + } else { + break; + } + } + colliding.sort_by(|&a, &b| { + let ya = ys[a] + label_y[a]; + let yb = ys[b] + label_y[b]; + yb.total_cmp(&ya) + }); + for _round in 0..64 { + let mut pushed = false; + for &j in &colliding { + if overlap( + ys[i] + label_y[i] - line_h, + line_h, + ys[j] + label_y[j] - line_h, + line_h, + ) || overlap(ys[i] + label_y[i] - line_h, line_h, ys[j] - cross, 2.0 * cross) + { + let ny = (ys[j] + label_y[j] - ys[i] - line_h - 2.0).round(); + if ny < label_y[i] { + label_y[i] = ny; + pushed = true; + } + } + } + if !pushed { + break; + } + } + } + + (0..n).map(|i| (label_x[i], label_y[i])).collect() +} diff --git a/crates/gipfel-web/src/compass.rs b/crates/gipfel-web/src/compass.rs new file mode 100644 index 0000000..b539e07 --- /dev/null +++ b/crates/gipfel-web/src/compass.rs @@ -0,0 +1,349 @@ +//! Compass strip (canvas port of the desktop `compass.rs`, Aligned style): +//! shares the image's horizontal transform so each direction sits above +//! its position in the image; drag horizontally to change the view azimuth. + +use web_sys::{HtmlCanvasElement, PointerEvent};use yew::prelude::*; + +use gipfel_core::projection::{projection_from_type, ProjArgs, Projection, ProjectionType, ViewParams}; + +use crate::bridge; + +pub const STRIP_H: f64 = 56.0; + +/// Damped, clamped Newton iteration for a scalar inverse projection: find +/// `a` such that `f(a) ≈ target` (identical to the desktop solver). +pub fn newton_inverse( + target: f64, + mut f: impl FnMut(f64) -> Option, + seed: f64, + lo: f64, + hi: f64, +) -> f64 { + let mut a = seed.clamp(lo, hi); + for _ in 0..6 { + let Some(y) = f(a) else { break }; + let resid = y - target; + let h = 1e-5; + let (Some(y1), Some(y2)) = (f(a + h), f(a - h)) else { + break; + }; + let d = (y1 - y2) / (2.0 * h); + if !d.is_finite() || d.abs() < 1e-9 { + break; + } + let step = (resid / d).clamp(-0.3, 0.3); + a = (a - step).clamp(lo, hi); + if step.abs() < 1e-10 { + break; + } + } + a +} + +/// azimuth whose image x is `x_img`, measured with `c_ref` as center angle +pub fn alph_at_img_x( + ptype: ProjectionType, + proj: &dyn Projection, + p: &ViewParams, + x_img: f64, + c_ref: f64, +) -> f64 { + let mut p = *p; + p.a_center = c_ref; + let seed = match ptype { + ProjectionType::Rectilinear => c_ref + (x_img / p.scale).atan(), + ProjectionType::Cylindrical => c_ref + x_img / p.scale, + }; + let (lo, hi) = match ptype { + ProjectionType::Rectilinear => (c_ref - 1.55, c_ref + 1.55), + ProjectionType::Cylindrical => (c_ref - 3.1, c_ref + 3.1), + }; + newton_inverse( + x_img, + |a| Some(proj.mac_x(ProjArgs::from_params(&p, a, 0.0))), + seed, + lo, + hi, + ) +} + +const DIRS: [(f64, &str); 8] = [ + (0.0, "N"), + (45.0, "NE"), + (90.0, "E"), + (135.0, "SE"), + (180.0, "S"), + (225.0, "SW"), + (270.0, "W"), + (315.0, "NW"), +]; + +/// everything the strip needs; all plain data so Yew can diff it +#[derive(Clone, Copy, PartialEq)] +pub struct CompassData { + pub ptype: ProjectionType, + pub parms: ViewParams, + pub center_deg: f64, + pub zoom: f64, + /// screen x of the image center (strip shares the image transform) + pub cx: f64, + /// lens FOV half-angle limit, radians (0 = none) + pub max_view_angle: f64, +} + +#[derive(Properties, PartialEq)] +pub struct Props { + pub data: CompassData, + /// bumped on window resize to force a redraw + pub tick: u32, + /// new azimuth in degrees while dragging + pub on_change: Callback, +} + +#[function_component(Compass)] +pub fn compass(props: &Props) -> Html { + let canvas_ref = use_node_ref(); + let drag = use_mut_ref(|| Option::<(f64, f64)>::None); // (press x, center_deg) + + let d = props.data; + + // draw + { + let canvas_ref = canvas_ref.clone(); + use_effect_with((d, props.tick), move |(d, _tick)| { + let Some(canvas) = canvas_ref.cast::() else { + return; + }; + let Some(ctx) = bridge::ctx2d(&canvas) else { + return; + }; + let dpr = bridge::window().device_pixel_ratio(); + let w = canvas.client_width() as f64; + let bw = (w * dpr).max(1.0) as u32; + let bh = (STRIP_H * dpr).max(1.0) as u32; + if canvas.width() != bw || canvas.height() != bh { + canvas.set_width(bw); + canvas.set_height(bh); + } + let _ = ctx.set_transform(dpr, 0.0, 0.0, dpr, 0.0, 0.0); + + let on = "rgb(217, 217, 217)"; + let bg = "rgb(41, 41, 41)"; + let divider = "rgb(82, 82, 82)"; + let red = "rgb(255, 102, 64)"; + + let fill = |c: &str| { + #[allow(deprecated)] + ctx.set_fill_style(&wasm_bindgen::JsValue::from_str(c)); + }; + let stroke = |c: &str, width: f64| { + #[allow(deprecated)] + ctx.set_stroke_style(&wasm_bindgen::JsValue::from_str(c)); + ctx.set_line_width(width); + }; + + fill(bg); + ctx.fill_rect(0.0, 0.0, w, STRIP_H); + stroke(divider, 1.0); + ctx.begin_path(); + ctx.move_to(0.0, 0.5); + ctx.line_to(w, 0.5); + ctx.stroke(); + + let label_y = 17.0f64; // text baseline of the direction row + let y0 = (STRIP_H - 10.0).round(); // baseline of the ticks + let tri_y = 25.0f64; + let z = d.zoom; + let proj = projection_from_type(d.ptype); + let cx = d.cx; + let c = d.parms.a_center; + + let x_img_at = |alph: f64| proj.get_coordinates(alph, 0.0, &d.parms).0; + + // adaptive tick step: first that yields >= 6 px spacing + let ppd = ((x_img_at(c + 1f64.to_radians()) - x_img_at(c - 1f64.to_radians())) + * z + / 2.0) + .abs(); + let step = [1.0f64, 2.0, 5.0, 10.0, 15.0, 30.0, 45.0, 90.0] + .into_iter() + .find(|s| s * ppd >= 6.0) + .unwrap_or(90.0); + + let a_lo = alph_at_img_x(d.ptype, &*proj, &d.parms, -cx / z, c) + .min(c - std::f64::consts::PI); + let a_hi = alph_at_img_x(d.ptype, &*proj, &d.parms, (w - cx) / z, c) + .max(c + std::f64::consts::PI); + // rectilinear folds directions behind the camera onto the same + // pixels; also clamp to the lens FOV (distortion can fold + // out-of-view directions back onto the strip) + let mut fold = match d.ptype { + ProjectionType::Rectilinear => 1.535, + ProjectionType::Cylindrical => std::f64::consts::PI, + }; + if d.max_view_angle > 0.0 { + fold = fold.min(d.max_view_angle); + } + let a_lo = a_lo.max(c - fold); + let a_hi = a_hi.min(c + fold); + + // baseline + stroke(on, 1.0); + ctx.begin_path(); + ctx.move_to(0.0, y0 + 0.5); + ctx.line_to(w, y0 + 0.5); + ctx.stroke(); + + // ticks + let step_rad = step.to_radians(); + let mut a = (a_lo / step_rad).floor() * step_rad; + while a <= a_hi { + let x = cx + x_img_at(a) * z; + if x.is_finite() && x >= -1.0 && x <= w + 1.0 { + let rem = a.to_degrees().rem_euclid(45.0); + let major = rem.min(45.0 - rem) < step / 2.0; + let x = x.round(); + ctx.begin_path(); + ctx.move_to(x, y0); + ctx.line_to(x, y0 - if major { 12.0 } else { 6.0 }); + ctx.stroke(); + } + a += step_rad; + } + + // direction labels + ctx.set_font("13px system-ui, sans-serif"); + ctx.set_text_align("center"); + for (deg, name) in DIRS { + let alph = deg.to_radians(); + let rel = (alph - c + std::f64::consts::PI).rem_euclid(std::f64::consts::TAU) + - std::f64::consts::PI; + if rel.abs() > fold { + continue; + } + let x = cx + x_img_at(alph) * z; + if !x.is_finite() || x < -20.0 || x > w + 20.0 { + continue; + } + fill(if deg == 0.0 { red } else { on }); + ctx.fill_text(name, x, label_y).ok(); + } + ctx.set_text_align("start"); + + // center marker: triangle + hairline + azimuth readout + fill(red); + ctx.begin_path(); + ctx.move_to(cx - 6.0, tri_y); + ctx.line_to(cx + 6.0, tri_y); + ctx.line_to(cx, tri_y + 8.0); + ctx.close_path(); + ctx.fill(); + stroke(red, 1.0); + ctx.begin_path(); + ctx.move_to(cx.round(), tri_y + 8.0); + ctx.line_to(cx.round(), y0); + ctx.stroke(); + fill(red); + ctx.fill_text(&format!("{:05.1}°", d.center_deg.rem_euclid(360.0)), cx + 10.0, tri_y + 9.0) + .ok(); + }); + } + + let local_x = { + let canvas_ref = canvas_ref.clone(); + move |client_x: f64| { + client_x + - canvas_ref + .cast::() + .map(|c| c.get_bounding_client_rect().left()) + .unwrap_or(0.0) + } + }; + + let on_pointerdown = { + let drag = drag.clone(); + let canvas_ref = canvas_ref.clone(); + let center_deg = props.data.center_deg; + Callback::from(move |e: PointerEvent| { + e.prevent_default(); + if let Some(c) = canvas_ref.cast::() { + let _ = c.set_pointer_capture(e.pointer_id()); + } + *drag.borrow_mut() = Some((e.client_x() as f64, center_deg)); + }) + }; + let on_pointermove = { + let drag = drag.clone(); + let on_change = props.on_change.clone(); + let d = props.data; + Callback::from(move |e: PointerEvent| { + let Some((x0, c0_deg)) = *drag.borrow() else { + return; + }; + let proj = projection_from_type(d.ptype); + let c0 = c0_deg.to_radians(); + let lx0 = local_x(e.client_x() as f64); + let a0 = alph_at_img_x(d.ptype, &*proj, &d.parms, (x0 - d.cx) / d.zoom, c0); + let a1 = alph_at_img_x(d.ptype, &*proj, &d.parms, (lx0 - d.cx) / d.zoom, c0); + on_change.emit((c0 - (a1 - a0)).to_degrees()); + }) + }; + let on_pointerup = { + let drag = drag.clone(); + let canvas_ref = canvas_ref.clone(); + Callback::from(move |e: PointerEvent| { + *drag.borrow_mut() = None; + if let Some(c) = canvas_ref.cast::() { + let _ = c.release_pointer_capture(e.pointer_id()); + } + }) + }; + + html! { + + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn roundtrip(ptype: ProjectionType, k0: f64, k1: f64, x0: f64) { + let proj = projection_from_type(ptype); + let mut parms = ViewParams::new(); + parms.a_center = 0.3; + parms.scale = 3000.0; + parms.k0 = k0; + parms.k1 = k1; + parms.x0 = x0; + for x in [-2500.0, -800.0, -50.0, 0.0, 120.0, 900.0, 2400.0] { + let a = alph_at_img_x(ptype, &*proj, &parms, x, parms.a_center); + let (x2, _) = proj.get_coordinates(a, 0.0, &parms); + assert!( + (x2 - x).abs() < 1e-6, + "{ptype:?} x={x}: round-trips to {x2} (alph {a})" + ); + } + } + + #[test] + fn inverse_rectilinear() { + roundtrip(ProjectionType::Rectilinear, 0.0, 0.0, 0.0); + } + #[test] + fn inverse_rectilinear_distorted() { + roundtrip(ProjectionType::Rectilinear, 0.002, -0.0003, 0.01); + } + #[test] + fn inverse_cylindrical() { + roundtrip(ProjectionType::Cylindrical, 0.0, 0.0, 0.0); + } +} diff --git a/crates/gipfel-web/src/main.rs b/crates/gipfel-web/src/main.rs index 8a39039..741393b 100644 --- a/crates/gipfel-web/src/main.rs +++ b/crates/gipfel-web/src/main.rs @@ -1,7 +1,9 @@ mod app; mod bridge; mod canvas_view; +mod compass; mod markers; +mod widgets; fn main() { yew::Renderer::::new().render(); diff --git a/crates/gipfel-web/src/widgets.rs b/crates/gipfel-web/src/widgets.rs new file mode 100644 index 0000000..f93f424 --- /dev/null +++ b/crates/gipfel-web/src/widgets.rs @@ -0,0 +1,76 @@ +//! Small form controls: full-width slider with value readout above +//! (mirrors the desktop `slider_item` layout) and a numeric input row. + +use web_sys::HtmlInputElement; +use yew::prelude::*; + +#[derive(Properties, PartialEq)] +pub struct SliderProps { + pub label: AttrValue, + /// formatted value shown right of the label + pub display: AttrValue, + pub value: f64, + pub min: f64, + pub max: f64, + pub step: f64, + pub onchange: Callback, +} + +#[function_component(SliderItem)] +pub fn slider_item(p: &SliderProps) -> Html { + let onchange = { + let onchange = p.onchange.clone(); + Callback::from(move |e: Event| { + let input: HtmlInputElement = e.target_unchecked_into(); + if let Ok(v) = input.value().parse::() { + onchange.emit(v); + } + }) + }; + html! { +
+
+ { p.label.as_ref().to_string() } + { p.display.as_ref().to_string() } +
+ +
+ } +} + +#[derive(Properties, PartialEq)] +pub struct NumProps { + pub label: AttrValue, + pub value: f64, + pub step: f64, + #[prop_or(2)] + pub decimals: usize, + pub onchange: Callback, +} + +#[function_component(NumItem)] +pub fn num_item(p: &NumProps) -> Html { + let onchange = { + let onchange = p.onchange.clone(); + Callback::from(move |e: Event| { + let input: HtmlInputElement = e.target_unchecked_into(); + if let Ok(v) = input.value().parse::() { + onchange.emit(v); + } + }) + }; + let display = format!("{:.*}", p.decimals, p.value); + html! { + + } +} diff --git a/crates/gipfel-web/www/style.css b/crates/gipfel-web/www/style.css index 1e632f2..523ee2b 100644 --- a/crates/gipfel-web/www/style.css +++ b/crates/gipfel-web/www/style.css @@ -80,8 +80,14 @@ body { } .main { flex: 1; - position: relative; min-width: 0; + display: flex; + flex-direction: column; +} +.canvaswrap { + flex: 1; + position: relative; + min-height: 0; } canvas.viewport { position: absolute; @@ -92,3 +98,49 @@ canvas.viewport { touch-action: none; cursor: grab; } +canvas.compass { + display: block; + width: 100%; + touch-action: none; + cursor: ew-resize; +} +.section { + margin-top: 14px; + font-size: 11px; + text-transform: uppercase; + letter-spacing: 1px; + color: #8a8a94; + border-bottom: 1px solid #2c2c34; + padding-bottom: 3px; +} +.slider { + display: flex; + flex-direction: column; + gap: 2px; + margin-top: 8px; +} +.sliderhead { + display: flex; + justify-content: space-between; + font-size: 12px; + color: #c8c8d0; +} +.slider input[type="range"] { + width: 100%; +} +.numrow { + display: flex; + justify-content: space-between; + align-items: center; + gap: 8px; + font-size: 13px; + margin-top: 6px; +} +.numrow input { + width: 130px; + padding: 4px 6px; + background: #24242c; + color: #e8e8ea; + border: 1px solid #34343e; + border-radius: 4px; +} -- cgit v1.2.3