text

Text as a field: label(uv).a is the coverage (white glyphs, anti-aliased).

text(source: string | PropRef, opts: {
    font?: string
    size?: number
    align?: 'left' | 'center' | 'right'
    position?: readonly [number, number]
    tracking?: number
    typography?: boolean | readonly TypographyField[]
    /** Add `.distance(uv)` / `.nearest(uv)`: signed distance, nearest outline point, letter index. */
    distance?: boolean
    /** Add `.letters(i)`: each letter's centre and half size (true = 64 slots, or a count up to 256). */
    letters?: boolean | number
} = {}): TextInput

Import text from shaders/std.

source is a string or a string prop. Feed it to other inputs (blur(label) for a glow), use it as a mask or a reveal target, or as a spawn shape for particles (samplePoints(label)).

By default it carries the Text component's typography: the component gets Font, Weight, Italic, Transform, Size, Letter Spacing, Line Height, Align, Max Width, Center and Rotation controls (the same ones the Text layer has), a bounding box that hugs the ink and scales the font from its corners, and double-click editing of the text prop on the canvas (its panel field is hidden, as on Text, unless the prop sets ui.hidden: false). typography: [...] keeps only some (an ASCII field might want just ['fontFamily', 'fontWeight']); typography: false turns them off and draws with font, size, align, position and tracking as given. With typography on, font ('800 Inter'), size (font size as a fraction of the canvas height), align, position and tracking set the controls' starting values.

Parameters

NameTypeNotes
sourcestring | PropRef
opts`{
font?: string
size?: number
align?: 'left' \| 'center' \| 'right'
position?: readonly [number, number]
tracking?: number
typography?: boolean \| readonly TypographyField[]
/** Add `.distance(uv)` / `.nearest(uv)`: signed distance, nearest outline point, letter index. */
distance?: boolean
/** Add `.letters(i)`: each letter's centre and half size (true = 64 slots, or a count up to 256). */
letters?: boolean \| number

}| Optional. Default`. |

Examples

```ts
// An input that feeds another is a const above the definition (an object literal can't
// refer to its own keys).
const label = text(p('title'), {font: '700 Inter', size: 0.2})
// …
inputs: {label, glow: blur(label, {radius: 30})},
paint: wgsl`return vec4f(vec3f(1.0, 0.5, 0.2) * (label(uv).a + glow(uv).a * 0.8), 1.0);`,

Shape outputs, for effects that grow from, outline or react per letter:

  • distance: true adds label.distance(uv) and label.nearest(uv). Both return a vec4f of (signed distance to the glyph outlines in canvas pixels, negative inside; the nearest outline point in uv as .yz; the index of the letter that point belongs to as .w, -1 with no text). distance blends smoothly between field texels: use its .x for halos, outlines, growth fronts. nearest reads one texel exactly, never mixing two strokes: use its .yz and .w (length((uv - n.yz) * viewport) is the exact distance anywhere). The field is rebuilt on the GPU only when the text changes, so it costs nothing while the text holds still.
  • letters: true (or a count, up to 256; default 64) adds label.letters(i): letter i in reading order, spaces skipped, as (centre in uv .xy, half width and half height in canvas pixels .zw, measured in the text's own frame; a rotated text's letters turn with it). Slots past the last letter have .z = -1.
```ts
const word = text(p('label'), {distance: true, letters: true})
// …
inputs: {word},
paint: wgsl`
  let f = word.distance(uv);
  let halo = exp(-max(f.x, 0.0) / 40.0);                 // glow falling off outside the glyphs
  let n = word.nearest(uv);
  let box = word.letters(i32(n.w));                       // the letter this pixel belongs to
  let pulse = 0.5 + 0.5 * sin(time * 3.0 - n.w * 0.6);    // a wave running letter to letter
  return vec4f(vec3f(halo * pulse), 1.0);
`,