快过年了,用五种不同的JS特效带你看烟花(包邮送元宇宙图书)

Posted 海拥✘

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了快过年了,用五种不同的JS特效带你看烟花(包邮送元宇宙图书)相关的知识,希望对你有一定的参考价值。

今天给大家带来几个好看的基于 html+CSS+JS 的烟花特效

雪花樱花浪漫贺卡 以及 圣诞树 我前段时间刚做过,感兴趣的也可以看看我前几期的博客:

雪花:https://haiyong.blog.csdn.net/article/details/105786233
樱花:https://haiyong.blog.csdn.net/article/details/122008942
圣诞树和贺卡:https://haiyong.blog.csdn.net/article/details/122024043

目录

🎇 五种不同的JS烟花特效

🍕 一、简单大气的烟花

演示地址:http://haiyong.site/fireworks1


HTML代码:

这里的HTML代码很简短

<div>
	<canvas id="canvas"></canvas>
</div>

CSS代码

css也只有这两段内容

body
  background:black;
  overflow:hidden;
  margin:0;

canvas
  background:#000;

JS代码

所有的源码都在这里了,复制粘贴即可

window.addEventListener("resize", resizeCanvas, false);
window.addEventListener("DOMContentLoaded", onLoad, false);
window.requestAnimationFrame = 
window.requestAnimationFrame       || 
window.webkitRequestAnimationFrame || 
window.mozRequestAnimationFrame    || 
window.oRequestAnimationFrame      || 
window.msRequestAnimationFrame     || 
function (callback) 
    window.setTimeout(callback, 1000/60);
;
 var canvas, ctx, w, h, particles = [], probability = 0.04,
     xPoint, yPoint;
 function onLoad() 
     canvas = document.getElementById("canvas");
     ctx = canvas.getContext("2d");
     resizeCanvas();     
     window.requestAnimationFrame(updateWorld);
  
 
 function resizeCanvas() 
     if (!!canvas) 
         w = canvas.width = window.innerWidth;
         h = canvas.height = window.innerHeight;
     
  
 
 function updateWorld() 
     update();
     paint();
     window.requestAnimationFrame(updateWorld);
  
 
 function update() 
     if (particles.length < 500 && Math.random() < probability) 
         createFirework();
     
     var alive = [];
     for (var i=0; i<particles.length; i++) 
         if (particles[i].move()) 
             alive.push(particles[i]);
         
     
     particles = alive;
  
 
 function paint() 
     ctx.globalCompositeOperation = 'source-over';
     ctx.fillStyle = "rgba(0,0,0,0.2)";
     ctx.fillRect(0, 0, w, h);
     ctx.globalCompositeOperation = 'lighter';
     for (var i=0; i<particles.length; i++) 
         particles[i].draw(ctx);
     
  
 
 function createFirework() 
     xPoint = Math.random()*(w-200)+100;
     yPoint = Math.random()*(h-200)+100;
     var nFire = Math.random()*50+100;
     var c = "rgb("+(~~(Math.random()*200+55))+","
          +(~~(Math.random()*200+55))+","+(~~(Math.random()*200+55))+")";
     for (var i=0; i<nFire; i++) 
         var particle = new Particle();
         particle.color = c;
         var vy = Math.sqrt(25-particle.vx*particle.vx);
         if (Math.abs(particle.vy) > vy) 
             particle.vy = particle.vy>0 ? vy: -vy;
         
         particles.push(particle);
     
  
 
 function Particle() 
     this.w = this.h = Math.random()*4+1;  
     this.x = xPoint-this.w/2;
     this.y = yPoint-this.h/2;     
     this.vx = (Math.random()-0.5)*10;
     this.vy = (Math.random()-0.5)*10;    
     this.alpha = Math.random()*.5+.5;     
     this.color;
  
 
 Particle.prototype = 
     gravity: 0.05,
     move: function () 
         this.x += this.vx;
         this.vy += this.gravity;
         this.y += this.vy;
         this.alpha -= 0.01;
         if (this.x <= -this.w || this.x >= screen.width ||
             this.y >= screen.height ||
             this.alpha <= 0) 
                 return false;
         
         return true;
     ,
     draw: function (c) 
         c.save();
         c.beginPath();         
         c.translate(this.x+this.w/2, this.y+this.h/2);
         c.arc(0, 0, this.w, 0, Math.PI*2);
         c.fillStyle = this.color;
         c.globalAlpha = this.alpha;         
         c.closePath();
         c.fill();
         c.restore();
     
  

