[][src]Struct pareen::Anim

pub struct Anim<F>(pub F);

Anim is the main type provided by pareen. It is a wrapper around any type implementing Fun.

Anim provides methods that transform or compose animations, allowing complex animations to be created out of simple pieces.

Methods

impl<F> Anim<F> where
    F: Fun
[src]

pub fn eval(&self, t: F::T) -> F::V[src]

Evaluate the animation at time t.

pub fn map<W>(self, f: impl Fn(F::V) -> W) -> Anim<impl Fun<T = F::T, V = W>>[src]

Transform an animation so that it applies a given function to its values.

Example

Turn (2.0 * t) into (2.0 * t).sqrt() + 2.0 * t:

let anim = pareen::prop(2.0f32).map(|value| value.sqrt() + value);

assert_approx_eq!(anim.eval(1.0), 2.0f32.sqrt() + 2.0);

pub fn map_time<S>(
    self,
    f: impl Fn(S) -> F::T
) -> Anim<impl Fun<T = S, V = F::V>>
[src]

Transform an animation so that it modifies time according to the given function before evaluating the animation.

Example

Run an animation two times slower:

let anim = pareen::cubic(&[1.0, 1.0, 1.0, 1.0]);
let slower_anim = anim.map_time(|t: f32| t / 2.0);

pub fn map_anim<W, G, A>(self, anim: A) -> Anim<impl Fun<T = F::T, V = W>> where
    G: Fun<T = F::V, V = W>,
    A: Into<Anim<G>>, 
[src]

pub fn map_time_anim<S, G, A>(self, anim: A) -> Anim<impl Fun<T = S, V = F::V>> where
    G: Fun<T = S, V = F::T>,
    A: Into<Anim<G>>, 
[src]

impl<F> Anim<F> where
    F: Fun,
    F::T: Copy
[src]

pub fn zip<W, G, A>(self, other: A) -> Anim<impl Fun<T = F::T, V = (F::V, W)>> where
    G: Fun<T = F::T, V = W>,
    A: Into<Anim<G>>, 
[src]

Combine two animations into one, yielding an animation having pairs as the values.

pub fn bind<W, G>(
    self,
    f: impl Fn(F::V) -> Anim<G>
) -> Anim<impl Fun<T = F::T, V = W>> where
    G: Fun<T = F::T, V = W>, 
[src]

impl<F> Anim<F> where
    F: Fun,
    F::T: Copy + Sub<Output = F::T>, 
[src]

pub fn shift_time(self, t_delay: F::T) -> Anim<impl Fun<T = F::T, V = F::V>>[src]

Shift an animation in time, so that it is moved to the right by t_delay.

impl<F> Anim<F> where
    F: Fun,
    F::T: Copy + PartialOrd
[src]

pub fn switch<G, A>(
    self,
    self_end: F::T,
    next: A
) -> Anim<impl Fun<T = F::T, V = F::V>> where
    G: Fun<T = F::T, V = F::V>,
    A: Into<Anim<G>>, 
[src]

Concatenate self with another animation in time, using self until time self_end (non-inclusive), and then switching to next.

Examples

Switch from one constant value to another:

let anim = pareen::constant(1.0f32).switch(0.5f32, 2.0);

assert_approx_eq!(anim.eval(0.0), 1.0);
assert_approx_eq!(anim.eval(0.5), 2.0);
assert_approx_eq!(anim.eval(42.0), 2.0);

Piecewise combinations of functions:

let cubic_1 = pareen::cubic(&[4.4034, 0.0, -4.5455e-2, 0.0]);
let cubic_2 = pareen::cubic(&[-1.2642e1, 2.0455e1, -8.1364, 1.0909]);
let cubic_3 = pareen::cubic(&[1.6477e1, -4.9432e1, 4.7773e1, -1.3818e1]);

// Use cubic_1 for [0.0, 0.4), cubic_2 for [0.4, 0.8) and
// cubic_3 for [0.8, ..).
let anim = cubic_1.switch(0.4, cubic_2).switch(0.8, cubic_3);

impl<F> Anim<F> where
    F: Fun,
    F::T: Copy + PartialOrd + Sub<Output = F::T>, 
