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.

3. Shapes, colors, transforms

More shapes, outlines, and the properties every shape shares: rotation, scale, color.

Loading live preview…

Source of learn_03_shapes — edit in the playground
//! Lesson 3 — Shapes, colors and transforms.
//!
//! Every shape is made with a builder (`Rect::new().size(..)`) that sets its
//! starting look. After `view.add`, the same names without arguments give you
//! the property to animate: `.size(120.0)` sets it, `.size()` animates it.

use oxframe::prelude::*;

#[oxframe::scene]
pub async fn shapes(view: View) {
    let square = view.add(
        Rect::new()
            .size(140.0)
            .radius(16.0)
            .x(-450.0)
            .fill(color!("#e13238")),
    );
    let circle = view.add(Circle::new().size(140.0).x(-150.0).fill(color!("#f5c242")));
    let hexagon = view.add(
        Polygon::new()
            .sides(6.0)
            .size(150.0)
            .x(150.0)
            .fill(color!("#68abdf")),
    );
    let star = view.add(
        Star::new()
            .points(5.0)
            .size(160.0)
            .x(450.0)
            .fill(color!("#2ecc71")),
    );
    // Outlines: `stroke` is the line color, `line_width` its thickness.
    let arrow = view.add(
        Line::new()
            .points([(-450.0, 220.0), (450.0, 220.0)])
            .stroke(Color::WHITE)
            .line_width(6.0)
            .end_arrow(true),
    );

    // Transforms: rotation (degrees), scale, and position.
    all((
        square.rotation().to(45.0, 1.0),
        circle.scale().to(Vec2::new(1.4, 0.7), 1.0),
        hexagon.sides().to(3.0, 1.0),
        star.rotation().to(144.0, 1.0),
        arrow.y().to(-440.0, 1.0),
    ))
    .await;
    // Colors interpolate smoothly too.
    all((
        square.fill().to(color!("#9b59b6"), 0.8),
        circle.fill().to(color!("#e67e22"), 0.8),
    ))
    .await;
}

// Try: give the hexagon an outline with `.stroke(Color::WHITE).line_width(8.0)`.

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

The shapes

Rect, Circle, Polygon, Star and Line are all made the same way: Shape::new(), then settings. Rect has .radius() for rounded corners, Polygon has .sides(), Line takes a list of points and can have arrows.

One name, two uses

This is the pattern you will use everywhere, so it is worth a second look:

  • Before adding: .size(140.0) sets the starting size.
  • After adding: .size() gives you the size, ready to animate with .to(...).

Things every shape has

  • x, y — position; rotation — degrees, clockwise; scale — a pair, so you can stretch (Vec2::new(1.4, 0.7)).
  • fill — the inside color; stroke and line_width — the outline.
  • opacity — 0 is invisible, 1 is solid.

Colors are written color!("#e13238") (checked when you compile — a typo is an error, not a surprise). When a color animates, it blends through perceptually even shades instead of going muddy in the middle.

Themes

A scene with no colors and no font sizes of its own already looks finished. Nodes take those from the project theme: .fill(Accent), .padding(Md), .radius(Lg), Txt::new().display(). Text with no fill uses the theme's text color.

Project::new("explainer").theme(Theme::dark().accent(color!("#68abdf")))

let title = view.add(Txt::new().text("Binary search").display());
let note = view.add(Txt::new().text("O(log n)").caption().fill(Muted));
title.fade_in().await;

Built-in themes: dark (the default), light, midnight, paper and vivid. .accent(color) rebuilds the palette from one brand color and keeps text at a 4.5:1 contrast. The same override can live in oxframe.toml without a recompile:

[theme]
name = "midnight"
accent = "#68abdf"

fade_in() and a duration of Fast, Normal or Slow use the theme's motion. One .theme(...) line changes every token in the video.

Relative position and frame shape

Place a node against another node or the safe area instead of counting pixels. The position is worked out again every frame, so it follows a neighbor that moves or changes size. pct(80) is 80% of the frame (or of a parent with a fixed size) along the axis it sets.

let title = view.add(Txt::new().text("Binary search").h1().to_edge(Edge::Top));
let card = view.add(Rect::new().width(pct(80)).height(300.0).fill(Surface));
let label = view.add(Txt::new().text("mid").caption().next_to(card, Side::Bottom));

align_to(other, Edge::Top) shares an edge. center_in(other) shares a center. to_edge stops at the safe area: a portrait frame leaves room for a vertical video's status bar and buttons. view.is_portrait() and view.safe_area() are there when a layout really does have to branch.

Declare every shape the video should exist in. oxframe check looks at each of them, and oxframe render --aspect 9:16 (or --aspect all) exports them.

Project::new("explainer").aspects([Aspect::Landscape, Aspect::Portrait, Aspect::Square])

Every shape and all its settings are listed in All nodes.

Edit this page on GitHub