pub struct Canvas<'a> { /* private fields */ }Expand description
A clipped, writable pixel target.
Every operation is clipped to Canvas::clip, which starts as the whole frame
and only ever shrinks. Clipping is rectangular: that covers scrolling regions,
damage-restricted repaint and nested panels, which is all a UI actually needs.
Arbitrary clip shapes are not planned.
Coordinates are physical pixels relative to the frame origin, never relative to the clip.
Implementations§
Source§impl<'a> Canvas<'a>
impl<'a> Canvas<'a>
Sourcepub fn new(frame: &'a mut Frame<'_>) -> Self
pub fn new(frame: &'a mut Frame<'_>) -> Self
Borrows a frame for drawing, clipped to the whole frame.
Sourcepub fn from_pixels(
pixels: &'a mut [u32],
size: Size,
stride: u32,
format: PixelFormat,
) -> Option<Self>
pub fn from_pixels( pixels: &'a mut [u32], size: Size, stride: u32, format: PixelFormat, ) -> Option<Self>
Wraps a raw pixel slice. Returns None if it is too small for the geometry.
Prefer Canvas::new; this exists for offscreen buffers and benchmarks that
have no Frame to hand.
Sourcepub const fn format(&self) -> PixelFormat
pub const fn format(&self) -> PixelFormat
Word layout of the target.
Sourcepub fn with_clip(&mut self, rect: Rect) -> Canvas<'_>
pub fn with_clip(&mut self, rect: Rect) -> Canvas<'_>
A canvas over the same pixels with a tighter clip.
The borrow ends when the returned canvas is dropped, so this is how a parent hands a child a region to draw in without either being able to escape it.
Sourcepub const fn is_clipped_out(&self) -> bool
pub const fn is_clipped_out(&self) -> bool
Returns true if the clip admits no pixels, so drawing can be skipped.
Sourcepub fn clear(&mut self, color: Color)
pub fn clear(&mut self, color: Color)
Fills the entire clip with an opaque colour.
This is the full-frame clear when the clip is untouched, and the damage-restricted clear when it is not.
Sourcepub fn copy_from(&mut self, src: &PixelView<'_>, regions: &[Rect])
pub fn copy_from(&mut self, src: &PixelView<'_>, regions: &[Rect])
Copies matching regions out of another buffer.
Source and destination coordinates are the same, so this is the damage-driven “publish what changed” blit a double-buffered backend needs — not a general blitter. Regions are clipped to both buffers.
Source§impl Canvas<'_>
impl Canvas<'_>
Source§impl Canvas<'_>
impl Canvas<'_>
Sourcepub fn draw_line(&mut self, a: Point, b: Point, color: impl Into<Paint>)
pub fn draw_line(&mut self, a: Point, b: Point, color: impl Into<Paint>)
Draws a one-pixel line between two pixel centres, anti-aliased.
Axis-aligned lines take an exact integer path and get no anti-aliasing, because a horizontal rule that has been softened to 97% grey looks like a rendering bug, not like quality. Everything else is a Wu-style two-pixel blend along the minor axis.
Thickness is not a parameter. Borders come from Canvas::stroke_rect and
Canvas::stroke_rounded_rect; thick arbitrary-angle lines are not
something a UI toolkit needs before it can draw a chart, and that is not
this milestone.
Source§impl Canvas<'_>
impl Canvas<'_>
Sourcepub fn fill_rect(&mut self, rect: Rect, color: impl Into<Paint>)
pub fn fill_rect(&mut self, rect: Rect, color: impl Into<Paint>)
Fills rect, compositing with the destination if the paint has alpha.
Sourcepub fn fill_rects(&mut self, rects: &[Rect], color: impl Into<Paint>)
pub fn fill_rects(&mut self, rects: &[Rect], color: impl Into<Paint>)
Fills every rectangle. Overlapping rectangles composite twice.
Sourcepub fn stroke_rect(
&mut self,
rect: Rect,
thickness: i32,
color: impl Into<Paint>,
)
pub fn stroke_rect( &mut self, rect: Rect, thickness: i32, color: impl Into<Paint>, )
Draws a border of thickness pixels inside rect.
The four bands are cut so they do not overlap. Drawing them as four full-length rectangles would composite the corners twice, which is invisible at full alpha and obvious at anything less.
Source§impl Canvas<'_>
impl Canvas<'_>
Sourcepub fn fill_rounded_rect(
&mut self,
rect: Rect,
radius: i32,
color: impl Into<Paint>,
)
pub fn fill_rounded_rect( &mut self, rect: Rect, radius: i32, color: impl Into<Paint>, )
Fills a rectangle with rounded corners, anti-aliased.
radius is clamped to half the shorter side; a radius of zero is exactly
Canvas::fill_rect.