我使用以下代码将可见图像保存为图像。
html2canvas(document.querySelector('.specific'), {
onrendered: function(canvas) {
theCanvas = canvas;
Canvas2Image.saveAsPNG(canvas);
}
});
有什么方法可以保存隐藏的
答案 0 :(得分:1)
有一些解决方案,例如显示属性切换或在隐藏元素内部渲染。
快速切换可见性属性
const el = document.querySelector('.specific');
el.style.display = "block"; // or any other property, like opacity, visibility...
html2canvas(el, {...}).then((canvas) => {
el.style.display = "none";
};
在不可见的包装纸中包裹div(并使其可见)
<div style="position: absolute; opacity: 0; pointer-events:none;">
<div class="specific"></div>
</div>
或
<div style="overflow: hidden; height: 0;">
<div class="specific"></div>
</div>
使用html2canvas onclone回调函数,您可以修改传递给渲染器的对象(我认为这是最好的解决方案)
html2canvas(document.querySelector('.specific'), {
onclone: function(doc){
doc.style.display = 'block';
// or doc.style.opacity = '1', doc.style.visibility = 'visible' ...
},
onrendered: function(canvas) {
theCanvas = canvas;
Canvas2Image.saveAsPNG(canvas);
}
});