Skip to content

Commit

Permalink
✨ Implement Fill::Dotted
Browse files Browse the repository at this point in the history
  • Loading branch information
gwennlbh committed May 11, 2024
1 parent dd57a14 commit 1d8012d
Show file tree
Hide file tree
Showing 11 changed files with 643 additions and 478 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ nanoid = "0.4.0"
console = { version = "0.15.8", features = ["windows-console-colors"] }
backtrace = "0.3.71"
slug = "0.1.5"
roxmltree = "0.19.0"


[dev-dependencies]
Expand Down
4 changes: 3 additions & 1 deletion Justfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export RUST_BACKTRACE := "1"

build:
cargo build --bin shapemaker
cp target/debug/shapemaker .
Expand All @@ -18,4 +20,4 @@ example-video out="out.mp4" args='':
./shapemaker video --colors colorschemes/palenight.css {{out}} --sync-with fixtures/schedule-hell.midi --audio fixtures/schedule-hell.flac --grid-size 16x10 --resolution 1920 {{args}}

example-image out="out.png" args='':
./shapemaker image --colors colorschemes/snazzy-light.json --resolution 3000 {{out}} {{args}}
./shapemaker image --colors colorschemes/palenight.css --resolution 3000 {{out}} {{args}}
970 changes: 566 additions & 404 deletions out.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 16 additions & 18 deletions src/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,23 +393,21 @@ impl Canvas {

pub fn random_fill(&self, hatchable: bool) -> Fill {
if hatchable {
match rand::thread_rng().gen_range(1..=2) {
1 => Fill::Solid(random_color(self.background)),
2 => {
let hatch_size = rand::thread_rng().gen_range(5..=100) as f32 * 1e-2;
Fill::Hatched(
random_color(self.background),
Angle(rand::thread_rng().gen_range(0.0..360.0)),
hatch_size,
// under a certain hatch size, we can't see the hatching if the ratio is not ½
if hatch_size < 8.0 {
0.5
} else {
rand::thread_rng().gen_range(1..=4) as f32 / 4.0
},
)
}
_ => unreachable!(),
if rand::thread_rng().gen_bool(0.75) {
Fill::Solid(random_color(self.background))
} else {
let hatch_size = rand::thread_rng().gen_range(5..=100) as f32 * 1e-2;
Fill::Hatched(
random_color(self.background),
Angle(rand::thread_rng().gen_range(0.0..360.0)),
hatch_size,
// under a certain hatch size, we can't see the hatching if the ratio is not ½
if hatch_size < 8.0 {
0.5
} else {
rand::thread_rng().gen_range(1..=4) as f32 / 4.0
},
)
}
} else {
Fill::Solid(random_color(self.background))
Expand Down Expand Up @@ -488,7 +486,7 @@ impl Canvas {
self.layers
.iter()
.flat_map(|layer| layer.objects.iter().flat_map(|(_, o)| o.fill))
.filter(|fill| matches!(fill, Fill::Hatched(..)))
.filter(|fill| matches!(fill, Fill::Hatched(..) | Fill::Dotted(..)))
.unique_by(|fill| fill.pattern_id())
.collect()
}
Expand Down
21 changes: 14 additions & 7 deletions src/examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,24 @@ pub fn dna_analysis_machine() -> Canvas {

hatches_layer.add_object(
point,
if rand::thread_rng().gen_bool(0.5) {
if rand::thread_rng().gen_bool(0.5) || point == red_circle_at {
Object::BigCircle(point)
} else {
Object::Rectangle(point, point)
}
.color(Fill::Hatched(
Color::White,
Angle(rand::thread_rng().gen_range(0.0..360.0)),
(i + 5) as f32 / 10.0,
0.25,
)),
.color(
// Fill::Dotted(Color::White, (i + 8) as f32 / 10.0, (i + 3) as f32 / 10.0),
if point == red_circle_at {
Fill::Dotted(Color::White, 3.0, 2.0)
} else {
Fill::Hatched(
Color::White,
Angle(rand::thread_rng().gen_range(0.0..360.0)),
(i + 5) as f32 / 10.0,
0.25,
)
},
),
);
}

Expand Down
28 changes: 24 additions & 4 deletions src/fill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub enum Fill {
Solid(Color),
Translucent(Color, f32),
Hatched(Color, Angle, f32, f32),
Dotted(Color, f32),
Dotted(Color, f32, f32),
}

// Operations that can be applied on fills.
Expand Down Expand Up @@ -86,8 +86,7 @@ impl RenderCSS for Fill {
Fill::Translucent(color, opacity) => {
format!("fill: {}; opacity: {};", color.render(colormap), opacity)
}
Fill::Dotted(..) => unimplemented!(),
Fill::Hatched(..) => {
Fill::Dotted(..) | Fill::Hatched(..) => {
format!("fill: url(#{});", self.pattern_id())
}
}
Expand Down Expand Up @@ -115,13 +114,16 @@ impl Fill {
pub fn pattern_id(&self) -> String {
if let Fill::Hatched(color, angle, thickness, spacing) = self {
return format!(
"pattern-{}-{}-{}-{}",
"pattern-hatched-{}-{}-{}-{}",
angle,
color.name(),
thickness,
spacing
);
}
if let Fill::Dotted(color, diameter, spacing) = self {
return format!("pattern-dotted-{}-{}-{}", color.name(), diameter, spacing);
}
String::from("")
}

Expand Down Expand Up @@ -171,6 +173,24 @@ impl Fill {

Some(pattern)
}
Fill::Dotted(color, diameter, spacing) => {
let box_size = diameter + 2.0 * spacing;
let pattern = svg::node::element::Pattern::new()
.set("id", self.pattern_id())
.set("patternUnits", "userSpaceOnUse")
.set("height", box_size)
.set("width", box_size)
.set("viewBox", format!("0,0,{},{}", box_size, box_size))
.add(
svg::node::element::Circle::new()
.set("cx", box_size / 2.0)
.set("cy", box_size / 2.0)
.set("r", diameter / 2.0)
.set("fill", color.render(colormapping)),
);

Some(pattern)
}
_ => None,
}
}
Expand Down
25 changes: 1 addition & 24 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,8 @@ pub fn main() -> Result<()> {
pub fn run(args: cli::Args) -> Result<()> {
let mut canvas = canvas_from_cli(&args);

let qrname = env::var("QRCODE_NAME").unwrap();

if args.cmd_image && !args.cmd_video {
canvas.set_grid_size(3, 3);
canvas.add_or_replace_layer(canvas.random_layer("root"));
canvas.new_layer("qr");
let qrcode = Object::Image(
vec![
canvas.world_region.topleft(),
canvas.world_region.topright(),
canvas.world_region.bottomright(),
canvas.world_region.bottomleft(),
][rand::thread_rng().gen_range(0..4)]
.region(),
format!("./{qrname}-qr.png"),
);
canvas.root().remove_all_objects_in(&qrcode.region());
canvas.set_background(Color::White);
canvas.add_object("qr", "qr", qrcode, None).unwrap();
canvas.put_layer_on_top("qr");
canvas.root().objects.values_mut().for_each(|o| {
if !o.object.fillable() {
o.fill = Some(Fill::Solid(Color::Black));
}
});
canvas = examples::title();

let rendered = canvas.render(true)?;
if args.arg_file.ends_with(".svg") {
Expand Down
13 changes: 11 additions & 2 deletions src/objects.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::{cell, collections::HashMap};

use crate::{ColorMapping, Fill, Filter, Point, Region, Transformation};
use itertools::Itertools;
use wasm_bindgen::prelude::*;
Expand Down Expand Up @@ -36,6 +34,10 @@ impl Object {
pub fn filter(self, filter: Filter) -> ColoredObject {
ColoredObject::from((self, None)).filter(filter)
}

pub fn transform(self, transformation: Transformation) -> ColoredObject {
ColoredObject::from((self, None)).transform(transformation)
}
}

#[derive(Debug, Clone)]
Expand All @@ -52,6 +54,11 @@ impl ColoredObject {
self
}

pub fn transform(mut self, transformation: Transformation) -> Self {
self.transformations.push(transformation);
self
}

pub fn clear_filters(&mut self) {
self.filters.clear();
}
Expand All @@ -78,6 +85,8 @@ impl ColoredObject {
css = self.fill.render_css(colormap, !self.object.fillable());
}

css += "transform-box: fill-box;";

css += self
.filters
.iter()
Expand Down
5 changes: 0 additions & 5 deletions src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ fn canvas() -> std::sync::MutexGuard<'static, Canvas> {
WEB_CANVAS.lock().unwrap()
}

#[wasm_bindgen(start)]
pub fn js_init() -> Result<(), JsValue> {
render_image(0.0, Color::Black)?;
Ok(())
}

// Can't bind Color.name directly, see https://github.com/rustwasm/wasm-bindgen/issues/1715
#[wasm_bindgen]
Expand Down
13 changes: 0 additions & 13 deletions street.fish

This file was deleted.

0 comments on commit 1d8012d

Please sign in to comment.