跨年夜,想请你看一场烟花秀!

news2024/9/28 15:25:19

代码分享地址:

链接:https://pan.baidu.com/s/1Cu_lKYfAlMBDttSzhVXPuQ 提取码:2ocd

代码效果展示:

在这里插入图片描述

源代码分享如下:

<!--
 * @Author: Xiao Wang
 * @Date: 2022-12-30 14:26
 * @Description: 
-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  <head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
    <style>
      body {
        margin: 0;
        padding: 0;
        overflow: hidden;
      }

      .city {
        width: 100%;
        position: fixed;
        bottom: 0px;
        z-index: 100;
        z-index: 10;
      }

      .city img {
        width: 100%;
      }

      #labels,
      #extra {
        /* color: rgb(106, 17, 11); */

        color: #daf6ff;
        text-shadow: 0 0 20px #0aafe6, 0 0 20px rgba(10, 175, 230, 0);
        font-weight: bold;
        font-size: 2.8rem;
        position: absolute;
        width: 100%;
        text-align: center;
        opacity: 0;
        transition: all 0.5s ease-in-out;
        z-index: 100;
        top: 30%;
      }
      #labels.hide {
        opacity: 0;
        transform: scale(1);
      }
      #labels.show,
      #extra.show {
        opacity: 1;
        z-index: 100;
        transform: scale(1.3);
      }
    </style>
    <title>Happy New Year</title>
  </head>
  <body onselectstart="return false">
    <canvas id="cas" style="background-color: rgba(0, 5, 24, 1)"
      >浏览器不支持canvas</canvas
    >
    <div id="labels"></div>
    <div class="city"><img src="./city_2.png" alt="" /></div>
    <img src="./moon.png" alt="" id="moon" style="visibility: hidden" />
    <div id="labels"></div>

    <script>
      let __audio = document.createElement("audio");
      //修改音乐在这里
      __audio.src = "http://music.163.com/song/media/outer/url?id=28828078.mp3";
      __audio.loop = true;
      __audio.volume = 0.6;
      window.onclick = function () {
        __audio.play();
      };
    </script>
    <script>
      let starCount = 1500;
      let starArr = new Array();

      //想要说的话在这里
      const my_labels = [
        "新的一年健健康康、平安喜乐!",
        "日月有情迎元旦,山川无阻庆新年",
      ];
      const shapes = [
        // "img$http://www.html5tricks.com/wp-content/uploads/2015/03/html5-canvas-drag-tree.png"
      ];
      let canvas = document.getElementById("cas");
      let ocas = document.createElement("canvas");
      let octx = ocas.getContext("2d");
      let ctx = canvas.getContext("2d");
      ocas.width = canvas.width = window.innerWidth;
      ocas.height = canvas.height = window.innerHeight;
      let bigbooms = [];

      window.onload = function () {
        initAnimate();

        const labels = document.getElementById("labels");
        for (let i = 0; i < my_labels.length; i++) {
          setTimeout(() => {
            // labels.className = "hide";
            labels.innerHTML = my_labels[i];
            labels.className = "show";
            if (i < my_labels.length - 1) {
              setTimeout(() => {
                labels.className = "hide";
              }, 3000);
            }
          }, 3500 * (i + 1));
        }

        for (let i = 0; i < starCount; i++) {
          let star = new StarMore();
          star.randomColor();
          starArr.push(star);
        }

        showStars();
      };

      function initAnimate() {
        drawBg();

        lastTime = new Date();
        animate();
      }

      let lastTime;
      function animate() {
        ctx.save();
        ctx.globalCompositeOperation = "destination-out";
        ctx.globalAlpha = 0.1;
        ctx.fillRect(0, 0, canvas.width, canvas.height);
        ctx.restore();

        let newTime = new Date();
        if (newTime - lastTime > 200 + (window.innerHeight - 767) / 2) {
          let random = Math.random() * 100 > 2 ? true : false;
          let x = getRandom(canvas.width / 5, (canvas.width * 4) / 5);
          let y = getRandom(50, 200);
          if (random) {
            let bigboom = new Boom(
              getRandom(canvas.width / 3, (canvas.width * 2) / 3),
              2,
              "#FFF",
              { x: x, y: y }
            );
            bigbooms.push(bigboom);
          } else {
            let bigboom = new Boom(
              getRandom(canvas.width / 3, (canvas.width * 2) / 3),
              2,
              "#FFF",
              {
                x: canvas.width / 2,
                y: 200,
              },
              shapes[parseInt(getRandom(0, shapes.length))]
            );
            bigbooms.push(bigboom);
          }
          lastTime = newTime;
        }

        stars.foreach(function () {
          this.paint();
        });

        drawMoon();

        bigbooms.foreach(function (index) {
          let that = this;
          if (!this.dead) {
            this._move();
            this._drawLight();
          } else {
            this.booms.foreach(function (index) {
              if (!this.dead) {
                this.moveTo(index);
              } else if (index === that.booms.length - 1) {
                bigbooms.splice(bigbooms.indexOf(that), 1);
              }
            });
          }
        });

        raf(animate);
      }

      function drawMoon() {
        let moon = document.getElementById("moon");
        let centerX = canvas.width - 200,
          centerY = 100,
          width = 80;
        if (moon.complete) {
          ctx.drawImage(moon, centerX, centerY, width, width);
        } else {
          moon.onload = function () {
            ctx.drawImage(moon, centerX, centerY, width, width);
          };
        }
        let index = 0;
        for (let i = 0; i < 10; i++) {
          ctx.save();
          ctx.beginPath();
          ctx.arc(
            centerX + width / 2,
            centerY + width / 2,
            width / 2 + index,
            0,
            2 * Math.PI
          );
          ctx.fillStyle = "rgba(240,219,120,0.005)";
          index += 2;
          ctx.fill();
          ctx.restore();
        }
      }

      Array.prototype.foreach = function (callback) {
        for (let i = 0; i < this.length; i++) {
          if (this[i] !== null) callback.apply(this[i], [i]);
        }
      };

      let raf =
        window.requestAnimationFrame ||
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        window.oRequestAnimationFrame ||
        window.msRequestAnimationFrame ||
        function (callback) {
          window.setTimeout(callback, 1000 / 10);
        };

      canvas.onclick = function () {
        let x = event.clientX;
        let y = event.clientY;
        let bigboom = new Boom(
          getRandom(canvas.width / 3, (canvas.width * 2) / 3),
          2,
          "#FFF",
          { x: x, y: y }
        );
        bigbooms.push(bigboom);
      };

      let Boom = function (x, r, c, boomArea, shape) {
        this.booms = [];
        this.x = x;
        this.y = canvas.height + r;
        this.r = r;
        this.c = c;
        this.shape = shape || false;
        this.boomArea = boomArea;
        this.theta = 0;
        this.dead = false;
        this.ba = parseInt(getRandom(80, 200));

        let audio = document.getElementsByTagName("audio");
        for (let i = 0; i < audio.length; i++) {
          if (
            audio[i].src.indexOf("shotfire") >= 0 &&
            (audio[i].paused || audio[i].ended)
          ) {
            // audio[i].play();
            break;
          }
        }
      };
      Boom.prototype = {
        _paint: function () {
          ctx.save();
          ctx.beginPath();
          ctx.arc(this.x, this.y, this.r, 0, 2 * Math.PI);
          ctx.fillStyle = this.c;
          ctx.fill();
          ctx.restore();
        },
        _move: function () {
          let dx = this.boomArea.x - this.x,
            dy = this.boomArea.y - this.y;
          this.x = this.x + dx * 0.01;
          this.y = this.y + dy * 0.01;

          if (Math.abs(dx) <= this.ba && Math.abs(dy) <= this.ba) {
            if (this.shape) {
              this._shapBoom();
            } else this._boom();
            this.dead = true;
          } else {
            this._paint();
          }
        },
        _drawLight: function () {
          ctx.save();
          ctx.fillStyle = "rgba(255,228,150,0.3)";
          ctx.beginPath();
          ctx.arc(
            this.x,
            this.y,
            this.r + 3 * Math.random() + 1,
            0,
            2 * Math.PI
          );
          ctx.fill();
          ctx.restore();
        },
        _boom: function () {
          let fragNum = getRandom(100, 300);
          let style = getRandom(0, 10) >= 5 ? 1 : 2;
          let color;
          if (style === 1) {
            color = {
              a: parseInt(getRandom(128, 255)),
              b: parseInt(getRandom(128, 255)),
              c: parseInt(getRandom(128, 255)),
            };
          }

          let fanwei = fragNum;
          let audio = document.getElementsByTagName("audio");
          for (let i = 0; i < audio.length; i++) {
            if (
              audio[i].src.indexOf("boom") >= 0 &&
              (audio[i].paused || audio[i].ended)
            ) {
              // audio[i].play();
              break;
            }
          }
          for (let i = 0; i < fragNum; i++) {
            if (style === 2) {
              color = {
                a: parseInt(getRandom(128, 255)),
                b: parseInt(getRandom(128, 255)),
                c: parseInt(getRandom(128, 255)),
              };
            }
            let a = getRandom(-Math.PI, Math.PI);
            let x = getRandom(0, fanwei) * Math.cos(a) + this.x;
            let y = getRandom(0, fanwei) * Math.sin(a) + this.y;
            let radius = getRandom(0, 2);
            let frag = new Frag(this.x, this.y, radius, color, x, y);
            this.booms.push(frag);
          }
        },
        _shapBoom: function () {
          let that = this;
          putValue(ocas, octx, this.shape, 5, function (dots) {
            let dx = canvas.width / 2 - that.x;
            let dy = canvas.height / 2 - that.y;
            for (let i = 0; i < dots.length; i++) {
              color = { a: dots[i].a, b: dots[i].b, c: dots[i].c };
              let x = dots[i].x;
              let y = dots[i].y;
              let radius = 1;
              let frag = new Frag(
                that.x,
                that.y,
                radius,
                color,
                x - dx,
                y - dy
              );
              that.booms.push(frag);
            }
          });
        },
      };

      function putValue(canvas, context, ele, dr, callback) {
        context.clearRect(0, 0, canvas.width, canvas.height);
        let img = new Image();
        if (ele.indexOf("img") >= 0) {
          let _src = ele.split("$")[1];
          img.src = _src;
          imgload(img, function () {
            context.drawImage(
              img,
              canvas.width / 2 - img.width / 2,
              canvas.height / 2 - img.width / 2
            );
            console.log("dots");
            dots = getimgData(canvas, context, dr);
            callback(dots);
          });
        } else {
          let text = ele;
          context.save();
          let fontSize = 128;
          context.font = fontSize + "px 宋体 bold";
          context.textAlign = "center";
          context.textBaseline = "middle";
          context.fillStyle =
            "rgba(" +
            parseInt(getRandom(128, 255)) +
            "," +
            parseInt(getRandom(128, 255)) +
            "," +
            parseInt(getRandom(128, 255)) +
            " , 1)";
          context.fillText(text, canvas.width / 2, canvas.height / 2);
          context.restore();
          dots = getimgData(canvas, context, dr);
          callback(dots);
        }
      }

      function imgload(img, callback) {
        if (img.complete) {
          callback.call(img);
        } else {
          img.onload = function () {
            callback.call(this);
          };
        }
      }

      function getimgData(canvas, context, dr) {
        let imgData = context.getImageData(0, 0, canvas.width, canvas.height);
        context.clearRect(0, 0, canvas.width, canvas.height);
        let dots = [];
        for (let x = 0; x < imgData.width; x += dr) {
          for (let y = 0; y < imgData.height; y += dr) {
            let i = (y * imgData.width + x) * 4;
            if (imgData.data[i + 3] > 128) {
              let dot = {
                x: x,
                y: y,
                a: imgData.data[i],
                b: imgData.data[i + 1],
                c: imgData.data[i + 2],
              };
              dots.push(dot);
            }
          }
        }
        return dots;
      }

      function getRandom(a, b) {
        return Math.random() * (b - a) + a;
      }

      let maxRadius = 1,
        stars = [];
      function drawBg() {
        for (let i = 0; i < 100; i++) {
          let r = Math.random() * maxRadius;
          let x = Math.random() * canvas.width;
          let y = Math.random() * 2 * canvas.height - canvas.height;
          let star = new Star(x, y, r);
          stars.push(star);
          star.paint();
        }
      }

      let Star = function (x, y, r) {
        this.x = x;
        this.y = y;
        this.r = r;
      };
      Star.prototype = {
        paint: function () {
          ctx.save();
          ctx.beginPath();
          ctx.arc(this.x, this.y, this.r, 0, 2 * Math.PI);
          ctx.fillStyle = "rgba(255,255,255," + this.r + ")";
          ctx.fill();
          ctx.restore();
        },
      };

      let focallength = 250;
      let Frag = function (centerX, centerY, radius, color, tx, ty) {
        this.tx = tx;
        this.ty = ty;
        this.x = centerX;
        this.y = centerY;
        this.dead = false;
        this.centerX = centerX;
        this.centerY = centerY;
        this.radius = radius;
        this.color = color;
      };

      Frag.prototype = {
        paint: function () {
          // ctx.beginPath();
          // ctx.arc(this.x , this.y , this.radius , 0 , 2*Math.PI);
          ctx.fillStyle =
            "rgba(" +
            this.color.a +
            "," +
            this.color.b +
            "," +
            this.color.c +
            ",1)";
          ctx.fillRect(
            this.x - this.radius,
            this.y - this.radius,
            this.radius * 2,
            this.radius * 2
          );
        },
        moveTo: function (index) {
          this.ty = this.ty + 0.3;
          let dx = this.tx - this.x,
            dy = this.ty - this.y;
          this.x = Math.abs(dx) < 0.1 ? this.tx : this.x + dx * 0.1;
          this.y = Math.abs(dy) < 0.1 ? this.ty : this.y + dy * 0.1;
          if (dx === 0 && Math.abs(dy) <= 80) {
            this.dead = true;
          }
          this.paint();
        },
      };

      let StarMore = function () {
        this.x = window.innerWidth * Math.random();
        this.y = 5000 * Math.random();
        this.text = ".";
        this.color = "white";
        this.randomColor = function () {
          let _r = Math.random();
          if (_r < 0.5) {
            this.color = "#333";
          } else {
            this.color = "white";
          }
        };
      };
      function showStars() {
        let canvas = document.getElementById("cas");
        context = canvas.getContext("2d");

        for (let n = 0; n < starCount; n++) {
          starArr[n].randomColor();
          context.fillStyle = starArr[n].color;
          context.fillText(starArr[n].text, starArr[n].x, starArr[n].y);
        }

        setTimeout("showStars()", 100);
      }

      // 代码参考来源:https://github.com/AndersonHJB/YearTiger2022
    </script>
  </body>
