jQuery 动态粒子效果示例代码


Posted in jQuery onJuly 07, 2020

效果图

jQuery 动态粒子效果示例代码

1.js部分

var RENDERER = {
	PARTICLE_COUNT : 1000,
	PARTICLE_RADIUS : 1,
	MAX_ROTATION_ANGLE : Math.PI / 60,
	TRANSLATION_COUNT : 500,
	
	init : function(strategy){
		this.setParameters(strategy);
		this.createParticles();
		this.setupFigure();
		this.reconstructMethod();
		this.bindEvent();
		this.drawFigure();
	},
	setParameters : function(strategy){
		this.$window = $(window);
		
		this.$container = $('#jsi-particle-container');
		this.width = this.$container.width();
		this.height = this.$container.height();
		
		this.$canvas = $('<canvas />').attr({width : this.width, height : this.height}).appendTo(this.$container);
		this.context = this.$canvas.get(0).getContext('2d');
		
		this.center = {x : this.width / 2, y : this.height / 2};
		
		this.rotationX = this.MAX_ROTATION_ANGLE;
		this.rotationY = this.MAX_ROTATION_ANGLE;
		this.strategyIndex = 0;
		this.translationCount = 0;
		this.theta = 0;
		
		this.strategies = strategy.getStrategies();
		this.particles = [];
	},
	createParticles : function(){
		for(var i = 0; i < this.PARTICLE_COUNT; i ++){
			this.particles.push(new PARTICLE(this.center));
		}
	},
	reconstructMethod : function(){
		this.setupFigure = this.setupFigure.bind(this);
		this.drawFigure = this.drawFigure.bind(this);
		this.changeAngle = this.changeAngle.bind(this);
	},
	bindEvent : function(){
		this.$container.on('click', this.setupFigure);
		this.$container.on('mousemove', this.changeAngle);
	},
	changeAngle : function(event){
		var offset = this.$container.offset(),
			x = event.clientX - offset.left + this.$window.scrollLeft(),
			y = event.clientY - offset.top + this.$window.scrollTop();
		
		this.rotationX = (this.center.y - y) / this.center.y * this.MAX_ROTATION_ANGLE;
		this.rotationY = (this.center.x - x) / this.center.x * this.MAX_ROTATION_ANGLE;
	},
	setupFigure : function(){
		for(var i = 0, length = this.particles.length; i < length; i++){
			this.particles[i].setAxis(this.strategies[this.strategyIndex]());
		}
		if(++this.strategyIndex == this.strategies.length){
			this.strategyIndex = 0;
		}
		this.translationCount = 0;
	},
	drawFigure : function(){
		requestAnimationFrame(this.drawFigure);
		
		this.context.fillStyle = 'rgba(0, 0, 0, 0.2)';
		this.context.fillRect(0, 0, this.width, this.height);
		
		for(var i = 0, length = this.particles.length; i < length; i++){
			var axis = this.particles[i].getAxis2D(this.theta);
			
			this.context.beginPath();
			this.context.fillStyle = axis.color;
			this.context.arc(axis.x, axis.y, this.PARTICLE_RADIUS, 0, Math.PI * 2, false);
			this.context.fill();
		}
		this.theta++;
		this.theta %= 360;
		
		for(var i = 0, length = this.particles.length; i < length; i++){
			this.particles[i].rotateX(this.rotationX);
			this.particles[i].rotateY(this.rotationY);
		}
		this.translationCount++;
		this.translationCount %= this.TRANSLATION_COUNT;
		
		if(this.translationCount == 0){
			this.setupFigure();
		}
	}
};
var STRATEGY = {
	SCATTER_RADIUS :150,
	CONE_ASPECT_RATIO : 1.5,
	RING_COUNT : 5,
	
	getStrategies : function(){
		var strategies = [];
		
		for(var i in this){
			if(this[i] == arguments.callee || typeof this[i] != 'function'){
				continue;
			}
			strategies.push(this[i].bind(this));
		}
		return strategies;
	},
	createSphere : function(){
		var cosTheta = Math.random() * 2 - 1,
			sinTheta = Math.sqrt(1 - cosTheta * cosTheta),
			phi = Math.random() * 2 * Math.PI;
			
		return {
			x : this.SCATTER_RADIUS * sinTheta * Math.cos(phi),
			y : this.SCATTER_RADIUS * sinTheta * Math.sin(phi),
			z : this.SCATTER_RADIUS * cosTheta,
			hue : Math.round(phi / Math.PI * 30)
		};
	},
	createTorus : function(){
		var theta = Math.random() * Math.PI * 2,
			x = this.SCATTER_RADIUS + this.SCATTER_RADIUS / 6 * Math.cos(theta),
			y = this.SCATTER_RADIUS / 6 * Math.sin(theta),
			phi = Math.random() * Math.PI * 2;
		
		return {
			x : x * Math.cos(phi),
			y : y,
			z : x * Math.sin(phi),
			hue : Math.round(phi / Math.PI * 30)
		};
	},
	createCone : function(){
		var status = Math.random() > 1 / 3,
			x,
			y,
			phi = Math.random() * Math.PI * 2,
			rate = Math.tan(30 / 180 * Math.PI) / this.CONE_ASPECT_RATIO;
		
		if(status){
			y = this.SCATTER_RADIUS * (1 - Math.random() * 2);
			x = (this.SCATTER_RADIUS - y) * rate;
		}else{
			y = -this.SCATTER_RADIUS;
			x = this.SCATTER_RADIUS * 2 * rate * Math.random();
		}
		return {
			x : x * Math.cos(phi),
			y : y,
			z : x * Math.sin(phi),
			hue : Math.round(phi / Math.PI * 30)
		};
	},
	createVase : function(){
		var theta = Math.random() * Math.PI,
			x = Math.abs(this.SCATTER_RADIUS * Math.cos(theta) / 2) + this.SCATTER_RADIUS / 8,
			y = this.SCATTER_RADIUS * Math.cos(theta) * 1.2,
			phi = Math.random() * Math.PI * 2;
		
		return {
			x : x * Math.cos(phi),
			y : y,
			z : x * Math.sin(phi),
			hue : Math.round(phi / Math.PI * 30)
		};
	}
};
var PARTICLE = function(center){
	this.center = center;
	this.init();
};
PARTICLE.prototype = {
	SPRING : 0.01,
	FRICTION : 0.9,
	FOCUS_POSITION : 300,
	COLOR : 'hsl(%hue, 100%, 70%)',
	
	init : function(){
		this.x = 0;
		this.y = 0;
		this.z = 0;
		this.vx = 0;
		this.vy = 0;
		this.vz = 0;
		this.color;
	},
	setAxis : function(axis){
		this.translating = true;
		this.nextX = axis.x;
		this.nextY = axis.y;
		this.nextZ = axis.z;
		this.hue = axis.hue;
	},
	rotateX : function(angle){
		var sin = Math.sin(angle),
			cos = Math.cos(angle),
			nextY = this.nextY * cos - this.nextZ * sin,
			nextZ = this.nextZ * cos + this.nextY * sin,
			y = this.y * cos - this.z * sin,
			z = this.z * cos + this.y * sin;
			
		this.nextY = nextY;
		this.nextZ = nextZ;
		this.y = y;
		this.z = z;
	},
	rotateY : function(angle){
		var sin = Math.sin(angle),
			cos = Math.cos(angle),
			nextX = this.nextX * cos - this.nextZ * sin,
			nextZ = this.nextZ * cos + this.nextX * sin,
			x = this.x * cos - this.z * sin,
			z = this.z * cos + this.x * sin;
			
		this.nextX = nextX;
		this.nextZ = nextZ;
		this.x = x;
		this.z = z;
	},
	rotateZ : function(angle){
		var sin = Math.sin(angle),
			cos = Math.cos(angle),
			nextX = this.nextX * cos - this.nextY * sin,
			nextY = this.nextY * cos + this.nextX * sin,
			x = this.x * cos - this.y * sin,
			y = this.y * cos + this.x * sin;
			
		this.nextX = nextX;
		this.nextY = nextY;
		this.x = x;
		this.y = y;
	},
	getAxis3D : function(){
		this.vx += (this.nextX - this.x) * this.SPRING;
		this.vy += (this.nextY - this.y) * this.SPRING;
		this.vz += (this.nextZ - this.z) * this.SPRING;
		
		this.vx *= this.FRICTION;
		this.vy *= this.FRICTION;
		this.vz *= this.FRICTION;
		
		this.x += this.vx;
		this.y += this.vy;
		this.z += this.vz;
		
		return {x : this.x, y : this.y, z : this.z};
	},
	getAxis2D : function(theta){
		var axis = this.getAxis3D(),
			scale = this.FOCUS_POSITION / (this.FOCUS_POSITION + axis.z);
			
		return {x : this.center.x + axis.x * scale, y : this.center.y - axis.y * scale, color : this.COLOR.replace('%hue', this.hue + theta)};
	}
};
$(function(){
	RENDERER.init(STRATEGY);
});

