欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

如何用前端html實(shí)現(xiàn)2024煙花效果

 更新時(shí)間:2024年11月27日 10:21:47   作者:小匠開發(fā)鋪  
這篇文章主要給大家介紹了關(guān)于如何用前端html實(shí)現(xiàn)2024煙花效果的相關(guān)資料,文中介紹的是JavaScript代碼定義了煙花碎片和火箭類,通過動(dòng)畫循環(huán)模擬煙花爆炸效果,需要的朋友可以參考下

用HTML、CSS和JavaScript編寫的網(wǎng)頁,主要用于展示“2024新年快樂!”的文字形式煙花效果。下面是對(duì)代碼主要部分的分析:

HTML結(jié)構(gòu)

  • 包含三個(gè)<canvas>元素,用于繪制動(dòng)畫。
  • 引入百度統(tǒng)計(jì)的腳本。

CSS樣式

  • 設(shè)置body的背景為黑色,并使得canvas元素絕對(duì)定位,覆蓋整個(gè)頁面。

JavaScript 功能

百度統(tǒng)計(jì)腳本:頁面開始時(shí)引入了百度統(tǒng)計(jì)的腳本,用于網(wǎng)頁訪問數(shù)據(jù)分析。

獲取URL參數(shù)GetRequest函數(shù)用于解析URL中的查詢字符串參數(shù)。

煙花碎片(Shard)類

每個(gè)Shard代表煙花爆炸后的一個(gè)碎片。

包含碎片的位置、顏色、大小、速度等屬性。

draw方法用于在canvas上繪制碎片。

update方法用于更新碎片的位置和狀態(tài)。

火箭(Rocket)類

表示發(fā)射的煙花火箭。

包含火箭的位置、速度、顏色等屬性。

draw方法用于在canvas上繪制火箭。

update方法用于更新火箭的位置。

explode方法用于模擬火箭爆炸,生成多個(gè)Shard實(shí)例。

初始化和動(dòng)畫循環(huán)

獲取所有canvas元素和對(duì)應(yīng)的2D渲染上下文。

根據(jù)屏幕大小調(diào)整字體大小,以適應(yīng)屏幕寬度,并在一個(gè)canvas上繪制“2024新年快樂!”文字。

通過讀取繪制的文字的像素?cái)?shù)據(jù),確定煙花爆炸的目標(biāo)位置。

使用requestAnimationFrame創(chuàng)建動(dòng)畫循環(huán),不斷更新和繪制火箭和碎片,模擬煙花效果。

輔助函數(shù)

lerp(線性插值函數(shù)):用于平滑地在兩個(gè)值之間插值,常用于動(dòng)畫效果中。

執(zhí)行流程

頁面加載完成后,動(dòng)畫循環(huán)開始運(yùn)行。

每隔一定幀數(shù),生成一個(gè)新的Rocket實(shí)例,模擬火箭發(fā)射。

當(dāng)火箭達(dá)到一定高度后,調(diào)用explode方法,生成多個(gè)Shard實(shí)例,模擬煙花爆炸。

碎片根據(jù)預(yù)設(shè)的目標(biāo)位置移動(dòng),最終形成“2024新年快樂!”的文字形狀。

<!DOCTYPE html>
<html lang="en">
<script>
  var _hmt = _hmt || [];
  (function () {
    var hm = document.createElement("script");
    hm.src = "https://#/hm.js?c923daf3182a4b0ce01878475080aadc";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
  })();
</script>

<head>
  <meta charset="UTF-8">
  <title>2024,新年快樂!</title>
</head>
<style>
  body {
    margin: 0;
    overflow: hidden;
    background: black;
  }

  canvas {
    position: absolute;
  }
</style>

