• PUDN用户
    了解作者
  • HTML
    开发工具
  • 3KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 0
    下载次数
  • 2018-08-14 09:21
    上传日期
Canvas应用非常广泛,基本上所有HTML5动画特效都会用到Canvas特性。今天我们要分享一款基于HTML5 Canvas实现的烟花特效,我们只需在黑色的画布上点击鼠标,即会有漂亮的烟花绽放在夜空当中。
1.rar
  • jiaoben2137
  • index.html
    9.7KB
内容介绍
<!DOCTYPE html> <html dir="ltr" lang="zh-CN"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width" /> <title>HTML5 Canvas烟花特效 场景十分华丽在线演示</title> <style> /* basic styles for black background and crosshair cursor */ body { background: #000; margin: 0; } canvas { cursor: crosshair; display: block; } </style> </head> <div style="text-align:center;clear:both"> <script src="/gg_bd_ad_720x90.js" type="text/javascript"></script> <script src="/follow.js" type="text/javascript"></script> </div> <canvas id="canvas">Canvas is not supported in your browser.</canvas> <script> // 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.
评论
    相关推荐
    • 七款经典的html5 +css3 控件
      HTML5 Canvas烟花特效 场景十分华丽 jQuery轻量级网页编辑器 选中即可编辑 CSS3手风琴菜单 下拉展开带弹性动画 HTML5/CSS3 3D环形图片墙 伴随旋转动画 HTML5 Canvas正弦波动画 可自定义波长和速度
    • GaussDB_100_1.0.1-DATABASE-REDHAT-64bit.tar.gz
      guassdb100在redhat上安装包,单机部署的包,安装步骤请看我的文中介绍,经过大量实验搭建总结出来的文档
    • 卷积神经网络
      这是卷积神经网络的一个实际用例,已经调试好了,能够在matlab上成功运行,适合从事卷积神经网络(CNN)研究的人员学习使用。
    • SIM800C_MQTT.rar
      使用SIM800C模块,使用MQTT协议,连接中国移动onenet平台,能实现数据的订阅、发布、存储等
    • android从bootloader到launcher启动流程整理
      讲述android 开机流程 从boot rom---bootloader---init--zygote---systemserver---ams 并附上自己整理的每个流程的流程图 ,清晰熟悉android 启动流程
    • Aerosim Blockset
      The AeroSim aeronautical simulation blockset provides a complete set of tools for the rapid development of nonlinear 6-degree of freedom aircraft dynamic models. In addition to the basic aircraft dynamics blocks, the library also includes complete aircraft models which can be customized through parameter files.
    • 微电网PSO优化算法
      微电网的粒子群优化调度算法,里面包括相关文献参考和电价数据,算法的过程解释的很清楚,是我学算法时候用到的资源。
    • 分数阶混沌系统:分数阶混沌系统的数值解。-matlab开发
      该工具箱包含可用于模拟一些著名的分数阶混沌系统的函数,例如: - 陈的系统, - Arneodo的系统, - Genesio-Tesi 的系统, - 洛伦兹系统, - 牛顿-莱普尼克系统, - 罗斯勒的系统, - Lotka-Volterra系统, - 达芬的系统, - 范德波尔的振荡器, -伏打的系统- 陆氏系统, - 刘的系统, - Chua的系统, - 金融系统, - 3 细胞 CNN。 这些函数以数值方式计算描述混沌系统的分数阶非线性微分方程的解。 每个函数返回总模拟时间的状态轨迹(吸引器)。 更多详情请看书: Ivo Petras,分数阶非线性系统:建模、分析和仿真,Springer,系列:非线性物理科学,2011,ISBN 978-3-642-18100-9。 http://www.springer.com/engineering/control/book/978-3-
    • matlabpam代码-dsp-library:用于光通信的MATLABDSP函数库
      matlab pam代码 DSP库 该项目包含几种不同的功能,可将DSP算法应用于光通信。 这些功能适用于相干和非相干(PAM,DMT)光通信。 大多数功能是相互独立的。 因此,该代码中的功能可以轻松使用,并与其他DSP功能结合使用。 用法 功能列表以及简短说明在文件中。 输入和输出参数的描述在每个函数的标题中。 讯号 通常,输入(和输出)信号在第一维度上具有时间(例如,列向量),而第二维度用于一次管理多个信号(例如,不同的极化,不同的参数等)。 之所以选择这种约定,是因为MATLAB通过将列保留在内存的连续部分中来存储矩阵,因此,这种约定比其他方法(行向量)要快。 参数 大多数功能使用参数结构作为输入参数。 函数中使用的参数的描述通常在标头中,而coherent-dsp函数的默认参数在文件中。 参考 执照 此代码在下发布。
    • 有关多目标跟踪的PHD滤波的一些资料
      一些关于多目标跟踪的新的文献,主要是有关概率假设密度(PHD)的