CanvasCraft (canvas_craft v0.2.0)

View Source

CanvasCraft - 2D graphics drawing library facade.

This module delegates drawing operations to the Skia backend by default. The canvas handle is represented as {backend_module, backend_ref}.

Examples:

  • In-memory WEBP export (no temp files): iex> {:ok, handle} = CanvasCraft.create_canvas(16, 16) iex> {:ok, bin} = CanvasCraft.export_webp(handle) iex> is_binary(bin) true

See guides/examples/ for runnable scripts demonstrating gradients, filters, images, and in-memory export.

Summary

Types

Backend-qualified canvas handle

Functions

Return capabilities supported by the selected backend

Clear the canvas to a given RGBA color (tuple {r,g,b,a}, 0..255). No-op for backends that don't support it yet.

Create a new canvas using the selected backend.

Draw UTF-8 text at x,y with RGBA color.

Export the canvas using backend. Defaults to PNG; if opts[:format] == :webp and the backend implements export_webp/2, that path is used.

Export PNG and write to file path (helper).

Export the raw RGBA buffer from the backend, if supported.

Export the canvas as WEBP binary using backend, no filesystem involved.

Export WEBP and write to file path (helper).

Fill a circle with a color.

Fill an axis-aligned rectangle. No-op for backends that don't support it yet; implemented later.

Fill an axis-aligned rectangle with a color.

Return a simple atom for smoke tests.

Load a TTF/OTF font from a filesystem path for the current backend (when supported).

Enable/disable antialiasing or set sample count (1,4,8).

Set current font size in points/pixels (backend-defined units).

Return true if backend supports the given feature

Write a binary returned from export_* to a file path (thin helper).

Types

canvas_handle()

@type canvas_handle() :: {module(), term()}

Backend-qualified canvas handle

Functions

capabilities(backend)

@spec capabilities(module()) :: MapSet.t()

Return capabilities supported by the selected backend

clear(arg, rgba)

@spec clear(canvas_handle(), {0..255, 0..255, 0..255, 0..255}) ::
  :ok | {:error, term()}

Clear the canvas to a given RGBA color (tuple {r,g,b,a}, 0..255). No-op for backends that don't support it yet.

create_canvas(width, height, opts \\ [])

@spec create_canvas(pos_integer(), pos_integer(), keyword()) ::
  {:ok, canvas_handle()} | {:error, term()}

Create a new canvas using the selected backend.

Options:

  • :backend - backend module (default: CanvasCraft.Backends.Skia)
  • other backend-specific options

draw_text(arg1, x, y, text, arg2)

@spec draw_text(
  canvas_handle(),
  number(),
  number(),
  String.t(),
  {0..255, 0..255, 0..255, 0..255}
) ::
  :ok | {:error, term()}

Draw UTF-8 text at x,y with RGBA color.

export_png(arg, opts \\ [])

@spec export_png(
  canvas_handle(),
  keyword()
) :: {:ok, binary()} | {:error, term()}

Export the canvas using backend. Defaults to PNG; if opts[:format] == :webp and the backend implements export_webp/2, that path is used.

export_png_to_file(handle, path, opts \\ [])

@spec export_png_to_file(canvas_handle(), Path.t(), keyword()) ::
  :ok | {:error, term()}

Export PNG and write to file path (helper).

export_raw(arg)

@spec export_raw(canvas_handle()) ::
  {:ok, {non_neg_integer(), non_neg_integer(), non_neg_integer(), binary()}}
  | {:error, term()}

Export the raw RGBA buffer from the backend, if supported.

export_webp(arg, opts \\ [])

@spec export_webp(
  canvas_handle(),
  keyword()
) :: {:ok, binary()} | {:error, term()}

Export the canvas as WEBP binary using backend, no filesystem involved.

export_webp_to_file(handle, path, opts \\ [])

@spec export_webp_to_file(canvas_handle(), Path.t(), keyword()) ::
  :ok | {:error, term()}

Export WEBP and write to file path (helper).

fill_circle(arg, cx, cy, radius, rgba)

@spec fill_circle(
  canvas_handle(),
  number(),
  number(),
  number(),
  {0..255, 0..255, 0..255, 0..255}
) ::
  :ok | {:error, term()}

Fill a circle with a color.

fill_rect(handle, x, y, w, h)

@spec fill_rect(canvas_handle(), number(), number(), number(), number()) ::
  :ok | {:error, term()}
@spec fill_rect(canvas_handle(), number(), number(), number(), number()) ::
  :ok | {:error, term()}

Fill an axis-aligned rectangle. No-op for backends that don't support it yet; implemented later.

fill_rect(arg, x, y, w, h, rgba)

@spec fill_rect(
  canvas_handle(),
  number(),
  number(),
  number(),
  number(),
  {0..255, 0..255, 0..255, 0..255}
) :: :ok | {:error, term()}

Fill an axis-aligned rectangle with a color.

hello()

@spec hello() :: :world

Return a simple atom for smoke tests.

load_font(arg, path)

@spec load_font(canvas_handle(), Path.t()) :: :ok | {:error, term()}

Load a TTF/OTF font from a filesystem path for the current backend (when supported).

set_antialias(arg, aa)

@spec set_antialias(canvas_handle(), boolean() | 1 | 4 | 8) :: :ok | {:error, term()}

Enable/disable antialiasing or set sample count (1,4,8).

set_font_size(arg, size)

@spec set_font_size(canvas_handle(), number()) :: :ok | {:error, term()}

Set current font size in points/pixels (backend-defined units).

supports?(backend, feature)

@spec supports?(module(), atom()) :: boolean()

Return true if backend supports the given feature

write_binary(bin, path)

@spec write_binary(binary(), Path.t()) :: :ok | {:error, term()}

Write a binary returned from export_* to a file path (thin helper).