Tomasz
Tomasz

Reputation: 99

Canvas - RGBA in gradient not working

I just want to use rgba to fill an object with gradient. However nothing appears on screen, simple addcolorstop works, just not with rgba.

This works:

 var ctx = this.context;
    var fillColor = ctx.createRadialGradient(100, 100, 10, 100, 100, 70);
    fillColor.addColorStop(0.2, "green");

    ctx.fillStyle = fillColor;
    ctx.beginPath();
    ctx.arc(obj.x, obj.y, obj.r, 0, 2 * Math.PI, true);
    ctx.closePath();
    ctx.fill();

This does not:

var ctx = this.context;
    var fillColor = ctx.createRadialGradient(100, 100, 10, 100, 100, 70);
    grad.addColorStop(0.0, 'rgba(0,0,0,1)');
    grad.addColorStop(0.5, 'rgba(0,0,0,0)');
    grad.addColorStop(1.0, 'rgba(0,0,0,1)');
    ctx.fillStyle = fillColor;
    ctx.beginPath();
    ctx.arc(obj.x, obj.y, obj.r, 0, 2 * Math.PI, true);
    ctx.closePath();
    ctx.fill();

Upvotes: 0

Views: 3537

Answers (1)

Danijel
Danijel

Reputation: 12709

In my case both examples don't work. If I assume that this is the canvas, then getContext() method should be used to return an object for drawing on the canvas:

var ctx = canvas.getContext('2d');
var grd = ctx.createRadialGradient(100, 100, 10, 100, 100, 70);
grd.addColorStop(0, 'rgba(0,0,0,0)');
grd.addColorStop(1, 'rgba(0,0,0,1)');

ctx.fillStyle = grd;
ctx.beginPath();
ctx.arc(150, 150, 100, 0, 2 * Math.PI, true);
ctx.closePath();
ctx.fill();

Upvotes: 1

Related Questions