html动画之制作烟花效果,JavaScript实现烟花绽放动画效果
先编写一个烟花绽放的动画效果。
放烟花时,一个烟花可分为两个阶段:(1)烟花上升到空中;(2)烟花炸开成碎片,炸开的碎片慢慢消散。
为此抽象出两个对象类:Firework和Particle。其中,Firework用于表示一个烟花对象,Particle用于表示一个烟花炸开后的各碎片。
Firework对象类定义6个属性:表示烟花上升轨迹中各点的坐标(x,y)、烟花弧状轨迹的偏转角度angle、上升阶段水平和垂直方向的位移改变量xSpeed和ySpeed、烟花的色彩色相hue。
坐标属性值y的初始值取画布的高度,表示烟花从地面上升到空中,其余各属性的初始值采用随机数确定。具体定义如下:
function Firework()
{
this.x = canvas.width/4*(1+3*Math.random());
this.y = canvas.height - 15;
this.angle = Math.random() * Math.PI / 4 - Math.PI / 6;
this.xSpeed = Math.sin(this.angle) *(6+Math.random()*7);
this.ySpeed = -Math.cos(this.angle) *(6+Math.random()*7);
this.hue = Math.floor(Math.random() * 360);
}
Firework对象类定义3个方法:绘制烟花上升轨迹的方法draw()、烟花上升时坐标改变方法update()和烟花炸开方法explode()。绘制烟花轨迹时,在各点(x,y)处绘制一个宽度为5、高度为15的填充小矩形表示一个轨迹点。烟花上升时,垂直方向速度ySpeed初始值为负的,每次上升时,ySpeed加上一个正值,表示上升在减速,当ySpeed的值大于0时,烟花上升到顶了(不能再上升),就炸开为70个碎片。具体方法的实现见后面的HTML文件内容。
Particle对象类定义8个属性:表示碎片散开轨迹中各点的坐标(x,y)、碎片弧状轨迹的偏转角度angle、散开时水平和垂直方向的位移改变量xSpeed和ySpeed、碎片的色彩色相hue、表示碎片小圆的半径size、碎片的亮度lightness。
function Particle(x,y,hue)
{
this.x = x;
this.y = y;
this.hue = hue;
this.lightness = 50;
this.size = 15 + Math.random() * 10;
this.angle = Math.random() * 2 * Math.PI;
this.xSpeed = Math.cos(this.angle) *(1+Math.random() * 6);
this.ySpeed = Math.sin(this.angle) *(1+Math.random() * 6);
}
Particle对象类定义2个方法:绘制碎片散开轨迹的方法draw()、碎片散开时坐标改变方法update()。碎片散开时逐渐变小(属性size值减量),当size值小于1时,从碎片数组中删除该碎片,表示碎片已消亡。
定义两个数组var fireworks=[];和var particles=[];分别存储烟花对象和炸开的碎片对象。
模拟动画的函数loop中,每隔一段时间(用count计数来实现)向fireworks数组中添加一个烟花对象,烟花对象上升到顶炸开后,从fireworks数组中删除该对象元素,然后向particles数组中添加70个碎片对象。
遍历两个数组的各对象,分别调用它们的draw()和update()方法。
编写的完整HTML文件内容如下。
烟花绽放
var canvas=document.getElementById('myCanvas');
ctx= canvas.getContext('2d');
var fireworks=[];
var particles=[];
var counter = 0;
function Firework()
{
this.x = canvas.width/4*(1+3*Math.random());
this.y = canvas.height - 15;
this.angle = Math.random() * Math.PI / 4 - Math.PI / 6;
this.xSpeed = Math.sin(this.angle) *(6+Math.random()*7);
this.ySpeed = -Math.cos(this.angle) *(6+Math.random()*7);
this.hue = Math.floor(Math.random() * 360);
}
Firework.prototype.draw= function()
{
ctx.save();
ctx.translate(this.x, this.y);
ctx.rotate(Math.atan2(this.ySpeed, this.xSpeed) + Math.PI / 2);
ctx.fillStyle =`hsl(${this.hue}, 100%, 50%)`;
ctx.fillRect(0, 0, 5, 15);
ctx.restore();
}
Firework.prototype.update= function()
{
this.x = this.x + this.xSpeed;
this.y = this.y + this.ySpeed;
this.ySpeed += 0.1;
}
Firework.prototype.explode= function()
{
for (var i = 0; i < 70; i++)
{
particles.push(new Particle(this.x, this.y, this.hue));
}
}
function Particle(x,y,hue)
{
this.x = x;
this.y = y;
this.hue = hue;
this.lightness = 50;
this.size = 15 + Math.random() * 10;
this.angle = Math.random() * 2 * Math.PI;
this.xSpeed = Math.cos(this.angle) *(1+Math.random() * 6);
this.ySpeed = Math.sin(this.angle) *(1+Math.random() * 6);
}
Particle.prototype.draw= function()
{
ctx.fillStyle = `hsl(${this.hue}, 100%, ${this.lightness}%)`;
ctx.beginPath();
ctx.arc(this.x, this.y, this.size, 0, 2 * Math.PI);
ctx.closePath();
ctx.fill();
}
Particle.prototype.update= function(index)
{
this.ySpeed += 0.05;
this.size = this.size*0.95;
this.x = this.x + this.xSpeed;
this.y = this.y + this.ySpeed;
if (this.size<1)
{
particles.splice(index,1);
}
}
function loop()
{
ctx.fillStyle = "rgba(0, 0, 0, 0.1)";
ctx.fillRect(0,0,canvas.width,canvas.height);
counter++;
if (counter==15)
{
fireworks.push(new Firework());
counter=0;
}
var i=fireworks.length;
while (i--)
{
fireworks[i].draw();
fireworks[i].update();
if (fireworks[i].ySpeed > 0)
{
fireworks[i].explode();
fireworks.splice(i, 1);
}
}
var i=particles.length;
while (i--)
{
particles[i].draw();
particles[i].update(i);
}
requestAnimationFrame(loop);
}
loop();
在浏览器中打开包含这段HTML代码的html文件,可以看到在浏览器窗口中呈现出如图所示的烟花绽放动画效果。
实现了烟花绽放的效果,我们还可以继续让一定区域内的绽放的烟花碎片拼成“Happy New Year”粒子文本。
编写如下的HTML代码。
迎新年烟花绽放
body { margin: 0; background: black; }
canvas { position: absolute; }
function Particle(x, y, hue)
{
this.x = x;
this.y = y;
this.hue = hue;
this.lightness = 50;
this.size = 15 + Math.random() * 10;
this.angle = Math.random() * 2 * Math.PI;
this.xSpeed = Math.cos(this.angle) * (1 + Math.random() * 6);
this.ySpeed = Math.sin(this.angle) * (1 + Math.random() * 6);
this.target = getTarget();
this.timer = 0;
}
Particle.prototype.draw= function()
{
ctx2.fillStyle =`hsl(${this.hue}, 100%, ${this.lightness}%)`;
ctx2.beginPath();
ctx2.arc(this.x, this.y, this.size, 0, 2 * Math.PI);
ctx2.closePath();
ctx2.fill();
}
Particle.prototype.update= function(idx)
{
if (this.target)
{
var dx = this.target.x - this.x;
var dy = this.target.y - this.y;
var dist = Math.sqrt(dx * dx + dy * dy);
var a = Math.atan2(dy, dx);
var tx = Math.cos(a) * 5;
var ty = Math.sin(a) * 5;
this.size = lerp(this.size, 1.5, 0.05);
if (dist < 5)
{
this.lightness = lerp(this.lightness, 100, 0.01);
this.xSpeed = this.ySpeed = 0;
this.x = lerp(this.x, this.target.x + fidelity / 2, 0.05);
this.y = lerp(this.y, this.target.y + fidelity / 2, 0.05);
this.timer += 1;
}
else if (dist < 10)
{
this.lightness = lerp(this.lightness, 100, 0.01);
this.xSpeed = lerp(this.xSpeed, tx, 0.1);
this.ySpeed = lerp(this.ySpeed, ty, 0.1);
this.timer += 1;
}
else
{
this.xSpeed = lerp(this.xSpeed, tx, 0.02);
this.ySpeed = lerp(this.ySpeed, ty, 0.02);
}
}
else
{
this.ySpeed += 0.05;
this.size = this.size*0.95;
if (this.size<1)
{
particles.splice(idx,1);
}
}
this.x = this.x + this.xSpeed;
this.y = this.y + this.ySpeed;
}
function Firework()
{
this.x = canvas2.width*(1+ 3*Math.random())/4;
this.y = canvas2.height - 15;
this.angle = Math.random() * Math.PI / 4 - Math.PI / 6;
this.xSpeed = Math.sin(this.angle) * (6 + Math.random() * 7);
this.ySpeed = -Math.cos(this.angle) * (6 + Math.random() * 7);
this.hue = Math.floor(Math.random() * 360);
}
Firework.prototype.draw= function()
{
ctx2.save();
ctx2.translate(this.x, this.y);
ctx2.rotate(Math.atan2(this.ySpeed, this.xSpeed) + Math.PI / 2);
ctx2.fillStyle = `hsl(${this.hue}, 100%, 50%)`;
ctx2.fillRect(0, 0, 5, 15);
ctx2.restore();
}
Firework.prototype.update= function()
{
this.x = this.x + this.xSpeed;
this.y = this.y + this.ySpeed;
this.ySpeed += 0.1;
}
Firework.prototype.explode= function()
{
for (var i = 0; i < 70; i++)
{
particles.push(new Particle(this.x, this.y, this.hue));
}
}
function lerp(a, b, t)
{
return Math.abs(b - a)> 0.1 ? a + t * (b - a) : b;
}
function getTarget()
{
if (targets.length > 0)
{
var idx = Math.floor(Math.random() * targets.length);
var { x, y } = targets[idx];
targets.splice(idx, 1);
x += canvas2.width / 2 - textWidth / 2;
y += canvas2.height / 2 - fontSize / 2;
return { x, y };
}
}
var canvas1=document.getElementById('myCanvas1');
ctx1= canvas1.getContext('2d');
var canvas2=document.getElementById('myCanvas2');
ctx2= canvas2.getContext('2d');
var canvas3=document.getElementById('myCanvas3');
ctx3= canvas3.getContext('2d');
var fontSize = 200;
var fireworks = [];
var particles = [];
var targets = [];
var fidelity = 3;
var counter = 0;
canvas2.width = canvas3.width = window.innerWidth;
canvas2.height = canvas3.height = window.innerHeight;
ctx1.fillStyle = '#000';
var text = 'Happy New Year';
var textWidth = 999999;
while (textWidth > window.innerWidth)
{
ctx1.font = `900 ${fontSize--}px Arial`;
textWidth = ctx1.measureText(text).width;
}
canvas1.width = textWidth;
canvas1.height = fontSize * 1.5;
ctx1.font = `900 ${fontSize}px Arial`;
ctx1.fillText(text, 0, fontSize);
var imgData = ctx1.getImageData(0, 0, canvas1.width, canvas1.height);
for (var i = 0, max = imgData.data.length; i < max; i += 4)
{
var alpha = imgData.data[i + 3];
var x = Math.floor(i / 4) % imgData.width;
var y = Math.floor(i / 4 / imgData.width);
if (alpha && x % fidelity === 0 && y % fidelity === 0)
{
targets.push({ x, y });
}
}
ctx3.fillStyle = '#FFF';
ctx3.shadowColor = '#FFF';
ctx3.shadowBlur = 25;
function loop()
{
ctx2.fillStyle = "rgba(0, 0, 0, .1)";
ctx2.fillRect(0, 0, canvas2.width, canvas2.height);
counter += 1;
if (counter==15)
{
fireworks.push(new Firework());
counter=0;
}
var i=fireworks.length;
while (i--)
{
fireworks[i].draw();
fireworks[i].update();
if (fireworks[i].ySpeed > 0)
{
fireworks[i].explode();
fireworks.splice(i, 1);
}
}
var i=particles.length;
while (i--)
{
particles[i].draw();
particles[i].update(i);
if (particles[i].timer >= 100 || particles[i].lightness >= 99)
{
ctx3.fillRect(particles[i].target.x, particles[i].target.y, fidelity + 1, fidelity + 1);
particles.splice(i, 1);
}
}
requestAnimationFrame(loop);
}
loop();
在浏览器中打开包含这段HTML代码的html文件,可以看到在浏览器窗口中呈现出如图所示的烟花绽放迎新年动画效果。图2中为了控制图片的大小,删除了大量的中间帧,因此和实际运行的效果有所不同。
以上就是JavaScript实现烟花绽放动画效果的详细内容,更多关于JavaScript动画效果的资料请关注脚本之家其它相关文章!
html动画之制作烟花效果,JavaScript实现烟花绽放动画效果相关推荐
- 庆元旦,放烟花(纯javascript版烟花效果)
转载自:随机博客http://www.cnblogs.com/random/archive/2009/01/01/1366394.html 2008年过去了,新的2009年到来了,2008太不平凡,发 ...
- HTML5制作斑马线表格,JavaScript实现的斑马线表格效果【隔行变色】
本文实例讲述了JavaScript实现的斑马线表格效果.分享给大家供大家参考,具体如下: 虽然现在有很多框架可以轻松的实现斑马线效果,而且兼容性也很不错,比如bootstrap,但是不可否认的是使用J ...
- html5 放大镜效果,JavaScript+HTML5 canvas实现放大镜效果完整示例
本文实例讲述了JavaScript+HTML5 canvas实现放大镜效果.分享给大家供大家参考,具体如下: 效果: www.jb51.net canvas放大镜 #copycanvas { bord ...
- html横向导航栏滑动效果,JavaScript实现滑动导航栏效果
本文实例为大家分享了js实现滑动导航栏效果的具体代码,供大家参考,具体内容如下 *{ margin: 0; padding: 0; } .navScroll{ position: relative; ...
- html自动弹出窗效果,JavaScript实现弹出窗口效果
本文实例为大家分享了JavaScript实现弹出窗口的具体代码,供大家参考,具体内容如下 思路 1.总体使用两个div,一个作为底层展示,一个做为弹出窗口: 2.两个窗口独立进行CSS设计,通过dis ...
- flash动画制作成品_「咻动画」flash动画在制作方面有哪些优势?
在flash小游戏渐渐被越来越多的线上游戏淹没之后,flash动画制作渐渐地也淡出了我们的生活,不少小伙伴会发出时代变了之类的感叹.其实flash动画的应用范围并不局限于flash小游戏,在网页设计以 ...
- flash制作文字笔顺_flash动画课件制作有什么优点
Flash已经逐渐成为交互性矢量技术的标准,是未来网页制作和网络课件制作的主流.Flash动画课件制作相比传统方式.PPT课件,在教学中发挥着重要的价值,本篇文章,拥有多年制作经验的艺虎动画,总结分享 ...
- 计算机应用基础说课方案,广东省“XX杯”说课大赛计算机应用基础类一等奖作品:PPT写字动画的制作教学设计方案.doc...
广东省"XX杯"说课大赛计算机应用基础类一等奖作品:PPT写字动画的制作教学设计方案.doc 文档编号:1054618 文档页数:5 上传时间: 2020-05-30 文档级别:精 ...
- 怎么用计算机自己做动画片,大师为你详解动画怎么制作
电脑现已成为我们工作.生活和娱乐必不可少的工具了,在使用电脑的过程中,可能会遇到动画怎么制作的问题,如果我们遇到了动画怎么制作的情况,该怎么处理怎么才能解决动画怎么制作带来的困扰呢,对于这样的问题其实 ...
最新文章
- make 操作技巧指南--gcc版本设置
- property、staticmethod、classmethod与__str__的用法
- vscode使用php调试
- 使用WebLogic共享库连续交付ADF应用程序
- 在eclipse中指定启动时java的位置
- *【SGU - 114】Telecasting station (带权中位数 或 三分)
- 温度 数值模拟 matlab,西安交通大学——温度场数值模拟(matlab)
- 将DHCP从win2000转移到2003上
- 《恋上数据结构第1季》单向链表、双向链表
- Jackson解析XML
- (六)ModelSim 下载安装以及crack的注册
- 无盘服务器 免费,免费无广告的网咖专用云无盘安装图文教程
- 梦幻西游易语言辅助教程
- win10系统的深度清理方法
- 烂在肚子里的救命知识!看看吧!
- 优化 | Pick and delivery problem的简介与建模实现(二)
- Android 项目必备(二十六)-->获取手机中所有 APP
- icpc 2020沈阳区域赛补题
- BOM操作(浏览器对象模型)
- autojs之自动答题
热门文章
- 【工具】linux中用top、ps命令查看进程中的线程
- 基于jsp的零食商城
- SQLIntegrityConstraintViolationException: Duplicate entry 'xxx' for key 'yyyzzz'异常解决
- Linux系统通过Shell脚本实现一个全方面的系统性能分析系统
- 为什么Eureka比ZooKeeper更适合做服务发现?
- [创业之路-71] :创业思维与打工思维的区别
- scrum站立会议学习
- AWE Asia 2020:影创“MR+行业”落地应用案例分享
- 局域网流量分析及工具
- python双色球选号_python实现双色球随机选号