</html>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/130618.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

剑指 Offer 20. 表示数值的字符串

题目 请实现一个函数用来判断字符串是否表示数值&#xff08;包括整数和小数&#xff09;。 数值&#xff08;按顺序&#xff09;可以分成以下几个部分&#xff1a; 若干空格 一个 小数 或者 整数 &#xff08;可选&#xff09;一个 ‘e’ 或 ‘E’ &#xff0c;后面跟着一个…

canopen11-sdo-2b写入命令

源码 1、SDO介绍 就对象而言,主机要访问节点词典的数据,因此主机是client客户端,节点是server服务器。上传与下载是对服务器来说的(这点和常识有点不太一样)。因此,上传指的是服务器发送数据给客户端,下载是客户端给服务器数据。 我们这里要用主机访问节点服务器2000位…

Go 语言从入门到实战

《Go 语言从入门到实战》 的学习笔记&#xff0c;欢迎阅读斧正。感觉该专栏整体来说对有些后端编程经验的来说比无后端编程经验的人更友好。 数据类型 运算符 算数运算符 比较运算符 用 比较数组 相同维数切含有相同个数元素的数组才可以比较&#xff0c;每个元素都相同的才…

四旋翼无人机学习第18节--cadence的bom表、网表导出,PCB板创建,层叠设置

