egui-112-circle-follow-mouse
This is an example with a heading, label, button, and circle that follows the mouse.
Source
Screenshots
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,
egui::color::Color32::TRANSPARENT,
egui::epaint::Stroke{width: 2.0, color: egui::color::Color32::from_rgb(255, 255, 255)}
);