DrawBox

fun DrawBox(state: State, onIntent: (Intent) -> Unit, modifier: Modifier = Modifier.fillMaxSize(), showGrid: Boolean = true, hiddenElementIds: Set<String> = emptySet(), overlay: @Composable BoxScope.() -> Unit = {}, selectionStyle: SelectionChromeStyle = SelectionChromeStyle.Default)

Main drawing canvas composable supporting both freehand drawing and geometric shapes.

DrawBox is a high-performance drawing surface that:

  • Renders both Element.Path (freehand) and Element.Shape (geometric) elements

  • Handles user gestures (tap, drag) and converts them to intents

  • Adapts gesture behavior based on the current Mode

  • Supports undo/redo for all drawing operations

  • Captures canvas as bitmap for saving

Drawing Modes:

Rendering Pipeline:

  1. Solid State.bgColor fill

  2. Optional State.bgPattern tiled via a cached ShaderBrush (rasterized once per pattern change with tint baked in; per-frame cost is a single drawRect)

  3. Sort elements by Element.zIndex (lower values render first)

  4. Render each element using renderElement

  5. Paths are rendered with strokes, shapes with their specific geometry

Architecture:

DrawBox (UI Layer)
↓ gesture input
Intent dispatch
↓
ViewMode/Reducer (State Layer)
↓ new state
DrawBox recomposes
↓ reads state.mode
Changes gesture behavior

Performance Notes:

  • Uses rememberGraphicsLayer to render elements efficiently

  • Pointer input is keyed by State.mode to update on mode changes

  • State.bgPattern is converted to a tiled ShaderBrush via remember; per-frame work is a single drawRect rather than an N×M painter tile loop

  • Large element lists may impact frame rate; consider implementing virtualization

  • On-screen canvas changes are tracked via state hashCode

Parameters

state

Current drawing state containing elements, colors, and current mode

onIntent

Callback to dispatch intents (user actions)

modifier

Layout modifier for the canvas (defaults to fillMaxSize)

See also

renderElement

for element-specific rendering