文章目录1 bom表导出2 网表导出3 PCB板创建4 PCB板文件重要设置5 层叠设置1 bom表导出 1、选择DSN文件&#xff0c;之后依次点击Tools&#xff0c;Bill of Materials。 2、当然&#xff0c;也可以点击图标进行bom导出。 3、下图标出的地方可以自行修改&#xff0c;最后点击O…

设计师必须知道的 5个设计灵感网站

设计没灵感&#xff0c;一定要上这5个网站&#xff0c;设计师每天必逛&#xff0c;建议收藏&#xff01; 设计素材免费下载&#xff1a; https://www.sucai999.com/?vNTYwNDUx 国内灵感网 1、设计之窗 http://www.333cn.com/ 设计之窗是一个设计师分享作品及备案的网站&…

【Ctfer训练计划】——命令执行的解题技巧(持续更新中)

作者名&#xff1a;Demo不是emo 主页面链接&#xff1a;主页传送门创作初心&#xff1a;舞台再大&#xff0c;你不上台&#xff0c;永远是观众&#xff0c;没人会关心你努不努力&#xff0c;摔的痛不痛&#xff0c;他们只会看你最后站在什么位置&#xff0c;然后羡慕或鄙夷座右…

什么是软考?软考有什么作用?

一、软考是什么&#xff1f; 1.软考介绍 计算机技术与软件专业技术资格&#xff08;水平&#xff09;考试是由国家人力资源和社会保障部、工业和信息化部领导的国家级考试&#xff0c;其目的是&#xff0c;科学、公正地对全国计算机与软件专业技术人员进行职业资格、专业技术…

