HTML5 Canvas 颜色帮助
您好,我的变量中有一个简单的数组,其中包含一些颜色
var clr = ['#FF0000', '#0000FF', '#FFFF00', '#008000', '#FFA500', '#800080', '#ffffff'];
,然后是一个函数,该函数应该在单引号中返回这些值之一,
function colors() {
var color;
color = "'";
color += Math.floor(Math.random() * clr.length);
color += "'";
return color;
}
然后调用该函数来显示各种彩色球
function CreateBall(x, y, vx, vy, r, s) {
this.color = colors();
this.x = x;
this.y = y;
this.vx = vx;
this.vy = vy;
this.r = r;
this.size = s;
}
但是它不起作用?有什么想法吗?
要查看完整代码,请查看此处的源代码:http://dev.driz.co.uk/池
Hi I have this simple array inside my variable with some colors
var clr = ['#FF0000', '#0000FF', '#FFFF00', '#008000', '#FFA500', '#800080', '#ffffff'];
and then a function which should return one of those values in single quotes
function colors() {
var color;
color = "'";
color += Math.floor(Math.random() * clr.length);
color += "'";
return color;
}
this function is then called to show various colored balls
function CreateBall(x, y, vx, vy, r, s) {
this.color = colors();
this.x = x;
this.y = y;
this.vx = vx;
this.vy = vy;
this.r = r;
this.size = s;
}
However it doesn't work? Any ideas why?
To see the full code, please look at the source here: http://dev.driz.co.uk/pool
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
目前,
colors()
返回的值是一个类似于"'#ffffff'"
的字符串。当实际应用此颜色值时,以下行会引发语法错误:这可能是由您在返回的颜色上加上单引号引起的 - 您不需要这样做是因为它已经是一个字符串值。您也从未真正从数组中提取值。将其用于您的
colors()
函数:At the moment, the value returned from
colors()
is a string that looks something like"'#ffffff'"
. When this colour value is actually applied a syntax error is thrown on the following line:This is likely to be caused by the single quotes you're wrapping around to your returned colour — you don't need to do this since it's already a string value. You also never actually pluck the values out of your array. Use this for your
colors()
function:这应该可以解决您的问题
演示
This should fix your problem
Demo