upgrade webapp

This commit is contained in:
Chris Tsang
2026-07-25 17:21:55 +01:00
parent df94675494
commit 84ba49f0b9
5 changed files with 3988 additions and 4891 deletions
+38 -5
View File
@@ -1,6 +1,9 @@
use wasm_bindgen::prelude::*;
use visioncortex::{Color, ColorImage, PathSimplifyMode};
use visioncortex::color_clusters::{Clusters, Runner, RunnerConfig, HIERARCHICAL_MAX, IncrementalBuilder, KeyingAction};
use visioncortex::color_clusters::{
Cluster, Clusters, ClustersView, IncrementalBuilder, KeyingAction, NeighbourInfo, Runner,
RunnerConfig, HIERARCHICAL_MAX,
};
use crate::canvas::*;
use crate::svg::*;
@@ -38,11 +41,41 @@ pub struct ColorImageConverter {
pub enum Stage {
New,
Clustering(IncrementalBuilder),
Reclustering(IncrementalBuilder),
Clustering(Box<dyn ClusterBuilder>),
Reclustering(Box<dyn ClusterBuilder>),
Vectorize(Clusters),
}
/// visioncortex 0.9 parameterises `IncrementalBuilder` over its four closures,
/// and `Runner::start()` returns them as anonymous `impl Fn` types, so the
/// builder can no longer be named in a field. The stage machine only ever steps
/// it, so erase the closures behind a trait object.
pub trait ClusterBuilder {
fn tick(&mut self) -> bool;
fn progress(&self) -> u32;
fn result(&mut self) -> Clusters;
}
impl<C, D, P, H> ClusterBuilder for IncrementalBuilder<C, D, P, H>
where
C: Fn(Color, Color) -> bool,
D: Fn(Color, Color) -> i32,
P: Fn(&ClustersView, &Cluster, &[NeighbourInfo]) -> bool,
H: Fn(&ClustersView, &Cluster, &[NeighbourInfo]) -> bool,
{
fn tick(&mut self) -> bool {
IncrementalBuilder::tick(self)
}
fn progress(&self) -> u32 {
IncrementalBuilder::progress(self)
}
fn result(&mut self) -> Clusters {
IncrementalBuilder::result(self)
}
}
impl ColorImageConverter {
pub fn new(params: ColorImageConverterParams) -> Self {
let canvas = Canvas::new_from_id(&params.canvas_id);
@@ -106,7 +139,7 @@ impl ColorImageConverter {
KeyingAction::Discard
},
}, image);
self.stage = Stage::Clustering(runner.start());
self.stage = Stage::Clustering(Box::new(runner.start()));
}
pub fn tick(&mut self) -> bool {
@@ -138,7 +171,7 @@ impl ColorImageConverter {
key_color: Default::default(),
keying_action: KeyingAction::Discard,
}, image);
self.stage = Stage::Reclustering(runner.start());
self.stage = Stage::Reclustering(Box::new(runner.start()));
},
_ => panic!("unknown hierarchical `{}`", self.params.hierarchical)
}