# Animation simple code

**URL:** <https://discourse.processing.org/t/animation-simple-code/27988>\
**Category:** Gallery\
**Created:** [February 22, 2021, 4:12am UTC](https://discourse.processing.org/t/animation-simple-code/27988 "2021-02-22T04:12:54Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [February 22, 2021, 4:12am UTC](https://discourse.processing.org/t/animation-simple-code/27988/1 "2021-02-22T04:12:54Z")

</div>

nice animation with si

```auto
// https://processing.org/examples/tree.html
// Recursive Tree by Daniel Shiffman. 
// kll add:
// make dynamic width, and color brown to green
// add mouseY move sun and color sun and background

float first = 80, strk = 20, ang =45, lvl, shrink = 0.766; // kll TEST stroke width to make tree more natural
float theta;   

void setup() {
  size(640, 360);
}

void draw() {
  float s = mouseY/(float) height;
  background(100*s, 100*s, 100+100*s);
  noStroke();
  fill(200,200*s,0); // yellow sun
  ellipse(width-60,200-100*s,60,60);
  fill(0, 100, 0); // dark green grass
  rect(0, height*0.85, width, height*(1-0.85));
  lvl = 1; // kll init branch level
  float a = (mouseX / (float) width) * ang; // Let's pick an angle [ang] degrees based on the mouse position
  theta = radians(a); // Convert it to radians
  translate(width/2, height); // Start the tree from the bottom of the screen
  stroke(80, 40*lvl, 80); // kll brown to green 
  strokeWeight(strk); // kll
  line(0, -20, 0, -first); // kll shorter, grow on the green // Draw a line 120 pixels
  translate(0, -first); // Move to the end of that line
  branch(first, strk,lvl); // Start the recursive branching!
}

void branch(float h, float strk,float lvl) { // kll added stroke width as parameter
  h *= shrink; // Each branch will be [shrink] the size of the previous one
  strk *= shrink; // kll
  stroke(80, 80*lvl, 80); // kll brown to green 
  strokeWeight(strk); // kll
  lvl *= 0.85/shrink;
  // All recursive functions must have an exit condition!!!!
  if (h > 2) { // Here, ours is when the length of the branch is 2 pixels or less
    push(); // Save the current state of transformation (i.e. where are we now)
    rotate(theta); // Rotate by theta
    line(0, 0, 0, -h); // Draw the branch
    translate(0, -h); // Move to the end of the branch
    branch(h, strk,lvl); // kll // Ok, now call myself to draw two new branches!!
    pop(); // Whenever we get back here, we "pop" in order to restore the previous matrix state
    // Repeat the same thing, only branch off to the "left" this time!
    push();
    rotate(-theta);
    line(0, 0, 0, -h);
    translate(0, -h);
    branch(h, strk,lvl); // kll
    pop();
  }
}

```

[http://kll.engineering-news.org/kllfusion01/articles.php?article\_id=154#here22](http://kll.engineering-news.org/kllfusion01/articles.php?article_id=154#here22)