<body>

  <canvas></canvas>
  <canvas></canvas>
  <canvas></canvas>

  <script>

    function GetRequest() {
      var url = decodeURI(location.search); //獲取url中"?"符后的字串
      var theRequest = new Object();
      if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        strs = str.split("&");
        for (var i = 0; i < strs.length; i++) {
          theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
        }
      }
      return theRequest;
    }
    class Shard {
      constructor(x, y, hue) {
        this.x = x;
        this.y = y;
        this.hue = hue;
        this.lightness = 50;
        this.size = 15 + Math.random() * 10;
        const angle = Math.random() * 2 * Math.PI;
        const blastSpeed = 1 + Math.random() * 6;
        this.xSpeed = Math.cos(angle) * blastSpeed;
        this.ySpeed = Math.sin(angle) * blastSpeed;
        this.target = getTarget();
        this.ttl = 100;
        this.timer = 0;
      }
      draw() {
        ctx2.fillStyle = `hsl(${this.hue}, 100%, ${this.lightness}%)`;
        ctx2.beginPath();
        ctx2.arc(this.x, this.y, this.size, 0, 2 * Math.PI);
        ctx2.closePath();
        ctx2.fill();
      }
      update() {
        if (this.target) {
          const dx = this.target.x - this.x;
          const dy = this.target.y - this.y;
          const dist = Math.sqrt(dx * dx + dy * dy);
          const a = Math.atan2(dy, dx);
          const tx = Math.cos(a) * 5;
          const ty = Math.sin(a) * 5;
          this.size = lerp(this.size, 1.5, 0.05);

          if (dist < 5) {
            this.lightness = lerp(this.lightness, 100, 0.01);
            this.xSpeed = this.ySpeed = 0;
            this.x = lerp(this.x, this.target.x + fidelity / 2, 0.05);
            this.y = lerp(this.y, this.target.y + fidelity / 2, 0.05);
            this.timer += 1;
          } else
            if (dist < 10) {
              this.lightness = lerp(this.lightness, 100, 0.01);
              this.xSpeed = lerp(this.xSpeed, tx, 0.1);
              this.ySpeed = lerp(this.ySpeed, ty, 0.1);
              this.timer += 1;
            } else {
              this.xSpeed = lerp(this.xSpeed, tx, 0.02);
              this.ySpeed = lerp(this.ySpeed, ty, 0.02);
            }
        } else {
          this.ySpeed += 0.05;
          //this.xSpeed = lerp(this.xSpeed, 0, 0.1);
          this.size = lerp(this.size, 1, 0.05);

          if (this.y > c2.height) {
            shards.forEach((shard, idx) => {
              if (shard === this) {
                shards.splice(idx, 1);
              }
            });
          }
        }
        this.x = this.x + this.xSpeed;
        this.y = this.y + this.ySpeed;
      }
    }

    class Rocket {
      constructor() {
        const quarterW = c2.width / 4;
        this.x = quarterW + Math.random() * (c2.width - quarterW);
        this.y = c2.height - 15;
        this.angle = Math.random() * Math.PI / 4 - Math.PI / 6;
        this.blastSpeed = 6 + Math.random() * 7;
        this.shardCount = 15 + Math.floor(Math.random() * 15);
        this.xSpeed = Math.sin(this.angle) * this.blastSpeed;
        this.ySpeed = -Math.cos(this.angle) * this.blastSpeed;
        this.hue = Math.floor(Math.random() * 360);
        this.trail = [];
      }
      draw() {
        ctx2.save();
        ctx2.translate(this.x, this.y);
        ctx2.rotate(Math.atan2(this.ySpeed, this.xSpeed) + Math.PI / 2);
        ctx2.fillStyle = `hsl(${this.hue}, 100%, 50%)`;
        ctx2.fillRect(0, 0, 5, 15);
        ctx2.restore();
      }
      update() {
        this.x = this.x + this.xSpeed;
        this.y = this.y + this.ySpeed;
        this.ySpeed += 0.1;
      }

      explode() {
        for (let i = 0; i < 70; i++) {
          shards.push(new Shard(this.x, this.y, this.hue));
        }
      }
    }

    console.log(GetRequest('val').val)
    // INITIALIZATION
    const [c1, c2, c3] = document.querySelectorAll('canvas');
    const [ctx1, ctx2, ctx3] = [c1, c2, c3].map(c => c.getContext('2d'));
    let fontSize = 200;
    const rockets = [];
    const shards = [];
    const targets = [];
    const fidelity = 3;
    let counter = 0;
    c2.width = c3.width = window.innerWidth;
    c2.height = c3.height = window.innerHeight;
    ctx1.fillStyle = '#000';
    const text = '2024新年快樂!'
    let textWidth = 99999999;

    while (textWidth > window.innerWidth) {
      ctx1.font = `900 ${fontSize--}px Arial`;
      textWidth = ctx1.measureText(text).width;
    }

    c1.width = textWidth;
    c1.height = fontSize * 1.5;
    ctx1.font = `900 ${fontSize}px Arial`;
    ctx1.fillText(text, 0, fontSize);
    const imgData = ctx1.getImageData(0, 0, c1.width, c1.height);
    for (let i = 0, max = imgData.data.length; i < max; i += 4) {
      const alpha = imgData.data[i + 3];
      const x = Math.floor(i / 4) % imgData.width;
      const y = Math.floor(i / 4 / imgData.width);

      if (alpha && x % fidelity === 0 && y % fidelity === 0) {
        targets.push({ x, y });
      }
    }

    ctx3.fillStyle = '#FFF';
    ctx3.shadowColor = '#FFF';
    ctx3.shadowBlur = 25;

    // ANIMATION LOOP
    (function loop() {
      ctx2.fillStyle = "rgba(0, 0, 0, .1)";
      ctx2.fillRect(0, 0, c2.width, c2.height);
      //ctx2.clearRect(0, 0, c2.width, c2.height);
      counter += 1;

      if (counter % 15 === 0) {
        rockets.push(new Rocket());
      }
      rockets.forEach((r, i) => {
        r.draw();
        r.update();
        if (r.ySpeed > 0) {
          r.explode();
          rockets.splice(i, 1);
        }
      });

      shards.forEach((s, i) => {
        s.draw();
        s.update();

        if (s.timer >= s.ttl || s.lightness >= 99) {
          ctx3.fillRect(s.target.x, s.target.y, fidelity + 1, fidelity + 1);
          shards.splice(i, 1);
        }
      });

      requestAnimationFrame(loop);
    })();

    // HELPER FUNCTIONS
    const lerp = (a, b, t) => Math.abs(b - a) > 0.1 ? a + t * (b - a) : b;

    function getTarget() {
      if (targets.length > 0) {
        const idx = Math.floor(Math.random() * targets.length);
        let { x, y } = targets[idx];
        targets.splice(idx, 1);

        x += c2.width / 2 - textWidth / 2;
        y += c2.height / 2 - fontSize / 2;

        return { x, y };
      }
    }
  </script>

