html and Svg to Canvas javascript

前端 未结 5 661
别那么骄傲
别那么骄傲 2021-02-02 01:36
&l
相关标签:
5条回答
  • 2021-02-02 01:43

    html2canvas doesn't allow to save SVG is an issue.
    https://github.com/niklasvh/html2canvas/issues/95

    If you want to save an SVG you an follow other way, like to transform your SVG into a PNG for example

    SVG -> canvas -> PNG -> canvas -> PNG

    0 讨论(0)
  • 2021-02-02 01:47

    As the html2canvas don't take svg elements, you need to convert all svg elements to canvas before you call the html2canvas method. You could use the canvg library to convert all the svg to canvas. You can pass the jquery object(which needs to convert from svg to canvas, can also be a parent element) to the following method:

    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;
        }
        });
    }
    
    0 讨论(0)
  • 2021-02-02 02:01

    You will need to use canvg library for drawing this svg to a temporary canvas and then remove that canvas once you take the screenshot using html2canvas.

    Here is the link for canvg : https://github.com/canvg/canvg

    Try this:

    //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');
    
    0 讨论(0)
  • 2021-02-02 02:01

    You can create your own innerHTML, like the following setSVG: enter image description here

    In fact, you only need to pay attention to two steps: creating the SVG context label and setting properties (with context)

    createElementNS and setAttributeNS are two methods you may need!

    Common namespaces are as follows:

    • svg:http://www.w3.org/2000/svg

    • xhtml:http://www.w3.org/1999/xhtml

    • xlink:http://www.w3.org/1999/xlink

    • xml:http://www.w3.org/XML/1998/namespace

    • xmlns:http://www.w3.org/2000/xmlns/

    0 讨论(0)
  • 2021-02-02 02:04

    Your solution works beautifully. Since I am not using jQuery in my application, I had to change couple of lines in svgCanvas for getting the svg elements and iterating through them. The rest of the functions worked without any changes. My code is...

    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);
            }
    }
    
    0 讨论(0)
提交回复
热议问题