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

问题原因:

  • 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时,导出图片背景不是透明色)