设置从 Google Charts 创建的透明画布/png 的背景颜色

shh*_*san 1 html javascript css background canvas

canvg我需要帮助设置通过使用创建的画布对象的背景颜色Google Charts Calendar

这是JS Fiddle

下载的 image/png 具有透明背景,因为画布是这样的。我想改变它,使其不再透明而是白色。我尝试setAttribute像这样使用:

  canvas.setAttribute(
    'style',
    'position: absolute; ' +
    'top: ' + (-chartArea.offsetHeight * 2) + 'px; ' +
    'left: ' + (-chartArea.offsetWidth * 2) + 'px;' +
    'backgroud-color: #fff;');
Run Code Online (Sandbox Code Playgroud)

也使用这种方式:canvas.style.backgroundColor = 'blue';但这些尝试失败了,我无法找到一个好的工作答案。

非常感谢所有帮助。

小智 5

尝试:

context = canvas.getContext("2d");

// set to draw behind current content
context.globalCompositeOperation = "destination-over";

// set background color
context.fillStyle = '#fff'; // <- background color

// draw background / rect on entire canvas
context.fillRect(0, 0, canvas.width, canvas.height);
Run Code Online (Sandbox Code Playgroud)

来源:http ://blogs.adobe.com/digitalmedia/2011/01/setting-the-background-color-when-generate-images-from-canvas-todataurl/