<!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烟花特效
时间: 2024-10-10 14:48:18
js烟花特效的相关文章
貌似3D效果的JS烟花特效代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>烟花代码</title> &
JS 面向对象 实现烟花特效
基本效果如图: 这里的烟花特效 是配合鼠标点击去实现的 (你要是想绑定别的事件也可) 创建一个烟花,从底部升起运动到目标位置 到达目标位置之后,删除它的同时 炸出一堆烟花 HTML布局+CSS样式 <div class="container"></div> 1 <style> 2 .container{ 3 width: 80%; 4 height: 300px; 5 border: 2px solid red; 6 background: #00
鼠标点击烟花特效
烟花特效介绍 就是本站鼠标点击的效果,不放截图了 源码部署 <script src="https://blog-static.cnblogs.com/files/zouwangblog/mouse-click.js"></script> <canvas width="1777" height="841" style="position: fixed; left: 0px; top: 0px; z-index
js网状特效源代码下载
原文:js网状特效源代码下载 源代码下载地址:http://www.zuidaima.com/share/1550463532010496.htm 適合自己的網站的開場
js 时钟特效
时钟特效 CreateTime--2018年2月24日15:11:23 Author:Marydon 实现方式:都是基于HTML5的canvas标签实现的 款式一 借助jQuery插件实现 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <meta name="
CSS3实现烟花特效 --Web前端
简单的烟花特效,比较简单,直接贴代码了…… <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>css3实现烟花特效</title> <style> * { margin: 0; padding: 0; } html{ width: 100%; height: 100%; } body{ width:
墙裂推荐4款js网页烟花特效
以下是几款网页特效和一款软件: http://keleyi.com/keleyi/phtml/jstexiao/1.htm http://keleyi.com/keleyi/phtml/jstexiao/2.htm http://keleyi.com/keleyi/phtml/html5/14.htm http://hovertree.com/texiao/js/17/ 另外推荐一款软件何问起键盘 转自:http://hovertree.com/h/bjaf/epbhdsa3.htm 更多特效:
Js文字特效—文字段逐个变色循环
自己用来练习的,附上详细注释,如果有和我一样喜欢并想要学习Dom特效创作的朋友,推荐先系统了解Javascript中Html Dom Object部分的内容,包括常用方法及属性. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>文字段逐个变色循环特效</title> </head> <body> <a href=&
原生JS投票特效
效果:http://hovertree.com/texiao/js/24/ 效果图: 代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>JS+CSS实现投票效果 - 何问起</title> <link rel="stylesheet" href="http:/