Skip to content

Instantly share code, notes, and snippets.

@hysysk hysysk/fadeInOut.pde
Created Sep 10, 2014

Embed
What would you like to do?
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
You can’t perform that action at this time.