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 › changing alpha of an image without redrawing it
Page Index Toggle Pages: 1
changing alpha of an image without redrawing it? (Read 1762 times)
changing alpha of an image without redrawing it?
Oct 20th, 2007, 9:45pm
 
Hi, I started using processing in college last year and still feel pretty new to it all.

i am writing a code for a multi-touch display where I am drawing an image every time the mouse button in pressed and when the user lets off the touch screen the image falls until it's off the screen.

I'm trying to create a trail that slowly fades.

is there a way to directly control an image's alpha value?  The only way I have found is the tint function and that seems to only work before the image is drawn, so I don't think it can be dynamically changed, ya?

Here's a code i figured to have the effect that I want, but I want it to happen for each image drawn dynamically rather than changing each image alpha before it's drawn:

int yspeed=0;
PImage b;
float[] x = new float [1000];
int n=0;


void setup() {
 size (600, 400);
 background (0, 0, 255);  
 b = loadImage("Vectoraindrop.gif");
 for(int i = 255; i > 0; i = i - 1){
   x[i]=i;
 }
}

void draw() {
 image(b, 300, 200);
 if(n<255){
   background (0, 0, 255);
   tint(255, 255, 255, (255-x[n]));
   image(b, 300, 200);
   n=n+1;
   println(x[n]);
 }
}
Re: changing alpha of an image without redrawing i
Reply #1 - Oct 20th, 2007, 10:12pm
 
Changing the opacity of a PImage won't change what's on screen. Once something is drawn to the screen, there is no link back from a bunch of pixels on the screen to the image that was responsible. So to fade the iamge, you have to keep redrawing it.
Re: changing alpha of an image without redrawing i
Reply #2 - Oct 20th, 2007, 10:33pm
 
So, is there no way to create a trail from an image?
Re: changing alpha of an image without redrawing i
Reply #3 - Oct 20th, 2007, 11:09pm
 
You can create a trail fairly easily, just not with the method you were thinking of.

There's 2 options:

Code:
void draw()
{
fill(0,0,0,8); //mostly transparent black
rect(0,0,width,height); // slowly fades anythign drawn previously.
image(myimg,xpos,ypos);
}


Or if you have thinggs you don't want to be faded, and need to just dissapear, then you'll have to store a "current fade" variable, and change it per frame:

Code:
int alpha;
void setup()
{
//...

alpha=255;
}

void draw()
{
background(0);
tint(255,255,255,alpha);
image(myimg,xpos,ypos);
if(alpha>0)
alpha--;
}
Page Index Toggle Pages: 1