[src]

pub fn seq<G, A>(
    self,
    self_end: F::T,
    next: A
) -> Anim<impl Fun<T = F::T, V = F::V>> where
    G: Fun<T = F::T, V = F::V>,
    A: Into<Anim<G>>, 
[src]

Play two animations in sequence, first playing self until time self_end (non-inclusive), and then switching to next. Note that next will see time starting at zero once it plays.

Example

Stay at value 5.0 for ten seconds, then increase value proportionally:

let anim_1 = pareen::constant(5.0f32);
let anim_2 = pareen::prop(2.0f32) + 5.0;
let anim = anim_1.seq(10.0, anim_2);

assert_approx_eq!(anim.eval(0.0), 5.0);
assert_approx_eq!(anim.eval(10.0), 5.0);
assert_approx_eq!(anim.eval(11.0), 7.0);

impl<F> Anim<F> where
    F: Fun,
    F::T: Copy + Sub<Output = F::T>, 
[src]

pub fn backwards(self, end: F::T) -> Anim<impl Fun<T = F::T, V = F::V>>[src]

Play an animation backwards, starting at time end.

Example

let anim = pareen::prop(2.0f32).backwards(1.0);

assert_approx_eq!(anim.eval(0.0f32), 2.0);
assert_approx_eq!(anim.eval(1.0f32), 0.0);

impl<F> Anim<F> where
    F: Fun,
    F::T: Copy,
    F::V: Copy + Num
[src]

pub fn scale_min_max(
    self,
    min: F::V,
    max: F::V
) -> Anim<impl Fun<T = F::T, V = F::V>>
[src]

Given animation values in [0.0 .. 1.0], this function transforms the values so that they are in [min .. max].

Example

let min = -3.0f32;
let max = 10.0;
let anim = pareen::id().scale_min_max(min, max);

assert_approx_eq!(anim.eval(0.0f32), min);
assert_approx_eq!(anim.eval(1.0f32), max);

impl<F> Anim<F> where
    F: Fun,
    F::V: Float
[src]

pub fn sin(self) -> Anim<impl Fun<T = F::T, V = F::V>>[src]

Apply Float::sin to the animation values.

pub fn cos(self) -> Anim<impl Fun<T = F::T, V = F::V>>[src]

Apply Float::cos to the animation values.

pub fn abs(self) -> Anim<impl Fun<T = F::T, V = F::V>>[src]

Apply Float::abs to the animation values.

pub fn powf(self, e: F::V) -> Anim<impl Fun<T = F::T, V = F::V>>[src]

Apply Float::powf to the animation values.

pub fn powi(self, n: i32) -> Anim<impl Fun<T = F::T, V = F::V>>[src]

Apply Float::powi to the animation values.

impl<F> Anim<F> where
    F: Fun,
    F::T: Copy + Float,
    F::V: Copy
[src]

pub fn squeeze(
    self,
    default: F::V,
    range: RangeInclusive<F::T>
) -> Anim<impl Fun<T = F::T, V = F::V>>
[src]

Transform an animation in time, so that its time [0 .. 1] is shifted and scaled into the given range.

In other words, this function can both delay and speed up or slow down a given animation.

For time inputs outside the range, the default value is returned.

Example

Go from zero to 2π in half a second:

// From zero to 2π in one second
let angle = pareen::circle();

// From zero to 2π from time 0.5 to 1.0
let anim = angle.squeeze(42.0f32, 0.5..=1.0);

assert_approx_eq!(anim.eval(0.0f32), 42.0);
assert_approx_eq!(anim.eval(0.5), 0.0);
assert_approx_eq!(anim.eval(1.0), std::f32::consts::PI * 2.0);
assert_approx_eq!(anim.eval(1.1), 42.0);

impl<W, F> Anim<F> where
    F: Fun,
    F::T: Copy + Mul<W, Output = W>,
    F::V: Copy + Add<W, Output = F::V> + Sub<Output = W>, 
[src]

pub fn lerp<G, A>(self, other: A) -> Anim<impl Fun<T = F::T, V = F::V>> where
    G: Fun<T = F::T, V = F::V>,
    A: Into<Anim<G>>, 
