# Drawing


Drawing in `fx` is immediate: each call paints onto the frame's canvas right away, over whatever was drawn before it, and nothing is kept between frames. Coordinates are stage pixels with `{0, 0}` at the top left (see [the stage](/docs/color/#the-stage)), colors are `0xAARRGGBB` (see [colors](/docs/color/#colors)). To move, fade, blur or clip several calls together, wrap them in a [group](/docs/group/).

## Shapes

```odin
rect   :: proc(r: Rect, c: Color)
rrect  :: proc(r: Rect, radius: f32, c: Color)
rrect4 :: proc(r: Rect, radii: [4]f32, p: sk.Paint)
border :: proc(r: Rect, c: Color, width: f32 = 1, radius: f32 = 0)
hline  :: proc(x, y, w: f32, c: Color, width: f32 = 1)
vline  :: proc(x, y, h: f32, c: Color, width: f32 = 1)
line   :: proc(a, b: Vec2, c: Color, width: f32 = 1)
circle :: proc(center: Vec2, r: f32, c: Color)
ring   :: proc(center: Vec2, r: f32, c: Color, width: f32 = 1)
```

All sizes are in px. A few details that are not in the names:

- `rrect4` takes a radius per corner, in the order top-left, top-right, bottom-right, bottom-left, and a paint instead of a color: pass `fx.fill(c)` for a plain one.
- `border` draws its line inside `r`, like CSS `box-sizing: border-box`, so a border of any width has the same outer edge as an `rrect` with the same rect and radius.
- `hline` and `vline` are rects: `y` is the top edge of an `hline` and `x` the left edge of a `vline`, and `width` grows down or to the right. Being rects, they are crisp when their coordinates are whole pixels.
- `line` and `ring` are strokes centered on the segment or the circle, with round ends.

```odin
draw_shapes :: proc(s: fx.Shot) {
	fx.backdrop(.Night)
	t, a := fx.theme.nt, fx.theme.na
	k := 0.5 - 0.5 * math.cos(s.p * math.TAU) // 0 → 1 → 0 over the loop
	fx.rect({160, 170, 300, 200}, t)
	fx.rrect({580, 170, 300, 200}, 100 * k, t)
	fx.rrect4({1000, 170, 300, 200}, {100 * k, 0, 100 * k, 0}, fx.fill(a))
	fx.border({1420, 170, 300, 200}, t, 4 + 20 * k, 40)
	fx.line({180, 780}, {440, 620 + 160 * k}, t, 8)
	fx.hline(580, 700, 300, a, 8)
	fx.vline(730 - 150 * k, 600, 200, a, 8)
	fx.circle({1150, 700}, 60 + 40 * k, t)
	fx.ring({1570, 700}, 100, a, 4 + 20 * k)
	names := [?]string{"rect", "rrect", "rrect4", "border", "line", "hline, vline", "circle", "ring"}
	for name, i in names {
		fx.label(name, {310 + f32(i % 4) * 420, 410 + f32(i / 4) * 430}, fx.mono(32), fx.theme.nm, {0.5, 0})
	}
}
```

