Save() from Image Array

Hi

I draw on the screen png images that are in motion. The images exist in an array, and I use a for loop in draw to show them. Now I want to save each frame of the images motion, on a PGraphics so that I export this as a transparent image sequence.

At the moment I am getting empty frames. Any idea how can I fix it?

Spot[] sp = new Spot[10];
PImage myImage;
int count = 0;
PGraphics pg;

void setup() {
  size(640, 480);
  imageMode(CENTER);
  pg = createGraphics(640, 480);

  myImage = loadImage("mainImage.png");
  for (int i = 0; i < sp.length; i++) {
    sp[i] = new Spot(random(width), random(height), random(0.2, 1.0), random(0.2, 2.0));
  }
}

void draw() {
  background(0, 0);

  pg.beginDraw();
  pg.loadPixels();
  for (int i = 0; i < sp.length; i++) {
    sp[i].move();
    sp[i].display();
  }

  pg.updatePixels();
  pg.endDraw();

  if (key == 'a') {
    count++;
    pg.save("normal"+count+".png");
  }
}

class Spot {
  float x, y, diameter, speed;
  Spot(float _x, float _y, float _diameter, float _speed) {
    x = _x;
    y = _y;
    diameter = _diameter;
    speed = _speed;
  }

  void move() {
    y += speed;
    if (y < - myImage.width*diameter/2) {
      x = random(width);
      y = height + myImage.width*diameter/2;
      speed = random(0.2, 2);
    }
  }

  void display() {
    pushMatrix();
    translate(x, y);
    rotate(TWO_PI*diameter);
    scale(diameter);
    //tint(255, 255, 255, 153);
    image(myImage, 0, 0);
    popMatrix();
  }
}

Answers

  • edited April 2014 Answer ✓

    Line #60 is stamping your myImage in the main canvas rather than your own PGraphics! ;;)

  • edited April 2014

    Thanks for the response GoToLoop I guess Im missing something because pointing pg to myImage, displays nothing on the screen..

    UPDATE*** Ok, I see what you mean! Cheers!

Sign In or Register to comment.