[src]

Linearly interpolate between two animations, starting at time zero and finishing at time one.

Examples

Linearly interpolate between two constant values:

let anim = pareen::constant(5.0f32).lerp(10.0);

assert_approx_eq!(anim.eval(0.0f32), 5.0);
assert_approx_eq!(anim.eval(0.5), 7.5);
assert_approx_eq!(anim.eval(1.0), 10.0);
assert_approx_eq!(anim.eval(2.0), 15.0);

It is also possible to linearly interpolate between two non-constant animations:

let anim = pareen::circle().sin().lerp(pareen::circle().cos());
let value: f32 = anim.eval(0.5f32);

impl<V, F> Anim<F> where
    V: Float,
    F: Fun<T = V, V = V>, 
[src]

pub fn seq_ease_in<E, G, A>(
    self,
    self_end: V,
    _easing: E,
    ease_duration: V,
    next: A
) -> Anim<impl Fun<T = V, V = V>> where
    E: Easing<V>,
    G: Fun<T = V, V = V>,
    A: Into<Anim<G>>, 
[src]

Play two animations in sequence, transitioning between them with an easing-in function from easer.

This is only available when enabling the easer feature for pareen.

The values of self at self_end and of next at time zero are used to determine the parameters of the easing function.

Note that, as with seq, the next animation will see time starting at zero once it plays.

Arguments

  • self_end - Time at which the self animation is to stop.
  • _easing - A struct implementing easer::functions::Easing. This determines the easing function that will be used for the transition. It is passed as a parameter here to simplify type inference.
  • ease_duration - The amount of time to use for transitioning to next.
  • next - The animation to play after transitioning.

Example

See seq_ease_in_out for an example.

pub fn seq_ease_out<E, G, A>(
    self,
    self_end: V,
    _: E,
    ease_duration: V,
    next: A
) -> Anim<impl Fun<T = V, V = V>> where
    E: Easing<V>,
    G: Fun<T = V, V = V>,
    A: Into<Anim<G>>, 
[src]

Play two animations in sequence, transitioning between them with an easing-out function from easer.

This is only available when enabling the easer feature for pareen.

The values of self at self_end and of next at time zero are used to determine the parameters of the easing function.

Note that, as with seq, the next animation will see time starting at zero once it plays.

Arguments

  • self_end - Time at which the self animation is to stop.
  • _easing - A struct implementing easer::functions::Easing. This determines the easing function that will be used for the transition. It is passed as a parameter here to simplify type inference.
  • ease_duration - The amount of time to use for transitioning to next.
  • next - The animation to play after transitioning.

Example

See seq_ease_in_out for an example.

pub fn seq_ease_in_out<E, G, A>(
    self,
    self_end: V,
    _: E,
    ease_duration: V,
    next: A
) -> Anim<impl Fun<T = V, V = V>> where
    E: Easing<V>,
    G: Fun<T = V, V = V>,
    A: Into<Anim<G>>, 
[src]

Play two animations in sequence, transitioning between them with an easing-in-out function from easer.

This is only available when enabling the easer feature for pareen.

The values of self at self_end and of next at time zero are used to determine the parameters of the easing function.

Note that, as with seq, the next animation will see time starting at zero once it plays.

Arguments

  • self_end - Time at which the self animation is to stop.
  • _easing - A struct implementing easer::functions::Easing. This determines the easing function that will be used for the transition. It is passed as a parameter here to simplify type inference.
  • ease_duration - The amount of time to use for transitioning to next.
  • next - The animation to play after transitioning.

Example

Play a constant value until time 0.5, then transition for 0.3 time units, using a cubic function, into a second animation:

let first_anim = pareen::constant(2.0);
let second_anim = pareen::prop(1.0f32);
let anim = first_anim.seq_ease_in_out(
    0.5,
    easer::functions::Cubic,
    0.3,
    second_anim,
);

The animation will look like this:

plot for seq_ease_in_out

impl<V, F> Anim<F> where
    F: Fun<V = Option<V>>,
    F::T: Copy
[src]

