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

The incremental canvas (spec/10 §4.9), copied from visualize's
`Visualize.Backend.CanvasIncremental` and the stale-column scroll of `Visualize.Incremental`
(visualize spec/09 §5, §7, D-38, D-106, D-324): a scrolling plot sends what scrolled in,
not the whole plot.

- **`scroll_mode/2`**, in precedence: `:none` for `{0, 0}`; `:full_redraw` when `|dx| ≥
  0.5·width` or `|dy| ≥ 0.5·height`; `:scroll_x` when `dy = 0`; `:scroll_y` when `dx = 0`;
  else `:scroll_xy`.
- **`exposed_regions/2`**: `[]` for no offset; one strip for a horizontal or vertical
  scroll; two strips forming an L for a diagonal one, the corner counted once; never
  more than two; strips without area removed.
- **`filter_to_regions/2`** answers the rows whose pixel position (two packed columns,
  already scaled) lies inside a region, bounds inclusive, by index, never row by row.
- **The record** `0x60` (`encode_incremental/4`): `mode:u8 dx:f32 dy:f32 vx vy vw vh:f32
  region_count:u8`, then per region `x y w h:f32 data_length:u32 data`, each region's
  data a whole binary stream (§4.2). `mode` is classified against the `viewport:` (the
  whole canvas by default). A zero offset is `{:error, :zero_offset}`, more than three
  regions `{:error, {:regions, n}}`, and an offset `scroll_mode/2` classifies
  `:full_redraw` `{:error, :full_redraw}`, a full redraw being `encode_full_redraw/2`'s.
- **The full redraw** (`encode_full_redraw/2`) is the same 27-byte header, mode `0x00`,
  a zero offset, the viewport and region count `0`, then the whole stream.
- **The clearing record** `0x61` (`clear: true`): `0x60`'s layout, mode `0x00` in place
  for a zero offset, an offset `scroll_mode/2` classifies `:full_redraw` `{:error,
  :full_redraw}`; the reader clears each region before it replays it.
- **`stale_regions/3`**: a scroll whose copied pixels are right only left of a column.

# `mode`

```elixir
@type mode() :: :scroll_x | :scroll_y | :scroll_xy
```

A scroll's mode.

# `region`

```elixir
@type region() :: {number(), number(), number(), number()}
```

A rectangle `{x, y, w, h}` of the canvas.

# `encode_full_redraw`

```elixir
@spec encode_full_redraw(Macaw.Draw.IR.Element.t(), {number(), number()}) ::
  {:ok, binary()} | {:error, [Macaw.Base.Fault.t()]}
```

The full redraw of `node` on a canvas `{width, height}`: the 27-byte header of mode
`0x00`, a zero offset, the canvas as the viewport, no region, then the whole stream.

# `encode_incremental`

```elixir
@spec encode_incremental(
  {number(), number()},
  {number(), number()},
  [{region(), Macaw.Draw.IR.Element.t()}],
  keyword()
) ::
  {:ok, binary()}
  | {:error,
     :zero_offset
     | :full_redraw
     | {:regions, pos_integer()}
     | [Macaw.Base.Fault.t()]}
```

The incremental record of a scroll by `{dx, dy}` on a canvas `{width, height}`, each
`{region, node}` of `regions` the region's bounds and the scene drawn there:
`viewport:` the rectangle that scrolls (the whole canvas by default), `clear: true` the
clearing record `0x61`, `precision:` the streams'.

# `exposed_regions`

```elixir
@spec exposed_regions({number(), number()}, {number(), number()}) ::
  [{float(), float(), float(), float()}] | {:error, [Macaw.Base.Fault.t()]}
```

The regions a scroll by `{dx, dy}` exposes on a canvas `{width, height}`.

    iex> Macaw.Draw.Render.Incremental.exposed_regions({20, 0}, {800, 600})
    [{780.0, 0.0, 20.0, 600.0}]
    iex> Macaw.Draw.Render.Incremental.exposed_regions({20, 30}, {800, 600})
    [{780.0, 0.0, 20.0, 600.0}, {0.0, 570.0, 780.0, 30.0}]

# `filter_to_regions`

```elixir
@spec filter_to_regions({[term()] | binary(), [term()] | binary()}, [region()]) ::
  [non_neg_integer()] | {:error, [Macaw.Base.Fault.t()]}
```

The rows whose pixel position lies inside one of `regions`, bounds inclusive: the
indexes, ascending, of the rows of two packed columns `{xs, ys}` (lists, or binaries of
little-endian f64s, positions already scaled) that do. A position that is not two
numbers is left out. `Macaw.Draw` reads columns, never a table (spec/06 §1.1), so the
Frame keeps these rows of its table (`Macaw.Frame.Render.Strip.filter_to_regions/3`).

    iex> Macaw.Draw.Render.Incremental.filter_to_regions({[10, 790, 795], [20, 300, 700]}, [{780.0, 0.0, 20.0, 600.0}])
    [1]

# `scroll_mode`

```elixir
@spec scroll_mode({number(), number()}, {number(), number()}) ::
  :none | mode() | :full_redraw | {:error, [Macaw.Base.Fault.t()]}
```

The mode of a scroll by `{dx, dy}` over a viewport `{width, height}`.

    iex> Macaw.Draw.Render.Incremental.scroll_mode({20, 0}, {800, 600})
    :scroll_x
    iex> Macaw.Draw.Render.Incremental.scroll_mode({400, 0}, {800, 600})
    :full_redraw

# `stale_regions`

```elixir
@spec stale_regions({number(), number()}, {number(), number()}, number() | nil) ::
  :full_redraw
  | {:plain, mode(), [region()]}
  | {:clearing, mode() | :in_place, [region()]}
  | {:error, [Macaw.Base.Fault.t()]}
```

A scroll by `offset` on a canvas `size` whose copied pixels are right only left of the
column `x` (`nil` for none): `:full_redraw` when every copied pixel is stale (`x ≤ 0`) or
the offset is a full redraw anyway; `{:plain, mode, exposed_regions/2}` when none is
(`x ≥ width` or `nil`); otherwise `{:clearing, mode, regions}`, the exposed regions cut
at `x` and then the clearing region `{x, 0, width − x, height}`, or `{:clearing,
:in_place, [that region]}` for no offset. The regions never overlap.

    iex> Macaw.Draw.Render.Incremental.stale_regions({20, 0}, {800, 600}, 700)
    {:clearing, :scroll_x, [{700, 0, 100, 600}]}

---

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