HTML5 canvas color help

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 that should return one of these 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 display 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, this does not work? Any ideas why?

To see the full code, see the source here: http://dev.driz.co.uk/pool

+3
source share
2 answers

This should solve your problem.

function colors() {
    return clr[Math.floor(Math.random() * clr.length)];
}

Demo

+2
source

, colors(), , "'#ffffff'". , :

122: gradient.addColorStop(.85, ball[i].color);

, , — , . . colors():

function colors() 
{
    return cls[Math.floor(Math.random() * clr.length)];
}
+4

Source: https://habr.com/ru/post/1778779/


All Articles