简体   繁体   English

html2canvas无法读取嵌入式背景颜色样式

[英]html2canvas is not reading inline background-color style

I am using html2canvas which works great except the background colour of the div is not being read when I use inline styling. 我正在使用html2canvas,除了使用div样式时不读取div的背景颜色外,它的效果都很好。 However it does work when a style sheet is used, but as the colour is controlled by react, I need it to read the inline style preferably, can anyone help fix this pls? 但是,当使用样式表时它确实可以工作,但是由于颜色是由react控制的,因此我需要它以读取内联样式为佳,有人可以帮助解决此问题吗?

My html: 我的html:

<div id="testDiv" class="page" style="width: 530px; height: 630px; background-color: red; position: relative;">...</div>

My js: 我的js:

function getThumbnail = (div) => {
  return html2canvas(div).then(canvas => {
    return canvas.toDataURL();
  });
}

The image is rendered with no background in the above code unless I add: 除非添加以下内容,否则上述代码中的图像将无背景渲染:

#testDiv {
  background-color: red;
}

in a style sheet but then it is not easy to make the background variable. 在样式表中,但是要设置背景变量并不容易。

UPDATE: I've also tried using css variables but html2canvas does not pick up this styling either. 更新:我也尝试过使用css变量,但是html2canvas也没有采用这种样式。

:root {
  --garment-colour: #000000;
};

#testDiv {
  background-color: var(--garment-colour);
}

It does show the background. 它确实显示了背景。 See console log for image data. 有关图像数据,请参见控制台日志。

html2canvas(document.querySelector("#testDiv")).then(canvas => {
    console.log(canvas.toDataURL());
});

Here is the fiddle: https://jsfiddle.net/nxrvsphz/ 这是小提琴: https : //jsfiddle.net/nxrvsphz/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM