mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-25 06:18:12 +08:00
Bezier split and trim implementation (#680)
* Added split implementation and UI Co-authored-by: Thomas Cheng <Androxium@users.noreply.github.com> * Added bezier split impl * Adjust struct traits * Implement trim and adjust FE code to handle multiple sliders per feature Co-authored-by: Thomas Cheng <contact.chengthomas@gmail.com> Co-authored-by: Rob Nadal <robnadal44@gmail.com> * Fix edge case in trim and add rust doc comments * Stylistic changes per review * More stylistic changes per review * replaced last explicit color usages Co-authored-by: Robert Nadal <Robnadal44@gmail.com> Co-authored-by: Thomas Cheng <Androxium@users.noreply.github.com> Co-authored-by: Thomas Cheng <contact.chengthomas@gmail.com>
This commit is contained in:
committed by
Keavon Chambers
co-authored by
Thomas Cheng
Thomas Cheng
Rob Nadal
parent
7f15cac5e2
commit
4eaffd0e5a
@@ -9,11 +9,10 @@ struct Point {
|
||||
y: f64,
|
||||
}
|
||||
|
||||
/// Wrapper of the `Bezier` struct to be used in JS.
|
||||
#[wasm_bindgen]
|
||||
/// Wrapper of the `Bezier` struct to be used in JS
|
||||
pub struct WasmBezier {
|
||||
internal: Bezier,
|
||||
}
|
||||
#[derive(Clone)]
|
||||
pub struct WasmBezier(Bezier);
|
||||
|
||||
/// Convert a `DVec2` into a `JsValue`
|
||||
pub fn vec_to_point(p: &DVec2) -> JsValue {
|
||||
@@ -25,60 +24,68 @@ impl WasmBezier {
|
||||
/// Expect js_points to be a list of 3 pairs
|
||||
pub fn new_quad(js_points: &JsValue) -> WasmBezier {
|
||||
let points: [DVec2; 3] = js_points.into_serde().unwrap();
|
||||
WasmBezier {
|
||||
internal: Bezier::from_quadratic_dvec2(points[0], points[1], points[2]),
|
||||
}
|
||||
WasmBezier(Bezier::from_quadratic_dvec2(points[0], points[1], points[2]))
|
||||
}
|
||||
|
||||
/// Expect js_points to be a list of 4 pairs
|
||||
pub fn new_cubic(js_points: &JsValue) -> WasmBezier {
|
||||
let points: [DVec2; 4] = js_points.into_serde().unwrap();
|
||||
WasmBezier {
|
||||
internal: Bezier::from_cubic_dvec2(points[0], points[1], points[2], points[3]),
|
||||
}
|
||||
WasmBezier(Bezier::from_cubic_dvec2(points[0], points[1], points[2], points[3]))
|
||||
}
|
||||
|
||||
pub fn set_start(&mut self, x: f64, y: f64) {
|
||||
self.internal.set_start(DVec2::from((x, y)));
|
||||
self.0.set_start(DVec2::new(x, y));
|
||||
}
|
||||
|
||||
pub fn set_end(&mut self, x: f64, y: f64) {
|
||||
self.internal.set_end(DVec2::from((x, y)));
|
||||
self.0.set_end(DVec2::new(x, y));
|
||||
}
|
||||
|
||||
pub fn set_handle_start(&mut self, x: f64, y: f64) {
|
||||
self.internal.set_handle_start(DVec2::from((x, y)));
|
||||
self.0.set_handle_start(DVec2::new(x, y));
|
||||
}
|
||||
|
||||
pub fn set_handle_end(&mut self, x: f64, y: f64) {
|
||||
self.internal.set_handle_end(DVec2::from((x, y)));
|
||||
self.0.set_handle_end(DVec2::new(x, y));
|
||||
}
|
||||
|
||||
pub fn get_points(&self) -> Vec<JsValue> {
|
||||
self.internal.get_points().iter().flatten().map(vec_to_point).collect()
|
||||
self.0.get_points().iter().flatten().map(vec_to_point).collect()
|
||||
}
|
||||
|
||||
pub fn to_svg(&self) -> String {
|
||||
self.internal.to_svg()
|
||||
self.0.to_svg()
|
||||
}
|
||||
|
||||
pub fn length(&self) -> f64 {
|
||||
self.internal.length()
|
||||
self.0.length()
|
||||
}
|
||||
|
||||
pub fn compute(&self, t: f64) -> JsValue {
|
||||
vec_to_point(&self.internal.compute(t))
|
||||
vec_to_point(&self.0.compute(t))
|
||||
}
|
||||
|
||||
pub fn compute_lookup_table(&self, steps: i32) -> Vec<JsValue> {
|
||||
self.internal.compute_lookup_table(Some(steps)).iter().map(vec_to_point).collect()
|
||||
self.0.compute_lookup_table(Some(steps)).iter().map(vec_to_point).collect()
|
||||
}
|
||||
|
||||
pub fn derivative(&self, t: f64) -> JsValue {
|
||||
vec_to_point(&self.internal.derivative(t))
|
||||
vec_to_point(&self.0.derivative(t))
|
||||
}
|
||||
|
||||
pub fn normal(&self, t: f64) -> JsValue {
|
||||
vec_to_point(&self.internal.normal(t))
|
||||
vec_to_point(&self.0.normal(t))
|
||||
}
|
||||
|
||||
pub fn split(&self, t: f64) -> JsValue {
|
||||
let bezier_points: [Vec<Point>; 2] = self
|
||||
.0
|
||||
.split(t)
|
||||
.map(|bezier| bezier.get_points().iter().flatten().map(|point| Point { x: point.x, y: point.y }).collect());
|
||||
JsValue::from_serde(&serde_json::to_string(&bezier_points).unwrap()).unwrap()
|
||||
}
|
||||
|
||||
pub fn trim(&self, t1: f64, t2: f64) -> WasmBezier {
|
||||
WasmBezier(self.0.trim(t1, t2))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user