diff --git a/src/functions/back.rs b/src/functions/back.rs index 8f90952..da7d02b 100644 --- a/src/functions/back.rs +++ b/src/functions/back.rs @@ -1,32 +1,33 @@ use super::ease::Easing; + /// This struct captures Back easing functions pub struct Back; impl Easing for Back { fn ease_in(t: f32, b: f32, c: f32, d: f32) -> f32 { let s = 1.70158_f32; - let inner_t = t / d; - let post_fix = inner_t; - c * (post_fix) * inner_t * ((s + 1.0) * inner_t - s) + b + let t = t / d; + c * t * t * ((s + 1.0) * t - s) + b } fn ease_out(t: f32, b: f32, c: f32, d: f32) -> f32 { let s = 1.70158_f32; - let inner_t = (t / d) - 1.0; - c * (inner_t * inner_t * ((s + 1.0) * inner_t + s) + 1.0) + b + let t = (t / d) - 1.0; + c * (t * t * ((s + 1.0) * t + s) + 1.0) + b } fn ease_in_out(t: f32, b: f32, c: f32, d: f32) -> f32 { - let mut s = 1.70158_f32; - let mut inner_t = t / (d / 2.0); - if inner_t < 1.0 { - s *= 1.525f32; - return c / 2.0 * (inner_t * inner_t * ((s + 1.0) * inner_t - s)) + b; + let s = 1.70158_f32; + let t = t / (d / 2.0); + if t < 1.0 { + let s = s * 1.525f32; + c / 2.0 * (t * t * ((s + 1.0) * t - s)) + b + } + else { + let t = t - 2.0; + let s = s * 1.525f32; + c / 2.0 * (t * t * ((s + 1.0) * t + s) + 2.0) + b } - inner_t -= 2.0; - let post_fix: f32 = inner_t; - let inner_s = s * 1.525f32; - return c / 2.0 * (post_fix * inner_t * ((inner_s + 1.0) * inner_t + inner_s) + 2.0) + b; } } diff --git a/src/functions/bounce.rs b/src/functions/bounce.rs index f06b2e2..01120b5 100644 --- a/src/functions/bounce.rs +++ b/src/functions/bounce.rs @@ -1,4 +1,5 @@ use super::ease::Easing; + /// This struct captures Bounce easing functions pub struct Bounce; @@ -8,22 +9,21 @@ impl Easing for Bounce { } fn ease_out(t: f32, b: f32, c: f32, d: f32) -> f32 { - let mut inner_t = t / d; - if inner_t < 1.0 / 2.75 { - c * (7.5625 * inner_t * inner_t) + b - } else if inner_t < 2.0 / 2.75 { - inner_t -= 1.5 / 2.75; - c * (7.5625 * inner_t * inner_t + 0.75) + b - } else if inner_t < 2.5 / 2.75 { - inner_t -= 2.25 / 2.75; - c * (7.5625 * inner_t * inner_t + 0.9375) + b + let t = t / d; + if t < 1.0 / 2.75 { + c * (7.5625 * t * t) + b + } else if t < 2.0 / 2.75 { + let t = t - 1.5 / 2.75; + c * (7.5625 * t * t + 0.75) + b + } else if t < 2.5 / 2.75 { + let t = t - 2.25 / 2.75; + c * (7.5625 * t * t + 0.9375) + b } else { - inner_t -= 2.625 / 2.75; - c * (7.5625 * inner_t * inner_t + 0.984375) + b + let t = t - 2.625 / 2.75; + c * (7.5625 * t * t + 0.984375) + b } } - fn ease_in_out(t: f32, b: f32, c: f32, d: f32) -> f32 { if t < (d / 2.0) { Bounce::ease_in(t * 2.0, 0.0, c, d) * 0.5 + b diff --git a/src/functions/circ.rs b/src/functions/circ.rs index 57adb98..ad21ee3 100644 --- a/src/functions/circ.rs +++ b/src/functions/circ.rs @@ -1,26 +1,28 @@ use super::ease::Easing; + /// This struct captures Circ easing functions pub struct Circ; impl Easing for Circ { fn ease_in(t: f32, b: f32, c: f32, d: f32) -> f32 { - let inner_t = t / d; - -c * ((1.0 - inner_t * inner_t).sqrt() - 1.0) + b + let t = t / d; + -c * ((1.0 - t * t).sqrt() - 1.0) + b } fn ease_out(t: f32, b: f32, c: f32, d: f32) -> f32 { - let inner_t = t / d - 1.0; - c * (1.0 - inner_t * inner_t).sqrt() + b + let t = t / d - 1.0; + c * (1.0 - t * t).sqrt() + b } fn ease_in_out(t: f32, b: f32, c: f32, d: f32) -> f32 { - let mut inner_t = t / (d / 2.0); - if inner_t < 1.0 { - return -c / 2.0 * ((1.0 - inner_t * inner_t).sqrt() - 1.0) + b; + let t = t / (d / 2.0); + if t < 1.0 { + -c / 2.0 * ((1.0 - t * t).sqrt() - 1.0) + b + } + else { + let t = t - 2.0; + c / 2.0 * ((1.0 - t * t).sqrt() + 1.0) + b } - - inner_t -= 2.0; - return c / 2.0 * ((1.0 - inner_t * inner_t).sqrt() + 1.0) + b; } } diff --git a/src/functions/cubic.rs b/src/functions/cubic.rs index 583f352..2381ce4 100644 --- a/src/functions/cubic.rs +++ b/src/functions/cubic.rs @@ -5,22 +5,24 @@ pub struct Cubic; impl Easing for Cubic { fn ease_in(t: f32, b: f32, c: f32, d: f32) -> f32 { - let inner_t = t / d; - c * inner_t.powi(3) + b + let t = t / d; + c * (t * t * t) + b } fn ease_out(t: f32, b: f32, c: f32, d: f32) -> f32 { - let inner_t = t / d - 1.0; - c * (inner_t.powi(3) + 1.0) + b + let t = t / d - 1.0; + c * ((t * t * t) + 1.0) + b } fn ease_in_out(t: f32, b: f32, c: f32, d: f32) -> f32 { - let mut inner_t = t / (d / 2.0); - if inner_t < 1.0 { - return c / 2.0 * inner_t.powi(3) + b; + let t = t / (d / 2.0); + if t < 1.0 { + c / 2.0 * (t * t * t) + b + } + else { + let t = t - 2.0; + c / 2.0 * (t * t * t + 2.0) + b } - inner_t = inner_t - 2.0; - return c / 2.0 * (inner_t.powi(3) + 2.0) + b; } } diff --git a/src/functions/elastic.rs b/src/functions/elastic.rs index 7a40151..2901bc3 100644 --- a/src/functions/elastic.rs +++ b/src/functions/elastic.rs @@ -10,18 +10,18 @@ impl Easing for Elastic { return b; } - let mut inner_t = t / d; - if inner_t == 1.0 { + let t = t / d; + if t == 1.0 { return b + c; } let p = d * 0.3; let a = c; let s = p / 4.0; - inner_t -= 1.0; - let post_fix = a * 2_f32.powf(10.0 * inner_t); - let temp = (inner_t * d - s) * (2.0 * PI) / p; - return -(post_fix * temp.sin()) + b; + let t = t - 1.0; + let post_fix = a * 2_f32.powf(10.0 * t); + let temp = (t * d - s) * (2.0 * PI) / p; + -(post_fix * temp.sin()) + b } fn ease_out(t: f32, b: f32, c: f32, d: f32) -> f32 { @@ -29,16 +29,16 @@ impl Easing for Elastic { return b; } - let inner_t = t / d; - if inner_t == 1.0 { + let t = t / d; + if t == 1.0 { return b + c; } let p = d * 0.3; let a = c; let s = p / 4.0; - let temp = (inner_t * d - s) * (2.0 * PI) / p; - return a * 2_f32.powf(-10.0 * inner_t) * temp.sin() + c + b; + let temp = (t * d - s) * (2.0 * PI) / p; + a * 2_f32.powf(-10.0 * t) * temp.sin() + c + b } fn ease_in_out(t: f32, b: f32, c: f32, d: f32) -> f32 { @@ -46,8 +46,8 @@ impl Easing for Elastic { return b; } - let mut inner_t = t / (d / 2.0); - if inner_t == 2.0 { + let t = t / (d / 2.0); + if t == 2.0 { return b + c; } @@ -55,17 +55,17 @@ impl Easing for Elastic { let a = c; let s = p / 4.0; - if inner_t < 1.0 { - inner_t -= 1.0; - let post_fix = a * 2_f32.powf(10.0 * inner_t); - let temp = (inner_t * d - s) * (2.0 * PI) / p; + if t < 1.0 { + let t = t - 1.0; + let post_fix = a * 2_f32.powf(10.0 * t); + let temp = (t * d - s) * (2.0 * PI) / p; return -0.5 * (post_fix * temp.sin()) + b; } - inner_t -= 1.0; - let post_fix = a * 2_f32.powf(-10.0 * inner_t); - let temp = (inner_t * d - s) * (2.0 * PI) / p; - return post_fix * temp.sin() * 0.5 + c + b; + let t = t - 1.0; + let post_fix = a * 2_f32.powf(-10.0 * t); + let temp = (t * d - s) * (2.0 * PI) / p; + post_fix * temp.sin() * 0.5 + c + b } } diff --git a/src/functions/expo.rs b/src/functions/expo.rs index 3ddc2dd..adcfa63 100644 --- a/src/functions/expo.rs +++ b/src/functions/expo.rs @@ -1,4 +1,5 @@ use super::ease::Easing; + /// This struct captures Expo easing functions pub struct Expo; @@ -26,12 +27,14 @@ impl Easing for Expo { if t == d { return b + c; } - let mut inner_t = t / (d / 2.0); - if inner_t < 1.0 { - return c / 2.0 * 2_f32.powf(10.0 * (inner_t - 1.0)) + b; + let t = t / (d / 2.0); + if t < 1.0 { + c / 2.0 * 2_f32.powf(10.0 * (t - 1.0)) + b + } + else { + let t = t - 1.0; + c / 2.0 * (-(2_f32.powf(-10.0 * t)) + 2.0) + b } - inner_t -= 1.0; - return c / 2.0 * (-(2_f32.powf(-10.0 * inner_t)) + 2.0) + b; } } diff --git a/src/functions/quad.rs b/src/functions/quad.rs index 962c315..667f0ff 100644 --- a/src/functions/quad.rs +++ b/src/functions/quad.rs @@ -5,22 +5,22 @@ pub struct Quad; impl Easing for Quad { fn ease_in(t: f32, b: f32, c: f32, d: f32) -> f32 { - let inner_t = t / d; - c * inner_t.powi(2) + b + let t = t / d; + c * t * t + b } fn ease_out(t: f32, b: f32, c: f32, d: f32) -> f32 { - let inner_t = t / d; - -c * inner_t * (inner_t - 2.0) + b + let t = t / d; + -c * t * (t - 2.0) + b } fn ease_in_out(t: f32, b: f32, c: f32, d: f32) -> f32 { - let mut t = t / (d / 2.0); + let t = t / (d / 2.0); if t < 1.0 { c / 2.0 * t * t + b } else { - t -= 1.0; + let t = t - 1.0; -c / 2.0 * (t * (t - 2.0) - 1.0) + b } } diff --git a/src/functions/quart.rs b/src/functions/quart.rs index 78faa5f..0729491 100644 --- a/src/functions/quart.rs +++ b/src/functions/quart.rs @@ -1,27 +1,29 @@ use super::ease::Easing; + /// This struct captures Quart easing functions pub struct Quart; impl Easing for Quart { fn ease_in(t: f32, b: f32, c: f32, d: f32) -> f32 { - let inner_t = t / d; - c * inner_t.powi(4) + b + let t = t / d; + c * (t * t * t * t) + b } fn ease_out(t: f32, b: f32, c: f32, d: f32) -> f32 { - let inner_t = t / d - 1.0; - -c * (inner_t.powi(4) - 1.0) + b + let t = t / d - 1.0; + -c * ((t * t * t * t) - 1.0) + b } fn ease_in_out(t: f32, b: f32, c: f32, d: f32) -> f32 { - let mut inner_t = t / (d / 2.0); - - if inner_t < 1.0 { - return c / 2.0 * inner_t * inner_t * inner_t * inner_t + b; + let t = t / (d / 2.0); + if t < 1.0 { + c / 2.0 * (t * t * t * t) + b + } + else { + let t = t - 2.0; + -c / 2.0 * ((t * t * t * t) - 2.0) + b } - inner_t -= 2.0; - return -c / 2.0 * (inner_t.powi(4) - 2.0) + b; } } diff --git a/src/functions/quint.rs b/src/functions/quint.rs index 99f6230..f4a1c42 100644 --- a/src/functions/quint.rs +++ b/src/functions/quint.rs @@ -5,23 +5,23 @@ pub struct Quint; impl Easing for Quint { fn ease_in(t: f32, b: f32, c: f32, d: f32) -> f32 { - let inner_t = t / d; - c * inner_t.powi(5) + b + let t = t / d; + c * (t * t * t * t * t) + b } fn ease_out(t: f32, b: f32, c: f32, d: f32) -> f32 { - let inner_t = t / d - 1.0; - c * (inner_t.powi(5) + 1.0) + b + let t = t / d - 1.0; + c * ((t * t * t * t * t) + 1.0) + b } fn ease_in_out(t: f32, b: f32, c: f32, d: f32) -> f32 { - let mut inner_t = t / (d / 2.0); - if inner_t < 1.0 { - return (c / 2.0) * inner_t.powi(5) + b; + let t = t / (d / 2.0); + if t < 1.0 { + (c / 2.0) * (t * t * t * t * t) + b + } else { + let t = t - 2.0; + c / 2.0 * ((t * t * t * t * t) + 2.0) + b } - inner_t -= 2.0; - return c / 2.0 * (inner_t.powi(5) + 2.0) + b; - } }