Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modified clock example to make the clock more readable. #2644

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Modified clock example to make the clock more readable. Added numbers…
… on the clock face and took the portion of the hour passed into consideration for the hour hand. It now looks like a reasonable clock.
  • Loading branch information
kgday committed Oct 19, 2024
commit ad34f03df4abb2c430bf6a4f5ac030d12d05863e
28 changes: 26 additions & 2 deletions examples/clock/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use iced::alignment;
use iced::{alignment, Radians};
use iced::mouse;
use iced::time;
use iced::widget::canvas::{stroke, Cache, Geometry, LineCap, Path, Stroke};
Expand Down Expand Up @@ -117,9 +117,12 @@ impl<Message> canvas::Program<Message> for Clock {
};

frame.translate(Vector::new(center.x, center.y));
let minutes_portion = Radians::from(hand_rotation(self.now.minute(), 60)) / 12.0;
let hour_hand_angle = Radians::from(hand_rotation(self.now.hour(), 12)) + minutes_portion;


frame.with_save(|frame| {
frame.rotate(hand_rotation(self.now.hour(), 12));
frame.rotate(hour_hand_angle);
frame.stroke(&short_hand, wide_stroke());
});

Expand Down Expand Up @@ -155,10 +158,31 @@ impl<Message> canvas::Program<Message> for Clock {
..canvas::Text::default()
});
});

// Draw clock numbers
for i in 1..=12 {
let distance_out = radius * 1.05;
let angle =
Radians::from(hand_rotation(i, 12)) - Radians::from(Degrees(90.0));
let x = distance_out * angle.0.cos();
let y = distance_out * angle.0.sin();

frame.fill_text(canvas::Text {
content: format!("{}", i),
size: (radius / 15.0).into(),
position: Point::new(x * 0.85, y * 0.85),
color: palette.secondary.strong.text,
horizontal_alignment: alignment::Horizontal::Center,
vertical_alignment: alignment::Vertical::Center,
font: Font::MONOSPACE,
..canvas::Text::default()
});
}
});

vec![clock]
}

}

fn hand_rotation(n: u32, total: u32) -> Degrees {
Expand Down