1
2
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
49
50
51
52
53
54
55
56
57
58
59
use tiny_skia::*;

fn main() {
    let triangle = create_triangle();

    let mut pixmap = Pixmap::new(400, 400).unwrap();

    let now = std::time::Instant::now();

    let mut paint = PixmapPaint::default();
    paint.quality = FilterQuality::Bicubic;

    pixmap.draw_pixmap(
        20,
        20,
        triangle.as_ref(),
        &paint,
        Transform::from_row(1.2, 0.5, 0.5, 1.2, 0.0, 0.0),
        None,
    );

    println!(
        "Rendered in {:.2}ms",
        now.elapsed().as_micros() as f64 / 1000.0
    );

    pixmap.save_png("image.png").unwrap();
}

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
}