# `Visualize.Hooks.CanvasBinary`
[🔗](https://github.com/dcoai/Visualize/blob/v0.8.3/lib/visualize/hooks/canvas_binary.ex#L1)

Phoenix LiveView hook that replays the canvas binary format on a `<canvas>`.

`CanvasBinaryChart` decodes the stream `Visualize.Backend.CanvasBinary` produces
(spec/09 §4) — from the element's `data-binary` attribute on mount and update, and from
`canvas_update` events pushed by the LiveView — and draws it on a Canvas 2D context.

## Usage

    <div id="chart" phx-hook="CanvasBinaryChart" phx-update="ignore"
         data-binary={@binary} data-width="800" data-height="600">
      <canvas width="800" height="600"></canvas>
    </div>

    # Push a new frame
    push_event(socket, "canvas_update", %{binary: Visualize.Backend.CanvasBinary.to_base64(bin)})

An element carrying `data-stream` reads its stream in segments instead (spec/10 §8.2,
D-203): the base64 `data-c` of each child of the element beside it that carries
`data-vis-stream`, as `Visualize.Render.Segments.binary/2` writes them, decoded and
concatenated, and drawn again when `data-drawn` names another drawing.
`Visualize.Components.Chart` draws this way.

The decoder (`js_decoder/0`) is shared with `Visualize.Hooks.CanvasIncremental` and is
written against `Visualize.Backend.CanvasBinary.Opcodes`: every opcode and every
command-stream sub-opcode has a case — the f64 and the f32 path records (D-73) share one
reader taking the coordinate width, and the cubic run (D-74) has its own — and an unknown
byte throws rather than
desynchronize silently. A full-matrix transform (`0xFF`) composes with `ctx.transform`.

The canvas's drawing size is the element's `data-width` × `data-height` at every draw
(spec/10 §8.2, #456): a size change re-sizes the canvas, which clears it, and redraws
the current `data-binary` even when it is the value last drawn.

# `js_decoder`

Returns the JavaScript of the shared decoder: `decodeBase64`, `drawSvgArc` and
`executeVisualizeBinary(ctx, bytes, startOffset, endOffset)`, which replays one stream
and returns the offset it reached, with the module-private readers of the path records
it calls.

# `js_hook`

Returns the JavaScript code for the CanvasBinaryChart hook.

Requires the decoder of `js_decoder/0` in the same module; `Visualize.Hooks.js_code/0`
includes both.

---

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