chiark / gitweb /
dice: Provide die_cooldown_path
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Mon, 11 Apr 2022 18:10:36 +0000 (19:10 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 15 Apr 2022 18:04:23 +0000 (19:04 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
base/misc.rs
wasm/wasm.rs

index f966e5a914f6719dd202c5a1006519e0d9c73ac0..ba3029cf80a4a5036a0871c2dbb0c7228828cba0 100644 (file)
@@ -32,6 +32,30 @@ pub fn raw_angle_transform(compass: u8) -> String {
   else { format!("rotate({})", -45 * (compass as i16)) }
 }
 
+#[throws(fmt::Error)]
+pub fn die_cooldown_path<W: fmt::Write>(mut w: W, r: f64, remaining: f64) {
+  write!(w, "M 0,-{r} A")?;
+
+  let mut arcto = move |proportion: f64| {
+    let angle = proportion * std::f64::consts::TAU;
+    let x = r * angle.cos();
+    let y = -r * angle.sin();
+    write!(w, " {r},{r} 0 0 1 {x},{y}")
+    //                            | | `sweep-flag (1 = clockwise)
+    //                            | `large-arc-flag (see below)
+    //                            `"angle" (ellipse skew angle)
+  };
+  
+  // This avoids ever trying to draw an arc segment that is around 180 degrees.
+  // If we did so there could be rounding errors that would mean we might
+  // disagree with the SVG renderer about whether the angle is <=> 180.
+  for split in [0.33, 0.67] {
+    if split >= remaining { break }
+    arcto(split)?;
+  }
+  arcto(remaining)?;
+}
+
 pub fn default<T:Default>() -> T { Default::default() }
 
 #[macro_export]
index c7db365b321f7a0f09a68045b051c68f4e6dccca..a88928fad8b4fb71ded071ac1d7fa100ea951ba7 100644 (file)
@@ -217,6 +217,16 @@ pub fn space_rect_attrs(x: Number, y: Number) -> JsValue {
     .to_jsvalue()
 }
 
+// ---------- die cooldown ----------
+
+#[wasm_bindgen]
+pub fn die_cooldown_path(radius: f64, remaining: f64) -> JsString {
+  let mut s = String::new();
+  base_misc::die_cooldown_path(&mut s, radius, remaining)
+    .expect("format to string failed!");
+  s.into()
+}
+
 // ---------- setup ----------
 
 #[wasm_bindgen]