Im converting a d3 svg object with use of canvg library to an canvas and display it as an image (png).
The resulting image has a transparent background, which is not what I want. I need this with a white background.
So i tried to set the background color of svg element. When viewing svg element it is fine, but the converted image is still transparent.
I also tried, to alter background of the canvas object, but this is also not working.
first approach (svg):
var svg = d3.select(".chart").append("svg").attr("style","background: white;")...
second aproach (canvas):
canvg(document.getElementById('canvas'), $("#chart").html());
var canvas = document.getElementById('canvas');
canvas.style.background = 'white';
var img = canvas.toDataURL('image/png');
document.write('<img src="' + img + '"/>');
Does anyone know, on which object I have to set the background color, in order to get it converted properly in png image ?
Edit: With the information mentioned in ThisOneGuys answers, I found this solution.
var svg = d3.select(".chart").append("svg") ...
svg.append("rect")
.attr("width", "100%")
.attr("height", "100%")
.attr("fill", "white");
With the appended rect, I get what I need :)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…