Visualize.Backend behaviour (Visualize v0.2.35)

Copy Markdown View Source

Behaviour defining the interface for rendering backends.

Backends convert the intermediate representation (IR) to their target output format (SVG strings, Canvas commands, etc.).

Built-in Backends

Configuration

Set the default backend in your config:

config :visualize, default_backend: :svg

Or specify at render time:

Visualize.Render.to_string(element, backend: :canvas)

Implementing a Custom Backend

To implement a custom backend, define a module that implements this behaviour:

defmodule MyApp.PDFBackend do
  @behaviour Visualize.Backend

  @impl true
  def path_data(path), do: ...

  @impl true
  def render_path(path, opts), do: ...

  @impl true
  def render_element(element, opts), do: ...

  @impl true
  def render_scene(elements, opts), do: ...
end

Summary

Callbacks

Converts a Path IR to backend-specific path data.

Renders an Element IR tree to the backend's output format.

Renders a Path IR to the backend's output format.

Renders a complete scene (list of elements) to output.

Wraps content in a root container.

Functions

Gets the configured default backend module.

Resolves a backend from various inputs.

Types

options()

@type options() :: keyword()

output()

@type output() :: iolist() | String.t() | map() | [tuple()]

Callbacks

path_data(t)

@callback path_data(Visualize.IR.Path.t()) :: String.t() | [tuple()]

Converts a Path IR to backend-specific path data.

For SVG, this returns the d attribute string (e.g., "M10,20L100,200Z"). For Canvas, this returns a list of drawing commands.

render_element(t, options)

@callback render_element(Visualize.IR.Element.t(), options()) :: output()

Renders an Element IR tree to the backend's output format.

render_path(t, options)

@callback render_path(Visualize.IR.Path.t(), options()) :: output()

Renders a Path IR to the backend's output format.

Options may include style information and other backend-specific settings.

render_scene(list, options)

@callback render_scene([Visualize.IR.Element.t()], options()) :: output()

Renders a complete scene (list of elements) to output.

wrap_root(output, options)

(optional)
@callback wrap_root(output(), options()) :: output()

Wraps content in a root container.

For SVG, this wraps in <svg> tags. For Canvas, this may generate the canvas element and setup code.

This callback is optional.

Functions

default()

@spec default() :: module()

Gets the configured default backend module.

resolve(module)

@spec resolve(atom() | nil) :: module()

Resolves a backend from various inputs.

Accepts: