var egg;
function setup() {
createCanvas(windowWidth,windowHeight);
egg=new Egg(width/2, height/2,height*0.4,height*0.2,height*0.1,0.3,10);
}
function draw() {
background(80);
egg.show();
if(!egg.run()){
egg=new Egg(width/2, height/2,height*0.4,height*0.2,height*0.1,0.3,10);
}
}
function Egg(x,y,s,s0,c,v,n){
var segs=[];
var numSegs=n;
var segRads=[];
var a1,a2;
var ttlMax=100;
var ttl=ttlMax;
var tteMax=200;
var tte=tteMax;
var offsetX=x;
a1=(numSegs-1)*TWO_PI/numSegs;
a2=0;
var s1=random(s*(1-v), s*(1+v));
var sOrigin=s1;
var s2=random(s*(1-v), s*(1+v));
segs.push(new BezLine(x,y,a1,a2,s0,s1,s2,c));
for(var i=1; i<numSegs; i++){
a1=(i-1)*TWO_PI/numSegs;
a2=i*TWO_PI/numSegs;
s1=s2;
s2=random(s*(1-v), s*(1+v));
if(i===numSegs-1){
s2=sOrigin;
}
segs.push(new BezLine(x,y,a1,a2,s0,s1,s2,c));
}
this.run=function(){
if(ttl>0){
ttl-=ttl/70;
}
if(tte>0){
tte--;
} else {
offsetX-=width/30;
}
return offsetX>-width/2;
};
this.show=function(){
push();
beginShape();
noStroke();
fill(255,(1-ttl/ttlMax)*255);
segs.forEach(function(seg,i){
seg.bezLine(i,1-ttl/ttlMax, offsetX-x);80
});
endShape(CLOSE);
fill(250,180,0,50+(1-ttl/ttlMax)*200);
ellipse(offsetX,y,s0*1.8);
stroke(255);
strokeWeight(s0/10);
noFill();
arc(offsetX,y,s0*1.5,s0*1.5,-3,-2);
stroke(250,150,0,50+(1-ttl/ttlMax)*200);
strokeWeight(s0/20);
arc(offsetX,y,s0*1.7,s0*1.7,-1,2);
pop();
};
}
function BezLine(x,y,a1,a2,r0,r1,r2,s){
var adjust=PI/2;
this.showLine=function(){
vertex(x+cos(a2+adjust)*r1, y+sin(a2+adjust)*r1);
};
this.bezLine=function(i,progress,offsetX){
var aa1=a1+adjust;
var aa2=a2+adjust;
var rad1=r0+(r1-r0)*progress;
var rad2=r0+(r2-r0)*progress;
push();
if(i===0){
vertex(x+offsetX+cos(aa1)*rad1, y+sin(aa1)*rad1);
}
bezierVertex(x+offsetX+cos(aa1)*rad1+cos(aa1+PI/2)*s, y+sin(aa1)*rad1+sin(aa1+PI/2)*s,
x+offsetX+cos(aa2)*rad2+cos(aa2-PI/2)*s, y+sin(aa2)*rad2+sin(aa2-PI/2)*s,
x+offsetX+cos(aa2)*rad2, y+sin(aa2)*rad2);
pop();
};
}
console