Struct iced::widget::canvas::Canvas[]

pub struct Canvas<Message, P> where
    P: Program<Message>, 
{ /* fields omitted */ }
This is supported on crate features canvas or glow_canvas only.

A widget capable of drawing 2D graphics.

Examples

The repository has a couple of examples showcasing how to use a Canvas:

  • clock, an application that uses the Canvas widget to draw a clock and its hands to display the current time.
  • game_of_life, an interactive version of the Game of Life, invented by John Conway.
  • solar_system, an animated solar system drawn using the Canvas widget and showcasing how to compose different transforms.

Drawing a simple circle

If you want to get a quick overview, here’s how we can draw a simple circle:

use iced::canvas::{self, Canvas, Cursor, Fill, Frame, Geometry, Path, Program};
use iced::{Color, Rectangle};

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

// Then, we implement the `Program` trait
impl Program<()> for Circle {
    fn draw(&self, bounds: Rectangle, _cursor: Cursor) -> Vec<Geometry>{
        // We prepare a new `Frame`
        let mut frame = Frame::new(bounds.size());

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

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

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

// Finally, we simply use our `Circle` to create the `Canvas`!
let canvas = Canvas::new(Circle { radius: 50.0 });

Implementations

impl<Message, P> Canvas<Message, P> where
    P: Program<Message>, 

pub fn new(program: P) -> Canvas<Message, P>

Creates a new Canvas.

pub fn width(self, width: Length) -> Canvas<Message, P>

Sets the width of the Canvas.

pub fn height(self, height: Length) -> Canvas<Message, P>

Sets the height of the Canvas.

Trait Implementations

impl<Message, P> Debug for Canvas<Message, P> where
    Message: Debug,
    P: Debug + Program<Message>, 

impl<Message, P, B> Widget<Message, Renderer<B>> for Canvas<Message, P> where
    P: Program<Message>,
    B: Backend, 

Auto Trait Implementations

impl<Message, P> RefUnwindSafe for Canvas<Message, P> where
    Message: RefUnwindSafe,
    P: RefUnwindSafe

impl<Message, P> Send for Canvas<Message, P> where
    Message: Send,
    P: Send

impl<Message, P> Sync for Canvas<Message, P> where
    Message: Sync,
    P: Sync

impl<Message, P> Unpin for Canvas<Message, P> where
    Message: Unpin,
    P: Unpin

impl<Message, P> UnwindSafe for Canvas<Message, P> where
    Message: UnwindSafe,
    P: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Downcast<T> for T

impl<T> From<T> for T[src]

impl<T> Instrument for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Pointable for T

type Init = T

The type for initializers.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> Upcast<T> for T

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,