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

document()

@type document() :: Skia.Document.t()

Functions

arc(document, opts \\ [])

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

  • oval Rect bounds of oval containing arc to draw
  • start_angle angle in degrees where arc begins
  • sweep_angle sweep angle in degrees; positive is clockwise
  • use_center if true, include the center of the oval
  • paint Paint stroke or fill, blend, color, and so on, used to draw

canvas(width, height)

@spec canvas(pos_integer(), pos_integer()) :: Skia.Document.t()

Creates an empty drawing document.

circle(document, opts \\ [])

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

  • center circle center
  • radius half the diameter of circle
  • paint Paint stroke or fill, blend, color, and so on, used to draw

clear(document, color, opts \\ [])

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

  • color Color4f representing unpremultiplied color.

clip_circle(document, opts \\ [])

@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

clip_path(document, path, opts \\ [])

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

  • path Path to combine with clip
  • op ClipOp to apply to clip
  • do_anti_alias true if clip is to be anti-aliased

example: https://fiddle.skia.org/c/@Canvas_clipPath

clip_rect(document, opts \\ [])

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

  • rect Rect to combine with clip
  • op ClipOp to apply to clip
  • do_anti_alias true if clip is to be anti-aliased

example: https://fiddle.skia.org/c/@Canvas_clipRect

commands(document)

@spec commands(Skia.Document.t()) :: [Skia.Command.t()]

Returns normalized commands in render order.

concat(document, opts \\ [])

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

  • matrix matrix to premultiply with existing Matrix

example: https://fiddle.skia.org/c/@Canvas_concat

group(document, opts, fun)

Adds a saved canvas group with optional transforms.

image(document, image, opts \\ [])

@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

layer(document, opts, fun)

Adds a saved layer with optional opacity.

line(document, opts \\ [])

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

  • p1 start of line segment
  • p2 end of line segment
  • paint stroke, blend, color, and so on, used to draw

measure_text(text, opts \\ [])

@spec measure_text(
  String.t(),
  keyword()
) ::
  {:ok, %{width: float(), bounds: {float(), float(), float(), float()}}}
  | {:error, atom()}

Measures text using the native text engine.

oval(document, opts \\ [])

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

  • oval Rect bounds of oval
  • paint Paint stroke or fill, blend, color, and so on, used to draw

example: https://fiddle.skia.org/c/@Canvas_drawOval

path(document, path, opts \\ [])

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

  • path Path to draw
  • paint stroke, blend, color, and so on, used to draw

example: https://fiddle.skia.org/c/@Canvas_drawPath

path_op(document, a, b, opts \\ [])

@spec path_op(Skia.Document.t(), term(), term(), keyword()) :: Skia.Document.t()

Adds a path_op command to the document.

path_outline(document, path, opts \\ [])

@spec path_outline(Skia.Document.t(), term(), keyword()) :: Skia.Document.t()

Adds a path_outline command to the document.

picture(document, picture, opts \\ [])

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

  • picture recorded drawing commands to play
  • matrix Matrix to rotate, scale, translate, and so on; may be None
  • paint Paint to apply transparency, filtering, and so on; may be None

record_picture(document)

@spec record_picture(Skia.Document.t()) ::
  {:ok, Skia.Picture.t()} | {:error, atom(), map()}

Records the document into a reusable Skia picture.

rect(document, opts \\ [])

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

  • rect rectangle to draw
  • paint stroke or fill, blend, color, and so on, used to draw

example: https://fiddle.skia.org/c/@Canvas_drawRect

render(document, opts \\ [])

@spec render(Skia.Document.t(), keyword() | Skia.RenderOptions.t()) ::
  {:ok, binary() | map()} | {:error, atom(), map()}

Renders the document according to Skia.RenderOptions.

rotate_at(document, opts \\ [])

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

  • degrees amount to rotate, in degrees
  • p the point to rotate about

example: https://fiddle.skia.org/c/@Canvas_rotate_2

scale(document, opts \\ [])

@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

style(document, opts, fun)

Adds a style scope for following commands in the group.

text(document, text, opts \\ [])

@spec text(Skia.Document.t(), String.t(), keyword()) :: Skia.Document.t()

Adds text with optional %Skia.TextStyle{} and %Skia.ParagraphStyle{} values.

text_blob(document, blob, opts \\ [])

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

  • blob glyphs, positions, and their paints' text size, typeface, and so on
  • origin horizontal and vertical offset applied to blob
  • paint blend, color, stroking, and so on, used to draw

to_batch(document)

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

to_jpeg(document, opts \\ [])

@spec to_jpeg(
  Skia.Document.t(),
  keyword()
) :: {:ok, binary()} | {:error, atom(), map()}

Renders the document to JPEG through the native renderer.

to_png(document)

@spec to_png(Skia.Document.t()) :: {:ok, binary()} | {:error, atom(), map()}

Renders the document to PNG through the native renderer.

to_raw(document)

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

to_webp(document, opts \\ [])

@spec to_webp(
  Skia.Document.t(),
  keyword()
) :: {:ok, binary()} | {:error, atom(), map()}

Renders the document to WEBP through the native renderer.

validate(document)

@spec validate(Skia.Document.t()) :: :ok | {:error, atom(), map()}

Validates the document before handing it to native code.

vertices(document, vertices, opts \\ [])

@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 Shader if Paint contains [Shader
  • or the opaque Paint color if Paint does not contain Shader

as the src of the blend and the interpolated vertex colors as the dst.

MaskFilter, PathEffect, and antialiasing on Paint are ignored.

  • vertices triangle mesh to draw
  • mode combines vertices' colors with Shader if present or Paint opaque color if not. Ignored if the vertices do not contain color.
  • paint specifies the Shader, used as Vertices texture, and ColorFilter.

example: https://fiddle.skia.org/c/@Canvas_drawVertices example: https://fiddle.skia.org/c/@Canvas_drawVertices_2