基于HTML代码实现图片碎片化加载功能
2020-08-18 04:09
1431 查看
今天来实现一个图片碎片化加载效果,效果如下:
我们分为 3 个步骤来实现:
- 定义 html 结构
- 拆分图片
- 编写动画函数
定义html结构
这里只需要一个 canvas 元素就可以了。
<html> <body> <canvas id="myCanvas" width="900" height="600" style="background-color: black;" ></canvas> </body> </html>
拆分图片
这个例子中,我们将图片按照 10 行 10 列的网格,拆分成 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; // 循环渲染 requestAnimationFrame(animate); };
requestAnimationFrame :告诉浏览器,你希望执行一个动画,并且要求浏览器在下次重绘之前调用指定的回调函数更新动画。
编写动画函数
接下来我们编写动画函数,让浏览器在每一次重绘前,随机渲染某个小碎片。
这里的核心是 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>
总结
通过这个 Demo,我们使用了 canvasAPI 实现了图片的碎片加载效果,是不是特别简单!
到此这篇关于基于HTML代码实现图片碎片化加载功能的文章就介绍到这了,更多相关html图片碎片化加载内容请搜索脚本之家以前的文章或继续浏览下面的相关文章,希望大家以后多多支持脚本之家!
相关文章推荐
- 基于ASP.NET+easyUI框架实现图片上传功能(表单)
- JavaScript学习笔记之基于定时器实现图片无缝滚动功能详解
- 二级缓存加载图片实现照片墙功能
- Android 通过开源框架实现加载网络图片并下载到SD卡通知系统相册显示,(并实现分享图片功能)
- Android实战简易教程-第二十八枪(基于Bmob实现头像图片设置和网络上传功能!)
- 实战,实现带进度的Glide图片加载功能
- Android图片加载框架最全解析(七),实现带进度的Glide图片加载功能
- 基于asp.net实现图片在线上传并在线裁剪功能
- Android Camera开发系列(上)——Camera的基本调用与实现拍照功能以及获取拍照图片加载大图片
- JavaScript模仿Pinterest实现图片预加载功能
- 基于jquery实现图片上传本地预览功能
- 基于javascript实现图片懒加载(亲测有效)
- 基于angular6.0实现的一个组件懒加载功能示例
- 基于纯JS实现多张图片的懒加载Lazy过程解析
- 基于Glide 4.4 实现加载进度监听 超大图片显示 自定义磁盘缓存路径
- 基于jQuery实现图片的前进与后退功能
- 一种基于Glide图片加载框架的Android RichText实现
- Javascript实现图片的预加载功能
- Python基于PyGraphics包实现图片截取功能的方法
- [置顶] viewPager+photoView实现网络图片加载左右滑动+手势缩放功能+滑动到下一页其他页面恢复默认大小