Skip to main content

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.

The shape​

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.
  • paint is your callback. It runs once per frame at this element's position in the render command stream.
  • &state is a void * passed to paint. It may be NULL.
  • The remaining arguments are Clay_ElementDeclaration fields. ccompose reserves .custom for the callback.

Three flavours differ only in child layout direction:

MacroDirectionSame as
Drawtop-to-bottomColumn
DrawRowleft-to-rightRow
DrawColumntop-to-bottomColumn

Callback signature​

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 Draw block stack on top of what you painted.
  • Siblings rendered after still paint on top of both.
  • The .backgroundColor you set on the Draw element is drawn before your callback runs.

Leaf vs block form​

// 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.

Passing state​

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.

.custom is reserved​

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.

Slot pool​

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_AcquireDrawSlot returns NULL.
  • Draw, DrawRow, and DrawColumn still open a layout element. The renderer draws its background, border, and corners but skips the callback because .custom.customData is NULL.
  • 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.

Escape hatch: attaching a callback to a non-Draw element​

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.

Gotchas​

  • In headless builds (-DCCOMPOSE_BACKEND_RAYLIB=OFF), the macros still declare layout elements, but callbacks never run and CC_AcquireDrawSlot returns NULL.
  • Callbacks follow Clay's render command order. Avoid relying on draw order across unrelated branches.
  • A callback can draw outside bb. Set .clip on 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 _tag to ccompose.

See also​

  • Containers: Column / Row / Box / Element for normal flow.
  • Lifecycle: when CC_Begin / CC_End run and when the pool resets.
  • Headless mode: compile-time backend switch.