Visualize.Hooks.Canvas (Visualize v0.2.35)

Copy Markdown View Source

Phoenix LiveView hook that replays Canvas 2D commands on a <canvas>.

CanvasChart executes the :json output of Visualize.Backend.Canvas from the element's data-commands attribute, or its :js output from data-script — the two attributes Visualize.Backend.Hybrid emits — on mount, on update, and on canvas_commands events pushed by the LiveView.

Usage

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

# Push a new frame
push_event(socket, "canvas_commands", %{commands: Visualize.Render.to_canvas_string(el, format: :json)})

Every command of the :json output names a Canvas 2D method (set_* commands assign the property), so the hook needs no path helpers (D-37).

Summary

Functions

Returns the JavaScript code for the CanvasChart hook.

Functions

js_hook()

Returns the JavaScript code for the CanvasChart hook.