pareen/
easer_combinators.rs

1use num_traits::Float;
2
3use easer::functions::Easing;
4
5use crate::{fun, Anim, Fun};
6
7impl<V, F> Anim<F>
8where
9    V: Float,
10    F: Fun<T = V, V = V>,
11{
12    fn seq_ease<G, H, A>(
13        self,
14        self_end: V,
15        ease: impl Fn(V, V, V) -> Anim<G>,
16        ease_duration: V,
17        next: A,
18    ) -> Anim<impl Fun<T = V, V = V>>
19    where
20        G: Fun<T = V, V = V>,
21        H: Fun<T = V, V = V>,
22        A: Into<Anim<H>>,
23    {
24        let next = next.into();
25
26        let ease_start_value = self.eval(self_end);
27        let ease_end_value = next.eval(V::zero());
28        let ease_delta = ease_end_value - ease_start_value;
29        let ease = ease(ease_start_value, ease_delta, ease_duration);
30
31        self.seq(self_end, ease).seq(self_end + ease_duration, next)
32    }
33
34    /// Play two animations in sequence, transitioning between them with an
35    /// easing-in function from
36    /// [`easer`](https://docs.rs/easer/0.2.1/easer/index.html).
37    ///
38    /// This is only available when enabling the `easer` feature for `pareen`.
39    ///
40    /// The values of `self` at `self_end` and of `next` at time zero are used
41    /// to determine the parameters of the easing function.
42    ///
43    /// Note that, as with [`seq`](struct.Anim.html#method.seq), the `next`
44    /// animation will see time starting at zero once it plays.
45    ///
46    /// # Arguments
47    ///
48    /// * `self_end` - Time at which the `self` animation is to stop.
49    /// * `_easing` - A struct implementing
50    ///     [`easer::functions::Easing`](https://docs.rs/easer/0.2.1/easer/functions/trait.Easing.html).
51    ///     This determines the easing function that will be used for the
52    ///     transition. It is passed as a parameter here to simplify type
53    ///     inference.
54    /// * `ease_duration` - The amount of time to use for transitioning to `next`.
55    /// * `next` - The animation to play after transitioning.
56    ///
57    /// # Example
58    ///
59    /// See [`seq_ease_in_out`](struct.Anim.html#method.seq_ease_in_out) for an example.
60    pub fn seq_ease_in<E, G, A>(
61        self,
62        self_end: V,
63        _easing: E,
64        ease_duration: V,
65        next: A,
66    ) -> Anim<impl Fun<T = V, V = V>>
67    where
68        E: Easing<V>,
69        G: Fun<T = V, V = V>,
70        A: Into<Anim<G>>,
71    {
72        self.seq_ease(self_end, ease_in::<E, V>, ease_duration, next)
73    }
74
75    /// Play two animations in sequence, transitioning between them with an
76    /// easing-out function from
77    /// [`easer`](https://docs.rs/easer/0.2.1/easer/index.html).
78    ///
79    /// This is only available when enabling the `easer` feature for `pareen`.
80    ///
81    /// The values of `self` at `self_end` and of `next` at time zero are used
82    /// to determine the parameters of the easing function.
83    ///
84    /// Note that, as with [`seq`](struct.Anim.html#method.seq), the `next`
85    /// animation will see time starting at zero once it plays.
86    ///
87    /// # Arguments
88    ///
89    /// * `self_end` - Time at which the `self` animation is to stop.
90    /// * `_easing` - A struct implementing
91    ///     [`easer::functions::Easing`](https://docs.rs/easer/0.2.1/easer/functions/trait.Easing.html).
92    ///     This determines the easing function that will be used for the
93    ///     transition. It is passed as a parameter here to simplify type
94    ///     inference.
95    /// * `ease_duration` - The amount of time to use for transitioning to `next`.
96    /// * `next` - The animation to play after transitioning.
97    ///
98    /// # Example
99    ///
100    /// See [`seq_ease_in_out`](struct.Anim.html#method.seq_ease_in_out) for an example.
101    pub fn seq_ease_out<E, G, A>(
102        self,
103        self_end: V,
104        _: E,
105        ease_duration: V,
106        next: A,
107    ) -> Anim<impl Fun<T = V, V = V>>
108    where
109        E: Easing<V>,
110        G: Fun<T = V, V = V>,
111        A: Into<Anim<G>>,
112    {
113        self.seq_ease(self_end, ease_out::<E, V>, ease_duration, next)
114    }
115
116    /// Play two animations in sequence, transitioning between them with an
117    /// easing-in-out function from
118    /// [`easer`](https://docs.rs/easer/0.2.1/easer/index.html).
119    ///
120    /// This is only available when enabling the `easer` feature for `pareen`.
121    ///
122    /// The values of `self` at `self_end` and of `next` at time zero are used
123    /// to determine the parameters of the easing function.
124    ///
125    /// Note that, as with [`seq`](struct.Anim.html#method.seq), the `next`
126    /// animation will see time starting at zero once it plays.
127    ///
128    /// # Arguments
129    ///
130    /// * `self_end` - Time at which the `self` animation is to stop.
131    /// * `_easing` - A struct implementing
132    ///     [`easer::functions::Easing`](https://docs.rs/easer/0.2.1/easer/functions/trait.Easing.html).
133    ///     This determines the easing function that will be used for the
134    ///     transition. It is passed as a parameter here to simplify type
135    ///     inference.
136    /// * `ease_duration` - The amount of time to use for transitioning to `next`.
137    /// * `next` - The animation to play after transitioning.
138    ///
139    /// # Example
140    ///
141    /// Play a constant value until time `0.5`, then transition for `0.3`
142    /// time units, using a cubic function, into a second animation:
143    /// ```
144    /// let first_anim = pareen::constant(2.0);
145    /// let second_anim = pareen::prop(1.0f32);
146    /// let anim = first_anim.seq_ease_in_out(
147    ///     0.5,
148    ///     easer::functions::Cubic,
149    ///     0.3,
150    ///     second_anim,
151    /// );
152    /// ```
153    /// The animation will look like this:
154    ///
155    /// ![plot for seq_ease_in_out](https://raw.githubusercontent.com/leod/pareen/master/images/seq_ease_in_out.png)
156    pub fn seq_ease_in_out<E, G, A>(
157        self,
158        self_end: V,
159        _: E,
160        ease_duration: V,
161        next: A,
162    ) -> Anim<impl Fun<T = V, V = V>>
163    where
164        E: Easing<V>,
165        G: Fun<T = V, V = V>,
166        A: Into<Anim<G>>,
167    {
168        self.seq_ease(self_end, ease_in_out::<E, V>, ease_duration, next)
169    }
170}
171
172/// Integrate an easing-in function from the
173/// [`easer`](https://docs.rs/easer/0.2.1/easer/index.html) library.
174///
175/// This is only available when enabling the `easer` feature for `pareen`.
176///
177/// # Arguments
178///
179/// * `start` - The start value for the easing function.
180/// * `delta` - The change in the value from beginning to end time.
181/// * `duration` - The total time between beginning and end.
182///
183/// # See also
184/// Documentation for [`easer::functions::Easing`](https://docs.rs/easer/0.2.1/easer/functions/trait.Easing.html).
185pub fn ease_in<E, V>(start: V, delta: V, duration: V) -> Anim<impl Fun<T = V, V = V>>
186where
187    V: Float,
188    E: Easing<V>,
189{
190    fun(move |t| E::ease_in(t, start, delta, duration))
191}
192
193/// Integrate an easing-out function from the
194/// [`easer`](https://docs.rs/easer/0.2.1/easer/index.html) library.
195///
196/// This is only available when enabling the `easer` feature for `pareen`.
197///
198/// # Arguments
199///
200/// * `start` - The start value for the easing function.
201/// * `delta` - The change in the value from beginning to end time.
202/// * `duration` - The total time between beginning and end.
203///
204/// # See also
205/// Documentation for [`easer::functions::Easing`](https://docs.rs/easer/0.2.1/easer/functions/trait.Easing.html).
206pub fn ease_out<E, V>(start: V, delta: V, duration: V) -> Anim<impl Fun<T = V, V = V>>
207where
208    V: Float,
209    E: Easing<V>,
210{
211    fun(move |t| E::ease_out(t, start, delta, duration))
212}
213
214/// Integrate an easing-in-out function from the
215/// [`easer`](https://docs.rs/easer/0.2.1/easer/index.html) library.
216///
217/// This is only available when enabling the `easer` feature for `pareen`.
218///
219/// # Arguments
220///
221/// * `start` - The start value for the easing function.
222/// * `delta` - The change in the value from beginning to end time.
223/// * `duration` - The total time between beginning and end.
224///
225/// # See also
226/// Documentation for [`easer::functions::Easing`](https://docs.rs/easer/0.2.1/easer/functions/trait.Easing.html).
227pub fn ease_in_out<E, V>(start: V, delta: V, duration: V) -> Anim<impl Fun<T = V, V = V>>
228where
229    V: Float,
230    E: Easing<V>,
231{
232    fun(move |t| E::ease_in_out(t, start, delta, duration))
233}