nvcode
nvcode

Reputation: 343

Why is this not working in Firefox?

Here is the example.

HTML

<body>
    <p>Click on a color to begin!</p>
<canvas id="colorPicker" width="300" height="300"></canvas>
</body>

JavaScript

var elem = document.getElementById('colorPicker');
var context = elem.getContext('2d');
var color, hue = [[255,   0,   0 ],[255, 255,   0 ],[  0, 255,   0 ],[  0, 255, 255 ],[  0,   0, 255 ],[255,   0, 255 ],[255,   0,   0],]
gradient = context.createLinearGradient(0, 0, elem.width, 0);
for (var i = 0; i <= 6; i++) {
    color = 'rgb(' + hue[i][0] + ', ' + hue[i][1] + ', ' + hue[i][2] + ')';
    gradient.addColorStop(i * 1/6, color);
    }
gradient2 = context.createLinearGradient(0, 0, 0, elem.height);
gradient2.addColorStop(0, 'rgba(255, 255, 255, 1)');
gradient2.addColorStop(0.01, 'rgba(255, 255, 255, 1)');
gradient2.addColorStop(0.50, 'rgba(255, 255, 255, 0)');
gradient2.addColorStop(0.52, 'rgba(255, 255, 255, 0)');
gradient2.addColorStop(0.75, 'rgba(0, 0, 0, 1)');
gradient2.addColorStop(1, 'rgba(255, 255, 255, 1)');
context.fillStyle = gradient;
context.fillRect(0, 0, 500, 500);
context.fillStyle = gradient2;
context.fillRect(0, 0, 500, 500);

elem.addEventListener('click', function(e) {
    var x = e.offsetX; // lame but close enough for now
    var y = e.offsetY;
    var data = context.getImageData(x, y, 1, 1).data;
    // just paint it to the bottom right as an example for now:
    var rgb = 'rgb(' + data[0] + ',' + data[1] + ',' + data[2] + ')';
    $('#fluidWrap').css({backgroundColor: rgb});
    $('body').css({backgroundColor: rgb});
    $('.content').css({backgroundColor: rgb});
}, false);

It works in every browser except firefox. Has it something to do with it being an rgb? I had a look and it is fine to use rgb in background-color.

What can I do to make this work?

Upvotes: 0

Views: 2172

Answers (2)

joedborg
joedborg

Reputation: 18353

It doesn't work in IE8 either (for me)

Fixed here http://jsfiddle.net/jdb1991/XR3QF/

Upvotes: 0

GriffinHeart
GriffinHeart

Reputation: 450

If you try using Firebug and breakpoint on the eventHandler you'll see that in Firefox the click event has:

e.clientX
e.clientY

but not e.offset.

Changed it and works for firefox.

Upvotes: 3

Related Questions