const dataURLtoBlob = (dataurl) => {
const arr = dataurl.split(',') //分割为数组,分割到第一个逗号
let bstr = window.atob(arr[1])
let n = bstr.length
let u8arr = new Uint8Array(n)
while (n--) {
u8arr[n] = bstr.charCodeAt(n)
}
return new Blob([u8arr], { type: 'png' })
}
const handleSubmit = async () => {
//解决文字偏移问题
const style = document.createElement('style')
document.head.appendChild(style)
style.sheet?.insertRule('body > div:last-child img { display: inline-block; }')
html2canvas(customModel.value, {
scale: 1,
scrollX: 0,
scrollY: 0
}).then((canvas) => {
style.remove()
let dataURL = canvas.toDataURL('image/png')
const blobUrl = dataURLtoBlob(dataURL)
const fileUrl = URL.createObjectURL(blobUrl)
// 创建a标签下载图片
const addElement = document.createElement('a')
addElement.href = fileUrl
addElement.download = '下载.png'
document.body.appendChild(addElement)
addElement.click()
document.body.removeChild(addElement)
})
}
06-04
1513
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-24
283
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
01-28