日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > HTML >内容正文

HTML

2022跨年烟花代码(四)HTML5全屏烟花动画特效

發布時間:2024/3/12 HTML 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 2022跨年烟花代码(四)HTML5全屏烟花动画特效 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

HTML5全屏煙花動畫特效

html代碼:

<!doctype html> <html> <head> <meta charset="utf-8"> <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> <body><canvas id="canvas"></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 dimensionscw = window.innerWidth,ch = window.innerHeight,// firework collectionfireworks = [],// particle collectionparticles = [],// starting huehue = 120,// when launching fireworks with a click, too many get launched at once without a limiter, one launch per 5 loop tickslimiterTotal = 5,limiterTick = 0,// this will time the auto launches of fireworks, one launch per 80 loop tickstimerTotal = 80,timerTick = 0,mousedown = false,// mouse x coordinate,mx,// mouse y coordinatemy;// 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 coordinatesthis.x = sx;this.y = sy;// starting coordinatesthis.sx = sx;this.sy = sy;// target coordinatesthis.tx = tx;this.ty = ty;// distance from starting point to targetthis.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 trailsthis.coordinates = [];this.coordinateCount = 3;// populate initial coordinate collection with the current coordinateswhile( 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 radiusthis.targetRadius = 1; }// update firework Firework.prototype.update = function( index ) {// remove last item in coordinates arraythis.coordinates.pop();// add current coordinates to the start of the arraythis.coordinates.unshift( [ this.x, this.y ] );// cycle the circle target indicator radiusif( this.targetRadius < 8 ) {this.targetRadius += 0.3;} else {this.targetRadius = 1;}// speed up the fireworkthis.speed *= this.acceleration;// get the current velocities based on angle and speedvar 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 reachedif( this.distanceTraveled >= this.distanceToTarget ) {createParticles( this.tx, this.ty );// remove the firework, use the index passed into the update function to determine which to removefireworks.splice( index, 1 );} else {// target not reached, keep travelingthis.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 yctx.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 circlectx.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 trailsthis.coordinates = [];this.coordinateCount = 5;while( this.coordinateCount-- ) {this.coordinates.push( [ this.x, this.y ] );}// set a random angle in all possible directions, in radiansthis.angle = random( 0, Math.PI * 2 );this.speed = random( 1, 10 );// friction will slow the particle downthis.friction = 0.95;// gravity will be applied and pull the particle downthis.gravity = 1;// set the hue to a random number +-20 of the overall hue variablethis.hue = random( hue - 20, hue + 20 );this.brightness = random( 50, 80 );this.alpha = 1;// set how fast the particle fades outthis.decay = random( 0.015, 0.03 ); }// update particle Particle.prototype.update = function( index ) {// remove last item in coordinates arraythis.coordinates.pop();// add current coordinates to the start of the arraythis.coordinates.unshift( [ this.x, this.y ] );// slow down the particlethis.speed *= this.friction;// apply velocitythis.x += Math.cos( this.angle ) * this.speed;this.y += Math.sin( this.angle ) * this.speed + this.gravity;// fade out the particlethis.alpha -= this.decay;// remove the particle once the alpha is low enough, based on the passed in indexif( 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 yctx.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 thoughvar particleCount = 30;while( particleCount-- ) {particles.push( new Particle( x, y ) );} }// main demo loop function loop() {// this function will run endlessly with requestAnimationFramerequestAnimFrame( loop );// increase the hue to get different colored fireworks over timehue += 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 entirelyctx.globalCompositeOperation = 'destination-out';// decrease the alpha property to create more prominent trailsctx.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 otherctx.globalCompositeOperation = 'lighter';var result = "QSVFOSU5QiU4NCUyMCVFNyVBNSU5RCVFNCVCRCVBMDIwMjIlMjBIQVBQWSUyME5FVyUyMFlFQVI="; var decodeTxt = window.atob(result);decodeTxt = decodeURI(decodeTxt);ctx.font = "50px sans-serif";var textData = ctx.measureText(decodeTxt);ctx.fillStyle = "rgba("+parseInt(random(0,255))+","+parseInt(random(0,255))+","+parseInt(random(0,255))+",0.3)";ctx.fillText(decodeTxt,cw /2-textData.width/2,ch/2); // loop over each firework, draw it, update itvar i = fireworks.length;while( i-- ) {fireworks[ i ].draw();fireworks[ i ].update( i );}// loop over each particle, draw it, update itvar i = particles.length;while( i-- ) {particles[ i ].draw();particles[ i ].update( i );}// launch fireworks automatically to random coordinates, when the mouse isn't downif( 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 screenfor(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 downif( limiterTick >= limiterTotal ) {if( mousedown ) {// start the firework at the bottom middle of the screen, then set the current mouse coordinates as the targetfireworks.push( new Firework( cw / 2, ch/2, mx, my ) );limiterTick = 0;}} else {limiterTick++;} }// mouse event bindings // update the mouse coordinates on mousemove canvas.addEventListener( 'mousemove', function( e ) {mx = e.pageX - canvas.offsetLeft;my = e.pageY - canvas.offsetTop; });// toggle mousedown state and prevent canvas from being selected canvas.addEventListener( 'mousedown', function( e ) {e.preventDefault();mousedown = true; });canvas.addEventListener( 'mouseup', function( e ) {e.preventDefault();mousedown = false; });// once the window loads, we are ready for some fireworks! window.onload = loop;</script></body> </html>

