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

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

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

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

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


相关推荐

  • oracle number转为timestamp timestamp转number

    oracle number转为timestamp timestamp转number现在平台有个字段是用来记录插入时间的,但是是用number型存储,想转为时间类型的。结果:SELECTTO_CHAR(字段名/(1000*60*60*24)+TO_DATE(‘1970-01-0108:00:00′,’YYYY-MM-DDHH24:MI:SS’),’YYYY-MM-DDHH24:MI:SS’)ASCDATE FROM表名;解决

    2022年7月24日
    23
  • Pythonista中文文档[通俗易懂]

    Pythonista中文文档[通俗易懂]本文档为Pythonista3.3版本官方文档的汉化版,主要为帮助不是很擅长英文的朋友进行阅读学习。本文档主要针对Pythonista中iOS独有的模块进行汉化,第三方模块仅提及一下以便知晓目前Py

    2022年8月6日
    12
  • c语言code用法_visual code c语言

    c语言code用法_visual code c语言此文章出处:http://www.cppblog.com/izualzhy/archive/2012/07/09/182456.html先看个例子:汉字一的UTF-8编码为0xE40xb80x80我们在google里搜索一下“一”,地址栏显示为:可以看到url的字符串里有一个%E4%B8%80这就是encode之后的值。因此,encode的处理过程也很明显了。特别是传中文参数时,如果没…

    2022年9月28日
    2
  • 使用iframe框架后的页面,执行跳转命令,目标页面内嵌至iframe的子页面的解决方法

    使用iframe框架后的页面,执行跳转命令,目标页面内嵌至iframe的子页面的解决方法问题描述 nbsp nbsp nbsp nbsp 在做项目的过程中 需要完成修改密码后重新登录的功能 但是前端页面使用了 IFrame 的框架 修改页面内嵌在的 index html 中 重新登录的页面就内嵌到原来的页面中 问题如图所示 nbsp 修改密码成功后 nbsp 出现问题 修改密码页面跳转到的登录页面内置到了子页面中 nbsp 登录后出现了如下页面 nbsp 前端使用的是 AngularJs 后台使用的 sp

    2025年8月23日
    2
  • SpringBoot test

    SpringBoot test原文地址:https://www.jianshu.com/p/72b19e24a602前言mac上idea快捷键,command+shift+T根据类生成快捷键。对spring容器中的类做单元测试在src/main下建立UserService类,对其进行单于测试,生产其单元测试类(使用command+shift+T快捷键),生成的test类在src/test下@Servi…

    2022年4月28日
    49
  • SPSS案例分析3:因子分析

    SPSS案例分析3:因子分析http hi baidu com datasoldier item 6689c4a50ec2 因子分析在各行各业的应用非常广泛 尤其是科研论文中因子分析更是频频出现 小兵也凑个热闹 参考 SPSS 统计分析 书中的案例 运用 SPSS 进行因子分析 作为我博客 nbsp SPSS 案例分析系列 nbsp nbsp 的第三篇文章 一 概念 探讨具有相关关系的变量之间 是否存在不能直

    2025年8月15日
    3

发表回复

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

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