How to fade in object with for loop

HI Guys,

i am relativ new to programming and I have a question.
I am trying to fade in an object by using a for loop that increases the alpha value of the object. But it does not fade in, it only appears.


int[] red = new int[4];
int[] blue = new int[4];
int[] green = new int[4];

// Step 1. Import the video library
import processing.video.*;

// Step 2. Declare a Capture object
Capture video;

void setup() {  
  size(595, 842); 
  background(255);

  //Initialize Capture object via Constructor
  video = new Capture(this, 320, 240);  
  video.start();
}

// An event for when a new frame is available
void captureEvent(Capture video) {  
  // Read the image from the camera.  
  video.read();
}
void draw() {
  loadPixels();
  video.loadPixels(); 

  // Get Colors
  color c1 = video.get(50, 50);
  color c2 = video.get(200, 50);
  color c3 = video.get(50, 200);
  color c4 = video.get(200, 200);

  //Process Colors

  //Get Int Value of Red
  int redValue1 = int(red(c1));
  int redValue2 = int(red(c2));
  int redValue3 = int(red(c3));
  int redValue4 = int(red(c4));

  //safe redValue in Array
  red[0] = redValue1;
  red[1] = redValue2;
  red[2] = redValue3;
  red[3] = redValue4;

  //Get Int Value of Blue
  int blueValue1 = int(blue(c1));
  int blueValue2 = int(blue(c2));
  int blueValue3 = int(blue(c3));
  int blueValue4 = int(blue(c4));

  //safe blueValue
  blue[0] = blueValue1;
  blue[1] = blueValue2;
  blue[2] = blueValue3;
  blue[3] = blueValue4;

  //Get Int Value of Green
  int greenValue1 = int(green(c1));
  int greenValue2 = int(green(c2));
  int greenValue3 = int(green(c3));
  int greenValue4 = int(green(c4));

  //safe greenvalue
  green[0] = greenValue1;
  green[1] = greenValue2;
  green[2] = greenValue3;
  green[3] = greenValue4;

  //Get Random Number for color selection of differnt pixels
  int randomColor = int(random(3));
  int randomX = int(random(width));
  int randomY = int(random(height));
  int randomShape = int(random(50));
  int randomSizeX = int(random(20, 150));
  int randomSizeY = int(random(20, 150));


 
  for (float i = 1; i<10; i++) {
    noStroke();
    fill(red[randomColor], blue[randomColor], green[randomColor],10*i);
    rect(randomX, randomY, randomSizeX, randomSizeY, randomShape, randomShape, randomShape, randomShape);
    delay(10);
  }
  
  delay(100);
}
1 Like

Yeah, processing doesn’t update the screen throughout.

Only once at the end of draw ().

Hence you see only the final result of what has happened before.

To avoid this you could get rid of the for loop and use the fact that draw in itself loops automatically 60 times per second.

Make i a global variable and at the end of draw say i++;

2 Likes

Thank you for your help!

nice idea but, it does not work for me…

The problem is that the object hast to be drawn at the same location and this is not the case if i set the variable global…

or maybe i am too noob to understand you :smiley:

1 Like

Ah, because you also load video…

Make it so that only when i reached 100, load the next frame

2 Likes