canvas可调参数的烟花定点绽放动画效果代码
代码语言:html
所属分类:动画
代码描述:canvas可调参数的烟花定点绽放动画效果代码
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
html, body {
margin: 0;
padding: 0;
}
body {
background: #171717;
color: #999;
font: 100%/18px helvetica, arial, sans-serif;
}
a {
color: #2fa1d6;
font-weight: bold;
text-decoration: none;
}
a:hover {
color: #fff;
}
#canvas-container {
background: #000 url(//repo.bfw.wiki/bfwrepo/images/bg/bg.jpg);
height: 400px;
left: 50%;
margin: -200px 0 0 -300px;
position: absolute;
top: 50%;
width: 600px;
z-index: 2;
}
canvas {
cursor: crosshair;
display: block;
position: relative;
z-index: 3;
}
canvas:active {
cursor: crosshair;
}
#skyline {
background: url(//repo.bfw.wiki/bfwrepo/images/bg/skyline.png) repeat-x 50% 0;
bottom: 0;
height: 135px;
left: 0;
position: absolute;
width: 100%;
z-index: 1;
}
#mountains1 {
background: url(//repo.bfw.wiki/bfwrepo/images/bg/mountains1.png) repeat-x 40% 0;
bottom: 0;
height: 200px;
left: 0;
position: absolute;
width: 100%;
z-index: 1;
}
#mountains2 {
background: url(//repo.bfw.wiki/bfwrepo/images/bg/mountains2.png) repeat-x 30% 0;
bottom: 0;
height: 250px;
left: 0;
position: absolute;
width: 100%;
z-index: 1;
}
#gui {
right: 0;
position: fixed;
top: 0;
z-index: 3;
}
</style>
</head>
<body>
<div id="gui"></div>
<div id="canvas-container">
<div id="mountains2"></div>
<div id="mountains1"></div>
<div id="skyline"></div>
</div>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/jquery.2.11.js"></script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/dat.gui-min.js"></script>
<script >
var Fireworks = function () {
/*=============================================================================*/
/* Utility
/*=============================================================================*/
var self = this;
var rand = function (rMi, rMa) {return ~~(Math.random() * (rMa - rMi + 1) + rMi);};
var hitTest = function (x1, y1, w1, h1, x2, y2, w2, h2) {return !(x1 + w1 < x2 || x2 + w2 < x1 || y1 + h1 < y2 || y2 + h2 < y1);};
window.requestAnimFrame = function () {return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function (a) {window.setTimeout(a, 1E3 / 60);};}();
/*=============================================================================*/
/* Initialize
/*=============================================================================*/
self.init = function () {
self.canvas = document.createElement('canvas');
self.canvasContainer = $('#canvas-container');
var canvasContainerDisabled = document.getElementById('canvas-container');
self.canvas.onselectstart = function () {
return false;
};
self.canvas.width = self.cw = 600;
self.canvas.height = self.ch = 400;
self.particles = [];
self.partCount = 30;
self.fireworks = [];
self.mx = self.cw / 2;
self.my = self.ch / 2;
self.currentHue = 170;
self.partSpeed = 5;
self.partSpeedVariance = 10;
self.partWind = 50;
self.partFriction = 5;
self.partGravity = 1;
self.hueMin = 150;
self.hueMax = 200;
self.fworkSpeed = 2;
self.fworkAccel = 4;
self.hueVariance = 30;
self.flickerDensity = 20;
self.showShockwave = false;
self.showTarget = true;
self.clearAlpha = 25;
self.canvasContainer.append(self.canvas);
self.ctx = self.canvas.getContext('2d');
self.ctx.lineCap = 'round';
self.ctx.lineJoin = 'round';
self.lineWidth = 1;
self.bindEvents();
self.canvasLoop();
self.canvas.onselectstart = function () {
return false;
};
};
/*=============================================================================*/
/* Particle Constructor
/*=============================================================================*/
var Particle = function (x, y, hue) {
this.x = x;
this.y = y;
this.coordLast = [
{ x: x, y: y },
{ x: x, y: y },
{ x: x, y: y }];
this.angle = rand(0, 360);
this.speed = rand(self.partSpeed - self.partSpeedVariance <= 0 ? 1 : self.partSpeed - self.partSpeedVariance, self.partSpeed + self.partSpeedVariance);
this.friction = 1 - self.partFriction / 100;
this.gravity = self.partGravity / 2;
this.hue = rand(hue - self.hueVariance, hue + self.hueVariance);
this.brightness = rand(50, 80);
this.alpha = rand(40, 100) / 100;
this.decay = rand(10, 50) / 1000;
this.wind = (rand(0, self.partWind) - self.partWind / 2) / 25;
this.lineWidth = self.lineWidth;
};
Particle.prototype.update = function (index) {
var radians = this.angle * Math.PI / 180;
var vx = Math.cos(radians) * this.speed;
var vy = Math.sin(radians) * this.speed + this.gravity;
this.speed *= this.friction;
this.coordLast[2].x = this.coordLast[1].x;
this.coordLast[2].y = this.coordLast[1].y;
this.coordLast[1].x = this.coordLast[0].x;
this.coordLast[1].y = this.coordLast[0].y;
this.coordLast[0].x = this.x;
this.coordLast[0].y = this.y;
this.x += vx;
this.y += vy;
this.angle += this.wind;
this.alpha -= this.decay;
if (!hitTest(0, 0, self.cw, self.ch, this.x - this.radius, this.y - this.radius, this.radius * 2, this.radius * 2) || this.alpha < .05) {
self.particles.splice(index, 1);
}
};
Particle.prototype.draw = function () {
var coordRand = rand(1, 3) - 1;
self.ctx.beginPath();
self.ctx.moveTo(Math.round(this.coordLast[coordRand].x), Math.round(this.coordLast[coordRand].y));
self.ctx.lineTo(Math.round(this.x), Math.round(this.y));
self.ctx.closePath();
self.ctx.strokeStyle = 'hsla(' + this.hue + ', 100%, ' + this.brightness + '%, ' + this.alpha + ')';
self.ctx.stroke();
if (self.flickerDensity > 0) {
var inverseDensity = 50 - self.flickerDensity;
if (rand(0, inverseDensity) === inverseDensity) {
self.ctx.beginPath();
self.ctx.arc(Math.round(this.x), Math.round(this.y), rand(this.lineWidth, this.lineWidth + 3) / 2, 0, Math.PI * 2, false);
self.ctx.closePath();
var randAlpha = rand(50, 100) / 100;
self.ctx.fillStyle = 'hsla(' + this.hue + ', 100%, ' + this.brightness + '%, ' + randAlpha + ')';
self.ctx.fill();
}
}
};
/*=============================================================================*/
/* Create Particles
/*=============================================================================*/
self.createParticles = function (x, y, hue) {
var countdown = self.partCount;
while (countdown--) {
self.particles.push(new Particle(x, y, hue));
}
};
/*=============================================================================*/
/* Update Particles
/*=============================================================================*/
self.updateParticles = function () {
var i = self.particles.length;
while (i--) {
var p .........完整代码请登录后点击上方下载按钮下载查看
网友评论0