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

pub struct Canvas<'a> { /* fields omitted */ }
This is supported on feature="canvas" only.

A widget capable of drawing 2D graphics.

A Canvas may contain multiple layers. A Layer is drawn using the painter's algorithm. In other words, layers will be drawn on top of each other in the same order they are pushed into the Canvas.

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.
  • 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, layer, Canvas, Drawable, Fill, Frame, Path};
use iced::Color;

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

// Then, we implement the `Drawable` trait
impl Drawable for Circle {
    fn draw(&self, frame: &mut Frame) {
        // We create a `Path` representing a simple circle
        let circle = Path::new(|p| p.circle(frame.center(), self.radius));

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

// We can use a `Cache` to avoid unnecessary re-tessellation
let cache: layer::Cache<Circle> = layer::Cache::new();

// Finally, we simply provide the data to our `Cache` and push the resulting
// layer into a `Canvas`
let canvas = Canvas::new()
    .push(cache.with(&Circle { radius: 50.0 }));

Methods

impl<'a> Canvas<'a>[src]

pub fn new() -> Canvas<'a>[src]

This is supported on feature="canvas" only.

Creates a new Canvas with no layers.

pub fn width(self, width: Length) -> Canvas<'a>[src]

This is supported on feature="canvas" only.

Sets the width of the Canvas.

pub fn height(self, height: Length) -> Canvas<'a>[src]

This is supported on feature="canvas" only.

Sets the height of the Canvas.

pub fn push(self, layer: impl Layer + 'a) -> Canvas<'a>[src]

This is supported on feature="canvas" only.

Adds a Layer to the Canvas.

It will be drawn on top of previous layers.

Trait Implementations

impl<'a> Debug for Canvas<'a>[src]

impl<'a, Message> Widget<Message, Renderer> for Canvas<'a>[src]

Auto Trait Implementations

impl<'a> !RefUnwindSafe for Canvas<'a>

impl<'a> !Send for Canvas<'a>

impl<'a> !Sync for Canvas<'a>

impl<'a> Unpin for Canvas<'a>

impl<'a> !UnwindSafe for Canvas<'a>

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> From<T> for T[src]

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

impl<T> SetParameter for T

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<V, T> VZip<V> for T where
    V: MultiLane<T>,