Images
Image adds a leaf element with one IMAGE render command. The raylib backend draws the texture inside its bounding box using the chosen scale mode and corner radius.
The shape
Image("id", CC_ImageRef*, .field = value, ...);
The first argument is an id of type const char *, following the container id rules. Pass "" for an anonymous element.
The second argument is a CC_ImageRef *, which pairs a Texture2D * with a scale mode. Create it with ImgFill, ImgFit, or ImgCrop.
The remaining arguments are Clay_ElementDeclaration fields, including .layout, .cornerRadius, .backgroundColor for tinting, .border, and .floating.
Minimal example
Texture2D avatar = CC_LoadImage("resources/avatar.png");
while (CC_Running()) {
CC_Begin();
Row("Header", .layout = {.sizing = {Grow(), Fixed(80)}, .childGap = 12}) {
Image("Avatar", ImgCrop(&avatar),
.layout = {.sizing = {Fixed(64), Fixed(64)}},
.cornerRadius = RadiusAll(999));
Column("HeaderText", .layout = {.sizing = {Grow(), Fit()}}) {
Text("ccompose",
.textColor = Color(236,236,236,255), .fontSize = 24);
}
}
CC_End();
}
CC_UnloadImage(avatar);
RadiusAll(999) produces a circle because the renderer clamps roundness to 1.0 of the shorter side. ImgCrop preserves the texture's aspect ratio and crops overflow. Here the 736×414 texture fills a centered 64×64 square.
Loading a texture
CC_LoadImage wraps raylib's LoadTexture. CC_UnloadImage wraps UnloadTexture.
// In main, after CC_Init():
Texture2D avatar = CC_LoadImage("resources/avatar.png");
// ...use during the frame loop...
// Before CC_Shutdown:
CC_UnloadImage(avatar);
Lifetime rules:
- Load textures after
CC_Init()and unload them beforeCC_Shutdown(), while raylib's GL context exists. - A failed load returns a
Texture2Dwithtex.id == 0. - The loaders support raylib's image formats, including PNG, JPG, BMP, TGA, GIF, and QOI.
CC_LoadImage / CC_UnloadImage are only declared in the raylib backend build (#ifndef CCOMPOSE_NO_BACKEND). Headless builds don't see them because Texture2D doesn't exist there.
Content-scale modes
CC_ImageRef carries a CC_ImageScale enum telling the renderer how the texture should fit inside the element's bounding box:
| Mode | Behavior | CSS analogue |
|---|---|---|
CC_IMAGE_FILL | Stretch to fill the box, aspect ratio ignored | object-fit: fill |
CC_IMAGE_FIT | Preserve aspect, shrink to fit inside the box, transparent letterbox margins | object-fit: contain |
CC_IMAGE_CROP | Preserve aspect, scale so the image covers the whole box, crop overflow evenly | object-fit: cover |
CC_IMAGE_FILL is zero, so a zero-initialized CC_ImageRef stretches the texture to fill its box.
Create a reference with the helper for the scale mode you need:
Image("A", ImgFill(&tex), ...); // stretches to box
Image("B", ImgFit(&tex), ...); // contain with letterbox
Image("C", ImgCrop(&tex), ...); // cover with crop
Choose the mode based on how much of the source image should remain visible:
- Use
ImgFillfor icons or backgrounds that should stretch, or when the source and box have the same aspect ratio. - Use
ImgFitfor galleries and previews that must show the entire image. - Use
ImgCropfor avatars and cards that must fill their box.
Rounded corners
Image honors .cornerRadius on all three scale modes. The raylib backend renders rounded images through a cached RenderTexture2D with a BLEND_MULTIPLIED alpha-mask pass:
- Look up (or allocate) an RT matching the bounding box, keyed on the Clay element id.
- Clear the RT. Draw a white rounded-rect mask at full size.
- Switch to
BLEND_MULTIPLIEDand draw the source texture over the mask. Pixels outside the rounded shape multiply by zero and disappear. - Blit the RT to the screen at the element's bounding box.
Zero-radius images draw directly with DrawTexturePro. The mask pass runs only for rounded images.
The cache holds 32 entries, evicts entries unused for 120 frames, and clears on CC_Shutdown(). It uses the Clay element id as its key. Give rounded images stable, non-empty ids so they reuse their render textures across frames. Changing an id each frame forces reallocation.
Image("Avatar", ImgCrop(&avatar),
.layout = {.sizing = {Fixed(64), Fixed(64)}},
.cornerRadius = RadiusAll(999));
Tinting
On image commands, .backgroundColor tints every pixel. Use Color(255,255,255,255) to keep the texture's original colors:
// Red-tinted overlay
Image("ErrorIcon", ImgFit(&icon),
.layout = {.sizing = {Fixed(24), Fixed(24)}},
.backgroundColor = Color(230, 80, 80, 255));
An all-zero .backgroundColor uses white as the tint. For a black tint, use Color(0,0,0,255).
Interaction with other fields
Image accepts Clay_ElementDeclaration fields and sets .image internally. These fields affect its layout and appearance:
- Give the image a size with
.layout.sizingor.aspectRatio. With no children,Fit()collapses to0×0. UseFixed(...)on both axes orGrow()inside a sized parent. - Set
.aspectRatioto constrain the box, such as16.0f / 9.0f, and useImgCropto fill it without distortion. - Set
.borderfor an inset frame drawn after the image. It follows.cornerRadius. - Use
.floatingfor image layers under text, as in the card example in Containers.
Per-frame ref pool
ImgFill, ImgFit, and ImgCrop call CC_AcquireImageRef() to acquire references from a 128-entry per-frame pool. The pool resets at CC_Begin(), and each pointer remains valid through CC_End(). This keeps the reference alive after the containing Row, Column, or Box block closes.
Calls beyond the 128th return NULL, and Image() draws nothing for those calls. Increase CC_IMAGE_REF_POOL_SIZE in src/image.c if you need more entries.
To manage the reference lifetime yourself, create a CC_ImageRef and pass its address:
CC_ImageRef ref = {&my_texture, CC_IMAGE_CROP};
Image("Thumbnail", &ref,
.layout = {.sizing = {Fixed(96), Fixed(96)}});
Keep ref alive through CC_End(). A local variable in the frame function works if that function ends the frame before returning.
Headless mode
Headless builds exclude Texture2D, CC_ImageRef, CC_LoadImage, and CC_UnloadImage. Tests can still set .image.imageData to a sentinel pointer and inspect the IMAGE command returned by CC_End(). No renderer draws that command.
See Headless mode for the general pattern.
Performance notes
- The mask cache allocates one
RenderTexture2Dper rounded image element. Once cached, each frame uses twoDrawTextureProcalls and oneDrawRectangleRoundedcall. - Resizing a rounded image reallocates its render texture. Avoid changing its dimensions every frame if possible.
- Zero-radius images skip the cache and use one
DrawTextureProcall. - The 128-entry
CC_ImageRefpool uses about 2 KB of BSS, at roughly 16 bytes per entry. Resetting it requires no allocation.