ferrux_canvas

Crates.ioferrux_canvas
lib.rsferrux_canvas
version0.3.2
sourcesrc
created_at2022-02-21 18:46:32.14226
updated_at2022-06-07 10:33:48.79016
descriptionAbstraction tool to ease the use of the Pixels buffer
homepagehttps://crates.io/crates/ferrux_canvas
repositoryhttps://github.com/kriogenia/ferrux_canvas
max_upload_size
id536912
size43,983
Soto Estévez (kriogenia)

documentation

https://docs.rs/ferrux_canvas/latest

README

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:

  • Use the drawing functions like [draw_line] and [draw_triangle].
  • Call the [render] method to print it on screen.
  • Use [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();
     }
     _ => (),
   }
 });

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.

Commit count: 51

cargo fmt