https://github.com/amamic1803/tinydraw-rs
A small 2D drawing library in Rust
https://github.com/amamic1803/tinydraw-rs
antialiasing crates-io drawing image rust-crate rust-lang shapes
Last synced: over 1 year ago
JSON representation
A small 2D drawing library in Rust
- Host: GitHub
- URL: https://github.com/amamic1803/tinydraw-rs
- Owner: amamic1803
- License: mit
- Created: 2023-02-03T18:50:41.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-05-07T15:23:44.000Z (about 2 years ago)
- Last Synced: 2024-08-10T23:15:10.512Z (almost 2 years ago)
- Topics: antialiasing, crates-io, drawing, image, rust-crate, rust-lang, shapes
- Language: Rust
- Homepage:
- Size: 165 KB
- Stars: 6
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# 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](#dependencies)).
[**Documentation**](https://docs.rs/tinydraw/latest/tinydraw/ "docs.rs")
[**Crate**](https://crates.io/crates/tinydraw "crates.io")
### Available Shapes
- line
- rectangle
- circle
- ellipse
### Example
```rust
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](https://crates.io/crates/bytemuck) (reading, exporting bytes)
[png](https://crates.io/crates/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](https://en.wikipedia.org/wiki/Xiaolin_Wu%27s_line_algorithm)
[GeeksforGeeks - Anti-aliased Line | Xiaolin Wu’s algorithm](https://www.geeksforgeeks.org/anti-aliased-line-xiaolin-wus-algorithm/)
[Stephan Brumme - Drawing Antialiased Circles and Ellipses](https://create.stephan-brumme.com/antialiased-circle/)
[David Moksha - Fast, Antialiased Circles and Ellipses from Xiaolin Wu’s concepts](https://yellowsplash.wordpress.com/2009/10/23/fast-antialiased-circles-and-ellipses-from-xiaolin-wus-concepts/)