【问题标题】:How to generate a transparent canvas using html2canvas [duplicate]如何使用 html2canvas [重复] 生成透明画布
【发布时间】:2019-03-18 18:54:08
【问题描述】:

这似乎应该是一件简单的事情,但它非常困难。

我只是有一个 div DOM 元素,里面有一些文本。

我正在调用它的 html2canvas 方法,它正在工作查找。

唯一的问题,画布的背景是白色的!

我试过了:

  • 将选项对象的背景属性设置为 null
  • 在画布和生成它的 DOM 元素上将 CSS 背景属性显式更改为透明

不幸的是,似乎没有任何效果。

希望有一个优雅的解决方案,但快速破解是可以的。

【问题讨论】:

标签: javascript html reactjs jsx html2canvas


【解决方案1】:

您必须将 backgroundColor 传递为 null 见:https://html2canvas.hertzen.com/configuration

let el = document.querySelector("#my-div")
html2canvas(el,{backgroundColor:null}).then(canvas => {
  document.body.appendChild(canvas)  
})

工作示例:https://codepen.io/emilio/pen/oaGLmb?editors=1111

【讨论】:

  • 就是这样。非常感谢!
  • {backgroundColor:null} 不适用于我的代码 ` html2canvas(document.querySelector(".resize-wrap"), { backgroundColor: null }).then(canvas => { this. dataURL = canvas.toDataURL(); _marker.MarkerImage = this.dataURL; this.MarkerLists.push(_marker); this.countMarker++; });`
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-23
  • 2017-04-12
  • 1970-01-01
  • 2019-03-31
  • 2019-07-05
相关资源
最近更新 更多