Skip to main content

simple_easing/
bounce.rs

1/// <https://easings.net/#easeInBounce>
2#[inline]
3#[must_use]
4pub fn bounce_in(t: f32) -> f32 {
5	1.0 - bounce_out(1.0 - t)
6}
7
8/// <https://easings.net/#easeOutBounce>
9#[inline]
10#[must_use]
11pub fn bounce_out(t: f32) -> f32 {
12	const N1: f32 = 7.5625;
13	const D1: f32 = 2.75;
14	if t < 1.0 / D1 {
15		N1 * t * t
16	} else if t < 2.0 / D1 {
17		N1.mul_add((t - 1.5 / D1).powi(2), 0.75)
18	} else if t < 2.5 / D1 {
19		N1.mul_add((t - 2.25 / D1).powi(2), 0.937_5)
20	} else {
21		N1.mul_add((t - 2.625 / D1).powi(2), 0.984_375)
22	}
23}
24
25/// <https://easings.net/#easeInOutBounce>
26#[inline]
27#[must_use]
28pub fn bounce_in_out(t: f32) -> f32 {
29	if t < 0.5 {
30		(1.0 - bounce_out(2.0f32.mul_add(-t, 1.0))) / 2.0
31	} else {
32		f32::midpoint(1.0, bounce_out(2.0f32.mul_add(t, -1.0)))
33	}
34}