</body>

</html>

步驟 1: 頁面結(jié)構(gòu)和引入腳本

  • HTML定義了三個(gè)canvas元素用于繪制煙花動(dòng)畫。
  • 引入百度統(tǒng)計(jì)腳本,用于收集頁面訪問數(shù)據(jù)。

步驟 2: 樣式設(shè)置

  • 頁面背景設(shè)置為黑色,canvas元素被設(shè)置為絕對(duì)定位,覆蓋整個(gè)屏幕。

步驟 3: JavaScript 功能實(shí)現(xiàn)

3.1 獲取URL參數(shù)

  • GetRequest函數(shù)解析當(dāng)前URL的查詢字符串,將參數(shù)保存在一個(gè)對(duì)象中返回。

3.2 定義煙花碎片(Shard)類

  • 每個(gè)Shard實(shí)例代表煙花爆炸后的一個(gè)碎片。
  • 包含位置、顏色、大小、速度等屬性。
  • draw方法用于繪制碎片。
  • update方法用于更新碎片的狀態(tài)和位置,包括模擬重力影響和向目標(biāo)位置移動(dòng)。

3.3 定義火箭(Rocket)類

  • 每個(gè)Rocket實(shí)例代表一個(gè)發(fā)射的煙花火箭。
  • 包含位置、速度、顏色等屬性。
  • draw方法用于繪制火箭。
  • update方法用于更新火箭的位置,模擬火箭上升。

3.4 初始化和動(dòng)畫循環(huán)

  • 初始化:根據(jù)屏幕大小調(diào)整字體大小,確保“2024新年快樂!”文字適應(yīng)屏幕寬度,并在canvas上繪制該文字。通過讀取文字像素?cái)?shù)據(jù)來確定煙花爆炸的目標(biāo)位置。
  • 動(dòng)畫循環(huán):使用requestAnimationFrame循環(huán)不斷更新和繪制火箭和碎片,以及檢測(cè)碎片是否達(dá)到目標(biāo)位置或生命周期結(jié)束。

3.5 輔助函數(shù)

  • lerp函數(shù):用于在兩個(gè)數(shù)值之間進(jìn)行線性插值,幫助平滑動(dòng)畫效果。

步驟 4: 執(zhí)行動(dòng)畫

  • 初始化畫布:調(diào)整畫布大小以適應(yīng)窗口,繪制“2024新年快樂!”文字,并基于此文字的像素?cái)?shù)據(jù)確定煙花目標(biāo)位置。

  • 啟動(dòng)動(dòng)畫循環(huán)

    • 每隔一定時(shí)間間隔,創(chuàng)建一個(gè)新的Rocket實(shí)例,模擬火箭發(fā)射。
    • 更新每個(gè)火箭的位置,當(dāng)火箭達(dá)到一定高度時(shí)觸發(fā)爆炸,生成多個(gè)Shard實(shí)例。
    • 更新每個(gè)Shard的位置,使其朝目標(biāo)位置移動(dòng)。
    • 當(dāng)Shard到達(dá)目標(biāo)位置或生命周期結(jié)束時(shí),從數(shù)組中移除。
  • 渲染煙花效果:通過不斷更新canvas上的火箭和碎片位置,以及繪制這些元素,形成動(dòng)態(tài)的煙花效果。碎片最終會(huì)根據(jù)預(yù)設(shè)的目標(biāo)位置排列,形成“2024新年快樂!”的文字形狀。

  • 通過這些步驟,代碼實(shí)現(xiàn)了一個(gè)視覺吸引的新年煙花祝福動(dòng)畫,既展示了編程技巧,也增添了節(jié)日氣氛。

  • explode方法在火箭達(dá)到頂點(diǎn)時(shí)被調(diào)用,生成多個(gè)Shard實(shí)例。

總結(jié) 

到此這篇關(guān)于如何用前端html實(shí)現(xiàn)2024煙花效果的文章就介紹到這了,更多相關(guān)前端html實(shí)現(xiàn)2024煙花內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論