DOM 图像元素仅生成白色图像

DOM element to image generates only white image

我正在使用 html2canvas 从 DOM 元素创建图像。

代码如下所示。

    const node = document.getElementById('domElement') 
    html2canvas(node, {
      windowWidth: node.scrollWidth,
      windowHeight: node.scrollHeight,
    }).then(canvas => {
      const image = canvas.toDataURL()
    })

但它只能渲染全白的图像。图片大小(l*b)准确无误

但是当我尝试渲染全身图像时,它显示了图像,但顶部是空白屏幕。

即什么时候,

    const node = document.body 

如何正确添加元素而不是图像上的空白屏幕?

The code is inside an antd design modal.

这条语句解决了我的问题。

window.scrollTo(0, 0)

最终代码看起来像

window.scrollTo(0, 0)
const node = document.getElementById('domElement') 
html2canvas(node, {
  windowWidth: node.scrollWidth,
  windowHeight: node.scrollHeight,
}).then(canvas => {
  const image = canvas.toDataURL()
})