2 releases
0.1.1 | Feb 16, 2023 |
---|---|
0.1.0 | Feb 16, 2023 |
#91 in Rendering
27 downloads per month
120KB
1K
SLoC
tinydraw-rs
tinydraw is a small library for 2D drawing in Rust
It's a simple crate used for drawing basic, anti-aliased shapes onto images, written in pure Rust. Support for reading and exporting images as PNG or bytes is included (dependencies).
Available Shapes
- line
- rectangle
- circle
- ellipse
Example
use tinydraw::ImageRGB8;
fn main() {
let background_color: [u8; 3] = [255, 155, 0];
let mut image: ImageRGB8 = ImageRGB8::new(640, 360, background_color);
image.draw_line(0, 0, 639, 359, [255, 255, 255], 1, 1.0);
image.draw_line(0, 359, 639, 0, [255, 255, 255], 1, 1.0);
image.draw_rectangle(0, 0, 639, 359, [255, 255, 255], 3, 1.0);
image.draw_ellipse(319, 179, 300, 150, [0, 0, 0], 0, 0.5);
image.draw_circle(149, 179, 30, [255, 255, 255], 0, 1.0);
image.draw_circle(149, 179, 20, [0, 0, 0], 0, 1.0);
image.draw_circle(489, 179, 30, [255, 255, 255], 0, 1.0);
image.draw_circle(489, 179, 20, [0, 0, 0], 0, 1.0);
image.draw_ellipse(319, 90, 80, 30, [255, 255, 255], 0, 1.0);
image.draw_ellipse(319, 90, 60, 20, [0, 0, 0], 0, 1.0);
image.to_png("image.png").unwrap();
}
This code generates the following image:
Limitations
- thickness above 1 doesn't work for:
- line
- circle
- ellipse
- coordinates exceeding the image bounds don't work for:
- rectangle
- circle
- ellipse
- only RGB images with bit depth of 8 are currently supported
Dependencies
bytemuck (reading, exporting bytes)
png (reading, exporting PNG)
Development
I intend to fix the limitations and perhaps add more shapes in the future. It depends on my free time and whether there will be any interest for this crate. If you encounter a bug or have any suggestions, feel free to open an issue. If you want to contribute, feel free to open a pull request.
References
Wikipedia - Xiaolin Wu's line algorithm
GeeksforGeeks - Anti-aliased Line | Xiaolin Wu’s algorithm
Stephan Brumme - Drawing Antialiased Circles and Ellipses
David Moksha - Fast, Antialiased Circles and Ellipses from Xiaolin Wu’s concepts
Dependencies
~1MB
~22K SLoC