当前位置:知之问问>百科问答>怎么用代码打造粒子风暴

怎么用代码打造粒子风暴

2024-09-22 22:58:04 编辑:join 浏览量:584

怎么用代码打造粒子风暴

后缀字:html

如果窗口名称有问题就把【<title>粒子风暴</title>】这句话改成【<title>粒子风暴</来自title>】

源代码:

<!DOCTYPEhtml>

<html>

<head>

<metacharse360问答t="utf-8">

<title>粒子风暴</title>

<style>

html,body{

margin:0px;

width:100%;

height:100%;

overflow:hidden;

background:#000;

}

#canvas{

positio我刑军季鱼给沙娘蒸著突n:absolute;

width:100%;

height:100%;

}

</style>

</h今第再木核提激挥星错想ead>

<body>

<canvasid="canvas"></canvas>

<script>

functionproject3D(x,y,z,vars){

varp,d;

x-=vars.camX;

形村连告城双都y-=vars.camY-8;

z-=vars.camZ粉算演触城学致福怕们;

p=Math.atan2(x,z);

d=Math.sqrt(x*x+z*z);

x=Math.sin(p-vars.yaw)*d;

z=Math.c征快需满os(p-vars.yaw)*修包究d;

p=Math.atan2(y,z);

d=Math明同附茶触灯西掌参他.sqrt(y*y+z*执地轴既否括之么错资z);

y=Math.s商拿展切兴袁师棉被in(p-vars.pit讲宪ch)*d;

z=M宁收因所课十青府映什ath.cos(p-vars.pitch)*d;

varrx1=-1000;

varry1=1;

varrx2=10政宣能剧00;

varry2=1;

varrx3=0;

varry3=0;

务行问刘换varrx4=x;

varry4=z;

varuc=(ry4-ry3)散快笑袁石改响场*(rx2-rx1)-(r画深x4-rx3)*(ry2-ry1);

varua=((rx4-rx3)*(ry1-ry3)-(ry4-ry3)*(rx1-rx3))/uc;

varub=((rx2-rx1)*(ry1-ry3)-古督叶织某次波所继所(ry2-ry1)*(rx1-rx3))/uc;

if(!z)z=0.000000001;

if(ua>0&&ua<1&&ub>0&&ub<1){

return{

x:***.cx+(rx1+ua*(rx2-rx1))*vars.scale,

y:***.cy+y/z*vars.scale,

d:(x*x+y*y+z*z)

};

}else{

return{d:-1};

}

}

functionelevation(x,y,z){

vardist=Math.sqrt(x*x+y*y+z*z);

if(dist&&z/dist>=-1&&z/dist<=1)returnMath.acos(z/dist);

return0.00000001;

}

functionrgb(col){

col+=0.000001;

varr=parseInt((0.5+Math.sin(col)*0.5)*16);

varg=parseInt((0.5+Math.cos(col)*0.5)*16);

varb=parseInt((0.5-Math.sin(col)*0.5)*16);

return"#"+r.toString(16)+g.toString(16)+b.toString(16);

}

functioninterpolateColors(RGB1,RGB2,degree){

varw2=degree;

varw1=1-w2;

return[w1*RGB1[0]+w2*RGB2[0],w1*RGB1[1]+w2*RGB2[1],w1*RGB1[2]+w2*RGB2[2]];

}

functionrgbArray(col){

col+=0.000001;

varr=parseInt((0.5+Math.sin(col)*0.5)*256);

varg=parseInt((0.5+Math.cos(col)*0.5)*256);

varb=parseInt((0.5-Math.sin(col)*0.5)*256);

return[r,g,b];

}

functioncolorString(arr){

varr=parseInt(arr[0]);

varg=parseInt(arr[1]);

varb=parseInt(arr[2]);

return"#"+("0"+r.toString(16)).slice(-2)+("0"+g.toString(16)).slice(-2)+("0"+b.toString(16)).slice(-2);

}

functionprocess(vars){

if(vars.points.length<vars.initParticles)for(vari=0;i<5;++i)spawnParticle(vars);

varp,d,t;

p=Math.atan2(vars.camX,vars.camZ);

d=Math.sqrt(vars.camX*vars.camX+vars.camZ*vars.camZ);

d-=Math.sin(vars.frameNo/80)/25;

t=Math.cos(vars.frameNo/300)/165;

vars.camX=Math.sin(p+t)*d;

vars.camZ=Math.cos(p+t)*d;

vars.camY=-Math.sin(vars.frameNo/220)*15;

vars.yaw=Math.PI+p+t;

vars.pitch=elevation(vars.camX,vars.camZ,vars.camY)-Math.PI/2;

vart;

for(vari=0;i<vars.points.length;++i){

x=vars.points[i].x;

y=vars.points[i].y;

z=vars.points[i].z;

d=Math.sqrt(x*x+z*z)/1.0075;

t=.1/(1+d*d/5);

p=Math.atan2(x,z)+t;

vars.points[i].x=Math.sin(p)*d;

vars.points[i].z=Math.cos(p)*d;

vars.points[i].y+=vars.points[i].vy*t*((Math.sqrt(vars.distributionRadius)-d)*2);

if(vars.points[i].y>vars.vortexHeight/2||d<.25){

vars.points.splice(i,1);

spawnParticle(vars);

}

}

}

