canvas焰火特效

2023-05-26,,

之前在抖音上看到了一个很漂亮的焰火效果。这会儿有时间就用canvas实现了一下。

演示地址:http://suohb.com/work/firework4.htm

先看效果:(静态图片看不太出效果,请直接查看演示地址,考验电脑CPU的时候到了)

实现原理:

焰火特效运用到的物理知识就是抛物线运动

看起来很复杂的焰火实际上就是一条条的抛物线。

从一个中心点向四周方向抛出一个质点。将质点的轨迹画出来,就是焰火效果了。

//每一个质点对象
1 var obj = {
x: x,//当前X坐标
y: y,//当前Y坐标
sx: Math.cos(deg)*curSpeed,//X轴方向速度
sy: Math.sin(deg)*curSpeed,//Y轴方向速度
len: len + level*10*Math.random(),//焰火显示长度(这么多质点连接起来)
limit: limit + level*10*Math.random(),//质点移动最大步数
color: color,//焰火颜色
level: level,//焰火等级(因为特效是二级焰火,可以做多级)
list:[{x:x,y:y}]//质点轨迹(将这些轨迹连起来就是焰火的其中一条线)
};
//向360度方向生成一批质点,形成一个焰火元素
1 function addFire(x,y,color,level){
curLevel = level ;
var lineLen = 10+level*20 + Math.random()*10,
deg ,
speed = 1 + Math.random()*level*.4 ,
len = 15 + Math.random()*level*6,
limit = len + 4 + Math.random()*level;
for(var i = 0 ; i < lineLen ; i ++){
deg = i*(Math.PI*2/lineLen)+Math.random() ;
var curSpeed = speed + Math.random();
var obj = 质点对象
list.push(obj);
}
}
//更新质点位置,并将新位置插入质点轨迹之中
1 function reviewFire(){
for(var i = 0 ; i <list.length ; i++){
let obj = list[i];
obj.x += obj.sx ;
obj.y += obj.sy ;
obj.sy += G ;//抛物运动中的重力加速度
obj.list.push({x:obj.x,y:obj.y});
obj.list = obj.list.slice(-obj.len);
}
}
//画出轨迹即可
1 function drawFire(){
cxt.clearRect(0,0,pageWidth,pageHeight);
var obj ;
for(var i = 0 ; i < list.length ; i ++){
obj = list[i] ;
cxt.beginPath();
for(var j = 0 ; j < obj.list.length ; j++){
if(i == 0)
cxt.moveTo(obj.list[j].x ,obj.list[j].y);
else{
cxt.lineTo(obj.list[j].x ,obj.list[j].y);
}
}
cxt.strokeStyle = obj.color ;
cxt.lineWidth = obj.level ;
cxt.stroke();
}
}

完整代码:

 <!doctype html>
<html>
<head>
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Cache-Control" content="no-cache" />
<meta http-equiv="Expires" content="0" />
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=no" />
<title>焰火特效-长按二维码关注公众号,了解更多特效</title>
<style type="text/css">
html{
height: 100%;
}
html,body,ul,li,canvas{
margin: 0;
padding: 0;
} </style>
</head>
<body bgcolor="#000000">
<canvas id="knife"></canvas>
<img src="../images/qr.jpg" style="position:fixed;bottom:0;width:100px;height:100px;right:0;">
</body>
<script>
var canvas = document.getElementById("knife");
canvas.style.position = "absolute" ;
canvas.style.top = 0 ;
var pageWidth = window.innerWidth ;
var pageHeight = window.innerHeight ;
canvas.width = window.innerWidth ;
canvas.height = window.innerHeight ;
var cxt = canvas.getContext("2d");
var list = [] ;
var G = 0.036 ;
var colors = ["#8b008b","#ff69b4","#7fff00","#1e90ff","#00bfff","#0FF","#7cfc00","#ffd700","#ffdead","#f00"];
var curLevel = 0 ;
var curColor = 0 ; function addFire(x,y,color,level){
curLevel = level ;
var lineLen = 10+level*20 + Math.random()*10,
deg ,
speed = 1 + Math.random()*level*.4 ,
len = 15 + Math.random()*level*6,
limit = len + 4 + Math.random()*level;
for(var i = 0 ; i < lineLen ; i ++){
deg = i*(Math.PI*2/lineLen)+Math.random() ;
var curSpeed = speed + Math.random();
var obj = {
x: x,
y: y,
sx: Math.cos(deg)*curSpeed,
sy: Math.sin(deg)*curSpeed,
len: len + level*10*Math.random(),
limit: limit + level*10*Math.random(),
color: color,
level: level,
list:[{x:x,y:y}]
};
list.push(obj);
}
}
function reviewFire(){
for(var i = 0 ; i <list.length ; i++){
let obj = list[i];
obj.x += obj.sx ;
obj.y += obj.sy ;
obj.sy += G ;
obj.list.push({x:obj.x,y:obj.y});
obj.list = obj.list.slice(-obj.len);
}
}
function drawFire(){
cxt.clearRect(0,0,pageWidth,pageHeight);
var obj ;
for(var i = 0 ; i < list.length ; i ++){
obj = list[i] ;
cxt.beginPath();
for(var j = 0 ; j < obj.list.length ; j++){
if(i == 0)
cxt.moveTo(obj.list[j].x ,obj.list[j].y);
else{
cxt.lineTo(obj.list[j].x ,obj.list[j].y);
}
}
cxt.strokeStyle = obj.color ;
cxt.lineWidth = obj.level ;
cxt.stroke();
}
}
function step(){
if(curLevel == 1 && list.length == 0){
addFire(pageWidth/2,100,colors[curColor ++ % colors.length],2);
}
reviewFire();
drawFire();
requestAnimationFrame(step)
}
requestAnimationFrame(step)
addFire(pageWidth/2,100,colors[curColor ++ % colors.length],2);
</script>
</html>

了解更多特效,请关注我的微信公众号:

canvas焰火特效的相关教程结束。

《canvas焰火特效.doc》

下载本文的Word格式文档,以方便收藏与打印。