Pauan / rust-dominator

Zero-cost ultra-high-performance declarative DOM library using FRP signals for Rust!
MIT License
999 stars 63 forks source link

Spring animations #8

Open rsaccon opened 5 years ago

rsaccon commented 5 years ago

I added Spring animations to dominator. It is based on they the current react native implementation. For very basic use case it works, see example but it is not well integrated yet with dominator animation, because your current animation code is only targeting timing animations such as easing (defined by a duration). Spring animations are defined by other parameters such as stiffness and damping.

So I have been thinking to generalize the data structures and as a first step replace in your code duration: f64 with an enum which can contain all the parameter of the chosen type of animation:

enum AnimationEnum {
    Timing(f64),  // duration
    Spring(SpringConfig),
   // Decay(f64),  // initial velocity (not implemented yet)
}

pub struct SpringConfig {
    stiffness: f64,
    damping: f64,
    mass: f64,
    initial_velocity: f64,
    overshoot_clamping: bool,
    rest_displacement_threshold: f64,
    rest_speed_threshold: f64,
}

What do think ? Is this something you would be interested in to accept a PR ?

Btw. one observation I made when working with dominator: If start a new rust project and then just add dominator dependency, it fails to build. Dominator is depending on signals 0.3.0. But only rust-signals latest version 0.3.2 is supposed to work with Rust nightly (I guess). Unfortunately, when I just delete the cargo lock files (dominator and/or dominator examples) and update to latest dependencies, then rebuild fails.

So for a new project with dominator dependency I just copy a cargo lockfile from a dominator example project and all works fine. But I guess that is not the right way to handle it!

I am using rustc 1.33.0-nightly (8e2063d02 2019-01-07)

Pauan commented 5 years ago

This is great, thanks! What's the purpose of the AnimationEnum? As far as I can tell, it isn't needed.

If start a new rust project and then just add dominator dependency, it fails to build.

Thanks for letting me know. There's some breaking changes with Future, so I'm waiting for that to stabilize and then I'll fix futures-signals and dominator.

rsaccon commented 5 years ago

What's the purpose of the AnimationEnum?

Let me try to explain. At the current stage of my Implementation I cannot use SpringAnimations with the original AnimatedSignalVec because a duration is required for animated_map:

pub trait AnimatedSignalVec: SignalVec {
    type Animation;

    fn animated_map<A, F>(self, duration: f64, f: F) -> AnimatedMap<Self, F>
    where
        F: FnMut(Self::Item, Self::Animation) -> A,
        Self: Sized;
}

But I want to provide either a duration (for timing based animations as in your current implementation), a SpringConfiguration for Spring Animations as added in my fork or for the not-implemented-yet DecayAnimation an initial velocity.

An enum containing these structs or types seems to me the easiest way to handle these different initial animation parameters. Does this make sense ?