Skip to content

Commit 7294588

Browse files
authored
Fix the Text2d text anchor's incorrect horizontal alignment (#8019)
1 parent 8aa217c commit 7294588

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

crates/bevy_text/src/text2d.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ pub fn extract_text2d_sprite(
103103
}
104104

105105
let text_glyphs = &text_layout_info.glyphs;
106-
let text_anchor = anchor.as_vec() * Vec2::new(1., -1.) - 0.5;
106+
let text_anchor = -(anchor.as_vec() + 0.5);
107107
let alignment_offset = text_layout_info.size * text_anchor;
108108
let mut color = Color::WHITE;
109109
let mut current_section = usize::MAX;

examples/2d/text2d.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
88
use bevy::{
99
prelude::*,
10+
sprite::Anchor,
1011
text::{BreakLineOn, Text2dBounds},
1112
};
1213

@@ -133,6 +134,29 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
133134
..default()
134135
});
135136
});
137+
138+
for (text_anchor, color) in [
139+
(Anchor::TopLeft, Color::RED),
140+
(Anchor::TopRight, Color::GREEN),
141+
(Anchor::BottomRight, Color::BLUE),
142+
(Anchor::BottomLeft, Color::YELLOW),
143+
] {
144+
commands.spawn(Text2dBundle {
145+
text: Text {
146+
sections: vec![TextSection::new(
147+
format!(" Anchor::{text_anchor:?} "),
148+
TextStyle {
149+
color,
150+
..slightly_smaller_text_style.clone()
151+
},
152+
)],
153+
..Default::default()
154+
},
155+
transform: Transform::from_translation(250. * Vec3::Y),
156+
text_anchor,
157+
..default()
158+
});
159+
}
136160
}
137161

138162
fn animate_translation(

0 commit comments

Comments
 (0)