本文为大家带来了一款,免费而又安全环保的HTML5 Canvas实现的放烟花特效。
效果如下:
代码如下:
XML/HTML Code复制内容到剪贴板- <!DOCTYPE HTML>
- <html>
- <head>
- <title>Canvas 实现放烟花特效</title>
- <meta charset="utf-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width,height=device-height,inital-scale=1.0,maximum-scale=1.0,user-scalable=no">
- <style type="text/css">
- html,body{height:100%;margin:0;padding:0}
- ul,li{text-indent:0;text-decoration:none;margin:0;padding:0}
- img{border:0}
- body{background-color:#000;color:#999;font:100%/18px helvetica, arial, sans-serif}
- canvas{cursor:crosshair;display:block;left:0;position:absolute;top:0;z-index:20}
- #header img{width:100%; height:20%;}
- #bg img{width:100%; height:80%;}
- #header,#bg{position:fixed;left:0;right:0;z-index:10}
- #header{top:0}
- #bg{position:fixed;z-index:1;bottom:0}
- audio{position:fixed;display:none;bottom:0;left:0;right:0;width:100%;z-index:5}
- </style>
- </head>
- <body>
- <div id="bg">
- <img id="bgimg" src="http://img.ivsky.com/img/tupian/pre/201508/02/yuzhou_xingkong_yu_yueliang-006.jpg">
- </div>
- <script src="http://cdn.bootcss.com/jquery/2.2.0/jquery.min.js"></script>
- <script>
- $(function(){
- var Fireworks = function(){
- var self = this;
- // 产生烟花随机数
- var rand = function(rMi, rMa){
- //按位取反运算符
- return ~~((Math.random()*(rMa-rMi+1))+rMi);
- },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(callback){
- window.setTimeout(callback,1000/60);
- }
- }();
- self.init = function(){
- self.canvas = document.createElement('canvas');
- //canvas 全屏
- selfself.canvas.width = self.cw = $(window).innerWidth();
- selfself.canvas.height = self.ch = $(window).innerHeight();
- self.particles = [];
- self.partCount = 150;
- self.fireworks = [];
- selfself.mx = self.cw/2;
- selfself.my = self.ch/2;
- self.currentHue = 30;
- self.partSpeed = 5;
- self.partSpeedVariance = 10;
- self.partWind = 50;
- self.partFriction = 5;
- self.partGravity = 1;
- self.hueMin = 0;
- self.hueMax = 360;
- self.fworkSpeed = 4;
- self.fworkAccel = 10;
- self.hueVariance = 30;
- self.flickerDensity = 25;
- self.showShockwave = true;
- self.showTarget = false;
- self.clearAlpha = 25;
- $(document.body).append(self.canvas);
- selfself.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;
- };
- };
- // 创建粒子
- self.createParticles = function(x,y, hue){
- var countdown = self.partCount;
- while(countdown--){
- var newParticle = {
- x: x,
- y: y,
- coordLast: [
- {x: x, y: y},
- {x: x, y: y},
- {x: x, y: y}
- ],
- angle: rand(0, 360),
- speed: rand(((self.partSpeed - self.partSpeedVariance) <= 0) ? 1 : self.partSpeed - self.partSpeedVariance, (self.partSpeed + self.partSpeedVariance)),
- friction: 1 - self.partFriction/100,
- gravity: self.partGravity/2,
- hue: rand(hue-self.hueVariance, hue+self.hueVariance),
- brightness: rand(50, 80),
- alpha: rand(40,100)/100,
- decay: rand(10, 50)/1000,
- wind: (rand(0, self.partWind) - (self.partWind/2))/25,
- lineWidth: self.lineWidth
- };
- self.particles.push(newParticle);
- }
- };
- // 更新粒子
- self.updateParticles = function(){
- var i = self.particles.length;
- while(i--){
- var p = self.particles[i];
- var radians = p.angle * Math.PI / 180;
- var vx = Math.cos(radians) * p.speed;
- var vy = Math.sin(radians) * p.speed;
- p.speed *= p.friction;
- p.coordLast[2].x = p.coordLast[1].x;
- p.coordLast[2].y = p.coordLast[1].y;
- p.coordLast[1].x = p.coordLast[0].x;
- p.coordLast[1].y = p.coordLast[0].y;
- p.coordLast[0].x = p.x;
- p.coordLast[0].y = p.y;
- p.x += vx;
- p.y += vy;
- p.y += p.gravity;
- p.angle += p.wind;
- p.alpha -= p.decay;
- if(!hitTest(0,0,self.cw,self.ch,p.x-p.radius, p.y-p.radius, p.radius*2, p.radius*2) || p.alpha < .05){
- self.particles.splice(i, 1);
- }
- };
- };
- // 绘制粒子
- self.drawParticles = function(){
- var i = self.particles.length;
- while(i--){
- var p = self.particles[i];
- var coordRand = (rand(1,3)-1);
- self.ctx.beginPath();
- self.ctx.moveTo(Math.round(p.coordLast[coordRand].x), Math.round(p.coordLast[coordRand].y));
- self.ctx.lineTo(Math.round(p.x), Math.round(p.y));
- self.ctx.closePath();
- self.ctx.strokeStyle = 'hsla('+p.hue+', 100%, '+p.brightness+'%, '+p.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(p.x), Math.round(p.y), rand(p.lineWidth,p.lineWidth+3)/2, 0, Math.PI*2, false)
- self.ctx.closePath();
- var randrandAlpha = rand(50,100)/100;
- self.ctx.fillStyle = 'hsla('+p.hue+', 100%, '+p.brightness+'%, '+randAlpha+')';
- self.ctx.fill();
- }
- }
- };
- };
- // 创建烟花
- self.createFireworks = function(startX, startY, targetX, targetY){
- var newFirework = {
- x: startX,
- y: startY,
- startX: startX,
- startY: startY,
- hitX: false,
- hitY: false,
- coordLast: [
- {x: startX, y: startY},
- {x: startX, y: startY},
- {x: startX, y: startY}
- ],
- targetX: targetX,
- targetY: targetY,
- speed: self.fworkSpeed,
- angle: Math.atan2(targetY - startY, targetX - startX),
- shockwaveAngle: Math.atan2(targetY - startY, targetX - startX)+(90*(Math.PI/180)),
- acceleration: self.fworkAccel/100,
- hue: self.currentHue,
- brightness: rand(50, 80),
- alpha: rand(50,100)/100,
- lineWidth: self.lineWidth
- };
- self.fireworks.push(newFirework);
- };
- // 更新烟花
- self.updateFireworks = function(){
- var i = self.fireworks.length;
- while(i--){
- var f = self.fireworks[i];
- self.ctx.lineWidth = f.lineWidth;
- vx = Math.cos(f.angle) * f.speed,
- vy = Math.sin(f.angle) * f.speed;
- f.speed *= 1 + f.acceleration;
- f.coordLast[2].x = f.coordLast[1].x;
- f.coordLast[2].y = f.coordLast[1].y;
- f.coordLast[1].x = f.coordLast[0].x;
- f.coordLast[1].y = f.coordLast[0].y;
- f.coordLast[0].x = f.x;
- f.coordLast[0].y = f.y;
- if(f.startX >= f.targetX){
- if(f.x + vx <= f.targetX){
- ff.x = f.targetX;
- f.hitX = true;
- } else {
- f.x += vx;
- }
- } else {
- if(f.x + vx >= f.targetX){
- ff.x = f.targetX;
- f.hitX = true;
- } else {
- f.x += vx;
- }
- }
- if(f.startY >= f.targetY){
- if(f.y + vy <= f.targetY){
- ff.y = f.targetY;
- f.hitY = true;
- } else {
- f.y += vy;
- }
- } else {
- if(f.y + vy >= f.targetY){
- ff.y = f.targetY;
- f.hitY = true;
- } else {
- f.y += vy;
- }
- }
- if(f.hitX && f.hitY){
- self.createParticles(f.targetX, f.targetY, f.hue);
- self.fireworks.splice(i, 1);
- }
- };
- };
- // 绘制烟花
- self.drawFireworks = function(){
- var i = self.fireworks.length;
- self.ctx.globalCompositeOperation = 'lighter';
- while(i--){
- var f = self.fireworks[i];
- self.ctx.lineWidth = f.lineWidth;
- var coordRand = (rand(1,3)-1);
- self.ctx.beginPath();
- self.ctx.moveTo(Math.round(f.coordLast[coordRand].x), Math.round(f.coordLast[coordRand].y));
- self.ctx.lineTo(Math.round(f.x), Math.round(f.y));
- self.ctx.closePath();
- self.ctx.strokeStyle = 'hsla('+f.hue+', 100%, '+f.brightness+'%, '+f.alpha+')';
- self.ctx.stroke();
- if(self.showTarget){
- self.ctx.save();
- self.ctx.beginPath();
- self.ctx.arc(Math.round(f.targetX), Math.round(f.targetY), rand(1,8), 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(f.x), Math.round(f.y));
- self.ctx.rotate(f.shockwaveAngle);
- self.ctx.beginPath();
- self.ctx.arc(0, 0, 1*(f.speed/5), 0, Math.PI, true);
- self.ctx.strokeStyle = 'hsla('+f.hue+', 100%, '+f.brightness+'%, '+rand(25, 60)/100+')';
- self.ctx.lineWidth = f.lineWidth;
- self.ctx.stroke();
- self.ctx.restore();
- }
- };
- };
- // 绑定事件
- self.bindEvents = function(){
- $(window).on('resize', function(){
- clearTimeout(self.timeout);
- self.timeout = setTimeout(function() {
- selfself.canvas.width = self.cw = $(window).innerWidth();
- selfself.canvas.height = self.ch = $(window).innerHeight();
- self.ctx.lineCap = 'round';
- self.ctx.lineJoin = 'round';
- }, 100);
- });
- $(self.canvas).on('mousedown', function(e){
- self.mx = e.pageX - self.canvas.offsetLeft;
- self.my = e.pageY - self.canvas.offsetTop;
- self.currentHue = rand(self.hueMin, self.hueMax);
- self.createFireworks(self.cw/2, self.ch, self.mx, self.my);
- $(self.canvas).on('mousemove.fireworks', function(e){
- self.mx = e.pageX - self.canvas.offsetLeft;
- self.my = e.pageY - self.canvas.offsetTop;
- self.currentHue = rand(self.hueMin, self.hueMax);
- self.createFireworks(self.cw/2, self.ch, self.mx, self.my);
- });
- });
- $(self.canvas).on('mouseup', function(e){
- $(self.canvas).off('mousemove.fireworks');
- });
- };
- self.clear = function(){
- self.particles = [];
- self.fireworks = [];
- self.ctx.clearRect(0, 0, self.cw, self.ch);
- };
- self.canvasLoop = function(){
- requestAnimFrame(self.canvasLoop, self.canvas);
- self.ctx.globalCompositeOperation = 'destination-out';
- self.ctx.fillStyle = 'rgba(0,0,0,'+self.clearAlpha/100+')';
- self.ctx.fillRect(0,0,self.cw,self.ch);
- self.updateFireworks();
- self.updateParticles();
- self.drawFireworks();
- self.drawParticles();
- };
- self.init();
- }
- var fworks = new Fireworks();
- $('#info-toggle').on('click', function(e){
- $('#info-inner').stop(false, true).slideToggle(100);
- e.preventDefault();
- });
- });
- </script>
- <canvas width="1400" height="449"></canvas>
- </body>
- </html>
是不是被HTML5强大的效果惊呆了,一饱眼福了吧。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。
更新日志
2024年11月17日
2024年11月17日
- 【雨果唱片】中国管弦乐《鹿回头》WAV
- APM亚流新世代《一起冒险》[FLAC/分轨][106.77MB]
- 崔健《飞狗》律冻文化[WAV+CUE][1.1G]
- 罗志祥《舞状元 (Explicit)》[320K/MP3][66.77MB]
- 尤雅.1997-幽雅精粹2CD【南方】【WAV+CUE】
- 张惠妹.2007-STAR(引进版)【EMI百代】【WAV+CUE】
- 群星.2008-LOVE情歌集VOL.8【正东】【WAV+CUE】
- 罗志祥《舞状元 (Explicit)》[FLAC/分轨][360.76MB]
- Tank《我不伟大,至少我能改变我。》[320K/MP3][160.41MB]
- Tank《我不伟大,至少我能改变我。》[FLAC/分轨][236.89MB]
- CD圣经推荐-夏韶声《谙2》SACD-ISO
- 钟镇涛-《百分百钟镇涛》首批限量版SACD-ISO
- 群星《继续微笑致敬许冠杰》[低速原抓WAV+CUE]
- 潘秀琼.2003-国语难忘金曲珍藏集【皇星全音】【WAV+CUE】
- 林东松.1997-2039玫瑰事件【宝丽金】【WAV+CUE】