HTML5使用画布来实现光标后面的火焰效果。

HTML5使用画布来实现光标后面的火焰效果。
这个例子展示了在Javascript中使用HTML5的画布元素在屏幕上跳动的火焰,火焰将跟随光标移动。
这种效果的完整代码如下:为了在HTML文件保存代码并查看效果,火焰将跟随光标移动:

复制代码代码如下所示:
帆布火焰效果

身体{边缘:0;填充:0;}
#帆布keleyi COM {显示:块;}
在window.onload =函数(){
无功keleyi_canvas = document.getelementbyid(画布凯尔+宜COM );
VaR CTX = keleyi_canvas.getcontext(2D);
无功W = window.innerwidth,H = window.innerheight;
keleyi_canvas.width = W;
keleyi_canvas.height = H;
var粒子{ };
var鼠标{ };
我们现在创建一些粒子
无功particle_count = 100;
对于(var i = 0;i < particle_count;i++)
{
Particles.push(新粒子());
}
keleyi_canvas.addeventlistener('mousemove ',track_mouse,假);
功能track_mouse(E)
{
X = e.pagex鼠标;
Y = e.pagey鼠标;
}
函数粒子()
{
this.speed = { x:- 2.5 + Math.random()* 5,Y:- 15 + Math.random()* 10 };
位置=鼠标坐标
现在火焰跟随鼠标坐标。
如果(鼠标,鼠标,y)
{
this.location = { x:老鼠。X,Y Y }:鼠标;
}
其他的
{
this.location = { x:W / 2,Y H / 2 };
}
半径范围= 10-30
this.radius = 10 + Math.random()* 20;
生命范围= 20-30
this.life = 20 + Math.random()* 10;
this.remaining_life = this.life;
颜色
这math.round。r(Math.random)*(255);
这math.round。G =(Math.random)*(255);
This.b = Math.round (Math.random () *255);
}
函数绘图()
{
ctx.globalcompositeoperation =源;
ctx.fillstyle =黑;
ctx.fillrect(0, 0,W,H);
ctx.globalcompositeoperation =打火机;
对于(var i = 0;i < particles.length;i++)
{
var =粒子{ };
Ctx.beginPath();
p.opacity = math.round(p.remaining_life / p.life * 100)/ 100
VaR梯度= ctx.createradialgradient(p.location。X,Y,0,p.location。p.location。X、Y、p.radius p.location。);
Gradient.addColorStop(0,RGBA(+公关+
Gradient.addColorStop(0.5,RGBA(+公关+
Gradient.addColorStop(1,RGBA(+公关+
ctx.fillstyle =梯度;
Ctx.arc(p.location。X,Y,p.location。p.radius,数学。PI * 2,假);
Ctx.fill();

p.remaining_life--;
p.radius--;
p.location。x = p.speed X;
p.location。Y = p.speed。Y;
如果(p.remaining_life<0 | | p.radius<0)
{
粒子{新粒子();
}
}
}
setInterval(画,86);
}
免责声明:本网信息来自于互联网,目的在于传递更多信息,并不代表本网赞同其观点。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,并请自行核实相关内容。本站不承担此类作品侵权行为的直接责任及连带责任。如若本网有任何内容侵犯您的权益,请及时联系我们,本站将会在24小时内处理完毕。
相关文章
返回顶部