Module cosmic::widget::canvas

source ·
Expand description

Canvases can be leveraged to draw interactive 2D graphics.

§Example: Drawing a Simple Circle

use iced::mouse;
use iced::widget::canvas;
use iced::{Color, Rectangle, Renderer, Theme};

// First, we define the data we need for drawing
#[derive(Debug)]
struct Circle {
    radius: f32,
}

// Then, we implement the `Program` trait
impl<Message> canvas::Program<Message> for Circle {
    // No internal state
    type State = ();

    fn draw(
        &self,
        _state: &(),
        renderer: &Renderer,
        _theme: &Theme,
        bounds: Rectangle,
        _cursor: mouse::Cursor
    ) -> Vec<canvas::Geometry> {
        // We prepare a new `Frame`
        let mut frame = canvas::Frame::new(renderer, bounds.size());

        // We create a `Path` representing a simple circle
        let circle = canvas::Path::circle(frame.center(), self.radius);

        // And fill it with some color
        frame.fill(&circle, Color::BLACK);

        // Then, we produce the geometry
        vec![frame.into_geometry()]
    }
}

// Finally, we simply use our `Circle` to create the `Canvas`!
fn view<'a, Message: 'a>(_state: &'a State) -> Element<'a, Message> {
    canvas(Circle { radius: 50.0 }).into()
}

Modules§

  • Handle events of a canvas.
  • Fill Geometry with a certain style.
  • A gradient that can be used as a fill for some geometry.
  • Build different kinds of 2D shapes.
  • Create lines from a Path and assigns them various attributes/styles.

Structs§

  • A widget capable of drawing 2D graphics.
  • The style used to fill geometry.
  • A cache group.
  • A raster image that can be drawn.
  • The dash pattern used when stroking the line.
  • An immutable set of points that may or may not be connected.
  • The style of a stroke.
  • A bunch of text that can be drawn to a canvas

Enums§

  • A Canvas event.
  • A fill which linearly interpolates colors along a direction.
  • The shape used at the end of open subpaths when they are stroked.
  • The shape used at the corners of paths or basic shapes when they are stroked.
  • The coloring style of some drawing.

Traits§

Type Aliases§

  • A simple cache that stores generated Geometry to avoid recomputation.
  • The frame supported by a renderer.
  • The geometry supported by a renderer.