开发者

fill style in javascript

开发者 https://www.devze.com 2023-03-30 02:43 出处:网络
I haveto fill an image with r g b a component. It is working with function something.fillStyle = \" rgba (\"+r+\",\"+g+\",\"+b+\",\"+a+\")\"; but i want to use something.fillStyle = value where value

I have to fill an image with r g b a component. It is working with function something.fillStyle = " rgba ("+r+","+g+","+b+","+a+")"; but i want to use something.fillStyle = value where value is the color value in hex of the form alpha r g b. When value is #ffff00 (yellow) it is fine but when I use a value with alpha (#ffffff00) it is always black. Is there any 开发者_开发知识库way to do this?


This is not possible, see http://www.w3.org/TR/css3-color/#numerical. Hex color values don't allow setting the "alpha" value, that's only possible with rgba and hsla.


It is possible, if you convert the hex to rgb and then add your alpha value. Also, you can set the globalAlpha which will apply even if the rest of the colors in the function are using hex colors.

if (myAlpha) {
    ctx.globalAlpha = myAlpha;
}

take a look at the hextorbg function here.

0

精彩评论

暂无评论...
验证码 换一张
取 消