🍜 二、在农村看到的烟花

演示地址:http://haiyong.site/fireworks2(需要使用电脑打开,没做响应式手机端打开一片黑,或者可以看看后面的烟花)

HTML代码:

这里的HTML代码还是一样的简短

<div id="jsi-fireworks-container" class="container"></div>

CSS代码

css也只有这三段内容

html, body
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
    overflow: hidden;
    background-color: #101010;

.container
    position: absolute;
    width: 500px;
    height: 500px;
    top: 50%;
    left: 50%;
    margin-top: -250px;
    margin-left: -250px;

canvas
    position: absolute;
    top: 0;
    left: 0;

JS代码

JS代码比较长,我这里放了一部分,需要完整源码的可以关注公众号【海拥】回复【烟花】

var RENDERER = 
	LEAF_INTERVAL_RANGE : min : 100, max : 200,
	FIREWORK_INTERVAL_RANGE : min : 20, max : 200,
	SKY_COLOR : 'hsla(210, 60%, %luminance%, 0.2)',
	STAR_COUNT : 100,
	
	init : function()
		this.setParameters();
		this.reconstructMethod();
		this.createTwigs();
		this.createStars();
		this.render();
	,
	setParameters : function()
		this.$container = $('#jsi-fireworks-container');
		this.width = this.$container.width();
		this.height = this.$container.height();
		this.distance = Math.sqrt(Math.pow(this.width / 2, 2) + Math.pow(this.height / 2, 2));
		this.contextFireworks = $('<canvas />').attr(width : this.width, height : this.height).appendTo(this.$container).get(0).getContext('2d');
		this.contextTwigs = $('<canvas />').attr(width : this.width, height : this.height).appendTo(this.$container).get(0).getContext('2d');
		
		this.twigs = [];
		this.leaves = [new LEAF(this.width, this.height, this)];
		this.stars = [];
		this.fireworks = [new FIREWORK(this.width, this.height, this)];
		
		this.leafInterval = this.getRandomValue(this.LEAF_INTERVAL_RANGE) | 0;
		this.maxFireworkInterval = this.getRandomValue(this.FIREWORK_INTERVAL_RANGE) | 0;
		this.fireworkInterval = this.maxFireworkInterval;
	,
	reconstructMethod : function()
		this.render = this.render.bind(this);
	,
	getRandomValue : function(range)
		return range.min + (range.max - range.min) * Math.random();
	,
	createTwigs : function()
		this.twigs.push(new TWIG(this.width, this.height, 0, 0, Math.PI * 3 / 4, 0));
		this.twigs.push(new TWIG(this.width, this.height, this.width, 0, -Math.PI * 3 / 4, Math.PI));
		this.twigs.push(new TWIG(this.width, this.height, 0, this.height, Math.PI / 4, Math.PI));
		this.twigs.push(new TWIG(this.width, this.height, this.width, this.height, -Math.PI / 4, 0));
	,
	createStars : function()
		for(var i = 0, length = this.STAR_COUNT; i < length; i++)
			this.stars.push(new STAR(this.width, this.height, this.contextTwigs, this));
		
	,
	render : function()
		requestAnimationFrame(this.render);
		
		var maxOpacity = 0,
			contextTwigs = this.contextTwigs,
			contextFireworks = this.contextFireworks;
		
		for(var i = this.fireworks.length - 1; i >= 0; i--)
			maxOpacity = Math.max(maxOpacity, this.fireworks[i].getOpacity());
		
		contextTwigs.clearRect(0, 0, this.width, this.height);
		contextFireworks.fillStyle = this.SKY_COLOR.replace('%luminance', 5 + maxOpacity * 15);
		contextFireworks.fillRect(0, 0, this.width, this.height);
		
		for(var i = this.fireworks.length - 1; i >= 0; i--)
			if(!this.fireworks[i].render(contextFireworks))
				this.fireworks.splice(i, 1);
			
		
		for(var i = this.stars.length - 1; i >= 0; i--)
			this.stars[i].render(contextTwigs);
		
		for(var i = this.twigs.length - 1; i >= 0; i--)
			this.twigs[i].render(contextTwigs);
		
		for(var i = this.leaves.length - 1; i >= 0包邮送 50 本OpenCVAI新书

包邮送 OpenCV4,深度学习,强化学习,AR,VR方向 50 本好书

包邮送书!前端开发值得看的书单

包邮送 8 本《数据结构与算法之美》

包邮送6本!一本机器学习入门书

包邮送书啦|《机器学习与深度学习算法基础》