Skip to content

Instantly share code, notes, and snippets.

@bakercp
Created September 12, 2014 22:23
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save bakercp/ec03c6e4668a36e817a0 to your computer and use it in GitHub Desktop.
BlendBounceClock
color[] colors = new color[4];
PVector[] vertices = new PVector[4];
int colorIndexOffset = 0;
long lastTime = 0;
void setup()
{
size(800, 800, P3D);
colors[0] = color(0, 255, 0);
colors[1] = color(255, 255, 0);
colors[2] = color(255, 0, 0);
colors[3] = color(0, 0, 255);
vertices[0] = new PVector(0, 0, 0);
vertices[1] = new PVector(width, 0, 0);
vertices[2] = new PVector(width, height, 0);
vertices[3] = new PVector(0, height, 0);
}
void draw()
{
long inc = 1000;
long thisTime = millis() % inc;
if (thisTime < lastTime)
{
colorIndexOffset = (colorIndexOffset + 1) % 4;
}
lastTime = thisTime;
background(255);
pushMatrix();
noStroke();
beginShape(QUADS);
for (int i = 0; i < 4; ++i)
{
color fromColor = colors[(colorIndexOffset + i) % 4];
color toColor = colors[(colorIndexOffset + 1 + i) % 4];
float amount = tween(thisTime, 0.0, inc, 0.0, 1.0);
color c = lerpColor(fromColor, toColor, amount);
fill(c);
vertex(vertices[i].x, vertices[i].y, vertices[i].z);
}
endShape();
popMatrix();
}
float tween(float value, float inputMin, float inputMax, float outputMin, float outputMax)
{
float t = value - inputMin;
float c = outputMax - outputMin;
float d = inputMax - inputMin;
float b = outputMin;
return easeOut(t, b, c, d);
}
float elasticEaseInOut(float t, float b, float c, float d) {
if (t==0) return b;
if ((t/=d/2)==2) return b+c;
float p=d*(.3f*1.5f);
float a=c;
float s=p/4;
if (t < 1) {
float postFix =a*pow(2, 10*(t-=1)); // postIncrement is evil
return -.5f*(postFix* sin( (t*d-s)*(2*PI)/p )) + b;
}
float postFix = a*pow(2, -10*(t-=1)); // postIncrement is evil
return postFix * sin( (t*d-s)*(2*PI)/p )*.5f + c + b;
}
float easeIn (float t, float b, float c, float d) {
return c - easeOut (d-t, 0, c, d) + b;
}
float easeOut(float t, float b, float c, float d) {
if ((t/=d) < (1/2.75f)) {
return c*(7.5625f*t*t) + b;
} else if (t < (2/2.75f)) {
float postFix = t-=(1.5f/2.75f);
return c*(7.5625f*(postFix)*t + .75f) + b;
} else if (t < (2.5/2.75)) {
float postFix = t-=(2.25f/2.75f);
return c*(7.5625f*(postFix)*t + .9375f) + b;
} else {
float postFix = t-=(2.625f/2.75f);
return c*(7.5625f*(postFix)*t + .984375f) + b;
}
}
float easeInOut(float t, float b, float c, float d) {
if (t < d/2) return easeIn (t*2, 0, c, d) * .5f + b;
else return easeOut (t*2-d, 0, c, d) * .5f + c*.5f + b;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment