6

For some reason, my firefox doesn´t show radial gradient when using a Canvas, does anyone know why? (I don´t have this problem on other computers)

here is some of the code I´m using:

var canvas = document.getElementById ( "layer2" ) ; 
var context = canvas.getContext ( "2d" ) ;   
var radgrad2 = context.createRadialGradient( x, y, 15 ,x-30,y-60, 0);
radgrad2.addColorStop(0, aux.color , .5);
radgrad2.addColorStop(0.75, "#ffffff" , .5 );
radgrad2.addColorStop( .5, "#ffffff" , .5);
context.fillStyle = radgrad2;

ps: I have this problem only in Firefox (it´s updated)

Camilo Barraza
  • 195
  • 1
  • 9

3 Answers3

0

I've found an odd problem in Firefox with SVG and radialGradients. If I define the fill in a CSS class and embed the CSS into the document it works fine, however if I move the CSS into a seperate file and use the 'link' tag to include the CSS, then the radialGradient doesn't work. This seems to be a bug in Firefox as it works in Chrome, Safari, Opera and even IE, but not in Firefox.

SPlatten
  • 5,334
  • 11
  • 57
  • 128
0

I'm not sure, but if this code work on other PCs under FireFox then may be you have an old version of the FireFox browser or calling document.getElementById before cavas tag with id "layer2" to be loaded. The other problem I noticed was that you using float numbers without leading zero. For example 0.5 instead of 0.5. What happens when you run this code?

window.addEventListener("load", function() {
            var canvas = document.getElementById ( "layer2" ) ; 
            if(!canvas.getContext) {
                alert("Your browser don't support canvas.");
            throw new Error("Your browser don't support canvas.");  
            }
            var context = canvas.getContext ( "2d" ) ;   
            var radgrad2 = context.createRadialGradient( x, y, 15 ,x-30,y-60, 0);
            radgrad2.addColorStop(0, aux.color , 0.5);
            radgrad2.addColorStop(0.75, "#ffffff" , 0.5 );
            radgrad2.addColorStop(0.5, "#ffffff" , 0.5);
            context.fillStyle = radgrad2;

}, false);
Georgi Naumov
  • 4,160
  • 4
  • 40
  • 62
0

After searching for months I now have an answer to this elusive question. Mozilla changed how their radial gradients work. To create a simple radial gradient you no longer need to create a path. You simply fill a rectangle instead. See below code example:

var radgrad2 = ctx.createRadialGradient(105,105,20,112,120,50);
radgrad2.addColorStop(0, '#FF5F98');
radgrad2.addColorStop(0.75, '#FF0188');
radgrad2.addColorStop(1, 'rgba(255,1,136,0)');

ctx.fillStyle = radgrad2;
ctx.fillRect(0,0,150,150);

This method is incredibly more efficient than creating paths with a radial gradient. Although, I can also see this method being a bit more limiting to developers. See this example from Mozilla's developer network for a better example: https://developer.mozilla.org/samples/canvas-tutorial/4_10_canvas_radialgradient.html

brainjam
  • 18,863
  • 8
  • 57
  • 82
Ash Blue
  • 5,344
  • 5
  • 30
  • 36