Canvas

Struct Canvas 

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

Canvas 绘制上下文

提供在图片表面上进行各种绘制操作的能力

Implementations§

Source§

impl Canvas

Source

pub fn new(width: u32, height: u32) -> Result<Self>

创建新的 Canvas

Source

pub fn new_with_color(width: u32, height: u32, color: Color) -> Result<Self>

创建指定背景色的 Canvas

Source

pub fn from_surface(surface: Surface) -> Self

从已有的 Surface 创建 Canvas

Source

pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self>

从文件加载图片并创建 Canvas

Source

pub fn width(&self) -> u32

获取 Canvas 宽度

Source

pub fn height(&self) -> u32

获取 Canvas 高度

Source

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

获取 Canvas 尺寸

Source

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

清空 Canvas 为指定颜色

Source

pub fn save_to_file<P: AsRef<Path>>(&self, path: P) -> Result<()>

保存到文件

Source

pub fn to_png_bytes(&self) -> Result<Vec<u8>>

导出为 PNG 字节数据

Source

pub fn to_jpeg_bytes(&self, quality: u8) -> Result<Vec<u8>>

导出为 JPEG 字节数据

Source

pub fn surface(&self) -> &Surface

获取底层 Surface

Source

pub fn surface_mut(&mut self) -> &mut Surface

获取可变的底层 Surface

Source

pub fn draw_point(&mut self, point: Point, paint: &Paint)

绘制点

Source

pub fn draw_line(&mut self, start: Point, end: Point, paint: &Paint)

绘制线段

Source

pub fn draw_rect(&mut self, rect: Rect, paint: &Paint)

绘制矩形

Source

pub fn draw_rounded_rect(&mut self, rect: Rect, radius: f32, paint: &Paint)

绘制圆角矩形

Source

pub fn draw_circle(&mut self, center: Point, radius: f32, paint: &Paint)

绘制圆形

Source

pub fn draw_oval(&mut self, rect: Rect, paint: &Paint)

绘制椭圆

Source

pub fn draw_text( &mut self, text: &str, position: Point, text_style: &TextStyle, paint: &Paint, )

绘制文本

Source

pub fn draw_image(&mut self, image: &Surface, position: Point) -> Result<()>

绘制图片

Source

pub fn draw_image_rect( &mut self, image: &Surface, src_rect: Option<Rect>, dst_rect: Rect, ) -> Result<()>

绘制图片的指定区域到指定位置

Source

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  │
│ (固定)  │ (水平拉) │ (固定)  │
└─────────┴──────────┴─────────┘
Source

pub fn measure_text(&self, text: &str, text_style: &TextStyle) -> (u32, u32)

测量文本尺寸

Source

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
);
Source

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
);
Source

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
);
Source

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);
Source

pub fn draw_path(&mut self, path: &Path, paint: &Paint)

绘制路径

根据 Paint 的样式绘制路径(填充或描边)

§参数
  • path - 要绘制的路径
  • paint - 绘制样式
Source

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

平移变换

将后续的绘制操作沿 x 和 y 方向平移

§参数
  • dx - x 方向的平移量
  • dy - y 方向的平移量
Source

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

缩放变换

将后续的绘制操作进行缩放

§参数
  • sx - x 方向的缩放因子
  • sy - y 方向的缩放因子
Source

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

旋转变换

将后续的绘制操作绕原点旋转指定角度

§参数
  • degrees - 旋转角度(度数)
Source

pub fn rotate_around(&mut self, degrees: f32, px: f32, py: f32)

绕指定点旋转

§参数
  • degrees - 旋转角度(度数)
  • px - 旋转中心的 x 坐标
  • py - 旋转中心的 y 坐标
Source

pub fn set_matrix(&mut self, matrix: &Matrix)

设置变换矩阵

直接设置当前的变换矩阵

§参数
  • matrix - 新的变换矩阵
Source

pub fn get_matrix(&self) -> &Matrix

获取当前变换矩阵

Source

pub fn concat(&mut self, matrix: &Matrix)

连接变换矩阵

将指定矩阵与当前矩阵相乘

