Coder Social home page Coder Social logo

pixel-canvas's Introduction

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

pixel-canvas's People

Contributors

a2aaron avatar porglezomp avatar sassman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

pixel-canvas's Issues

DPI isn't right on Mac with retina display

The demo included in the crate-level doc-string doesn't render correctly.

  • As written, the circle that is supposed to follow the mouse is centered at a point twice as far from the upper left corner as the actual mouse pointer.

  • If I add .hidpi(true) to the canvas setup, the window covers the same amount of screen area (as measured in inches), but the image is rendered at twice the pixel density. The circle is smaller. But its center is still twice as far from the upper left as the mouse pointer.

The error has to be either in input.rs or in Glutin itself...

Winit changes cause compilation failure

So i have been trying to install pixel-canvas to give it a try, but when i try to build it, this error gets popped out into my terminal

error[E0599]: no method named `hidpi_factor` found for type `&winit::window::Window` in the current scope
   --> G:\Rust\.cargo\registry\src\github.com-1ecc6299db9ec823\pixel-canvas-0.2.0\src\canvas.rs:221:42
    |
221 |             display.gl_window().window().hidpi_factor()
    |                                          ^^^^^^^^^^^^ method not found in `&winit::window::Window`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0599`.
error: could not compile `pixel-canvas`.
warning: build failed, waiting for other jobs to finish...
error: build failed

i'm using the latest stable version of Rust with MSVC Build tools

Update the dependency

Please update the dependency glium from 0.26 to 0.28, so that the upstream fixes from glutin->winit can be used to pass the compilation on Applle Silicon Macs

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.