pub fn unwrap_or<G, A>(self, default: A) -> Anim<impl Fun<T = F::T, V = V>> where
    G: Fun<T = F::T, V = V>,
    A: Into<Anim<G>>, 
[src]

Unwrap an animation of optional values.

At any time, returns the animation value if it is not None, or the given default value otherwise.

Examples

let anim1 = pareen::constant(Some(42)).unwrap_or(-1);
assert_eq!(anim1.eval(2), 42);
assert_eq!(anim1.eval(3), 42);
let cond = pareen::fun(|t| t % 2 == 0);
let anim1 = pareen::cond(cond, Some(42), None).unwrap_or(-1);
assert_eq!(anim1.eval(2), 42);
assert_eq!(anim1.eval(3), -1);

pub fn map_or<W, G, H, A>(
    self,
    default: A,
    f: impl Fn(V) -> Anim<H>
) -> Anim<impl Fun<T = F::T, V = W>> where
    G: Fun<T = F::T, V = W>,
    H: Fun<T = F::T, V = W>,
    A: Into<Anim<G>>, 
[src]

Applies a function to the contained value (if any), or returns the provided default (if not).

Note that the function f itself returns an animation.

Example

Animate a player's position offset if it is moving:

fn my_offset_anim(
    move_dir: Option<f32>
) -> pareen::Anim<impl pareen::Fun<T = f32, V = f32>> {
    let move_speed = 2.0f32;

    pareen::constant(move_dir).map_or(
        0.0,
        move |move_dir| pareen::prop(move_dir) * move_speed,
    )
}

let move_anim = my_offset_anim(Some(1.0));
let stay_anim = my_offset_anim(None);

assert_approx_eq!(move_anim.eval(0.5), 1.0);
assert_approx_eq!(stay_anim.eval(0.5), 0.0);

Trait Implementations

impl<T, V, F> From<F> for Anim<WrapFn<T, V, F>> where
    F: Fn(T) -> V, 
[src]

impl<T, V> From<V> for Anim<ConstantClosure<T, V>> where
    V: Copy
[src]

impl<F: Clone> Clone for Anim<F>[src]

impl<F: Debug> Debug for Anim<F>[src]

impl<F, G> Sub<Anim<G>> for Anim<F> where
    F: Fun,
    G: Fun<T = F::T>,
    F::V: Sub<G::V>, 
[src]

type Output = Anim<AddClosure<F, NegClosure<G>>>

The resulting type after applying the - operator.

impl<F, G> Add<Anim<G>> for Anim<F> where
    F: Fun,
    G: Fun<T = F::T>,
    F::V: Add<G::V>, 
[src]

type Output = Anim<AddClosure<F, G>>

The resulting type after applying the + operator.

impl<V, F> Add<V> for Anim<F> where
    V: Copy,
    F: Fun<V = V>, 
[src]

type Output = Anim<AddClosure<F, ConstantClosure<F::T, F::V>>>

The resulting type after applying the + operator.

impl<F, G> Mul<Anim<G>> for Anim<F> where
    F: Fun,
    F::T: Copy,
    G: Fun<T = F::T>,
    F::V: Mul<G::V>, 
[src]

type Output = Anim<MulClosure<F, G>>

The resulting type after applying the * operator.

impl<V, F> Mul<V> for Anim<F> where
    V: Copy,
    F: Fun<V = V>,
    F::T: Copy
[src]

type Output = Anim<MulClosure<F, ConstantClosure<F::T, F::V>>>

The resulting type after applying the * operator.

impl<V, F> Neg for Anim<F> where
    V: Copy,
    F: Fun<V = V>, 
[src]

type Output = Anim<NegClosure<F>>

The resulting type after applying the - operator.

Auto Trait Implementations

impl<F> Send for Anim<F> where
    F: Send

impl<F> Sync for Anim<F> where
    F: Sync

impl<F> Unpin for Anim<F> where
    F: Unpin

impl<F> UnwindSafe for Anim<F> where
    F: UnwindSafe

impl<F> RefUnwindSafe for Anim<F> where
    F: RefUnwindSafe

Blanket Implementations

impl<T> From<!> for T[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = !

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]