Skip to content
Merged
Show file tree
Hide file tree
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
86 changes: 85 additions & 1 deletion crates/rustmotion-core/src/css/animation.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::css::style::CssStyle;
use crate::css::style::{FilterFn, Size, TransformFn};
use crate::css::style::{BorderRadius, ClipPath, FilterFn, Size, TransformFn};
use crate::css::units::{Length, LengthPercentage};
use crate::engine::animator::AnimatedProperties;

Expand Down Expand Up @@ -74,6 +74,18 @@ pub fn apply_animated_props(css: &mut CssStyle, props: &AnimatedProperties) {
if props.height >= 0.0 {
css.height = Some(Size::Length(LengthPercentage::Px(props.height)));
}

if props.border_radius >= 0.0 {
css.border_radius = Some(BorderRadius::Uniform(LengthPercentage::Px(
props.border_radius,
)));
}

if props.clip_path_progress >= 0.0 {
if let Some(ClipPath::Morph { progress, .. }) = css.clip_path.as_mut() {
*progress = props.clip_path_progress;
}
}
}

#[cfg(test)]
Expand Down Expand Up @@ -152,6 +164,78 @@ mod tests {
}
}

#[test]
fn animated_border_radius_reaches_the_css_style() {
let mut css = CssStyle::default();
let props = AnimatedProperties {
border_radius: 100.0,
..AnimatedProperties::default()
};
apply_animated_props(&mut css, &props);

match css.border_radius {
Some(BorderRadius::Uniform(LengthPercentage::Px(v))) => {
assert!((v - 100.0).abs() < 1e-6)
}
other => panic!("expected a uniform 100px border-radius, got {other:?}"),
}
}

#[test]
fn animated_clip_path_progress_writes_onto_the_morph_variant() {
let mut css = CssStyle {
clip_path: Some(ClipPath::Morph {
from: Box::new(ClipPath::Circle {
radius: LengthPercentage::Px(10.0),
origin: None,
}),
to: Box::new(ClipPath::Circle {
radius: LengthPercentage::Px(50.0),
origin: None,
}),
progress: 0.0,
}),
..CssStyle::default()
};
let props = AnimatedProperties {
clip_path_progress: 0.75,
..AnimatedProperties::default()
};
apply_animated_props(&mut css, &props);

match css.clip_path {
Some(ClipPath::Morph { progress, .. }) => {
assert!((progress - 0.75).abs() < 1e-6)
}
other => panic!("expected the morph's progress to be updated, got {other:?}"),
}
}

#[test]
fn no_clip_path_progress_leaves_a_non_morph_clip_path_untouched() {
let mut css = CssStyle {
clip_path: Some(ClipPath::Circle {
radius: LengthPercentage::Px(10.0),
origin: None,
}),
..CssStyle::default()
};
let props = AnimatedProperties {
clip_path_progress: 0.5,
..AnimatedProperties::default()
};
apply_animated_props(&mut css, &props);

assert_eq!(
css.clip_path,
Some(ClipPath::Circle {
radius: LengthPercentage::Px(10.0),
origin: None,
}),
"a static (non-morph) clip-path must not be mutated by clip_path_progress"
);
}

#[test]
fn blur_and_glow_compose_into_filter_list() {
let mut css = CssStyle::default();
Expand Down
20 changes: 20 additions & 0 deletions crates/rustmotion-core/src/css/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1442,6 +1442,26 @@ pub enum ClipPath {
NodePath {
id: String,
},
/// A shape that sweeps continuously from `from` to `to` as `progress`
/// goes `0.0` → `1.0` (#384) — the chamfered-wall-style beat where the
/// clip itself is the animation, not a transform riding on top of a
/// frozen shape.
///
/// `progress` is meant to be driven by an ordinary `keyframes` animation
/// on the `clip_path_progress` property (see `KNOWN_MOTION_PROPERTIES`);
/// the literal value here is only the resting position when nothing
/// animates it.
///
/// `from` and `to` must be the same variant — and, for `polygon`, the
/// same vertex count — for the paint pass to have a correspondence to
/// interpolate along. A mismatch is reported on stderr and the node is
/// left unclipped for that frame; see `engine::paint_pass::morph_clip_path_to_skia`.
Morph {
from: Box<ClipPath>,
to: Box<ClipPath>,
#[serde(default)]
progress: f32,
},
}

#[cfg(test)]
Expand Down
7 changes: 7 additions & 0 deletions crates/rustmotion-core/src/engine/animator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,7 @@ pub struct AnimatedProperties {
pub perspective: f32,
pub draw_progress: f32,
pub motion_progress: f32,
pub clip_path_progress: f32,
pub char_animation: Option<ResolvedCharAnimation>,
}

Expand Down Expand Up @@ -565,6 +566,7 @@ impl Default for AnimatedProperties {
perspective: -1.0,
draw_progress: -1.0,
motion_progress: -1.0,
clip_path_progress: -1.0,
char_animation: None,
}
}
Expand Down Expand Up @@ -647,6 +649,9 @@ impl AnimatedProperties {
if other.motion_progress >= 0.0 {
self.motion_progress = other.motion_progress;
}
if other.clip_path_progress >= 0.0 {
self.clip_path_progress = other.clip_path_progress;
}
if other.char_animation.is_some() {
self.char_animation = other.char_animation.clone();
}
Expand Down Expand Up @@ -911,6 +916,7 @@ fn apply_property(props: &mut AnimatedProperties, property: &str, value: f64) {
"perspective" => props.perspective = value as f32,
"draw_progress" => props.draw_progress = value as f32,
"motion_progress" => props.motion_progress = value as f32,
"clip_path_progress" => props.clip_path_progress = value as f32,
_ => {}
}
}
Expand Down Expand Up @@ -1137,6 +1143,7 @@ fn get_property_value(props: &AnimatedProperties, property: &str) -> f64 {
"perspective" => props.perspective as f64,
"draw_progress" => props.draw_progress as f64,
"motion_progress" => props.motion_progress as f64,
"clip_path_progress" => props.clip_path_progress as f64,
_ => 0.0,
}
}
Expand Down
Loading
Loading