Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically use RGBa values in fillStyle in <canvas>?

Using <canvas>, I want to set the RGBa value of the rectangle using a variable.

for example:

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)"; 

doesn't work.

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?

like image 256
John Avatar asked Dec 24 '09 08:12

John


1 Answers

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 + ")";  
like image 103
Christian C. Salvadó Avatar answered Sep 20 '22 15:09

Christian C. Salvadó