用前端html如何实现2024烟花效果

打印 上一主题 下一主题

主题 894|帖子 894|积分 2682

用HTML、CSS和JavaScript编写的网页,重要用于展示“2024新年快乐!”的文字情势烟花效果。下面是对代码重要部门的分析:

HTML布局



  • 包含三个<canvas>元素,用于绘制动画。
  • 引入百度统计的脚本。
CSS样式



  • 设置body的背景为玄色,并使得canvas元素绝对定位,覆盖整个页面。
JavaScript 功能


  • 百度统计脚本:页面开始时引入了百度统计的脚本,用于网页访问数据分析。
  • 获取URL参数:GetRequest函数用于分析URL中的查询字符串参数。
  • 烟花碎片(Shard)类

    • 每个Shard代表烟花爆炸后的一个碎片。
    • 包含碎片的位置、颜色、大小、速率等属性。
    • draw方法用于在canvas上绘制碎片。
    • update方法用于更新碎片的位置和状态。

  • 火箭(Rocket)类

    • 表现发射的烟花火箭。
    • 包含火箭的位置、速率、颜色等属性。
    • draw方法用于在canvas上绘制火箭。
    • update方法用于更新火箭的位置。
    • explode方法用于模拟火箭爆炸,天生多个Shard实例。

  • 初始化和动画循环

    • 获取所有canvas元素和对应的2D渲染上下文。
    • 根据屏幕大小调整字体大小,以适应屏幕宽度,并在一个canvas上绘制“2024新年快乐!”文字。
    • 通过读取绘制的文字的像素数据,确定烟花爆炸的目的位置。
    • 使用requestAnimationFrame创建动画循环,不绝更新和绘制火箭和碎片,模拟烟花效果。

  • 辅助函数

    • lerp(线性插值函数):用于平滑地在两个值之间插值,常用于动画效果中。

  • 实验流程
          


    • 页面加载完成后,动画循环开始运行。
    • 每隔一定帧数,天生一个新的Rocket实例,模拟火箭发射。
    • 当火箭到达一定高度后,调用explode方法,天生多个Shard实例,模拟烟花爆炸。
    • 碎片根据预设的目的位置移动,终极形成“2024新年快乐!”的文字形状。     
      1. <!DOCTYPE html>
      2. <html lang="en">
      3. <script>
      4.   var _hmt = _hmt || [];
      5.   (function () {
      6.     var hm = document.createElement("script");
      7.     hm.src = "https://hm.baidu.com/hm.js?c923daf3182a4b0ce01878475080aadc";
      8.     var s = document.getElementsByTagName("script")[0];
      9.     s.parentNode.insertBefore(hm, s);
      10.   })();
      11. </script>
      12. <head>
      13.   <meta charset="UTF-8">
      14.   <title>2024,新年快乐!</title>
      15. </head>
      16. <style>
      17.   body {
      18.     margin: 0;
      19.     overflow: hidden;
      20.     background: black;
      21.   }
      22.   canvas {
      23.     position: absolute;
      24.   }
      25. </style>
      26. <body>
      27.   <canvas></canvas>
      28.   <canvas></canvas>
      29.   <canvas></canvas>
      30.   <script>
      31.     function GetRequest() {
      32.       var url = decodeURI(location.search); //获取url中"?"符后的字串
      33.       var theRequest = new Object();
      34.       if (url.indexOf("?") != -1) {
      35.         var str = url.substr(1);
      36.         strs = str.split("&");
      37.         for (var i = 0; i < strs.length; i++) {
      38.           theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
      39.         }
      40.       }
      41.       return theRequest;
      42.     }
      43.     class Shard {
      44.       constructor(x, y, hue) {
      45.         this.x = x;
      46.         this.y = y;
      47.         this.hue = hue;
      48.         this.lightness = 50;
      49.         this.size = 15 + Math.random() * 10;
      50.         const angle = Math.random() * 2 * Math.PI;
      51.         const blastSpeed = 1 + Math.random() * 6;
      52.         this.xSpeed = Math.cos(angle) * blastSpeed;
      53.         this.ySpeed = Math.sin(angle) * blastSpeed;
      54.         this.target = getTarget();
      55.         this.ttl = 100;
      56.         this.timer = 0;
      57.       }
      58.       draw() {
      59.         ctx2.fillStyle = `hsl(${this.hue}, 100%, ${this.lightness}%)`;
      60.         ctx2.beginPath();
      61.         ctx2.arc(this.x, this.y, this.size, 0, 2 * Math.PI);
      62.         ctx2.closePath();
      63.         ctx2.fill();
      64.       }
      65.       update() {
      66.         if (this.target) {
      67.           const dx = this.target.x - this.x;
      68.           const dy = this.target.y - this.y;
      69.           const dist = Math.sqrt(dx * dx + dy * dy);
      70.           const a = Math.atan2(dy, dx);
      71.           const tx = Math.cos(a) * 5;
      72.           const ty = Math.sin(a) * 5;
      73.           this.size = lerp(this.size, 1.5, 0.05);
      74.           if (dist < 5) {
      75.             this.lightness = lerp(this.lightness, 100, 0.01);
      76.             this.xSpeed = this.ySpeed = 0;
      77.             this.x = lerp(this.x, this.target.x + fidelity / 2, 0.05);
      78.             this.y = lerp(this.y, this.target.y + fidelity / 2, 0.05);
      79.             this.timer += 1;
      80.           } else
      81.             if (dist < 10) {
      82.               this.lightness = lerp(this.lightness, 100, 0.01);
      83.               this.xSpeed = lerp(this.xSpeed, tx, 0.1);
      84.               this.ySpeed = lerp(this.ySpeed, ty, 0.1);
      85.               this.timer += 1;
      86.             } else {
      87.               this.xSpeed = lerp(this.xSpeed, tx, 0.02);
      88.               this.ySpeed = lerp(this.ySpeed, ty, 0.02);
      89.             }
      90.         } else {
      91.           this.ySpeed += 0.05;
      92.           //this.xSpeed = lerp(this.xSpeed, 0, 0.1);
      93.           this.size = lerp(this.size, 1, 0.05);
      94.           if (this.y > c2.height) {
      95.             shards.forEach((shard, idx) => {
      96.               if (shard === this) {
      97.                 shards.splice(idx, 1);
      98.               }
      99.             });
      100.           }
      101.         }
      102.         this.x = this.x + this.xSpeed;
      103.         this.y = this.y + this.ySpeed;
      104.       }
      105.     }
      106.     class Rocket {
      107.       constructor() {
      108.         const quarterW = c2.width / 4;
      109.         this.x = quarterW + Math.random() * (c2.width - quarterW);
      110.         this.y = c2.height - 15;
      111.         this.angle = Math.random() * Math.PI / 4 - Math.PI / 6;
      112.         this.blastSpeed = 6 + Math.random() * 7;
      113.         this.shardCount = 15 + Math.floor(Math.random() * 15);
      114.         this.xSpeed = Math.sin(this.angle) * this.blastSpeed;
      115.         this.ySpeed = -Math.cos(this.angle) * this.blastSpeed;
      116.         this.hue = Math.floor(Math.random() * 360);
      117.         this.trail = [];
      118.       }
      119.       draw() {
      120.         ctx2.save();
      121.         ctx2.translate(this.x, this.y);
      122.         ctx2.rotate(Math.atan2(this.ySpeed, this.xSpeed) + Math.PI / 2);
      123.         ctx2.fillStyle = `hsl(${this.hue}, 100%, 50%)`;
      124.         ctx2.fillRect(0, 0, 5, 15);
      125.         ctx2.restore();
      126.       }
      127.       update() {
      128.         this.x = this.x + this.xSpeed;
      129.         this.y = this.y + this.ySpeed;
      130.         this.ySpeed += 0.1;
      131.       }
      132.       explode() {
      133.         for (let i = 0; i < 70; i++) {
      134.           shards.push(new Shard(this.x, this.y, this.hue));
      135.         }
      136.       }
      137.     }
      138.     console.log(GetRequest('val').val)
      139.     // INITIALIZATION
      140.     const [c1, c2, c3] = document.querySelectorAll('canvas');
      141.     const [ctx1, ctx2, ctx3] = [c1, c2, c3].map(c => c.getContext('2d'));
      142.     let fontSize = 200;
      143.     const rockets = [];
      144.     const shards = [];
      145.     const targets = [];
      146.     const fidelity = 3;
      147.     let counter = 0;
      148.     c2.width = c3.width = window.innerWidth;
      149.     c2.height = c3.height = window.innerHeight;
      150.     ctx1.fillStyle = '#000';
      151.     const text = '2024新年快乐!'
      152.     let textWidth = 99999999;
      153.     while (textWidth > window.innerWidth) {
      154.       ctx1.font = `900 ${fontSize--}px Arial`;
      155.       textWidth = ctx1.measureText(text).width;
      156.     }
      157.     c1.width = textWidth;
      158.     c1.height = fontSize * 1.5;
      159.     ctx1.font = `900 ${fontSize}px Arial`;
      160.     ctx1.fillText(text, 0, fontSize);
      161.     const imgData = ctx1.getImageData(0, 0, c1.width, c1.height);
      162.     for (let i = 0, max = imgData.data.length; i < max; i += 4) {
      163.       const alpha = imgData.data[i + 3];
      164.       const x = Math.floor(i / 4) % imgData.width;
      165.       const y = Math.floor(i / 4 / imgData.width);
      166.       if (alpha && x % fidelity === 0 && y % fidelity === 0) {
      167.         targets.push({ x, y });
      168.       }
      169.     }
      170.     ctx3.fillStyle = '#FFF';
      171.     ctx3.shadowColor = '#FFF';
      172.     ctx3.shadowBlur = 25;
      173.     // ANIMATION LOOP
      174.     (function loop() {
      175.       ctx2.fillStyle = "rgba(0, 0, 0, .1)";
      176.       ctx2.fillRect(0, 0, c2.width, c2.height);
      177.       //ctx2.clearRect(0, 0, c2.width, c2.height);
      178.       counter += 1;
      179.       if (counter % 15 === 0) {
      180.         rockets.push(new Rocket());
      181.       }
      182.       rockets.forEach((r, i) => {
      183.         r.draw();
      184.         r.update();
      185.         if (r.ySpeed > 0) {
      186.           r.explode();
      187.           rockets.splice(i, 1);
      188.         }
      189.       });
      190.       shards.forEach((s, i) => {
      191.         s.draw();
      192.         s.update();
      193.         if (s.timer >= s.ttl || s.lightness >= 99) {
      194.           ctx3.fillRect(s.target.x, s.target.y, fidelity + 1, fidelity + 1);
      195.           shards.splice(i, 1);
      196.         }
      197.       });
      198.       requestAnimationFrame(loop);
      199.     })();
      200.     // HELPER FUNCTIONS
      201.     const lerp = (a, b, t) => Math.abs(b - a) > 0.1 ? a + t * (b - a) : b;
      202.     function getTarget() {
      203.       if (targets.length > 0) {
      204.         const idx = Math.floor(Math.random() * targets.length);
      205.         let { x, y } = targets[idx];
      206.         targets.splice(idx, 1);
      207.         x += c2.width / 2 - textWidth / 2;
      208.         y += c2.height / 2 - fontSize / 2;
      209.         return { x, y };
      210.       }
      211.     }
      212.   </script>
      213. </body>
      214. </html>
      复制代码
         
                 步骤 1: 页面布局和引入脚本
    • HTML定义了三个canvas元素用于绘制烟花动画。
    • 引入百度统计脚本,用于收集页面访问数据。
    • 步骤 2: 样式设置
    • 页面背景设置为玄色,canvas元素被设置为绝对定位,覆盖整个屏幕。
    • 步骤 3: JavaScript 功能实现

      3.1 获取URL参数
    • GetRequest函数分析当前URL的查询字符串,将参数保存在一个对象中返回。
    • 3.2 定义烟花碎片(Shard)类
    • 每个Shard实例代表烟花爆炸后的一个碎片。
    • 包含位置、颜色、大小、速率等属性。
    • draw方法用于绘制碎片。
    • update方法用于更新碎片的状态和位置,包罗模拟重力影响和向目的位置移动。
    • 3.3 定义火箭(Rocket)类
    • 每个Rocket实例代表一个发射的烟花火箭。
    • 包含位置、速率、颜色等属性。
    • draw方法用于绘制火箭。
    • update方法用于更新火箭的位置,模拟火箭上升。
    • 3.4 初始化和动画循环
    • 初始化:根据屏幕大小调整字体大小,确保“2024新年快乐!”文字适应屏幕宽度,并在canvas上绘制该文字。通过读取文字像素数据来确定烟花爆炸的目的位置。
    • 动画循环:使用requestAnimationFrame循环不绝更新和绘制火箭和碎片,以及检测碎片是否到达目的位置或生命周期竣事。
    • 3.5 辅助函数
    • lerp函数:用于在两个数值之间进行线性插值,资助平滑动画效果。
    • 步骤 4: 实验动画
    • 初始化画布:调整画布大小以适应窗口,绘制“2024新年快乐!”文字,并基于此文字的像素数据确定烟花目的位置。
    • 启动动画循环

      • 每隔一定时间间隔,创建一个新的Rocket实例,模拟火箭发射。
      • 更新每个火箭的位置,当火箭到达一定高度时触发爆炸,天生多个Shard实例。
      • 更新每个Shard的位置,使其朝目的位置移动。
      • 当Shard到达目的位置或生命周期竣事时,从数组中移除。

    • 渲染烟花效果:通过不绝更新canvas上的火箭和碎片位置,以及绘制这些元素,形成动态的烟花效果。碎片终极会根据预设的目的位置排列,形成“2024新年快乐!”的文字形状。
    • 通过这些步骤,代码实现了一个视觉吸引的新年烟花祝福动画,既展示了编程本领,也增加了节日气氛。
    • explode方法在火箭到达顶点时被调用,天生多个Shard实例。


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

民工心事

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表