Skip to main content

Canvas2dContext

Struct Canvas2dContext 

Source
pub struct Canvas2dContext {
    pub surface: Surface,
    pub path: PathBuilder,
    pub state: DrawState,
    stack: Vec<DrawState>,
}
Expand description

1 つの <canvas> に対する 2D コンテキストの全状態。

JS 側のオブジェクトは軽いハンドルだけを持ち、実体はこれを指す。 パスと面はどちらもここに持たせて、描画の途中経過が JS オブジェクトの寿命に左右されないようにする。

Fields§

§surface: Surface§path: PathBuilder§state: DrawState§stack: Vec<DrawState>

save() の退避スタック。

Implementations§

Source§

impl Canvas2dContext

Source

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

Source

pub fn save(&mut self)

現在の状態を退避する。上限を超えたら黙って無視する (restore との対応が崩れるが、落ちるよりはよい)。

Source

pub fn restore(&mut self)

直近の退避状態へ戻す。スタックが空なら何もしない (仕様どおり。restore の呼び過ぎはエラーではない)。

Source

pub fn apply_alpha(&self, argb: u32) -> u32

globalAlpha を適用した色を返す。

アルファは掛け算で合成する。globalAlpha=0.5 の半透明色は さらに薄くなる(仕様どおり)。

Source

pub fn fill_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)

矩形を透明で消す。globalAlpha の影響は受けない(仕様どおり)。

回転が入っていると軸平行に消せないため、パスの外接矩形を消す。 厳密ではないが、clearRect に回転を掛ける使い方は稀。

Source

pub fn fill(&mut self, rule: FillRule)

現在のパスを塗る。

Source

pub fn stroke(&mut self)

現在のパスを線描する。

Source

pub fn translate(&mut self, tx: f32, ty: f32)

現在の変換へ平行移動を積む。

Source

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

現在の変換へ拡大縮小を積む。

Source

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

現在の変換へ回転を積む。

Source

pub fn set_transform(&mut self, m: Matrix)

変換行列を直接置き換える(setTransform)。

Source

pub fn reset_transform(&mut self)

変換を単位行列へ戻す(resetTransform)。

Source§

impl Canvas2dContext

Source

pub fn stroke_with_width(&mut self)

現在のパスを、lineWidth を反映して線描する。

Source§

impl Canvas2dContext

Source

pub fn apply_dash(&self, subpaths: &[SubPath]) -> Vec<SubPath>

現在の破線設定をパスへ適用する。実線ならそのまま返す。

Source§

impl Canvas2dContext

Source

pub fn clip(&mut self, rule: FillRule)

現在のパスでクリップ領域を狭める(clip())。

save/restore で元に戻る。状態を複製するだけで復帰できるよう Rc で共有しており、色と違って正しく戻せる

Source

pub fn sync_clip(&mut self)

描画前に、現在のクリップ領域を Surface へ渡す。

Trait Implementations§

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

Source§

type Output = T

Should always be Self
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.