functiondrawFloor(vars){

varx,y,z,d,point,a;

for(vari=-25;i<=25;i+=1){

for(varj=-25;j<=25;j+=1){

x=i*2;

z=j*2;

y=vars.floor;

d=Math.sqrt(x*x+z*z);

point=project3D(x,y-d*d/85,z,vars);

if(point.d!=-1){

size=1+15000/(1+point.d);

a=0.15-Math.pow(d/50,4)*0.15;

if(a>0){

vars.ctx.fillStyle=colorString(interpolateColors(rgbArray(d/26-vars.frameNo/40),[0,128,32],.5+Math.sin(d/6-vars.frameNo/8)/2));

vars.ctx.globalAlpha=a;

vars.ctx.fillRect(point.x-size/2,point.y-size/2,size,size);

}

}

}

}

vars.ctx.fill;

for(vari=-25;i<=25;i+=1){

for(varj=-25;j<=25;j+=1){

x=i*2;

z=j*2;

y=-vars.floor;

d=Math.sqrt(x*x+z*z);

point=project3D(x,y+d*d/85,z,vars);

if(point.d!=-1){

size=1+15000/(1+point.d);

a=0.15-Math.pow(d/50,4)*0.15;

if(a>0){

vars.ctx.fillStyle=colorString(interpolateColors(rgbArray(-d/26-vars.frameNo/40),[32,0,128],.5+Math.sin(-d/6-vars.frameNo/8)/2));

vars.ctx.globalAlpha=a;

vars.ctx.fillRect(point.x-size/2,point.y-size/2,size,size);

}

}

}

}

}

functionsortFunction(a,b){

returnb.dist-a.dist;

}

functiondraw(vars){

vars.ctx.globalAlpha=.15;

vars.ctx.fill;

vars.ctx.fillRect(0,0,canvas.width,canvas.height);

drawFloor(vars);

varpoint,x,y,z,a;

for(vari=0;i<vars.points.length;++i){

x=vars.points[i].x;

y=vars.points[i].y;

z=vars.points[i].z;

point=project3D(x,y,z,vars);

if(point.d!=-1){

vars.points[i].dist=point.d;

size=1+vars.points[i].radius/(1+point.d);

d=Math.abs(vars.points[i].y);

a=.8-Math.pow(d/(vars.vortexHeight/2),1000)*.8;

vars.ctx.globalAlpha=a>=0&&a<=1?a:0;

vars.ctx.fillStyle=rgb(vars.points[i].color);

if(point.x>-1&&point.x<vars.canvas.width&&point.y>-1&&point.y<vars.canvas.height)vars.ctx.fillRect(point.x-size/2,point.y-size/2,size,size);

}

}

vars.points.sort(sortFunction);

}

functionspawnParticle(vars){

varp,ls;

pt={};

p=Math.PI*2*Math.random();

ls=Math.sqrt(Math.random()*vars.distributionRadius);

pt.x=Math.sin(p)*ls;

pt.y=-vars.vortexHeight/2;

pt.vy=vars.initV/20+Math.random()*vars.initV;

pt.z=Math.cos(p)*ls;

pt.radius=200+800*Math.random();

pt.color=pt.radius/1000+vars.frameNo/250;

vars.points.push(pt);

}

functionframe(vars){

if(vars===undefined){

varvars={};

vars.canvas=document.querySelector("canvas");

vars.ctx=vars.canvas.getContext("2d");

vars.canvas.width=document.body.clientWidth;

vars.canvas.height=document.body.clientHeight;

window.addEventListener("resize",function(){

vars.canvas.width=document.body.clientWidth;

vars.canvas.height=document.body.clientHeight;

***.cx=vars.canvas.width/2;

***.cy=vars.canvas.height/2;

},true);

vars.frameNo=0;

vars.camX=0;

vars.camY=0;

vars.camZ=-14;

vars.pitch=elevation(vars.camX,vars.camZ,vars.camY)-Math.PI/2;

vars.yaw=0;

***.cx=vars.canvas.width/2;

***.cy=vars.canvas.height/2;

vars.bounding=10;

vars.scale=500;

vars.floor=26.5;

vars.points=[];

vars.initParticles=700;

vars.initV=.01;

vars.distributionRadius=800;

vars.vortexHeight=25;

}

vars.frameNo++;

requestAnimationFrame(function(){

frame(vars);

});

process(vars);

draw(vars);

}

frame();

</script>

</body>

</html>

标签:风暴,粒子,打造

版权声明:文章由 知之问问 整理收集,来源于互联网或者用户投稿,如有侵权,请联系我们,我们会立即处理。如转载请保留本文链接:https://www.zhzhwenwen.com/answer/345984.html
热门文章