在< canvas&gt ;?中的fillStyle中编程使用RGBa值? [英] Programatically use RGBa values in fillStyle in <canvas>?

查看:142
本文介绍了在< canvas&gt ;?中的fillStyle中编程使用RGBa值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用< canvas> 学习编程。我想使用一个变量设置矩形的RGBa值。

I'm learning programming with <canvas>. I want to set the RGBa value of the rectangle using a variable.

例如


var r_a = 0.3;

ctx.fillStyle =rgba(32,45,21,r_a);



不起作用。但 ctx.fillStyle =rgba(32,45,21,0.3); 可以工作。

fillStyle 只接受字符串。那么如何使用一些变量来设置rgba值的值,而不是显式定义这些值呢?

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

这篇关于在&lt; canvas&gt ;?中的fillStyle中编程使用RGBa值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