javascript - Radial Gradient in canvas doesn't fade properly -


i trying make radial gradient javascipt/html canvas. problem gradients don't overlap if alpha channel isn't working.

this code using:

var gradient1 = ctx.createradialgradient(300, 300, 300, 300, 300, 0); gradient1.addcolorstop(0,"rgba(0, 0, 0, 0)"); gradient1.addcolorstop(1,"#ff0000"); ctx.fillstyle = gradient1; ctx.fillrect(x1, y1, 600, 600); ctx.fillrect(x1, y1, 600, 600); 

here picture:

http://i.imgur.com/tv5r48h.png

this reason fades black-like color rather staying red. leads act weird when 2 of these gradients of different colors touching.

how can make react normally?

cause

the gradient defined is red-black , both color , alpha channel interpolated . @ 50% halfway between red , black, 50% visible why becoming black-ish.

cure

to fix make sure both color stops same color alpha channel changed. keep color same way:

gradient1.addcolorstop(0, "rgba(255, 0, 0, 0)"); // red, transparent gradient1.addcolorstop(0, "#f00");               // red, solid 

click link below see in action:

var ctx = document.queryselector("canvas").getcontext("2d");    var gradient1 = ctx.createradialgradient(300, 300, 300, 300, 300, 0);  gradient1.addcolorstop(0,"rgba(255, 0, 0, 0)");  gradient1.addcolorstop(1,"#ff0000");  ctx.fillstyle = gradient1;  ctx.fillrect(0, 0, 600, 600);  ctx.fillrect(0, 0, 600, 600);
<canvas width=600 height=600></canvas>


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -