js烟花特效
<!DOCTYPE html>
<html>
<head>
<style>
body{background:#000;margin:0;}
canvas{cursor:crosshair;display:block;}
</style>
</head>
<body>
<canvas id="canvas">Canvas is not supported in your browser.</canvas>
<script>
window.requestAnimFrame=(function(){
return window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||function(callback){
window.setTimeout(callback,1000/60)
}
})();
var canvas=document.getElementById("canvas"),ctx=canvas.getContext("2d"),cw=window.innerWidth,ch=window.innerHeight,fireworks=[],particles=[],hue=120,limiterTotal=5,limiterTick=0,timerTotal=80,timerTick=0,mousedown=false,mx,my;canvas.width=cw;canvas.height=ch;
function random(min,max){return Math.random()*(max-min)+min}
function calculateDistance(p1x,p1y,p2x,p2y){
var xDistance=p1x-p2x,yDistance=p1y-p2y;
return Math.sqrt(Math.pow(xDistance,2)+Math.pow(yDistance,2))
}
function Firework(sx,sy,tx,ty){
this.x=sx;
this.y=sy;
this.sx=sx;
this.sy=sy;
this.tx=tx;
this.ty=ty;
this.distanceToTarget=calculateDistance(sx,sy,tx,ty);
this.distanceTraveled=0;
this.coordinates=[];
this.coordinateCount=3;
while(this.coordinateCount--)
{
this.coordinates.push([this.x,this.y])
}
this.angle=Math.atan2(ty-sy,tx-sx);
this.speed=2;
this.acceleration=1.05;
this.brightness=random(50,70);
this.targetRadius=1
}
Firework.prototype.update=function(index){
this.coordinates.pop();
this.coordinates.unshift([this.x,this.y]);
if(this.targetRadius<8){
this.targetRadius+=0.3
}
else{
this.targetRadius=1
}
this.speed*=this.acceleration;
var vx=Math.cos(this.angle)*this.speed,vy=Math.sin(this.angle)*this.speed;
this.distanceTraveled=calculateDistance(this.sx,this.sy,this.x+vx,this.y+vy);
if(this.distanceTraveled>=this.distanceToTarget){
createParticles(this.tx,this.ty);
fireworks.splice(index,1)
}else{
this.x+=vx;this.y+=vy
}
};
Firework.prototype.draw=function(){
ctx.beginPath();
ctx.moveTo(this.coordinates[this.coordinates.length-1][0],this.coordinates[this.coordinates.length-1][1]);
ctx.lineTo(this.x,this.y);
ctx.strokeStyle="hsl("+hue+", 100%, "+this.brightness+"%)";
ctx.stroke();
ctx.beginPath();
ctx.arc(this.tx,this.ty,this.targetRadius,0,Math.PI*2);
ctx.stroke()
};
function Particle(x,y){
this.x=x;this.y=y;
this.coordinates=[];
this.coordinateCount=5;
while(this.coordinateCount--){
this.coordinates.push([this.x,this.y])
}
this.angle=random(0,Math.PI*2);
this.speed=random(1,10);
this.friction=0.95;
this.gravity=1;
this.hue=random(hue-20,hue+20);
this.brightness=random(50,80);
this.alpha=1;
this.decay=random(0.015,0.03)
}
Particle.prototype.update=function(index){
this.coordinates.pop();
this.coordinates.unshift([this.x,this.y]);
this.speed*=this.friction;
this.x+=Math.cos(this.angle)*this.speed;this.y+=Math.sin(this.angle)*this.speed+this.gravity;this.alpha-=this.decay;
if(this.alpha<=this.decay){
particles.splice(index,1)
}
};
Particle.prototype.draw=function(){
ctx.beginPath();
ctx.moveTo(this.coordinates[this.coordinates.length-1][0],this.coordinates[this.coordinates.length-1][1]);
ctx.lineTo(this.x,this.y);
ctx.strokeStyle="hsla("+this.hue+", 100%, "+this.brightness+"%, "+this.alpha+")";
ctx.stroke()
};
function createParticles(x,y){
var particleCount=30;
while(particleCount--){
particles.push(new Particle(x,y))
}
}
function loop(){
requestAnimFrame(loop);
hue+=0.5;
ctx.globalCompositeOperation="destination-out";
ctx.fillStyle="rgba(0, 0, 0, 0.5)";
ctx.fillRect(0,0,cw,ch);
ctx.globalCompositeOperation="lighter";
var i=fireworks.length;
while(i--){
fireworks[i].draw();fireworks[i].update(i)
}
var i=particles.length;
while(i--){
particles[i].draw();particles[i].update(i)
}
if(timerTick>=timerTotal){
if(!mousedown){
fireworks.push(new Firework(cw/2,ch,random(0,cw),random(0,ch/2)));
timerTick=0
}
}
else{ timerTick++}
if(limiterTick>=limiterTotal){
if(mousedown){
fireworks.push(new Firework(cw/2,ch,mx,my));
limiterTick=0
}
}
else{ limiterTick++}
}
canvas.addEventListener("mousemove",function(e){
mx=e.pageX-canvas.offsetLeft;
my=e.pageY-canvas.offsetTop
});
canvas.addEventListener("mousedown",function(e){
e.preventDefault();mousedown=true
});
canvas.addEventListener("mouseup",function(e){
e.preventDefault();mousedown=false
});
window.onload=loop;
</script>
</body>
</html>
js烟花特效的更多相关文章
- 未封装的js放大镜特效
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>j ...
- 第五章 JS典型特效
注意: 1.JS在HTML中从上到下依次执行,所以获取元素的结果与JS的位置有关 <!DOCTYPE html> <html> <head> <title&g ...
- CSS3实现烟花特效 --web前端
烟花特效,比较简单,直接贴代码了…… <!DOCTYPE html><html lang="en"><head> <meta charse ...
- Three.js粒子特效,shader渲染初探(一篇非常详细的介绍)
Three.js粒子特效,shader渲染初探 转载来源:https://juejin.im/post/5b0ace63f265da0db479270a 这大概是个序 关于Three.js,网上有不多 ...
- js 时钟特效
时钟特效 CreateTime--2018年2月24日15:11:23 Author:Marydon 实现方式:都是基于HTML5的canvas标签实现的 款式一 借助jQuery插件实现 < ...
- 墙裂推荐4款js网页烟花特效
以下是几款网页特效和一款软件: http://keleyi.com/keleyi/phtml/jstexiao/1.htm http://keleyi.com/keleyi/phtml/jstexi ...
- 原生JS投票特效
效果:http://hovertree.com/texiao/js/24/ 效果图: 代码如下: <!DOCTYPE html> <html lang="en"& ...
- Js文字特效—文字段逐个变色循环
自己用来练习的,附上详细注释,如果有和我一样喜欢并想要学习Dom特效创作的朋友,推荐先系统了解Javascript中Html Dom Object部分的内容,包括常用方法及属性. <!DOCTY ...
- javascript js写特效日历
<!doctype html public "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
随机推荐
- gitbook 制作 beego 参考手册
安装gitbook工具 npm install -g gitbook-cli 从github 下载beego文档 https://github.com/beego/beedoc 创建目录 在 zh-c ...
- angularjs开发常见问题-1(持续更新中...)
angularJs中学习中- 1.刷新当前页面数据:$state.reload service.create(data).then(function (newItem) { flash.success ...
- Openstack Ice-House 版本号说明--之中的一个 NOVA
OpenStack Icehouse在4.17正式公布,看了下release note,发现改变不小,说明openstack还是在高速发展中,有不少新的特性增加,也有些小的剔除.以下就我所关注的项目做 ...
- Python调用C可执行程序(subprocess) 分类: python 服务器搭建 C/C++ shell 2015-04-13 21:03 87人阅读 评论(0) 收藏
从Python 2.4开始,Python引入subprocess模块来管理子进程,以取代一些旧模块的方法:如 os.system.os.spawn.os.popen.popen2.commands. ...
- Oracle怎么更改用户名
http://blog.csdn.net/weiwenhp/article/details/8094575 目录(?)[-] 改用户名的用处 怎么改用户名 还要做的工作 想干坏事明文显示密码 很多 ...
- php的ob_flush和flush(转)
php.ini中 output_buffering = off 关闭php的缓存 implicit_flush = Off php不会立即输出到浏览器.如果是ON,相当于每次ECHO 立刻执行一个FL ...
- CentOS6.4下使用默认的文档查看器打开PDF文档乱码的解决方案
最近在CentOS6.4下使用其默认的文档查看器打开PDF文档时出现乱码的方块,有两种方法可以解决. 方法一:修改/etc/fonts/conf.d/49-sansserif.conf文件,如 ...
- android开发的学习路线
参考资料:千锋3G学院--课程大纲 http://www.mobiletrain.org 看了专业的培训机构的课程大纲,才知道,自己学习android的路途才刚刚开始!特此整理分享一下,希望能帮 ...
- oracle分组查询
分组函数 在分组函数中,如果有一个查找项分组,其他项必须也分组,比如下面的语句会报错,因为sal分组了,而ename没有分组: 1.显示工资最高的员工: 2.显示所有员工的平均工资: 2.1使用系统函 ...
- Android(java)学习笔记177:BroadcastReceiver之 应用程序安装和卸载 的广播接收者
国内的主流网络公司(比如网易.腾讯.百度等等),他们往往采用数据挖掘技术获取用户使用信息,从而采用靶向营销.比如电脑上,我们浏览网页的时候,往往会发现网页上会出现我们之前经常浏览内容的商 ...