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

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

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

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

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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 解决主流浏览器不再支持Flash Player的一个简单可行的办法

    解决主流浏览器不再支持Flash Player的一个简单可行的办法解决主流浏览器不再支持FlashPlayer的一个简单可行的办法今天一个朋友因为打不开网页上的flash视频而发愁,本人便自告奋勇地要帮助他解决问题。自从2020年12月起,各大浏览器纷纷宣布不支持FlashPlayer,导致很多网站不能正常使用。网上的解决方案五花八门,安装插件的,借助脚本的,借助中间件的,实行起来非常困难。经过几个小时的尝试,终于找到了最快速可行的、没有技术门槛的方法:下载一个特殊的旧版国产浏览器。这个浏览器就是傲游浏览器(Max…

    2022年5月4日
    100
  • vagrant box镜像百度下载地址

    vagrant box镜像百度下载地址

    2022年2月8日
    38
  • 马哥学习—-李洋个人笔记–启动故障排除

    马哥学习—-李洋个人笔记–启动故障排除故障1删除/boot之后的恢复步骤:1重启电源,迅速按esc进去选择启动模式,然后选cd-rom这项(从光驱启动)2重启后进入救援模式(选择rescue),选择语言和键盘布局后,一路回车到下一步。3询问是否需要网络选项,一般来说,救援模式不需要网络,选择no,回车进入下一步。4这一步提示内容大意为:救援系统将尝试寻找你的linux安装,并在目录mnt/sysimage下安装它…

    2022年7月21日
    12
  • centos7设置go代理

    centos7设置go代理wgethttps://dl.google.com/go/go1.10.3.linux-amd64.tar.gztar-C/usr/local-zxvfgo1.10.3.linux-amd64.tar.gzvim/etc/profileexportGOROOT=/usr/local/goexportPATH=$PATH:$GOROOT/binexportGOPROXY=https://goproxy.cnsource/etc/profilegoversion..

    2022年7月26日
    12
  • 免费且超级好用的搜索引擎INSO[通俗易懂]

    免费且超级好用的搜索引擎INSO[通俗易懂]免费且超级好用的搜索引擎INSO已经上线啦,界面UI是采用FlatUI设计,能够搜索到很多很多资源,近期资源一般来说要等10天左右,否则基本上是枪版。后面我会推出开发这个搜索引擎的系列教程的,尽请期待!网址是http://www.inso.hk

    2022年7月18日
    25
  • mysql executenonquery_ExecuteNonQuery()返回值注意点

    mysql executenonquery_ExecuteNonQuery()返回值注意点查询某个表中是否有数据的时候,我用了ExecuteNonQuery(),并通过判断值是否大于0来判断数据的存在与否。结果与我所设想的很不一致,调试时才发现,其执行后返回的结果是-1,对此我很是不理解,回头查了下资料,如下显示:SqlCommand.ExecuteNonQuery方法对连接执行Transact-SQL语句并返回受影响的行数。备注:可以使用ExecuteNonQuery来…

    2022年9月12日
    0

发表回复

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

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