<span @click="toImage()">导出图片</span>
<div class="imageBox">
....
</div>
import html2canvas from 'html2canvas'
crossTabletoImage () {
const canvas = document.createElement('canvas')
const canvasBox = document.querySelector('.imageBox')
const width = canvasBox.clientWidth
const height = canvasBox.clientHeight
canvas.width = width
canvas.height = height
canvas.style.width = width + 'px'
canvas.style.height = height + 'px'
const context = canvas.getContext('2d')
context.scale(1, 1)
const options = {
backgroundColor: null,
canvas: canvas,
useCORS: true
}
html2canvas(canvasBox, options).then((canvas) => {
const dataURL = canvas.toDataURL('image/png')
this.downloadImage(dataURL)
})
},
downloadImage (url) {
const a = document.createElement('a')
a.href = url
a.download = '图片命名'
a.click()
}