在做canvas插入文本框+截图功能的时候使用 html2canvas, 出现保存的图片模糊的问题。
搜了很多帖子,都有略微差别,有的改了源码也不说 = =,
最后找到可用的,清晰度很高的。保存一下。
下面是例子
let targetDom = document.getElementById('content')
let targetWidth = targetDom.offsetWidth
let targetHeight = targetDom.offsetHeight
let canvas = document.createElement('canvas')
let scale = window.devicePixelRatio
canvas.width = targetWidth * scale
canvas.height = targetHeight * scale
canvas.style.width = targetWidth * scale + 'px'
canvas.style.height = targetHeight * scale + 'px'
canvas.getContext('2d').scale(scale, scale)
html2canvas(targetDom, {
allowTaint: false,
scale,
canvas,
logging: false,
useCORS: true,
width: targetWidth,
height: targetHeight,
ignoreElements: (element) => {
if (element.id === 'printHide') {
return true
}
}
}).then(canvas => {
this.cachePatinUrl = canvas.toDataURL('image/jpeg', 1.0)
this.cacheHeight = canvas.height
this.cacheWidth = canvas.width
})

很清晰。
6438

被折叠的 条评论
为什么被折叠?