2.css部分

html,body {
 width: 100%;
 height: 100%;
 margin: 0;
 padding: 0;
 overflow: hidden;
}
.container{
 width: 100%;
 height: 100%;
 margin: 0;
 padding: 0;
 background-color: #000000;
}

3.html部分

<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
<div id="jsi-particle-container" class="container"></div>

以上就是jQuery 动态粒子效果示例代码的详细内容,更多关于jQuery 动态粒子效果的资料请关注三水点靠木其它相关文章!

jQuery 相关文章推荐
基于jQuery实现文字打印动态效果
Apr 21 jQuery
解决jQuery ajax动态新增节点无法触发点击事件的问题
May 24 jQuery
jQuery实现的表格前端排序功能示例
Sep 18 jQuery
jQuery选择器之子元素过滤选择器
Sep 28 jQuery
jquery鼠标悬停导航下划线滑出效果
Sep 29 jQuery
前端html中jQuery实现对文本的搜索功能并把搜索相关内容显示出来
Nov 14 jQuery
jquery 通过ajax请求获取后台数据显示在表格上的方法
Aug 08 jQuery
JS/jQuery实现获取时间的方法及常用类完整示例
Mar 07 jQuery
jQuery事件blur()方法的使用实例讲解
Mar 30 jQuery
使用jquery-easyui的布局layout写后台管理页面的代码详解
Jun 19 jQuery
javascript/jquery实现点击触发事件的方法分析
Nov 11 jQuery
jQuery实现鼠标拖动图片功能
Mar 04 jQuery
jQuery实现简单飞机大战
Jul 05 #jQuery
jQuery实现简单日历效果
Jul 05 #jQuery
jQuery实现飞机大战小游戏
Jul 05 #jQuery
jquery实现上传图片功能
Jun 29 #jQuery
jQuery实时统计输入框字数及限制
Jun 24 #jQuery
jQuery实现移动端下拉展现新的内容回弹动画
Jun 24 #jQuery
如何解决jQuery 和其他JS库的冲突
Jun 22 #jQuery
You might like
使用PHPMyAdmin修复论坛数据库的图文方法
2012/01/09 PHP
深入解析PHP内存管理之谁动了我的内存
2013/06/20 PHP
给WordPress中的留言加上楼层号的PHP代码实例
2015/12/14 PHP
PHP中递归的实现实例详解
2017/11/14 PHP
自写的利用PDO对mysql数据库增删改查操作类
2018/02/19 PHP
php和redis实现秒杀活动的流程
2019/07/17 PHP
AngularJS向后端ASP.NET API控制器上传文件
2016/02/03 Javascript
深入分析javascript中的错误处理机制
2016/07/17 Javascript
JS搜狐面试题分析
2016/12/16 Javascript
深入理解vue路由的使用
2017/03/24 Javascript
Angular2 http jsonp的实例详解
2017/08/31 Javascript
浅谈Vue.js之初始化el以及数据的绑定说明
2019/11/14 Javascript
koa中间件核心(koa-compose)源码解读分析
2020/06/15 Javascript
详解js中的原型,原型对象,原型链
2020/07/16 Javascript
原生js生成图片验证码
2020/10/11 Javascript
vue登录页实现使用cookie记住7天密码功能的方法
2021/02/18 Vue.js
详细介绍Python语言中的按位运算符
2013/11/26 Python
Python中设置变量作为默认值时容易遇到的错误
2015/04/03 Python
最基础的Python的socket编程入门教程
2015/04/23 Python
Python中转换角度为弧度的radians()方法
2015/05/18 Python
python提取页面内url列表的方法
2015/05/25 Python
深入解析Python编程中super关键字的用法
2016/06/24 Python
python实现反转部分单向链表
2018/09/27 Python
python实现把两个二维array叠加成三维array示例
2019/11/29 Python
python+opencv实现车牌定位功能(实例代码)
2019/12/24 Python
python中有函数重载吗
2020/05/28 Python
美国受信赖的教育产品供应商:Nest Learning
2018/06/14 全球购物
迪奥官网:Dior.com
2018/12/04 全球购物
申请任职学生会干部自荐书范文
2014/02/13 职场文书
社区反邪教工作方案
2014/06/16 职场文书
公司租房协议书
2014/10/14 职场文书
单位租房协议范本
2014/12/03 职场文书
结婚老公保证书
2015/02/26 职场文书
工程项目合作意向书
2015/05/08 职场文书
七一活动主持词
2015/06/29 职场文书
SQL注入的实现以及防范示例详解
2021/06/02 MySQL