Expand description

The Canvas is the main entry point of the library. It handles window creation and input, calls your render callback, and presents the image on the screen.

You create and configure a Canvas via builder methods. You can create a perfectly functionl, bare-bones canvas just by calling Canvas::new with your dimensions, and then calling render. If you want a fancier canvas (like handling input, with a custom title, etc.) you can configure that as well. For example:

let canvas = Canvas::new(512, 512)
    .title("Tile")
    .hidpi(true)
    .show_ms(true)
    .state(MouseState::new())
    .input(MouseState::handle_input);

This adds a 512x512 window called “Title”, that renders in hidpi mode, displays the frame rendering time, and tracks the position of the mouse in physical pixels. For more information on event handlers, see the input module.

Once you’ve created your canvas, you can use it to render your art. Do whatever you want in the render callback, the image you build will be displayed in the window when your render callback returns.

canvas.render(|mouse, image| {
    let width = image.width() as usize;
    for (y, row) in image.chunks_mut(width).enumerate() {
        for (x, pixel) in row.iter_mut().enumerate() {
            *pixel = make_your_own_color(x, y, mouse.x, mouse.y);
        }
    }
});

Structs

A Canvas manages a window and event loop, handing the current state to the renderer, and presenting its image on the screen.

Information about the Canvas.

Type Definitions

A type that represents an event handler.