今天来实现一个图片碎片化加载效果,效果如下:
我们分为 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 实现了图片的碎片加载效果,是不是特别简单!
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
白云城资源网 Copyright www.dyhadc.com
暂无“基于HTML代码实现图片碎片化加载功能”评论...
更新日志
2024年05月13日
2024年05月13日
- 决胜巅峰阿格斯技能怎么用 技能效果介绍
- 暗区突围正版官网入口分享 暗区突围官网下载链接
- 暗区突围PC端怎么下载 暗区突围PC端下载教程
- 暗区突围PC端官网入口 暗区突围国际服官网链接
- 《王国之泪》一周年!塞尔达公主声优晒手绘贺图庆祝
- Tango工作室关闭 玩家上Steam好评轰炸《完美音浪》
- 《女神异闻录》系列去年销量超过500万份 新作开发中
- ABC唱片-《美国西电录音九号测试碟》HD德国版[WAV+CUE]
- 姜克美《霸王别姬HQ》头版限量编号[WAV+CUE]
- [ABC]安娜-胆麦发烧女声[6N纯银镀膜][2016[正版CD低速原抓WAV+CUE]
- 解压感十足!这款破坏建筑的游戏现仅59.5元即可入手
- 西班牙一地颁布禁酒令 晚九点半至早八点半禁售
- 吴彦祖发文纪念母亲:每天都想妈妈
- 群星.1989-彩色的年纪-第六届大学城纪念专辑【飞碟】【WAV+CUE】
- 张秀卿.1995-青春【宝丽金】【WAV+CUE】