LavaUI docs GitHub

struct Canvas

Swift

struct Canvas

Custom-drawn control: Yoga sizes the box; the app emits draw commands.

Use this for product-specific widgets (meters, charts, diagram chrome) that should not live in LavaUI. The paint closure runs every emit with the absolute frame; call DrawList primitives freely. No Yoga children.

  • continuousRedraw: register with AnimationDriver so the frame loop keeps painting while true (e.g. a live equalizer). Flip false when idle.
  • onGesture: down/move/up with local + window coordinates, for a synchronized inspection cursor, drag-to-zoom, or range selection. Store paint-only interaction values in @DrawState; ordinary @State intentionally recomputes the owning body on every change.
  • onWheel: deltas plus the pointer's position local to this canvas, for zooming around the cursor rather than the center.
  • onHover: enter/leave, for a canvas whose paint changes under the pointer. A hover fill would come free from a .hoverBackground(); this is for the ones that draw the difference themselves, and it is also what makes the canvas resolvable as a hover target at all.

Sources/LavaUI/Canvas.swift:94

Constructors

init(label: String = "Canvas", width: Dimension = .auto, height: Dimension = .auto, flexGrow: Float = 0, minWidth: Float = 0, minHeight: Float = 0, continuousRedraw: Bool = false, onTap: (() -> Void)? = nil, onGesture: ((CanvasGesture) -> Void)? = nil, onWheel: ((_ dx: Float, _ dy: Float, _ localX: Float, _ localY: Float) -> Void)? = nil, onHover: ((Bool) -> Void)? = nil, onPointerMove: ((_ localX: Float, _ localY: Float) -> CursorShape?)? = nil, paint: @escaping (DrawList, CanvasFrame) -> Void)

Creates a canvas. Every parameter matches the property of the same name.

Sources/LavaUI/Canvas.swift:133

Properties

var continuousRedraw: Bool

When true, this leaf asks for ~60fps redraws via AnimationDriver.

Sources/LavaUI/Canvas.swift:108

var dumpDetail: String { get }

Not documented.

Sources/LavaUI/Canvas.swift:163

var flexGrow: Float

Share of the parent's leftover main-axis space this canvas takes; 0 takes none.

Sources/LavaUI/Canvas.swift:102

var height: Dimension

Height of the box. .auto leaves it to the parent and flexGrow.

Sources/LavaUI/Canvas.swift:100

var label: String

Name shown in the layout tree and to agents.

Sources/LavaUI/Canvas.swift:96

var minHeight: Float

Smallest height layout may give the box, in points.

Sources/LavaUI/Canvas.swift:106

var minWidth: Float

Smallest width layout may give the box, in points.

Sources/LavaUI/Canvas.swift:104

var onGesture: ((CanvasGesture) -> Void)?

Called for every press, drag and release on the canvas. See CanvasGesture.

Sources/LavaUI/Canvas.swift:112

var onHover: ((Bool) -> Void)?

Called with true when the pointer enters the canvas and false when it leaves.

Sources/LavaUI/Canvas.swift:117

var onPointerMove: ((_ localX: Float, _ localY: Float) -> CursorShape?)?

Called as the pointer moves over the canvas with no button held, with its position relative to the canvas's top-left. Returns the cursor to show there, or nil for the canvas's own .cursor.

For a canvas whose pointer depends on what is under it — a link in a document, a handle in a diagram. The renderer says which node is hovered and can never say where inside one, so this is the one hover question that comes back to the app, and only for canvases that ask. Runs on every coalesced move, so keep it to a lookup.

Sources/LavaUI/Canvas.swift:127

var onTap: (() -> Void)?

Called when a button goes down on the canvas — on the press, not the release.

Sources/LavaUI/Canvas.swift:110

var onWheel: ((_ dx: Float, _ dy: Float, _ localX: Float, _ localY: Float) -> Void)?

Called on a wheel scroll over the canvas, with the deltas in notches and the pointer's position relative to the canvas's top-left.

Sources/LavaUI/Canvas.swift:115

var paint: (DrawList, CanvasFrame) -> Void

Draws the canvas. Called on every emit with the canvas's window-space frame. Emit commands into the DrawList; anything drawn outside the frame is not clipped.

Sources/LavaUI/Canvas.swift:130

var width: Dimension

Width of the box. .auto leaves it to the parent and flexGrow.

Sources/LavaUI/Canvas.swift:98

Methods

func mountPrimitive() -> any AnyViewNode

Not documented.

Sources/LavaUI/Canvas.swift:170

func reconcilePrimitive(_ node: any AnyViewNode) -> any AnyViewNode

Not documented.

Sources/LavaUI/Canvas.swift:184