summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2020-04-01 03:14:49 +0200
committerLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2020-04-02 03:44:14 +0200
commitf4f8f62f55bcaf5b92b7af96544f153f88bab287 (patch)
tree9dc55fb7c9ace20d5f1788208b970657c6057f93
parentac8875f81d3ddc50b6ccafb0a867a6df3058e557 (diff)
downloadiced-f4f8f62f55bcaf5b92b7af96544f153f88bab287.tar.gz
iced-f4f8f62f55bcaf5b92b7af96544f153f88bab287.tar.bz2
iced-f4f8f62f55bcaf5b92b7af96544f153f88bab287.zip
Add example for `Canvas` widget
Diffstat (limited to '')
-rw-r--r--wgpu/src/widget/canvas.rs41
1 files changed, 39 insertions, 2 deletions
diff --git a/wgpu/src/widget/canvas.rs b/wgpu/src/widget/canvas.rs
index 3a9605c9..5d531a0a 100644
--- a/wgpu/src/widget/canvas.rs
+++ b/wgpu/src/widget/canvas.rs
@@ -33,11 +33,48 @@ pub use text::Text;
/// 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 in
-/// the same order they are pushed into the [`Canvas`].
+/// 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`].
///
/// [`Canvas`]: struct.Canvas.html
/// [`Layer`]: layer/trait.Layer.html
+///
+/// # Example
+/// Let's see how we can draw a circle:
+///
+/// ```
+/// # mod iced {
+/// # pub use iced_wgpu::canvas;
+/// # pub use iced_native::Color;
+/// # }
+/// 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 }));
+/// ```
#[derive(Debug)]
pub struct Canvas<'a> {
width: Length,