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 & HelpSyntax Questions › Basic animation problem
Page Index Toggle Pages: 1
Basic animation problem (Read 987 times)
Basic animation problem
Dec 13th, 2005, 11:57pm
 
As a Processing newbie, I am confused about how to animate a line in Processing. I've looked at your Linear example:

// Linear
// by REAS <http://reas.com>

float a = 100;

void setup()
{
 size(200, 200);
 stroke(255);
 framerate(30);
}

void draw()
{
 background(51);
 a = a - 1;
 if (a < 0) {
   a = height;
 }
 line(0, a, width, a);  
}

I understand what's going on here fine. But if I rewrite this using a 'for' loop rather than an 'if' test as follows:

// Linear . Saulbass (for) version

void setup()
{
 size(200, 200);
 stroke(255);
 framerate(30);
}

void draw()
{
 background(51);
 for(int a=10; a<80; a=a+5) {
   line(0, a, 200, a);  
   }
}

the program waits until the drawing is completed before displaying the result. i.e. no animation. Just a series of already written lines. Why is this? Can I force a display after each iteration of the line command and hence give the impression of animation.

Apologies if this is elementary but after a day looking through the reference and examples it seems that all the animation examples use 'if' test structures. FWIW I find for's easier to 'read'.


Re: Basic animation problem
Reply #1 - Dec 14th, 2005, 7:05am
 
The problem with your version is the for loop keeps resetting the value of 'a' to 10, so nothing visually moves. Although, your lines are all getting redrawn appox 30fps (in the same place.) For loops finish all their iterations before control moves out of the loop, which is why you're not getting the incremental changes you get using "if".
However, If you'd like to work with what you've got, you'll just need to increment 'a' and the loop limit, with a value that is changing in draw(). Here's a simple modification to your code, with some comments. Please let me know if you need any additional explanation.

// begin modified program

/*I declared a new variable 'b' up here
so it has global scope and can be
seen in the draw() structure*/
int b = 0;

void setup()  
{  
 size(200, 200);  
 stroke(255);  
 framerate(30);  
}  

void draw()  
{  
 background(51);  
/*this conditional works like Reas' in the line example, (only your lines will move down.) When 'b' is greater than height, our lines have left the display window, so we reset 'b'to 0 to draw lines at the top again.*/
if (b>height){
   b=0;
}

/* 'a' is initially assigned the value of 'b'when the for loop executes. After the for loop finishes and control exits the loop, 'b' is incremented. When the for loop executes again (during the next draw cycle,)the value of 'a' is initialized again, but to the new value of 'b', which makes the lines appear to move down. Notice I also needed to add 'b' to the loop limit"a<80+b", so the value limit of 80 would remain relative to the value of 'a'*/
 for(int a=b; a<80+b; a+=5) {  
   line(0, a, 200, a);  
 }  
 //don't forget to increment b
b++; // this is a shortcut for writing b=b+1;
}
Re: Basic animation problem
Reply #2 - Dec 14th, 2005, 5:49pm
 
irag,

thanks for taking the time to answer my query. I've looked at your reply and I think perhaps I didn't phrase my question clearly enough.

I was wondering if I could force a screen draw update inside a 'for' loop i.e.

for(int a=b; a<80+b; a+=20) {  
   line(0, a, 200, a);
   draw();  
 }

Your example is similar to Reas's original in that the screen update happens at the end of the draw() loop iteration. Perhaps my confusion is just due to my unfamiliarity with the Processing code flow.

Am I right in thinking that the draw() command is the actual command that issues a screen update?
In this case when the code reaches a command like:
line(0, a, 200, a);
I assume it updates a hidden screen memory area and when it reaches the end of the draw() loop it then updates the screen with this hidden buffer.

I should perhaps explain further what I am looking to do with Processing. Simply put, I want to make a simple slide show presentation program - taking a sequence of images (fname.1.jpg,fname.2.jpg,.....,fname.100.jpg) and dissolve from one to another. i.e. mix from fname1.jpg to fname2.jpg then mix to fname3.jpg etc;

This is what I have come up with so far:



// SlideShow by Saulbass 14 Dec 2005
PImage a,b; // seems to be needed for loadimage to work

