Bezier-rs: Convert to_svg used in subpaths to match style used in beziers (#854)

Converted subpath::to_svg to Bezier style
This commit is contained in:
Rob Nadal
2022-11-14 22:37:06 -08:00
committed by Keavon Chambers
parent 9c87658ae4
commit 00c8fa83c2
5 changed files with 65 additions and 145 deletions

View File

@@ -383,15 +383,8 @@ impl WasmBezier {
pub fn rotate(&self, angle: f64, pivot_x: f64, pivot_y: f64) -> String {
let original_bezier_svg = self.get_bezier_path();
let rotated_bezier = self.0.rotate_about_point(angle, DVec2::new(pivot_x, pivot_y));
let empty_string = String::new();
let mut rotated_bezier_svg = String::new();
rotated_bezier.to_svg(
&mut rotated_bezier_svg,
CURVE_ATTRIBUTES.to_string().replace(BLACK, RED),
empty_string.clone(),
empty_string.clone(),
empty_string,
);
rotated_bezier.to_svg(&mut rotated_bezier_svg, CURVE_ATTRIBUTES.to_string().replace(BLACK, RED), String::new(), String::new(), String::new());
let pivot = draw_circle(pivot_x, pivot_y, 3., GRAY, 1.5, WHITE);
// Line between pivot and start point on curve
@@ -433,15 +426,8 @@ impl WasmBezier {
let bezier_curve_svg = self.get_bezier_path();
let empty_string = String::new();
let mut line_svg = String::new();
line.to_svg(
&mut line_svg,
CURVE_ATTRIBUTES.to_string().replace(BLACK, RED),
empty_string.clone(),
empty_string.clone(),
empty_string,
);
line.to_svg(&mut line_svg, CURVE_ATTRIBUTES.to_string().replace(BLACK, RED), String::new(), String::new(), String::new());
let intersections_svg = self
.intersect(&line, None)
@@ -460,15 +446,8 @@ impl WasmBezier {
let bezier_curve_svg = self.get_bezier_path();
let empty_string = String::new();
let mut quadratic_svg = String::new();
quadratic.to_svg(
&mut quadratic_svg,
CURVE_ATTRIBUTES.to_string().replace(BLACK, RED),
empty_string.clone(),
empty_string.clone(),
empty_string,
);
quadratic.to_svg(&mut quadratic_svg, CURVE_ATTRIBUTES.to_string().replace(BLACK, RED), String::new(), String::new(), String::new());
let intersections_svg = self
.intersect(&quadratic, Some(error))
@@ -487,15 +466,8 @@ impl WasmBezier {
let bezier_curve_svg = self.get_bezier_path();
let empty_string = String::new();
let mut cubic_svg = String::new();
cubic.to_svg(
&mut cubic_svg,
CURVE_ATTRIBUTES.to_string().replace(BLACK, RED),
empty_string.clone(),
empty_string.clone(),
empty_string,
);
cubic.to_svg(&mut cubic_svg, CURVE_ATTRIBUTES.to_string().replace(BLACK, RED), String::new(), String::new(), String::new());
let intersections_svg = self
.intersect(&cubic, Some(error))
@@ -526,7 +498,6 @@ impl WasmBezier {
}
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
@@ -538,9 +509,9 @@ impl WasmBezier {
bezier_curve.to_svg(
&mut curve_svg,
CURVE_ATTRIBUTES.to_string().replace(BLACK, &format!("hsl({}, 100%, 50%)", (40 * index))),
empty_string.clone(),
empty_string.clone(),
empty_string.clone(),
String::new(),
String::new(),
String::new(),
);
curve_svg
})
@@ -549,7 +520,6 @@ impl WasmBezier {
}
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
@@ -561,9 +531,9 @@ impl WasmBezier {
bezier_curve.to_svg(
&mut curve_svg,
CURVE_ATTRIBUTES.to_string().replace(BLACK, &format!("hsl({}, 100%, 50%)", (40 * index))),
empty_string.clone(),
empty_string.clone(),
empty_string.clone(),
String::new(),
String::new(),
String::new(),
);
curve_svg
})

View File

@@ -1,4 +1,4 @@
use bezier_rs::{ManipulatorGroup, Subpath, ToSVGOptions};
use bezier_rs::{ManipulatorGroup, Subpath};
use glam::DVec2;
use wasm_bindgen::prelude::*;
@@ -37,11 +37,23 @@ impl WasmSubpath {
}
pub fn to_svg(&self) -> String {
format!("{}{}{}", SVG_OPEN_TAG, self.0.to_svg(ToSVGOptions::default()), SVG_CLOSE_TAG)
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.0.to_svg(ToSVGOptions::default()), length_text, SVG_CLOSE_TAG)
format!("{}{}{}{}", SVG_OPEN_TAG, self.to_default_svg(), length_text, SVG_CLOSE_TAG)
}
}