Skip to main content

Canvas2DContext

Struct Canvas2DContext 

Source
pub struct Canvas2DContext {
    pub width: f32,
    pub height: f32,
    /* private fields */
}
Expand description

W3C-Compatible 2D Immediate-Mode Canvas Rendering Context.

Fields§

§width: f32§height: f32

Implementations§

Source§

impl Canvas2DContext

Source

pub fn new(width: f32, height: f32) -> Self

Source

pub fn save(&mut self)

Source

pub fn restore(&mut self)

Source

pub fn translate(&mut self, dx: f32, dy: f32)

Source

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

Source

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

Source

pub fn transform(&mut self, a: f32, b: f32, c: f32, d: f32, e: f32, f: f32)

Source

pub fn set_transform(&mut self, transform: Transform2D)

Source

pub fn get_transform(&self) -> Transform2D

Source

pub fn reset_transform(&mut self)

Source

pub fn set_fill_style(&mut self, color: Color)

Source

pub fn set_stroke_style(&mut self, color: Color)

Source

pub fn set_line_width(&mut self, width: f32)

Source

pub fn set_line_cap(&mut self, cap: CanvasLineCap)

Sets the stroke line termination cap style (Butt, Round, or Square).

Source

pub fn line_cap(&self) -> CanvasLineCap

Returns the currently active stroke line cap style.

Source

pub fn set_line_join(&mut self, join: CanvasLineJoin)

Sets the stroke corner join style (Miter, Round, or Bevel).

Source

pub fn line_join(&self) -> CanvasLineJoin

Returns the currently active stroke corner join style.

Source

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

Source

pub fn set_line_dash(&mut self, segments: &[f32])

Sets the stroke dash pattern (HTML5 ctx.setLineDash(segments)).

Negative or non-finite values cause the call to be ignored per the HTML5 Canvas 2D spec; an odd-length pattern is automatically concatenated with itself so dash/gap pairs repeat cleanly; an empty slice restores solid strokes.

Source

pub fn get_line_dash(&self) -> &[f32]

Returns the currently active stroke dash pattern.

Source

pub fn set_line_dash_offset(&mut self, offset: f32)

Sets the stroke dash phase offset (ctx.lineDashOffset).

Source

pub fn line_dash_offset(&self) -> f32

Returns the current stroke dash phase offset.

Source

pub fn set_font(&mut self, font: impl Into<String>)

Sets the active CSS font shorthand (e.g. "bold 12px monospace").

Source

pub fn font(&self) -> &str

Returns the active CSS font string.

Source

pub fn set_text_align(&mut self, align: CanvasTextAlign)

Sets horizontal text alignment (Left, Center, Right, Start, End).

Source

pub fn set_text_baseline(&mut self, baseline: CanvasTextBaseline)

Sets vertical text baseline (Alphabetic, Top, Middle, Bottom, Hanging, Ideographic).

Source

pub fn measure_text(&self, text: &str) -> CanvasTextMetrics

Measures text using the active Self::set_font styling and registered platform text measurer (PangoCairo when available, OpenType/inline shaping fallback headlessly).

Source

pub fn begin_path(&mut self)

Source

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

Source

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

Source

pub fn quadratic_curve_to(&mut self, cpx: f32, cpy: f32, x: f32, y: f32)

Appends a quadratic Bézier curve from the current point to (x, y) using control point (cpx, cpy).

Source

pub fn bezier_curve_to( &mut self, cp1x: f32, cp1y: f32, cp2x: f32, cp2y: f32, x: f32, y: f32, )

Appends a cubic Bézier curve from the current point to (x, y) using control points (cp1x, cp1y) and (cp2x, cp2y).

Source

pub fn arc( &mut self, cx: f32, cy: f32, radius: f32, start_angle: f32, end_angle: f32, ccw: bool, )

Source

pub fn rect(&mut self, x: f32, y: f32, w: f32, h: f32)

Source

pub fn round_rect( &mut self, x: f32, y: f32, w: f32, h: f32, radii: impl IntoRoundRectRadii, )

Adds a rounded rectangle subpath (x, y, w, h) with corner radii (uniform f32, 1–4 element slice/array [tl, tr, br, bl], or BorderRadius) per the HTML5 Canvas 2D roundRect specification.

Source

pub fn close_path(&mut self)

Source

pub fn clip(&mut self)

Intersects the current clipping region with the current path.

Wrap with Self::save and Self::restore to scope the clip region.

Source

pub fn fill_rect(&mut self, x: f32, y: f32, w: f32, h: f32)

Source

pub fn stroke_rect(&mut self, x: f32, y: f32, w: f32, h: f32)

Source

pub fn clear_rect(&mut self, x: f32, y: f32, w: f32, h: f32)

Source

pub fn stroke(&mut self)

Source

pub fn fill(&mut self)

Source

pub fn fill_text(&mut self, text: impl Into<String>, x: f32, y: f32)

Renders filled text at (x, y) using the active font, fill_style, text_align, and text_baseline.

Source

pub fn fill_text_with_max_width( &mut self, text: impl Into<String>, x: f32, y: f32, max_width: f32, )

Renders filled text at (x, y), horizontally constrained to max_width pixels.

Source

pub fn stroke_text(&mut self, text: impl Into<String>, x: f32, y: f32)

Renders stroked/outlined text at (x, y) using the active font, stroke_style, and line_width.

Source

pub fn stroke_text_with_max_width( &mut self, text: impl Into<String>, x: f32, y: f32, max_width: f32, )

Renders stroked/outlined text at (x, y), horizontally constrained to max_width pixels.

Source

pub fn finish(self) -> Vec<CanvasCommand>

Emits all recorded canvas commands.

Source

pub fn commands(&self) -> &[CanvasCommand]

Source

pub fn rasterize( &self, fb: &mut FrameBuffer, origin_x: f32, origin_y: f32, clip: Option<Rect>, )

Software analytical anti-aliased rasterization directly onto a FrameBuffer.

Trait Implementations§

Source§

impl Clone for Canvas2DContext

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Canvas2DContext

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

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> IntoInitialState<T> for T

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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, !>

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<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,