简体   繁体   English

画布的填充样式不起作用

[英]the fillStyle of canvas is not working

i have a function that has a loop.我有一个有循环的函数。 in the loop it creates a canvas and sets the opacity.在循环中,它创建一个画布并设置不透明度。 Then it sets the background color and converts the canvas to an image.然后它设置背景颜色并将画布转换为图像。

Somehow the Opacity is being set on the canvas but the background color doesn't get set.不知何故,在画布上设置了不透明度,但没有设置背景颜色。

if (remain <= 0) {
    var canvas = document.createElement('canvas');
    context = canvas.getContext('2d');
    for (var i = 0; i < img.length; ++i) {
        if (img[i]) {
         var opacity = item.opa;
         context.globalAlpha = opacity;
         context.drawImage(img[i],0,0);
        }
    }
    var background = mp.colbk; //returns rgb(255,0,0)
    context.fillStyle = background;
    var im = new Image();
    im.src = canvas.toDataURL();
}

Im not sure why my background is not being set.我不知道为什么我的背景没有被设置。 any suggestions?有什么建议?

Thank you in advance.先感谢您。

With context.fillStyle = background , you are NOT setting the background color of the canvas.使用context.fillStyle = background ,您不会设置画布的背景颜色。 Instead, it sets the fill color of the drawing tool for the canvas.相反,它为画布设置绘图工具的填充颜色。

In other words, context.fillStyle only applies to lines or shapes drawn on the canvas afterwards .换句话说, context.fillStyle仅适用于之后在画布上绘制的线条或形状。


To fill the canvas with a color, use the fillRect() function:要用颜色填充画布,请使用fillRect()函数:

context.fillStyle = background;
context.fillRect(0, 0, canvas.width, canvas.height);

This canvas cheat sheet proved to be helpful事实证明,这张画布备忘单很有帮助

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

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