Skip to main content

Canvas

Struct Canvas 

Source
pub struct Canvas;
Expand description

Immediate-mode canvas facade that wraps the low-level drawing functions.

All methods paint immediately (no retained scene graph). Create one per frame or per start_app call and issue draw commands through it.

Implementations§

Source§

impl Canvas

Source

pub fn new() -> Self

Create a new canvas handle. This is zero-cost — no allocation occurs.

Source

pub fn clear(&self, color: Color)

Clear the canvas with a solid color.

Source

pub fn fill_rect(&self, rect: Rect, color: Color)

Draw a filled rectangle.

Source

pub fn fill_rounded_rect(&self, rect: Rect, radius: f32, color: Color)

Draw a filled rounded rectangle with uniform corner radius.

Source

pub fn fill_circle(&self, center: Point2D, radius: f32, color: Color)

Draw a filled circle.

Source

pub fn arc( &self, center: Point2D, radius: f32, start_angle: f32, end_angle: f32, thickness: f32, color: Color, )

Draw a circular arc stroke from start_angle to end_angle (radians).

Source

pub fn bezier( &self, from: Point2D, ctrl1: Point2D, ctrl2: Point2D, to: Point2D, thickness: f32, color: Color, )

Draw a cubic Bézier curve stroke.

Source

pub fn text(&self, text: &str, pos: Point2D, size: f32, color: Color)

Draw text at a position.

Source

pub fn line(&self, from: Point2D, to: Point2D, thickness: f32, color: Color)

Draw a line between two points.

Source

pub fn image(&self, rect: Rect, data: &[u8])

Draw an image from encoded bytes (PNG, JPEG, GIF, WebP).

Source

pub fn linear_gradient(&self, rect: Rect, stops: &[GradientStop])

Fill a rectangle with a linear gradient.

Source

pub fn radial_gradient(&self, rect: Rect, stops: &[GradientStop])

Fill a rectangle with a radial gradient.

Source

pub fn save(&self)

Push the current transform/clip/opacity state.

Source

pub fn restore(&self)

Restore the most recently saved state.

Source

pub fn translate(&self, tx: f32, ty: f32)

Apply a 2D translation to subsequent draw commands.

Source

pub fn rotate(&self, angle: f32)

Apply a 2D rotation (radians) to subsequent draw commands.

Source

pub fn scale(&self, sx: f32, sy: f32)

Apply a uniform scale to subsequent draw commands.

Source

pub fn transform(&self, a: f32, b: f32, c: f32, d: f32, tx: f32, ty: f32)

Apply a full 2D affine transform.

Source

pub fn clip(&self, rect: Rect)

Intersect the current clip with a rectangle.

Source

pub fn set_opacity(&self, alpha: f32)

Set layer opacity (0.0–1.0) for subsequent draw commands.

Source

pub fn dimensions(&self) -> (u32, u32)

Get the canvas dimensions in pixels.

Source

pub fn width(&self) -> u32

Get the canvas width in pixels.

Source

pub fn height(&self) -> u32

Get the canvas height in pixels.

Trait Implementations§

Source§

impl Default for Canvas

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.