简体   繁体   English

html2canvas 不捕捉图像

[英]html2canvas not capturing image

html2canvas.js not capturing image. html2canvas.js 不捕获图像。 it leaves white space where the image occurs.它在图像出现的地方留下空白。

function capture()
{
    html2canvas(document.body, {
        allowTaint: true,
        logging:true,
        onrendered: function(canvas) {
        imagestring = canvas.toDataURL("image/png");
        console.log(imagestring);
        document.body.appendChild(canvas);
        }
    });
}

I have tried a lot but i cannot find solution .我已经尝试了很多,但我找不到解决方案。

Help is appreciated :)帮助表示赞赏:)

It works, when I host it in the server.当我将它托管在服务器中时,它可以工作。 The security restrictions causes it to fail.安全限制导致它失败。

If you're experiencing the issue only in production but locally it works, without the AllowTaint attribute it wont work in production.如果您仅在生产中遇到问题,但在本地它可以工作,如果没有 AllowTaint 属性,它将无法在生产中工作。 so Add that.所以添加。

See example:见示例:

看例子

html2canvas did not render my source images when I used an image button made using html input tag:当我使用使用 html input 标签制作的图像按钮时,html2canvas 没有呈现我的源图像:

<input type="image" src="...">

html2canvas works perfectly with img tags, though.不过,html2canvas 与 img 标签完美配合。 So, I switched to html button tag (you can use url link tag similarly) and inserted img tag inside it:所以,我切换到 html 按钮标签(你可以类似地使用 url 链接标签)并在其中插入 img 标签:

<button type="button"><img src="..."></button>

Also I found that html2canvas can render image if it is a background-image added in styles (you can keep your input tags!):我还发现 html2canvas 可以渲染图像,如果它是在样式中添加的背景图像(您可以保留输入标签!):

in styles:
...#mybutton{
        background-image: url(...);
        background-repeat: no-repeat;
        background-size: 30px 30px;
        background-color:transparent;
    }
in html:
<input id="mybutton" type="image">
<button id="mybutton" type="button"></button>

The later is particularly helpful if you have video tags.如果您有视频标签,后者尤其有用。 html2canvas does not render poster image for it, so all you have to do is to add that poster image to background-image of your video tag in styles and you will get the picture for your video. html2canvas 不会为其渲染海报图像,因此您所要做的就是将该海报图像添加到样式中视频标签的背景图像中,然后您将获得视频的图片。

Hope this workaround helps somebody.希望这个解决方法可以帮助某人。

You will need to add the following options in html2canvas.您需要在 html2canvas 中添加以下选项。

allowTaint: true
useCORS: true

在此处输入图片说明

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

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