§参数
  • matrix - 要连接的矩阵
Source

pub fn reset_matrix(&mut self)

重置变换矩阵为单位矩阵

Source

pub fn save(&mut self)

保存当前的绘制状态

将当前的变换矩阵和裁剪区域压入状态栈

Source

pub fn restore(&mut self)

恢复之前保存的绘制状态

从状态栈中弹出并恢复变换矩阵和裁剪区域

Source

pub fn get_save_count(&self) -> usize

获取当前保存的状态层数

Source

pub fn restore_to_count(&mut self, count: usize)

恢复到指定的保存层级

§参数
  • count - 目标层级数
Source

pub fn clip_rect(&mut self, rect: Rect, op: ClipOp)

矩形裁剪

将绘制区域限制在指定的矩形内

§参数
  • rect - 裁剪矩形
  • op - 裁剪操作(相交或差集)
Source

pub fn clip_path(&mut self, path: &Path, op: ClipOp)

路径裁剪

将绘制区域限制在指定的路径内

§参数
  • path - 裁剪路径
  • op - 裁剪操作(相交或差集)
Source

pub fn get_clip_bounds(&self) -> Option<Rect>

获取当前裁剪区域的边界

Source

pub fn is_clipped(&self, x: i32, y: i32) -> bool

检查点是否在裁剪区域内

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<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<Src, Scheme> ApproxFrom<Src, Scheme> for Src
where Scheme: ApproxScheme,

Source§

type Err = NoError

The error type produced by a failed conversion.
Source§

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 Src
where Dst: ApproxFrom<Src, Scheme>, Scheme: ApproxScheme,

Source§

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>

Convert the subject into an approximately equivalent representation.
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, Dst> ConvAsUtil<Dst> for T

Source§

fn approx(self) -> Result<Dst, Self::Err>
where Self: Sized + ApproxInto<Dst>,

Approximate the subject with the default scheme.
Source§

fn approx_by<Scheme>(self) -> Result<Dst, Self::Err>
where Self: Sized + ApproxInto<Dst, Scheme>, Scheme: ApproxScheme,

Approximate the subject with a specific scheme.
Source§

impl<T> ConvUtil for T

Source§

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>
where Self: Sized + ApproxInto<Dst, Scheme>, Scheme: ApproxScheme,

Approximate the subject to a given type with a specific scheme.
Source§

fn into_as<Dst>(self) -> Dst
where Self: Sized + Into<Dst>,

Convert the subject to a given type.
Source§

fn try_as<Dst>(self) -> Result<Dst, Self::Err>
where Self: Sized + TryInto<Dst>,

Attempt to convert the subject to a given type.
Source§

fn value_as<Dst>(self) -> Result<Dst, Self::Err>
where Self: Sized + ValueInto<Dst>,

Attempt a value conversion of the subject to a given type.
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> 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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

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

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<Src> TryFrom<Src> for Src

Source§

type Err = NoError

The error type produced by a failed conversion.
Source§

fn try_from(src: Src) -> Result<Src, <Src as TryFrom<Src>>::Err>

Convert the given value into the subject type.
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<Src, Dst> TryInto<Dst> for Src
where Dst: TryFrom<Src>,

Source§

type Err = <Dst as TryFrom<Src>>::Err

The error type produced by a failed conversion.
Source§

fn try_into(self) -> Result<Dst, <Src as TryInto<Dst>>::Err>

Convert the subject into the destination type.
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<Src> ValueFrom<Src> for Src

Source§

type Err = NoError

The error type produced by a failed conversion.
Source§

fn value_from(src: Src) -> Result<Src, <Src as ValueFrom<Src>>::Err>

Convert the given value into an exactly equivalent representation.
Source§

impl<Src, Dst> ValueInto<Dst> for Src
where Dst: ValueFrom<Src>,

Source§

type Err = <Dst as ValueFrom<Src>>::Err

The error type produced by a failed conversion.
Source§

fn value_into(self) -> Result<Dst, <Src as ValueInto<Dst>>::Err>

Convert the subject into an exactly equivalent representation.