欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

js实现3D粒子酷炫动态旋转特效

程序员文章站 2022-06-22 14:33:04
js实现3d粒子酷炫动态旋转特效(效果比较酷炫,中途不仅有形态的变换,还有颜色的变化,希望大家能够喜欢)代码实现过程中的静态截图

js实现3d粒子酷炫动态旋转特效(效果比较酷炫,中途不仅有形态的变换,还有颜色的变化,希望大家能够喜欢)

代码实现过程中的静态截图

js实现3D粒子酷炫动态旋转特效

js实现3D粒子酷炫动态旋转特效

<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
 <head>
 <title> new document </title>
 <meta name="generator" content="editplus">
 <meta name="author" content="">
 <meta name="keywords" content="">
 <meta name="description" content="">
 <style>
 html,body{
 margin:0px;
 width:100%;
 height:100%;
 overflow:hidden;
 background:#000;
}

#canvas{
 position:absolute;
 width:100%;
 height:100%;
}

 </style>
 </head>

 <body>
 <canvas id="canvas"></canvas>
 <script>
 function project3d(x,y,z,vars){

 var p,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.cos(p-vars.yaw)*d;
 p=math.atan2(y,z);
 d=math.sqrt(y*y+z*z);
 y=math.sin(p-vars.pitch)*d;
 z=math.cos(p-vars.pitch)*d;
 var rx1=-1000;
 var ry1=1;
 var rx2=1000;
 var ry2=1;
 var rx3=0;
 var ry3=0;
 var rx4=x;
 var ry4=z;
 var uc=(ry4-ry3)*(rx2-rx1)-(rx4-rx3)*(ry2-ry1);
 var ua=((rx4-rx3)*(ry1-ry3)-(ry4-ry3)*(rx1-rx3))/uc;
 var ub=((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:vars.cx+(rx1+ua*(rx2-rx1))*vars.scale,
 y:vars.cy+y/z*vars.scale,
 d:(x*x+y*y+z*z)
 };
 }else{
 return { d:-1 };
 }
}


function elevation(x,y,z){

 var dist = math.sqrt(x*x+y*y+z*z);
 if(dist && z/dist>=-1 && z/dist <=1) return math.acos(z / dist);
 return 0.00000001;
}


function rgb(col){

 col += 0.000001;
 var r = parseint((0.5+math.sin(col)*0.5)*16);
 var g = parseint((0.5+math.cos(col)*0.5)*16);
 var b = parseint((0.5-math.sin(col)*0.5)*16);
 return "#"+r.tostring(16)+g.tostring(16)+b.tostring(16);
}


function interpolatecolors(rgb1,rgb2,degree){
 
 var w2=degree;
 var w1=1-w2;
 return [w1*rgb1[0]+w2*rgb2[0],w1*rgb1[1]+w2*rgb2[1],w1*rgb1[2]+w2*rgb2[2]];
}


function rgbarray(col){

 col += 0.000001;
 var r = parseint((0.5+math.sin(col)*0.5)*256);
 var g = parseint((0.5+math.cos(col)*0.5)*256);
 var b = parseint((0.5-math.sin(col)*0.5)*256);
 return [r, g, b];
}


function colorstring(arr){

 var r = parseint(arr[0]);
 var g = parseint(arr[1]);
 var b = parseint(arr[2]);
 return "#"+("0" + r.tostring(16) ).slice (-2)+("0" + g.tostring(16) ).slice (-2)+("0" + b.tostring(16) ).slice (-2);
}


function process(vars){


 if(vars.points.length<vars.initparticles) for(var i=0;i<5;++i) spawnparticle(vars);
 var p,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;
 
 var t;
 for(var i=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);
 }
 }
}

function drawfloor(vars){
 
 var x,y,z,d,point,a;
 for (var i = -25; i <= 25; i += 1) {
 for (var j = -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.fillstyle = "#82f";
 for (var i = -25; i <= 25; i += 1) {
 for (var j = -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);
 }
 }
 }
 } 
}

function sortfunction(a,b){
 return b.dist-a.dist;
}

function draw(vars){

 vars.ctx.globalalpha=.15;
 vars.ctx.fillstyle="#000";
 vars.ctx.fillrect(0, 0, canvas.width, canvas.height);
 
 drawfloor(vars);
 
 var point,x,y,z,a;
 for(var i=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);
}


function spawnparticle(vars){

 var p,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); 
}

function frame(vars) {

 if(vars === undefined){
 var vars={};
 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;
 vars.cx=vars.canvas.width/2;
 vars.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;
 vars.cx=vars.canvas.width/2;
 vars.cy=vars.canvas.height/2;
 vars.bounding=10;
 vars.scale=500;
 vars.floor=26.5;

 vars.points=[];
 vars.initparticles=2000;
 vars.initv=.01;
 vars.distributionradius=800;
 vars.vortexheight=25;
 }

 vars.frameno++;
 requestanimationframe(function() {
 frame(vars);
 });

 process(vars);
 draw(vars);
}
frame();

 </script>
 </body>
</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

相关标签: js 粒子 旋转