保姆教程系列三、Redis高可用(Cluster集群模式)

系列文章目录 &#xff01;&#xff01;&#xff01;是的没错&#xff0c;胖友们&#xff0c;保姆教程系列又更新了&#xff01;&#xff01;&#xff01; 保姆教程系列一、Redis部署 so easy 保姆教程系列二、Redis高可用&#xff08;主从同步哨兵模式&#xff09; 保姆教程系…

java开发的医院体检预约系统

简介 体检项目预约网站&#xff0c;普通用户注册登录可以网上预约体检项目&#xff0c;经过后台人员审核后可以去体检。用户还可以记录自己的身体指标下载体检报个&#xff0c;查看医嘱等。医院后台可以进行权限管理&#xff0c;实现多角色管理后台的其他业务等&#xff0c;实…

前端—化繁为简

化繁为简 HTML5要的就是简单、避免不必要的复杂性。HTML5的口号是“简单至上&#xff0c;尽可能简化”。因此&#xff0c;HTML5做了以下改进&#xff1a; 以浏览器原生能力替代复杂的JavaScript代码。 新的简化的DOCTYPE。 新的简化的字符集声明。 简单而强大的HTML5API。 我…

【 shell 编程 】第2篇 判断

判断 文章目录判断一、条件测试二、流程控制1.单分支结构2.双分支结构3.多分支结构4.嵌套结构三、匹配模式一、条件测试 格式&#xff1a; 格式1&#xff1a;test 条件表达式 格式2&#xff1a;[ 条件表达式 ] 格式3&#xff1a;[[ 条件表达式 ]] 注意&#xff1a;[] 中的条件…

