import html2canvas from "html2canvas"
export function domRefToImage(refDom, imageName = null, scale = 1, imageType = "image/png", backgroundColor = '#fff') {
if (imageName === null) {
let nowDate = new Date()
imageName = '' + nowDate.getFullYear() + (nowDate.getMonth() + 1) + nowDate.getDate() + nowDate.getHours() + nowDate.getMinutes() + nowDate.getSeconds()
}
const canvas = document.createElement("canvas")
const width = parseInt(window.getComputedStyle(refDom).width)
const height = parseInt(window.getComputedStyle(refDom).height)
canvas.width = width * scale
canvas.height = height * scale
canvas.style.width = width + 'px'
canvas.style.height = height + 'px'
const ctx = canvas.getContext("2d");
ctx.scale(scale, scale);
const options = {
backgroundColor,
canvas,
useCORS: true
}
html2canvas(refDom, options).then((canvas) => {
let a = document.createElement('a')
a.href = canvas.toDataURL(imageType)
a.download = imageName
a.click()
})
}