Skip to content

Instantly share code, notes, and snippets.

@hysysk
Created September 10, 2014 14:42
Show Gist options
  • Save hysysk/b52c4744888268530bb1 to your computer and use it in GitHub Desktop.
Save hysysk/b52c4744888268530bb1 to your computer and use it in GitHub Desktop.
Fade In/Fade Out of two images
PImage imgA, imgB;
float transparency;
float targetTransparency;
boolean isFadeAtoB;
boolean isFadeCompleted;
int currentTime;
int fadeCompletedTime;
float easing;
void setup() {
imgA = loadImage("a.jpg");
imgB = loadImage("b.jpg");
size(imgA.width, imgA.height);
isFadeAtoB = true;
transparency = 0;
targetTransparency = 255;
isFadeCompleted = false;
easing = 0.02;
}
void draw() {
background(255);
if(targetTransparency - transparency <= 1.0) {
currentTime = millis();
if(isFadeCompleted) {
if(currentTime - fadeCompletedTime >= 5000) {
transparency = 0;
isFadeAtoB = !isFadeAtoB;
isFadeCompleted = false;
}
} else {
isFadeCompleted = true;
fadeCompletedTime = currentTime;
}
}
if(isFadeAtoB) {
noTint();
image(imgA, 0, 0);
tint(255, transparency);
image(imgB, 0, 0);
} else {
noTint();
image(imgB, 0, 0);
tint(255, transparency);
image(imgA, 0, 0);
}
transparency += (targetTransparency - transparency) * easing;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment