Canvas

Struct Canvas 

Source
pub struct Canvas {
    pub pixmap: Pixmap,
    pub scale: f32,
    /* private fields */
}
Expand description

A Canvas is an abstraction over a tiny-skia Pixmap that allows for drawing at different scales. This allows for using the same drawing commands to produce images of different sizes.

§Example

use wassily_core::*;
use tiny_skia::Color;

fn draw(canvas: &mut Canvas) {
    canvas.fill(Color::from_rgba8(128, 128, 128, 255));
    let center = pt(canvas.width() / 2, canvas.height() / 2);
    Shape::new()
        .ellipse(center, 300.0, 200.0)
        .fill_color(Color::from_rgba8(75, 0, 130, 255))
        .stroke_color(Color::from_rgba8(255, 165, 0, 255))
        .stroke_weight(10.0)
        .draw(canvas);
}

fn main() {
   let mut canvas1 = Canvas::new(500, 500);
    // Draw a 300x200 ellipse at the center of a 500 x 500 canvas.
    draw(&mut canvas1);

    let mut canvas2 = Canvas::with_scale(500, 500, 2.0);
    // Draw a 600x400 ellipse at the center of a 1000 x 1000 canvas. That is,
    // the exact same image as above, but, double the width and height.
    draw(&mut canvas2);

    canvas1.save_png("./scale1.png");
    canvas2.save_png("./scale2.png");
}

Fields§

§pixmap: Pixmap

The underlying tiny-skia Pixmap.

§scale: f32

The scale factor of the canvas. This is the factor by which the width and height are scaled. For example, a scale of 2.0 means that the width and height are doubled when rendering.

Implementations§

Source§

impl Canvas

Source

pub fn new(width: u32, height: u32) -> Canvas

Create a new unscaled Canvas.

Source

pub fn from_image(image: &DynamicImage) -> Canvas

Source

pub fn with_scale(width: u32, height: u32, scale: f32) -> Canvas

Create a new Canvas with a scale factor. The actual size of the canvas is the width times the scale and the height times the scale. However, all drawing commands should use the pre-scaled width and height.

Source

pub fn width(&self) -> u32

Source

pub fn height(&self) -> u32

The pre-scaled height of the canvas in pixels as a u32.

Source

pub fn w_f32(&self) -> f32

The pre-scaled width of the canvas in pixels as a f32.

Source

pub fn h_f32(&self) -> f32

The pre-scaled height of the canvas in pixels as a f32.

Source

pub fn w_usize(&self) -> usize

The pre-scaled width of the canvas in pixels as a usize.

Source

pub fn h_usize(&self) -> usize

The pre-scaled height of the canvas in pixels as a usize.

Source

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

Fill the entire canvas with a Color.

Source

pub fn fill_path( &mut self, path: &Path, paint: &mut Paint<'_>, fill_rule: FillRule, transform: Transform, clip_mask: Option<&Mask>, )

Fill the path with a Paint.

Source

pub fn fill_rect( &mut self, rect: Rect, paint: &mut Paint<'_>, transform: Transform, clip_mask: Option<&Mask>, )

Fill a rectangle with a Paint.

Source

pub fn stroke_path( &mut self, path: &Path, paint: &mut Paint<'_>, stroke: &Stroke, transform: Transform, clip_mask: Option<&Mask>, )

Stroke a path with a Paint.

Source

pub fn dot(&mut self, x: f32, y: f32, color: Color)

Color a single pixel on the canvas. !!! Warning this function will not scale with the canvas.

Source

pub fn save_png<P: AsRef<Path>>(&self, path: P)

Save the Canvas as a png.

Source

pub fn save_jpg<P: AsRef<Path>>(&self, path: P)

Save the Canvas as a jpg.

Source

pub fn save_tiff<P: AsRef<Path>>(&self, path: P)

Save the Canvas as a tiff.

Source

pub fn data(&self) -> &[u8]

Source

pub fn take(self) -> Vec<u8>

Trait Implementations§

Source§

impl From<&Canvas> for RgbaImage

Convert a &Canvas to a RgbaImage.

Source§

fn from(canvas: &Canvas) -> Self

Converts to this type from the input type.
Source§

impl From<&ImageBuffer<Rgb<u8>, Vec<u8>>> for Canvas

Convert a &RgbImage to a Canvas.

Source§

fn from(ib: &RgbImage) -> Self

Converts to this type from the input type.
Source§

impl From<&ImageBuffer<Rgba<u8>, Vec<u8>>> for Canvas

Convert a &RgbaImage to a Canvas.

Source§

fn from(ib: &RgbaImage) -> Self

Converts to this type from the input type.
Source§

impl From<Canvas> for RgbaImage

Convert a Canvas to a RgbaImage.

Source§

fn from(canvas: Canvas) -> Self

Converts to this type from the input type.
Source§

impl From<ImageBuffer<Rgb<u8>, Vec<u8>>> for Canvas

Convert a RgbImage to a Canvas.

Source§

fn from(ib: RgbImage) -> Self

Converts to this type from the input type.
Source§

impl From<ImageBuffer<Rgba<u8>, Vec<u8>>> for Canvas

Convert a RgbaImage to a Canvas.

Source§

fn from(ib: RgbaImage) -> Self

Converts to this type from the input type.

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

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V