Layout
Most of fx draws at coordinates you give it. That is right for motion graphics, where the positions are the animation, and for a handful of things whose place you know. It stops being right when sizes depend on content: a button as wide as its label, a paragraph that wraps and pushes down what follows, a list of rows, a sidebar next to a page. For those, layout is a small flexbox. You describe a tree of boxes, and it measures the text, shares out the space, places everything and draws it.
It is made for mockups of interfaces, and the interface kit is built on it.
A first layout#
layout :: proc(r: Rect, b: Box = {}) -> bool
box :: proc(b: Box) -> bool
item :: proc(b: Box)
txt :: proc(s: string, f: Font, c: Color, b: Box = {})
glyph :: proc(name: string, size: f32, c: Color, b: Box = {}, width: f32 = 1.6)
spacer :: proc(weight: f32 = 1)layout opens a tree whose root fills the rect r. box adds a container; what you call inside its scope becomes its children. Both always return true and are written as if statements only for the scope: the box closes, and the layout is computed and drawn, when its block ends.
The leaves are item (a box without children: a divider, a swatch, a bar), txt (text), glyph (an icon of the icon set, size pixels square, stroked width units wide on its 24-unit grid), picture (an image, below) and spacer (an invisible item that takes the free space along its parent's direction, pushing its neighbours apart).
Every one of them is described by a Box, a struct with a field for everything an element can have. Fields you leave out are zero, and zero always means "nothing special": a row, as big as its content, no padding, no background.

layout_first :: proc(s: fx.Shot) {
fx.backdrop(.Night)
th := fx.theme
body := "Every frame is a pure function of time, so the frames of a video can be drawn in any order, on every core at once."
// the root always fills its rect; this one only centers the card
if fx.layout(fx.stage(), {justify = .Center, align = .Center}) {
card := fx.Box{dir = .Col, w = fx.fixed(knob("width", 1100, 700, 1700)), pad = fx.pads(56), gap = 32, bg = th.sf, radius = 28}
if fx.box(card) {
if fx.box({dir = .Row, w = fx.grow(), gap = 20, align = .Center}) {
fx.glyph("video", 54, th.red)
fx.txt("Renders", fx.sans(62, 600, -0.02), th.ink)
fx.spacer()
fx.txt("3 running", fx.mono(32), th.sub)
}
fx.item({w = fx.grow(), h = fx.fixed(2), bg = th.bd})
// the card is as tall as its content, so it grows as the text is typed
fx.txt(fx.typed(body, s.lt, 0.5, 3.2), fx.sans(42), th.mut, {wrap = true})
}
}
}The root always has exactly the size of r, whatever its content. For a card that is as big as what is in it, make the card a box inside a root that only positions it, as above.
Sizes#
Size :: struct {
kind: Sizing, // Fit, Grow, Fixed, Percent
v: f32,
min, max: f32, // 0 = unbounded
}
fit :: proc "contextless" (min: f32 = 0, max: f32 = 0) -> Size
fixed :: proc "contextless" (v: f32) -> Size
grow :: proc "contextless" (weight: f32 = 1, min: f32 = 0, max: f32 = 0) -> Size
pct :: proc "contextless" (p: f32) -> SizeA box has a w and an h, each one of four kinds:
| Size | The element is |
|---|---|
fit(min, max) |
As big as its content: its children plus gaps and padding, or its text. This is the default. |
fixed(v) |
v pixels. |
grow(weight, min, max) |
Along the parent's direction: its content size plus a share of the space the parent has left over, in proportion to weight. Across it: as big as the parent's inside. |
pct(p) |
p (0..1) of the parent's inside, which is its size minus its padding. |
min and max bound a fit or grow size in pixels.

layout_sizes :: proc(s: fx.Shot) {
fx.backdrop(.Night)
th := fx.theme
rows := [2][3]struct {
name: string,
w: fx.Size,
} {
{{"fixed(300)", fx.fixed(300)}, {"fit()", fx.fit()}, {"grow()", fx.grow()}},
{{"pct(0.25)", fx.pct(0.25)}, {"grow()", fx.grow()}, {"grow(2)", fx.grow(2)}},
}
w := 1340 + 380 * math.sin(s.p * math.TAU) // the container breathes; watch who gives way
if fx.layout({960 - w / 2, 310, w, 460}, {dir = .Col, pad = fx.pads(28), gap = 20, border = th.ns, border_w = 2, radius = 24}) {
for row in rows {
if fx.box({dir = .Row, w = fx.grow(), h = fx.grow(), gap = 20}) {
for c, i in row {
cell := fx.Box{w = c.w, h = fx.grow(), pad = fx.padxy(28, 0), justify = .Center, align = .Center, radius = 14}
cell.bg = i == 0 ? th.na : th.nt
if fx.box(cell) do fx.txt(c.name, fx.mono(32), th.night)
}
}
}
}
}Two details are different from CSS and worth knowing:
- A growing element starts from its own content size and gets its share of the leftover space on top. Two
grow()boxes with different content are therefore not the same width. For equal columns usepct, or give them equal content. - A
pctchild counts as zero when its parent measures its content, so afitparent does not make room for it. Usepctinside parents whose size is fixed or grown.
When the children need more room than there is, growing elements and wrapped text shrink in proportion, down to nothing. Fixed and fitted elements keep their size and stick out of the parent.
Direction, gap, padding, alignment#
Dir :: enum u8 {Row, Col}
Justify :: enum u8 {Start, Center, End, Between}
Align :: enum u8 {Start, Center, End}
pads :: proc "contextless" (v: f32) -> [4]f32 // the same on all four sides
padxy :: proc "contextless" (x, y: f32) -> [4]f32 // left/right, top/bottomdir lays the children out in a row (the default) or a column. gap is the space between neighbours, pad the space between the box's edge and its children, as {top, right, bottom, left}.
justify places the children along the direction: at the start, centered, at the end, or with the free space shared out between them (.Between replaces gap). align places each child across the direction. There is no "stretch": a child fills the cross direction when its own size there is grow().

layout_align :: proc(s: fx.Shot) {
fx.backdrop(.Night)
th := fx.theme
justify := fx.Justify(int(s.lt) % 4) // the next one every second
align := fx.Align(int(knob("align", 1, 0, 2) + 0.5))
root := fx.Box{dir = .Row, justify = justify, align = align, gap = knob("gap", 24, 0, 160)}
root.pad, root.border, root.border_w, root.radius = fx.pads(36), th.ns, 2, 24
if fx.layout({260, 150, 1400, 560}, root) {
for h, i in ([?]f32{200, 340, 130}) {
fx.item({w = fx.fixed(240), h = fx.fixed(h), bg = i == 1 ? th.na : th.nt, radius = 16})
}
}
fx.label(fmt.tprintf("justify = .%v align = .%v", justify, align), {960, 850}, fx.mono(40), th.nm, {0.5, 0.5})
}How a box looks#
| Field | Effect |
|---|---|
bg |
Fill color. |
paint |
A paint instead of bg, for a gradient. Its points are stage coordinates, not relative to the box. |
border, border_w |
Outline color and width in px (1 when only the color is set). |
sides |
Which sides get the border, as a set of .Top, .Right, .Bottom, .Left. Empty means all four. A partial border is drawn as straight lines and ignores radius. |
radius |
Corner radius of background, border, shadow and clip. |
shadow |
A Shadow{dx, dy, blur, spread, c} behind the box; drawn when c is set. |
clip |
Cuts the children off at the box's (rounded) edge. |
float, at |
A floating box takes no space in its parent and is drawn above its siblings, with its top-left corner at pixels from the parent's top-left corner (padding is not added). |
picture :: proc(img: ^sk.Image, b: Box, fit := Image_Fit.Cover)picture puts an image (from load_image) into the tree. An image has no content size here, so give it a w and an h. A picture is not rounded by its own radius; put it in a box with radius and clip.

layout_look :: proc(s: fx.Shot) {
fx.backdrop(.Night)
th := fx.theme
img := fx.load_image("assets/frame.jpg")
if fx.layout(fx.stage(), {justify = .Center, align = .Center}) {
card := fx.Box{dir = .Col, w = fx.fixed(980), bg = th.sf, border = th.bds, border_w = 2}
card.radius, card.clip = knob("radius", 32, 0, 100), true // children are cut to the rounded shape
card.shadow = {dy = 30, blur = 80, c = 0xaa000000}
card.dy = 12 * math.sin(s.p * math.TAU)
if fx.box(card) {
fx.picture(img, {w = fx.grow(), h = fx.fixed(450)})
if fx.box({dir = .Col, w = fx.grow(), pad = fx.padxy(44, 30), gap = 8}) {
fx.txt("Latent space", fx.sans(60, 600, -0.02), th.ink)
fx.txt("Scene 3 of 8", fx.sans(34), th.mut)
}
foot := fx.Box{dir = .Row, w = fx.grow(), pad = fx.padxy(44, 24), bg = th.sf2, border = th.bds, border_w = 2, sides = {.Top}}
if fx.box(foot) {
fx.txt("19.00 s", fx.mono(32), th.mut)
fx.spacer()
fx.txt("1920 × 1080", fx.mono(32), th.mut)
}
// a float takes no space; `at` is measured from the parent's top-left corner
if fx.box({float = true, at = {36, 36}, pad = fx.padxy(18, 8), bg = th.red, radius = 10}) {
fx.txt("new", fx.caps(28, 0.12, 500), th.sf)
}
}
}
}An element is drawn in this order: shadow, background, border, the draw callback, its own text, icon or image, its children, then its floating children.
Text#
txt takes the text, a Font and a color. Without anything else it is one line, as wide as the text, and it never shrinks.
Set wrap and it wraps at whatever width the layout gives it; unless you set w yourself, a wrapped text grows to fill its parent. talign (.Left, .Center, .Right) aligns the lines of wrapped text inside that width. It has no effect on one-line text, which is only as wide as itself: to right-align a single line, put it in a box with justify = .End (the percentages in the next demo).
Padding belongs to containers. A Box given to txt, glyph or picture sets the element's id, size, background, border and animation, but its pad is ignored; wrap the element in a box to pad it. glyph always sets its own w and h.
All the text of one txt has one font and one color. For a line with mixed weights or colors, use a row of txt elements.
Custom drawing#
draw: proc(r: Rect, data: rawptr)
data: rawptrWhen an element should contain something the layout has no element for (a chart, a progress bar, a shader), give it a draw procedure. It is called with the element's final rect, after the background and border and before the children, and may draw anything with the rest of fx. data is handed to it unchanged.

layout_draw :: proc(s: fx.Shot) {
fx.backdrop(.Night)
th := fx.theme
jobs := [?]struct {
name: string,
speed, done: f32,
}{{"fable", 0.36, 0}, {"showreel", 0.2, 0}, {"spot", 0.11, 0}}
bar :: proc(r: fx.Rect, data: rawptr) { // called with the element's final rect
done := (^f32)(data)^
fx.rrect(r, r.h / 2, fx.theme.sf3)
fx.rrect({r.x, r.y, max(r.w * done, r.h), r.h}, r.h / 2, done == 1 ? fx.theme.ok : fx.theme.ink)
}
if fx.layout({410, 260, 1100, 560}, {dir = .Col, pad = fx.pads(56), justify = .Between, bg = th.sf, radius = 28}) {
for &j in jobs { // by reference: the pointer is read when the layout ends
j.done = fx.clamp01((s.lt - 0.3) * j.speed)
if fx.box({dir = .Row, w = fx.grow(), gap = 32, align = .Center}) {
fx.txt(j.name, fx.sans(42, 500), th.ink, {w = fx.fixed(240)})
fx.item({w = fx.grow(), h = fx.fixed(26), draw = bar, data = &j.done})
if fx.box({w = fx.fixed(110), justify = .End}) { // right-aligned in a fixed column
fx.txt(fmt.tprintf("%d%%", int(j.done * 100)), fx.mono(34), th.mut)
}
}
}
}
}The callback runs when the layout's scope ends, not where the element is declared, so whatever data points to must still be alive and unchanged then. A pointer to a loop variable is neither; point into the array (for &j in jobs), or copy the value with new_clone(v, context.temp_allocator), which lasts until the frame is done.
Animating elements#
o: Maybe(f32), // opacity, nil = 1
dx, dy: f32, // offset in px
s: Maybe(f32), // scale, nil = 1
rot: f32, // degrees
blur: f32, // gaussian blur sigma
gray: f32, // 0..1 desaturateThese fields of Box animate one element, with its children, around its own center. They are the same as the fields of a group, and are applied after the layout is done: a moved, scaled or faded element still occupies the place it was given, and its neighbours do not react. That is what you want for entrances and for emphasis.
To animate the layout itself, animate what it is computed from: a fixed width, a gap, a padding, the text. The tree is rebuilt every frame, so any field can be a function of time.

layout_animate :: proc(s: fx.Shot) {
fx.backdrop(.Night)
th := fx.theme
names := [?]string{"s1_prompt", "s2_attention", "s3_latent", "s4_reasoning", "s5_title"}
out := fx.ep(s.lt, 3.6, 4.1, fx.ease_in)
if fx.layout({460, 120, 1000, 840}, {dir = .Col, pad = fx.pads(48), gap = 20, bg = th.sf, radius = 28, o = 1 - out}) {
fx.txt("Scenes", fx.sans(58, 600, -0.02), th.ink)
for name, i in names {
k := fx.stagger(s.lt, i, 0.3, knob("step", 0.12, 0, 0.4), 0.6)
pick := i == 2 ? fx.ep(s.lt, 2.0, 2.3, fx.ease_back) : 0 // one row is picked later
row := fx.Box{dir = .Row, w = fx.grow(), h = fx.grow(), pad = fx.padxy(28, 0), gap = 22, align = .Center, radius = 14}
row.bg = fx.mix(th.sf2, th.ink, pick)
row.o, row.dy, row.blur = k, 50 * (1 - k), 12 * (1 - k) // rises in, sharpening
row.s = 1 + 0.06 * pick // and the picked one grows a little, around its own center
if fx.box(row) {
fx.glyph("play", 34, fx.mix(th.red, th.sf, pick))
fx.txt(name, fx.mono(36), fx.mix(th.ink, th.sf, pick))
}
}
}
}Fading, blurring or graying an element draws it into a layer the size of its rect plus its shadow, so children that stick out of the element are cut off while that lasts. An element with an opacity of 0 is skipped entirely, children included.
Finding an element again#
rect_of :: proc(id: string) -> (Rect, bool)Give an element an id and, once the layout has been drawn, rect_of returns where it ended up: in stage pixels, after every transform that applied to it, both its own animation and any group around the layout. The second result is false when no element with that id was drawn this frame (an element at opacity 0 is not).
This is how a pointer finds a button or a highlight frames a row without the scene knowing any coordinates. Lay out, then ask, then draw on top:

layout_rect_of :: proc(s: fx.Shot) {
fx.backdrop(.Night)
th := fx.theme
press := fx.hit(s.lt, 4.2, 0.3)
if fx.layout({460, 130, 1000, 820}, {dir = .Col, pad = fx.pads(48), gap = 20, bg = th.sf, radius = 28}) {
fx.txt("Projects", fx.sans(58, 600, -0.02), th.ink)
for name in ([?]string{"fable", "showreel", "spot"}) {
if fx.box({id = name, dir = .Row, w = fx.grow(), h = fx.grow(), pad = fx.padxy(32, 0), align = .Center, bg = th.sf2, radius = 14}) {
fx.txt(name, fx.mono(38), th.ink)
}
}
if fx.box({dir = .Row, w = fx.grow(), justify = .End, pad = {12, 0, 0, 0}}) {
if fx.box({id = "render", pad = fx.padxy(44, 22), bg = th.ink, radius = 14, s = 1 - 0.08 * press}) {
fx.txt("Render", fx.sans(40, 500), th.sf)
}
}
}
// the layout is drawn; now its elements can be asked where they ended up
first, _ := fx.rect_of("fable")
last, _ := fx.rect_of("spot")
button, _ := fx.rect_of("render")
frame := fx.lerp_rect(first, last, fx.ep(s.lt, 2.1, 2.7, fx.ease_in_out))
fx.border(fx.inset(frame, -10), fx.alpha(th.na, fx.win(s.lt, 1.2, 5.4)), 5, 22)
a, b, c := fx.center(first), fx.center(last), fx.center(button) + {70, 26}
fx.cursor({{0.5, {1620, 1000}, false}, {1.3, a, false}, {2.0, a, false}, {2.7, b, false}, {3.3, b, false}, {4.2, c, true}}, 0.3, 5.6, 76)
}The frame and the pointer are drawn after the layout's scope has ended, which is the only place the rects exist. The pointer is cursor from the interface kit, which also has a ready-made highlight.
Ids are remembered until the end of the frame and are shared by all layouts of the frame, so two elements with the same id overwrite each other. For a rotated element the rect is the upright box around it.
layout_reset :: proc()layout_reset forgets all ids. The renderer calls it before every frame; you never need to, unless you drive fx from a renderer of your own.
What happens when#
fx is immediate-mode: there are no persistent elements, and a scene describes its whole layout again for every frame. Inside the layout scope nothing is measured or drawn yet; box, txt and the others only add nodes to a tree. When the scope ends, the tree is processed in one go:
- Widths from the inside out: every element's content width. Text is measured on one line; wrapped text counts as zero.
- Widths from the outside in: percentages are resolved, free space is shared among growing elements, and overflow is taken back from growing elements and wrapped text.
- Text is shaped at the width it ended up with. This is where wrapping happens.
- Heights, in the same two passes, now that every text knows how many lines it has.
- Positions, then drawing, parents before children.
Three things follow from that:
- Anything you draw with other
fxcalls inside the scope lands underneath the layout, because the layout is drawn at the end of the scope. Draw into an element with itsdrawcallback, and on top of the layout after the scope. rect_ofonly knows elements of layouts that have already ended in this frame.- Layouts cannot be nested (it is an assertion). Use boxes inside one layout, or several layouts one after the other.
Rebuilding the tree every frame is cheap: laid-out text is cached per thread, so text that does not change is not shaped again. Interface elements made of text, rects and layers cost about 2 ms per 1080p frame on one CPU thread.
layout and box end themselves at the end of their scope. If opening and closing a layout in the same block does not suit your code, layout_begin(r, b) and layout_end() are the same thing as two calls.
Box#
| Field | Type | Meaning |
|---|---|---|
id |
string |
Name for rect_of. |
dir |
Dir |
.Row (default) or .Col. |
w, h |
Size |
fit() (default), fixed, grow or pct. |
pad |
[4]f32 |
Padding: top, right, bottom, left. |
gap |
f32 |
Space between children along dir. |
justify |
Justify |
Children along dir: .Start, .Center, .End, .Between. |
align |
Align |
Children across dir: .Start, .Center, .End. |
bg |
Color |
Background color. |
paint |
Maybe(sk.Paint) |
Background paint instead of bg. |
border |
Color |
Border color. |
border_w |
f32 |
Border width, 1 if unset. |
sides |
Sides |
Sides with a border; empty = all. |
radius |
f32 |
Corner radius. |
shadow |
Shadow |
{dx, dy, blur, spread, c}. |
clip |
bool |
Clip children to the box. |
draw, data |
proc(r: Rect, data: rawptr), rawptr |
Custom content and what is passed to it. |
float, at |
bool, Vec2 |
Take the box out of the flow and place it from the parent's top-left corner. |
wrap |
bool |
Text: wrap at the given width. |
talign |
Text_Align |
Text: alignment of wrapped lines. |
o |
Maybe(f32) |
Opacity. |
dx, dy |
f32 |
Offset in px. |
s |
Maybe(f32) |
Scale around the center. |
rot |
f32 |
Rotation in degrees around the center. |
blur |
f32 |
Blur sigma. |
gray |
f32 |
Desaturation, 0..1. |
Reference#
| Name | What it is |
|---|---|
layout(r, b) |
Opens a layout in r; computed and drawn when its scope ends. |
layout_begin(r, b), layout_end() |
The same without a scope. |
box(b) |
A container; its scope holds its children. |
item(b) |
A box without children. |
txt(s, f, c, b) |
Text. |
glyph(name, size, c, b, width) |
An icon. |
picture(img, b, fit) |
An image. |
spacer(weight) |
Free space along the parent's direction. |
fit(min, max), fixed(v), grow(weight, min, max), pct(p) |
Sizes. |
pads(v), padxy(x, y) |
Padding on all sides, or per axis. |
rect_of(id) |
Where an element was drawn, in stage pixels. |
layout_reset() |
Forgets all ids; called by the renderer every frame. |
Box |
Everything an element can have. |
Size, Sizing |
A width or height and its kind. |
Dir, Justify, Align |
Direction and alignment. |
Side, Sides |
Border sides. |
Shadow |
A box shadow. |