fractalkitty looking for math? fractalkitty.com

weekly creative coding

fall sun

#random#trig#vectors#classes#image#animation#nature#sky#plants#light#wcc

cloudsfallingsleavestreeCloudFallTree

let clouds;
let fallings;
let leaves;
let tree;
let t;
function preload(){
  leaves = [];
  leaves[0] = loadImage("/assets/Leaf1-cap.png")
  leaves[1] = loadImage("/assets/Leaf2.png")
  leaves[2] = loadImage("/assets/Leaf3.png")  
}
function setup(){
  createCanvas(500, 500);
  background(2, 27, 217);
  tree = []
  clouds = [];
  fallings = [];
  for(let i = 0;i<20;i++){
    clouds.push(new Cloud)
  }
  for(let i = 0;i<100;i++){
    fallings.push(new Fall)
  }
  for(let i = 0;i<2000;i++){
    tree.push(new Tree)
  }
}
function draw(){
  t = frameCount/80;
  translate(width/2,height/2);
  background(50,107,257);
  for(let i = 0;i<tree.length;i++){
    tree[i].display()
  }
  for(let i = 0;i<clouds.length;i++){
    clouds[i].display()
  }
  for(let i = 0;i<fallings.length;i++){
    fallings[i].display()
  }
  
}

class Cloud{
  constructor(){
    this.x = random(-width/2,width/2)
    this.y = random(-height/2,height/2);
    this.n = random( 300,400);
    this.speed = random(0.000001,0.00001)
    this.array = []
    for(let i = 0;i<this.n;i++){
      this.array[i]=createVector(this.x+randomGaussian(10,40),this.y+randomGaussian(10,40),random(10,50))
    }
    this.speed = random(-2,2)
  }
  display(){
    fill(255,255,255,2);
    noStroke()
    for(let i = 0;i<this.n;i++){
      circle(this.array[i].x,this.array[i].y,this.array[i].z);
      this.array[i].x += this.speed/10
      this.array[i].y += this.speed/100
      this.array[i].x += this.speed/10
      this.array[i].y += this.speed/100
    }
    if (abs(this.array.x)>width){
      this.x = random(-width,-width/2.1)
      this.y = random(-height/2,height/2);
      for(let i = 0;i<this.n;i++){
      this.array[i]=createVector(this.x+randomGaussian(10,40),this.y+randomGaussian(10,40),random(10,50))
    }
    }
  }
}

class Fall{
  constructor(){
    this.type = floor(random(0,2.999));
    this.angle = random(0,TWO_PI);
    this.r = random(0,width*2)
    this.step = random(0.001,0.01);
    this.step2 = random(0.2,1);
    this.time = 0
  }
  display(){
    push()
     translate( this.r*cos(this.angle)-this.r/3*sin(this.angle/5),
               this.r*sin(this.angle)-cos(this.angle*this.step2));
    rotate(t*this.step2*2)
    let size = min(50,abs(this.r/12))
    image(leaves[this.type],0,0,size,size)
    pop()
    this.angle+=this.step
    this.r+=this.step2
    if (this.r>width*2){
      this.r = 0.1
      this.time = 0
      this.angle = random(0,TWO_PI);
    }
  }
}

class Tree{
  constructor(){
    this.x = randomGaussian(0,10);
    this.y = randomGaussian(0,10);
    this.x1 = this.x
    this.y1 = this.y
    this.a = random(0.1,TWO_PI)
    this.s = random(2,4);
    this.type = floor(random(1,2.999));
    
  }
  display(){
    push()
    translate(this.x,this.y)
    rotate(this.a)
    image(leaves[this.type],0,0,this.s,this.s)
    pop()
    this.x = this.x1+20*sin(t/this.a)*cos(t/4)
    this.y = this.y1+20*cos(t/this.a)-10*cos(t/4)
  }
}