gpt4 book ai didi

javascript - 歌剧错误 : Uncaught exception: TypeError: Cannot convert 'xxxxxx' to object

转载 作者:行者123 更新时间:2023-11-29 15:52:19 30 4
gpt4 key购买 nike

我今天来展示 opera 在 Jquery 中抛出的错误,关于对象转换,这是代码(函数 setColor(x,y) ):

colourpixel = $('#colour').css('background-color').match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);//["rgb(0, 70, 255", "0", "70", "255"]

var canvas = document.createElement('canvas');
canvas.height=190;
canvas.width=190;
canvascontext = canvas.getContext("2d");
defaultdata = $('#light').get(0).getContext("2d").getImageData(0,0,190,190);
canvascontext.putImageData(defaultdata,0,0);

canvascontext.globalCompositeOperation = 'destination-atop';
canvascontext.fillStyle='rgb( '+colourpixel[1]+', '+colourpixel[2]+', '+colourpixel[3]+')';

这是歌剧抛出的错误:

Uncaught exception: TypeError: Cannot convert 'colourpixel' to object  
Error thrown at line 157, column 1 in setColor(x, y) in file://localhost/home/angelus/Desarrollo/webs/ColorP/functions.js:
canvascontext.fillStyle='rgb( '+colourpixel[1]+', '+colourpixel[2]+', '+colourpixel[3]+')';
called from line 61, column 2 in <anonymous function>(event) in file://localhost/home/angelus/Desarrollo/webs/ColorP/functions.js:
setColor(x,y);
called from line 55, column 294 in <anonymous function: handle>(a) in file://localhost/home/angelus/Desarrollo/webs/ColorP/jquery.min.js:
i=i.handler.apply(this,arguments);
called via Function.prototype.apply() from line 49, column 569 in <anonymous function: o>() in file://localhost/home/angelus/Desarrollo/webs/ColorP/jquery.min.js:
return typeof c!=="undefined"&&!c.event.triggered?c.event.handle.apply(o.elem,arguments):w

我尝试创建像数组一样的对象 (var colourpixel = new Array();) 但没有运行。

提前致谢!

最佳答案

我不确定最佳解决方案,因为我根本不处理这种颜色情况,但问题是:

在 Opera 中,当您将样式设置为 rgb(...) 时,例如:

<div id="colour" style="background-color: rgb(0, 70, 255);">​

在大多数浏览器中,$('#colour').css('background-color') 你会得到:"rgb(0, 70, 255)",但在 Opera 中不是这样,您将得到 "#0046ff" 的十六进制格式,因此您的正则表达式将不匹配,colourpixel 将是 null,不是匹配数组。这会导致您的错误,与 null[1] 一样。

Here's a quick test to demonstrate this , 在任何其他主流浏览器中测试它,然后是 Opera。

关于javascript - 歌剧错误 : Uncaught exception: TypeError: Cannot convert 'xxxxxx' to object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3899985/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com