Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
              
            
!

CSS

              
                
              
            
!

JS

              
                

// algorithm of creating the flower adapted from Dancing Flame by aadebdeb,
// and we add the petal animation based on that

let offset1;
let offset2;
let scale;
let size = 0.0005;
let c1;
let c2;
let px, py, px1, px2,px3, px4, px5, px6;
let r;
let speed;
let rad;

let elements = [];
let fadeout


function setup() {
  canvas = createCanvas(windowWidth, windowHeight);
  background(128);  
  noStroke(); 
  //generate random offset value for vertexs of the flower
  offset1 = new createVector(random(10000), random(10000));
  offset2 = new createVector(random(10000), random(10000));
  
  //mousepressed function to change the colour
  mousePressed();

  
  scale=0.01;
  frameRate(10);

  fadeout=1;
  size=0.001;


  //generate random y positions for petals
  for (let i = 0; i <6; i++) {
    elements[i] = {
      y:random(250,windowHeight-250),
    };
  }
  
  // set the inital position of 6 petals
  px1=height/2;
  px2=height/2;
  px3=height/2;
  px4=height/2;
  px5=height/2;
  px6=height/2;
  
  rad=10;
 
}


function draw(){
  background(0);

//if the scale of the geometric pattern is bigger than 0.05, start getting smaller  
if (scale >= 0.05 ){
    scale-=size;


//if the flower is getting too small too much, than stop the shrink function
if (scale <= 0.01) {
    size=0;

  }


//manually call the petal function 6 times with different values of scale and position
  push();
  petal(px1,elements[0].y, random(0.005,0.01));
  pop();
  
  
  push();
  petal(px2, elements[1].y, random(0.008,0.01));
  pop();
  
  push();
  petal(px3,elements[2].y,random(0.005,0.008));
  pop();

  push();
  petal(px4, elements[3].y, random(0.005,0.01));
  pop();

  push();
  petal(px5, elements[4].y, random(0.005,0.01));
  pop();

  push();
  petal(px6, elements[5].y, random(0.005,0.01));
  pop();


// the number of layers on the flower is decreasing, which represent the flower start to wither away.
  rad+=1;

}


// call the flower function 
  push();
  translate(400,height/2); 
  flower();

  // if only one layer stays on the flower, wipe off the whole animation to black
  if(rad>=250){
  fill(0);
  ellipse(0,0,fadeout);
  }
  pop();
 

  fadeout+=5;

  //set random x-direction falling speed of petal
  px1+=random(6,15);
  px2+=random(5,8);
  px3+=random(4,10); 
  px4+=random(10,16);
  px5+=random(15,20);
  px6+=random(5,10);

  
  
  
 
  
  
   // if the petal falling near the bottom, return it to the middle of the sreen
  if(px1>windowWidth-100){     
        px1=400;   
         }
  
  if(px2>windowWidth-100){  
        px2=400; 
         }
  
    if(px3>windowWidth-100){   
        px3=random(300,400); 
         }

    if(px4>windowWidth-100){
        px4=random(300,400); 
         }


     if(px5>windowWidth-100){ 
        px5=random(300,400);
         }
      if(px6>windowWidth-100){
        px6=random(300,400); 
         }

  console.log(rad);

 

}


// petal funtion, x, y: x and y position of the flower, s: scale of the flower
function petal(x,y,s){
//use translate to move the petal
    translate(x,y);
//fill the same colour with the inner layer and the randomly generated c1/c2 value
    fill(map(0, 0, 250, red(c1), red(c2)),
         map(0, 0, 250, green(c1), green(c2)),
         map(0, 0, 250, blue(c1), blue(c2))
         );

  
  
    beginShape();

  // creating vertex in 360 degree directions
    for(var angle = 0; angle < 360; angle += 0.5){
      var radian = radians(angle); 
 //the original x and y position without offset
      var x = 20 * cos(radian);
      var y = 20 * sin(radian);

    //x and y position of the vertex changes due to the noise funtion which created some really nature but random value of the vertex
    // it zooms in and out in z-index according to the frame Count
      var nx = x + map(
        noise(
          x * s + offset1.x, 
          y * s + offset1.y, 
          frameCount * 0.015
        ), 
        
        0, 1, -200, 200);
      var ny = y + map(
        noise(x * s + offset2.x, 
              y * s + offset2.y, 
              frameCount * 0.015
             ), 0, 1, -200, 200);
     //draw the vertex
      vertex(nx, ny);

    }
    endShape(CLOSE);

  }
    
  



// flower function
function flower(){
  
// this for loop is for creating multi-layers of the flower, 
//and the number of layers is also changing with the time because the rad vairable is increasing
 for(var radius=250; radius>0; radius -= rad){

// fill the colour acoording to which laywer it is
    fill(map(radius, 0, 250, red(c1), red(c2)),
         map(radius, 0, 250, green(c1), green(c2)),
         map(radius, 0, 250, blue(c1), blue(c2)),
         );
// this for loop is for creating vertex in 360 degree directions, the angle is increasing by 0.5 so it seems that all the vertex is connected
    beginShape();
    for(var angle = 0; angle < 360; angle += 0.5){
      var radian = radians(angle); 
      //set the x and y position of the vertex
      var x = radius * cos(radian);
      var y = radius * sin(radian);
   //set nx, ny as the original position plus the offset generated randomly to create more natural but random value
      var nx = x + map(
        noise(
          x * scale + offset1.x, 
          y * scale + offset1.y, 
          frameCount * 0.015
        ),   
        0, 1, -200, 200);
      

      var ny = y + map(
        noise(x * scale + offset2.x, 
              y * scale + offset2.y, 
              frameCount * 0.015
             ), 0, 1, -200, 200);
    //draw these vertex 
      vertex(nx, ny);
    }
    endShape(CLOSE);
  }
  //the scale (how withering the flower is) is increasing.
    scale += size;
}


//mouse pressed to generate new colour value
function mousePressed(){
  c1 = color(noise(255), random(255), random(255));
  c2 = color(random(255), random(255), random(255));
}

              
            
!
999px

Console