Created
June 6, 2016 14:24
-
-
Save Longor1996/e6f0c0c7f6208859917b7fe992e0045c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Java port of: | |
//EASINGS :: https://github.com/prideout/par | |
//Robert Penner's easing functions. | |
// | |
//The MIT License | |
//Copyright (c) 2015 Philip Rideout | |
//Ported by: | |
//Lars Longor K | |
//06.06.2016 | |
//----------------------------------------------------------------------------- | |
//BEGIN PUBLIC API | |
//----------------------------------------------------------------------------- | |
/* | |
public static final float par_easings_linear(float t); | |
public static final float par_easings_in_cubic(float t); | |
public static final float par_easings_out_cubic(float t); | |
public static final float par_easings_in_out_cubic(float t); | |
public static final float par_easings_in_quad(float t); | |
public static final float par_easings_out_quad(float t); | |
public static final float par_easings_in_out_quad(float t); | |
public static final float par_easings_in_elastic(float t); | |
public static final float par_easings_out_elastic(float t); | |
public static final float par_easings_in_out_elastic(float t); | |
public static final float par_easings_in_bounce(float t); | |
public static final float par_easings_out_bounce(float t); | |
public static final float par_easings_in_out_bounce(float t); | |
public static final float par_easings_in_back(float t); | |
public static final float par_easings_out_back(float t); | |
public static final float par_easings_in_out_back(float t); | |
*/ | |
//----------------------------------------------------------------------------- | |
//END PUBLIC API | |
//----------------------------------------------------------------------------- | |
public class ParEasings { | |
private static final float PAR_PI = (float) Math.PI; | |
/** #define PAR_MIN(a, b) (a > b ? b : a) **/ | |
private static final float par_min(float a, float b) { | |
return a > b ? b : a; | |
} | |
/** #define PAR_MAX(a, b) (a > b ? a : b) **/ | |
private static final float par_max(float a, float b) { | |
return a > b ? a : b; | |
} | |
/** #define PAR_CLAMP(v, lo, hi) PAR_MAX(lo, par_min(hi, v)) **/ | |
@SuppressWarnings("unused") | |
private static final float par_clamp(float v, float lo, float hi) { | |
return par_max(lo, par_min(hi, v)); | |
} | |
/** #define PAR_SQR(a) ((a) * (a)) **/ | |
@SuppressWarnings("unused") | |
private static final float par_sqr(float a) { | |
return (a * a); | |
} | |
private static final float pow(float b, float e) { | |
return (float) Math.pow(b, e); | |
} | |
private static final float sqrt(float f) { | |
return (float) Math.sqrt(f); | |
} | |
private static final float fabsf(float f) { | |
return (float) Math.abs(f); | |
} | |
private static final float sin(float f) { | |
return (float) Math.sin(f); | |
} | |
private static final float cos(float f) { | |
return (float) Math.cos(f); | |
} | |
private static final float asin(float f) { | |
return (float) Math.asin(f); | |
} | |
@SuppressWarnings("unused") | |
private static final float acos(float f) { | |
return (float) Math.acos(f); | |
} | |
// ----------------------------------------------------------------------------- | |
// START OF EASING FUNCTIONS | |
// ----------------------------------------------------------------------------- | |
public static final float par_easings__linear(float t, float b, float c, float d) { | |
return c * t / d + b; | |
} | |
public static final float par_easings__in_cubic(float t, float b, float c, float d) { | |
t /= d; | |
return c * t * t * t + b; | |
} | |
public static final float par_easings__out_cubic(float t, float b, float c, float d) { | |
t = t / d - 1f; | |
return c * (t * t * t + 1f) + b; | |
} | |
public static final float par_easings__in_out_cubic(float t, float b, float c, float d) { | |
t /= d / 2f; | |
if (t < 1f) { | |
return c / 2f * t * t * t + b; | |
} | |
t -= 2f; | |
return c / 2f * (t * t * t + 2f) + b; | |
} | |
public static final float par_easings__in_quad(float t, float b, float c, float d) { | |
t /= d; | |
return c * t * t + b; | |
} | |
public static final float par_easings__out_quad(float t, float b, float c, float d) { | |
t /= d; | |
return -c * t * (t - 2f) + b; | |
} | |
public static final float par_easings__in_out_quad(float t, float b, float c, float d) { | |
t /= d / 2f; | |
if (t < 1f) { | |
return c / 2f * t * t + b; | |
} | |
--t; | |
return -c / 2f * (t * (t - 2f) - 1f) + b; | |
} | |
public static final float par_easings__in_quart(float t, float b, float c, float d) { | |
t /= d; | |
return c * t * t * t * t + b; | |
} | |
public static final float par_easings__out_quart(float t, float b, float c, float d) { | |
t = t / d - 1f; | |
return -c * (t * t * t * t - 1f) + b; | |
} | |
public static final float par_easings__in_out_quart(float t, float b, float c, float d) { | |
t /= d / 2f; | |
if (t < 1f) { | |
return c / 2f * t * t * t * t + b; | |
} | |
t -= 2f; | |
return -c / 2f * (t * t * t * t - 2f) + b; | |
} | |
public static final float par_easings__in_quint(float t, float b, float c, float d) { | |
t /= d; | |
return c * t * t * t * t * t + b; | |
} | |
public static final float par_easings__out_quint(float t, float b, float c, float d) { | |
t = t / d - 1f; | |
return c * (t * t * t * t * t + 1f) + b; | |
} | |
public static final float par_easings__in_out_quint(float t, float b, float c, float d) { | |
t /= d / 2f; | |
if (t < 1f) { | |
return c / 2f * t * t * t * t * t + b; | |
} | |
t -= 2f; | |
return c / 2f * (t * t * t * t * t + 2f) + b; | |
} | |
public static final float par_easings__in_sine(float t, float b, float c, float d) { | |
return -c * cos(t / d * (PAR_PI / 2f)) + c + b; | |
} | |
public static final float par_easings__out_sine(float t, float b, float c, float d) { | |
return c * sin(t / d * (PAR_PI / 2f)) + b; | |
} | |
public static final float par_easings__in_out_sine(float t, float b, float c, float d) { | |
return -c / 2f * (cos(PAR_PI * t / d) - 1f) + b; | |
} | |
public static final float par_easings__in_out_expo(float t, float b, float c, float d) { | |
t /= d / 2f; | |
if (t < 1f) { | |
return c / 2f * pow(2f, 10f * (t - 1f)) + b; | |
} | |
return c / 2f * (-pow(2f, -10f * --t) + 2f) + b; | |
} | |
public static final float par_easings__in_circ(float t, float b, float c, float d) { | |
t /= d; | |
return -c * (sqrt(1f - t * t) - 1f) + b; | |
} | |
public static final float par_easings__out_circ(float t, float b, float c, float d) { | |
t = t / d - 1f; | |
return c * sqrt(1f - t * t) + b; | |
} | |
public static final float par_easings__in_out_circ(float t, float b, float c, float d) { | |
t /= d / 2f; | |
if (t < 1f) { | |
return -c / 2f * (sqrt(1f - t * t) - 1f) + b; | |
} | |
t -= 2f; | |
return c / 2f * (sqrt(1f - t * t) + 1f) + b; | |
} | |
public static final float par_easings__in_elastic(float t, float b, float c, float d) { | |
float a, p, s; | |
s = 1.70158f; | |
p = 0f; | |
a = c; | |
if (p != 0f) { | |
p = d * 0.3f; | |
} | |
if (a < fabsf(c)) { | |
a = c; | |
s = p / 4f; | |
} else { | |
s = p / (2f * PAR_PI) * asin(c / a); | |
} | |
t -= 1f; | |
return -(a * pow(2f, 10f * t) * sin((t * d - s) * (2f * PAR_PI) / p)) + b; | |
} | |
public static final float par_easings__out_elastic(float t, float b, float c, float d) { | |
float a, p, s; | |
s = 1.70158f; | |
p = 0f; | |
a = c; | |
if (p != 0f) { | |
p = d * 0.3f; | |
} | |
if (a < fabsf(c)) { | |
a = c; | |
s = p / 4f; | |
} else { | |
s = p / (2f * PAR_PI) * asin(c / a); | |
} | |
return a * pow(2f, -10f * t) * sin((t * d - s) * (2f * PAR_PI) / p) + c + b; | |
} | |
public static final float par_easings__in_out_elastic(float t, float b, float c, float d) { | |
float a, p, s; | |
s = 1.70158f; | |
p = 0f; | |
a = c; | |
if (p != 0f) { | |
p = d * (0.3f * 1.5f); | |
} | |
if (a < fabsf(c)) { | |
a = c; | |
s = p / 4f; | |
} else { | |
s = p / (2f * PAR_PI) * asin(c / a); | |
} | |
if (t < 1f) { | |
t -= 1f; | |
return -0.5f * (a * pow(2f, 10f * t) * sin((t * d - s) * (2f * PAR_PI) / p)) + b; | |
} | |
t -= 1f; | |
return a * pow(2f, -10f * t) * sin((t * d - s) * (2f * PAR_PI) / p) * 0.5f + c + b; | |
} | |
public static final float par_easings__in_back(float t, float b, float c, float d) { | |
float s = 1.70158f; | |
t /= d; | |
return c * t * t * ((s + 1f) * t - s) + b; | |
} | |
public static final float par_easings__out_back(float t, float b, float c, float d) { | |
float s = 1.70158f; | |
t = t / d - 1f; | |
return c * (t * t * ((s + 1f) * t + s) + 1f) + b; | |
} | |
public static final float par_easings__in_out_back(float t, float b, float c, float d) { | |
float s = 1.70158f; | |
t /= d / 2f; | |
s *= 1.525f; | |
if (t < 1f) { | |
return c / 2f * (t * t * ((s + 1f) * t - s)) + b; | |
} | |
s *= 1.525f; | |
t -= 2f; | |
return (c / 2f * (t * t * (s + 1f) * t + s) + 2f) + b; | |
} | |
public static final float par_easings__in_bounce(float t, float b, float c, float d) { | |
float v = par_easings__out_bounce(d - t, 0f, c, d); | |
return c - v + b; | |
} | |
public static final float par_easings__out_bounce(float t, float b, float c, float d) { | |
t /= d; | |
if (t < 1.0f / 2.75f) { | |
return c * (7.5625f * t * t) + b; | |
} | |
if (t < 2.0f / 2.75f) { | |
t -= 1.5f / 2.75f; | |
return c * (7.5625f * t * t + 0.75f) + b; | |
} | |
if (t < 2.5f / 2.75f) { | |
t -= 2.25f / 2.75f; | |
return c * (7.5625f * t * t + 0.9375f) + b; | |
} | |
t -= 2.625f / 2.75f; | |
return c * (7.5625f * t * t + 0.984375f) + b; | |
} | |
public static final float par_easings__in_out_bounce(float t, float b, float c, float d) { | |
float v; | |
if (t < d / 2f) { | |
v = par_easings__in_bounce(t * 2f, 0f, c, d); | |
return v * 0.5f + b; | |
} | |
v = par_easings__out_bounce(t * 2f - d, 0f, c, d); | |
return v * 0.5f + c * 0.5f + b; | |
} | |
// ----------------------------------------------------------------------------- | |
// END OF EASING FUNCTIONS | |
// ----------------------------------------------------------------------------- | |
public static final float par_easings_linear(float t) { | |
return par_easings__linear(t, 0f, 1f, 1f); | |
} | |
public static final float par_easings_in_cubic(float t) { | |
return par_easings__in_cubic(t, 0f, 1f, 1f); | |
} | |
public static final float par_easings_out_cubic(float t) { | |
return par_easings__out_cubic(t, 0f, 1f, 1f); | |
} | |
public static final float par_easings_in_out_cubic(float t) { | |
return par_easings__in_out_cubic(t, 0f, 1f, 1f); | |
} | |
public static final float par_easings_in_quad(float t) { | |
return par_easings__in_quad(t, 0f, 1f, 1f); | |
} | |
public static final float par_easings_out_quad(float t) { | |
return par_easings__out_quad(t, 0f, 1f, 1f); | |
} | |
public static final float par_easings_in_out_quad(float t) { | |
return par_easings__in_out_quad(t, 0f, 1f, 1f); | |
} | |
public static final float par_easings_in_elastic(float t) { | |
return par_easings__in_elastic(t, 0f, 1f, 1f); | |
} | |
public static final float par_easings_out_elastic(float t) { | |
return par_easings__out_elastic(t, 0f, 1f, 1f); | |
} | |
public static final float par_easings_in_out_elastic(float t) { | |
return par_easings__in_out_elastic(t, 0f, 1f, 1f); | |
} | |
public static final float par_easings_in_bounce(float t) { | |
return par_easings__in_bounce(t, 0f, 1f, 1f); | |
} | |
public static final float par_easings_out_bounce(float t) { | |
return par_easings__out_bounce(t, 0f, 1f, 1f); | |
} | |
public static final float par_easings_in_out_bounce(float t) { | |
return par_easings__in_out_bounce(t, 0f, 1f, 1f); | |
} | |
public static final float par_easings_in_back(float t) { | |
return par_easings__in_back(t, 0f, 1f, 1f); | |
} | |
public static final float par_easings_out_back(float t) { | |
return par_easings__out_back(t, 0f, 1f, 1f); | |
} | |
public static final float par_easings_in_out_back(float t) { | |
return par_easings__in_out_back(t, 0f, 1f, 1f); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment