copyChart () { // 复制charts为图片
// 获取highcharts的svg图片
const img = new Image()
img.src = `` //图片路径
// 将svg转化为canvas
const canvas = document.createElement('canvas')
const context = canvas.getContext('2d')
canvas.width = 600
canvas.height = 400
const _this = this
img.onload = function () {
// svg图加载完毕后绘制到canvas上
context.drawImage(img, 10, 10)
// 将canvas转化为png图片
const imgEle = new Image()
imgEle.src = canvas.toDataURL('image/png')
_this.$refs.chartImg.appendChild(imgEle)
imgEle.onload = function () {
// 选择图片并复制
const el = _this.$refs.chartImg.children[0]
const range = document.createRange()
const selection = window.getSelection()
range.selectNode(el)
selection.removeAllRanges()
selection.addRange(range)
document.execCommand('copy')
selection.removeAllRanges()
// 复制成功可发送成功message
el.parentNode.removeChild(el)
}
}
}
js 复制图片至剪贴板(原生js,可复制word等、不可微信)
最新推荐文章于 2023-12-15 20:46:30 发布