Repository navigation
Custom Drawing
Use Draw, DrawRow, or DrawColumn to paint custom content inside a rectangle positioned by Clay. Your callback can draw gradients, charts, vector paths, or other content through raylib.
void paint(CC_BoundingBox bb, void *user);
Draw("canvas", paint, &state,
.layout = { .sizing = { Grow(), Fixed(200) } },
.cornerRadius = RadiusAll(8));-
"canvas"is the element id used for hover, click, and scroll identity. -
paintis your callback. It runs once per frame at this element's position in the render command stream. -
&stateis avoid *passed topaint. It may beNULL. - The remaining arguments are
Clay_ElementDeclarationfields. ccompose reserves.customfor the callback.
Three flavours differ only in child layout direction:
| Macro | Direction | Same as |
|---|---|---|
Draw |
top-to-bottom | Column |
DrawRow |
left-to-right | Row |
DrawColumn |
top-to-bottom | Column |
typedef void (*CC_DrawFn)(CC_BoundingBox bb, void *user);bb is the element's absolute-pixel rectangle (top-left origin, y grows down). Use bb.x / bb.y / bb.width / bb.height to position your primitives.
static void paint_progress(CC_BoundingBox bb, void *user) {
float *pct = (float *)user; // 0.0 .. 1.0
DrawRectangle((int)bb.x, (int)bb.y,
(int)(bb.width * (*pct)),
(int)bb.height,
(Color){ 80, 180, 255, 255 });
}
static float progress = 0.42f;
Draw("progress", paint_progress, &progress,
.layout = { .sizing = { Grow(), Fixed(6) } },
.backgroundColor = Color(40, 40, 50, 255),
.cornerRadius = RadiusAll(3));Clay emits the callback in its render command stream:
- Children declared inside the
Drawblock stack on top of what you painted. - Siblings rendered after still paint on top of both.
- The
.backgroundColoryou set on the Draw element is drawn before your callback runs.
// Leaf: no children, callback draws everything:
Draw("canvas", paint_fn, &state,
.layout = {.sizing = {Grow(), Fixed(200)}},
.cornerRadius = RadiusAll(8));
// Block: callback draws first, children layered on top:
Draw("canvas", paint_fn, &state, .layout = {...}) {
Text("Overlay", .fontSize = 20, ...);
}Both forms use the container macros' single-iteration for loop. Follow the container control-flow rules: avoid break and use braces around conditional container blocks.
Use user to pass colors, animation state, texture pointers, or other data to the callback. Keep that data valid through CC_End(), when Clay_EndLayout invokes the callback.
typedef struct Chart {
const float *samples;
int count;
Color stroke;
} Chart;
static void paint_chart(CC_BoundingBox bb, void *user) {
Chart *c = (Chart *)user;
// ... draw c->samples across [bb.x, bb.x+bb.width] ...
}
Chart c = { my_series, 128, (Color){ 200, 220, 255, 255 } };
Draw("chart", paint_chart, &c,
.layout = { .sizing = { Grow(), Fixed(120) } });A local variable in the frame function remains valid if that function calls CC_End() before returning.
The Draw macro assigns .custom last, overwriting any .custom = {...} argument. ccompose uses that field to carry the callback to the renderer.
// The macro overwrites this .custom payload.
Draw("canvas", paint, NULL,
.custom = { .customData = my_thing },
.layout = { ... });The same goes for .custom = Shadow(...): on a Draw element it is dropped. Put the shadow on a parent element that wraps the drawn one. See Styling.
For direct control of .custom, use Element, Column, Row, or Box with CC_AcquireDrawSlot, as shown below.
Draw slots come from a per-frame pool of CC_DRAW_POOL_SIZE entries, 128 by default. CC_Begin() resets the pool without heap allocation. When the pool is full:
-
CC_AcquireDrawSlotreturnsNULL. -
Draw,DrawRow, andDrawColumnstill open a layout element. The renderer draws its background, border, and corners but skips the callback because.custom.customDataisNULL. - ccompose reports no error. Check the return value when acquiring slots directly.
Every CircularProgress ring takes one slot from this pool too, so rings and Draw elements count toward the same cap.
If you need more than 128 slots per frame, batch related drawing into one callback.
For a runtime layout direction or direct control of .custom, acquire a slot explicitly:
Element(direction, "canvas",
.layout = { .sizing = { Grow(), Fixed(200) } },
.custom = { .customData = CC_AcquireDrawSlot(paint_fn, &state) }) {
/* optional children */
}The returned pointer is valid until the next CC_Begin(). Don't hold onto it across frames.
- In headless builds (
-DCCOMPOSE_BACKEND_RAYLIB=OFF), the macros still declare layout elements, but callbacks never run andCC_AcquireDrawSlotreturnsNULL. - Callbacks follow Clay's render command order. Avoid relying on draw order across unrelated branches.
- A callback can draw outside
bb. Set.clipon the element or an ancestor to hide overflow. See Scroll and clip. - The renderer also supports Clay's vendored 3D-model demo. ccompose identifies its own slots with
_tag = -1. Leave_tagto ccompose.
-
Containers:
Column/Row/Box/Elementfor normal flow. -
Lifecycle: when
CC_Begin/CC_Endrun and when the pool resets. - Headless mode: compile-time backend switch.