# Type in motion


[`text` and `label`](/docs/draw/) draw a line of text as one thing, and a group can move or fade that thing. The procedures here go one level down, to the single letter: lines whose letters arrive one after another, text that decodes itself, and the positions of the glyphs for everything else. For text that arrives word by word, each word at a time of its own, see [`kinetic`](/docs/ui/).

## Letters

```odin
Letters :: struct {
	t0:     f32, // when the first letter starts
	step:   f32, // delay between letters
	dur:    f32, // each letter's move, 0 = 0.5
	rise:   f32, // px it comes up from (negative: drops in)
	blur:   f32, // starts this blurred
	scale:  f32, // starts this much bigger (0.4 = 140%)
	spin:   f32, // starts turned by this many degrees
	out:    f32, // when letters start leaving again (0 = never), mirrored
	ease:   Ease,
	random: bool, // letters start in a shuffled order
}

letters :: proc(s: string, p: Vec2, f: Font, c: Color, l: Letters, anchor := Vec2{0, 0}) -> Rect
```

`letters` draws one line of text like `label` does (same position, same `anchor`, and it returns the line's rect), except that every letter makes its own entrance. The `Letters` struct says when and how.

The when is in video time, in seconds: the first letter starts at `t0`, each next one `step` later, and each takes `dur` to arrive. Inside a scene that is `t0 = s.start + 0.4`, not `0.4`. `letters` reads the frame's time itself, so there is no time argument.

```odin
type_letters :: proc(s: fx.Shot) {
	fx.backdrop(.Night)
	l := fx.Letters {
		t0   = s.start + 0.4, // video time, like everything in a scene
		step = knob("step", 0.04, 0, 0.15),
		dur  = 0.7,
		rise = knob("rise", 60, -200, 200),
		blur = knob("blur", 14, 0, 40),
		out  = s.start + 3.6,
	}
	fx.letters("Letter by letter", {960, 540}, fx.sans(210, 600, -0.03), fx.theme.nt, l, {0.5, 0.5})
}
```

The how is where each letter starts from. Everything you set is combined, and every letter fades in as it comes:

| Field | The letter starts |
| --- | --- |
| `rise` | That many px below its place (negative: above, so it drops in). |
| `blur` | Blurred by that sigma in px, sharpening as it arrives. |
| `scale` | That much bigger: 0.4 is 140% of its size. Negative starts smaller. |
| `spin` | Turned by that many degrees. |
| `random` | Not a start state: the letters come in a random order (some of them together) instead of left to right. The order is the same on every frame and in every render. |

`ease` shapes the move and is `ease_out5` unless you set it. Use `ease_back` for letters that overshoot.

```odin
type_options :: proc(s: fx.Shot) {
	fx.backdrop(.Night)
	rows := [?]struct {
		name: string,
		l:	fx.Letters,
	} {
		{"rise = 80", {rise = 80}},
		{"rise = -80", {rise = -80}},
		{"blur = 24", {blur = 24}},
		{"scale = 1.2", {scale = 1.2}},
		{"spin = 90, rise = 40", {spin = 90, rise = 40}},
		{"random = true, blur = 12", {random = true, blur = 12}},
	}
	for row, i in rows {
		y := 150 + f32(i) * 156
		l := row.l
		l.t0, l.step, l.dur = s.start + 0.4 + f32(i) * 0.12, 0.05, 0.6
		l.out = s.start + 4.2
		fx.label(row.name, {150, y}, fx.mono(34), fx.theme.nm, {0, 0.5})
		fx.letters("Typography", {1040, y}, fx.sans(124, 600, -0.03), i % 2 == 0 ? fx.theme.nt : fx.theme.na, l, {0, 0.5})
	}
}
```

`out` is the time at which the letters start to leave again, by the same road they came: a letter that rose in sinks out, one that came in blurred blurs away. Leaving is quicker than arriving (each letter takes 0.6 of `dur`, and they follow each other at half of `step`). At 0 the line stays.

Things to know:

- One line only: there is no wrapping. For a paragraph, call it once per line and offset `t0`.
- Kerning is kept. The letters end up where `label` would have drawn the string, so you can swap one for the other.
- A letter in motion is drawn in a group of its own, a blurred one if you set `blur`. Once it has arrived it is plain text again, so a long line costs something only while it moves.
- The rect it returns is the settled line's, from the first frame on. Use it to place what belongs next to the text.

## Scramble

```odin
scramble :: proc(s: string, p: f32, seed: u32 = 1, rate: f32 = 30, hide: int = 0, set := SCRAMBLE_SET) -> string

SCRAMBLE_SET :: "abcdefghijklmnopqrstuvwxyz0123456789<>/\\[]{}=+*#%&"
```

`scramble` returns `s` with only the first `p` (0..1) of it readable. The rest is replaced by characters from `set` that change `rate` times a second: a message being decoded. It draws nothing itself, so pass the result to `text` or `label`. Drive `p` with [`prog`](/docs/anim/#progress).

Spaces and line breaks are never scrambled, so the shape of the text is there from the start. With `hide` above 0 only that many undecoded characters are shown ahead of the readable part and the rest is blank, which looks like typing with noise at the cursor. `seed` makes two scrambles on the same frame differ. `set` must be plain ASCII.

Use a monospaced font. In a proportional one the line changes its width with every new character and jitters.

```odin
type_scramble :: proc(s: fx.Shot) {
	fx.backdrop(.Night)
	p := fx.prog(s.lt, 0.5, 3.4) // how much of the text is decoded
	rate := knob("rate", 30, 2, 60)
	f := fx.mono(84, 500)
	fx.text(fx.scramble("every frame is a function", p, 1, rate), 190, 240, f, fx.theme.nt)
	// hide = 3: only three undecoded characters run ahead
	fx.text(fx.scramble("of time and nothing else", p, 2, rate, 3), 190, 480, f, fx.theme.nt)
	// its own alphabet
	fx.text(fx.scramble("0110 1001 1100 0011 1010", p, 3, rate, 0, "01"), 190, 720, f, fx.theme.na)
}
```

## Glyphs

```odin
Glyph :: struct {
	s:    string, // the character
	x, w: f32, // its place in the line (kerning kept)
}

glyphs :: proc(s: string, f: Font) -> []Glyph
```

`glyphs` is what `letters` is built on: for a one-line text in a font it returns every character with its `x` (from the start of the line) and its width `w`. Draw each with `fx.text(g.s, x0 + g.x, y, f, c)` and you have the line back, with one position per letter to do with as you like: a wave, a color per letter, letters that scatter.

A space comes back with `w = 0` (and no position); skip it. The slice lives in the frame's scratch memory, like every string `fx` hands you: use it in the frame, do not keep it.

```odin
type_glyphs :: proc(s: fx.Shot) {
	fx.backdrop(.Night)
	f := fx.sans(230, 700, -0.02)
	word := "Wavelength"
	size := fx.measure(word, f)
	x0, y0 := 960 - size.x / 2, 500 - size.y / 2
	amp := knob("amp", 50, 0, 150)
	for g, i in fx.glyphs(word, f) {
		if g.w == 0 do continue // a space
		y := y0 + amp * math.sin(s.p * 2 * math.TAU - f32(i) * 0.6)
		fx.text(g.s, x0 + g.x, y, f, i == 4 ? fx.theme.na : fx.theme.nt)
		// the glyph's place in the line: x and w
		fx.rect({x0 + g.x + 3, y0 + size.y + 180, g.w - 6, 8}, i == 4 ? fx.theme.na : fx.theme.ns)
	}
}
```

## Reference

| Procedure | Returns |
| --- | --- |
| `letters(s, p, f, c, l, anchor)` | Draws one line whose letters arrive per `l`; the line's rect. |
| `scramble(s, p, seed, rate, hide, set)` | `s` with everything after the first `p` of it replaced by changing characters. |
| `glyphs(s, f)` | Each character of a one-line text with its `x` and width. |
