Skip to content

Commit

Permalink
Merge pull request #6 from Kerollmops/master
Browse files Browse the repository at this point in the history
Clear easing functions for more clarity and performances
  • Loading branch information
orhanbalci authored Dec 9, 2016
2 parents 02023b9 + 920f922 commit 6b668e4
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 96 deletions.
29 changes: 15 additions & 14 deletions src/functions/back.rs
Original file line number Diff line number Diff line change
@@ -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;
}
}

Expand Down
24 changes: 12 additions & 12 deletions src/functions/bounce.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::ease::Easing;

/// This struct captures Bounce easing functions
pub struct Bounce;

Expand All @@ -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
Expand Down
22 changes: 12 additions & 10 deletions src/functions/circ.rs
Original file line number Diff line number Diff line change
@@ -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;
}
}

Expand Down
20 changes: 11 additions & 9 deletions src/functions/cubic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
40 changes: 20 additions & 20 deletions src/functions/elastic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,62 +10,62 @@ 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 {
if t == 0.0 {
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 {
if t == 0.0 {
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;
}

let p = d * 0.3 * 1.5;
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
}
}

Expand Down
13 changes: 8 additions & 5 deletions src/functions/expo.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::ease::Easing;

/// This struct captures Expo easing functions
pub struct Expo;

Expand Down Expand Up @@ -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;
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/functions/quad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
22 changes: 12 additions & 10 deletions src/functions/quart.rs
Original file line number Diff line number Diff line change
@@ -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;
}
}

Expand Down
20 changes: 10 additions & 10 deletions src/functions/quint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

}
}

Expand Down

0 comments on commit 6b668e4

Please sign in to comment.