Last active
August 24, 2018 15:50
-
-
Save atduskgreg/34f4f8bf6c63b4f62c25 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
float linearTween(float t, float startValue, float deltaT, float duration) { | |
return deltaT*t/duration + startValue; | |
} | |
float cubicEaseInOut(float t, float startValue, float deltaT, float duration) { | |
t /= duration/2; | |
if (t < 1) { | |
return deltaT/2*t*t*t + startValue; | |
} else { | |
t -= 2; | |
return deltaT/2*(t*t*t + 2) + startValue; | |
} | |
} | |
float quadraticEaseInOut(float t, float startValue, float deltaT, float duration) { | |
t /= duration/2; | |
if (t < 1) { | |
return deltaT/2*t*t + startValue; | |
} else { | |
t--; | |
return -deltaT/2 * (t*(t-2) - 1) + startValue; | |
} | |
} | |
float quinticEaseInOut(float t, float startValue, float deltaT, float duration) { | |
t /= duration/2; | |
if (t < 1) { | |
return deltaT/2*t*t*t*t*t + startValue; | |
} else { | |
t-=2; | |
return deltaT/2*(t*t*t*t*t + 2) + startValue; | |
} | |
} | |
float exponentialEaseInOut (float t, float startValue, float deltaT, float duration) { | |
t /= duration/2; | |
if (t < 1) { | |
return deltaT/2 * pow( 2, 10 * (t - 1) ) + startValue; | |
} else { | |
t--; | |
return deltaT/2 * ( -pow( 2, -10 * t) + 2 ) + startValue; | |
} | |
}; | |
int targetY = 250; | |
int prevY = 0; | |
int easeDuration = 50; | |
int easeFunction = 0; | |
int x = 0; | |
float t = 0; | |
ArrayList<PVector> line; | |
void setup() { | |
size(1000, 500); | |
line = new ArrayList<PVector>(); | |
line.add(new PVector(0, 0)); | |
} | |
void draw() { | |
background(0); | |
t += 1; | |
if (t > easeDuration) { | |
t = easeDuration; | |
} | |
float y = prevY; | |
String ease = ""; | |
if (easeFunction == 0) { | |
ease = "quadratic"; | |
y = quadraticEaseInOut(t, prevY, targetY-prevY, easeDuration); | |
} | |
if (easeFunction == 1) { | |
ease = "cubic"; | |
y = cubicEaseInOut(t, prevY, targetY-prevY, easeDuration); | |
} | |
if (easeFunction == 2) { | |
ease = "quintic"; | |
y = quinticEaseInOut(t, prevY, targetY-prevY, easeDuration); | |
} | |
if (easeFunction == 3) { | |
ease = "exponential"; | |
y = exponentialEaseInOut(t, prevY, targetY-prevY, easeDuration); | |
} | |
if (easeFunction == 4) { | |
ease = "linear"; | |
y = linearTween(t, prevY, targetY-prevY, easeDuration); | |
} | |
float x = line.get(line.size()-1).x + 1; | |
line.add(new PVector(x, y)); | |
noFill(); | |
stroke(255); | |
beginShape(); | |
for (PVector p : line) { | |
if (x > width/2) { | |
vertex(p.x - (x-width/2), p.y); | |
} else { | |
vertex(p.x, p.y); | |
} | |
} | |
endShape(); | |
fill(0, 255, 0); | |
text("selected: " + ease + "\n0 - quadrtic\n1- cubic\n2 - quintic\n3 - exponential\n4 - linear", 25, height-100); | |
} | |
void mousePressed() { | |
prevY = targetY; | |
targetY = mouseY; | |
println("prevY: " + prevY + " targetY: " + targetY); | |
t = 0; | |
} | |
void keyPressed() { | |
if (key == '0') { | |
easeFunction = 0; | |
} | |
if (key == '1') { | |
easeFunction = 1; | |
} | |
if (key == '2') { | |
easeFunction = 2; | |
} | |
if (key == '3') { | |
easeFunction = 3; | |
} | |
if (key == '4') { | |
easeFunction = 4; | |
} | |
println(easeFunction); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment