Save hidden DIV as canvas image

感情迁移 提交于 2020-02-25 06:53:46

问题


I used the following code to save the visible as image.

html2canvas(document.querySelector('.specific'), {
        onrendered: function(canvas) {
        theCanvas = canvas;
        Canvas2Image.saveAsPNG(canvas); 
    }
});

Is there any way that I can able to save the hidden


回答1:


There are some solutions out there, like for example display property toggle, or render inside hidden element.

Solution 1

Quickly toggle visibility property

const el = document.querySelector('.specific');
el.style.display = "block"; // or any other property, like opacity, visibility...
html2canvas(el, {...}).then((canvas) => {
   el.style.display = "none";
};

Solution 2

Wrap your div (and make it visible) inside an invisible wrapper

<div style="position: absolute; opacity: 0; pointer-events:none;">
    <div class="specific"></div>
</div>

or

<div style="overflow: hidden; height: 0;">
    <div class="specific"></div>
</div>

Solution 3

Using html2canvas onclone callback function you can modify object passed to renderer (I think it's the best solution)

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); 
    }
});


来源:https://stackoverflow.com/questions/52116877/save-hidden-div-as-canvas-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!