pub struct Canvas { /* private fields */ }Expand description
Canvas 绘制上下文
提供在图片表面上进行各种绘制操作的能力
Implementations§
Source§impl Canvas
impl Canvas
Sourcepub fn from_surface(surface: Surface) -> Self
pub fn from_surface(surface: Surface) -> Self
从已有的 Surface 创建 Canvas
Sourcepub fn dimensions(&self) -> (u32, u32)
pub fn dimensions(&self) -> (u32, u32)
获取 Canvas 尺寸
Sourcepub fn to_png_bytes(&self) -> Result<Vec<u8>>
pub fn to_png_bytes(&self) -> Result<Vec<u8>>
导出为 PNG 字节数据
Sourcepub fn surface_mut(&mut self) -> &mut Surface
pub fn surface_mut(&mut self) -> &mut Surface
获取可变的底层 Surface
Sourcepub fn draw_point(&mut self, point: Point, paint: &Paint)
pub fn draw_point(&mut self, point: Point, paint: &Paint)
绘制点
Sourcepub fn draw_rounded_rect(&mut self, rect: Rect, radius: f32, paint: &Paint)
pub fn draw_rounded_rect(&mut self, rect: Rect, radius: f32, paint: &Paint)
绘制圆角矩形
Sourcepub fn draw_circle(&mut self, center: Point, radius: f32, paint: &Paint)
pub fn draw_circle(&mut self, center: Point, radius: f32, paint: &Paint)
绘制圆形
Sourcepub fn draw_text(
&mut self,
text: &str,
position: Point,
text_style: &TextStyle,
paint: &Paint,
)
pub fn draw_text( &mut self, text: &str, position: Point, text_style: &TextStyle, paint: &Paint, )
绘制文本
Sourcepub fn draw_image_rect(
&mut self,
image: &Surface,
src_rect: Option<Rect>,
dst_rect: Rect,
) -> Result<()>
pub fn draw_image_rect( &mut self, image: &Surface, src_rect: Option<Rect>, dst_rect: Rect, ) -> Result<()>
绘制图片的指定区域到指定位置
Sourcepub fn draw_image_nine(
&mut self,
image: &Surface,
center: Rect,
dst: Rect,
) -> Result<()>
pub fn draw_image_nine( &mut self, image: &Surface, center: Rect, dst: Rect, ) -> Result<()>
九宫格绘制图片(类似 skia-safe 的 draw_image_nine)
将图片分割成 3x3 的九宫格,智能拉伸以适应目标区域。 四个角落保持不变,边缘单向拉伸,中心区域双向拉伸。
§参数
image- 要绘制的图片center- 中心矩形区域,定义九宫格的分割方式dst- 目标绘制区域
§九宫格分割示例
┌─────────┬──────────┬─────────┐
│ 角落1 │ 上边缘 │ 角落2 │
│ (固定) │ (水平拉) │ (固定) │
├─────────┼──────────┼─────────┤
│ 左边缘 │ 中心 │ 右边缘 │
│ (垂直拉)│ (双向拉) │ (垂直拉)│
├─────────┼──────────┼─────────┤
│ 角落3 │ 下边缘 │ 角落4 │
│ (固定) │ (水平拉) │ (固定) │
└─────────┴──────────┴─────────┘Sourcepub fn draw_dashed_line(&mut self, start: Point, end: Point, paint: &Paint)
pub fn draw_dashed_line(&mut self, start: Point, end: Point, paint: &Paint)
绘制虚线
根据 Paint 中的虚线样式绘制虚线。如果虚线样式为 Solid,则绘制实线。
§参数
start- 起点end- 终点paint- 绘制样式(包含虚线模式)
§示例
use image_renderer::{Canvas, Color, Paint, Point, paint::DashStyle};
let mut canvas = Canvas::new(400, 300).unwrap();
let mut paint = Paint::stroke(Color::BLUE, 2.0);
paint.set_dash_style(DashStyle::ShortDash);
canvas.draw_dashed_line(
Point::new(50, 100),
Point::new(350, 100),
&paint
);Sourcepub fn draw_dashed_rect(&mut self, rect: Rect, paint: &Paint)
pub fn draw_dashed_rect(&mut self, rect: Rect, paint: &Paint)
绘制虚线矩形
根据 Paint 中的虚线样式绘制矩形边框。如果虚线样式为 Solid,则绘制实线矩形。
§参数
rect- 矩形区域paint- 绘制样式(包含虚线模式)
§示例
use image_renderer::{Canvas, Color, Paint, Rect, paint::DashStyle};
let mut canvas = Canvas::new(400, 300).unwrap();
let mut paint = Paint::stroke(Color::RED, 2.0);
paint.set_dash_style(DashStyle::DashDot);
canvas.draw_dashed_rect(
Rect::new(50, 50, 300, 200),
&paint
);Sourcepub fn draw_dashed_rounded_rect(
&mut self,
rect: Rect,
radius: f32,
paint: &Paint,
)
pub fn draw_dashed_rounded_rect( &mut self, rect: Rect, radius: f32, paint: &Paint, )
绘制虚线圆角矩形
根据 Paint 中的虚线样式绘制圆角矩形边框。如果虚线样式为 Solid,则绘制实线圆角矩形。
§参数
rect- 矩形区域radius- 圆角半径paint- 绘制样式(包含虚线模式)
§示例
use image_renderer::{Canvas, Color, Paint, Rect, paint::DashStyle};
let mut canvas = Canvas::new(400, 300).unwrap();
let mut paint = Paint::stroke(Color::BLUE, 2.0);
paint.set_dash_style(DashStyle::ShortDash);
canvas.draw_dashed_rounded_rect(
Rect::new(50, 50, 300, 200),
20.0,
&paint
);Sourcepub fn draw_custom_border_rect(
&mut self,
rect: Rect,
border: &Border,
radius: &BorderRadius,
)
pub fn draw_custom_border_rect( &mut self, rect: Rect, border: &Border, radius: &BorderRadius, )
绘制自定义边框的矩形(支持每条边独立配置,每个角独立圆角)
§参数
rect- 矩形区域border- 边框配置(每条边可独立设置颜色、线宽、虚线样式)radius- 圆角配置(每个角可独立设置圆角半径)
§示例
use image_renderer::{Canvas, Color, Border, BorderSide, BorderRadius, DashStyle, Rect};
let mut canvas = Canvas::new(400, 300).unwrap();
// 创建不同样式的边框
let border = Border::new(
BorderSide::solid(Color::RED, 2.0), // 上边:红色实线
BorderSide::dashed(Color::BLUE, 3.0, DashStyle::ShortDash), // 右边:蓝色虚线
BorderSide::solid(Color::GREEN, 2.0), // 下边:绿色实线
BorderSide::dashed(Color::YELLOW, 3.0, DashStyle::Dot), // 左边:黄色点线
);
// 创建不同的圆角
let radius = BorderRadius::new(20.0, 10.0, 5.0, 15.0);
let rect = Rect::new(50, 50, 300, 200);
canvas.draw_custom_border_rect(rect, &border, &radius);Auto Trait Implementations§
impl Freeze for Canvas
impl RefUnwindSafe for Canvas
impl Send for Canvas
impl Sync for Canvas
impl Unpin for Canvas
impl UnwindSafe for Canvas
Blanket Implementations§
Source§impl<Src, Scheme> ApproxFrom<Src, Scheme> for Srcwhere
Scheme: ApproxScheme,
impl<Src, Scheme> ApproxFrom<Src, Scheme> for Srcwhere
Scheme: ApproxScheme,
Source§fn approx_from(src: Src) -> Result<Src, <Src as ApproxFrom<Src, Scheme>>::Err>
fn approx_from(src: Src) -> Result<Src, <Src as ApproxFrom<Src, Scheme>>::Err>
Convert the given value into an approximately equivalent representation.
Source§impl<Dst, Src, Scheme> ApproxInto<Dst, Scheme> for Srcwhere
Dst: ApproxFrom<Src, Scheme>,
Scheme: ApproxScheme,
impl<Dst, Src, Scheme> ApproxInto<Dst, Scheme> for Srcwhere
Dst: ApproxFrom<Src, Scheme>,
Scheme: ApproxScheme,
Source§type Err = <Dst as ApproxFrom<Src, Scheme>>::Err
type Err = <Dst as ApproxFrom<Src, Scheme>>::Err
The error type produced by a failed conversion.
Source§fn approx_into(self) -> Result<Dst, <Src as ApproxInto<Dst, Scheme>>::Err>
fn approx_into(self) -> Result<Dst, <Src as ApproxInto<Dst, Scheme>>::Err>
Convert the subject into an approximately equivalent representation.
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T, Dst> ConvAsUtil<Dst> for T
impl<T, Dst> ConvAsUtil<Dst> for T
Source§impl<T> ConvUtil for T
impl<T> ConvUtil for T
Source§fn approx_as<Dst>(self) -> Result<Dst, Self::Err>where
Self: Sized + ApproxInto<Dst>,
fn approx_as<Dst>(self) -> Result<Dst, Self::Err>where
Self: Sized + ApproxInto<Dst>,
Approximate the subject to a given type with the default scheme.
Source§fn approx_as_by<Dst, Scheme>(self) -> Result<Dst, Self::Err>
fn approx_as_by<Dst, Scheme>(self) -> Result<Dst, Self::Err>
Approximate the subject to a given type with a specific scheme.
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.