Macaw.Draw.SVG.Element (macaw_draw v0.1.0)

Copy Markdown View Source

An SVG element as data (spec/07 §24), copied from visualize's Visualize.SVG.Element: %Macaw.Draw.SVG.Element{tag, attrs, children, content}, mirroring an element one to one: tag an SVG tag atom, attrs a map, children a list of elements, content text or nil.

iex> alias Macaw.Draw.SVG.Element
iex> Element.svg(width: 10) |> Element.append(Element.circle(r: 2)) |> to_string()
~s(<svg width="10" xmlns="http://www.w3.org/2000/svg"><circle r="2"/></svg>)

new(tag, attrs) and one constructor per tag build elements, each taking a map or a keyword list; attrs/2 merges attributes; append/2 appends an element, a scene IR element (converted by from_ir/1) or a list mixing both; content/2 sets the text. from_ir/1 converts a Macaw.Draw.IR.Element tree to the element tree its SVG renderer writes, exactly: the renderer writes through it (spec/10 §3.2). The element implements String.Chars, and Phoenix.HTML.Safe when Phoenix.HTML is loaded, so it interpolates into a template as markup; Macaw.Draw takes no Phoenix dependency.

Summary

Types

Attributes: a map, or a keyword list.

A refusal: faults with their paths.

t()

Functions

Appends an element, a Macaw.Draw.IR.Element (by from_ir/1), or a list mixing both.

Merges attributes into an element's.

A <circle> element.

A <clipPath> element.

Sets an element's text content; nil clears it.

A <defs> element.

A <ellipse> element.

The element tree the SVG renderer writes for a scene IR element tree (spec/10 §3): each node's tag, its attributes as Macaw.Draw.Render.SVG writes them, its children and its text, so the builder and the renderer write one markup. A clipped node converted alone is its definition and itself, side by side in a group; a node the renderer refuses is left out, as an interpolation leaves it out (spec/10 §3.5).

A <g> element.

A <line> element.

A <linearGradient> element.

An element of tag with attrs.

A <path> element.

A <polygon> element.

A <polyline> element.

A <radialGradient> element.

A <rect> element.

A <stop> element.

A root <svg>, with xmlns="http://www.w3.org/2000/svg".

A <text> element.

A <title> element.

A <tspan> element.

Types

attrs()

@type attrs() :: map() | keyword()

Attributes: a map, or a keyword list.

error()

@type error() :: {:error, [Macaw.Base.Fault.t()]}

A refusal: faults with their paths.

t()

@type t() :: %Macaw.Draw.SVG.Element{
  attrs: map(),
  children: [t()],
  content: String.t() | nil,
  tag: atom()
}

Functions

append(parent, children)

@spec append(t(), t() | Macaw.Draw.IR.Element.t() | [t() | Macaw.Draw.IR.Element.t()]) ::
  t() | error()

Appends an element, a Macaw.Draw.IR.Element (by from_ir/1), or a list mixing both.

attrs(element, attrs)

@spec attrs(t(), attrs()) :: t() | error()

Merges attributes into an element's.

circle(attrs \\ %{})

@spec circle(attrs()) :: t() | error()

A <circle> element.

clipPath(attrs \\ %{})

@spec clipPath(attrs()) :: t() | error()

A <clipPath> element.

content(element, text)

@spec content(t(), String.t() | nil) :: t() | error()

Sets an element's text content; nil clears it.

defs(attrs \\ %{})

@spec defs(attrs()) :: t() | error()

A <defs> element.

ellipse(attrs \\ %{})

@spec ellipse(attrs()) :: t() | error()

A <ellipse> element.

from_ir(node)

@spec from_ir(Macaw.Draw.IR.Element.t()) :: t() | error()

The element tree the SVG renderer writes for a scene IR element tree (spec/10 §3): each node's tag, its attributes as Macaw.Draw.Render.SVG writes them, its children and its text, so the builder and the renderer write one markup. A clipped node converted alone is its definition and itself, side by side in a group; a node the renderer refuses is left out, as an interpolation leaves it out (spec/10 §3.5).

g(attrs \\ %{})

@spec g(attrs()) :: t() | error()

A <g> element.

line(attrs \\ %{})

@spec line(attrs()) :: t() | error()

A <line> element.

linearGradient(attrs \\ %{})

@spec linearGradient(attrs()) :: t() | error()

A <linearGradient> element.

new(tag, attrs \\ %{})

@spec new(atom(), attrs()) :: t() | error()

An element of tag with attrs.

path(attrs \\ %{})

@spec path(attrs()) :: t() | error()

A <path> element.

polygon(attrs \\ %{})

@spec polygon(attrs()) :: t() | error()

A <polygon> element.

polyline(attrs \\ %{})

@spec polyline(attrs()) :: t() | error()

A <polyline> element.

radialGradient(attrs \\ %{})

@spec radialGradient(attrs()) :: t() | error()

A <radialGradient> element.

rect(attrs \\ %{})

@spec rect(attrs()) :: t() | error()

A <rect> element.

stop(attrs \\ %{})

@spec stop(attrs()) :: t() | error()

A <stop> element.

svg(attrs \\ %{})

@spec svg(attrs()) :: t() | error()

A root <svg>, with xmlns="http://www.w3.org/2000/svg".

text(attrs \\ %{})

@spec text(attrs()) :: t() | error()

A <text> element.

title(attrs \\ %{})

@spec title(attrs()) :: t() | error()

A <title> element.

tspan(attrs \\ %{})

@spec tspan(attrs()) :: t() | error()

A <tspan> element.