Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Module Mosaic_ui.Canvas
Low-level drawing surface for custom cell-level rendering.
Mutable cell-level drawing surface.
A canvas is a Renderable.t backed by a Grid.t that the caller draws into directly. The grid is resized to match the node's layout dimensions at the start of each render pass. On render the canvas blits its grid to the parent at the node's position.
Use draw_text, fill_rect, draw_box, draw_line, or set_cell to draw into the canvas. Drawing changes persist between frames. To draw at render time, register a callback with set_on_draw; the callback fires after auto-resize each pass. Combine with ~live:true on the renderable for per-frame animation. For one-shot or event-driven drawing, call request_render after drawing to schedule a re-render.
In the declarative Vnode system, use Vnode.canvas.
create ~parent () is a canvas node attached to parent.
The internal grid starts at 1x1 and is resized to match the node's layout dimensions on the first render pass. The grid shares the parent's glyph pool (if available) and uses `Unicode width computation.
Optional parameters:
index is the child index within parent. Defaults to appending at the end.
id is the node identifier. Defaults to None.
style is the layout style. Defaults to the empty style.
visible controls node visibility. Defaults to true.
z_index is the stacking order. Defaults to 0.
opacity is the compositing opacity. Defaults to 1.0.
respect_alpha controls alpha blending within the canvas grid. Defaults to false. The compositing step to the parent always respects source alpha regardless of this flag.
See also set_on_resize to redraw content when dimensions change.
Starts at 1 and is updated to match layout dimensions at the start of each render pass. Inside set_on_draw and set_on_resize callbacks the value reflects the current layout dimensions.
Starts at 1 and is updated to match layout dimensions at the start of each render pass. Inside set_on_draw and set_on_resize callbacks the value reflects the current layout dimensions.
Drawing
Drawing operations write into the canvas grid. Changes persist between frames. Drawing does not auto-schedule a re-render; call request_render when done, or use set_on_draw for render-time drawing.
val draw_text :
?style:Ansi.Style.t->?tab_width:int ->t->x:int ->y:int ->text:string ->
unit
draw_text t ~x ~y ~text draws text as a single line into the canvas grid at column x, row y.
Updates respect_alpha on the underlying grid. Layout dimensions are managed by the layout system and are not affected by this call.
Callbacks
val set_on_draw : t->(t->delta:float -> unit) option-> unit
set_on_draw t cb registers cb as the render-time drawing callback for t, replacing any previously registered callback. None clears the callback.
cb fires each render pass, after the grid has been auto-resized. ~delta is the elapsed time in milliseconds since the last frame. width and height reflect the current layout dimensions inside cb. Content from previous frames persists in the grid; call clear first for full redraws.
Note. Combine with ~live:true on the renderable for per-frame animation.
set_on_resize t cb registers cb as the resize callback for t, replacing any previously registered callback. None clears the callback.
cb fires when the canvas grid is resized due to layout changes. The grid has already been resized when cb is called; use width and height to read the new dimensions. When both a resize callback and a draw callback are registered, the resize callback is called first in the same render pass.
Call this after drawing into the canvas outside of a draw callback to make changes visible. Not needed after clear (which schedules automatically) or inside a set_on_draw callback (which already runs inside a render pass).