We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Fade to black again....
Page Index Toggle Pages: 1
Fade to black again.... (Read 877 times)
Fade to black again....
Jan 31st, 2006, 11:38am
 
Hi, I'm sure I'm doing something wrong but......

I'm trying to fade a sketch to black over time, something that I thought would be a relatively painless process. I have the following code:

Quote:
int x=0;

void setup() {
 size(300,300);
 colorMode(RGB);
 background(0,0,0);
 smooth();
}

void draw() {
     fill(0,0,0,2);
     noStroke();
     rect(0, 0, 300, 300);
     
     x++;
     if(x>300) { x=0; }
     
     stroke(255,255,255);
     line( x,0,x,300);
}


I thought that the line that's drawn would slowly fade to black? Or have I missed something? While this does fade the line out, it doesn't fade all the way to black. Using higher values (15 for example) as the alpha parameter to fill() works as I would expect. I've tried passing a "color" variable (e.g. color c=#000000) to the fill function but it gives the same result.

I've searched this board for similar issues and found this...
http://processing.org/discourse/yabb/YaBB.cgi?board=Syntax;action=display;num=1083650609

...but I'm not sure if its exactly the same problem and to be honest the end of the discussion goes over my head a bit! One of the replies (from mKoser) offers a solution (using the pixels[] array) which does work, but it's incredibly slow!

Have I missed something?
Re: Fade to black again....
Reply #1 - Jan 31st, 2006, 12:14pm
 
The reason it doesn't go to black is to do with rounding numbers to integers when deciding what the on-screen colour is.

(example numbers)
If it's got a value of 4,4,4 then you draw black with an alpha value of 2, it's go to reduce the colour of 4,4,4 by about 0.25,0.25,0.25 which gives 3.75,3.75,3.75

Now the on screen colour can only be an integer, so 3.75 is rounded back up to 4. So every iteration it tries to darken the pixel, but it ends up getting rounded back up to 4.


As for a solution.. well it works the way you expec in P3D mode, but that doesn't support smooth() and some of your vertical lines don't get drawn.. but I think that's due to a bug in how P3D works out on screen positions...
Re: Fade to black again....
Reply #2 - Feb 13th, 2006, 1:12pm
 
Thank you very much for taking the time to explaining why this wasn't working quite as I'd expected. Back to the pixels[] array for me then!
Re: Fade to black again....
Reply #3 - Nov 6th, 2006, 12:44am
 
perhaps this is a bit of a late post but as seeing i had the same prob. i managed to solve it in a somewhat other way i dont know wich would be better seeing as i am just new to processing but for the fade time just use seconds X framerate

void setup(){
 size (300,300);
 background(0,0,0);
 frameRate(15);
}
int i=0;
void draw() {
 i++;
   fill(255,255,255,i); //i is used for transperancy
 rect(0,0,width,height);

//the 60 stands for 4 seconds seeing as its 4x15
 if (i > 60) {
   i = 0;
 }
}
Page Index Toggle Pages: 1