Skip to content

Commit

Permalink
Merge pull request #7 from sinproject-iwasaki/5-add-text-animation
Browse files Browse the repository at this point in the history
Add text animation #5
  • Loading branch information
sinproject-iwasaki authored Mar 15, 2024
2 parents 3928a06 + 165c400 commit 8a20b03
Showing 1 changed file with 38 additions and 5 deletions.
43 changes: 38 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ pub fn run() {
.add_plugins(DefaultPlugins)
.add_systems(Startup, setup)
.add_systems(Update, update_time)
// .add_systems(Update, animate_transform)
.add_systems(Update, animate_rotation)
.add_systems(Update, animate_scale)
.run();
}

Expand All @@ -25,13 +28,14 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
};
let text_justification = JustifyText::Center;

commands
.spawn(Text2dBundle {
commands.spawn((
Text2dBundle {
text: Text::from_section("00:00:00", text_style.clone())
.with_justify(text_justification),
..default()
})
.insert(TimeText);
},
TimeText,
));
}

fn update_time(mut query: Query<&mut Text, With<TimeText>>, windows: Query<&Window>) {
Expand All @@ -42,9 +46,38 @@ fn update_time(mut query: Query<&mut Text, With<TimeText>>, windows: Query<&Wind
let font_size_by_height = window.height();
let font_size = font_size_by_width.min(font_size_by_height) / 1.5;

for mut text in query.iter_mut() {
for mut text in &mut query {
let now = Local::now();
text.sections[0].value = now.format("%H:%M:%S.%3f").to_string();
text.sections[0].style.font_size = font_size;
}
}

// fn animate_transform(
// time: Res<Time>,
// mut query: Query<&mut Transform, (With<Text>, With<TimeText>)>,
// ) {
// for mut transform in query.iter_mut() {
// transform.translation.x = 50.0 * time.elapsed_seconds().sin();
// transform.translation.y = 50.0 * time.elapsed_seconds().cos();
// }
// }

fn animate_rotation(
time: Res<Time>,
mut query: Query<&mut Transform, (With<Text>, With<TimeText>)>,
) {
for mut transform in query.iter_mut() {
transform.rotation = Quat::from_rotation_z(time.elapsed_seconds().cos() * 0.1);
}
}

fn animate_scale(time: Res<Time>, mut query: Query<&mut Transform, (With<Text>, With<TimeText>)>) {
for mut transform in query.iter_mut() {
// transform.translation = Vec3::new(400.0, 0.0, 0.0);

let scale = (time.elapsed_seconds().sin() + 1.1) * 2.0;
transform.scale.x = scale;
transform.scale.y = scale;
}
}

0 comments on commit 8a20b03

Please sign in to comment.