Draw Box
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:
PEN (Mode.PEN): Tap/drag creates Element.Path for freehand drawing
RECTANGLE/CIRCLE/TRIANGLE/ARROW/LINE: Tap/drag creates Element.Shape of that type
Rendering Pipeline:
Solid State.bgColor fill
Optional State.bgPattern tiled via a cached ShaderBrush (rasterized once per pattern change with tint baked in; per-frame cost is a single
drawRect)Sort elements by Element.zIndex (lower values render first)
Render each element using renderElement
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 behaviorPerformance 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
drawRectrather than an N×M painter tile loopLarge element lists may impact frame rate; consider implementing virtualization
On-screen canvas changes are tracked via state hashCode
Parameters
Current drawing state containing elements, colors, and current mode
Callback to dispatch intents (user actions)
Layout modifier for the canvas (defaults to fillMaxSize)