以编程方式在< canvas>中的fillStyle中使用RGBa值 [英] Programmatically use RGBa values in fillStyle in <canvas>?
本文介绍了以编程方式在< canvas>中的fillStyle中使用RGBa值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用< canvas>
,我想使用变量设置矩形的RGBa值。
Using <canvas>
, I want to set the RGBa value of the rectangle using a variable.
例如:
ctx.fillStyle = "rgba(32, 45, 21, 0.3)";
工作正常,但使用变量:
works fine, but using it with a variable:
var r_a = 0.3;
ctx.fillStyle = "rgba(32, 45, 21, r_a)";
不起作用。
显然 fillStyle
只接受字符串。那么如何使用某个变量设置rgba值的值而不是显式定义值呢?
Apparently fillStyle
only accepts a string. So how do I set a value of the rgba value using some variable instead of explicitly defining the values?
推荐答案
你只需要连接 r_a
变量以正确构建字符串:
You just need to concatenate the r_a
variable to build the string correctly:
var r_a = 0.3;
ctx.fillStyle = "rgba(32, 45, 21, " + r_a + ")";
这篇关于以编程方式在< canvas>中的fillStyle中使用RGBa值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文