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 › Header with Animated Background and Static Text.
Page Index Toggle Pages: 1
Header with Animated Background and Static Text. (Read 918 times)
Header with Animated Background and Static Text.
Dec 8th, 2009, 5:20am
 
hi folks, i am trying to make an animated header for a page which work fine. I found most of the code elsewhere and I'm trying to learn the under-pinnings of it. It works  however, it is really slow because the text is being drawn every frame too. is it possible to have the animation in the background and have the text drawn only once and have it remain? i can't figure out how to get the text to stay without redrawing it with the animation. i tried noLoop() but couldn't figure it out.

// Set number of circles
int count = 33;

// Set maximum and minimum circle size
int maxSize = 100;
int minSize = 20;

// Build float array to store circle properties
float[][] e = new float[count][5];

// Set size of dot in circle center
float ds=2;

// Selected mode switch
int sel = 0;

// Set drag switch to false
boolean dragging=false;

// If user drags mouse...
void mouseDragged(){
 
 // Set drag switch to true
 dragging=true;
}
 
// If user releases mouse...
void mouseReleased(){
 
 // ..user is no-longer dragging
 dragging=false;
}

// Set up canvas
void setup(){
 
 // Frame rate
 frameRate(15);
 
 // Size of canvas (width,height)
 //size(screen.width, screen.height);
 size(850,140);
 
 // Stroke/line/border thickness
 strokeWeight(1);
 
 // Initiate array with random values for circles
 for(int j=0;j< count;j++){
   e[j][0]=random(width); // X
   e[j][1]=random(height); // Y
   e[j][2]=random(minSize,maxSize); // Radius
   e[j][3]=random(-.5,.5); // X Speed
   e[j][4]=random(-.5,.5); // Y Speed
 }

}

// Begin main draw loop (called 25 times per second)
void draw(){
 
  // Fill background black
  background(0);
 
 // Begin looping through circle array
 for (int j=0;j< count;j++){
   
   // Disable shape stroke/border
   noStroke();
   
   // Cache diameter and radius of current circle
   float radi=e[j][2];
   float diam=radi/2;
   
   // If the cursor is within 2x the radius of current circle...
   if( dist(e[j][0],e[j][1],mouseX,mouseY) < radi ){
     
     // Change fill color to orange and change transparency
     //fill(64,187,128,100);
     fill(#ffa500, 100);
     
     // Remember user has circle "selected"  
     sel=1;
     
     // If user has mouse down and is moving...
     if(dragging){
       
       // Move circle to circle position
       e[j][0]=mouseX;
       e[j][1]=mouseY;
     }
   } else {
     // Keep fill color blue
     fill(64,128,187,100);
     
     // User has nothing "selected"
     sel=0;
   }
   
   // Draw circle
   ellipse(e[j][0],e[j][1],radi,radi);
   
   // Move circle
   e[j][0]+=e[j][3];
   e[j][1]+=e[j][4];
           
   
   /* Wrap edges of canvas so circles leave the top
      and re-enter the bottom, etc... */
   if( e[j][0] < -diam      ){ e[j][0] = width+diam;  }
   if( e[j][0] > width+diam ){ e[j][0] = -diam;       }
   if( e[j][1] < 0-diam     ){ e[j][1] = height+diam; }
   if( e[j][1] > height+diam){ e[j][1] = -diam;       }
   
   // If current circle is selected...
   if(sel==1){
     
     // Set fill color of center dot to white..
     fill(255,255,255,255);
     
     // ..and set stroke color of line to green.
     stroke(#ffff00,200);
   } else {            
     
     // otherwise set center dot color to black..
     fill(0,0,0,255);
     
     // and set line color to turquoise.
     stroke(64,128,128,255);
   }
   
   // Loop through all circles
   for(int k=0;k< count;k++){
     
     // If the circles are close...
     if( dist(e[j][0],e[j][1],e[k][0],e[k][1]) < radi){
       
       // Stroke a line from current circle to adjacent circle
       line(e[j][0],e[j][1],e[k][0],e[k][1]);
     }
   }
   
   // Turn off stroke/border
   noStroke();      
   
   // Draw dot in center of circle
   rect(e[j][0]-ds,e[j][1]-ds,ds*2,ds*2);
 }
 drawText();
}

PFont myFont;
void drawText() {
 myFont = createFont("Calibri", 42);
 textFont(myFont);
 fill(176, 224, 230);
 text("Some text for a title.", 10, 50);
 text("2010 Kickoff", 75, 90);
 
 myFont = createFont("Calibri Italic", 22);
 textFont(myFont);
 fill(255);
 text("Some more text in a different area.", 250, 120);
}
Re: Header with Animated Background and Static Text.
Reply #1 - Dec 8th, 2009, 5:25am
 
please delete the double post. one is enough
Re: Header with Animated Background and Static Text.
Reply #2 - Dec 8th, 2009, 6:18am
 
Triple post, two to delete (by going inside them, you should see a Delete button). That's the forum acting up, I think.

The text drawing isn't slow...
What is slow is the double createFont() inside the drawText() call!
You should do:
Code:
PFont myFont1, myFont2;
void setup() {
size(850, 140); // Should always be the first instruction of setup()
// [...] other setup stuff
myFont1 = createFont("Calibri", 42);
myFont2 = createFont("Calibri Italic", 22);
}
void drawText() {
textFont(myFont1);
fill(176, 224, 230);
text("Some text for a title.", 10, 50);
text("2010 Kickoff", 75, 90);

textFont(myFont2);
fill(255);
text("Some more text in a different area.", 250, 120);
}

Beware of createFont() on the Web: if your users haven't Calibri installed in their system, they will see a default font. That's why Processing uses preferably bitmap fonts, faster to display and perfect if you use only some fixed sizes.
Re: Header with Animated Background and Static Text.
Reply #3 - Dec 8th, 2009, 8:48am
 
Thank you PhiLho and Cedric for your help! i now understand how it works. i'm just trying to learn Processing. It is really a good way for a Right-brained person to learn Left-brained logic. it's a good recipe! Thanks again! ps, the server was acting up this morning. i tried to delete the other one but it won't let me.
Page Index Toggle Pages: 1