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 › How to Draw with an Animation
Page Index Toggle Pages: 1
How to Draw with an Animation? (Read 444 times)
How to Draw with an Animation?
Oct 18th, 2006, 11:16am
 
Hi,
I'm trying to paint with an animated brush, such as a rotating box. I animate the box by calling background() in the beginning of my draw loop, which doesn't let me draw with the box. I would like a simple mousePressed() solution, which would let me save the current contents of the matrix to the background, without interrupting the animation. I'm relatively new to Processing. Can anyone suggest a strategy?

thanks

Re: How to Draw with an Animation?
Reply #1 - Oct 18th, 2006, 5:12pm
 
Is this what you mean:

Code:

ArrayList boxes;
void setup()
{
size(400,400);
rectMode(CENTER);
boxes = new ArrayList();
smooth();
}

void mousePressed()
{
boxes.add(new Box(mouseX,mouseY));
}

void draw()
{
background(255);
for(int i = 0; i < boxes.size(); i++){
Box box = (Box)boxes.get(i);
box.draw();
}
}

class Box
{
int x, y, w, h, direction;
float rotation;
Box(int x, int y)
{
this(x,y,20,20);
}
Box(int x, int y, int w, int h)
{
this.direction = (int)random(2)==0?-1:1;
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}

void draw()
{
rotation+=0.05*direction;
noFill();
stroke(0);
pushMatrix();
translate(x,y);
rotate(rotation);
rect(0,0,w,h);
popMatrix();
}
}


-seltar
Re: How to Draw with an Animation?
Reply #2 - Oct 18th, 2006, 5:23pm
 
Or something like this perhaps:

Code:

PImage bg;
Box box;
void setup()
{
size(400,400);
rectMode(CENTER);
smooth();
box = new Box(0,0);
background(255);
bg = new PImage(width,height);
fillImage(bg,color(255));
}

void mousePressed()
{
loadPixels();
bg.pixels = pixels;
bg.updatePixels();
}

void draw()
{
background(bg);
box.setPosition(mouseX,mouseY);
box.draw();
}

class Box
{
int x, y, w, h, direction;
float rotation;
Box(int x, int y)
{
this(x,y,20,20);
}
Box(int x, int y, int w, int h)
{
this.direction = (int)random(2)==0?-1:1;
setPosition(x,y);
this.w = w;
this.h = h;
}

void setPosition(int x, int y)
{
this.x = x;
this.y = y;
}
void draw()
{
rotation+=0.05*direction;
noFill();
stroke(0);
pushMatrix();
translate(x,y);
rotate(rotation);
rect(0,0,w,h);
popMatrix();
}
}

void fillImage(PImage img, color c)
{
for(int i = 0; i < img.pixels.length; i++)
img.pixels[i] = c;
img.updatePixels();
}


-seltar
Re: How to Draw with an Animation?
Reply #3 - Oct 20th, 2006, 3:59am
 
Actually, the second example was just what I'm looking for. thank you very much
Page Index Toggle Pages: 1