电气器件系列三十五:开关电源选型实例

开关模式电源&#xff08;Switch Mode Power Supply&#xff0c;简称SMPS&#xff09;&#xff0c;又称交换式电源、开关变换器&#xff0c;是一种高频化电能转换装置&#xff0c;是电源供应器的一种。其功能是将一个位准的电压&#xff0c;透过不同形式的架构转换为用户端所需…

c++11 标准模板(STL)(std::deque)(三)

定义于头文件 <deque> std::deque 赋值给容器 std::deque<T,Allocator>::operator deque& operator( const deque& other ); (1) deque& operator( deque&& other ); (2)(C11 起) (C17 前) deque& operator( deque&& other ) no…

Docker的基本使用

文章目录1.初识Docker1.1 为什么会有docker出现&#xff1f;1.2 docker的理念1.3 Docker和虚拟机的区别1.4 Docker的基本组成(docker的三要素)1.4.1 镜像1.4.2 容器1.4.3 仓库1.5 Docker的架构2.Docker的安装3.常用命令3.1 帮助启动类命令3.2 镜像命令3.3 容器命令1.初识Docker…

使用Lua Script实现不同的限流算法

文章目录Redis中执行Lua ScriptRedis中Debug Lua Script固定窗口代码关键部分解释验证滑动窗口令牌桶漏桶参考资料Redis中执行Lua Script redis-cli --eval /tmp/script.lua mykey somekey , arg1 arg2特别注意&#xff1a;key和arg之间是空格逗号空格&#xff0c;否则脚本调用…

