kriogenia/ferrux_canvas

Ferrux Canvas is an abstraction layer over the Pixels crate

Ferrux Canvas is an abstraction layer over the Winit

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.

Issues

Collection of the latest Issues

kriogenia

kriogenia

Comment Icon0

Improve the example so it makes use of the new filling function

kriogenia

kriogenia

Comment Icon0

We are drawing the vertical lines using Bresenham. A best approach to improve performance is using simple loop. Implement this alternativa for vertical lines.

kriogenia

kriogenia

Comment Icon0

We are using Bresenham to draw horizontal lines, that's an overkill, implement a basic loop instead to improve performance developing those lines

kriogenia

kriogenia

Comment Icon0

The unit tests have been left apart during this development based on the current unexperience with mocking in Rust. This should not be left aside and the test should be created as soon as possible.

kriogenia

kriogenia

Comment Icon0

Our current drawing buffer is a matrix with a two-dimensional vector. A single buffer could be faster. Investigate that possibility and implement it if it's an improvement.

  • Create a benchmark for line drawing and render.
  • Create the new buffer implementation
  • Check which one is faster
  • Complete the change if it's an improvement.
kriogenia

kriogenia

Comment Icon0

The current background of the Canvas is always black. Create a configuration option to specify a different colour.

kriogenia

kriogenia

Comment Icon0

Right now, the rendering only draws the current buffer over the previous image. A configuration feature named autoreset could be implemented to allow the specification of this action. Allowing to automatically clear the buffer afteer each rendering (optimal to uses like graphic engines).

Versions

Find the latest versions by id

0.3.1 - Mar 05, 2022

Link: https://crates.io/crates/ferrux_canvas/0.3.1

What's Changed

  • V0.3 by @kriogenia in https://github.com/kriogenia/ferrux_canvas/pull/23

  • fill_triangle, a new way to draw in the canvas. This new function of the canvas trait will allow to draw a triangle and fill all its content with the specified color.

  • Performance improvements on horizontal line drawing

  • Performance improvements on vertical line drawing

  • Improvements in the Bresenham algorithm usage

Full Changelog: https://github.com/kriogenia/ferrux_canvas/compare/0.1.1...0.3.0

0.2.0 - Feb 25, 2022

Link: https://crates.io/crates/ferrux_canvas/0.2.0

What's Changed

color

Colors!! The canvas is not black and white only any more, it now accepts colors. A new package color is now available in the crate with three exposed members: Color, ColorBuilder and palette. The use for the colors is allow passing them in the drawing operations to make the drawing in a color different from white.

Color

Color is the struct holding the RGBA representation of the color. It can be built manually specifying each component value or using the from_rgba function that will construct the color with the RGBA string.

ColorBuilder

A tool to help build colors and ease the definition of new similar colors, allowing the creation of a lot of shades with the same builder.

palette

Package holding five precompiled Colors that are commonly used: white #fff, black #000, red #f00, green #0f0 and blue #00f. All of them have the maximum alpha.

Breaking changes

Sadly, this version contains breaking changes. The previous drawing operations of Canvas: draw_pixel, draw_line and draw_triangle won't work anymore with the previous API. They now require and extra parameter to specifiy the Color. I was thinking about keeping them through overloading making using a default color or white, but as this is a very young and relaxed library without almost users I think it's not that bad to make a breaking version early in development.

Full Changelog: https://github.com/kriogenia/ferrux_canvas/compare/0.1.1...0.2.0

0.1.1 - Feb 21, 2022

Link: https://crates.io/crates/ferrux_canvas/0.1.1

What's Changed

Full Changelog: https://github.com/kriogenia/ferrux_canvas/compare/0.1.0...0.1.1

0.1.0 - Feb 21, 2022

Canvas

Trait to manage the buffer with the tools needed for it:

  • width, gives the width of the canvas.
  • height, gives the height of the canvas.
  • render, draws the current frame on the screen.
  • draw_pixel, draws a single pixel in the frame.
  • draw_line, draws a line between two points in the frame.
  • draw_triangle, draws a triangle between three points in the frame.
  • clear_frame, renders an empty frame on the screen.
  • reset_frame, clears the content drawn in the frame.

WinitCanvas

Implementation of Canvas allowing to draw in the screen using the Winit crate.

  • new, creates a new WinitCanvas with a reference to the Winit Window.

CanvasError

Enmeration of errors that can be thrown using Canvas.

Full Changelog: https://github.com/kriogenia/ferrux_canvas/commits/0.1.0

Information - Updated Aug 01, 2022

Stars: 0
Forks: 0
Issues: 8

A mid-level 2D graphics library for rust

rgx is a 2D graphics library built on top of Vulkan/

A mid-level 2D graphics library for rust

Safe Rust bindings to the Skia Graphics Library

Safe Rust bindings to the

Safe Rust bindings to the Skia Graphics Library

Vulkano Vulkan Graphics API for Rust

Vulkano is a Rust wrapper around Wgpu-rs or the Vulkan graphics GPU API

Vulkano Vulkan Graphics API for Rust

This is a NES (Nintendo Entertainment System) emulator written in Rust using SDL2 for graphics...

This is a NES (Nintendo Entertainment System) emulator written in Rust using SDL2 for graphics and input

This is a NES (Nintendo Entertainment System) emulator written in Rust using SDL2 for graphics...

Nanachi - pure Rust 2D graphics library

NOTE: Nanachi is still buggy and the API will changed!

Nanachi - pure Rust 2D graphics library

chip8 emulator written in rust, using rust-sdl2 for graphics

Fully implemented all 35 original chip8 opcodes

chip8 emulator written in rust, using rust-sdl2 for graphics

Rust 3D graphics library for the browser

A work-in-progress Rust -> WASM 3D graphics library for the browser built on top of WebGL, aimed at making it dead simple to do graphics

Rust 3D graphics library for the browser

Rust Graphics Crates

This is an opinionated list of modern graphics crates in Rust

Rust Graphics Crates

🕊 ɱυɠҽ · むげ

Minimal, Modular, Multiplatform Graphics Enginefor Rust and WebAssembly

🕊 ɱυɠҽ · むげ

A Rust library for rendering SVG Graphics with WASM

This crate provides a fast and effective way to interact with SVG's using WebAssembly

A Rust library for rendering SVG Graphics with WASM

▴ rust-wgpu-triangle

The "Hello world" of graphics programming in Rust + wgpu

▴ rust-wgpu-triangle
Facebook Instagram Twitter GitHub Dribbble
Privacy