为什么 html2canvas 顶部有一个空白区域?

joh*_*ohn 20 html javascript css html2canvas

我的有一个空白区域,我html2canvas不确定发生了什么。到目前为止,这是我的代码。

function doFunction() {

  html2canvas(document.querySelector("#capture"), ).then(canvas => {
    $("body").append(canvas);
  });
}

$("button").click(function() {
  doFunction();
});
Run Code Online (Sandbox Code Playgroud)
div {
  box-sizing: border-box;
  font-family: 'ABeeZee', sans-serif;
}

body {
  background-color: red;
}

#capture {
  width: 900px;
  height: 900px;
  background-color: aqua;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="http://html2canvas.hertzen.com/dist/html2canvas.js"></script>
<div id="capture">
  Hello
</div>
<button type="button">Click Me!</button>
Run Code Online (Sandbox Code Playgroud)

这是附加的画布。请注意,顶部有一个空白区域。我怎样才能删除它?

在此处输入图片说明

Faz*_*man 26

GitHub 问题

您应该检查文档滚动,当页面滚动处于活动状态或向下滚动页面时,我面临同样的问题。

尝试添加

{
    scrollX: 0,
    scrollY: -window.scrollY
}
Run Code Online (Sandbox Code Playgroud)

到 html2canvas 选项

  • @Fazlur-rahman 给出的这个选项是 html2canvas 中的选项,如下所示: html2canvas(data, {allowTaint: true ,scrollX:0,scrollY: -window.scrollY }).then(canvas =&gt; {}); (4认同)