【问题标题】:html and Svg to Canvas javascripthtml 和 Svg 到 Canvas javascript
【发布时间】:2013-08-18 18:38:08
【问题描述】:
<div id="Contents">
<div style="float:left;margin-left:10px;">
<svg></svg>
</div>
<div style="float:left;margin-left:10px;">
<svg></svg>
</div>
</div>

这是我的 html 代码。我想把它转换成画布图像。

html2canvas($("#Contents"), {
  onrendered: function(canvas) {
   window.open(canvas.toDataURL());

  }
});

我使用 html2canvas 插件将其转换为图像,但它不显示 svg。 我解决了转换 svg tp 画布但现在 html2canvas 不起作用

  var $to=$("#MainContents").clone();

            $($to).children(".box").each(function() {
    var svg = ResizeArray[$(this).children(".box-content").children().attr("new-id")-1].svg();
            var Thiscanvas = document.createElement("canvas");
            Thiscanvas.setAttribute("style", "height:" + 100 + "px;width:" + 100+ "px;");

            canvg(Thiscanvas, svg);

            $(this).append(Thiscanvas);

        });
        html2canvas($($to), {
      onrendered: function(canvasq) {
        var w=window.open(canvasq.toDataURL());
        w.print();
      }
    });

我可以将 svg 转换为画布,但 html2canvas 功能不起作用。

【问题讨论】:

标签: javascript html canvas svg html2canvas


【解决方案1】:

html2canvas 不允许保存 SVG 是个问题。
https://github.com/niklasvh/html2canvas/issues/95

如果你想保存 SVG,你可以按照其他方式,例如将 SVG 转换为 PNG

SVG -> 画布 -> PNG -> 画布 -> PNG

【讨论】:

  • 我可以将 html2canvas 与 canvg 一起使用吗? .在使用 html2canvas 之前,我将首先转换 canvg。我认为他们不能一起工作。
  • 您可以尝试一下,在我发布到我的答案中的链接中查看有人已经尝试过这个
【解决方案2】:

由于 html2canvas 不带 svg 元素,所以在调用 html2canvas 方法之前,您需要将所有 svg 元素转换为画布。您可以使用 canvg 库将所有​​ svg 转换为画布。您可以将jquery对象(需要从svg转换为canvas,也可以是父元素)传递给以下方法:

function svgToCanvas (targetElem) {
var nodesToRecover = [];
var nodesToRemove = [];

var svgElem = targetElem.find('svg');

svgElem.each(function(index, node) {
    var parentNode = node.parentNode;
    var svg = parentNode.innerHTML;

    var canvas = document.createElement('canvas');

    canvg(canvas, svg);

    nodesToRecover.push({
        parent: parentNode,
        child: node
    });
    parentNode.removeChild(node);

    nodesToRemove.push({
        parent: parentNode,
        child: canvas
    });

    parentNode.appendChild(canvas);
});

html2canvas(targetElem, {
    onrendered: function(canvas) {
        var ctx = canvas.getContext('2d');
        ctx.webkitImageSmoothingEnabled = false;
        ctx.mozImageSmoothingEnabled = false;
        ctx.imageSmoothingEnabled = false;
    }
    });
}

【讨论】:

  • 非常感谢您! sn-p 就像一个魅力。我终于可以让它工作了。但是,我仍然需要对其进行校准,因为我注意到快照以一种奇怪的方式裁剪图表。
【解决方案3】:

您的解决方案效果很好。因为我没有在我的应用程序中使用 jQuery,所以我不得不更改 svgCanvas 中的几行来获取 svg 元素并遍历它们。其余功能无需任何更改即可工作。我的代码是...

function svgToCanvas (targetElem) {
        var nodesToRecover = [];
        var nodesToRemove = [];

        var svgElems = document.getElementsByTagName("svg");

        for (var i=0; i<svgElems.length; i++) {
            var node = svgElems[i];
            var parentNode = node.parentNode;
            var svg = parentNode.innerHTML;

            var canvas = document.createElement('canvas');

            canvg(canvas, svg);

            nodesToRecover.push({
                parent: parentNode,
                child: node
            });
            parentNode.removeChild(node);

            nodesToRemove.push({
                parent: parentNode,
                child: canvas
            });

            parentNode.appendChild(canvas);
        }
}

【讨论】:

  • 您的代码有很多未使用的变量。我知道它很旧,但你可能应该看看它:-)
【解决方案4】:

您需要使用 canvg 库将此 svg 绘制到临时画布上,然后在使用 html2canvas 截取屏幕截图后移除该画布。

这里是canvg的链接:https://github.com/canvg/canvg

试试这个:

//find all svg elements in $container
//$container is the jQuery object of the div that you need to convert to image. This div may contain highcharts along with other child divs, etc
var svgElements= $container.find('svg');

//replace all svgs with a temp canvas
svgElements.each(function () {
    var canvas, xml;

    canvas = document.createElement("canvas");
    canvas.className = "screenShotTempCanvas";
    //convert SVG into a XML string
    xml = (new XMLSerializer()).serializeToString(this);

    // Removing the name space as IE throws an error
    xml = xml.replace(/xmlns=\"http:\/\/www\.w3\.org\/2000\/svg\"/, '');

    //draw the SVG onto a canvas
    canvg(canvas, xml);
    $(canvas).insertAfter(this);
    //hide the SVG element
    this.className = "tempHide";
    $(this).hide();
});

//...
//HERE GOES YOUR CODE FOR HTML2CANVAS SCREENSHOT
//...

//After your image is generated revert the temporary changes
$container.find('.screenShotTempCanvas').remove();
$container.find('.tempHide').show().removeClass('tempHide');

【讨论】:

    【解决方案5】:

    您可以创建自己的innerHTML,如下setSVGenter image description here

    其实只需要注意两步:创建SVG上下文标签和设置属性(带上下文)

    createElementNS 和 setAttributeNS 是你可能需要的两个方法!

    常见的命名空间如下:

    【讨论】:

    • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 how to answer
    • 感谢您的建议。我已经修改了答案
    猜你喜欢
    • 2021-07-12
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 2012-07-29
    • 1970-01-01
    • 2023-01-29
    • 1970-01-01
    • 2014-03-15
    相关资源
    最近更新 更多