pub struct Canvas<T: Renderer> { /* private fields */ }
Expand description
Main 2D drawing context.
Implementations
sourceimpl<T> Canvas<T> where
T: Renderer,
impl<T> Canvas<T> where
T: Renderer,
sourcepub fn new_with_text_context(
renderer: T,
text_context: TextContext
) -> Result<Self, ErrorKind>
pub fn new_with_text_context(
renderer: T,
text_context: TextContext
) -> Result<Self, ErrorKind>
Creates a new canvas with the specified renderer and using the fonts registered with the
provided TextContext
. Note that the context is explicitly shared, so that any fonts
registered with a clone of this context will also be visible to this canvas.
sourcepub fn set_size(&mut self, width: u32, height: u32, dpi: f32)
pub fn set_size(&mut self, width: u32, height: u32, dpi: f32)
Sets the size of the default framebuffer (screen size)
sourcepub fn clear_rect(
&mut self,
x: u32,
y: u32,
width: u32,
height: u32,
color: Color
)
pub fn clear_rect(
&mut self,
x: u32,
y: u32,
width: u32,
height: u32,
color: Color
)
Clears the rectangle area defined by left upper corner (x,y), width and height with the provided color.
sourcepub fn flush(&mut self)
pub fn flush(&mut self)
Tells the renderer to execute all drawing commands and clears the current internal state
Call this at the end of each frame.
pub fn screenshot(&mut self) -> Result<ImgVec<RGBA8>, ErrorKind>
sourcepub fn save(&mut self)
pub fn save(&mut self)
Pushes and saves the current render state into a state stack.
A matching restore() must be used to restore the state.
sourcepub fn restore(&mut self)
pub fn restore(&mut self)
Restores the previous render state
Restoring the initial/first state will just reset it to the defaults
sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Resets current state to default values. Does not affect the state stack.
sourcepub fn save_with(&mut self, callback: impl FnMut(&mut Self))
pub fn save_with(&mut self, callback: impl FnMut(&mut Self))
Saves the current state before calling the callback and restores it afterwards
This is less error prone than remembering to match save() -> restore() calls
sourcepub fn set_global_alpha(&mut self, alpha: f32)
pub fn set_global_alpha(&mut self, alpha: f32)
Sets the transparency applied to all rendered shapes.
Already transparent paths will get proportionally more transparent as well.
sourcepub fn global_composite_operation(&mut self, op: CompositeOperation)
pub fn global_composite_operation(&mut self, op: CompositeOperation)
Sets the composite operation.
sourcepub fn global_composite_blend_func(
&mut self,
src_factor: BlendFactor,
dst_factor: BlendFactor
)
pub fn global_composite_blend_func(
&mut self,
src_factor: BlendFactor,
dst_factor: BlendFactor
)
Sets the composite operation with custom pixel arithmetic.
sourcepub fn global_composite_blend_func_separate(
&mut self,
src_rgb: BlendFactor,
dst_rgb: BlendFactor,
src_alpha: BlendFactor,
dst_alpha: BlendFactor
)
pub fn global_composite_blend_func_separate(
&mut self,
src_rgb: BlendFactor,
dst_rgb: BlendFactor,
src_alpha: BlendFactor,
dst_alpha: BlendFactor
)
Sets the composite operation with custom pixel arithmetic for RGB and alpha components separately.
sourcepub fn set_render_target(&mut self, target: RenderTarget)
pub fn set_render_target(&mut self, target: RenderTarget)
Sets a new render target. All drawing operations after this call will happen on the provided render target
sourcepub fn create_image_empty(
&mut self,
width: usize,
height: usize,
format: PixelFormat,
flags: ImageFlags
) -> Result<ImageId, ErrorKind>
pub fn create_image_empty(
&mut self,
width: usize,
height: usize,
format: PixelFormat,
flags: ImageFlags
) -> Result<ImageId, ErrorKind>
Allocates an empty image with the provided domensions and format.
sourcepub fn create_image<'a, S: Into<ImageSource<'a>>>(
&mut self,
src: S,
flags: ImageFlags
) -> Result<ImageId, ErrorKind>
pub fn create_image<'a, S: Into<ImageSource<'a>>>(
&mut self,
src: S,
flags: ImageFlags
) -> Result<ImageId, ErrorKind>
Creates image from specified image data.
pub fn get_image(&self, id: ImageId) -> Option<&T::Image>
pub fn get_image_mut(&mut self, id: ImageId) -> Option<&mut T::Image>
sourcepub fn realloc_image(
&mut self,
id: ImageId,
width: usize,
height: usize,
format: PixelFormat,
flags: ImageFlags
) -> Result<(), ErrorKind>
pub fn realloc_image(
&mut self,
id: ImageId,
width: usize,
height: usize,
format: PixelFormat,
flags: ImageFlags
) -> Result<(), ErrorKind>
Resizes an image to the new provided dimensions.
sourcepub fn load_image_file<P: AsRef<FilePath>>(
&mut self,
filename: P,
flags: ImageFlags
) -> Result<ImageId, ErrorKind>
pub fn load_image_file<P: AsRef<FilePath>>(
&mut self,
filename: P,
flags: ImageFlags
) -> Result<ImageId, ErrorKind>
Decode an image from file
sourcepub fn load_image_mem(
&mut self,
data: &[u8],
flags: ImageFlags
) -> Result<ImageId, ErrorKind>
pub fn load_image_mem(
&mut self,
data: &[u8],
flags: ImageFlags
) -> Result<ImageId, ErrorKind>
Decode an image from memory
sourcepub fn update_image<'a, S: Into<ImageSource<'a>>>(
&mut self,
id: ImageId,
src: S,
x: usize,
y: usize
) -> Result<(), ErrorKind>
pub fn update_image<'a, S: Into<ImageSource<'a>>>(
&mut self,
id: ImageId,
src: S,
x: usize,
y: usize
) -> Result<(), ErrorKind>
Updates image data specified by image handle.
sourcepub fn delete_image(&mut self, id: ImageId)
pub fn delete_image(&mut self, id: ImageId)
Deletes created image.
sourcepub fn image_size(&self, id: ImageId) -> Result<(usize, usize), ErrorKind>
pub fn image_size(&self, id: ImageId) -> Result<(usize, usize), ErrorKind>
Returns the size in pixels of the image for the specified id.
sourcepub fn filter_image(
&mut self,
target_image: ImageId,
filter: ImageFilter,
source_image: ImageId
)
pub fn filter_image(
&mut self,
target_image: ImageId,
filter: ImageFilter,
source_image: ImageId
)
Renders the given source_image into target_image while applying a filter effect.
The target image must have the same size as the source image. The filtering is recorded
as a drawing command and run by the renderer when Self::flush()
is called.
The filtering does not take any transformation set on the Canvas into account nor does it change the current rendering target.
sourcepub fn reset_transform(&mut self)
pub fn reset_transform(&mut self)
Resets current transform to a identity matrix.
sourcepub fn set_transform(&mut self, a: f32, b: f32, c: f32, d: f32, e: f32, f: f32)
pub fn set_transform(&mut self, a: f32, b: f32, c: f32, d: f32, e: f32, f: f32)
Premultiplies current coordinate system by specified matrix.
The parameters are interpreted as matrix as follows: [a c e] [b d f] [0 0 1]
sourcepub fn rotate(&mut self, angle: f32)
pub fn rotate(&mut self, angle: f32)
Rotates the current coordinate system. Angle is specified in radians.
sourcepub fn skew_x(&mut self, angle: f32)
pub fn skew_x(&mut self, angle: f32)
Skews the current coordinate system along X axis. Angle is specified in radians.
sourcepub fn skew_y(&mut self, angle: f32)
pub fn skew_y(&mut self, angle: f32)
Skews the current coordinate system along Y axis. Angle is specified in radians.
sourcepub fn transform(&self) -> Transform2D
pub fn transform(&self) -> Transform2D
Returns the current transformation matrix
TODO: It’s not ok that this method returns Transform2D while set_transform accepts 6 floats - make it consistant
sourcepub fn scissor(&mut self, x: f32, y: f32, w: f32, h: f32)
pub fn scissor(&mut self, x: f32, y: f32, w: f32, h: f32)
Sets the current scissor rectangle.
The scissor rectangle is transformed by the current transform.
sourcepub fn intersect_scissor(&mut self, x: f32, y: f32, w: f32, h: f32)
pub fn intersect_scissor(&mut self, x: f32, y: f32, w: f32, h: f32)
Intersects current scissor rectangle with the specified rectangle.
The scissor rectangle is transformed by the current transform. Note: in case the rotation of previous scissor rect differs from the current one, the intersection will be done between the specified rectangle and the previous scissor rectangle transformed in the current transform space. The resulting shape is always rectangle.
sourcepub fn reset_scissor(&mut self)
pub fn reset_scissor(&mut self)
Reset and disables scissoring.
sourcepub fn contains_point(
&self,
path: &mut Path,
x: f32,
y: f32,
fill_rule: FillRule
) -> bool
pub fn contains_point(
&self,
path: &mut Path,
x: f32,
y: f32,
fill_rule: FillRule
) -> bool
Returns true if the specified point (x,y) is in the provided path, and false otherwise.
sourcepub fn fill_path(&mut self, path: &mut Path, paint: Paint)
pub fn fill_path(&mut self, path: &mut Path, paint: Paint)
Fills the provided Path with the specified Paint.
sourcepub fn stroke_path(&mut self, path: &mut Path, paint: Paint)
pub fn stroke_path(&mut self, path: &mut Path, paint: Paint)
Strokes the provided Path with the specified Paint.
sourcepub fn add_font<P: AsRef<FilePath>>(
&mut self,
file_path: P
) -> Result<FontId, ErrorKind>
pub fn add_font<P: AsRef<FilePath>>(
&mut self,
file_path: P
) -> Result<FontId, ErrorKind>
Adds a font file to the canvas
sourcepub fn add_font_mem(&mut self, data: &[u8]) -> Result<FontId, ErrorKind>
pub fn add_font_mem(&mut self, data: &[u8]) -> Result<FontId, ErrorKind>
Adds a font to the canvas by reading it from the specified chunk of memory.
sourcepub fn add_font_dir<P: AsRef<FilePath>>(
&mut self,
dir_path: P
) -> Result<Vec<FontId>, ErrorKind>
pub fn add_font_dir<P: AsRef<FilePath>>(
&mut self,
dir_path: P
) -> Result<Vec<FontId>, ErrorKind>
Adds all .ttf files from a directory
sourcepub fn measure_text<S: AsRef<str>>(
&mut self,
x: f32,
y: f32,
text: S,
paint: Paint
) -> Result<TextMetrics, ErrorKind>
pub fn measure_text<S: AsRef<str>>(
&mut self,
x: f32,
y: f32,
text: S,
paint: Paint
) -> Result<TextMetrics, ErrorKind>
Returns information on how the provided text will be drawn with the specified paint.
sourcepub fn measure_font(&mut self, paint: Paint) -> Result<FontMetrics, ErrorKind>
pub fn measure_font(&mut self, paint: Paint) -> Result<FontMetrics, ErrorKind>
Returns font metrics for a particular Paint.
sourcepub fn break_text<S: AsRef<str>>(
&mut self,
max_width: f32,
text: S,
paint: Paint
) -> Result<usize, ErrorKind>
pub fn break_text<S: AsRef<str>>(
&mut self,
max_width: f32,
text: S,
paint: Paint
) -> Result<usize, ErrorKind>
Returns the maximum index-th byte of text that will fit inside max_width.
The retuned index will always lie at the start and/or end of a UTF-8 code point sequence or at the start or end of the text
sourcepub fn break_text_vec<S: AsRef<str>>(
&mut self,
max_width: f32,
text: S,
paint: Paint
) -> Result<Vec<Range<usize>>, ErrorKind>
pub fn break_text_vec<S: AsRef<str>>(
&mut self,
max_width: f32,
text: S,
paint: Paint
) -> Result<Vec<Range<usize>>, ErrorKind>
Returnes a list of ranges representing each line of text that will fit inside max_width
sourcepub fn fill_text<S: AsRef<str>>(
&mut self,
x: f32,
y: f32,
text: S,
paint: Paint
) -> Result<TextMetrics, ErrorKind>
pub fn fill_text<S: AsRef<str>>(
&mut self,
x: f32,
y: f32,
text: S,
paint: Paint
) -> Result<TextMetrics, ErrorKind>
Fills the provided string with the specified Paint.
sourcepub fn stroke_text<S: AsRef<str>>(
&mut self,
x: f32,
y: f32,
text: S,
paint: Paint
) -> Result<TextMetrics, ErrorKind>
pub fn stroke_text<S: AsRef<str>>(
&mut self,
x: f32,
y: f32,
text: S,
paint: Paint
) -> Result<TextMetrics, ErrorKind>
Strokes the provided string with the specified Paint.
Trait Implementations
Auto Trait Implementations
impl<T> !RefUnwindSafe for Canvas<T>
impl<T> !Send for Canvas<T>
impl<T> !Sync for Canvas<T>
impl<T> Unpin for Canvas<T> where
T: Unpin,
<T as Renderer>::Image: Unpin,
impl<T> !UnwindSafe for Canvas<T>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more