vendredi 14 août 2015

Javascript Context.fillStyle = variable not changing colors

The following code should assign the color of a particle at instantiation. However, for some reason the color is not being assigned.

function Particle(x, y, r, color)
{
    this.x = x || 300;
    this.y = y || 300;
    this.r = r || 1;
    this.xv = 2;
    this.yv = 2;
    this.color = color | "#ff00ff";
}


Particle.prototype.render = function()
{
    context.beginPath();
    context.arc(this.x, this.y, this.r, 2 * Math.PI, false);
    context.fillStyle = this.color;
    context.fill();
}

However I can change the color of the particle by manually assigning the color in render like so.

Particle.prototype.render = function()
{
    context.beginPath();
    context.arc(this.x, this.y, this.r, 2 * Math.PI, false);
    context.fillStyle = "#ff00ff";
    context.fill();
}

I would love to know why the color is not being assigned correctly. Thanks!




Aucun commentaire:

Enregistrer un commentaire