Skip to content

Instantly share code, notes, and snippets.

@hadamlenz
Last active May 22, 2017 21:00
Show Gist options
  • Save hadamlenz/255a9bf0fe82ced251a3039460000195 to your computer and use it in GitHub Desktop.
Save hadamlenz/255a9bf0fe82ced251a3039460000195 to your computer and use it in GitHub Desktop.
/*
*create a new ease Easing ease
*instasiate the new ease ease = new Ease(start, end, steps, type)
*get the value from in draw from ease.step();
*
*ease types are in the switch case starting on line 29
*/
class Easing {
float start, end, change;
int steps, currentStep;
String type;
boolean done;
Easing(float tstart, float tend, int tsteps, String ttype) {
start = tstart;
end = tend;
change = end - start;
steps = tsteps;
type = ttype;
currentStep = 0;
done = false;
}
float step() {
float output = 0.0;
if (!done) {
switch(type) {
case "circularOut":
output = circularOut(currentStep, start, change, steps);
break;
case "cubicIn":
output = cubicIn(currentStep, start, change, steps);
break;
case "cubicOut":
output = cubicOut(currentStep, start, change, steps);
break;
case "linear":
output = linear(currentStep, start, change, steps);
break;
case "quadraticIn":
output = quadraticIn(currentStep, start, change, steps);
break;
case "quadraticOut":
output = quadraticOut(currentStep, start, change, steps);
break;
case "quadraticInOut":
output = quadraticInOut(currentStep, start, change, steps);
break;
case "quintic":
output = quintic(currentStep, start, change, steps);
break;
}
currentStep++;
if (currentStep > steps) {
done = true;
}
}
return output;
}
/*
* the following is inspired from http://gizma.com/easing/
*t current time
*b start value
*c change in value
*d duration
*/
float circularOut(float t, float b, float c, float d) {
t /= d;
t--;
return c * sqrt(1 - t*t) + b;
}
float cubicIn(float t, float b, float c, float d) {
t /= d;
return c*t*t*t + b;
};
float cubicOut(float t, float b, float c, float d) {
t /= d;
t--;
return c*(t*t*t + 1) + b;
};
float linear(float t, float b, float c, float d) {
return c*t/d + b;
};
float quintic(float t, float b, float c, float d) {
t /= d/2;
if (t < 1) return c/2*t*t*t*t*t + b;
t -= 2;
return c/2*(t*t*t*t*t + 2) + b;
}
float quadraticIn(float t, float b, float c, float d) {
t /= d;
return c*t*t + b;
};
float quadraticOut(float t, float b, float c, float d) {
t /= d;
return -c * t*(t-2) + b;
}
float quadraticInOut(float t, float b, float c, float d) {
t /= d/2;
if (t < 1) {
return c/2*t*t + b;
}
t--;
return -c/2 * (t*(t-2) - 1) + b;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment