Convert reduce() and offset() Bezier-rs wasm demo functions to render as SVG (#783)

* convert reduce and offset wasm func to return svg

* address comments
This commit is contained in:
Thomas Cheng
2022-10-05 23:54:02 -04:00
committed by Keavon Chambers
parent 55f6d13daf
commit dccff784c5
3 changed files with 75 additions and 49 deletions
@@ -396,15 +396,50 @@ impl WasmBezier {
to_js_value(points)
}
pub fn reduce(&self) -> JsValue {
let bezier_points: Vec<Vec<Point>> = self.0.reduce(None).into_iter().map(bezier_to_points).collect();
to_js_value(bezier_points)
pub fn reduce(&self) -> String {
let empty_string = String::new();
let original_curve_svg = self.get_bezier_path();
let bezier_curves_svg: String = self
.0
.reduce(None)
.iter()
.enumerate()
.map(|(idx, bezier_curve)| {
let mut curve_svg = String::new();
bezier_curve.to_svg(
&mut curve_svg,
CURVE_ATTRIBUTES.to_string().replace(BLACK, &format!("hsl({}, 100%, 50%)", (40 * idx))),
empty_string.clone(),
empty_string.clone(),
empty_string.clone(),
);
curve_svg
})
.fold(original_curve_svg, |acc, item| format!("{acc}{item}"));
wrap_svg_tag(bezier_curves_svg)
}
/// The wrapped return type is `Vec<Vec<Point>>`.
pub fn offset(&self, distance: f64) -> JsValue {
let bezier_points: Vec<Vec<Point>> = self.0.offset(distance).into_iter().map(bezier_to_points).collect();
to_js_value(bezier_points)
pub fn offset(&self, distance: f64) -> String {
let empty_string = String::new();
let original_curve_svg = self.get_bezier_path();
let bezier_curves_svg = self
.0
.offset(distance)
.iter()
.enumerate()
.map(|(idx, bezier_curve)| {
let mut curve_svg = String::new();
bezier_curve.to_svg(
&mut curve_svg,
CURVE_ATTRIBUTES.to_string().replace(BLACK, &format!("hsl({}, 100%, 50%)", (40 * idx))),
empty_string.clone(),
empty_string.clone(),
empty_string.clone(),
);
curve_svg
})
.fold(original_curve_svg, |acc, item| format!("{acc}{item}"));
wrap_svg_tag(bezier_curves_svg)
}
/// The wrapped return type is `Vec<CircleSector>`.