xxxxxxxxxx
76
const canvasSize = 664;
let leafID = 0;
let colors = [
'#47251B',
'#633B31',
'#676B30',
'#8E3E01',
'#999808',
'#9A0000',
'#AB9A54',
'#D55C01',
'#D68F4F',
'#FE9900'
];
let leaves = [];
function setup() {
createCanvas(canvasSize, canvasSize*0.666);
background('#F7F7F7');
}
function draw() {
for( let i = 0; i < leaves.length; i++ ){
leaves[i].fall();
}
}
class Leaf {
constructor(x,y,leafID){
this.x = x;
this.y = y;
this.color = int(random(colors.length));
this.id = leafID;
this.speed = random( 1 , 5 );
this.size = random(10,30);
}
place(){
noStroke();
fill(colors[this.color]);
smooth();
circle( this.x, this.y, this.size );
}
fall(){
this.y = this.y + this.speed;
this.place();
if( this.y > height ){
this.destroy();
}
}
destroy(){
let found = leaves.find(element => element.id === this.id);
let destroyIndex = leaves.indexOf(found);
leaves.splice(destroyIndex, 1);
}
}
function mousePressed(){
leaves.push( new Leaf( mouseX, mouseY, leafID ) );
leaves[leaves.length-1].place();
leafID++;
}