總結

以上是生活随笔為你收集整理的2022跨年烟花代码(四)HTML5全屏烟花动画特效的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: www.com国产 | 最新亚洲精品 | 扒下小娇妻的内裤打屁股 | 免费无码肉片在线观看 | 佐山爱av在线 | 香蕉网站在线 | 日韩深夜视频 | 97久久综合| 黄色日韩 | 欧美视频一 | 蜜桃做爰免费网站 | 日本少妇bb| 四虎亚洲精品 | 一本大道久久精品 | 涩涩天堂 | 操批网站 | 色热热| 欧美亚洲91 | 国产乱淫精品一区二区三区毛片 | 蜜桃av中文字幕 | 国产爽视频 | 成年人免费大片 | 色婷婷www| 九九热av| a级一片 | 亚洲少妇中文字幕 | 都市激情亚洲一区 | 亚洲一区二区在线播放 | 可以免费观看的av | 国产第一页第二页 | 亚洲专区第一页 | 看全色黄大色黄大片大学生 | 欧美亚洲综合久久 | 日韩一区二区三区精品视频 | 中文字幕a级片 | 精品亚洲国产成人av制服丝袜 | 日韩字幕在线 | 亚洲午夜久久久久久久久 | 你懂的欧美 | 中文字幕亚洲欧美日韩 | 都市激情校园春色 | 好吊视频一区 | 高清三区 | 快射视频在线观看 | 成人av免费网站 | 国产伦理久久精品久久久久 | 西西人体44www大胆无码 | 午夜黄色在线观看 | 手机av在线免费观看 | 欧美另类在线视频 | 秋霞一区| 久久久免费毛片 | 成人av一区 | 久久国产精品久久国产精品 | 久久发布国产伦子伦精品 | 国语对白一区二区 | 性生活视屏| 爱逼综合| 寡妇高潮一级视频免费看 | 99riav视频 | 国产黄色大片网站 | 精品国产aⅴ一区二区三区东京热 | 无码国产精品一区二区免费式直播 | 国产综合视频一区二区 | 在线看片中文字幕 | 夜夜嗷| 激情网站在线观看 | 色屁屁一区二区三区 | 亚洲av无码不卡一区二区三区 | 麻豆国产精品 | 欧美v日本 | 一级特黄aaa大片 | 精品一卡二卡 | 91网站在线免费看 | 亚洲精品国产熟女久久久 | 中文成人无字幕乱码精品区 | 亚洲视频在线观看一区二区三区 | 加勒比精品| 欧美涩涩视频 | 综合视频一区二区 | 亚洲综合网av | 夜夜摸夜夜操 | 免费激情片 | 一区二区美女 | 免费看国产黄色片 | 日日爱影视 | 狠狠网站| 精品一区二区三区四区五区六区 | 欧美精品1| 久久久久久久久久久久国产精品 | a亚洲天堂 | 艳母日本动漫在线观看 | 梦梦电影免费高清在线观看 | 亚洲美女在线播放 | 四虎色播| 欧美一级片在线观看 | 日本少妇bbb | 青青伊人国产 | 亚洲色图狠狠干 |