【烟花代码】,情人节,情侣生日礼物代码适用

【烟花代码】,情人节,情侣生日礼物代码适用

【烟花代码】,情人节,情侣生日礼物代码适用

老规矩,先上效果图
在这里插入图片描述
图片做了加速处理,放樱花的位置,速度完全由点击控制。

1初始化粒子系统

	var Particle = function(x, y, hue){
   
		this.x = x;
		this.y = y;
		this.coordLast = [
			{
   x: x, y: y},
			{
   x: x, y: y},
			{
   x: x, y: y}
		];
		this.angle = rand(0, 360);
		this.speed = rand(((self.partSpeed - self.partSpeedVariance) <= 0) ? 1 : self.partSpeed - self.partSpeedVariance, (self.partSpeed + self.partSpeedVariance));
		this.friction = 1 - self.partFriction/100;
		this.gravity = self.partGravity/2;
		this.hue = rand(hue-self.hueVariance, hue+self.hueVariance);
		this.brightness = rand(50, 80);
		this.alpha = rand(40,100)/100;
		this.decay = rand(10, 50)/1000;
		this.wind = (rand(0, self.partWind) - (self.partWind/2))/25;
		this.lineWidth = self.lineWidth;
	};

2粒子系统的更新

	Particle.prototype.update = function(index){
   
		var radians = this.angle * Math.PI / 180;
		var vx = Math.cos(radians) * this.speed;
		var vy = Math.sin(radians) * this.speed + this.gravity;
		this.speed *= this.friction;
						
		this.coordLast[2].x = this.coordLast[1].x;
		this.coordLast[2].y = this.coordLast[1].y;
		this.coordLast[1].x = this.coordLast[0].x;
		this.coordLast[1].y = this.coordLast[0].y;
		this.coordLast[0].x = this.x;
		this.coordLast[0].y = this.y;
		
		this.x += vx * self.dt;
		this.y += vy * self.dt;
		
		this.angle += this.wind;				
		this.alpha -= this.decay;
		
		if(!hitTest(0,0,self.cw,self.ch,this.x-this.radius, this.y-this.radius, this.radius*2, this.radius*2) || this.alpha < .05){
   					
			self.particles.splice(index, 1);	
		}			
	};

3粒子系统的画图

	Particle.prototype.draw = function(){
   
		var coordRand = (rand(1,3)-1);
		self.ctx.beginPath();								
		self.ctx.moveTo(Math.round(this.coordLast[coordRand].x), Math.round(this.coordLast[coordRand].y));
		self.ctx.lineTo(Math.round(this.x), Math.round(this.y));
		self.ctx.closePath();				
		self.ctx.strokeStyle = 'hsla('+this.hue+', 100%, '+this.brightness+'%, '+this.alpha+')';
		self.ctx.stroke();				
		
		if(self.flickerDensity > 0){
   
			var inverseDensity = 50 - self.flickerDensity;					
			if(rand(0, inverseDensity) === inverseDensity){
   
				self.ctx.beginPath();
				self.ctx.arc(Math.round(this.x), Math.round(this.y), rand(this.lineWidth,this.lineWidth+3)/2, 0, Math.PI*2, false)
				self.ctx.closePath();
				var randAlpha = rand(50,100)/100;
				self.ctx.fillStyle = 'hsla('+this.hue+', 100%, '+this.brightness+'%, '+randAlpha+')';
				self.ctx.fill();
			}	
		}
	};

4创建烟花例子

self.createParticles = function(x,y, hue){
   
	var countdown = self.partCount;
	while(countdown--){
   						
		self.particles.push(new Particle(x, y, hue));
	}
};

