Visualize.SVG (Visualize v0.2.25)

Copy Markdown View Source

Main SVG module providing a fluent API for building SVG documents.

Examples

render/1 returns iodata, which a template writes as it is; flattened, it is the markup:

iex> alias Visualize.SVG
iex> SVG.new(width: 400, height: 300)
...> |> SVG.append(:rect, x: 10, y: 10, width: 100, height: 50, fill: "blue")
...> |> SVG.append(:circle, cx: 200, cy: 150, r: 40, fill: "red")
...> |> SVG.render()
...> |> IO.iodata_to_binary()
~s(<svg height="300" width="400" xmlns="http://www.w3.org/2000/svg">) <>
  ~s(<rect fill="blue" height="50" width="100" x="10" y="10"/>) <>
  ~s(<circle cx="200" cy="150" fill="red" r="40"/></svg>)

Summary

Functions

Appends a child element to the SVG.

Appends multiple children at once.

Creates a circle element

Creates a group element with optional attributes.

Creates a line element

Creates a new SVG root element.

Creates a path element

Creates a polygon element

Creates a polyline element

Creates a rect element

Renders the SVG element tree to an iolist (efficient for Phoenix).

Creates a text element

Renders the SVG element tree to a string.

Wraps elements in a group with a transform.

Functions

append(parent, child)

Appends a child element to the SVG.

Can take either an Element struct or a tag atom with attributes.

append(parent, tag, attrs \\ [])

append_all(parent, children)

Appends multiple children at once.

circle(attrs \\ [])

Creates a circle element

group(attrs \\ [])

@spec group(keyword()) :: Visualize.SVG.Element.t()

Creates a group element with optional attributes.

line(attrs \\ [])

Creates a line element

new(attrs \\ [])

@spec new(keyword()) :: Visualize.SVG.Element.t()

Creates a new SVG root element.

path(attrs \\ [])

Creates a path element

polygon(attrs \\ [])

Creates a polygon element

polyline(attrs \\ [])

Creates a polyline element

rect(attrs \\ [])

Creates a rect element

render(element)

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

Renders the SVG element tree to an iolist (efficient for Phoenix).

text(content, attrs \\ [])

Creates a text element

to_string(element)

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

Renders the SVG element tree to a string.

translate(elements, x, y)

Wraps elements in a group with a transform.