您现在的位置是:网站首页> 编程资料编程资料
canvas烟花特效锦集HTML5 Canvas实现烟花绽放特效实例教程 HTML5 Canvas 超炫酷烟花绽放动画实现代码
2023-10-16
258人已围观
简介 这篇文章主要介绍了canvas烟花特效锦集,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
canvas可以实现不同动画效果,本文主要记录几种不同节日烟花效果实现。
实现一

html
css
body { background: #000; margin: 0; } canvas { cursor: crosshair; display: block; } js
// when animating on canvas, it is best to use requestAnimationFrame instead of setTimeout or setInterval // not supported in all browsers though and sometimes needs a prefix, so we need a shim window.requestAnimFrame = (function () { return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || function (callback) { window.setTimeout(callback, 1000 / 60); }; })(); // now we will setup our basic variables for the demo var canvas = document.getElementById('canvas'), ctx = canvas.getContext('2d'), // full screen dimensions cw = window.innerWidth, ch = window.innerHeight, // firework collection fireworks = [], // particle collection particles = [], // starting hue hue = 120, // when launching fireworks with a click, too many get launched at once without a limiter, one launch per 5 loop ticks limiterTotal = 5, limiterTick = 0, // this will time the auto launches of fireworks, one launch per 80 loop ticks timerTotal = 80, timerTick = 0, mousedown = false, // mouse x coordinate, mx, // mouse y coordinate my; // set canvas dimensions canvas.width = cw; canvas.height = ch; // now we are going to setup our function placeholders for the entire demo // get a random number within a range function random(min, max) { return Math.random() * (max - min) + min; } // calculate the distance between two points function calculateDistance(p1x, p1y, p2x, p2y) { var xDistance = p1x - p2x, yDistance = p1y - p2y; return Math.sqrt(Math.pow(xDistance, 2) + Math.pow(yDistance, 2)); } // create firework function Firework(sx, sy, tx, ty) { // actual coordinates this.x = sx; this.y = sy; // starting coordinates this.sx = sx; this.sy = sy; // target coordinates this.tx = tx; this.ty = ty; // distance from starting point to target this.distanceToTarget = calculateDistance(sx, sy, tx, ty); this.distanceTraveled = 0; // track the past coordinates of each firework to create a trail effect, increase the coordinate count to create more prominent trails this.coordinates = []; this.coordinateCount = 3; // populate initial coordinate collection with the current coordinates 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); // circle target indicator radius this.targetRadius = 1; } // update firework Firework.prototype.update = function (index) { // remove last item in coordinates array this.coordinates.pop(); // add current coordinates to the start of the array this.coordinates.unshift([this.x, this.y]); // cycle the circle target indicator radius if (this.targetRadius < 8) { this.targetRadius += 0.3; } else { this.targetRadius = 1; } // speed up the firework this.speed *= this.acceleration; // get the current velocities based on angle and speed var vx = Math.cos(this.angle) * this.speed, vy = Math.sin(this.angle) * this.speed; // how far will the firework have traveled with velocities applied? this.distanceTraveled = calculateDistance(this.sx, this.sy, this.x + vx, this.y + vy); // if the distance traveled, including velocities, is greater than the initial distance to the target, then the target has been reached if (this.distanceTraveled >= this.distanceToTarget) { createParticles(this.tx, this.ty); // remove the firework, use the index passed into the update function to determine which to remove fireworks.splice(index, 1); } else { // target not reached, keep traveling this.x += vx; this.y += vy; } } // draw firework Firework.prototype.draw = function () { ctx.beginPath(); // move to the last tracked coordinate in the set, then draw a line to the current x and y 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(); // draw the target for this firework with a pulsing circle ctx.arc(this.tx, this.ty, this.targetRadius, 0, Math.PI * 2); ctx.stroke(); } // create particle function Particle(x, y) { this.x = x; this.y = y; // track the past coordinates of each particle to create a trail effect, increase the coordinate count to create more prominent trails this.coordinates = []; this.coordinateCount = 5; while (this.coordinateCount--) { this.coordinates.push([this.x, this.y]); } // set a random angle in all possible directions, in radians this.angle = random(0, Math.PI * 2); this.speed = random(1, 10); // friction will slow the particle down this.friction = 0.95; // gravity will be applied and pull the particle down this.gravity = 1; // set the hue to a random number +-20 of the overall hue variable this.hue = random(hue - 20, hue + 20); this.brightness = random(50, 80); this.alpha = 1; // set how fast the particle fades out this.decay = random(0.015, 0.03); } // update particle Particle.prototype.update = function (index) { // remove last item in coordinates array this.coordinates.pop(); // add current coordinates to the start of the array this.coordinates.unshift([this.x, this.y]); // slow down the particle this.speed *= this.friction; // apply velocity this.x += Math.cos(this.angle) * this.speed; this.y += Math.sin(this.angle) * this.speed + this.gravity; // fade out the particle this.alpha -= this.decay; // remove the particle once the alpha is low enough, based on the passed in index if (this.alpha <= this.decay) { particles.splice(index, 1); } } // draw particle Particle.prototype.draw = function () { ctx.beginPath(); // move to the last tracked coordinates in the set, then draw a line to the current x and y 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(); } // create particle group/explosion function createParticles(x, y) { // increase the particle count for a bigger explosion, beware of the canvas performance hit with the increased particles though var particleCount = 30; while (particleCount--) { particles.push(new Particle(x, y)); } } // main demo loop function loop() { // this function will run endlessly with requestAnimationFrame requestAnimFrame(loop); // increase the hue to get different colored fireworks over time hue += 0.5; // normally, clearRect() would be used to clear the canvas // we want to create a trailing effect though // setting the composite operation to destination-out will allow us to clear the canvas at a specific opacity, rather than wiping it entirely ctx.globalCompositeOperation = 'destination-out'; // decrease the alpha property to create more prominent trails ctx.fillStyle = 'rgba(0, 0, 0, 0.5)'; ctx.fillRect(0, 0, cw, ch); // change the composite operation back to our main mode // lighter creates bright highlight points as the fireworks and particles overlap each other ctx.globalCompositeOperation = 'lighter'; var text = "HAPPY NEW YEAR !"; ctx.font = "50px sans-serif"; var textData = ctx.measureText(text); ctx.fillStyle = "rgba(" + parseInt(random(0, 255)) + "," + parseInt(random(0, 255)) + "," + parseInt(random(0, 255)) + ",0.3)"; ctx.fillText(text, cw / 2 - textData.width / 2, ch / 2); // loop over each firework, draw it, update it var i = fireworks.length; while (i--) { fireworks[i].draw(); fireworks[i].update(i); } // loop over each particle, draw it, update it var i = particles.length; while (i--) { particles[i].draw(); particles[i].update(i); } // launch fireworks automatically to random coordinates, when the mouse isn't down if (timerTick >= timerTotal) { if (!mousedown) { // start the firework at the bottom middle of the screen, then set the random target coordinates, the random y coordinates will be set within the range of the top half of the screen for (var h = 0; h < 50; h++) { fireworks.push(new Firework(cw / 2, ch / 2, random(0, cw), random(0, ch))); } timerTick = 0; } } else { timerTick++; } // limit the rate at which fireworks get launched when mouse is down if (limiterTick >= limiterTotal) { if (mousedown) { // start the firework at the bottom middle of the screen, then set the current mouse coordinates as the target fireworks.push(new Firework(cw / 2, ch / 2, mx, my)); limiterTick = 0; } } el
相关内容
- 使用Html5、CSS实现文字阴影效果Html/CSS前端实现文字边框阴影效果在线CSS盒子阴影效果代码工具css box-shadow实现曲边阴影与翘边阴影css3 box-shadow阴影(外阴影与外发光)图示讲解纯css3使用transform属性控制文字变形3D阴影效果源码详解CSS3阴影 box-shadow的使用和技巧总结 5分钟让你掌握css3阴影、倒影、渐变小技巧(小编推荐)css3实现渐变、阴影、超出指定文本省略号显示等一些效果实例css实现动态阴影、蚀刻文本、渐变文本效果
- 详解Html5原生拖拽操作html5拖拽排序多图片上传插件特效源码HTML5 拖拽批量上传文件的示例代码html5实现多图片预览上传及点击可拖拽控件html5 canvas绘制可拖拽的3D立体文字效果源码html5使用Drag事件编辑器拖拽上传图片的示例代码HTML5拖拽API经典实例详解
- html5中如何将图片的绝对路径转换成文件对象html5实现点击弹出图片功能html5 录制mp3音频支持采样率和比特率设置html5表单的required属性使用html5调用摄像头实例代码HTML5页面音频自动播放的实现方式Html5大屏数据可视化开发的实现html实现弹窗的实例HTML5来实现本地文件读取和写入的实现方法HTML 罗盘式时钟的实现HTML5简单实现添加背景音乐的几种方法
- html5 canvas绘制放射性渐变色效果html5 canvas实现交互式彩色渐变背景动画特效源码html5 canvas绘制的圆形气泡渐变背景动画特效源码HTML5 Canvas实现图片缩放、翻转、颜色渐变的代码示例HTML5 canvas实现渐变色圆形进度条特效源码html5使用canvas实现的圆形渐变进度条加载动画特效源码使用javascript和HTML5 Canvas画的四渐变色播放按钮效果html5 Canvas画图教程(4)—未闭合的路径及渐变色的填充方法
- HTML5实现分享到微信好友朋友圈QQ好友QQ空间微博二维码功能Html5 实现微信分享及自定义内容的流程Html5页面在微信端的分享的实现方法微信端html5页面调用分享接口示例详解html5 canvas 微信海报分享(个人爬坑)HTML5中外部浏览器唤起微信分享功能的代码
- canvas基础之图形验证码的示例用canvas显示验证码的实现利用html5 canvas破解简单验证码及getImageData接口应用canvas实现滑动验证的实现示例
- canvas三角函数模拟水波效果的示例代码使用 HTML5 Canvas 制作水波纹效果点击图片就会触发
- HTML5 使用 sessionStorage 进行页面传值的方法在html页面中取得session中的值的方法
- 天天飞车A级赛车男爵属性速度性能全面解析_天天飞车攻略_手机游戏_游戏攻略_
- 全民飞机大战烧饼修改器刷分教程_手机游戏_游戏攻略_
点击排行
本栏推荐
