Batched, immutable drawing documents rendered through Skia.
Build a %Skia.Document{} with pipe-friendly commands, then end the pipeline
with a renderer such as to_png/1, to_raw/1, or render/2.
{:ok, png} =
Skia.canvas(800, 600)
|> Skia.clear(:white)
|> Skia.rect(x: 40, y: 40, width: 120, height: 80, fill: :red)
|> Skia.to_png()
Summary
Functions
Adds a arc command to the document.
Creates an empty drawing document.
Adds a circle command to the document.
Adds a clear command to the document.
Adds a clip_circle command to the document.
Adds a clip_path command to the document.
Adds a clip_rect command to the document.
Returns normalized commands in render order.
Adds a concat command to the document.
Adds a saved canvas group with optional transforms.
Adds a image command to the document.
Adds a saved layer with optional opacity.
Adds a line command to the document.
Measures text using the native text engine.
Adds a oval command to the document.
Adds a path command to the document.
Adds a path_op command to the document.
Adds a path_outline command to the document.
Adds a picture command to the document.
Records the document into a reusable Skia picture.
Adds a rect command to the document.
Renders the document according to Skia.RenderOptions.
Adds a rotate_at command to the document.
Adds a scale command to the document.
Adds a style scope for following commands in the group.
Adds text with optional %Skia.TextStyle{} and %Skia.ParagraphStyle{} values.
Adds a text_blob command to the document.
Encodes the document to the term batch a native renderer would consume.
Renders the document to JPEG through the native renderer.
Renders the document to PNG through the native renderer.
Renders the document to a raw RGBA buffer.
Renders the document to WEBP through the native renderer.
Validates the document before handing it to native code.
Adds a vertices command to the document.
Types
@type document() :: Skia.Document.t()
Functions
@spec arc( Skia.Document.t(), keyword() ) :: Skia.Document.t()
Adds a arc command to the document.
Native: skia_safe::Canvas::draw_arc
Native signature: fn draw_arc(&self, oval: impl AsRef<Rect>, start_angle: scalar, sweep_angle: scalar, use_center: bool, paint: &Paint) -> &Self
Native source: src/core/canvas.rs:1492
Native skia_safe::Canvas::draw_arc docs:
Draws arc using clip, Matrix, and Paint paint.
Arc is part of oval bounded by oval, sweeping from start_angle to start_angle plus
sweep_angle. start_angle and sweep_angle are in degrees.
start_angle of zero places start point at the right middle edge of oval.
A positive sweep_angle places arc end point clockwise from start point;
a negative sweep_angle places arc end point counterclockwise from start point.
sweep_angle may exceed 360 degrees, a full circle.
If use_center is true, draw a wedge that includes lines from oval
center to arc end points. If use_center is false, draw arc between end points.
If Rect oval is empty or sweep_angle is zero, nothing is drawn.
ovalRectbounds of oval containing arc to drawstart_angleangle in degrees where arc beginssweep_anglesweep angle in degrees; positive is clockwiseuse_centeriftrue, include the center of the ovalpaintPaintstroke or fill, blend, color, and so on, used to draw
@spec canvas(pos_integer(), pos_integer()) :: Skia.Document.t()
Creates an empty drawing document.
@spec circle( Skia.Document.t(), keyword() ) :: Skia.Document.t()
Adds a circle command to the document.
Native: skia_safe::Canvas::draw_circle
Native signature: fn draw_circle(&self, center: impl Into<Point>, radius: scalar, paint: &Paint) -> &Self
Native source: src/core/canvas.rs:1464
Native skia_safe::Canvas::draw_circle docs:
Draws circle at center with radius using clip, Matrix, and Paint paint.
If radius is zero or less, nothing is drawn.
In paint: paint::Style determines if circle is stroked or filled;
if stroked, Paint stroke width describes the line thickness.
centercircle centerradiushalf the diameter of circlepaintPaintstroke or fill, blend, color, and so on, used to draw
@spec clear(Skia.Document.t(), term(), keyword()) :: Skia.Document.t()
Adds a clear command to the document.
Native: skia_safe::Canvas::clear
Native signature: fn clear(&self, color: impl Into<Color4f>) -> &Self
Native source: src/core/canvas.rs:1235
Native skia_safe::Canvas::clear docs:
Fills clip with color color using BlendMode::Src.
This has the effect of replacing all pixels contained by clip with color.
colorColor4frepresenting unpremultiplied color.
@spec clip_circle( Skia.Document.t(), keyword() ) :: Skia.Document.t()
Adds a clip_circle command to the document.
Implemented via native calls: skia_safe::Canvas::clip_path
@spec clip_path(Skia.Document.t(), term(), keyword()) :: Skia.Document.t()
Adds a clip_path command to the document.
Native: skia_safe::Canvas::clip_path
Native signature: fn clip_path(&self, path: &Path, op: impl Into<Option<ClipOp>>, do_anti_alias: impl Into<Option<bool>>) -> &Self
Native source: src/core/canvas.rs:1141
Native skia_safe::Canvas::clip_path docs:
Replaces clip with the intersection or difference of clip and path,
with an aliased or anti-aliased clip edge. PathFillType determines if path
describes the area inside or outside its contours; and if path contour overlaps
itself or another path contour, whether the overlaps form part of the area.
path is transformed by Matrix before it is combined with clip.
pathPathto combine with clipopClipOpto apply to clipdo_anti_aliastrueif clip is to be anti-aliased
@spec clip_rect( Skia.Document.t(), keyword() ) :: Skia.Document.t()
Adds a clip_rect command to the document.
Native: skia_safe::Canvas::clip_rect
Native signature: fn clip_rect(&self, rect: impl AsRef<Rect>, op: impl Into<Option<ClipOp>>, do_anti_alias: impl Into<Option<bool>>) -> &Self
Native source: src/core/canvas.rs:1083
Native skia_safe::Canvas::clip_rect docs:
Replaces clip with the intersection or difference of clip and rect,
with an aliased or anti-aliased clip edge. rect is transformed by Matrix
before it is combined with clip.
rectRectto combine with clipopClipOpto apply to clipdo_anti_aliastrueif clip is to be anti-aliased
@spec commands(Skia.Document.t()) :: [Skia.Command.t()]
Returns normalized commands in render order.
@spec concat( Skia.Document.t(), keyword() ) :: Skia.Document.t()
Adds a concat command to the document.
Native: skia_safe::Canvas::concat
Native signature: fn concat(&self, matrix: &Matrix) -> &Self
Native source: src/core/canvas.rs:1044
Native skia_safe::Canvas::concat docs:
Replaces Matrix with matrix premultiplied with existing Matrix.
This has the effect of transforming the drawn geometry by matrix, before transforming the
result with existing Matrix.
matrixmatrix to premultiply with existingMatrix
@spec group(Skia.Document.t(), keyword(), (Skia.Document.t() -> Skia.Document.t())) :: Skia.Document.t()
Adds a saved canvas group with optional transforms.
@spec image(Skia.Document.t(), term(), keyword()) :: Skia.Document.t()
Adds a image command to the document.
Native: skia_safe::Canvas::draw_image_with_sampling_options
Native signature: fn draw_image_with_sampling_options(&self, image: impl AsRef<Image>, left_top: impl Into<Point>, sampling: impl Into<SamplingOptions>, paint: Option<&Paint>) -> &Self
Native source: src/core/canvas.rs:1613
@spec layer(Skia.Document.t(), keyword(), (Skia.Document.t() -> Skia.Document.t())) :: Skia.Document.t()
Adds a saved layer with optional opacity.
@spec line( Skia.Document.t(), keyword() ) :: Skia.Document.t()
Adds a line command to the document.
Native: skia_safe::Canvas::draw_line
Native signature: fn draw_line(&self, p1: impl Into<Point>, p2: impl Into<Point>, paint: &Paint) -> &Self
Native source: src/core/canvas.rs:1333
Native skia_safe::Canvas::draw_line docs:
Draws line segment from p1 to p2 using clip, Matrix, and Paint paint.
In paint: Paint stroke width describes the line thickness;
paint::Cap draws the end rounded or square;
paint::Style is ignored, as if were set to paint::Style::Stroke.
p1start of line segmentp2end of line segmentpaintstroke, blend, color, and so on, used to draw
@spec measure_text( String.t(), keyword() ) :: {:ok, %{width: float(), bounds: {float(), float(), float(), float()}}} | {:error, atom()}
Measures text using the native text engine.
@spec oval( Skia.Document.t(), keyword() ) :: Skia.Document.t()
Adds a oval command to the document.
Native: skia_safe::Canvas::draw_oval
Native signature: fn draw_oval(&self, oval: impl AsRef<Rect>, paint: &Paint) -> &Self
Native source: src/core/canvas.rs:1395
Native skia_safe::Canvas::draw_oval docs:
Draws oval oval using clip, Matrix, and Paint.
In paint: paint::Style determines if oval is stroked or filled;
if stroked, Paint stroke width describes the line thickness.
ovalRectbounds of ovalpaintPaintstroke or fill, blend, color, and so on, used to draw
@spec path(Skia.Document.t(), term(), keyword()) :: Skia.Document.t()
Adds a path command to the document.
Native: skia_safe::Canvas::draw_path
Native signature: fn draw_path(&self, path: &Path, paint: &Paint) -> &Self
Native source: src/core/canvas.rs:1582
Native skia_safe::Canvas::draw_path docs:
Draws Path path using clip, Matrix, and Paint paint.
Path contains an array of path contour, each of which may be open or closed.
In paint: paint::Style determines if RRect is stroked or filled:
if filled, PathFillType determines whether path contour describes inside or
outside of fill; if stroked, Paint stroke width describes the line thickness,
paint::Cap describes line ends, and paint::Join describes how
corners are drawn.
pathPathto drawpaintstroke, blend, color, and so on, used to draw
@spec path_op(Skia.Document.t(), term(), term(), keyword()) :: Skia.Document.t()
Adds a path_op command to the document.
@spec path_outline(Skia.Document.t(), term(), keyword()) :: Skia.Document.t()
Adds a path_outline command to the document.
@spec picture(Skia.Document.t(), term(), keyword()) :: Skia.Document.t()
Adds a picture command to the document.
Native: skia_safe::Canvas::draw_picture
Native signature: fn draw_picture(&self, picture: impl AsRef<Picture>, matrix: Option<&Matrix>, paint: Option<&Paint>) -> &Self
Native source: src/core/canvas.rs:1973
Native skia_safe::Canvas::draw_picture docs:
Draws Picture picture, using clip and Matrix; transforming picture with
Matrix matrix, if provided; and use Paint paint alpha, ColorFilter,
ImageFilter, and BlendMode, if provided.
If paint is not None, then the picture is always drawn into a temporary layer before
actually landing on the canvas. Note that drawing into a layer can also change its
appearance if there are any non-associative blend modes inside any of the pictures elements.
picturerecorded drawing commands to playmatrixMatrixto rotate, scale, translate, and so on; may beNonepaintPaintto apply transparency, filtering, and so on; may beNone
@spec record_picture(Skia.Document.t()) :: {:ok, Skia.Picture.t()} | {:error, atom(), map()}
Records the document into a reusable Skia picture.
@spec rect( Skia.Document.t(), keyword() ) :: Skia.Document.t()
Adds a rect command to the document.
Native: skia_safe::Canvas::draw_rect
Native signature: fn draw_rect(&self, rect: impl AsRef<Rect>, paint: &Paint) -> &Self
Native source: src/core/canvas.rs:1351
Native skia_safe::Canvas::draw_rect docs:
Draws Rect rect using clip, Matrix, and Paint paint.
In paint: paint::Style determines if rectangle is stroked or filled;
if stroked, Paint stroke width describes the line thickness, and
paint::Join draws the corners rounded or square.
rectrectangle to drawpaintstroke or fill, blend, color, and so on, used to draw
@spec render(Skia.Document.t(), keyword() | Skia.RenderOptions.t()) :: {:ok, binary() | map()} | {:error, atom(), map()}
Renders the document according to Skia.RenderOptions.
@spec rotate_at( Skia.Document.t(), keyword() ) :: Skia.Document.t()
Adds a rotate_at command to the document.
Native: skia_safe::Canvas::rotate
Native signature: fn rotate(&self, degrees: scalar, p: Option<Point>) -> &Self
Native source: src/core/canvas.rs:1008
Native skia_safe::Canvas::rotate docs:
Rotates Matrix by degrees about a point at (p.x, p.y). Positive degrees rotates
clockwise.
Mathematically, constructs a rotation matrix; premultiplies the rotation matrix by a
translation matrix; then replaces Matrix with the resulting matrix premultiplied with
Matrix.
This has the effect of rotating the drawing about a given point before transforming the
result with Matrix.
degreesamount to rotate, in degreespthe point to rotate about
@spec scale( Skia.Document.t(), keyword() ) :: Skia.Document.t()
Adds a scale command to the document.
Native: skia_safe::Canvas::scale
Native signature: fn scale(&mut self, sx: f32, sy: f32)
Native source: examples/hello/canvas.rs:38
@spec style(Skia.Document.t(), keyword(), (Skia.Document.t() -> Skia.Document.t())) :: Skia.Document.t()
Adds a style scope for following commands in the group.
@spec text(Skia.Document.t(), String.t(), keyword()) :: Skia.Document.t()
Adds text with optional %Skia.TextStyle{} and %Skia.ParagraphStyle{} values.
@spec text_blob(Skia.Document.t(), term(), keyword()) :: Skia.Document.t()
Adds a text_blob command to the document.
Native: skia_safe::Canvas::draw_text_blob
Native signature: fn draw_text_blob(&self, blob: impl AsRef<TextBlob>, origin: impl Into<Point>, paint: &Paint) -> &Self
Native source: src/core/canvas.rs:1942
Native skia_safe::Canvas::draw_text_blob docs:
Draws TextBlob blob at (origin.x, origin.y), using clip, Matrix, and Paint
paint.
blob contains glyphs, their positions, and paint attributes specific to text:
Typeface, Paint text size, Paint text scale x, Paint text skew x,
Paint align, Paint hinting, anti-alias, Paint fake bold, Paint font embedded
bitmaps, Paint full hinting spacing, LCD text, Paint linear text, and Paint
subpixel text.
TextEncoding must be set to TextEncoding::GlyphId.
Elements of paint: PathEffect, MaskFilter, Shader,
ColorFilter, and ImageFilter; apply to blob.
blobglyphs, positions, and their paints' text size, typeface, and so onoriginhorizontal and vertical offset applied to blobpaintblend, color, stroking, and so on, used to draw
@spec to_batch(Skia.Document.t()) :: %{ width: pos_integer(), height: pos_integer(), commands: [Skia.Command.t()] }
Encodes the document to the term batch a native renderer would consume.
@spec to_jpeg( Skia.Document.t(), keyword() ) :: {:ok, binary()} | {:error, atom(), map()}
Renders the document to JPEG through the native renderer.
@spec to_png(Skia.Document.t()) :: {:ok, binary()} | {:error, atom(), map()}
Renders the document to PNG through the native renderer.
@spec to_raw(Skia.Document.t()) :: {:ok, %{ width: pos_integer(), height: pos_integer(), stride: pos_integer(), data: binary() }} | {:error, atom(), map()}
Renders the document to a raw RGBA buffer.
@spec to_webp( Skia.Document.t(), keyword() ) :: {:ok, binary()} | {:error, atom(), map()}
Renders the document to WEBP through the native renderer.
@spec validate(Skia.Document.t()) :: :ok | {:error, atom(), map()}
Validates the document before handing it to native code.
@spec vertices(Skia.Document.t(), term(), keyword()) :: Skia.Document.t()
Adds a vertices command to the document.
Native: skia_safe::Canvas::draw_vertices
Native signature: fn draw_vertices(&self, vertices: &Vertices, mode: BlendMode, paint: &Paint) -> &Self
Native source: src/core/canvas.rs:2009
Native skia_safe::Canvas::draw_vertices docs:
Draws Vertices vertices, a triangle mesh, using clip and Matrix.
If paint contains an Shader and vertices does not contain tex coords, the shader is
mapped using the vertices' positions.
BlendMode is ignored if Vertices does not have colors. Otherwise, it combines
- the
ShaderifPaintcontains [Shader - or the opaque
Paintcolor ifPaintdoes not containShader
as the src of the blend and the interpolated vertex colors as the dst.
MaskFilter, PathEffect, and antialiasing on Paint are ignored.
verticestriangle mesh to drawmodecombines vertices' colors withShaderif present orPaintopaque color if not. Ignored if the vertices do not contain color.paintspecifies theShader, used asVerticestexture, andColorFilter.
example: https://fiddle.skia.org/c/@Canvas_drawVertices example: https://fiddle.skia.org/c/@Canvas_drawVertices_2