Skip to main content

DrawContext

Struct DrawContext 

Source
pub struct DrawContext { /* private fields */ }
Expand description

Immediate-mode drawing context used by canvas(size, draw).

Implementations§

Source§

impl DrawContext

Source

pub fn bounds(&self) -> Bounds<Pixels>

Returns the canvas-local bounds for the current draw pass.

Source

pub fn size(&self) -> Size<Pixels>

Returns the canvas-local size for the current draw pass.

Source

pub fn command_count(&self) -> usize

Number of queued draw commands that have not been flushed to the window.

Source

pub fn is_empty(&self) -> bool

Whether there are no queued draw commands.

Source

pub fn reserve_commands(&mut self, additional: usize)

Reserve command storage for a known batch size.

Real-time canvases should call this before emitting large, similarly sized batches to avoid growing the command buffer during a frame.

Source

pub fn path_count(&self) -> usize

Number of queued path commands.

Source

pub fn quad_count(&self) -> usize

Number of queued quad commands.

Source

pub fn filled_quad_count(&self) -> usize

Number of queued filled quad commands.

Source

pub fn stroked_quad_count(&self) -> usize

Number of queued stroked quad commands.

Source

pub fn text_count(&self) -> usize

Number of queued text commands.

Source

pub fn image_count(&self) -> usize

Number of queued image commands.

Source

pub fn state_stack_depth(&self) -> usize

Number of saved drawing states waiting to be restored.

Source

pub fn to_text(&self) -> String

Content-safe summary of queued canvas drawing work.

Source

pub fn fill_path(&mut self, path: &Path<Pixels>, fill: impl Into<Background>)

Fill an existing path with the given background.

Source

pub fn stroke_path(&mut self, path: &Path<Pixels>, stroke: Stroke)

Stroke a path using the retained source outline stored on the path.

Source

pub fn stroke_line( &mut self, from: Point<Pixels>, to: Point<Pixels>, stroke: Stroke, )

Stroke a straight line segment between two points.

Source

pub fn fill_rect(&mut self, bounds: Bounds<Pixels>, fill: impl Into<Background>)

Fill an axis-aligned rectangle.

Source

pub fn fill_rects( &mut self, rects: impl IntoIterator<Item = (Bounds<Pixels>, Background)>, )

Queue a batch of axis-aligned rectangles with one command-buffer reservation.

Source

pub fn fill_rounded_rect( &mut self, bounds: Bounds<Pixels>, radii: impl Into<Corners<Pixels>>, fill: impl Into<Background>, )

Fill a rounded rectangle.

Source

pub fn stroke_rect(&mut self, bounds: Bounds<Pixels>, stroke: Stroke)

Stroke the outline of an axis-aligned rectangle (web canvas strokeRect).

Source

pub fn stroke_rounded_rect( &mut self, bounds: Bounds<Pixels>, radii: impl Into<Corners<Pixels>>, stroke: Stroke, )

Stroke the outline of a rounded rectangle.

Source

pub fn fill_circle( &mut self, center: Point<Pixels>, radius: Pixels, fill: impl Into<Background>, )

Fill a circle.

Source

pub fn fill_circles( &mut self, circles: impl IntoIterator<Item = (Point<Pixels>, Pixels, Background)>, )

Queue a batch of filled circles through the renderer’s rounded-quad fast path.

Source

pub fn stroke_circle( &mut self, center: Point<Pixels>, radius: Pixels, stroke: Stroke, )

Stroke a circle.

Source

pub fn fill_ellipse( &mut self, center: Point<Pixels>, radius_x: Pixels, radius_y: Pixels, fill: impl Into<Background>, )

Fill an ellipse centered at center with the given horizontal and vertical radii.

Source

pub fn stroke_ellipse( &mut self, center: Point<Pixels>, radius_x: Pixels, radius_y: Pixels, stroke: Stroke, )

Stroke an ellipse centered at center with the given horizontal and vertical radii.

Source

pub fn with_transform<R>( &mut self, matrix: TransformationMatrix, f: impl FnOnce(&mut Self) -> R, ) -> R

Apply an affine transform to commands issued within the callback.

Source

pub fn with_opacity<R>( &mut self, opacity: f32, f: impl FnOnce(&mut Self) -> R, ) -> R

Multiply the current drawing opacity for commands issued within the callback.

Source

pub fn with_clip<R>( &mut self, bounds: Bounds<Pixels>, f: impl FnOnce(&mut Self) -> R, ) -> R

Restrict drawing to an additional clip rectangle.

Source

pub fn save(&mut self)

Save the current drawing state (transform, opacity, clip) onto the state stack.

Mirrors CanvasRenderingContext2D.save(). Pair every save with a matching DrawContext::restore. This is the flat alternative to the scoped DrawContext::with_transform / DrawContext::with_opacity / DrawContext::with_clip helpers, for loops and recursion where closures are awkward.

Source

pub fn restore(&mut self)

Restore the most recently saved drawing state. No-op if the stack is empty.

Mirrors CanvasRenderingContext2D.restore().

Source

pub fn translate(&mut self, x: Pixels, y: Pixels)

Translate the current transform by the given offset, in canvas-local pixels.

Source

pub fn rotate(&mut self, radians: f32)

Rotate the current transform clockwise by the given angle in radians.

Source

pub fn scale(&mut self, x: f32, y: f32)

Scale the current transform by independent x and y factors.

Source

pub fn set_global_alpha(&mut self, alpha: f32)

Set the global drawing opacity applied to subsequent commands (clamped to 0.0..=1.0).

Mirrors CanvasRenderingContext2D.globalAlpha. Saved and restored by DrawContext::save / DrawContext::restore.

Source

pub fn clip_rect(&mut self, bounds: Bounds<Pixels>)

Restrict subsequent drawing to the given rectangle, intersected with any existing clip. The flat counterpart to DrawContext::with_clip.

Source

pub fn draw_text( &mut self, text: &ShapedLine, origin: Point<Pixels>, color: Hsla, )

Draw a shaped line of text at the given local origin.

Source

pub fn draw_text_aligned( &mut self, text: &ShapedLine, origin: Point<Pixels>, color: Hsla, align: TextAlign, )

Draw a shaped line of text anchored horizontally at origin per align, mirroring the web canvas textAlign (origin.x is the left/center/right anchor).

Source

pub fn draw_image(&mut self, image: Arc<RenderImage>, bounds: Bounds<Pixels>)

Draw an image filling the given rectangle, respecting the current transform, clip, and opacity. Mirrors CanvasRenderingContext2D.drawImage.

Source

pub fn draw_image_rounded( &mut self, image: Arc<RenderImage>, bounds: Bounds<Pixels>, corner_radii: impl Into<Corners<Pixels>>, )

Draw an image filling the given rectangle with rounded corners.

Source

pub fn draw_image_frame( &mut self, image: Arc<RenderImage>, bounds: Bounds<Pixels>, frame_index: usize, grayscale: bool, )

Draw a single frame of a multi-frame image (e.g. an animated GIF), optionally desaturated.

Source

pub fn flush(&mut self, window: &mut Window, _cx: &mut App)

Flush any queued draw commands into the window.

Call this when mixing DrawContext drawing with direct Window painting and you need exact interleaving order.

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = !

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more