Struct tiny_skia::PathBuilder
source · pub struct PathBuilder { /* private fields */ }
Expand description
A path builder.
Implementations§
source§impl PathBuilder
impl PathBuilder
sourcepub fn new() -> PathBuilder
pub fn new() -> PathBuilder
Creates a new builder.
Examples found in repository?
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
fn crate_triangle() -> Pixmap {
let mut paint = Paint::default();
paint.set_color_rgba8(50, 127, 150, 200);
paint.anti_alias = true;
let mut pb = PathBuilder::new();
pb.move_to(0.0, 20.0);
pb.line_to(20.0, 20.0);
pb.line_to(10.0, 0.0);
pb.close();
let path = pb.finish().unwrap();
let mut pixmap = Pixmap::new(20, 20).unwrap();
pixmap.fill_path(
&path,
&paint,
FillRule::Winding,
Transform::identity(),
None,
);
pixmap
}
More examples
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
fn main() {
let mut pb = PathBuilder::new();
pb.move_to(50.0, 100.0);
pb.cubic_to(130.0, 20.0, 390.0, 120.0, 450.0, 30.0);
let path = pb.finish().unwrap();
let mut paint = Paint::default();
paint.set_color_rgba8(50, 127, 150, 200);
paint.anti_alias = true;
let mut pixmap = Pixmap::new(500, 500).unwrap();
let mut transform = Transform::identity();
for i in 0..20 {
let mut stroke = Stroke::default();
stroke.width = 2.0 - (i as f32 / 10.0);
pixmap.stroke_path(&path, &paint, &stroke, transform, None);
transform = transform.pre_translate(0.0, 20.0);
}
pixmap.save_png("image.png").unwrap();
}
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
fn create_triangle() -> Pixmap {
let mut paint = Paint::default();
paint.set_color_rgba8(50, 127, 150, 200);
paint.anti_alias = true;
let mut pb = PathBuilder::new();
pb.move_to(0.0, 200.0);
pb.line_to(200.0, 200.0);
pb.line_to(100.0, 0.0);
pb.close();
let path = pb.finish().unwrap();
let mut pixmap = Pixmap::new(200, 200).unwrap();
pixmap.fill_path(
&path,
&paint,
FillRule::Winding,
Transform::identity(),
None,
);
let path = PathBuilder::from_rect(Rect::from_ltrb(0.0, 0.0, 200.0, 200.0).unwrap());
let stroke = Stroke::default();
paint.set_color_rgba8(200, 0, 0, 220);
pixmap.stroke_path(&path, &paint, &stroke, Transform::identity(), None); // TODO: stroke_rect
pixmap
}
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
fn main() {
let clip_path = {
let mut pb = PathBuilder::new();
pb.push_circle(250.0, 250.0, 200.0);
pb.push_circle(250.0, 250.0, 100.0);
pb.finish().unwrap()
};
let clip_path = clip_path
.transform(Transform::from_row(1.0, -0.3, 0.0, 1.0, 0.0, 75.0))
.unwrap();
let mut mask = Mask::new(500, 500).unwrap();
mask.fill_path(&clip_path, FillRule::EvenOdd, true, Transform::default());
let mut paint = Paint::default();
paint.anti_alias = false;
paint.set_color_rgba8(50, 127, 150, 200);
let mut pixmap = Pixmap::new(500, 500).unwrap();
pixmap.fill_rect(
Rect::from_xywh(0.0, 0.0, 500.0, 500.0).unwrap(),
&paint,
Transform::identity(),
Some(&mask),
);
pixmap.save_png("image.png").unwrap();
}
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
fn main() {
let mut paint = Paint::default();
paint.set_color_rgba8(0, 127, 0, 200);
paint.anti_alias = true;
let path = {
let mut pb = PathBuilder::new();
const RADIUS: f32 = 250.0;
const CENTER: f32 = 250.0;
pb.move_to(CENTER + RADIUS, CENTER);
for i in 1..8 {
let a = 2.6927937 * i as f32;
pb.line_to(CENTER + RADIUS * a.cos(), CENTER + RADIUS * a.sin());
}
pb.finish().unwrap()
};
let mut stroke = Stroke::default();
stroke.width = 6.0;
stroke.line_cap = LineCap::Round;
stroke.dash = StrokeDash::new(vec![20.0, 40.0], 0.0);
let mut pixmap = Pixmap::new(500, 500).unwrap();
pixmap.stroke_path(&path, &paint, &stroke, Transform::identity(), None);
pixmap.save_png("image.png").unwrap();
}
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
fn main() {
let mut paint = Paint::default();
paint.anti_alias = false;
paint.shader = LinearGradient::new(
Point::from_xy(100.0, 100.0),
Point::from_xy(900.0, 900.0),
vec![
GradientStop::new(0.0, Color::from_rgba8(50, 127, 150, 200)),
GradientStop::new(1.0, Color::from_rgba8(220, 140, 75, 180)),
],
SpreadMode::Pad,
Transform::identity(),
)
.unwrap();
let mut pb = PathBuilder::new();
pb.move_to(60.0, 60.0);
pb.line_to(160.0, 940.0);
pb.cubic_to(380.0, 840.0, 660.0, 800.0, 940.0, 800.0);
pb.cubic_to(740.0, 460.0, 440.0, 160.0, 60.0, 60.0);
pb.close();
let path = pb.finish().unwrap();
let mut pixmap = Pixmap::new(1000, 1000).unwrap();
pixmap.fill_path(
&path,
&paint,
FillRule::Winding,
Transform::identity(),
None,
);
pixmap.save_png("image.png").unwrap();
}
sourcepub fn with_capacity(
verbs_capacity: usize,
points_capacity: usize
) -> PathBuilder
pub fn with_capacity( verbs_capacity: usize, points_capacity: usize ) -> PathBuilder
Creates a new builder with a specified capacity.
Number of points depends on a verb type:
- Move - 1
- Line - 1
- Quad - 2
- Cubic - 3
- Close - 0
sourcepub fn from_rect(rect: Rect) -> Path
pub fn from_rect(rect: Rect) -> Path
Creates a new Path
from Rect
.
Never fails since Rect
is always valid.
Segments are created clockwise: TopLeft -> TopRight -> BottomRight -> BottomLeft
The contour is closed.
Examples found in repository?
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
fn create_triangle() -> Pixmap {
let mut paint = Paint::default();
paint.set_color_rgba8(50, 127, 150, 200);
paint.anti_alias = true;
let mut pb = PathBuilder::new();
pb.move_to(0.0, 200.0);
pb.line_to(200.0, 200.0);
pb.line_to(100.0, 0.0);
pb.close();
let path = pb.finish().unwrap();
let mut pixmap = Pixmap::new(200, 200).unwrap();
pixmap.fill_path(
&path,
&paint,
FillRule::Winding,
Transform::identity(),
None,
);
let path = PathBuilder::from_rect(Rect::from_ltrb(0.0, 0.0, 200.0, 200.0).unwrap());
let stroke = Stroke::default();
paint.set_color_rgba8(200, 0, 0, 220);
pixmap.stroke_path(&path, &paint, &stroke, Transform::identity(), None); // TODO: stroke_rect
pixmap
}
sourcepub fn from_circle(cx: f32, cy: f32, radius: f32) -> Option<Path>
pub fn from_circle(cx: f32, cy: f32, radius: f32) -> Option<Path>
Creates a new Path
from a circle.
See PathBuilder::push_circle
for details.
Examples found in repository?
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
fn main() {
let triangle = crate_triangle();
let mut paint = Paint::default();
paint.anti_alias = true;
paint.shader = Pattern::new(
triangle.as_ref(),
SpreadMode::Repeat,
FilterQuality::Bicubic,
1.0,
Transform::from_row(1.5, -0.4, 0.0, -0.8, 5.0, 1.0),
);
let path = PathBuilder::from_circle(200.0, 200.0, 180.0).unwrap();
let mut pixmap = Pixmap::new(400, 400).unwrap();
pixmap.fill_path(
&path,
&paint,
FillRule::Winding,
Transform::identity(),
None,
);
pixmap.save_png("image.png").unwrap();
}
sourcepub fn from_oval(oval: Rect) -> Option<Path>
pub fn from_oval(oval: Rect) -> Option<Path>
Creates a new Path
from an oval.
See PathBuilder::push_oval
for details.
sourcepub fn move_to(&mut self, x: f32, y: f32)
pub fn move_to(&mut self, x: f32, y: f32)
Adds beginning of a contour.
Multiple continuous MoveTo segments are not allowed. If the previous segment was also MoveTo, it will be overwritten with the current one.
Examples found in repository?
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
fn crate_triangle() -> Pixmap {
let mut paint = Paint::default();
paint.set_color_rgba8(50, 127, 150, 200);
paint.anti_alias = true;
let mut pb = PathBuilder::new();
pb.move_to(0.0, 20.0);
pb.line_to(20.0, 20.0);
pb.line_to(10.0, 0.0);
pb.close();
let path = pb.finish().unwrap();
let mut pixmap = Pixmap::new(20, 20).unwrap();
pixmap.fill_path(
&path,
&paint,
FillRule::Winding,
Transform::identity(),
None,
);
pixmap
}
More examples
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
fn main() {
let mut pb = PathBuilder::new();
pb.move_to(50.0, 100.0);
pb.cubic_to(130.0, 20.0, 390.0, 120.0, 450.0, 30.0);
let path = pb.finish().unwrap();
let mut paint = Paint::default();
paint.set_color_rgba8(50, 127, 150, 200);
paint.anti_alias = true;
let mut pixmap = Pixmap::new(500, 500).unwrap();
let mut transform = Transform::identity();
for i in 0..20 {
let mut stroke = Stroke::default();
stroke.width = 2.0 - (i as f32 / 10.0);
pixmap.stroke_path(&path, &paint, &stroke, transform, None);
transform = transform.pre_translate(0.0, 20.0);
}
pixmap.save_png("image.png").unwrap();
}
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
fn create_triangle() -> Pixmap {
let mut paint = Paint::default();
paint.set_color_rgba8(50, 127, 150, 200);
paint.anti_alias = true;
let mut pb = PathBuilder::new();
pb.move_to(0.0, 200.0);
pb.line_to(200.0, 200.0);
pb.line_to(100.0, 0.0);
pb.close();
let path = pb.finish().unwrap();
let mut pixmap = Pixmap::new(200, 200).unwrap();
pixmap.fill_path(
&path,
&paint,
FillRule::Winding,
Transform::identity(),
None,
);
let path = PathBuilder::from_rect(Rect::from_ltrb(0.0, 0.0, 200.0, 200.0).unwrap());
let stroke = Stroke::default();
paint.set_color_rgba8(200, 0, 0, 220);
pixmap.stroke_path(&path, &paint, &stroke, Transform::identity(), None); // TODO: stroke_rect
pixmap
}
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
fn main() {
let mut paint = Paint::default();
paint.set_color_rgba8(0, 127, 0, 200);
paint.anti_alias = true;
let path = {
let mut pb = PathBuilder::new();
const RADIUS: f32 = 250.0;
const CENTER: f32 = 250.0;
pb.move_to(CENTER + RADIUS, CENTER);
for i in 1..8 {
let a = 2.6927937 * i as f32;
pb.line_to(CENTER + RADIUS * a.cos(), CENTER + RADIUS * a.sin());
}
pb.finish().unwrap()
};
let mut stroke = Stroke::default();
stroke.width = 6.0;
stroke.line_cap = LineCap::Round;
stroke.dash = StrokeDash::new(vec![20.0, 40.0], 0.0);
let mut pixmap = Pixmap::new(500, 500).unwrap();
pixmap.stroke_path(&path, &paint, &stroke, Transform::identity(), None);
pixmap.save_png("image.png").unwrap();
}
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
fn main() {
let mut paint = Paint::default();
paint.anti_alias = false;
paint.shader = LinearGradient::new(
Point::from_xy(100.0, 100.0),
Point::from_xy(900.0, 900.0),
vec![
GradientStop::new(0.0, Color::from_rgba8(50, 127, 150, 200)),
GradientStop::new(1.0, Color::from_rgba8(220, 140, 75, 180)),
],
SpreadMode::Pad,
Transform::identity(),
)
.unwrap();
let mut pb = PathBuilder::new();
pb.move_to(60.0, 60.0);
pb.line_to(160.0, 940.0);
pb.cubic_to(380.0, 840.0, 660.0, 800.0, 940.0, 800.0);
pb.cubic_to(740.0, 460.0, 440.0, 160.0, 60.0, 60.0);
pb.close();
let path = pb.finish().unwrap();
let mut pixmap = Pixmap::new(1000, 1000).unwrap();
pixmap.fill_path(
&path,
&paint,
FillRule::Winding,
Transform::identity(),
None,
);
pixmap.save_png("image.png").unwrap();
}
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
fn main() {
let mut paint1 = Paint::default();
paint1.set_color_rgba8(50, 127, 150, 200);
paint1.anti_alias = true;
let mut paint2 = Paint::default();
paint2.set_color_rgba8(220, 140, 75, 180);
paint2.anti_alias = false;
let path1 = {
let mut pb = PathBuilder::new();
pb.move_to(60.0, 60.0);
pb.line_to(160.0, 940.0);
pb.cubic_to(380.0, 840.0, 660.0, 800.0, 940.0, 800.0);
pb.cubic_to(740.0, 460.0, 440.0, 160.0, 60.0, 60.0);
pb.close();
pb.finish().unwrap()
};
let path2 = {
let mut pb = PathBuilder::new();
pb.move_to(940.0, 60.0);
pb.line_to(840.0, 940.0);
pb.cubic_to(620.0, 840.0, 340.0, 800.0, 60.0, 800.0);
pb.cubic_to(260.0, 460.0, 560.0, 160.0, 940.0, 60.0);
pb.close();
pb.finish().unwrap()
};
let mut pixmap = Pixmap::new(1000, 1000).unwrap();
pixmap.fill_path(
&path1,
&paint1,
FillRule::Winding,
Transform::identity(),
None,
);
pixmap.fill_path(
&path2,
&paint2,
FillRule::Winding,
Transform::identity(),
None,
);
pixmap.save_png("image.png").unwrap();
}
sourcepub fn line_to(&mut self, x: f32, y: f32)
pub fn line_to(&mut self, x: f32, y: f32)
Adds a line from the last point.
- If
Path
is empty - adds Move(0, 0) first. - If
Path
ends with Close - adds Move(last_x, last_y) first.
Examples found in repository?
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
fn crate_triangle() -> Pixmap {
let mut paint = Paint::default();
paint.set_color_rgba8(50, 127, 150, 200);
paint.anti_alias = true;
let mut pb = PathBuilder::new();
pb.move_to(0.0, 20.0);
pb.line_to(20.0, 20.0);
pb.line_to(10.0, 0.0);
pb.close();
let path = pb.finish().unwrap();
let mut pixmap = Pixmap::new(20, 20).unwrap();
pixmap.fill_path(
&path,
&paint,
FillRule::Winding,
Transform::identity(),
None,
);
pixmap
}
More examples
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
fn create_triangle() -> Pixmap {
let mut paint = Paint::default();
paint.set_color_rgba8(50, 127, 150, 200);
paint.anti_alias = true;
let mut pb = PathBuilder::new();
pb.move_to(0.0, 200.0);
pb.line_to(200.0, 200.0);
pb.line_to(100.0, 0.0);
pb.close();
let path = pb.finish().unwrap();
let mut pixmap = Pixmap::new(200, 200).unwrap();
pixmap.fill_path(
&path,
&paint,
FillRule::Winding,
Transform::identity(),
None,
);
let path = PathBuilder::from_rect(Rect::from_ltrb(0.0, 0.0, 200.0, 200.0).unwrap());
let stroke = Stroke::default();
paint.set_color_rgba8(200, 0, 0, 220);
pixmap.stroke_path(&path, &paint, &stroke, Transform::identity(), None); // TODO: stroke_rect
pixmap
}
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
fn main() {
let mut paint = Paint::default();
paint.set_color_rgba8(0, 127, 0, 200);
paint.anti_alias = true;
let path = {
let mut pb = PathBuilder::new();
const RADIUS: f32 = 250.0;
const CENTER: f32 = 250.0;
pb.move_to(CENTER + RADIUS, CENTER);
for i in 1..8 {
let a = 2.6927937 * i as f32;
pb.line_to(CENTER + RADIUS * a.cos(), CENTER + RADIUS * a.sin());
}
pb.finish().unwrap()
};
let mut stroke = Stroke::default();
stroke.width = 6.0;
stroke.line_cap = LineCap::Round;
stroke.dash = StrokeDash::new(vec![20.0, 40.0], 0.0);
let mut pixmap = Pixmap::new(500, 500).unwrap();
pixmap.stroke_path(&path, &paint, &stroke, Transform::identity(), None);
pixmap.save_png("image.png").unwrap();
}
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
fn main() {
let mut paint = Paint::default();
paint.anti_alias = false;
paint.shader = LinearGradient::new(
Point::from_xy(100.0, 100.0),
Point::from_xy(900.0, 900.0),
vec![
GradientStop::new(0.0, Color::from_rgba8(50, 127, 150, 200)),
GradientStop::new(1.0, Color::from_rgba8(220, 140, 75, 180)),
],
SpreadMode::Pad,
Transform::identity(),
)
.unwrap();
let mut pb = PathBuilder::new();
pb.move_to(60.0, 60.0);
pb.line_to(160.0, 940.0);
pb.cubic_to(380.0, 840.0, 660.0, 800.0, 940.0, 800.0);
pb.cubic_to(740.0, 460.0, 440.0, 160.0, 60.0, 60.0);
pb.close();
let path = pb.finish().unwrap();
let mut pixmap = Pixmap::new(1000, 1000).unwrap();
pixmap.fill_path(
&path,
&paint,
FillRule::Winding,
Transform::identity(),
None,
);
pixmap.save_png("image.png").unwrap();
}
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
fn main() {
let mut paint1 = Paint::default();
paint1.set_color_rgba8(50, 127, 150, 200);
paint1.anti_alias = true;
let mut paint2 = Paint::default();
paint2.set_color_rgba8(220, 140, 75, 180);
paint2.anti_alias = false;
let path1 = {
let mut pb = PathBuilder::new();
pb.move_to(60.0, 60.0);
pb.line_to(160.0, 940.0);
pb.cubic_to(380.0, 840.0, 660.0, 800.0, 940.0, 800.0);
pb.cubic_to(740.0, 460.0, 440.0, 160.0, 60.0, 60.0);
pb.close();
pb.finish().unwrap()
};
let path2 = {
let mut pb = PathBuilder::new();
pb.move_to(940.0, 60.0);
pb.line_to(840.0, 940.0);
pb.cubic_to(620.0, 840.0, 340.0, 800.0, 60.0, 800.0);
pb.cubic_to(260.0, 460.0, 560.0, 160.0, 940.0, 60.0);
pb.close();
pb.finish().unwrap()
};
let mut pixmap = Pixmap::new(1000, 1000).unwrap();
pixmap.fill_path(
&path1,
&paint1,
FillRule::Winding,
Transform::identity(),
None,
);
pixmap.fill_path(
&path2,
&paint2,
FillRule::Winding,
Transform::identity(),
None,
);
pixmap.save_png("image.png").unwrap();
}
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
fn main() {
let path1 = {
let mut pb = PathBuilder::new();
pb.move_to(1200.0, 1200.0);
pb.line_to(3200.0, 18800.0);
pb.cubic_to(7600.0, 16800.0, 13200.0, 16000.0, 18800.0, 16000.0);
pb.cubic_to(14800.0, 9200.0, 8800.0, 3200.0, 1200.0, 1200.0);
pb.close();
pb.finish().unwrap()
};
let path2 = {
let mut pb = PathBuilder::new();
pb.move_to(18800.0, 1200.0);
pb.line_to(16800.0, 18800.0);
pb.cubic_to(12400.0, 16800.0, 6800.0, 16000.0, 1200.0, 16000.0);
pb.cubic_to(5200.0, 9200.0, 11200.0, 3200.0, 18800.0, 1200.0);
pb.close();
pb.finish().unwrap()
};
let mut pixmap = Pixmap::new(20000, 20000).unwrap();
let clip_path = {
let mut pb = PathBuilder::new();
pb.push_circle(10000.0, 10000.0, 7000.0);
pb.finish().unwrap()
};
let mut mask = Mask::new(20000, 20000).unwrap();
mask.fill_path(&clip_path, FillRule::Winding, true, Transform::default());
let mut paint = Paint::default();
paint.set_color_rgba8(90, 175, 100, 150);
paint.anti_alias = true;
let large_rect = Rect::from_xywh(500.0, 500.0, 19000.0, 19000.0).unwrap();
pixmap.fill_rect(large_rect, &paint, Transform::identity(), None);
paint.set_color_rgba8(50, 127, 150, 200);
paint.anti_alias = true;
pixmap.fill_path(
&path1,
&paint,
FillRule::Winding,
Transform::default(),
Some(&mask),
);
paint.set_color_rgba8(220, 140, 75, 180);
paint.anti_alias = false;
pixmap.fill_path(
&path2,
&paint,
FillRule::Winding,
Transform::default(),
None,
);
paint.set_color_rgba8(255, 10, 15, 180);
paint.anti_alias = true;
let mut stroke = Stroke::default();
stroke.width = 0.8; // hairline
pixmap.stroke_path(&path2, &paint, &stroke, Transform::default(), None);
pixmap.save_png("image.png").unwrap();
}
sourcepub fn quad_to(&mut self, x1: f32, y1: f32, x: f32, y: f32)
pub fn quad_to(&mut self, x1: f32, y1: f32, x: f32, y: f32)
Adds a quad curve from the last point to x
, y
.
- If
Path
is empty - adds Move(0, 0) first. - If
Path
ends with Close - adds Move(last_x, last_y) first.
sourcepub fn cubic_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x: f32, y: f32)
pub fn cubic_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x: f32, y: f32)
Adds a cubic curve from the last point to x
, y
.
- If
Path
is empty - adds Move(0, 0) first. - If
Path
ends with Close - adds Move(last_x, last_y) first.
Examples found in repository?
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
fn main() {
let mut pb = PathBuilder::new();
pb.move_to(50.0, 100.0);
pb.cubic_to(130.0, 20.0, 390.0, 120.0, 450.0, 30.0);
let path = pb.finish().unwrap();
let mut paint = Paint::default();
paint.set_color_rgba8(50, 127, 150, 200);
paint.anti_alias = true;
let mut pixmap = Pixmap::new(500, 500).unwrap();
let mut transform = Transform::identity();
for i in 0..20 {
let mut stroke = Stroke::default();
stroke.width = 2.0 - (i as f32 / 10.0);
pixmap.stroke_path(&path, &paint, &stroke, transform, None);
transform = transform.pre_translate(0.0, 20.0);
}
pixmap.save_png("image.png").unwrap();
}
More examples
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
fn main() {
let mut paint = Paint::default();
paint.anti_alias = false;
paint.shader = LinearGradient::new(
Point::from_xy(100.0, 100.0),
Point::from_xy(900.0, 900.0),
vec![
GradientStop::new(0.0, Color::from_rgba8(50, 127, 150, 200)),
GradientStop::new(1.0, Color::from_rgba8(220, 140, 75, 180)),
],
SpreadMode::Pad,
Transform::identity(),
)
.unwrap();
let mut pb = PathBuilder::new();
pb.move_to(60.0, 60.0);
pb.line_to(160.0, 940.0);
pb.cubic_to(380.0, 840.0, 660.0, 800.0, 940.0, 800.0);
pb.cubic_to(740.0, 460.0, 440.0, 160.0, 60.0, 60.0);
pb.close();
let path = pb.finish().unwrap();
let mut pixmap = Pixmap::new(1000, 1000).unwrap();
pixmap.fill_path(
&path,
&paint,
FillRule::Winding,
Transform::identity(),
None,
);
pixmap.save_png("image.png").unwrap();
}
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
fn main() {
let mut paint1 = Paint::default();
paint1.set_color_rgba8(50, 127, 150, 200);
paint1.anti_alias = true;
let mut paint2 = Paint::default();
paint2.set_color_rgba8(220, 140, 75, 180);
paint2.anti_alias = false;
let path1 = {
let mut pb = PathBuilder::new();
pb.move_to(60.0, 60.0);
pb.line_to(160.0, 940.0);
pb.cubic_to(380.0, 840.0, 660.0, 800.0, 940.0, 800.0);
pb.cubic_to(740.0, 460.0, 440.0, 160.0, 60.0, 60.0);
pb.close();
pb.finish().unwrap()
};
let path2 = {
let mut pb = PathBuilder::new();
pb.move_to(940.0, 60.0);
pb.line_to(840.0, 940.0);
pb.cubic_to(620.0, 840.0, 340.0, 800.0, 60.0, 800.0);
pb.cubic_to(260.0, 460.0, 560.0, 160.0, 940.0, 60.0);
pb.close();
pb.finish().unwrap()
};
let mut pixmap = Pixmap::new(1000, 1000).unwrap();
pixmap.fill_path(
&path1,
&paint1,
FillRule::Winding,
Transform::identity(),
None,
);
pixmap.fill_path(
&path2,
&paint2,
FillRule::Winding,
Transform::identity(),
None,
);
pixmap.save_png("image.png").unwrap();
}
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
fn main() {
let path1 = {
let mut pb = PathBuilder::new();
pb.move_to(1200.0, 1200.0);
pb.line_to(3200.0, 18800.0);
pb.cubic_to(7600.0, 16800.0, 13200.0, 16000.0, 18800.0, 16000.0);
pb.cubic_to(14800.0, 9200.0, 8800.0, 3200.0, 1200.0, 1200.0);
pb.close();
pb.finish().unwrap()
};
let path2 = {
let mut pb = PathBuilder::new();
pb.move_to(18800.0, 1200.0);
pb.line_to(16800.0, 18800.0);
pb.cubic_to(12400.0, 16800.0, 6800.0, 16000.0, 1200.0, 16000.0);
pb.cubic_to(5200.0, 9200.0, 11200.0, 3200.0, 18800.0, 1200.0);
pb.close();
pb.finish().unwrap()
};
let mut pixmap = Pixmap::new(20000, 20000).unwrap();
let clip_path = {
let mut pb = PathBuilder::new();
pb.push_circle(10000.0, 10000.0, 7000.0);
pb.finish().unwrap()
};
let mut mask = Mask::new(20000, 20000).unwrap();
mask.fill_path(&clip_path, FillRule::Winding, true, Transform::default());
let mut paint = Paint::default();
paint.set_color_rgba8(90, 175, 100, 150);
paint.anti_alias = true;
let large_rect = Rect::from_xywh(500.0, 500.0, 19000.0, 19000.0).unwrap();
pixmap.fill_rect(large_rect, &paint, Transform::identity(), None);
paint.set_color_rgba8(50, 127, 150, 200);
paint.anti_alias = true;
pixmap.fill_path(
&path1,
&paint,
FillRule::Winding,
Transform::default(),
Some(&mask),
);
paint.set_color_rgba8(220, 140, 75, 180);
paint.anti_alias = false;
pixmap.fill_path(
&path2,
&paint,
FillRule::Winding,
Transform::default(),
None,
);
paint.set_color_rgba8(255, 10, 15, 180);
paint.anti_alias = true;
let mut stroke = Stroke::default();
stroke.width = 0.8; // hairline
pixmap.stroke_path(&path2, &paint, &stroke, Transform::default(), None);
pixmap.save_png("image.png").unwrap();
}
sourcepub fn close(&mut self)
pub fn close(&mut self)
Closes the current contour.
A closed contour connects the first and the last Point with a line, forming a continuous loop.
Does nothing when Path
is empty or already closed.
Open and closed contour will be filled the same way. Stroking an open contour will add LineCap at contour’s start and end. Stroking an closed contour will add LineJoin at contour’s start and end.
Examples found in repository?
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
fn crate_triangle() -> Pixmap {
let mut paint = Paint::default();
paint.set_color_rgba8(50, 127, 150, 200);
paint.anti_alias = true;
let mut pb = PathBuilder::new();
pb.move_to(0.0, 20.0);
pb.line_to(20.0, 20.0);
pb.line_to(10.0, 0.0);
pb.close();
let path = pb.finish().unwrap();
let mut pixmap = Pixmap::new(20, 20).unwrap();
pixmap.fill_path(
&path,
&paint,
FillRule::Winding,
Transform::identity(),
None,
);
pixmap
}
More examples
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
fn create_triangle() -> Pixmap {
let mut paint = Paint::default();
paint.set_color_rgba8(50, 127, 150, 200);
paint.anti_alias = true;
let mut pb = PathBuilder::new();
pb.move_to(0.0, 200.0);
pb.line_to(200.0, 200.0);
pb.line_to(100.0, 0.0);
pb.close();
let path = pb.finish().unwrap();
let mut pixmap = Pixmap::new(200, 200).unwrap();
pixmap.fill_path(
&path,
&paint,
FillRule::Winding,
Transform::identity(),
None,
);
let path = PathBuilder::from_rect(Rect::from_ltrb(0.0, 0.0, 200.0, 200.0).unwrap());
let stroke = Stroke::default();
paint.set_color_rgba8(200, 0, 0, 220);
pixmap.stroke_path(&path, &paint, &stroke, Transform::identity(), None); // TODO: stroke_rect
pixmap
}
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
fn main() {
let mut paint = Paint::default();
paint.anti_alias = false;
paint.shader = LinearGradient::new(
Point::from_xy(100.0, 100.0),
Point::from_xy(900.0, 900.0),
vec![
GradientStop::new(0.0, Color::from_rgba8(50, 127, 150, 200)),
GradientStop::new(1.0, Color::from_rgba8(220, 140, 75, 180)),
],
SpreadMode::Pad,
Transform::identity(),
)
.unwrap();
let mut pb = PathBuilder::new();
pb.move_to(60.0, 60.0);
pb.line_to(160.0, 940.0);
pb.cubic_to(380.0, 840.0, 660.0, 800.0, 940.0, 800.0);
pb.cubic_to(740.0, 460.0, 440.0, 160.0, 60.0, 60.0);
pb.close();
let path = pb.finish().unwrap();
let mut pixmap = Pixmap::new(1000, 1000).unwrap();
pixmap.fill_path(
&path,
&paint,
FillRule::Winding,
Transform::identity(),
None,
);
pixmap.save_png("image.png").unwrap();
}
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
fn main() {
let mut paint1 = Paint::default();
paint1.set_color_rgba8(50, 127, 150, 200);
paint1.anti_alias = true;
let mut paint2 = Paint::default();
paint2.set_color_rgba8(220, 140, 75, 180);
paint2.anti_alias = false;
let path1 = {
let mut pb = PathBuilder::new();
pb.move_to(60.0, 60.0);
pb.line_to(160.0, 940.0);
pb.cubic_to(380.0, 840.0, 660.0, 800.0, 940.0, 800.0);
pb.cubic_to(740.0, 460.0, 440.0, 160.0, 60.0, 60.0);
pb.close();
pb.finish().unwrap()
};
let path2 = {
let mut pb = PathBuilder::new();
pb.move_to(940.0, 60.0);
pb.line_to(840.0, 940.0);
pb.cubic_to(620.0, 840.0, 340.0, 800.0, 60.0, 800.0);
pb.cubic_to(260.0, 460.0, 560.0, 160.0, 940.0, 60.0);
pb.close();
pb.finish().unwrap()
};
let mut pixmap = Pixmap::new(1000, 1000).unwrap();
pixmap.fill_path(
&path1,
&paint1,
FillRule::Winding,
Transform::identity(),
None,
);
pixmap.fill_path(
&path2,
&paint2,
FillRule::Winding,
Transform::identity(),
None,
);
pixmap.save_png("image.png").unwrap();
}
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
fn main() {
let path1 = {
let mut pb = PathBuilder::new();
pb.move_to(1200.0, 1200.0);
pb.line_to(3200.0, 18800.0);
pb.cubic_to(7600.0, 16800.0, 13200.0, 16000.0, 18800.0, 16000.0);
pb.cubic_to(14800.0, 9200.0, 8800.0, 3200.0, 1200.0, 1200.0);
pb.close();
pb.finish().unwrap()
};
let path2 = {
let mut pb = PathBuilder::new();
pb.move_to(18800.0, 1200.0);
pb.line_to(16800.0, 18800.0);
pb.cubic_to(12400.0, 16800.0, 6800.0, 16000.0, 1200.0, 16000.0);
pb.cubic_to(5200.0, 9200.0, 11200.0, 3200.0, 18800.0, 1200.0);
pb.close();
pb.finish().unwrap()
};
let mut pixmap = Pixmap::new(20000, 20000).unwrap();
let clip_path = {
let mut pb = PathBuilder::new();
pb.push_circle(10000.0, 10000.0, 7000.0);
pb.finish().unwrap()
};
let mut mask = Mask::new(20000, 20000).unwrap();
mask.fill_path(&clip_path, FillRule::Winding, true, Transform::default());
let mut paint = Paint::default();
paint.set_color_rgba8(90, 175, 100, 150);
paint.anti_alias = true;
let large_rect = Rect::from_xywh(500.0, 500.0, 19000.0, 19000.0).unwrap();
pixmap.fill_rect(large_rect, &paint, Transform::identity(), None);
paint.set_color_rgba8(50, 127, 150, 200);
paint.anti_alias = true;
pixmap.fill_path(
&path1,
&paint,
FillRule::Winding,
Transform::default(),
Some(&mask),
);
paint.set_color_rgba8(220, 140, 75, 180);
paint.anti_alias = false;
pixmap.fill_path(
&path2,
&paint,
FillRule::Winding,
Transform::default(),
None,
);
paint.set_color_rgba8(255, 10, 15, 180);
paint.anti_alias = true;
let mut stroke = Stroke::default();
stroke.width = 0.8; // hairline
pixmap.stroke_path(&path2, &paint, &stroke, Transform::default(), None);
pixmap.save_png("image.png").unwrap();
}
sourcepub fn last_point(&self) -> Option<Point>
pub fn last_point(&self) -> Option<Point>
Returns the last point if any.
sourcepub fn push_rect(&mut self, rect: Rect)
pub fn push_rect(&mut self, rect: Rect)
Adds a rectangle contour.
The contour is closed and has a clock-wise direction.
sourcepub fn push_oval(&mut self, oval: Rect)
pub fn push_oval(&mut self, oval: Rect)
Adds an oval contour bounded by the provided rectangle.
The contour is closed and has a clock-wise direction.
sourcepub fn push_circle(&mut self, x: f32, y: f32, r: f32)
pub fn push_circle(&mut self, x: f32, y: f32, r: f32)
Adds a circle contour.
The contour is closed and has a clock-wise direction.
Does nothing when:
radius
<= 0- any value is not finite or really large
Examples found in repository?
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
fn main() {
let clip_path = {
let mut pb = PathBuilder::new();
pb.push_circle(250.0, 250.0, 200.0);
pb.push_circle(250.0, 250.0, 100.0);
pb.finish().unwrap()
};
let clip_path = clip_path
.transform(Transform::from_row(1.0, -0.3, 0.0, 1.0, 0.0, 75.0))
.unwrap();
let mut mask = Mask::new(500, 500).unwrap();
mask.fill_path(&clip_path, FillRule::EvenOdd, true, Transform::default());
let mut paint = Paint::default();
paint.anti_alias = false;
paint.set_color_rgba8(50, 127, 150, 200);
let mut pixmap = Pixmap::new(500, 500).unwrap();
pixmap.fill_rect(
Rect::from_xywh(0.0, 0.0, 500.0, 500.0).unwrap(),
&paint,
Transform::identity(),
Some(&mask),
);
pixmap.save_png("image.png").unwrap();
}
More examples
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
fn main() {
let path1 = {
let mut pb = PathBuilder::new();
pb.move_to(1200.0, 1200.0);
pb.line_to(3200.0, 18800.0);
pb.cubic_to(7600.0, 16800.0, 13200.0, 16000.0, 18800.0, 16000.0);
pb.cubic_to(14800.0, 9200.0, 8800.0, 3200.0, 1200.0, 1200.0);
pb.close();
pb.finish().unwrap()
};
let path2 = {
let mut pb = PathBuilder::new();
pb.move_to(18800.0, 1200.0);
pb.line_to(16800.0, 18800.0);
pb.cubic_to(12400.0, 16800.0, 6800.0, 16000.0, 1200.0, 16000.0);
pb.cubic_to(5200.0, 9200.0, 11200.0, 3200.0, 18800.0, 1200.0);
pb.close();
pb.finish().unwrap()
};
let mut pixmap = Pixmap::new(20000, 20000).unwrap();
let clip_path = {
let mut pb = PathBuilder::new();
pb.push_circle(10000.0, 10000.0, 7000.0);
pb.finish().unwrap()
};
let mut mask = Mask::new(20000, 20000).unwrap();
mask.fill_path(&clip_path, FillRule::Winding, true, Transform::default());
let mut paint = Paint::default();
paint.set_color_rgba8(90, 175, 100, 150);
paint.anti_alias = true;
let large_rect = Rect::from_xywh(500.0, 500.0, 19000.0, 19000.0).unwrap();
pixmap.fill_rect(large_rect, &paint, Transform::identity(), None);
paint.set_color_rgba8(50, 127, 150, 200);
paint.anti_alias = true;
pixmap.fill_path(
&path1,
&paint,
FillRule::Winding,
Transform::default(),
Some(&mask),
);
paint.set_color_rgba8(220, 140, 75, 180);
paint.anti_alias = false;
pixmap.fill_path(
&path2,
&paint,
FillRule::Winding,
Transform::default(),
None,
);
paint.set_color_rgba8(255, 10, 15, 180);
paint.anti_alias = true;
let mut stroke = Stroke::default();
stroke.width = 0.8; // hairline
pixmap.stroke_path(&path2, &paint, &stroke, Transform::default(), None);
pixmap.save_png("image.png").unwrap();
}
sourcepub fn finish(self) -> Option<Path>
pub fn finish(self) -> Option<Path>
Finishes the builder and returns a Path
.
Returns None
when Path
is empty or has invalid bounds.
Examples found in repository?
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
fn crate_triangle() -> Pixmap {
let mut paint = Paint::default();
paint.set_color_rgba8(50, 127, 150, 200);
paint.anti_alias = true;
let mut pb = PathBuilder::new();
pb.move_to(0.0, 20.0);
pb.line_to(20.0, 20.0);
pb.line_to(10.0, 0.0);
pb.close();
let path = pb.finish().unwrap();
let mut pixmap = Pixmap::new(20, 20).unwrap();
pixmap.fill_path(
&path,
&paint,
FillRule::Winding,
Transform::identity(),
None,
);
pixmap
}
More examples
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
fn main() {
let mut pb = PathBuilder::new();
pb.move_to(50.0, 100.0);
pb.cubic_to(130.0, 20.0, 390.0, 120.0, 450.0, 30.0);
let path = pb.finish().unwrap();
let mut paint = Paint::default();
paint.set_color_rgba8(50, 127, 150, 200);
paint.anti_alias = true;
let mut pixmap = Pixmap::new(500, 500).unwrap();
let mut transform = Transform::identity();
for i in 0..20 {
let mut stroke = Stroke::default();
stroke.width = 2.0 - (i as f32 / 10.0);
pixmap.stroke_path(&path, &paint, &stroke, transform, None);
transform = transform.pre_translate(0.0, 20.0);
}
pixmap.save_png("image.png").unwrap();
}
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
fn create_triangle() -> Pixmap {
let mut paint = Paint::default();
paint.set_color_rgba8(50, 127, 150, 200);
paint.anti_alias = true;
let mut pb = PathBuilder::new();
pb.move_to(0.0, 200.0);
pb.line_to(200.0, 200.0);
pb.line_to(100.0, 0.0);
pb.close();
let path = pb.finish().unwrap();
let mut pixmap = Pixmap::new(200, 200).unwrap();
pixmap.fill_path(
&path,
&paint,
FillRule::Winding,
Transform::identity(),
None,
);
let path = PathBuilder::from_rect(Rect::from_ltrb(0.0, 0.0, 200.0, 200.0).unwrap());
let stroke = Stroke::default();
paint.set_color_rgba8(200, 0, 0, 220);
pixmap.stroke_path(&path, &paint, &stroke, Transform::identity(), None); // TODO: stroke_rect
pixmap
}
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
fn main() {
let clip_path = {
let mut pb = PathBuilder::new();
pb.push_circle(250.0, 250.0, 200.0);
pb.push_circle(250.0, 250.0, 100.0);
pb.finish().unwrap()
};
let clip_path = clip_path
.transform(Transform::from_row(1.0, -0.3, 0.0, 1.0, 0.0, 75.0))
.unwrap();
let mut mask = Mask::new(500, 500).unwrap();
mask.fill_path(&clip_path, FillRule::EvenOdd, true, Transform::default());
let mut paint = Paint::default();
paint.anti_alias = false;
paint.set_color_rgba8(50, 127, 150, 200);
let mut pixmap = Pixmap::new(500, 500).unwrap();
pixmap.fill_rect(
Rect::from_xywh(0.0, 0.0, 500.0, 500.0).unwrap(),
&paint,
Transform::identity(),
Some(&mask),
);
pixmap.save_png("image.png").unwrap();
}
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
fn main() {
let mut paint = Paint::default();
paint.set_color_rgba8(0, 127, 0, 200);
paint.anti_alias = true;
let path = {
let mut pb = PathBuilder::new();
const RADIUS: f32 = 250.0;
const CENTER: f32 = 250.0;
pb.move_to(CENTER + RADIUS, CENTER);
for i in 1..8 {
let a = 2.6927937 * i as f32;
pb.line_to(CENTER + RADIUS * a.cos(), CENTER + RADIUS * a.sin());
}
pb.finish().unwrap()
};
let mut stroke = Stroke::default();
stroke.width = 6.0;
stroke.line_cap = LineCap::Round;
stroke.dash = StrokeDash::new(vec![20.0, 40.0], 0.0);
let mut pixmap = Pixmap::new(500, 500).unwrap();
pixmap.stroke_path(&path, &paint, &stroke, Transform::identity(), None);
pixmap.save_png("image.png").unwrap();
}
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
fn main() {
let mut paint = Paint::default();
paint.anti_alias = false;
paint.shader = LinearGradient::new(
Point::from_xy(100.0, 100.0),
Point::from_xy(900.0, 900.0),
vec![
GradientStop::new(0.0, Color::from_rgba8(50, 127, 150, 200)),
GradientStop::new(1.0, Color::from_rgba8(220, 140, 75, 180)),
],
SpreadMode::Pad,
Transform::identity(),
)
.unwrap();
let mut pb = PathBuilder::new();
pb.move_to(60.0, 60.0);
pb.line_to(160.0, 940.0);
pb.cubic_to(380.0, 840.0, 660.0, 800.0, 940.0, 800.0);
pb.cubic_to(740.0, 460.0, 440.0, 160.0, 60.0, 60.0);
pb.close();
let path = pb.finish().unwrap();
let mut pixmap = Pixmap::new(1000, 1000).unwrap();
pixmap.fill_path(
&path,
&paint,
FillRule::Winding,
Transform::identity(),
None,
);
pixmap.save_png("image.png").unwrap();
}
Trait Implementations§
source§impl Clone for PathBuilder
impl Clone for PathBuilder
source§fn clone(&self) -> PathBuilder
fn clone(&self) -> PathBuilder
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more