Skip to content

Commit

Permalink
Fix the Text2d text anchor's incorrect horizontal alignment (bevyen…
Browse files Browse the repository at this point in the history
  • Loading branch information
ickshonpe authored and Shfty committed Mar 19, 2023
1 parent 4df64fa commit d7fd934
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion crates/bevy_text/src/text2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ pub fn extract_text2d_sprite(
}

let text_glyphs = &text_layout_info.glyphs;
let text_anchor = anchor.as_vec() * Vec2::new(1., -1.) - 0.5;
let text_anchor = -(anchor.as_vec() + 0.5);
let alignment_offset = text_layout_info.size * text_anchor;
let mut color = Color::WHITE;
let mut current_section = usize::MAX;
Expand Down
24 changes: 24 additions & 0 deletions examples/2d/text2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use bevy::{
prelude::*,
sprite::Anchor,
text::{BreakLineOn, Text2dBounds},
};

Expand Down Expand Up @@ -133,6 +134,29 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
..default()
});
});

for (text_anchor, color) in [
(Anchor::TopLeft, Color::RED),
(Anchor::TopRight, Color::GREEN),
(Anchor::BottomRight, Color::BLUE),
(Anchor::BottomLeft, Color::YELLOW),
] {
commands.spawn(Text2dBundle {
text: Text {
sections: vec![TextSection::new(
format!(" Anchor::{text_anchor:?} "),
TextStyle {
color,
..slightly_smaller_text_style.clone()
},
)],
..Default::default()
},
transform: Transform::from_translation(250. * Vec3::Y),
text_anchor,
..default()
});
}
}

fn animate_translation(
Expand Down

0 comments on commit d7fd934

Please sign in to comment.