Search code examples
html5-canvashtml2canvasc3

Thickness of canvas axis line is increasing


I am converting svg(bar graph) to canvas using html5 canvas and then to image.But while converting the thickness of x-axis and y-axis line are increasing.

this is my code

var html = d3.select(
                    '#div2 #' + $scope.selectedoption + ' svg')
                    .attr("version", 1.1).attr("xmlns",
                            "http://www.w3.org/2000/svg")
                    .node().parentNode.innerHTML;

            // d3.select("#mycanvas").style("display", "none");

            var canvasval = document.getElementById('mycanvas');

            if (canvasval != null)
                document.getElementById('mycanvas').remove();


            var newcanvas = document.createElement('canvas');
            newcanvas.setAttribute('id', "mycanvas");
            newcanvas.setAttribute('width', "900");
            newcanvas.setAttribute('height', "900");



            var olddiv = document.getElementById('drawcanvas');
            olddiv.appendChild(newcanvas);

            // console.log(document.getElementById('mycanvas'))
            var canvas = document.getElementById("mycanvas"), context = canvas
                    .getContext("2d");


            // build the query selector for the desired canvas



            context.lineWidth = 0.0;


            var DOMURL = window.URL;

            var img = new Image();
            var svg = new Blob([ html ], {
                type : 'image/svg+xml;base64;charset=UTF-8;'
            });
            var url = DOMURL.createObjectURL(svg);

            img.onload = function() {
                context.drawImage(img, 0,0);
                DOMURL.revokeObjectURL(url);
            }

            img.src = url;
            img.Timeout=0;

            var flag = 0;

            /*
             * var olddiv =
             * document.getElementById('drawcanvas');
             * olddiv.appendChild(newcanvas);
             */

            // $scope.hidevalue=false;
        }
        html2canvas(
                document.getElementById(canvaseleid),
                {
                    onrendered : function(canvas) {
                        var imgData = canvas
                                .toDataURL('image/png');

                        var a = document.createElement("a");
                        a.download = "rept.png";
                        a.href = imgData;
})

I couldnot figure out the reason for it.I have tried setting several css properties as well.

Thanks


Solution

  • You need to set the fill to transparent (the thick line you are seeing is actually the fill) for the axes. You'll need to add a stroke color instead (to show a line)

    You must do this in inline CSS - you could use d3 to add these styles in, like so (after the chart has been drawn in SVG, and before calling html2canvas)

    d3.selectAll(".domain")
        .style("stroke", "black")
        .style("fill", "transparent");