60 lines
1.7 KiB
Rust
60 lines
1.7 KiB
Rust
use bezier_rs::{ManipulatorGroup, Subpath};
|
|
use glam::DVec2;
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
use crate::svg_drawing::*;
|
|
|
|
/// Wrapper of the `Subpath` struct to be used in JS.
|
|
#[wasm_bindgen]
|
|
pub struct WasmSubpath(Subpath);
|
|
|
|
#[wasm_bindgen]
|
|
impl WasmSubpath {
|
|
/// Expects js_points to be an unbounded list of triples, where each item is a tuple of floats.
|
|
pub fn from_triples(js_points: &JsValue, closed: bool) -> WasmSubpath {
|
|
let point_triples: Vec<[Option<DVec2>; 3]> = js_points.into_serde().unwrap();
|
|
let manipulator_groups = point_triples
|
|
.into_iter()
|
|
.map(|point_triple| ManipulatorGroup {
|
|
anchor: point_triple[0].unwrap(),
|
|
in_handle: point_triple[1],
|
|
out_handle: point_triple[2],
|
|
})
|
|
.collect();
|
|
WasmSubpath(Subpath::new(manipulator_groups, closed))
|
|
}
|
|
|
|
pub fn set_anchor(&mut self, index: usize, x: f64, y: f64) {
|
|
self.0[index].anchor = DVec2::new(x, y);
|
|
}
|
|
|
|
pub fn set_in_handle(&mut self, index: usize, x: f64, y: f64) {
|
|
self.0[index].in_handle = Some(DVec2::new(x, y));
|
|
}
|
|
|
|
pub fn set_out_handle(&mut self, index: usize, x: f64, y: f64) {
|
|
self.0[index].out_handle = Some(DVec2::new(x, y));
|
|
}
|
|
|
|
pub fn to_svg(&self) -> String {
|
|
format!("{}{}{}", SVG_OPEN_TAG, self.to_default_svg(), SVG_CLOSE_TAG)
|
|
}
|
|
|
|
fn to_default_svg(&self) -> String {
|
|
let mut subpath_svg = String::new();
|
|
self.0.to_svg(
|
|
&mut subpath_svg,
|
|
CURVE_ATTRIBUTES.to_string(),
|
|
ANCHOR_ATTRIBUTES.to_string(),
|
|
HANDLE_ATTRIBUTES.to_string(),
|
|
HANDLE_LINE_ATTRIBUTES.to_string(),
|
|
);
|
|
subpath_svg
|
|
}
|
|
|
|
pub fn length(&self) -> String {
|
|
let length_text = draw_text(format!("Length: {:.2}", self.0.length(None)), 5., 193., BLACK);
|
|
format!("{}{}{}{}", SVG_OPEN_TAG, self.to_default_svg(), length_text, SVG_CLOSE_TAG)
|
|
}
|
|
}
|