为什么这在 Firefox 中不起作用?

发布于 2024-12-10 09:31:45 字数 1928 浏览 0 评论 0原文

这里是示例

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);

它适用于除 Firefox 之外的所有浏览器。和它是rgb有关系吗?我看了一下,在 background-color

我该怎么做才能使这项工作成功?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半窗疏影 2024-12-17 09:31:45

如果您尝试在 eventHandler 上使用 Firebug 和断点,您将看到在 Firefox 中单击事件有:

e.clientX
e.clientY

但没有 e.offset

更改它并适用于 Firefox。

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.

对你的占有欲 2024-12-17 09:31:45

它在 IE8 中也不起作用(对我来说)

已修复
http://jsfiddle.net/jdb1991/XR3QF/

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文