Each of the filled shapes also comes in a form that takes a [paint](#paints) instead of a color, for gradients, strokes, glows and blend modes:

```odin
rect_paint   :: proc(r: Rect, p: sk.Paint)
rrect_paint  :: proc(r: Rect, radius: f32, p: sk.Paint)
circle_paint :: proc(center: Vec2, r: f32, p: sk.Paint)
paint_all    :: proc(p: sk.Paint)
```

`paint_all` fills everything that can be drawn on: the whole frame, or the current group's clip.

### Shadows

```odin
shadow :: proc(r: Rect, radius: f32 = 0, dy: f32 = 20, blur: f32 = 40, spread: f32 = 0, c: Color = 0x40000000, dx: f32 = 0)
```

A soft shadow under a rounded rect, with the arguments of a CSS `box-shadow`: `dx` and `dy` move it, `blur` is the CSS blur radius in px (the gaussian's sigma is half of it), `spread` grows the shape (or shrinks it, when negative) before it is blurred, and `c` is its color, by default black at 25%. `shadow` draws only the shadow: call it first, then draw the shape itself with the same rect and radius. Two shadows under one shape, a wide faint one and a tight dark one, look more natural than one.

```odin
draw_shadow :: proc(s: fx.Shot) {
	fx.rect(fx.stage(), fx.theme.bg) // shadows need a light surface to fall on
	lift := 0.5 - 0.5 * math.cos(s.p * math.TAU) // the card rises and comes back down
	card := fx.Rect{610, 330 - 40 * lift, 700, 420}
	dy := knob("dy", 24, 0, 120) * (0.4 + lift)
	blur := knob("blur", 60, 0, 200) * (0.4 + lift)
	fx.shadow(card, 36, dy, blur, knob("spread", -8, -40, 40), 0x5016160f)
	fx.rrect(card, 36, fx.theme.sf)
	fx.border(card, fx.theme.bd, 2, 36)
	fx.label("shadow", fx.center(card), fx.sans(96, 600, -0.03), fx.theme.ink, {0.5, 0.5})
}
```

## Paints

A paint says how a shape is filled: with a color or a gradient, as a fill or a stroke, blurred, trimmed, added to what is below. It is a plain struct from the `sk` package (`import sk "effects:sk"`), the Odin bindings for Skia, and you rarely fill it in by hand: the procedures here build one and return it, and the modifiers take a paint and return a changed copy, so they nest.

```odin
fill   :: proc(c: Color) -> sk.Paint
stroke :: proc(c: Color, w: f32 = 1) -> sk.Paint

linear :: proc(a, b: Vec2, colors: ..Color) -> sk.Paint
radial :: proc(center: Vec2, r: f32, colors: ..Color) -> sk.Paint
sweep  :: proc(center: Vec2, colors: ..Color) -> sk.Paint

Stop :: struct {c: Color, at: f32}
with_stops :: proc(p: sk.Paint, stops: ..Stop) -> sk.Paint

additive :: proc(p: sk.Paint) -> sk.Paint
trimmed  :: proc(p: sk.Paint, a, b: f32) -> sk.Paint
soft     :: proc(p: sk.Paint, sigma: f32) -> sk.Paint
```

| Paint | What it does |
| --- | --- |
| `fill(c)` | A solid fill. |
| `stroke(c, w)` | An outline `w` px wide, centered on the shape's edge, with round ends and corners. |
| `linear(a, b, colors)` | A gradient from point `a` to point `b` through 2 to 8 evenly spaced colors. Before `a` and after `b` the end colors continue. |
| `radial(center, r, colors)` | A gradient from `center` outward to radius `r`. |
| `sweep(center, colors)` | A gradient around `center`, starting at 3 o'clock and running clockwise. |
| `with_stops(p, stops)` | The gradient `p` with these colors at these positions (`at` in 0..1, ascending) instead of evenly spaced ones. Up to 8 stops. |
| `additive(p)` | Adds its light to what is below instead of covering it (the `Plus` blend mode): overlapping strokes get brighter. |
| `trimmed(p, a, b)` | Draws only the part from `a` to `b` of the outline's length, both 0..1. Animate `b` from 0 to 1 and a line draws itself. |
| `soft(p, sigma)` | Blurs the shape's edges by a gaussian of `sigma` px: glows and soft shadows. |

The points of a gradient are in the same coordinates as the shape, not relative to it: a `linear` from `{0, 0}` to `{1920, 0}` runs across the stage, and a shape drawn with it shows the part it covers. Gradients are dithered, so slow, dark ones do not show bands.

```odin
draw_paints :: proc(s: fx.Shot) {
	fx.backdrop(.Night)
	a, n, t := fx.theme.na, fx.theme.night2, fx.theme.nt
	k := 0.5 - 0.5 * math.cos(s.p * math.TAU)
	names := [?]string{"linear", "radial", "sweep", "with_stops"}
	for name, i in names {
		r := fx.Rect{120 + f32(i) * 430, 110, 390, 390}
		c := fx.center(r)
		p: sk.Paint
		switch i {
		case 0: p = fx.linear({r.x, r.y}, {r.x + r.w, r.y + r.h}, n, a, t) // from corner to corner
		case 1: p = fx.radial(c, 120 + 160 * k, t, a, n) // the radius breathes
		case 2: p = fx.sweep(c, a, t, n, a) // around c, from 3 o'clock
		case 3: p = fx.with_stops(fx.linear({r.x, 0}, {r.x + r.w, 0}), {n, 0}, {a, 0.1 + 0.8 * k}, {t, 1})
		}
		fx.rrect_paint(r, 40, p)
		fx.label(name, {c.x, r.y + r.h + 30}, fx.mono(32), fx.theme.nm, {0.5, 0})
	}
	// any paint fills text too
	shine := fx.linear({-900 + 1800 * k, 0}, {900 + 1800 * k, 0}, a, t, a)
	fx.text_paint("text_paint", 120, 650, fx.sans(300, 700, -0.04), shine)
}
```

`sk.Paint` has a few fields without a helper. Set them on the returned paint:

```odin
p := fx.stroke(fx.theme.nt, 6)
p.dash = {24, 16}         // 24 px on, 16 px off
p.square_cap = true       // square ends and mitered corners instead of round ones
fx.rrect_paint({700, 700, 500, 200}, 40, p)

g := fx.linear({1300, 0}, {1800, 0}, fx.theme.na, fx.theme.nt)
g.color = fx.with_alpha(0, 0.4) // with a gradient, only the alpha of color is used: the gradient at 40%
fx.rect_paint({1300, 700, 500, 200}, g)
```

| Field | Meaning |
| --- | --- |
| `color` | `0xAARRGGBB`. With a gradient only its alpha is used. |
| `stroke` | Stroke width in px, 0 = fill. |
| `blur` | Blur of the shape's edges, sigma in px (`soft`). |
| `shader`, `sh`, `stops`, `stop_colors`, `stop_pos` | The gradient (`linear`, `radial`, `sweep`, `with_stops`). |
| `dash` | `{on, off}` lengths in px for a dashed stroke, `{0, 0}` = solid. |
| `trim` | The part of the outline to draw (`trimmed`). |
| `blend` | A blend mode (`sk.Blend`) plus one, 0 = normal. `additive` sets it to `Plus`; the modes are listed under [blend](/docs/group/#blend). |
| `square_cap` | Square stroke ends and mitered corners instead of round ones. |

## Paths

```odin
draw_path :: proc(path: ^sk.Path, p: sk.Paint)
```

Anything that is not a rectangle or a circle is a path: curves, polygons, outlines parsed from SVG. Paths are made with the `sk` package and drawn with any paint.

```odin
path_new      :: proc() -> ^Path
path_from_svg :: proc(d: cstring) -> ^Path   // from an SVG path's d attribute; nil when it does not parse
path_free     :: proc(p: ^Path)

path_move_to  :: proc(p: ^Path, x, y: f32)
path_line_to  :: proc(p: ^Path, x, y: f32)
path_quad_to  :: proc(p: ^Path, x1, y1, x2, y2: f32)          // one control point, then the end
path_cubic_to :: proc(p: ^Path, x1, y1, x2, y2, x3, y3: f32)  // two control points, then the end
path_close    :: proc(p: ^Path)

path_add_rrect  :: proc(p: ^Path, x, y, w, h, r: f32)
path_add_circle :: proc(p: ^Path, cx, cy, r: f32)
path_bounds     :: proc(p: ^Path, out: ^[4]f32)   // x, y, w, h
path_length     :: proc(p: ^Path) -> f32          // in px
```

A path you make is yours to free. Building one every frame is cheap, and with `defer sk.path_free(path)` right after it is made it cannot leak:

```odin
draw_path :: proc(s: fx.Shot) {
	fx.backdrop(.Night)
	path := sk.path_new()
	defer sk.path_free(path) // a path you make is yours to free
	sk.path_move_to(path, 200, 760)
	sk.path_cubic_to(path, 560, 760, 560, 320, 920, 320)
	sk.path_cubic_to(path, 1280, 320, 1360, 780, 1720, 520)
	fx.draw_path(path, fx.stroke(fx.theme.nb, 4)) // the whole path, faint

	k := fx.ep(s.lt, 0.4, 2.8, fx.ease_in_out) * (1 - fx.ep(s.lt, 3.4, 3.9))
	a := fx.theme.na
	glow := fx.soft(fx.additive(fx.stroke(fx.alpha(a, 0.6), 40)), 20)
	fx.draw_path(path, fx.trimmed(glow, 0, k)) // a wide, blurred, additive stroke under …
	fx.draw_path(path, fx.trimmed(fx.stroke(a, 10), 0, k)) // … the line itself
}
```

The demo draws the same path three times: once whole and faint, once as a wide, blurred, additive stroke for the glow, and once as the line. Both of the last two are `trimmed` to the same progress. A stroke with round ends that is trimmed to nothing (`b = 0`) still leaves a dot at the start of the path, the size of the stroke: skip the call until the progress is above 0 if the dot should not be there.

`trimmed` is not limited to paths. Any shape drawn with a stroke can be trimmed, which makes a progress ring one call (a circle's outline starts at 3 o'clock and runs clockwise):

```odin
fx.circle_paint({400, 800}, 120, fx.trimmed(fx.stroke(fx.theme.na, 16), 0, s.p))
```

A filled SVG outline works the same way. A path's coordinates are its own, so place and scale it with a [group](/docs/group/):

```odin
star := sk.path_from_svg("M12 2l3 7 7 .6-5.3 4.6 1.7 7.3L12 17.8 5.6 21.5l1.7-7.3L2 9.6 9 9z") // 24 units wide
defer sk.path_free(star)
if fx.group({x = 1100, y = 200, s = 20}) {
	fx.draw_path(star, fx.linear({0, 0}, {24, 24}, fx.theme.na, fx.theme.nt))
}
```

## Text

```odin
text          :: proc(s: string, x, y: f32, f: Font, c: Color, max_w: f32 = 0, align := Text_Align.Left) -> Rect
label         :: proc(s: string, p: Vec2, f: Font, c: Color, anchor := Vec2{0, 0}) -> Rect
text_baseline :: proc(s: string, x, y: f32, f: Font, c: Color) -> Rect
text_paint    :: proc(s: string, x, y: f32, f: Font, p: sk.Paint, max_w: f32 = 0, align := Text_Align.Left)
```

Four ways to put the same text on the frame, differing in how they are positioned:

- `text` puts the top-left corner of the text's box at `(x, y)`. With `max_w` above 0 the text wraps at that width and `align` (`.Left`, `.Center`, `.Right`) places the lines inside it; with 0 it stays on one line.
- `label` positions an unwrapped text by an anchor: `{0, 0}` puts the box's top-left corner at `p`, `{0.5, 0.5}` centers it on `p`, `{1, 0}` right-aligns it to `p`. This is the one to use for anything centered or right-aligned.
- `text_baseline` puts the baseline of one line at `y`, so texts of different sizes stand on the same line.
- `text_paint` is `text` with a [paint](#paints) instead of a color (it is in the paints demo above), and returns nothing.

The first three return the box they drew into, which is how you put something next to or under a text without measuring it first. For a wrapped `text` the box is `max_w` wide.

```odin
draw_text :: proc(s: fx.Shot) {
	fx.backdrop(.Night)
	max_w := knob("max_w", 1000, 500, 1500)
	align := fx.Text_Align(int(s.p * 3) % 3) // .Left, .Center, .Right, a third of the loop each
	f := fx.sans(92, 500, -0.025)
	f.lh = 1.2
	line := "Every frame is a pure function of time, drawn with Skia from Odin."
	box := fx.text(line, 210, 410, f, fx.theme.nt, max_w, align)
	fx.border(fx.inset(box, -32), fx.theme.ns, 2) // text returns the box it filled
	info := fmt.tprintf("align = .%v   box = %.0f × %.0f px", align, box.w, box.h)
	fx.label(info, {178, 280}, fx.mono(36), fx.theme.nm)
}
```

```odin
draw_label :: proc(s: fx.Shot) {
	fx.backdrop(.Night)
	p := fx.Vec2{960, 360}
	turn := s.p * math.TAU
	anchor := fx.Vec2{0.5 + 0.5 * math.sin(turn), 0.5 - 0.5 * math.cos(2 * turn)}
	box := fx.label("anchored", p, fx.sans(150, 600, -0.03), fx.theme.nt, anchor)
	fx.border(box, fx.theme.nb, 2)
	fx.circle(p, 14, fx.theme.na) // p stays put: the anchor says which part of the box sits on it
	info := fmt.tprintf("anchor = {{%.2f, %.2f}}", anchor.x, anchor.y)
	fx.label(info, {960, 620}, fx.mono(34), fx.theme.nm, {0.5, 0})

	// text_baseline: three sizes standing on one line
	x: f32 = 330
	fx.hline(270, 900, 1380, fx.theme.na, 2)
	for size in ([?]f32{48, 96, 170}) {
		x += fx.text_baseline("Baseline", x, 900, fx.sans(size, 500, -0.02), fx.theme.nt).w + 50
	}
}
```

> **Note:** A call draws its whole text in one font and one color. For mixed weights or colors in one line, draw the runs one after another using the returned boxes, or use [`kinetic`](/docs/ui/) or a [layout](/docs/layout/) row.

### Fonts

```odin
Font :: struct {
	family:    cstring,
	size:      f32,
	weight:    f32,
	tracking:  f32,
	lh:        f32,
	tabular:   bool,
	italic:    bool,
	underline: bool,
	upper:     bool,
	max_lines: int,
}
```

A `Font` is a value describing how text is set. Nothing is loaded or cached by making one, so build them inline. The theme's helpers `sans`, `mono` and `caps` return one for the [theme's families](/docs/theme/#fonts): `fx.sans(72, 600, -0.03)`.

| Field | Meaning |
| --- | --- |
| `family` | The name a font file was registered under: `"SG"` and `"JBM"` are built in, your own come from `font`. |
| `size` | Font size in px. |
| `weight` | 100..900, 0 = 400. A variable font is set to exactly this weight; a family of static files uses the nearest one registered. |
| `tracking` | Letter spacing in em, like CSS `letter-spacing: 0.1em`: `-0.03` tightens a headline, `0.1` opens up capitals. |
| `lh` | Line height as a multiple of `size`, 0 = the font's own. |
| `tabular` | Tabular figures: every digit as wide as the others, so a running number does not jitter. |
| `italic` | Uses the family's italic file. A family without one gets its upright face slanted instead, which is no substitute: register the italic (see below). |
| `underline` | Underlines the text. |
| `upper` | Draws the text in capitals, like CSS `text-transform: uppercase`. |
| `max_lines` | With `max_w`: the most lines to draw, 0 = unlimited. Text that is cut off ends in `…`. |

```odin
draw_font :: proc(s: fx.Shot) {
	fx.backdrop(.Night)
	th := fx.theme
	f := fx.sans(150, knob("weight", 500, 300, 700), knob("tracking", -0.03, -0.08, 0.3))
	fx.text("Space Grotesk", 150, 90, f, th.nt)

	row := fx.Font{family = th.sans, size = 60, weight = 500}
	n := fmt.tprintf("%.2f", fx.count(s.lt, 0, 5, 0, 1111.1, fx.ease_linear))
	plain, tab, under, upper, two := row, row, row, row, row
	tab.tabular, under.underline, upper.upper, two.max_lines = true, true, true, 2
	fx.label(n, {1130, 380}, plain, th.nt, {1, 0}) // right-aligned: proportional digits make it jitter
	fx.label(n, {1130, 500}, tab, th.na, {1, 0}) // tabular digits all have one width
	fx.text("Underlined", 730, 620, under, th.nt)
	fx.text("Uppercase", 730, 740, upper, th.nt)
	fx.text("Two lines at most, and whatever does not fit is cut off", 730, 860, two, th.nt, 700)
	names := [?]string{"default", "tabular", "underline", "upper", "max_lines = 2"}
	for name, i in names do fx.label(name, {150, 396 + f32(i) * 120}, fx.mono(32), th.nm)
}
```

```odin
font :: proc(family: string, path: string) -> bool
```

`font` registers a font file under a family name, once, when the video is set up. The path is relative to the project directory unless it is absolute. Register several files under one name (weights, italics) and `Font.weight` and `Font.italic` pick among them. It returns false, and prints `fx: cannot load font` with the path, when the file cannot be read. [Theme and fonts](/docs/theme/#your-own-fonts) shows it in a project.

### Measuring

```odin
measure :: proc(s: string, f: Font, max_w: f32 = 0) -> Vec2
shape   :: proc(s: string, f: Font, max_w: f32 = 0, align := Text_Align.Left) -> ^sk.Text
metrics :: proc(t: ^sk.Text) -> sk.Text_Metrics
```

`measure` returns the width and height in px a text takes, without drawing it. `shape` is the step every text procedure starts with: it lays the text out and returns the layout, and `metrics` reads it:

| `sk.Text_Metrics` | |
| --- | --- |
| `width` | Width of the longest line, px. |
| `height` | Height of all lines, px. |
| `baseline` | Distance from the top of the box to the first line's baseline, px. |
| `max_intrinsic` | The width the text would have on one line. |
| `lines` | Number of lines. |

Layouts are cached per render thread, so drawing the same text with the same font every frame lays it out once, and measuring before drawing costs nothing extra. A layout belongs to the frame it was asked for in: do not keep the pointer.

## Icons

```odin
icon      :: proc(name: string, x, y, size: f32, c: Color, width: f32 = 1.6)
icon_path :: proc(name: string) -> (Icon_Path, bool)

Icon_Path :: struct {
	path:   ^sk.Path,
	filled: bool,
}
```

`icon` draws one of the built-in line icons, `size` px wide and high, with its top-left corner at `(x, y)`. The icons are designed on a 24-unit grid and `width` is the stroke in those units, so it scales with the icon: at the default 1.6 a 48 px icon has a 3.2 px line. A name that does not exist draws nothing. All 77 names are on the [theme page](/docs/theme/#icons).

`icon_path` returns the icon's 24-unit path for drawing it yourself, with any paint; `filled` says whether it is meant to be filled rather than stroked. The path belongs to the library: do not free it.

```odin
draw_icon :: proc(s: fx.Shot) {
	fx.backdrop(.Night)
	width := knob("width", 1.6, 0.4, 4)
	names := [?]string{"search", "mail", "calendar", "bell", "users", "settings"}
	for name, i in names {
		x := 190 + f32(i) * 280
		fx.icon(name, x, 190, 140, fx.theme.nt, width)
		fx.label(name, {x + 70, 370}, fx.mono(32), fx.theme.nm, {0.5, 0})
	}
	// icon_path hands out the 24-unit path itself: here the globe draws itself, 15 times larger
	if ip, ok := fx.icon_path("globe"); ok {
		k := fx.ep(s.lt, 0.3, 2.6, fx.ease_in_out) * (1 - fx.ep(s.lt, 3.4, 3.9))
		if fx.group({x = 780, y = 540, s = 15}) {
			fx.draw_path(ip.path, fx.trimmed(fx.stroke(fx.theme.na, width), 0, k))
		}
	}
}
```

## Images

```odin
load_image :: proc(path: string) -> ^sk.Image
image_size :: proc(img: ^sk.Image) -> Vec2
image      :: proc(img: ^sk.Image, r: Rect, alpha: f32 = 1, fit := Image_Fit.Cover, focus := Vec2{0.5, 0.5}, zoom: f32 = 1)

Image_Fit :: enum {
	Fill,    // stretch to r
	Contain,
	Cover,   // fill r, cropping the image
}
```

`load_image` reads a PNG, JPEG or WebP file, relative to the project directory unless the path is absolute. The file is decoded the first time and the same image is returned from then on, from any thread, so call it right in the scene. When the file cannot be loaded it prints `fx: cannot load image` and returns nil, and `image` draws nothing for nil.

`image` draws it into `r`:

| Argument | Meaning |
| --- | --- |
| `alpha` | Opacity, 0..1. |
| `fit` | `.Fill` stretches the image to `r`. `.Contain` shows all of it, centered, leaving bars. `.Cover`, the default, fills `r` and crops what does not fit. |
| `focus` | With `.Cover`: which part stays in view when the image is cropped, 0..1 on each axis. `{0, 0}` keeps the top-left corner, `{0.5, 0.5}` the middle. |
| `zoom` | With `.Cover`: crops further in, 1 = as much of the image as fits. Values below 1 count as 1. |

Animating `focus` pans across a picture and animating `zoom` pushes in, the two moves of a photo in a film. `image_size` returns the image's size in its own pixels.

```odin
draw_image :: proc(s: fx.Shot) {
	fx.backdrop(.Night)
	img := fx.load_image("assets/frame.jpg") // decoded once, then handed out by path
	pan := 0.5 - 0.5 * math.cos(s.p * math.TAU) // 0 → 1 → 0
	fits := [?]fx.Image_Fit{.Fill, .Contain, .Cover}
	for fit, i in fits {
		r := fx.Rect{210 + f32(i) * 520, 200, 460, 560}
		fx.rect(r, fx.theme.night2)
		fx.image(img, r, fit = fit, focus = {pan, 0.5}, zoom = knob("zoom", 1, 1, 4))
		fx.border(r, fx.theme.ns, 2)
		fx.label(fmt.tprintf(".%v", fit), {r.x + r.w / 2, r.y + r.h + 36}, fx.mono(32), fx.theme.nm, {0.5, 0})
	}
}
```

For rounded corners, a grade or a blur, draw the image inside a [group](/docs/group/) with `clip` and `radius`, `sat`, `blur` and so on. Frames of a video file are drawn the same way by [`footage`](/docs/footage/).

## Baked layers

```odin
bake :: proc(key: string, r: Rect, res: f32, paint: proc(r: Rect, data: rawptr), data: rawptr = nil, alpha: f32 = 1)
```

Some parts of a frame never change: a background gradient, a vignette, a field of a few thousand dots. `bake` draws such a layer once into an image and from then on every frame, on every render thread, draws that image instead. On the CPU a full-screen gradient costs about 5 ms a frame; baked, it costs one image draw. `backdrop` and `vignette` are baked this way.

```odin
stars :: proc(r: fx.Rect, data: rawptr) {
	rng := fx.rng(7)
	for _ in 0 ..< 4000 {
		p := fx.Vec2{r.x + fx.rand(&rng) * r.w, r.y + fx.rand(&rng) * r.h}
		fx.circle(p, 1 + 3 * fx.rand(&rng), fx.alpha(fx.theme.nt, 0.5 * fx.rand(&rng)))
	}
}

sky :: proc(s: fx.Shot) {
	fx.backdrop(.Night)
	fx.bake("sky/stars", fx.stage(), 1, stars) // 4,000 circles the first time, one image after that
}
```

| Argument | Meaning |
| --- | --- |
| `key` | Names the picture. The image is stored under the key, the size of `r` and `res`, for as long as the program runs. |
| `r` | Where the image is drawn, and the area `paint` is asked to fill. |
| `res` | The image's resolution as a multiple of `r`'s size in stage px. 1 is one image pixel per stage pixel, 0.5 a quarter of the pixels. |
| `paint` | Draws the layer with ordinary `fx` calls, in the same coordinates as if it drew straight to the frame. It is called once. |
| `data` | Handed to `paint`, for its parameters. It is read only during that one call. |
| `alpha` | Opacity the image is drawn with, 0..1. This can change every frame. |

What to keep in mind:

- The layer has to be static. `paint` runs once, so anything it reads from the time is frozen at the first frame that asked for it. Animate around a baked layer with `alpha` or a group, not inside it.
- The key has to say everything the picture depends on. `vignette` puts its strength in the key. A key that changes every frame bakes, and keeps, a new image every frame.
- `res` below 1 is for smooth pictures only (`backdrop` uses 0.5). Gradients are dithered so that slow, dark ones do not band, and a layer baked at less than full size loses that when it is scaled up. If a dark gradient shows bands, bake it at 1 or draw it directly.
- `res` counts stage pixels. A render at twice the size (`--scale 2`) still gets the image baked at `res` times 1920×1080, scaled up.
- Do not call `bake` from inside a `paint` procedure, and that includes `backdrop` and `vignette`: the cache is locked while `paint` runs, and the second call would wait forever.

## Shaders

```odin
shader :: proc(sksl: cstring, r: Rect, uniforms: ..f32)
```

`shader` fills `r` with a picture computed per pixel by an SkSL program (Skia's shading language, close to GLSL). `main` receives the pixel's position in the coordinates `r` is given in (stage px, unless a group around it scales or moves them), and the `uniforms` are passed as floats in the order the program declares them. A program that does not compile draws nothing and prints `fx: shader:` with the compiler's message.

```odin
STRIPES :: `
uniform float t;
half4 main(float2 p) {
	float v = 0.5 + 0.5 * sin((p.x + p.y) * 0.02 - t * 4);
	return half4(v, v * 0.4, v * 0.25, 1);
}`

fx.shader(STRIPES, {160, 160, 800, 400}, s.t)
```

The editor previews on the GPU, where shaders are fast. Renders are drawn on the CPU, and there a heavy full-screen shader (warped noise, a raymarched body) costs about 2 s per 1080p frame on one thread. [Shaders](/docs/shade/) has `shader_scaled`, which draws such pictures at a fraction of the size, and the ready-made fields built on it.

## Reference

| Procedure | Draws or returns |
| --- | --- |
| `rect(r, c)`, `rect_paint(r, p)` | A rectangle. |
| `rrect(r, radius, c)`, `rrect_paint(r, radius, p)` | A rounded rectangle. |
| `rrect4(r, radii, p)` | A rounded rectangle with a radius per corner. |
| `border(r, c, width, radius)` | A border inside `r`. |
| `hline(x, y, w, c, width)`, `vline(x, y, h, c, width)` | A horizontal or vertical line as a rect. |
| `line(a, b, c, width)` | A line between two points. |
| `circle(center, r, c)`, `circle_paint(center, r, p)` | A circle. |
| `ring(center, r, c, width)` | A circle's outline. |
| `shadow(r, radius, dy, blur, spread, c, dx)` | A soft shadow under a rounded rect. |
| `draw_path(path, p)` | A path from the `sk` package. |
| `paint_all(p)` | The paint over everything. |
| `fill(c)`, `stroke(c, w)` | A solid fill or stroke paint. |
| `linear(a, b, colors)`, `radial(center, r, colors)`, `sweep(center, colors)` | A gradient paint. |
| `with_stops(p, stops)` | A gradient with explicit stop positions (`Stop{c, at}`). |
| `additive(p)`, `trimmed(p, a, b)`, `soft(p, sigma)` | The paint adding light, drawing part of an outline, blurred. |
| `text(s, x, y, f, c, max_w, align)` | Text from its top-left corner, optionally wrapped; returns its box. |
| `label(s, p, f, c, anchor)` | Text placed by an anchor; returns its box. |
| `text_baseline(s, x, y, f, c)` | One line with its baseline at `y`; returns its box. |
| `text_paint(s, x, y, f, p, max_w, align)` | Text filled with a paint. |
| `font(family, path)` | Registers a font file; false when it cannot be loaded. |
| `measure(s, f, max_w)` | Width and height of a text. |
| `shape(s, f, max_w, align)`, `metrics(t)` | A text's layout and its `sk.Text_Metrics`. |
| `icon(name, x, y, size, c, width)` | A built-in icon. |
| `icon_path(name)` | An icon's `Icon_Path` and whether it exists. |
| `load_image(path)`, `image_size(img)` | An image file, loaded once; its size in px. |
| `image(img, r, alpha, fit, focus, zoom)` | An image in a rect (`Image_Fit`: `.Fill`, `.Contain`, `.Cover`). |
| `bake(key, r, res, paint, data, alpha)` | A static layer, rendered once and reused. |
| `shader(sksl, r, uniforms)` | A rect filled by an SkSL program. |

Groups (`Xf`, `group`, `frosted`) live in the same source file and have [their own page](/docs/group/).
