#pixel-art #art #interactive #image #graphics #events

pixel-canvas

A crate to make it easy to build interactive computer art with just a pixel buffer

5 releases

0.2.3 Jun 19, 2022
0.2.2 Dec 11, 2020
0.2.1 Mar 1, 2020
0.2.0 Sep 16, 2019
0.1.0 Aug 25, 2019

#116 in Rendering

Download history 99/week @ 2024-07-20 139/week @ 2024-07-27 38/week @ 2024-08-03 58/week @ 2024-08-10 39/week @ 2024-08-17 52/week @ 2024-08-24 56/week @ 2024-08-31 37/week @ 2024-09-07 47/week @ 2024-09-14 84/week @ 2024-09-21 73/week @ 2024-09-28 17/week @ 2024-10-05 25/week @ 2024-10-12 46/week @ 2024-10-19 32/week @ 2024-10-26 36/week @ 2024-11-02

139 downloads per month
Used in fpgrars

MIT/Apache

32KB
568 lines

Crates.io Docs.rs Build Status

Pixel Canvas

This crate is designed to make it easy to build interactive computer art with just a pixel buffer. For inspiration, consider looking at https://shadertoy.com and http://www.iquilezles.org/www/index.htm, there are a lot of cool art pieces to see and explanations of fun techniques!

Usage

To make a piece of art, you create and configure a Canvas object, and then you ask it to render with your code. The canvas will do state management and hand you an image to modify. Whatever modifications you make to the image will be displayed on the screen.

Example

use pixel_canvas::{Canvas, Color, input::MouseState};

fn main() {
    // Configure the window that you want to draw in. You can add an event
    // handler to build interactive art. Input handlers for common use are
    // provided.
    let canvas = Canvas::new(512, 512)
        .title("Tile")
        .state(MouseState::new())
        .input(MouseState::handle_input);
    // The canvas will render for you at up to 60fps.
    canvas.render(|mouse, image| {
        // Modify the `image` based on your state.
        let width = image.width() as usize;
        for (y, row) in image.chunks_mut(width).enumerate() {
            for (x, pixel) in row.iter_mut().enumerate() {
                let dx = x as i32 - mouse.x;
                let dy = y as i32 - mouse.y;
                let dist = dx * dx + dy * dy;
                *pixel = Color {
                    r: if dist < 128 * 128 { dy as u8 } else { 0 },
                    g: if dist < 128 * 128 { dx as u8 } else { 0 },
                    b: (x * y) as u8,
                }
            }
        }
    });
}

or run an included example (with nightly):

cargo +nightly run --example api_example

License: MIT OR Apache-2.0

Dependencies

~7–15MB
~208K SLoC