Crates.io | tinydraw |
lib.rs | tinydraw |
version | 0.1.1 |
source | src |
created_at | 2023-02-16 15:17:29.215701 |
updated_at | 2023-02-16 15:19:44.774834 |
description | A small library for 2D drawing in Rust |
homepage | |
repository | https://github.com/DarkLord76865/tinydraw-rs |
max_upload_size | |
id | 786839 |
size | 120,410 |
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).
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:
bytemuck (reading, exporting bytes)
png (reading, exporting PNG)
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.
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