简体   繁体   English

html2canvas 忽略我的 svg 元素

[英]html2canvas ignores my svg elements

I try to print screen of my page and then save it into base64 , it works good but it looks like that svg elements are ignored, and some css styles work bad, like :before and :after , original page looks -我尝试打印页面的屏幕,然后将其保存到base64 ,它运行良好,但看起来svg元素被忽略了,并且某些css样式效果不佳,例如:before:after ,原始页面看起来 - 在此处输入图片说明

but after conversion it looks like this -但转换后它看起来像这样 - 在此处输入图片说明

You can see the circle on right side is ignore, and arrow in top menu and same :before and :after on tabs and add new tab button (+).您可以看到右侧的圆圈被忽略,顶部菜单中的箭头和选项卡上的:before:after相同,并添加新选项卡按钮 (+)。 My convert code looks -我的转换代码看起来 -

printOnePage(){
        document.getElementById('helpPage').style.display= 'none';
        let page = document.getElementById('appContainer');
        html2canvas(page,{
            onrendered: function(canvas) {
                $('#img_val').val(canvas.toDataURL("image/png"));
                console.log(document.getElementById('img_val'));
                //document.getElementById("phpSendForm").submit();
            }
        });
        document.getElementById('helpPage').style.display= '';

    }

Any tips what can cause this ?任何提示可能导致这种情况?

Html2canvas ignores your svg elements if the CSS properties are in the CSS page.如果 CSS 属性在 CSS 页面中,Html2canvas 会忽略您的 svg 元素。

I had the following problem: I had an svg block and inside a line block.我遇到了以下问题:我有一个 svg 块和一个行块。 And html2canvas capture the div block but doesn't capture my line.并且 html2canvas 捕获 div 块但不捕获我的行。 The style of each has been declared in the CSS page with the class name of these two elements. CSS 页面中已经用这两个元素的类名声明了每个的样式。

And the solution was just to put the CSS properties inside the svg and line tag, like this:解决方案只是将 CSS 属性放在 svg 和 line 标签中,如下所示:

<div>
<svg width="100%" height="100%">
<line style="cursor: pointer; stroke: black; stroke-width: 2;" />
</svg>
</div>

html2canvas support SVG rendering since 0.5.0-alpha1 just html2canvas 从0.5.0-alpha1开始支持SVG 渲染

  1. update更新
  2. add the allowTaint property.添加allowTaint属性。

like blow:喜欢吹:

html2canvas(copyDom[0], {
        useCORS: true,
        imageTimeout:0,
        allowTaint: true //you can add this property
    }).then(function (canvas) {})

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

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