console
<html>
<head>
<meta charset=utf-8>
<style>
body {
background: #000;
margin: 0;
}
canvas {
cursor: crosshair;
display: block;
}
</style>
</head>
<body>
<canvas id=0></canvas>
</body>
<script>
$=function(b){return document.getElementById(b)};
requestAnimFrame = ( function() {
return requestAnimationFrame || webkitRequestAnimationFrame || mozRequestAnimationFrame ||
function( callback ) {
window.setTimeout( callback, 1000 / 60 );
};
})();
var canvas = $(0),
ctx = canvas.getContext( '2d' ),
cw = window.innerWidth,
ch = window.innerHeight,
fireworks = [],
particles = [],
色调 = 120,
limiterTotal = 5,
limiterTick = 0,
timerTotal = 80,
timerTick = 0,
mousedown = false,
mp;
canvas.width = cw;
canvas.height = ch;
function random( min, max ) {
return Math.random() * ( max - min ) + min;
}
function calculateDistance( p1, p2 ) {
var xDistance = p1[0] - p2[0],
yDistance = p1[1] - p2[1];
return Math.sqrt( Math.pow( xDistance, 2 ) + Math.pow( yDistance, 2 ) );
}
function 烟花( 起点, 目标点 ) {
this.点 = 起点;
this.起点 = 起点;
this.目标点 = 目标点;
this.distanceToTarget = calculateDistance( 起点, 目标点 );
this.distanceTraveled = 0;
this.坐标集合 = [];
this.coordinateCount = 3;
while( this.coordinateCount-- ) {
this.坐标集合.push( 起点 );
}
this.angle = Math.atan2( 目标点[1] - 起点[1], 目标点[0] - 起点[0] );
this.速度 = 2;
this.加速度 = 1.05;
this.亮度 = random( 50, 70 );
this.targetRadius = 1;
}
烟花.prototype.update = function( index ) {
this.坐标集合.pop();
this.坐标集合.unshift( this.点 );
if( this.targetRadius < 8 ) {
this.targetRadius += 0.3;
} else {
this.targetRadius = 1;
}
this.速度 *= this.加速度;
var vx = Math.cos( this.angle ) * this.速度,
vy = Math.sin( this.angle ) * this.速度;
this.distanceTraveled = calculateDistance( this.起点, [this.点[0] + vx, this.点[1] + vy] );
if( this.distanceTraveled >= this.distanceToTarget ) {
createParticles( this.目标点 );
fireworks.splice( index, 1 );
} else {
this.点=[this.点[0]+vx,this.点[1]+vy];
}
}
烟花.prototype.画 = function() {
ctx.beginPath();
ctx.moveTo( this.坐标集合[ this.坐标集合.length - 1][ 0 ], this.坐标集合[ this.坐标集合.length - 1][ 1 ] );
ctx.lineTo( this.点[0], this.点[1] );
ctx.strokeStyle = 'hsl(' + 色调 + ', 100%, ' + this.亮度 + '%)';
ctx.stroke();
ctx.beginPath();
ctx.arc( this.目标点[0], this.目标点[1], this.targetRadius, 0, Math.PI * 2 );
ctx.stroke();
}
function Particle( 点 ) {
this.点 = 点;
this.坐标集合 = [];
this.coordinateCount = 5;
while( this.coordinateCount-- ) this.坐标集合.push( 点 );
this.angle = random( 0, Math.PI * 2 );
this.速度 = random( 1, 10 );
this.friction = 0.95;
this.重力加速度 = 1;
this.色调 = random( 色调 - 20, 色调 + 20 );
this.亮度 = random( 50, 80 );
this.alpha = 1;
this.decay = random( 0.015, 0.03 );
}
Particle.prototype.update = function( index ) {
this.坐标集合.pop();
this.坐标集合.unshift( this.点 );
this.速度 *= this.friction;
this.点=[this.点[0]+Math.cos( this.angle ) * this.速度,this.点[1]+Math.sin( this.angle ) * this.速度 + this.重力加速度];
this.alpha -= this.decay;
if( this.alpha <= this.decay ) {
particles.splice( index, 1 );
}
}
Particle.prototype.画 = function() {
ctx. beginPath();
ctx.moveTo( this.坐标集合[ this.坐标集合.length - 1 ][ 0 ], this.坐标集合[ this.坐标集合.length - 1 ][ 1 ] );
ctx.lineTo( this.点[0], this.点[1] );
ctx.strokeStyle = 'hsla(' + this.色调 + ', 100%, ' + this.亮度 + '%, ' + this.alpha + ')';
ctx.stroke();
}
function createParticles( 点 ) {
var particleCount = 30;
while( particleCount-- ) {
particles.push( new Particle( 点 ) );
}
}
function loop() {
requestAnimFrame( loop );
色调 += 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 ].画();
fireworks[ i ].update( i );
}
var i = particles.length;
while( i-- ) {
particles[ i ].画();
particles[ i ].update( i );
}
if( timerTick >= timerTotal ) {
if( !mousedown ) {
fireworks.push( new 烟花( [cw / 2, ch], [random( 0, cw ), random( 0, ch / 2 )] ) );
timerTick = 0;
}
} else {
timerTick++;
}
if( limiterTick >= limiterTotal ) {
if( mousedown ) {
fireworks.push( new 烟花( [cw / 2, ch], mp ) );
limiterTick = 0;
}
} else {
limiterTick++;
}
}
canvas.addEventListener( 'mousemove', function( e ) {
mp = [e.pageX - canvas.offsetLeft, 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>
</html>