简体   繁体   English

下载画布到PNG黑色背景

[英]Download canvas to PNG black background

I am building some charts on a canvas using Charts.js and Bootstrap. 我正在使用Charts.js和Bootstrap在画布上构建一些图表。 Users want to be able to download a PNG file of the chart to copy it into a Powerpoint slide. 用户希望能够下载图表的PNG文件以将其复制到Powerpoint幻灯片中。 I have the following function working and it downloads the file but it has a completely black background. 我有以下功能工作,它下载文件,但它有一个完全黑色的背景。 On the page the image has a white background. 在页面上图像具有白色背景。 I see a bunch of posts about this with JPEG and the solution says to use PNG but I am using PNG. 我看到一些关于JPEG的帖子和解决方案说使用PNG,但我使用的是PNG。 Not sure if there is something I can change in the canvas itself. 不确定画布本身是否有可以改变的东西。 I have tried to explicitly set the background color of the canvas to white but that doesn't help. 我试图将画布的背景颜色明确地设置为白色,但这没有帮助。

the downloadToPNG function: downloadToPNG功能:

function downloadToPNG() {
    var canvas   = document.getElementById('parentCanvas');
    var dt = canvas.toDataURL('image/png');
    var hiddenElement = document.createElement('a');
    hiddenElement.href = dt;
    hiddenElement.target = '_blank';
    hiddenElement.download = fileName;
    hiddenElement.click();
    hiddenElement.remove();
}

The background might be because of some lost cascaded CSS values, you might want to double check the background colors for html, body, and every parent element. 背景可能是因为某些丢失的级联CSS值,您可能需要仔细检查html,body和每个父元素的背景颜色。 This may also have to do with the png information being passed around as base64 encoded, which is normal method for the browser to hash image information into alphanumerics. 这也可能与以base64编码传递的png信息有关,这是浏览器将图像信息散列到字母数字中的常规方法。 This thread might help with that how to save canvas as png image? 这个线程可能有助于如何将画布保存为png图像?

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

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