diff options
author | 2022-10-07 11:41:50 -0700 | |
---|---|---|
committer | 2022-10-07 12:01:07 -0700 | |
commit | 12a87c54eb68b992676060c80e518ffb29445cfc (patch) | |
tree | b3fba16f2dd95a084cdbc3850f59a049c103c355 /examples | |
parent | f9a6efcaa03728f43aaa105af8936c1ed4778388 (diff) | |
download | iced-12a87c54eb68b992676060c80e518ffb29445cfc.tar.gz iced-12a87c54eb68b992676060c80e518ffb29445cfc.tar.bz2 iced-12a87c54eb68b992676060c80e518ffb29445cfc.zip |
Added support for relative positioning of gradient fills. Addressed some PR feedback.
Diffstat (limited to 'examples')
-rw-r--r-- | examples/modern_art/src/main.rs | 41 | ||||
-rw-r--r-- | examples/solar_system/src/main.rs | 7 |
2 files changed, 29 insertions, 19 deletions
diff --git a/examples/modern_art/src/main.rs b/examples/modern_art/src/main.rs index 0601fc44..362e4ad1 100644 --- a/examples/modern_art/src/main.rs +++ b/examples/modern_art/src/main.rs @@ -1,5 +1,5 @@ use iced::widget::canvas::{ - self, fill, Cache, Canvas, Cursor, Fill, Frame, Geometry, Gradient, + self, Cache, Canvas, Cursor, Frame, Geometry, Gradient, Position, Location }; use iced::{ executor, Application, Color, Command, Element, Length, Point, Rectangle, @@ -76,6 +76,20 @@ impl<Message> canvas::Program<Message> for ModernArt { } } +fn random_direction() -> Location { + match thread_rng().gen_range(0..8) { + 0 => Location::TopLeft, + 1 => Location::Top, + 2 => Location::TopRight, + 3 => Location::Right, + 4 => Location::BottomRight, + 5 => Location::Bottom, + 6 => Location::BottomLeft, + 7 => Location::Left, + _ => Location::TopLeft + } +} + fn generate_box(frame: &mut Frame, bounds: Size) -> bool { let solid = rand::random::<bool>(); @@ -87,8 +101,13 @@ fn generate_box(frame: &mut Frame, bounds: Size) -> bool { ) }; - let gradient = |top_left: Point, bottom_right: Point| -> Gradient { - let mut builder = Gradient::linear(top_left, bottom_right); + let gradient = |top_left: Point, size: Size| -> Gradient { + let mut builder = Gradient::linear(Position::Relative { + top_left, + size, + start: random_direction(), + end: random_direction() + }); let stops = thread_rng().gen_range(1..15u32); let mut i = 0; @@ -114,25 +133,13 @@ fn generate_box(frame: &mut Frame, bounds: Size) -> bool { frame.fill_rectangle( top_left, size, - Fill { - style: fill::Style::Solid(random_color()), - ..Default::default() - }, + random_color(), ); } else { frame.fill_rectangle( top_left, size, - Fill { - style: fill::Style::Gradient(&gradient( - top_left, - Point::new( - top_left.x + size.width, - top_left.y + size.height, - ), - )), - ..Default::default() - }, + &gradient(top_left, size), ); }; diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs index 6f38f480..9200391b 100644 --- a/examples/solar_system/src/main.rs +++ b/examples/solar_system/src/main.rs @@ -19,6 +19,7 @@ use iced::{ }; use std::time::Instant; +use crate::canvas::Position; pub fn main() -> iced::Result { SolarSystem::run(Settings { @@ -202,8 +203,10 @@ impl<Message> canvas::Program<Message> for State { let earth = Path::circle(Point::ORIGIN, Self::EARTH_RADIUS); let earth_fill = Gradient::linear( - Point::new(-Self::EARTH_RADIUS, 0.0), - Point::new(Self::EARTH_RADIUS, 0.0), + Position::Absolute { + start: Point::new(-Self::EARTH_RADIUS, 0.0), + end: Point::new(Self::EARTH_RADIUS, 0.0) + } ) .add_stop(0.2, Color::from_rgb(0.15, 0.50, 1.0)) .add_stop(0.8, Color::from_rgb(0.0, 0.20, 0.47)) |