Available on crate feature
canvas
only.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§
- fill
- Fill
Geometry
with a certain style. - gradient
- A gradient that can be used as a fill for some geometry.
- path
- Build different kinds of 2D shapes.
- stroke
- Create lines from a
Path
and assigns them various attributes/styles.
Structs§
- Action
- A runtime action that can be performed by some widgets.
- Canvas
- A widget capable of drawing 2D graphics.
- Fill
- The style used to fill geometry.
- Group
- A cache group.
- Image
- A raster image that can be drawn.
- Line
Dash - The dash pattern used when stroking the line.
- Path
- An immutable set of points that may or may not be connected.
- Stroke
- The style of a stroke.
- Text
- A bunch of text that can be drawn to a canvas
Enums§
- Event
- A user interface event.
- Gradient
- A fill which linearly interpolates colors along a direction.
- LineCap
- The shape used at the end of open subpaths when they are stroked.
- Line
Join - The shape used at the corners of paths or basic shapes when they are stroked.
- Style
- The coloring style of some drawing.