How do I animate something in processing
in
Programming Questions
•
6 months ago
Hi,
I'd like to use some example code (see below) to ask the following question:
How do I make the vine grow, so that one leaf at a time appears every second?
How do I make the center line of the vine grow from the bottom of the screen to the top of the screen?
Example code:
void setup() {
size(100, 100);
smooth();
noStroke();
noLoop();
}
void draw() {
vine(33, 9, 16);
}
// Function vine has three variables:
// X-coordinate,
// Total number of leaves,
// Width of the leaf in pixels
void vine(int x, int numLeaves, int leafSize) {
stroke(255);
line(x, 0, x, height); // draw the vine that holds the leaves
noStroke();
int gap = height / numLeaves; // the gap between the leaves is the screen height divided by number of leaves.
int direction = 1; // so that the leaves can be placed on both sides?
for (int i = 0; i < numLeaves; i++) {
int r = int(random(gap));
leaf(x, gap*i + r, leafSize, direction);
direction = -direction; // if this is not active, there will only be leaves on one side.
}
}
// the actial leaf drawn. The leaves will be multiplied in the vine function.
void leaf(int x, int y, int size, int dir) {
pushMatrix();
translate(x, y); // move the position
scale(size); // scale to size
beginShape(); // draw the leaf
vertex(1.0*dir, -0.7);
bezierVertex(1.0*dir, -0.7, 0.4*dir, -1.0, 0.0, 0.0);
bezierVertex(0.0, 0.0, 1.0*dir, 0.4, 1.0*dir, -0.7);
endShape();
popMatrix();
}
1