void setup()
{
 size(200, 200);  // Size should be the first statement. Image size
 String dummy = "../dummy.jpg"; // set String to a dummy image name
 a = loadImage(dummy); b = loadImage(dummy);  // 'declare' both images
 PFont fontA = loadFont("../CourierNew36.vlw"); textFont(fontA, 12); // font setup
}
  //Declare main loop variables here. (not in setup).
  float Maxlcv = 255; // Maximum value of loop count variable, lcv.
  //Can be adjusted along with DissolveSpeed to control rate of transition.
  float lcv = Maxlcv; //loop count variable
  float transp; // transparency variable
  String prefix = "../TestWallJPGs/TestWallJPGs."; String suffix = ".jpg"; // String variables pointing to files
  int BufferANum = 1; int BufferBNum = BufferANum - 1; // variables for buffers a and b
  String bufferA = prefix+BufferANum+suffix; // buffer a destination String
  String bufferB = prefix+BufferBNum+suffix; // buffer b destination String
  float DissolveSpeed = 40; // Controls the speed of the dissolve. i.e try .(max 60) or (min0.5)
  int SequLen = 132; // Maximum count of frames in sequence.

void draw() // Main SlideShow loop
{
transp = lcv ; // set transp variable
background(0); // Set the background to black in between each drawing.
tint(255,255); // set the tint to full alpha
image(a, 0, 0); // display image a
tint(255,transp); // set the tint value - colour,alpha to slowly dissolve on buffer b
image(b, 0, 0); // display image b
//text(BufferANum,0,250); // give a visual indication of current BufferANum if needed.
 lcv = lcv - DissolveSpeed; //decrement loop variable. Change this to speed up the dissolve
 if (lcv < 0) // do this until lcv = 0, then reset lcv to 255, and update the image stores
   {   lcv = Maxlcv; // reset the loop counter
          BufferANum = BufferANum + 1; // increment the buffer a frame number
          BufferBNum = BufferANum - 1; // update the buffer b frame number
          String bufferA = prefix+BufferANum+suffix; // update the strings
          String bufferB = prefix+BufferBNum+suffix; // update the strings
          a = loadImage(bufferA); // update image in the a buffer.
          b = loadImage(bufferB); // update image in the b buffer.
           if (BufferANum > SequLen) {BufferANum = 1;} // Reset the BufferANum loop if it goes beyond sequence length
       }
}




And it seems to work fairly well. (Notice I haven't used any 'for' structures!)

I suppose I was hoping to structure it as follows:

for framenum = 1 to sequencelength step 1

for transp = 1 to 255 step 10


display bufferA


display bufferB (with varying transparency)

next transp
next framenum

(in a very simplified form...) but I'm coming to the conclusion that this isn't Processings way...

I'm assuming that I can export this SlideShow as a java app and that I can then setup my mac (running 10.3) to autorun & present this SlideShow java app from startup so that the machine will effectively act as a full frame automatic SlideShow presenter. (I haven't yet looked at this in detail, I just wanted to check that this is possible in theory).

Finally one other question!
I've tried my simple SlideShow sketch on an Dual G4/866 Mac (10.3) and a Pentium 4 2Ghz (XP) and with a large image size of say 1024x768 the screen update is very slow. Is there anything I can do speed up my SlideShow sketch?

Many thanks,

Saulbass.
Re: Basic animation problem
Reply #3 - Dec 14th, 2005, 7:07pm
 
The screen is only updated at the end of draw(). While in draw(), Processing writes images to an offscreen buffer and then at the end of draw, this buffer is pushed to the Display Window.
Re: Basic animation problem
Reply #4 - Dec 15th, 2005, 9:01pm
 
"...I'm assuming that I can export this SlideShow as a java app and that I can then setup my mac (running 10.3) to autorun & present this SlideShow java app from startup so that the machine will effectively act as a full frame automatic SlideShow presenter. (I haven't yet looked at this in detail, I just wanted to check that this is possible in theory)."

It's possible, but IPhoto does this really well, I assume with hardware acceleration (which'll very simply solve the performance issue.) However, I certainly don't want to squelch the creative spirit. Here's some info on setting up a startup app. in Panther.
www.osxfaq.com/Tutorials/LearningCenter/HowTo/Startup/index.ws
Converting a java app to OS X app bundle is as simple as running jar bundler. Here's some info on that: http://developer.apple.com/documentation/Java/Conceptual/Jar_Bundler/About/chapter_2_section_1.html
Re: Basic animation problem
Reply #5 - Dec 16th, 2005, 1:17am
 
irag,

Thanks for the links, really appreciated...

(re. iPhoto I've had a quick look at it but always felt uncomfortable with how it forces you down it's own pre-defined paths. I also need the images to display in a fixed order and I would also like to control the update rate.)
Page Index Toggle Pages: 1