import html2canvas from "html2canvas";
export function base64ImgtoFile(dataurl, filename = 'file') {
let arr = dataurl.split(',')
let mime = arr[0].match(/:(.*?);/)[1]
let suffix = mime.split('/')[1]
let bstr = atob(arr[1])
let n = bstr.length
let u8arr = new Uint8Array(n)
while (n--) {
u8arr[n] = bstr.charCodeAt(n)
}
return new File([u8arr], `${filename}.${suffix}`, {
type: mime
})
}
export function toImages(imageTofile) {
return new Promise((resolve, reject) => {
html2canvas(imageTofile, {
backgroundColor: null,
useCORS: true
}).then((canvas) => {
const url = canvas.toDataURL('image/png');
resolve(base64ImgtoFile(url))
}).catch((err) => {
reject(err)
})
})
}
// 调用
toImage() {
let that=this;
toImages(this.$refs.imageTofile).then((imgFileRaw) => {
that.imgFileRaw = base64ImgtoFile(this.htmlUrl);
}).catch((error) => {
that.imgFileRaw = null;
});
},