HTML5中怎么用Canvas实现超炫酷烟花绽放动画


这篇文章主要介绍了HTML5中怎么用Canvas实现超炫酷烟花绽放动画的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇HTML5中怎么用Canvas实现超炫酷烟花绽放动画文章都会有所收获,下面我们一起来看看吧。
  HTML代码:
XML/HTML Code复制内容到剪贴板

  HTML的结构非常简单,即构造了一个canvas容器,我们会利用JS在这个容器中生成一个Canvas对象。看最后的JS代码你就会知道了。
  CSS代码:
CSS Code复制内容到剪贴板
#canvas-container { background: #000 url(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(skyline.png) repeat-x 50% 0; bottombottom: 0; height: 135px; left: 0; position: absolute; width: 100%; z-index: 1;
} #mountains1 { background: url(mountains1.png) repeat-x 40% 0; bottombottom: 0; height: 200px; left: 0; position: absolute; width: 100%; z-index: 1;
} #mountains2 { background: url(mountains2.png) repeat-x 30% 0; bottombottom: 0; height: 250px; left: 0; position: absolute; width: 100%; z-index: 1;
} #gui { rightright: 0; position: fixed; top: 0; z-index: 3;
}
  CSS代码没什么特别,主要也就定义一下背景色和边框之类的。
  接下来是最重要的Javascript代码。
  Javascript代码:
JavaScript Code复制内容到剪贴板
self.init = function(){
self.dt = 0;
self.oldTime = Date.now();
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;
};
};
  这段JS代码主要是往canvas容器中构造一个Canvas对象,并且对这个canvas对象的外观以及动画属性作了初始化。
JavaScript Code复制内容到剪贴板
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)

};
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 * self.dt; this.y += vy * self.dt; 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

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();
}
}
};
  这段JS代码的功能是实现烟花爆炸后的小颗粒的绘制,从draw方法中可以看出,创建几个随机点,烟花颗粒即可在这个范围的随机点中散落。
JavaScript Code复制内容到剪贴板
var Firework = function(startX, startY, targetX, targetY){ this.x = startX; this.y = startY; this.startX = startX; this.startY = startY; this.hitX = false; this.hitY = false; th免费云主机域名is.coordLast = [
{x: startX, y: startY},
{x: startX, y: startY},
{x: startX, y: startY}
]; this.targetX = targetX; this.targetY = targetY; this.speed = self.fworkSpeed; this.angle = Math.atan2(targetY – startY, targetX – startX); this.shockwaveAngle = Math.atan2(targetY – startY, targetX – startX)+(90*(Math.PI/180)); this.acceleration = self.fworkAccel/100; this.hue = self.currentHue; this.brightness = rand(50, 80); this.alpha = rand(50,100)/100; this.lineWidth = self.lineWidth; this.targetRadius = 1;
};
Firework.prototype.update = function(index){
self.ctx.lineWidth = this.lineWidth;
vx = Math.cos(this.angle) * this.speed,
vy = Math.sin(this.angle) * this.speed; this.speed *= 1 + this.acceleration; 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; if(self.showTarget){ if(this.targetRadius

} else { this.targetRadius = 1 * self.dt;
}
} if(this.startX >= this.targetX){ if(this.x + vx

} else { this.x += vx * self.dt;
}
} else { if(this.x + vx >= this.targetX){ this.x = this.targetX; this.hitX = true;
} else { this.x += vx * self.dt;
}
} if(this.startY >= this.targetY){ if(this.y + vy

} else { this.y += vy * self.dt;
}
} else { if(this.y + vy >= this.targetY){ this.y = this.targetY; this.hitY = true;
} else { this.y += vy * self.dt;
}
} if(this.hitX && this.hitY){ var randExplosion = rand(0, 9);
self.createParticles(this.targetX, this.targetY, this.hue);
self.fireworks.splice(index, 1);
}
};
Firework.prototype.draw = function(){
self.ctx.lineWidth = this.lineWidth; 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.showTarget){
self.ctx.save();
self.ctx.beginPath();
self.ctx.arc(Math.round(this.targetX), Math.round(this.targetY), this.targetRadius, 0, Math.PI*2, false)
self.ctx.closePath();
self.ctx.lineWidth = 1;
self.ctx.stroke();
self.ctx.restore();
} if(self.showShockwave){
self.ctx.save();
self.ctx.translate(Math.round(this.x), Math.round(this.y));
self.ctx.rotate(this.shockwaveAngle);
self.ctx.beginPath();
self.ctx.arc(0, 0, 1*(this.speed/5), 0, Math.PI, true);
self.ctx.strokeStyle = ‘hsla(‘+this.hue+’, 100%, ‘+this.brightness+’%, ‘+rand(25, 60)/100+’)’;
self.ctx.lineWidth = this.lineWidth;
self.ctx.stroke();
self.ctx.restore();
}
};
  这段JS代码是创建烟花实例的,我们也可以从draw方法中看出,当我们鼠标点击画布中的某点时,烟花发射的目的地就在那个点上。关于“HTML5中怎么用Canvas实现超炫酷烟花绽放动画”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“HTML5中怎么用Canvas实现超炫酷烟花绽放动画”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注百云主机行业资讯频道。

相关推荐: html组件怎么写

这篇“html组件怎么写”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“html组件怎么写”文章吧。   1.HTML语义化   语义化HTML代…

免责声明:本站发布的图片视频文字,以转载和分享为主,文章观点不代表本站立场,本站不承担相关法律责任;如果涉及侵权请联系邮箱:360163164@qq.com举报,并提供相关证据,经查实将立刻删除涉嫌侵权内容。

Like (0)
Donate 微信扫一扫 微信扫一扫
Previous 10/16 11:45
Next 10/16 17:16

相关推荐