#canvas #pixel #graphics #2d

ferrux_canvas

Abstraction tool to ease the use of the Pixels buffer

4 releases (2 breaking)

0.3.2 Jun 7, 2022
0.3.1 Mar 5, 2022
0.3.0 Mar 4, 2022
0.2.0 Feb 25, 2022
0.1.1 Feb 21, 2022

#50 in #canvas

MIT/Apache

32KB
436 lines

Crates.io docs.rs Rust

FerruX Canvas

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.

Usage

Building a canvas

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)?;

Running a canvas

The main flow to use a canvas is:

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();
     }
     _ => (),
   }
 });

About

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.

License

Licensed, at your option, under either of:

Contribution

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.

Dependencies

~9–24MB
~354K SLoC