# `Macaw.Draw.Render.Canvas`
[🔗](https://github.com/dcoai/macaw_draw/blob/v0.1.0/lib/macaw/draw/render/canvas.ex#L1)

The canvas commands (spec/10 §4.1), copied from visualize's `Visualize.Backend.Canvas`,
`Visualize.Backend.Canvas.Arc` and `Visualize.Backend.Canvas.Output`: a scene written as
Canvas 2D commands.

| Function | Option | Default | Meaning |
|---|---|---|---|
| every function | `:format` | `:json` | `:commands`, a list of `{name, args}` terms; `:json`, an iolist of a JSON array of `{"cmd": name, "args": [...]}` with no whitespace |
| `render_path/2` | `:style` | `%{}` | the style the path is drawn in |
| every function | `:resolve` | `:literal` | the theme resolution the scene was drawn in; a canvas takes literal colors |

**Every command names a Canvas 2D method or property** (visualize D-37), a `set_<name>`
command the property `<name>`, but the seven the replay runs itself (§7.9):
`nonScalingLineWidth`, `linearGradient`, `radialGradient`, `hatchPattern`,
`rotate_around`, `motion` and `motionEnd` (`replayed/0`). visualize's `:js` script output
and its root wrapper with an inline `<script>` are not carried over: a page evaluates no
script (macaw spec/10 §1).

**Commands per node** (§4.1): a leaf's setup (its transform's commands, then its style's
setup) is its own, inside `save`/`restore` when it is not empty (visualize D-179);
`:path` → `beginPath`, the path's commands, the apply; `:rect` → `rect`; `:circle` →
`arc(cx, cy, r, 0, 2π)`; `:ellipse` → `ellipse`; `:line` → `moveTo`, `lineTo` and only a
stroke; `:polyline`, `:polygon` → `moveTo` then `lineTo`s, a polygon closed; `:text` →
its text style, `strokeText` first where `stroke_first?/2`, then `fillText` where it
`fills?/2`, one per `tspan` line; `:group` → `save`, transform, style setup, children,
`restore`; `:root` → its children; `:image` → `drawImage`; a clipped node → `save`, its
transform, `beginPath`, the shape's geometry, `clip` (inside) or the ±100,000
rectangle and `clip("evenodd")` (outside), its own commands without its transform,
`restore`; definitions, `:title` and `:desc` → nothing.

**The style** (§4.1.1): `set_fillStyle`, `set_strokeStyle`, `set_lineWidth` or
`nonScalingLineWidth`, `set_globalCompositeOperation`, an effect's shadow or filter,
`set_lineCap`, `set_lineJoin`, `setLineDash`, `set_lineDashOffset` and
`set_globalAlpha`, each for a key present. **The apply** is `fill` where the node
`fills?/2`, then `stroke` where it `strokes?/2`, each at its paint's own opacity. **A
node's opacity composes** (§2.3, visualize D-304): a child's `opacity` is written as the
product of its own and every group's above it. **A paint is drawn, not set** (D-175): a
gradient over the node's tight box, a hatch from the user-space origin, inside
`save`/`restore`. **Motion spans** (D-176): a node with `"data-mw-id"` is drawn between
`motion(id, box, transitions [, animation [, enter, exit]])` and `motionEnd`.

**Faults** (§2.5): a scene is checked as the SVG renderer checks it before a command is
written, so a node no target can write is the same fault on each; the JSON serializer
refuses a command of no known shape with `{:bad_command, command}`.

# `command`

```elixir
@type command() :: {atom(), list()}
```

A canvas command: a method or property name and its arguments.

# `error`

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

A refusal: faults with their paths.

# `canvas_names`

```elixir
@spec canvas_names() :: %{methods: [atom()], properties: [atom()]}
```

The Canvas 2D methods the commands call, and the `set_` commands of its properties.

# `json`

```elixir
@spec json([command()]) :: {:ok, iodata()} | error()
```

Commands as a JSON array of `{"cmd": name, "args": [...]}`, with no whitespace: strings
escaped, numbers formatted as path data is (`Macaw.Draw.IR.Path.format_number/1`), a
`set_` command named as it is. A command of no known shape is `{[i], {:bad_command,
command}}`, `i` its index, where visualize raised.

    iex> {:ok, json} = Macaw.Draw.Render.Canvas.json([{:moveTo, [0, 1.5]}, {:set_fillStyle, ["#f00"]}])
    iex> IO.iodata_to_binary(json)
    ~s([{"cmd":"moveTo","args":[0,1.5]},{"cmd":"set_fillStyle","args":["#f00"]}])

# `path_data`

```elixir
@spec path_data(Macaw.Draw.IR.Path.t()) :: [command()] | error()
```

A path's commands, absolute: `moveTo`, `lineTo`, `bezierCurveTo`, `quadraticCurveTo`,
`closePath`, and an arc as `ellipse` by the endpoint-to-center conversion (a zero radius
a `lineTo`, nothing when its end is the current point).

A path opens at the origin, as `Macaw.Draw.IR.Path.absolute/1` walks it, so one whose
first command that draws is no move begins with `moveTo(0, 0)`. A canvas has no such
point: its first `lineTo` would open the subpath where it ends, and a `closePath` would
return there, not to the origin this walk returns to, so an arc after it would be drawn
from a point the canvas is not at (macaw_frame#85).

    iex> Macaw.Draw.Render.Canvas.path_data(Macaw.Draw.IR.Path.from_commands([{:M, 1, 2}, {:l, 3, 0}, :Z]))
    [{:moveTo, [1, 2]}, {:lineTo, [4, 2]}, {:closePath, []}]

# `render_node`

```elixir
@spec render_node(Macaw.Draw.IR.Element.t(), keyword()) ::
  {:ok, [command()] | iodata()} | error()
```

A node and everything under it, as commands or JSON.

    iex> Macaw.Draw.Render.Canvas.render_node(Macaw.Draw.IR.Element.circle(1, 2, 3, %{fill: "#f00"}), format: :commands)
    {:ok, [{:save, []}, {:set_fillStyle, ["#f00"]}, {:beginPath, []}, {:arc, [1, 2, 3, 0, 6.283185307179586]}, {:fill, []}, {:restore, []}]}

# `render_path`

```elixir
@spec render_path(Macaw.Draw.IR.Path.t(), keyword()) ::
  {:ok, [command()] | iodata()} | error()
```

A path alone: the `:style` option's setup, `beginPath`, the path's commands and its
draws.

    iex> path = Macaw.Draw.IR.Path.from_commands([{:M, 0, 0}, {:L, 4, 0}])
    iex> Macaw.Draw.Render.Canvas.render_path(path, format: :commands, style: %{stroke: "#000", fill: :none})
    {:ok, [{:set_strokeStyle, ["#000"]}, {:beginPath, []}, {:moveTo, [0, 0]}, {:lineTo, [4, 0]}, {:stroke, []}]}

# `render_scene`

```elixir
@spec render_scene([Macaw.Draw.IR.Element.t()], keyword()) ::
  {:ok, [command()] | iodata()} | error()
```

A list of nodes, their commands in order.

# `replayed`

```elixir
@spec replayed() :: [atom()]
```

The seven commands that name no Canvas 2D method or property, which the replay runs
itself (§4.1, §7.9).

---

*Consult [api-reference.md](api-reference.md) for complete listing*
