Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-uk1 committed Apr 10, 2023
1 parent b4b33b7 commit 071ccbd
Show file tree
Hide file tree
Showing 4 changed files with 672 additions and 12 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ features = ["derive"]
# This is used for research but not really needed; maybe refactor.
[dev-dependencies]
rand = "0.8.0"
snog = { git = "https://github.com/derekdreery/snog" }

[target.'cfg(target_arch="wasm32")'.dev-dependencies]
getrandom = { version = "0.2.0", features = ["js"] }

[patch.crates-io]
kurbo = { path = "." }
69 changes: 69 additions & 0 deletions examples/svg_path.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use kurbo::{
paths::svg::{self, OneVec},
Affine, Line, Point, Size,
};
use snog::{
peniko::{Color, Stroke},
App, RenderCtx,
};

fn main() {
let data = Data::new();
App::new_with_data(data).with_render(render).run()
}

// TODO both the lifetime of the RenderCtx and the ref to user data could be the same - nothing is
// gained by having one longer than the other.
fn render(data: &mut Data, mut ctx: RenderCtx<'_>) {
let Size { width, height } = ctx.screen().size();

let stroke = Stroke::new(0.005);
let scale = Affine::scale_non_uniform(width, height);
let brush = Color::WHITE;
ctx.stroke(&stroke, scale, &brush, None, &data.path);
}

struct Data {
path: svg::Path,
}

impl Data {
fn new() -> Self {
let path = svg::Path::try_from(vec![
svg::PathEl::MoveTo(OneVec::single(Point::new(0.1, 0.1))),
svg::PathEl::Bearing(std::f64::consts::FRAC_PI_2),
svg::PathEl::LineToRel(
OneVec::try_from(vec![Point::new(0.2, 0.0), Point::new(-0.1, -0.2)]).unwrap(),
),
svg::PathEl::VertRel(OneVec::single(-0.2)),
svg::PathEl::CubicToRel(OneVec::single(svg::CubicTo {
to: Point::new(0.2, 0.0),
ctrl1: Point::new(0.1, 0.1),
ctrl2: Point::new(0.1, -0.1),
})),
svg::PathEl::SmoothCubicToRel(
OneVec::try_from(vec![
svg::SmoothCubicTo {
to: Point::new(0.2, 0.0),
ctrl2: Point::new(0.1, -0.1),
},
svg::SmoothCubicTo {
to: Point::new(0.2, 0.0),
ctrl2: Point::new(0.1, -0.1),
},
])
.unwrap(),
),
svg::PathEl::QuadToRel(OneVec::single(svg::QuadTo {
to: Point::new(0.0, 0.1),
ctrl: Point::new(0.1, 0.1),
})),
svg::PathEl::SmoothQuadToRel(
OneVec::try_from(vec![Point::new(0.0, 0.1), Point::new(0.0, 0.1)]).unwrap(),
),
])
.unwrap();

Data { path }
}
}
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ mod rounded_rect_radii;
mod shape;
pub mod simplify;
mod size;
#[cfg(feature = "std")]
mod svg;
//#[cfg(feature = "std")]
//mod svg;
mod translate_scale;
mod vec2;

Expand All @@ -131,7 +131,7 @@ pub use crate::rounded_rect::*;
pub use crate::rounded_rect_radii::*;
pub use crate::shape::*;
pub use crate::size::*;
#[cfg(feature = "std")]
pub use crate::svg::*;
//#[cfg(feature = "std")]
//pub use crate::svg::*;
pub use crate::translate_scale::*;
pub use crate::vec2::*;
Loading

0 comments on commit 071ccbd

Please sign in to comment.