[前端] 基于HTML代码实现图片碎片化加载功能

2164 0
王子 2022-10-19 15:52:25 | 显示全部楼层 |阅读模式
今天来实现一个图片碎片化加载效果,效果如下:


我们分为 3 个步骤来实现:
    定义 html 结构拆分图片编写动画函数
定义html结构
这里只需要一个 canvas 元素就可以了。
  1. <html>
  2.   <body>
  3.     <canvas
  4.       id="myCanvas"
  5.       width="900"
  6.       height="600"
  7.       style="background-color: black;"
  8.     ></canvas>
  9.   </body>
  10. </html>
复制代码
拆分图片
这个例子中,我们将图片按照 10 行 10 列的网格,拆分成 100 个小碎片,这样就可以对每一个小碎片独立渲染了。
  1. let image = new Image();
  2. image.src = "https://cdn.yinhengli.com/canvas-example.jpeg";
  3. let boxWidth, boxHeight;
  4. // 拆分成 10 行,10 列
  5. let rows = 10,
  6.   columns = 20,
  7.   counter = 0;
  8. image.onload = function () {
  9.   // 计算每一行,每一列的宽高
  10.   boxWidth = image.width / columns;
  11.   boxHeight = image.height / rows;
  12.   // 循环渲染
  13.   requestAnimationFrame(animate);
  14. };
复制代码
requestAnimationFrame :告诉浏览器,你希望执行一个动画,并且要求浏览器在下次重绘之前调用指定的回调函数更新动画。
编写动画函数
接下来我们编写动画函数,让浏览器在每一次重绘前,随机渲染某个小碎片。
这里的核心是 context.drawImage 方法。
  1. let canvas = document.getElementById("myCanvas");
  2. let context = canvas.getContext("2d");
  3. function animate() {
  4.   // 随机渲染某个模块
  5.   let x = Math.floor(Math.random() * columns);
  6.   let y = Math.floor(Math.random() * rows);
  7.   // 核心
  8.   context.drawImage(
  9.     image,
  10.     x * boxWidth,  // canvas 中横坐标起始位置
  11.     y * boxHeight, // canvas 中纵坐标起始位置
  12.     boxWidth,      // 画图的宽度(小碎片图像的宽)
  13.     boxHeight,     // 画图的高度(小碎片图像的高)
  14.     x * boxWidth,  // 从大图的 x 坐标位置开始画图
  15.     y * boxHeight, // 从大图的 y 坐标位置开始画图
  16.     boxWidth,      // 从大图的 x 位置开始,画多宽(小碎片图像的宽)
  17.     boxHeight      // 从大图的 y 位置开始,画多高(小碎片图像的高)
  18.   );
  19.   counter++;
  20.   // 如果模块渲染了 90%,就让整个图片显示出来。
  21.   if (counter > columns * rows * 0.9) {
  22.     context.drawImage(image, 0, 0);
  23.   } else {
  24.     requestAnimationFrame(animate);
  25.   }
  26. }
复制代码
完整代码
  1. <html>
  2.   <body>
  3.     <canvas
  4.       id="myCanvas"
  5.       width="900"
  6.       height="600"
  7.       style="background-color: black;"
  8.     ></canvas>
  9.     <script>
  10.       let image = new Image();
  11.       image.src = "https://cdn.yinhengli.com/canvas-example.jpeg";
  12.       let canvas = document.getElementById("myCanvas");
  13.       let context = canvas.getContext("2d");
  14.       let boxWidth, boxHeight;
  15.       let rows = 10,
  16.         columns = 20,
  17.         counter = 0;
  18.       image.onload = function () {
  19.         boxWidth = image.width / columns;
  20.         boxHeight = image.height / rows;
  21.         requestAnimationFrame(animate);
  22.       };
  23.       function animate() {
  24.         let x = Math.floor(Math.random() * columns);
  25.         let y = Math.floor(Math.random() * rows);
  26.         context.drawImage(
  27.           image,
  28.           x * boxWidth, // 横坐标起始位置
  29.           y * boxHeight, // 纵坐标起始位置
  30.           boxWidth, // 图像的宽
  31.           boxHeight, // 图像的高
  32.           x * boxWidth, // 在画布上放置图像的 x 坐标位置
  33.           y * boxHeight, // 在画布上放置图像的 y 坐标位置
  34.           boxWidth, // 要使用的图像的宽度
  35.           boxHeight // 要使用的图像的高度
  36.         );
  37.         counter++;
  38.         if (counter > columns * rows * 0.9) {
  39.           context.drawImage(image, 0, 0);
  40.         } else {
  41.           requestAnimationFrame(animate);
  42.         }
  43.       }
  44.     </script>
  45.   </body>
  46. </html>
复制代码
总结
通过这个 Demo,我们使用了 canvasAPI 实现了图片的碎片加载效果,是不是特别简单!
到此这篇关于基于HTML代码实现图片碎片化加载功能的文章就介绍到这了,更多相关html图片碎片化加载内容请搜索中国红客联盟以前的文章或继续浏览下面的相关文章,希望大家以后多多支持中国红客联盟!

本帖子中包含更多资源

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

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

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行