Crates.io | ferrux_canvas |
lib.rs | ferrux_canvas |
version | 0.3.2 |
source | src |
created_at | 2022-02-21 18:46:32.14226 |
updated_at | 2022-06-07 10:33:48.79016 |
description | Abstraction tool to ease the use of the Pixels buffer |
homepage | https://crates.io/crates/ferrux_canvas |
repository | https://github.com/kriogenia/ferrux_canvas |
max_upload_size | |
id | 536912 |
size | 43,983 |
Ferrux Canvas is an abstraction layer over the Pixels crate. It manages the pixel buffer exposing simple operations to draw pixels, lines and figures in the screen. In its current state it only works with Winit.
The list of current goals is listed in the repository's project.
Right now, the only Canvas provided is WinitCanvas, which requires a winit Window, which will need itself an EventLoop reference.
let event_loop = winit::event_loop::EventLoop::new();
let window = winit::window::Window::new(&event_loop)?;
let canvas = ferrux_canvas::canvas::winit::WinitCanvas::new(&window)?;
The main flow to use a canvas is:
draw_line
] and [draw_triangle
].render
] method to print it on screen.reset_frame
] to clear the current buffer and draw a new frame.The following example takes the [WinitCanvas
] we built and draws a morphing triangle.
let mut x: i32 = 1;
let mut incrementing = true;
event_loop.run(move |event, _, control_flow| {
match event {
Event::MainEventsCleared => {
window.request_redraw();
}
Event::RedrawRequested(_) => {
if !(1..100).contains(&x) {
incrementing = !incrementing;
}
x += if incrementing { 1 } else { -1 };
canvas.draw_triangle((100, (100 - x) as u32), (100 - x as u32, 100),
(200 - x as u32, 200 - x as u32), palette::WHITE);
canvas.fill_triangle((100, (100 - x) as u32), (100 - x as u32, 100),
(200 - x as u32, 200 - x as u32), Color::from_rgba("2303b0dd"));
canvas.render().unwrap();
canvas.reset_frame();
}
_ => (),
}
});
The FerruX Canvas is a tool developed while creating the FerruXengine, an attempt of 3D graphics engine I was trying to make. I made this canvas to ease the use of the Pixels buffer used by the engine. As the tool proved useful for other possible future projects I was thinking of, and a couple of mutuals of mine thought that they could use it too, it was decided to extract it as its own library.
Licensed, at your option, under either of:
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.