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事件_动力节点Java学院整理
Jul 05 jQuery
原生js jquery ajax请求以及jsonp的调用方法
Aug 04 jQuery
jquery动态赋值id与动态取id方法示例
Aug 21 jQuery
jquery在vue脚手架中的使用方式示例
Aug 29 jQuery
jquery之基本选择器practice(实例讲解)
Sep 30 jQuery
利用JQUERY实现多个AJAX请求等待的实例
Dec 14 jQuery
jQuery实现鼠标移到某个对象时弹出显示层功能
Aug 23 jQuery
javascript异步处理与Jquery deferred对象用法总结
Jun 04 jQuery
jquery获取并修改触发事件的DOM元素示例【基于target 属性】
Oct 10 jQuery
使用jQuery实现掷骰子游戏
Oct 24 jQuery
jQuery实现的移动端图片缩放功能组件示例
May 01 jQuery
jQuery实现带进度条的轮播图
Sep 13 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
回答PHPCHINA上的几个问题:URL映射
2007/02/14 PHP
php引用地址改变变量值的问题
2012/03/23 PHP
PHP常见字符串操作函数与用法总结
2019/03/04 PHP
PHP快速排序算法实现的原理及代码详解
2019/04/03 PHP
使用laravel指定日志文件记录任意日志
2019/10/17 PHP
javascript获得CheckBoxList选中的数量
2009/10/27 Javascript
关于jQuery UI 使用心得及技巧
2012/10/10 Javascript
javascript使用定时函数实现跳转到某个页面
2013/12/25 Javascript
$(document).ready(function() {})不执行初始化脚本
2014/06/19 Javascript
js限制checkbox选中个数以限制六个为例
2014/07/15 Javascript
jQuery UI库中dialog对话框功能使用全解析
2016/04/23 Javascript
jQuery获取及设置表单input各种类型值的方法小结
2016/05/24 Javascript
Js得到radiobuttonlist选中值的两种方法(推荐)
2016/08/25 Javascript
基于jQuery ztree实现表格风格的树状结构
2018/08/31 jQuery
JavaScript链式调用实例浅析
2018/12/19 Javascript
PHP实现基于Redis的MessageQueue队列封装操作示例
2019/02/02 Javascript
javascript实现动态时钟的启动和停止
2020/07/29 Javascript
vue实现图片按比例缩放问题操作
2020/08/11 Javascript
解决iView Table组件宽度只变大不变小的问题
2020/11/13 Javascript
[37:21]完美世界DOTA2联赛PWL S2 Inki vs Magma 第二场 11.22
2020/11/24 DOTA
二种python发送邮件实例讲解(python发邮件附件可以使用email模块实现)
2013/12/03 Python
Python实现自动为照片添加日期并分类的方法
2017/09/30 Python
Python实现判断并移除列表指定位置元素的方法
2018/04/13 Python
pandas DataFrame创建方法的方式
2019/08/02 Python
详解appium自动化测试工具(monitor、uiautomatorviewer)
2021/01/27 Python
万得城电器土耳其网站:欧洲第一大电子产品零售商
2016/10/07 全球购物
大女孩胸罩:Big Girls Bras
2016/12/15 全球购物
Lowe’s加拿大:家居装修、翻新和五金店
2019/12/06 全球购物
影视艺术学院毕业生自荐信
2013/11/13 职场文书
高等教育学自荐书范文
2014/02/10 职场文书
道德大讲堂实施方案
2014/05/14 职场文书
党的群众路线教育实践活动总结报告
2014/07/03 职场文书
银行催款通知书
2015/04/17 职场文书
春季运动会加油词
2015/07/18 职场文书
2016年习主席讲话学习心得体会
2016/01/20 职场文书
六年级情感作文之500字
2019/10/23 职场文书