Fade in/out effect

So I have this code and I’m trying to make the text fade in and out. I could make in fade in but I couldn’t make it fade out. Could anyone help me?

int t=10; //transparency
void setup()
{
  size(200,200);
}
void draw()
{
  if(t<=255)
  {
    t=t+1;
    fill(23,45,67,t);
    text("help",10,100);
  }
  else if(t>255)
  {
    t=t-1;
    fill(23,45,67,t);
    text("help",10,100);
  }
}
1 Like

hi,
besides that a code is better readable if it is formatted,
you need to work on the
if else…
https://processing.org/reference/else.html

and best is you use a println("");
to check how any logic you code turn out.

or other way could be to take out the text("",x,y);
after the if () fill…
and make it a

text("help, t: "+t,10,100);
3 Likes

sorry! I wasn’t sure how to keep the format since I’ve just made an account but maybe it’s easier to understand now. I’m not sure how to fix my ‘if’ statement because to my understanding, the value of t would keep adding until it reaches 255, and will start to decrease once it hits over 255. I’m not sure if there’s anything wrong with that logic, so maybe you could help me check over my line of thinking?

Explanation

Why the text couldnt fade out is caused by if else logic.
When t reached 256 (t > 255), it will back to 255 and then it will be back again to 256, because it does qualified to enter the else part where the t should be greater than 255. When t is 255 it enters if part rather than else part
So after the text faded in, it will be back and forth between 255 and 256.

Try

@kll has posted or
Print the t to see what happen

3 Likes

Thank you for clarifying that! I’ll try to fix my ‘if’ and check the value of t