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: f32Implementations§
Source§impl Canvas2DContext
impl Canvas2DContext
pub fn new(width: f32, height: f32) -> Self
pub fn save(&mut self)
pub fn restore(&mut self)
pub fn translate(&mut self, dx: f32, dy: f32)
pub fn scale(&mut self, sx: f32, sy: f32)
pub fn rotate(&mut self, radians: f32)
pub fn transform(&mut self, a: f32, b: f32, c: f32, d: f32, e: f32, f: f32)
pub fn set_transform(&mut self, transform: Transform2D)
pub fn get_transform(&self) -> Transform2D
pub fn reset_transform(&mut self)
pub fn set_fill_style(&mut self, color: Color)
pub fn set_stroke_style(&mut self, color: Color)
pub fn set_line_width(&mut self, width: f32)
Sourcepub fn set_line_cap(&mut self, cap: CanvasLineCap)
pub fn set_line_cap(&mut self, cap: CanvasLineCap)
Sets the stroke line termination cap style (Butt, Round, or Square).
Sourcepub fn line_cap(&self) -> CanvasLineCap
pub fn line_cap(&self) -> CanvasLineCap
Returns the currently active stroke line cap style.
Sourcepub fn set_line_join(&mut self, join: CanvasLineJoin)
pub fn set_line_join(&mut self, join: CanvasLineJoin)
Sets the stroke corner join style (Miter, Round, or Bevel).
Sourcepub fn line_join(&self) -> CanvasLineJoin
pub fn line_join(&self) -> CanvasLineJoin
Returns the currently active stroke corner join style.
pub fn set_global_alpha(&mut self, alpha: f32)
Sourcepub fn set_line_dash(&mut self, segments: &[f32])
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.
Sourcepub fn get_line_dash(&self) -> &[f32]
pub fn get_line_dash(&self) -> &[f32]
Returns the currently active stroke dash pattern.
Sourcepub fn set_line_dash_offset(&mut self, offset: f32)
pub fn set_line_dash_offset(&mut self, offset: f32)
Sets the stroke dash phase offset (ctx.lineDashOffset).
Sourcepub fn line_dash_offset(&self) -> f32
pub fn line_dash_offset(&self) -> f32
Returns the current stroke dash phase offset.
Sourcepub fn set_font(&mut self, font: impl Into<String>)
pub fn set_font(&mut self, font: impl Into<String>)
Sets the active CSS font shorthand (e.g. "bold 12px monospace").
Sourcepub fn set_text_align(&mut self, align: CanvasTextAlign)
pub fn set_text_align(&mut self, align: CanvasTextAlign)
Sets horizontal text alignment (Left, Center, Right, Start, End).
Sourcepub fn set_text_baseline(&mut self, baseline: CanvasTextBaseline)
pub fn set_text_baseline(&mut self, baseline: CanvasTextBaseline)
Sets vertical text baseline (Alphabetic, Top, Middle, Bottom, Hanging, Ideographic).
Sourcepub fn measure_text(&self, text: &str) -> CanvasTextMetrics
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).
pub fn begin_path(&mut self)
pub fn move_to(&mut self, x: f32, y: f32)
pub fn line_to(&mut self, x: f32, y: f32)
Sourcepub fn quadratic_curve_to(&mut self, cpx: f32, cpy: f32, x: f32, y: f32)
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).
Sourcepub fn bezier_curve_to(
&mut self,
cp1x: f32,
cp1y: f32,
cp2x: f32,
cp2y: f32,
x: f32,
y: f32,
)
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).
pub fn arc( &mut self, cx: f32, cy: f32, radius: f32, start_angle: f32, end_angle: f32, ccw: bool, )
pub fn rect(&mut self, x: f32, y: f32, w: f32, h: f32)
Sourcepub fn round_rect(
&mut self,
x: f32,
y: f32,
w: f32,
h: f32,
radii: impl IntoRoundRectRadii,
)
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.
pub fn close_path(&mut self)
Sourcepub fn clip(&mut self)
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.
pub fn fill_rect(&mut self, x: f32, y: f32, w: f32, h: f32)
pub fn stroke_rect(&mut self, x: f32, y: f32, w: f32, h: f32)
pub fn clear_rect(&mut self, x: f32, y: f32, w: f32, h: f32)
pub fn stroke(&mut self)
pub fn fill(&mut self)
Sourcepub fn fill_text(&mut self, text: impl Into<String>, x: f32, y: f32)
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.
Sourcepub fn fill_text_with_max_width(
&mut self,
text: impl Into<String>,
x: f32,
y: f32,
max_width: f32,
)
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.
Sourcepub fn stroke_text(&mut self, text: impl Into<String>, x: f32, y: f32)
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.
Sourcepub fn stroke_text_with_max_width(
&mut self,
text: impl Into<String>,
x: f32,
y: f32,
max_width: f32,
)
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.
Sourcepub fn finish(self) -> Vec<CanvasCommand>
pub fn finish(self) -> Vec<CanvasCommand>
Emits all recorded canvas commands.