<script module="html2canvas" lang="renderjs">
import html2canvas from 'html2canvas';
export default {
created() {
var that = this
setTimeout(function() {
// that.emitData()
}, 3000)
},
methods: {
emitData() {
console.log('开始。。。')
// 根据自己需要截图区域
this.create('detail');
},
async create(id) {
if (!id) {
return false;
}
try {
this.$ownerInstance.callMethod('showLoading', true);
//调用上一个script的方法
const timeout = setTimeout(async () => {
const domId = document.getElementById(id);
const canvas = await html2canvas(domId, {
width: domId.offsetWidth,
//设置canvas尺寸与所截图尺寸相同,防止白边
height: domId.offsetHeight, //防止白边
logging: true,
useCORS: true
});
const base64 = canvas.toDataURL('image/jpg', 1);
console.log("base64图", base64)
this.$ownerInstance.callMethod('renderFinish', {
path: base64,
el: id
});
clearTimeout(timeout);
}, 500);
} catch (error) {
console.log(error)
}
}
}
}
</script>
<script>
showLoading() {
console.log("加载中")
uni.showLoading({
title: '加载中...',
mask: false
});
},
renderFinish(opt) {
console.log("生成完成",opt.path)
base64ToPath(opt.path)
.then(path => {
console.log(path,'path')
uni.hideLoading()
this.saveImage(path);
})
.catch(error => {
console.error('临时路径转换出错了:', error);
});
},
saveImage(url){
uni.saveFile({
tempFilePath:url,
async success(res) {
console.log(res.savedFilePath, 'res.savedFilePath')
uni.openDocument({
filePath:res.savedFilePath,
success: function(FileRes) {
console.log('打开成功');
}
});
setTimeout(function() {uni.navigateBack();}, 500);
},
complete(res) {
console.log(res,'res')
}
})
},
</script>