5绑定鼠标时间,mousedown,mouseup

	self.bindEvents = function(){
   
		$(window).on('resize', function(){
   			
			clearTimeout(self.timeout);
			self.timeout = setTimeout(function() {
   
				self.ctx.lineCap = 'round';
				self.ctx.lineJoin = 'round';
			}, 100);
		});
		
		$(self.canvas).on('mousedown', function(e){
   
			var randLaunch = rand(0, 5);
			self.mx = e.pageX - self.canvasContainer.offset().left;
			self.my = e.pageY - self.canvasContainer.offset().top;
			self.currentHue = rand(self.hueMin, self.hueMax);
			self.createFireworks(self.cw/2, self.ch, self.mx, self.my);	
			
			$(self.canvas).on('mousemove.fireworks', function(e){
   
				var randLaunch = rand(0, 5);
				self.mx = e.pageX - self.canvasContainer.offset().left;
				self.my = e.pageY - self.canvasContainer.offset().top;
				self.currentHue = rand(self.hueMin, self.hueMax);
				self.createFireworks(self.cw/2, self.ch, self.mx, self.my);									
			});	
			
		});
		
		$(self.canvas).on('mouseup', function(e){
   
			$(self.canvas).off('mousemove.fireworks');									
		});
					
	}

有想要完整源码的小伙伴可以加我,q:2316773638

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/2861.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长

相关推荐

  • linux 开启allow_url_fopen,如何开启allow_url_fopen函数[通俗易懂]

    linux 开启allow_url_fopen,如何开启allow_url_fopen函数[通俗易懂]有些程序比如dede和discuz,都会有需要打开这个函数,不打开这个函数的甚至无法安装!如何解决这个问题呢?这里给出打开这个函数的终极解决办法:1.首先确保你拥有服务器的操作权限,如果只是虚拟空间客户,那么你就联系主机商帮助操作吧;2.打开PHP的配置文件php.ini,如果你的主机是win2003该文件在C:\WINDOWS目录下,直接用记事本打开就可以,如果是apache那么就是在你的php…

    2022年7月16日
  • 面试官问我:一个 TCP 连接可以发多少个 HTTP 请求?我竟然回答不上来..

    面试官问我:一个 TCP 连接可以发多少个 HTTP 请求?我竟然回答不上来..

    2022年2月12日
  • vue框架中用于表单数据绑定的指令_jsp获取表单数据

    vue框架中用于表单数据绑定的指令_jsp获取表单数据v-modelv-model指定可以实现表单值与属性的双向绑定。即表单元素中更改了值会自动的更新属性中的值,属性中的值更新了会自动更新表单中的值绑定的属性和事件v-model在内部为不同的输入元

    2022年7月31日
  • linux移植ntpdate「建议收藏」

    linux移植ntpdate「建议收藏」今天想让板子在开机的时候自动去同步网络上的时间,网上查了一下,需要使用到ntpdate命令。但是我使用的文件系统(busybox制作的文件系统)没有该命令,所以移植了一下。由于移植ntpdate需要用到openssl的头文件和库,所以也移植了openssl。PC系统:UbuntuUbuntu12.04.4LTS1.创建工作目录1mkdirc

    2022年9月24日
  • Android 基于TCP的 Socket 编程实现(结合 okio)

    Android 基于TCP的 Socket 编程实现(结合 okio)两个进程如果要进行通讯最基本的一个前提就是能够唯一的标识一个进程,在本地进程通讯中我们可以使用PID来唯一标识一个进程,但PID只在本地是唯一的,网络中两个进程PID冲突几率很大,这时我们就需要通过其他手段来唯一标识网络中的进程了,我们知道IP层的ip地址可以唯一标示主机,而TCP层协议和端口号结合就可以唯一标示主机的一个进程了。

    2022年6月7日
  • Ubuntu安装MariaDB教程「建议收藏」

    Ubuntu安装MariaDB教程「建议收藏」一、环境服务器:Ubuntu16.04.1LTS(GUN/Linux4.4.0-91-genericx86_64)数据库版本:MariaDB10.3二、安装流程2.1进入MariaDB网站在https://downloads.mariadb.org/mariadb/repositories/#mirror=neusoft该地址中,可以查找对应系统的安装命令配置…

    2022年6月11日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号