diff --git a/examples/dynamic_stroke_size.rs b/examples/dynamic_stroke_size.rs new file mode 100644 index 0000000..876fa6b --- /dev/null +++ b/examples/dynamic_stroke_size.rs @@ -0,0 +1,105 @@ +//! Demonstrate surgical changes on the fields of `Shape` (path, fill, stroke). +//! The triangle changes fill color; the hexagon changes stroke width; and +//! the node positions of all shapes are extracted from the path to apply different +//! rotations on size. + +use bevy::{color::palettes::css::*, prelude::*}; +use bevy_prototype_lyon::prelude::*; + +fn main() { + App::new() + .add_plugins((DefaultPlugins, ShapePlugin)) + .add_systems(Startup, setup_system) + .add_systems(Update, redraw_line_width) + .add_systems(Update, redraw_fill) + .add_systems(Update, rotate_shape_by_size) + .run(); +} + +// Marker traits to uniquely identify entities. +#[derive(Component)] +struct HexagonShape; +#[derive(Component)] +struct TriangleShape; + +/// Walk Path to get the maximum x coordinate. +fn get_max_x(shape: &Shape) -> f32 { + shape + .path + .iter() + .map(|p| p.to().x) + .chain(shape.path.iter().map(|p| p.from().x)) + .fold(0f32, |acc, x| if x - acc > 1e-8 { x } else { acc }) +} + +/// Over time, rotate smaller shapes faster. +fn rotate_shape_by_size(mut query: Query<(&mut Transform, &Shape)>, time: Res