美文网首页
使用html2canvas时,导出图片背景不是透明色

使用html2canvas时,导出图片背景不是透明色

作者: 羊驼626 | 来源:发表于2022-03-16 16:17 被阅读0次

问题原因:

  • dom容器的背景颜色不为透明,将dom容器样式设置为background: transparent
  • html2canvas的options参数不为null,传入配置项backgroundColor: null
  • canvas.toDataURL('image/jpeg')导出的base64会自带白色背景,因为jpeg图片不支持alpha通道,使用canvas.toDataURL('image/png')即可
const options = {
  backgroundColor: null // null或transparent可将canvas背景设置为透明
}
html2canvas(dom, options).then(canvas => {
        const base64 = canvas.toDataURL('image/png')
      })

相关文章

网友评论

      本文标题:使用html2canvas时,导出图片背景不是透明色

      本文链接:https://www.haomeiwen.com/subject/ctmpdrtx.html