Fork me on GitHub

egui.info

Unofficial notes about the excellent egui crate

egui-101-moving-circle

This is a simple example of a circle which moves on each frame drawn

Source

Screenshots

Screenshot

Description

When creating the ExampleApp struct, we add two values to hold the position of the circle.

struct ExampleApp {
    cx: f32,
    cy: f32,
}

These values need initialized in the Default trait implementation. Rust will call the default() function and expect to get a fully initialized structure back as the return value. In rust, the last expression of a function is the return value if not otherwise specified.

impl Default for ExampleApp {
    fn default() -> Self {
        Self {
            cx: 100.0,
            cy: 100.0,
        }
    }
}

To actually draw the circle, we use an egui::painter::Painter struct. It takes four arguments: position, radius, color, and stroke.

let painter = ui.painter();

painter.circle(
    egui::Pos2{x:self.cx, y:self.cy}, 
    50.0, 
    Color32::TRANSPARENT, 
    Stroke{width: 2.0, color: Color32::from_rgb(255, 255, 255)}
);