Skip to main content

anchored_text_gizmos/
anchored_text_gizmos.rs

1//! Example demonstrating how to use text gizmos with anchors.
2//!
3//! The anchor selects which part of the text is aligned to the isometry’s position:
4//! `(0, 0)` center, `(-0.5, 0.0)` left edge, `(0.0, 0.5)` top edge.
5
6use bevy::color::palettes::css::{BLUE, GREEN, ORANGE, RED, YELLOW};
7use bevy::prelude::*;
8
9fn main() {
10    App::new()
11        .add_plugins(DefaultPlugins)
12        .add_systems(Startup, setup_camera)
13        .add_systems(Update, anchors)
14        .run();
15}
16
17fn setup_camera(mut commands: Commands) {
18    commands.spawn(Camera2d);
19}
20
21fn anchors(mut text_gizmos: Gizmos, time: Res<Time>) {
22    let t = time.elapsed_secs();
23    for (label, anchor, color) in [
24        ("left", vec2(-0.5, 0.0), RED),
25        ("right", vec2(0.5, 0.0), ORANGE),
26        ("center", Vec2::ZERO, YELLOW),
27        ("top", vec2(0.0, 0.5), GREEN),
28        ("bottom", vec2(0.0, -0.5), BLUE),
29    ] {
30        let position = Vec2::splat(350.0) * anchor;
31        text_gizmos.text_2d(
32            Isometry2d::from_translation(position),
33            "+",
34            12.,
35            Vec2::ZERO,
36            Color::WHITE,
37        );
38        text_gizmos.text_2d(
39            Isometry2d::new(position, Rot2::radians(t)),
40            label,
41            25.,
42            anchor,
43            color,
44        );
45    }
46}