【发布时间】:2022-02-01 05:14:02
【问题描述】:
【问题讨论】:
标签: html2canvas
【问题讨论】:
标签: html2canvas
这似乎是 box-shadow 的问题。尝试禁用并导出检查。 html2canvas 不支持 Box-shadow。
【讨论】:
这解决了我的问题:
box-shadow: unset !important;
当我们使用上面的 CSS 代码时,我们可以清除相关 div 中的 box-shadow,这样 html2canvas 就可以轻松读取 HTML 的内容了。
【讨论】:
这是一个奇怪的事情,但即使我将主容器背景颜色设置为白色,它也没有反映出来。我想我会尝试在主容器中添加另一个 div,位置为绝对值,顶部 0,左侧 0,背景颜色为白色。
虽然我不完全了解发生了什么,但它成功了。
【讨论】:
我不确定这有多聪明,但我用这种方式解决了一个类似的问题:
// A function that assigns a shadow style to an element
function setShadowStyle(el, shadowStyle) {
el.style.boxShadow = shadowStyle;
}
// ... some functions omitted ...
function savePNG() {
// Set the element with which we will work
let graph = document.querySelector("#graph");
// Set the element's shadow style to default
const defaultShadowStyle = window.getComputedStyle(graph).boxShadow;
// Removing the shadow before starting html2canvas
setShadowStyle(graph, 'none');
// We perform all the necessary actions of html2canvas
html2canvas(graph).then(function (canvas) {
let img = document.body.appendChild(canvas);
download(img, "image/png", "graph.png");
// Be sure to set some timeout, after which we remove the temporary canvas block
setTimeout(function () {
img.remove();
}, 500);
});
// Return the original style of the shadow to the corresponding block
setShadowStyle(graph, defaultShadowStyle);
}
【讨论】:
options.onClone 函数中执行此操作,这样您就不必重新设置样式。 html2canvas.hertzen.com/configuration