Skip to content

Commit

Permalink
✨ Add remove_objects_in
Browse files Browse the repository at this point in the history
  • Loading branch information
ewen-lbh committed May 4, 2024
1 parent 7310c35 commit 8f7ada8
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 10 deletions.
6 changes: 6 additions & 0 deletions src/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,12 @@ impl Canvas {
return self.height() as f32 / self.width() as f32;
}

pub fn remove_all_objects_in(&mut self, region: &Region) {
self.layers
.iter_mut()
.for_each(|layer| layer.remove_all_objects_in(region));
}

/// returns a list of all unique filters used throughout the canvas
/// used to only generate one definition per filter
///
Expand Down
16 changes: 16 additions & 0 deletions src/examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@ use crate::*;

pub fn dna_analysis_machine() -> Canvas {
let mut canvas = Canvas::new(vec!["root"]);

canvas.colormap = ColorMapping {
black: "#000000".into(),
white: "#ffffff".into(),
red: "#cf0a2b".into(),
green: "#22e753".into(),
blue: "#2734e6".into(),
yellow: "#f8e21e".into(),
orange: "#f05811".into(),
purple: "#6a24ec".into(),
brown: "#a05634".into(),
pink: "#e92e76".into(),
gray: "#81a0a8".into(),
cyan: "#4fecec".into(),
};

canvas.set_grid_size(16, 9);
canvas.set_background(Color::Black);
let mut hatches_layer = Layer::new("root");
Expand Down
8 changes: 7 additions & 1 deletion src/layer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::{ColorMapping, ColoredObject, Fill, Filter, Object, ObjectSizes};
use crate::{ColorMapping, ColoredObject, Containable, Fill, Filter, Object, ObjectSizes, Region};
use anyhow::Context;
use std::collections::HashMap;
use wasm_bindgen::prelude::*;
use web_sys::js_sys::RegExp;

#[derive(Debug, Clone, Default)]
// #[wasm_bindgen(getter_with_clone)]
Expand Down Expand Up @@ -36,6 +37,11 @@ impl Layer {
self.flush();
}

pub fn remove_all_objects_in(&mut self, region: &Region) {
self.objects
.retain(|_, ColoredObject(o, ..)| !o.region().within(region))
}

pub fn paint_all_objects(&mut self, fill: Fill) {
for (_id, ColoredObject(_, ref mut maybe_fill, _)) in &mut self.objects {
*maybe_fill = Some(fill.clone());
Expand Down
13 changes: 7 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ pub fn run(args: cli::Args) {
let mut canvas = canvas_from_cli(&args);

if args.cmd_image && !args.cmd_video {
canvas.root().add_object(
"hello",
Object::Text(Anchor(3, 4), "hello world!".into(), 16.0)
.color(Fill::Solid(Color::Black)),
);
canvas.set_background(Color::White);
// canvas.root().add_object(
// "hello",
// Object::Text(Anchor(3, 4), "hello world!".into(), 16.0)
// .color(Fill::Solid(Color::Black)),
// );
// canvas.set_background(Color::White);
canvas = examples::dna_analysis_machine();
let rendered = canvas.render(&vec!["*"], true);
if args.arg_file.ends_with(".svg") {
std::fs::write(args.arg_file, rendered).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ impl Object {
Object::Text(anchor, _, _) | Object::Dot(anchor) | Object::SmallCircle(anchor) => {
(anchor, anchor).into()
}
Object::BigCircle(center) => (center, center).into(), // FIXME will be wrong lmao,
Object::BigCircle(center) => Region::from((center, center)),
Object::RawSVG(_) => {
unimplemented!()
}
Expand Down
9 changes: 8 additions & 1 deletion src/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ impl Point {
pub fn translated(&self, dx: i32, dy: i32) -> Self {
Self((self.0 as i32 + dx) as usize, (self.1 as i32 + dy) as usize)
}

pub fn region(&self) -> Region {
Region {
start: self.clone(),
end: self.clone(),
}
}
}

impl From<(usize, usize)> for Point {
Expand Down Expand Up @@ -129,7 +136,7 @@ impl From<(&CenterAnchor, &CenterAnchor)> for Region {
fn from(value: (&CenterAnchor, &CenterAnchor)) -> Self {
Region {
start: (value.0 .0, value.0 .1).into(),
end: (value.1 .0, value.1 .1).into(),
end: (value.1 .0 - 1, value.1 .1 - 1).into(),
}
}
}
Expand Down
10 changes: 9 additions & 1 deletion src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use wasm_bindgen::{JsValue, UnwrapThrowExt};

use crate::{
examples, layer, Anchor, Canvas, CenterAnchor, Color, ColorMapping, Fill, Filter, FilterType,
HatchDirection, Layer, Object, Point,
HatchDirection, Layer, Object, Point, Region,
};

static WEB_CANVAS: Lazy<Mutex<Canvas>> = Lazy::new(|| Mutex::new(Canvas::default_settings()));
Expand Down Expand Up @@ -59,12 +59,20 @@ pub fn render_image(opacity: f32, color: Color) -> Result<(), JsValue> {
cyan: "#4fecec".into(),
};

canvas.remove_all_objects_in(&Region::from_topleft(Point(8, 2), (2, 2)));
canvas.remove_all_objects_in(&Point(11, 7).region());

*WEB_CANVAS.lock().unwrap() = canvas;
render_canvas_at(String::from("body"));

Ok(())
}

#[wasm_bindgen]
pub fn map_to_midi_controller() {

}

#[wasm_bindgen]
pub fn render_canvas_into(selector: String) -> () {
let svgstring = canvas().render(&vec!["*"], false);
Expand Down

0 comments on commit 8f7ada8

Please sign in to comment.