Relative & compound path

This commit is contained in:
Chris Tsang
2020-11-09 00:37:40 +08:00
parent 99cb79895b
commit 5f18837b61
7 changed files with 59 additions and 54 deletions

View File

@@ -22,7 +22,7 @@ console_log = { version = "0.2", features = ["color"] }
wasm-bindgen = { version = "0.2", features = ["serde-serialize"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
visioncortex = "0.2.0"
visioncortex = "0.3.0"
# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires

View File

@@ -1,5 +1,5 @@
use wasm_bindgen::prelude::*;
use visioncortex::{clusters::Clusters, Color, ColorName, PointI32, PathSimplifyMode};
use visioncortex::{clusters::Clusters, Color, ColorName, PathSimplifyMode};
use crate::{canvas::*};
use crate::svg::*;
@@ -69,7 +69,7 @@ impl BinaryImageConverter {
self.canvas.log(&format!("tick {}", self.counter));
let cluster = self.clusters.get_cluster(self.counter);
if cluster.size() >= self.params.filter_speckle {
let svg_path = cluster.to_svg_path(
let paths = cluster.to_compound_path(
self.mode,
self.params.corner_threshold,
self.params.length_threshold,
@@ -77,9 +77,8 @@ impl BinaryImageConverter {
self.params.splice_threshold
);
let color = Color::color(&ColorName::White);
self.svg.prepend_path_with_fill(
&svg_path,
&PointI32::default(),
self.svg.prepend_path(
&paths,
&color,
);
}

View File

@@ -1,5 +1,5 @@
use wasm_bindgen::prelude::*;
use visioncortex::{PathSimplifyMode, PointI32};
use visioncortex::PathSimplifyMode;
use visioncortex::color_clusters::{IncrementalBuilder, Clusters, Runner, RunnerConfig};
use crate::canvas::*;
@@ -94,16 +94,15 @@ impl ColorImageConverter {
if self.counter < view.clusters_output.len() {
self.canvas.log("Vectorize tick");
let cluster = view.get_cluster(view.clusters_output[self.counter]);
let svg_path = cluster.to_svg_path(
let paths = cluster.to_compound_path(
&view, false, self.mode,
self.params.corner_threshold,
self.params.length_threshold,
self.params.max_iterations,
self.params.splice_threshold
);
self.svg.prepend_path_with_fill(
&svg_path,
&PointI32::new(0, 0),
self.svg.prepend_path(
&paths,
&cluster.residue_color(),
);
self.counter += 1;

View File

@@ -1,5 +1,5 @@
use web_sys::Element;
use visioncortex::{Color, PointI32};
use visioncortex::{Color, CompoundPath, PointF64};
use super::common::document;
pub struct Svg {
@@ -13,11 +13,12 @@ impl Svg {
Self { element }
}
pub fn prepend_path_with_fill(&mut self, path_string: &str, offset: &PointI32, color: &Color) {
pub fn prepend_path(&mut self, paths: &CompoundPath, color: &Color) {
let path = document()
.create_element_ns(Some("http://www.w3.org/2000/svg"), "path")
.unwrap();
path.set_attribute("d", path_string).unwrap();
let (string, offset) = paths.to_svg_string(true, PointF64::default());
path.set_attribute("d", &string).unwrap();
path.set_attribute(
"transform",
format!("translate({},{})", offset.x, offset.y).as_str(),