网站首页 > 精选文章 正文
1.获取systemInfo
const systemInfo = uni.getSystemInfoSync();
2.需要截图的元素列表
const resultList = [
{ id: 1, z-index: 0, url: 'https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png' },
{ id: 2, z-index: 10, url: 'https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png' },
{ id: 3, z-index: 20, url: 'https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png' },
{ id: 4, z-index: -1, url: 'https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png' },
];
resultList.sort((a, b) => a.z_index - b.z_index);
3.使用canvas把所有元素统一截图保存
const drawCanvas = () => {
const dpr = systemInfo.pixelRatio;
console.log("systemInfo.pixelRatio", systemInfo.pixelRatio);
// 获取canvas
const query = wx.createSelectorQuery();
query
.select("#canvas-container")
.fields({
node: true,
size: true,
})
.exec((res) => {
const canvas = res[0].node;
// 小程序版本现在只支持canvas2d,不能使用老版canvas截图了
const ctx = canvas.getContext("2d");
// 保存的截图宽高
res[0].node.width = 216 * dpr;
res[0].node.height = 384 * dpr;
// 定义一个异步函数来处理每个元素
async function processItem(item: any) {
return new Promise((resolve) => {
console.log("forEach", item.type);
const img = canvas.createImage();
img.src = item.url;
img.onload = () => {
console.log("类型", item.type);
// 先保存一下设置
ctx.save();
// 最后画出来
ctx.drawImage(
img,
item.x ? item.x * dpr : 0,
item.y ? item.y * dpr : 0,
item.width * dpr,
item.height * dpr
);
// 不要忘记恢复之前的设置
ctx.restore();
resolve(true);
};
img.onerror = (err) => {
console.log("onerror", err);
resolve(true);
};
});
}
// 使用async函数来包裹整个循环
async function processItems(arr) {
for (const item of arr) {
await processItem(item); // 等待当前异步操作完成
}
}
// 调用函数
processItems(resultList)
.then(() => {
console.log("All items processed.");
// canvas转文件的临时路径 (本地路径)
wx.canvasToTempFilePath({
x: 0,
y: 0,
width: 216,
height: 384,
destWidth: 216 * dpr,
destHeight: 384 * dpr,
canvas,
fileType: "jpg",
quality: 1,
success: (res) => {
console.log("res.tempFilePath", res);
// 将canvas图片上传到阿里oss云服务器(有接口就调用后端接口上传图片)
uploadFileToOSS(res.tempFilePath, resultList);
},
fail: (err) => {
console.log("err", err);
uni.hideLoading();
},
});
})
.catch((error) => {
console.error("Error processing items:", error);
uni.hideLoading();
});
});
};
drawCanvas();
4.官方插件 wxml-to-canvas
如果想要使用截图库可以使用这个插件,只不过可能会有一些坑(比如不是原生小程序开发,使用uniapp的时候)。这个文章可以解决一部分问题:wxml-to-canvas使用过坑记录
猜你喜欢
- 2025-06-13 前端必看!7 个 Vue3 性能优化实战技巧,让页面飞起来
- 2025-06-13 Vue2 升级 Vue3 一文通关(vue-cli2.0升级3.0)
- 2025-06-13 Vue2的16种传参通信方式(vue有几种传参方式)
- 2025-06-13 面试官:聊聊你知道的Vue与React的区别
- 2025-06-13 vue3.0总结之ref与reactive(vue ref reactive)
- 2025-06-13 90% 的前端工程师都不知道的 Vue3 实战神操作,看完直呼后悔没早学
- 2025-06-13 vue3 组件初始化流程(vue组件初始化顺序)
- 2025-06-13 从 React 过渡到 Vue 3:开发者的实践指南
- 2025-06-13 vue重定向(vue重定向后403报错)
- 2025-06-13 全方位带你掌握 ref、reactive,开启 Vue3 响应式的大门
- 最近发表
- 标签列表
-
- 向日葵无法连接服务器 (32)
- git.exe (33)
- vscode更新 (34)
- dev c (33)
- git ignore命令 (32)
- gitlab提交代码步骤 (37)
- java update (36)
- vue debug (34)
- vue blur (32)
- vscode导入vue项目 (33)
- vue chart (32)
- vue cms (32)
- 大雅数据库 (34)
- 技术迭代 (37)
- 同一局域网 (33)
- github拒绝连接 (33)
- vscode php插件 (32)
- vue注释快捷键 (32)
- linux ssr (33)
- 微端服务器 (35)
- 导航猫 (32)
- 获取当前时间年月日 (33)
- stp软件 (33)
- http下载文件 (33)
- linux bt下载 (33)