Visualize.SVG.Renderer (Visualize v0.2.35)

Copy Markdown View Source

Renders SVG elements to strings or iolists.

Optimized for efficient string building using iolists.

Summary

Functions

The SVG attribute name for an attribute key, shared by every SVG serialiser (D-11).

Renders an SVG element tree to an iolist.

Renders an SVG element tree to a string.

Functions

attribute_name(key)

@spec attribute_name(atom() | String.t()) :: String.t()

The SVG attribute name for an attribute key, shared by every SVG serialiser (D-11).

A snake_case key hyphenates (:stroke_width → "stroke-width") unless it names one of the camel-cased SVG attributes (:view_box → "viewBox", :preserve_aspect_ratio, :gradient_units, :pattern_units, :clip_path_units, :marker_width, :marker_height, :ref_x, :ref_y, …); an already camel-cased atom and a binary key pass through unchanged.

Examples

iex> Visualize.SVG.Renderer.attribute_name(:stroke_width)
"stroke-width"
iex> Visualize.SVG.Renderer.attribute_name(:view_box)
"viewBox"
iex> Visualize.SVG.Renderer.attribute_name(:viewBox)
"viewBox"

render(element)

@spec render(Visualize.SVG.Element.t()) :: iolist()

Renders an SVG element tree to an iolist.

Using iolists is more efficient than string concatenation and works well with Phoenix/LiveView.

render_to_string(element)

@spec render_to_string(Visualize.SVG.Element.t()) :: String.t()

Renders an SVG element tree to a string.