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


Posted in Javascript onSeptember 13, 2020

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>

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

Javascript 相关文章推荐
JavaScript创建命名空间(namespace)的最简实现
Dec 11 Javascript
JavaScript 继承详解(三)
Jul 13 Javascript
JavaScript修改浏览器tab标题小技巧
Jan 06 Javascript
AngularJS监听路由的变化示例代码
Sep 23 Javascript
通过JS获取Request.QueryString()参数的值实现方法
Sep 27 Javascript
KnockoutJS 3.X API 第四章之数据控制流component绑定
Oct 10 Javascript
Vue组件之Tooltip的示例代码
Oct 18 Javascript
解决Vue 通过下表修改数组,页面不渲染的问题
Mar 08 Javascript
vue-cli脚手架引入图片的几种方法总结
Mar 13 Javascript
mint-ui在vue中的使用示例
Apr 05 Javascript
jQuery实现日历效果
Sep 11 jQuery
Vue.Draggable实现交换位置
Apr 07 Vue.js
原生JS实现九宫格抽奖
Sep 13 #Javascript
jQuery实现带进度条的轮播图
Sep 13 #jQuery
js+canvas实现画板功能
Sep 13 #Javascript
jQuery实现鼠标拖拽登录框移动效果
Sep 13 #jQuery
jQuery实现简单全选框
Sep 13 #jQuery
jQuery+ajax实现用户登录验证
Sep 13 #jQuery
js+audio实现音乐播放器
Sep 13 #Javascript
You might like
php打开远程文件的方法和风险及解决方法
2013/11/12 PHP
学习php设计模式 php实现建造者模式
2015/12/07 PHP
深入解析WordPress中加载模板的get_template_part函数
2016/01/11 PHP
javascript之学会吝啬 精简代码
2010/04/25 Javascript
mailto的使用技巧分享
2012/12/21 Javascript
HTML上传控件取消选择
2013/03/06 Javascript
javascript数组去重方法终极总结
2014/06/05 Javascript
jquery通过load获取文件的内容并跳到锚点的方法
2015/01/29 Javascript
JS显示下拉列表框内全部元素的方法
2015/03/31 Javascript
javascript数据结构之双链表插入排序实例详解
2015/11/25 Javascript
JavaScript几种数组去掉重复值的方法推荐
2016/04/12 Javascript
Bootstrap table分页问题汇总
2016/05/30 Javascript
window.close(); 关闭浏览器窗口js代码的总结介绍
2016/07/14 Javascript
Jquery-data的三种用法
2017/04/18 jQuery
使用Vue.js开发微信小程序开源框架mpvue解析
2018/03/20 Javascript
微信小程序实现图片上传功能
2018/05/28 Javascript
Vue中mintui的field实现blur和focus事件的方法
2018/08/25 Javascript
vue服务端渲染添加缓存的方法
2018/09/18 Javascript
使用form-create动态生成vue自定义组件和嵌套表单组件
2019/01/18 Javascript
jquery实现垂直无限轮播的方法分析
2019/07/16 jQuery
微信小程序单选框自定义赋值
2020/05/26 Javascript
vue v-model的用法解析
2020/10/19 Javascript
一文秒懂nodejs中的异步编程
2021/01/28 NodeJs
python中bisect模块用法实例
2014/09/25 Python
Python实现判断字符串中包含某个字符的判断函数示例
2018/01/08 Python
Python实现的多进程拷贝文件并显示百分比功能示例
2019/04/09 Python
Pandas操作CSV文件的读写实现方法
2019/11/13 Python
flask框架配置mysql数据库操作详解
2019/11/29 Python
python tkinter之顶层菜单、弹出菜单实例
2020/03/04 Python
澳大利亚儿童鞋在线:The Trybe
2019/07/16 全球购物
应届生护士求职信
2013/11/01 职场文书
工艺工程师工作职责
2013/11/23 职场文书
募捐倡议书
2014/04/14 职场文书
学生评语集锦
2015/01/04 职场文书
家庭贫困证明
2015/06/16 职场文书
《秋思》教学反思
2016/02/23 职场文书