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