乱码乱a∨中文字幕,在线免费激情视频,亚洲欧美久久夜夜潮,国产在线网址

  1. <sub id="hjl7n"></sub>

    1. <sub id="hjl7n"></sub>

      <legend id="hjl7n"></legend>

      當(dāng)前位置:首頁 >  站長 >  編程技術(shù) >  正文

      基于HTML代碼實(shí)現(xiàn)圖片碎片化加載功能

       2020-11-06 16:57  來源: 腳本之家   我來投稿 撤稿糾錯(cuò)

        阿里云優(yōu)惠券 先領(lǐng)券再下單

      這篇文章主要介紹了基于HTML代碼實(shí)現(xiàn)圖片碎片化加載功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

      今天來實(shí)現(xiàn)一個(gè)圖片碎片化加載效果,效果如下:

      我們分為 3 個(gè)步驟來實(shí)現(xiàn):

      定義 html 結(jié)構(gòu)

      拆分圖片

      編寫動畫函數(shù)

      定義html結(jié)構(gòu)

      這里只需要一個(gè) canvas 元素就可以了。

      <html>
        <body>
          <canvas
            id="myCanvas"
            width="900"
            height="600"
            style="background-color: black;"
          ></canvas>
        </body>
      </html>

      拆分圖片

      這個(gè)例子中,我們將圖片按照 10 行 10 列的網(wǎng)格,拆分成 100 個(gè)小碎片,這樣就可以對每一個(gè)小碎片獨(dú)立渲染了。

      let image = new Image();
      image.src ="https://cdn.yinhengli.com/canvas-example.jpeg";

      let boxWidth, boxHeight;
      // 拆分成 10 行,10 列
      let rows = 10,
        columns = 20,
        counter = 0;

      image.onload = function () {
        // 計(jì)算每一行,每一列的寬高
        boxWidth = image.width / columns;
        boxHeight = image.height / rows;
        // 循環(huán)渲染
        requestAnimationFrame(animate);
      };

      requestAnimationFrame :告訴瀏覽器,你希望執(zhí)行一個(gè)動畫,并且要求瀏覽器在下次重繪之前調(diào)用指定的回調(diào)函數(shù)更新動畫。

      編寫動畫函數(shù)

      接下來我們編寫動畫函數(shù),讓瀏覽器在每一次重繪前,隨機(jī)渲染某個(gè)小碎片。

      這里的核心是 context.drawImage 方法。

      let canvas = document.getElementById("myCanvas");
      let context = canvas.getContext("2d");

      function animate() {
        // 隨機(jī)渲染某個(gè)模塊
        let x = Math.floor(Math.random() * columns);
        let y = Math.floor(Math.random() * rows);
        // 核心
        context.drawImage(
          image,
          x * boxWidth,  // canvas 中橫坐標(biāo)起始位置
          y * boxHeight, // canvas 中縱坐標(biāo)起始位置
          boxWidth,      // 畫圖的寬度(小碎片圖像的寬)
          boxHeight,     // 畫圖的高度(小碎片圖像的高)
          x * boxWidth,  // 從大圖的 x 坐標(biāo)位置開始畫圖
          y * boxHeight, // 從大圖的 y 坐標(biāo)位置開始畫圖
          boxWidth,      // 從大圖的 x 位置開始,畫多寬(小碎片圖像的寬)
          boxHeight      // 從大圖的 y 位置開始,畫多高(小碎片圖像的高)
        );
        counter++;
        // 如果模塊渲染了 90%,就讓整個(gè)圖片顯示出來。
        if (counter > columns * rows * 0.9) {
          context.drawImage(image, 0, 0);
        } else {
          requestAnimationFrame(animate);
        }
      }

      完整代碼

      <html>
        <body>
          <canvas
            id="myCanvas"
            width="900"
            height="600"
            style="background-color: black;"
          ></canvas>
          <script>
            let image = new Image();
            image.src ="https://cdn.yinhengli.com/canvas-example.jpeg";

      let canvas = document.getElementById("myCanvas");
            let context = canvas.getContext("2d");
            let boxWidth, boxHeight;
            let rows = 10,
              columns = 20,
              counter = 0;

            image.onload = function () {
              boxWidth = image.width / columns;
              boxHeight = image.height / rows;
              requestAnimationFrame(animate);
            };

            function animate() {
              let x = Math.floor(Math.random() * columns);
              let y = Math.floor(Math.random() * rows);
              context.drawImage(
                image,
                x * boxWidth, // 橫坐標(biāo)起始位置
                y * boxHeight, // 縱坐標(biāo)起始位置
                boxWidth, // 圖像的寬
                boxHeight, // 圖像的高
                x * boxWidth, // 在畫布上放置圖像的 x 坐標(biāo)位置
                y * boxHeight, // 在畫布上放置圖像的 y 坐標(biāo)位置
                boxWidth, // 要使用的圖像的寬度
                boxHeight // 要使用的圖像的高度
              );
              counter++;
              if (counter > columns * rows * 0.9) {
                context.drawImage(image, 0, 0);
              } else {
                requestAnimationFrame(animate);
              }
            }
          </script>
        </body>
      </html>

      總結(jié)

      通過這個(gè) Demo,我們使用了 canvasAPI 實(shí)現(xiàn)了圖片的碎片加載效果,是不是特別簡單!

      到此這篇關(guān)于基于HTML代碼實(shí)現(xiàn)圖片碎片化加載功能的文章就介紹到這了,更多相關(guān)html圖片碎片化加載內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持腳本之家!

      來源:腳本之家

      鏈接:https://www.jb51.net/web/731888.html

      申請創(chuàng)業(yè)報(bào)道,分享創(chuàng)業(yè)好點(diǎn)子。點(diǎn)擊此處,共同探討創(chuàng)業(yè)新機(jī)遇!

      相關(guān)標(biāo)簽
      html

      相關(guān)文章

      熱門排行

      信息推薦