pub fn cond<F, G, H, Cond, A, B>(
cond: Cond,
a: A,
b: B
) -> Anim<impl Fun<T = F::T, V = G::V>>where
F::T: Clone,
F: Fun<V = bool>,
G: Fun<T = F::T>,
H: Fun<T = F::T, V = G::V>,
Cond: Into<Anim<F>>,
A: Into<Anim<G>>,
B: Into<Anim<H>>,
Expand description
Return the value of one of two animations depending on a condition.
This allows returning animations of different types conditionally.
Note that the condition cond
may either be a value true
and false
, or
it may itself be a dynamic animation of type bool
.
For dynamic conditions, in many cases it suffices to use either
Anim::switch
or
Anim::seq
instead of this function.
Examples
Constant conditions
The following example does not compile, because the branches have different types:
ⓘ
let cond = true;
let anim = if cond { pareen::constant(1) } else { pareen::id() };
However, this does compile:
let cond = true;
let anim = pareen::cond(cond, 1, pareen::id());
assert_eq!(anim.eval(2), 1);
Dynamic conditions
let cond = pareen::fun(|t| t * t <= 4);
let anim_1 = 1;
let anim_2 = pareen::id();
let anim = pareen::cond(cond, anim_1, anim_2);
assert_eq!(anim.eval(1), 1); // 1 * 1 <= 4
assert_eq!(anim.eval(2), 1); // 2 * 2 <= 4
assert_eq!(anim.eval(3), 3); // 3 * 3 > 4