pub struct _<Message, P> where
P: Program<Message>, { /* private fields */ }
Available on crate feature
pure
only.Expand description
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 theCanvas
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 theCanvas
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
sourceimpl<Message, P> Canvas<Message, P> where
P: Program<Message>,
impl<Message, P> Canvas<Message, P> where
P: Program<Message>,
Trait Implementations
sourceimpl<Message, P> Debug for Canvas<Message, P> where
Message: Debug,
P: Debug + Program<Message>,
impl<Message, P> Debug for Canvas<Message, P> where
Message: Debug,
P: Debug + Program<Message>,
sourceimpl<Message, P, B> Widget<Message, Renderer<B>> for Canvas<Message, P> where
P: Program<Message>,
B: Backend,
impl<Message, P, B> Widget<Message, Renderer<B>> for Canvas<Message, P> where
P: Program<Message>,
B: Backend,
sourcefn on_event(
&mut self,
event: Event,
layout: Layout<'_>,
cursor_position: Point,
_renderer: &Renderer<B>,
_clipboard: &mut dyn Clipboard,
shell: &mut Shell<'_, Message>
) -> Status
fn on_event(
&mut self,
event: Event,
layout: Layout<'_>,
cursor_position: Point,
_renderer: &Renderer<B>,
_clipboard: &mut dyn Clipboard,
shell: &mut Shell<'_, Message>
) -> Status
sourcefn mouse_interaction(
&self,
layout: Layout<'_>,
cursor_position: Point,
_viewport: &Rectangle<f32>,
_renderer: &Renderer<B>
) -> Interaction
fn mouse_interaction(
&self,
layout: Layout<'_>,
cursor_position: Point,
_viewport: &Rectangle<f32>,
_renderer: &Renderer<B>
) -> Interaction
Returns the current mouse::Interaction
of the Widget
. Read more
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
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more