Skip to content

Commit 0bb6a8c

Browse files
committed
feat: 增加animation的duration计算
1 parent 09fcb0e commit 0bb6a8c

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

src/style_parser.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ impl<'i> Visitor<'i> for StyleVisitor<'i> {
116116

117117
});
118118
});
119+
// 更具percentage排序
120+
keyframe_data.keyframes.sort_by(|a, b| a.percentage.partial_cmp(&b.percentage).unwrap());
119121

120122
let mut keyframes = self.keyframes.borrow_mut();
121123
keyframes.insert(keyframe_data.name, keyframe_data.keyframes);

src/style_propetries/animation.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use std::{cell::RefCell, collections::HashMap, rc::Rc};
44

5-
use lightningcss::{printer::PrinterOptions, properties::{animation, Property}, traits::ToCss, values::time};
5+
use lightningcss::{printer::PrinterOptions, properties::{animation, Property}, traits::ToCss, values::{easing::EasingFunction, time}};
66

77
use crate::{generate_expr_lit_num, generate_invalid_expr, style_parser::KeyFrameItem, visitor::parse_style_values};
88
use swc_core::{common::DUMMY_SP, ecma::ast::*};
@@ -15,7 +15,8 @@ pub struct Animation {
1515
pub animation_name: Option<String>,
1616
pub animation_duration: f32,
1717
pub animation_delay: f32,
18-
pub animation_iteration: f32
18+
pub animation_iteration: f32,
19+
pub animation_timeing_function: EasingFunction
1920
// pub value: Option<Vec<KeyFrameItem>>
2021
}
2122

@@ -27,6 +28,7 @@ impl From<(String, &Property<'_>, Rc<RefCell<HashMap<String, Vec<KeyFrameItem>>>
2728
let mut animation_duration: f32 = 0.0;
2829
let mut animation_delay: f32 = 0.0;
2930
let mut animation_iteration: f32 = 1.0;
31+
let mut animation_timeing_function: EasingFunction = EasingFunction::Ease;
3032

3133
match value.1 {
3234
// Property::AnimationName(_, _) => todo!(),
@@ -51,7 +53,9 @@ impl From<(String, &Property<'_>, Rc<RefCell<HashMap<String, Vec<KeyFrameItem>>>
5153
animation_iteration = match animation.iteration_count {
5254
animation::AnimationIterationCount::Number(num) => num,
5355
animation::AnimationIterationCount::Infinite => -1.0,
54-
}
56+
};
57+
58+
animation_timeing_function = animation.timing_function.clone();
5559
});
5660
},
5761
_ => {}
@@ -63,7 +67,8 @@ impl From<(String, &Property<'_>, Rc<RefCell<HashMap<String, Vec<KeyFrameItem>>>
6367
animation_name,
6468
animation_duration,
6569
animation_delay,
66-
animation_iteration
70+
animation_iteration,
71+
animation_timeing_function
6772
}
6873

6974
}
@@ -76,7 +81,7 @@ impl ToExpr for Animation {
7681
let keyframe_map = self.keyframs.borrow();
7782
if let Some(keyframe_items) = keyframe_map.get(name) {
7883

79-
84+
let mut mut_percentage = 0.0;
8085

8186
return PropertyTuple::One(
8287
"animation".to_string(),
@@ -108,7 +113,9 @@ impl ToExpr for Animation {
108113
value: Box::new(Expr::Array(ArrayLit {
109114
span: DUMMY_SP,
110115
elems: keyframe_items.into_iter().map(|item| {
111-
Some(ExprOrSpread {
116+
let item_duration = (item.percentage - mut_percentage) * self.animation_duration * 1000.0;
117+
mut_percentage = item.percentage;
118+
return Some(ExprOrSpread {
112119
spread: None,
113120
expr: Box::new(Expr::Object(ObjectLit {
114121
span: DUMMY_SP,
@@ -117,6 +124,10 @@ impl ToExpr for Animation {
117124
key: PropName::Str("percentage".into()),
118125
value: Box::new(generate_expr_lit_num!(item.percentage as f64))
119126
}))),
127+
PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
128+
key: PropName::Str("duration".into()),
129+
value: Box::new(generate_expr_lit_num!(item_duration as f64))
130+
}))),
120131
PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
121132
key: PropName::Str("event".into()),
122133
value: Box::new(Expr::Object(ObjectLit {

0 commit comments

Comments
 (0)