Pre-1.0: the API is not stable yet and may change from version to version. Pin the exact version in your Cargo.toml and check the changelog before upgrading.

5. Text

Titles, typewriter effects, mixed styles and code.

Loading live preview…

Source of learn_05_text — edit in the playground
//! Lesson 5 — Text.
//!
//! `Txt` draws text with the built-in Inter font (or JetBrains Mono with
//! `.family(MONO)`). Text animates like anything else: tweening the text
//! types it out, `end` reveals it letter by letter, and `rich` mixes styles.

use oxframe::prelude::*;

#[oxframe::scene]
pub async fn text(view: View) {
    let title = view.add(Txt::new().text("").font_size(96.0).weight(800.0).y(-150.0));
    // Typewriter: from "" to the full text.
    title
        .text()
        .to("Hello, text", 1.0)
        .ease(presets::LINEAR)
        .await;

    let line = view.add(
        Txt::new()
            .font_size(44.0)
            .rich([
                span("Mix "),
                span("bold").bold(),
                span(", "),
                span("color").color(color!("#f5c242")),
                span(" and "),
                span("italic").italic(),
            ])
            .end(0.0),
    );
    // Reveal the line letter by letter.
    line.end().to(1.0, 1.0).ease(presets::LINEAR).await;

    let code = view.add(
        Code::new()
            .code("let x = 42; // code, highlighted")
            .font_size(36.0)
            .y(150.0)
            .opacity(0.0),
    );
    code.opacity().to(1.0, 0.5).await;
    wait_for(0.5).await;
}

// Try: `title.morph_to("Hello, world", 1.0).await;` — shared letters slide into place.

pub fn project() -> Project {
    Project::new("text").scene(text())
}

Txt

Txt::new().text("Hello").font_size(96.0).weight(800.0) is a text shape. Two fonts are built in: Inter (default) and JetBrains Mono (.family(MONO)). Both include Cyrillic. Because the fonts ship with oxframe, your text looks exactly the same on every computer. Txt::font_size, Txt::weight and Code::font_size take whole numbers too: .font_size(64).

Text animates too

  • title.text().to("Hello, text", 1.0) types the new text like a typewriter.
  • .end() from 0 to 1 reveals the text letter by letter (you will see end again for shapes in lesson 9).
  • title.morph_to("New title", 1.0) slides letters that both texts share into place and fades the rest.

Mixed styles and code

Txt::new().rich([span("Mix "), span("bold").bold(), span(" and "), span("color").color(color!("#f5c242"))])

Code shows source code with syntax highlighting, and can dim everything except the lines you are talking about: Code::new().code(src).focus(3, 4). The quicksort showcase uses it heavily.

code.code().to(next, 1.0) does not type the new source. Tokens that stay slide to their new places, deleted ones fade, and new ones appear. insert(line, col, text, seconds), remove(((line, col), (line, col)), seconds) and replace edit one spot; lines are counted from 1 and columns from 0. .flash() on that tween lights the inserted text for a moment. A focus on lines that move rides with them. The code steps example writes a function this way.

Edit this page on GitHub