Skip to main content

Canvas

Struct Canvas 

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

像素级自由画布(内部 tiny_skia::Pixmap,逻辑坐标 × scale)。

Implementations§

Source§

impl Canvas

Source

pub fn new(w: f32, h: f32, scale: f32) -> Result<Canvas>

新建透明底画布:逻辑 w×h,物理尺寸 = 逻辑 × scale

Source

pub fn scale(&self) -> f32

超采样系数。

Source

pub fn width_px(&self) -> u32

物理宽(像素)。

Source

pub fn height_px(&self) -> u32

物理高(像素)。

Source

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

整张填充某色(铺底)。

Source

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

实心(可圆角)矩形。

Source

pub fn stroke_rect( &mut self, x: f32, y: f32, w: f32, h: f32, radius: f32, line_w: f32, color: Color, )

描边(可圆角)矩形。线宽沿路径居中。

Source

pub fn v_gradient( &mut self, x: f32, y: f32, w: f32, h: f32, radius: f32, top: Color, bottom: Color, )

竖直线性渐变的(可圆角)矩形:顶 top → 底 bottom

Source

pub fn line( &mut self, x0: f32, y0: f32, x1: f32, y1: f32, line_w: f32, color: Color, )

直线段(圆头)。

Source

pub fn disc(&mut self, cx: f32, cy: f32, r: f32, color: Color)

实心圆。

Source

pub fn ring(&mut self, cx: f32, cy: f32, r: f32, line_w: f32, color: Color)

描边圆环。

Source

pub fn arc( &mut self, cx: f32, cy: f32, r: f32, start_deg: f32, sweep_deg: f32, line_w: f32, color: Color, )

圆弧(圆头描边):从 start_deg 起、扫过 sweep_deg(度,0=右、顺时针为正)。用折线逼近, 适合做环形进度 / 仪表。

Source

pub fn polygon( &mut self, pts: &[(f32, f32)], fill: Option<Color>, stroke: Option<(f32, Color)>, )

多边形:pts 为逻辑坐标顶点;可填充、可描边(或都给)。

Source

pub fn radar(&mut self, cx: f32, cy: f32, r: f32, values: &[f32], st: &Radar)

雷达图:以 (cx, cy) 为心、r 为外接半径,values(各轴 0..1)画数据多边形 + 网格。

Source

pub fn text( &mut self, x: f32, y: f32, box_w: f32, opts: &RenderOptions, build: impl FnOnce(&mut ParaBuilder), ) -> Result<f32>

把一段样式化段落渲成透明底小图并合成到 (x, y),占位宽 box_w(逻辑像素;段落对齐在此宽内生效)。 返回该文本盒的渲染高度(逻辑像素),便于纵向流式排版。复用整条版式管线(CJK 整形 / 抗锯齿白拿)。

Source

pub fn text_doc( &mut self, x: f32, y: f32, box_w: f32, opts: &RenderOptions, doc: &Document, ) -> Result<f32>

text,但直接给一份 Document(可多段 / 表格等)。

Source

pub fn text_mid( &mut self, x: f32, cy: f32, box_w: f32, opts: &RenderOptions, build: impl FnOnce(&mut ParaBuilder), ) -> Result<f32>

text,但把文字的实际墨迹纵向居中于中线 cy(逻辑像素),x 仍是左缘。给 「标签 / 数值 / 进度条 / 圆牌同一行居中」的卡片行用——按行盒居中会偏高(行盒底部留白多),故取首末非透明 行的中点对齐 cy,文字与同心线上的形状才真正齐平,不同字号也一致。

返回墨迹右缘相对 x 的逻辑宽度(全透明返回 0):据此把下一段接着往右摆,且每段各自居中于同一 cy, 不会因不同字号共用基线而让小字下沉。

Source

pub fn blit(&mut self, img: &RgbaImage, px: i32, py: i32)

把一张 RGBA 图按物理像素坐标 (px, py) 直接合成(source-over,不缩放)。

Source

pub fn encode(&self, format: OutputFormat) -> Result<Vec<u8>>

format 编码为图片字节。

Source

pub fn into_rgba(self) -> Result<RgbaImage>

取(去预乘的)RGBA 图,供进一步合成。

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> ErasedDestructor for T
where T: 'static,

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, 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<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.