From 81096ef454a9aacacf3853f7dfe96b8b9228f200 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 14 Apr 2020 06:38:06 +0200 Subject: Implement `From` for `canvas::Fill` --- examples/clock/src/main.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'examples/clock') diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index a85a964c..1379d3a6 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -101,10 +101,7 @@ impl canvas::Drawable for LocalTime { let clock = canvas::Path::new(|path| path.circle(center, radius)); - frame.fill( - &clock, - canvas::Fill::Color(Color::from_rgb8(0x12, 0x93, 0xD8)), - ); + frame.fill(&clock, Color::from_rgb8(0x12, 0x93, 0xD8)); fn draw_hand( n: u32, -- cgit From 46cd0891d25c2dd48e182747d8c1f9579b066490 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 14 Apr 2020 06:54:12 +0200 Subject: Implement `canvas::Path::circle` helper method --- examples/clock/src/main.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'examples/clock') diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index 1379d3a6..dab91eab 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -95,11 +95,13 @@ impl From> for LocalTime { impl canvas::Drawable for LocalTime { fn draw(&self, frame: &mut canvas::Frame) { + use canvas::Path; + let center = frame.center(); let radius = frame.width().min(frame.height()) / 2.0; let offset = Vector::new(center.x, center.y); - let clock = canvas::Path::new(|path| path.circle(center, radius)); + let clock = Path::circle(center, radius); frame.fill(&clock, Color::from_rgb8(0x12, 0x93, 0xD8)); -- cgit From 6d7f2b30cc9fd4022681f766ee3b77cdb6c8de0a Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 14 Apr 2020 07:08:24 +0200 Subject: Simplify drawing logic in `clock` example --- examples/clock/src/main.rs | 74 ++++++++++++++++++++-------------------------- 1 file changed, 32 insertions(+), 42 deletions(-) (limited to 'examples/clock') diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index dab91eab..827379fa 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -99,63 +99,53 @@ impl canvas::Drawable for LocalTime { let center = frame.center(); let radius = frame.width().min(frame.height()) / 2.0; - let offset = Vector::new(center.x, center.y); let clock = Path::circle(center, radius); - frame.fill(&clock, Color::from_rgb8(0x12, 0x93, 0xD8)); - fn draw_hand( - n: u32, - total: u32, - length: f32, - offset: Vector, - path: &mut canvas::path::Builder, - ) { - let turns = n as f32 / total as f32; - let t = 2.0 * std::f32::consts::PI * (turns - 0.25); + let short_hand = + Path::line(Point::ORIGIN, Point::new(0.0, -0.5 * radius)); - let x = length * t.cos(); - let y = length * t.sin(); + let long_hand = + Path::line(Point::ORIGIN, Point::new(0.0, -0.8 * radius)); - path.line_to(Point::new(x, y) + offset); - } + let thin_stroke = canvas::Stroke { + width: radius / 100.0, + color: Color::WHITE, + line_cap: canvas::LineCap::Round, + ..canvas::Stroke::default() + }; - let hour_and_minute_hands = canvas::Path::new(|path| { - path.move_to(center); - draw_hand(self.hour, 12, 0.5 * radius, offset, path); + let wide_stroke = canvas::Stroke { + width: thin_stroke.width * 3.0, + ..thin_stroke + }; - path.move_to(center); - draw_hand(self.minute, 60, 0.8 * radius, offset, path) - }); + frame.translate(Vector::new(center.x, center.y)); - frame.stroke( - &hour_and_minute_hands, - canvas::Stroke { - width: radius / 100.0 * 3.0, - color: Color::WHITE, - line_cap: canvas::LineCap::Round, - ..canvas::Stroke::default() - }, - ); + frame.with_save(|frame| { + frame.rotate(hand_rotation(self.hour, 12)); + frame.stroke(&short_hand, wide_stroke); + }); - let second_hand = canvas::Path::new(|path| { - path.move_to(center); - draw_hand(self.second, 60, 0.8 * radius, offset, path) + frame.with_save(|frame| { + frame.rotate(hand_rotation(self.minute, 60)); + frame.stroke(&long_hand, wide_stroke); }); - frame.stroke( - &second_hand, - canvas::Stroke { - width: radius / 100.0, - color: Color::WHITE, - line_cap: canvas::LineCap::Round, - ..canvas::Stroke::default() - }, - ); + frame.with_save(|frame| { + frame.rotate(hand_rotation(self.second, 60)); + frame.stroke(&long_hand, thin_stroke); + }); } } +fn hand_rotation(n: u32, total: u32) -> f32 { + let turns = n as f32 / total as f32; + + 2.0 * std::f32::consts::PI * turns +} + mod time { use iced::futures; -- cgit