濮阳杆衣贸易有限公司

主頁 > 知識庫 > 基于HTML代碼實現(xiàn)圖片碎片化加載功能

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

熱門標簽:貴陽ai外呼系統(tǒng) 長春極信防封電銷卡公司 智能電銷機器人廣告語 重慶人工智能電銷機器人報價 強訊外呼系統(tǒng) 電銷外呼線路改不外呼線路 愛巢地圖標注 crm外呼系統(tǒng)好不好 電話機器人批發(fā)

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

 

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

  • 定義 html 結(jié)構(gòu)
  • 拆分圖片
  • 編寫動畫函數(shù)

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

這里只需要一個 canvas 元素就可以了。

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

拆分圖片

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

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 () {
  // 計算每一行,每一列的寬高
  boxWidth = image.width / columns;
  boxHeight = image.height / rows;
  // 循環(huán)渲染
  requestAnimationFrame(animate);
};

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

編寫動畫函數(shù)

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

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

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

function animate() {
  // 隨機渲染某個模塊
  let x = Math.floor(Math.random() * columns);
  let y = Math.floor(Math.random() * rows);
  // 核心
  context.drawImage(
    image,
    x * boxWidth,  // canvas 中橫坐標起始位置
    y * boxHeight, // canvas 中縱坐標起始位置
    boxWidth,      // 畫圖的寬度(小碎片圖像的寬)
    boxHeight,     // 畫圖的高度(小碎片圖像的高)
    x * boxWidth,  // 從大圖的 x 坐標位置開始畫圖
    y * boxHeight, // 從大圖的 y 坐標位置開始畫圖
    boxWidth,      // 從大圖的 x 位置開始,畫多寬(小碎片圖像的寬)
    boxHeight      // 從大圖的 y 位置開始,畫多高(小碎片圖像的高)
  );
  counter++;
  // 如果模塊渲染了 90%,就讓整個圖片顯示出來。
  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, // 橫坐標起始位置
          y * boxHeight, // 縱坐標起始位置
          boxWidth, // 圖像的寬
          boxHeight, // 圖像的高
          x * boxWidth, // 在畫布上放置圖像的 x 坐標位置
          y * boxHeight, // 在畫布上放置圖像的 y 坐標位置
          boxWidth, // 要使用的圖像的寬度
          boxHeight // 要使用的圖像的高度
        );
        counter++;
        if (counter > columns * rows * 0.9) {
          context.drawImage(image, 0, 0);
        } else {
          requestAnimationFrame(animate);
        }
      }
    </script>
  </body>
</html>

總結(jié)

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

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

標簽:保定 內(nèi)蒙古 山南 吳忠 廣安 陜西 清遠 上海

巨人網(wǎng)絡通訊聲明:本文標題《基于HTML代碼實現(xiàn)圖片碎片化加載功能》,本文關(guān)鍵詞  基于,HTML,代碼,實現(xiàn),圖片,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《基于HTML代碼實現(xiàn)圖片碎片化加載功能》相關(guān)的同類信息!
  • 本頁收集關(guān)于基于HTML代碼實現(xiàn)圖片碎片化加載功能的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    巩留县| 莱芜市| 崇明县| 灵璧县| 大余县| 通州区| 岐山县| 治县。| 敦化市| 吴桥县| 阳原县| 久治县| 博野县| 仁寿县| 门源| 响水县| 汕头市| 家居| 临西县| 正阳县| 治多县| 阳高县| 博白县| 吴旗县| 新源县| 罗江县| 桑日县| 上林县| 河南省| 宁阳县| 临西县| 深泽县| 砚山县| 汉川市| 玉龙| 兴山县| 张掖市| 沛县| 光泽县| 治多县| 柏乡县|