API网关在API安全性中的作用

从单一应用程序切换到微服务时&#xff0c;客户端的行为不能与客户端具有该应用程序的一个入口点的行为相同。简单来说就是微服务上的某一部分功能与单独实现该应用程序时存在不同。 目前在使用微服务时&#xff0c;客户端必须处理微服务体系结构带来的所有复杂性&#xff0c;…

一个月裂变50多万人?商城引流模式玩法——分享购

随着经济进入转型的关键期&#xff0c;零售行业的营销模式正在发生转变&#xff0c;以消费者为中心驱动营销数字化成为当下企业的共识。 新零售时代数字化的核心是数据能力的全面升级&#xff0c;企业通过数字化协同能力整合线上线下全域消费场景&#xff0c;赋能消费者深度洞…

JDK16及其以后的版本Maven打包成可运行的程序的方法

Java应用的分发一直是一个比较麻烦的问题。这是因为Java应用的运行需要虚拟机的支持&#xff0c;仅有Java应用打包的JAR文件是不够的&#xff0c;目标机器还需要安装版本匹配的JDK或JRE。随着云原生和容器化技术的流行&#xff0c;Java应用可以选择以容器镜像的形式来打包和分发…

以太网,拥塞控制与 AQM

拥塞的表现是冲突&#xff0c;大量消费者挤兑有限资源&#xff0c;即拥塞&#xff0c;在一个绝对没有冲突的系统里&#xff0c;自然就没有拥塞。而冲突的来源有两个&#xff0c;一个是无序&#xff0c;一个是贪婪。 仅引入秩序不能解决冲突&#xff0c;但可减少冲突损耗&#…

JavaScript高级 浏览器的渲染原理

浏览器的渲染原理1. 网页的解析过程2. 浏览器内核3. 浏览器渲染过程1. HTML解析2. 生成CSS规则3. 构建Render Tree4. 布局&#xff08;layout&#xff09;5. 绘制&#xff08;Paint&#xff09;4. 回流和重绘解析1. 回流2. 重绘3. web 性能优化4. 合成和性能优化5. 浏览器遇到 …