处理 imageData 的 Web Worker 使用 Firefox,但不适用于 Chrome
当我运行处理将 imageData 传递给网络工作者然后返回的代码时,Firefox 工作得很好,但 Chrome 给出了“未捕获的错误:DATA_CLONE_ERR:DOM 异常 25”
搜索 google 表明旧版本的 Chrome 曾经可以工作?
我又检查了一些,似乎我需要在发送图像数据之前运行 JSON.stringify 和 JSON.parse,但随后它就停止工作了。在 FF 9 中运行的代码是:
image.js:
var myImageData = context.getImageData(0, 0, canvas.width, canvas.height).data;
var worker = new Worker("http://direct.link/helpers/worker.js");
worker.postMessage(myImageData);
worker.onmessage = function(event) {
var value = event.data;
switch (value.cmd){
case 'last':
//doing stuff
break;
default:
//doing stuff
});
}
worker.js:
addEventListener('message', function(event) {
var myImageData = event.data;
// doing stuff.
sendItBack(colors);
});
};
function sendItBack(colors){
each(colors, function(index, value){
self.postMessage(value);
});
self.postMessage({'cmd': 'last'});
}
我应该使用什么方法来将此图像数据来回发送到应用程序和 Web Worker?
谢谢!
编辑:
如果我复制到常规数组,则 Chrome 开始工作...
var newImageData = [];
for (var i=0,len=myImageData.length;i<len;++i) newImageData[i] = myImageData[i];
因此 chrome 无法将 CanvasPixelArray 传递给工作人员,但它可以传递常规数组。但火狐可以。
When I run code that deals with imageData being passed to a web worker and then back, then Firefox works great but Chrome gives "Uncaught Error: DATA_CLONE_ERR: DOM Exception 25"
Searching google suggests that older versions of Chrome used to work?
I checked some more and it seemed as if I needed to run JSON.stringify and JSON.parse on the imagedata before sending it but then it stops working everywhere. The code that works in FF 9 is:
image.js:
var myImageData = context.getImageData(0, 0, canvas.width, canvas.height).data;
var worker = new Worker("http://direct.link/helpers/worker.js");
worker.postMessage(myImageData);
worker.onmessage = function(event) {
var value = event.data;
switch (value.cmd){
case 'last':
//doing stuff
break;
default:
//doing stuff
});
}
worker.js:
addEventListener('message', function(event) {
var myImageData = event.data;
// doing stuff.
sendItBack(colors);
});
};
function sendItBack(colors){
each(colors, function(index, value){
self.postMessage(value);
});
self.postMessage({'cmd': 'last'});
}
What method should I use in order to send this imagedata back and forth the app and the web worker?
Thanks!
EDIT:
If I copy to a regular array then Chrome starts working...
var newImageData = [];
for (var i=0,len=myImageData.length;i<len;++i) newImageData[i] = myImageData[i];
So chrome can't pass a CanvasPixelArray to a worker but it can pass a regular Array. But firefox can.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我所做的是将整个 imagedata 对象从上下文传递给工作人员,而不仅仅是数据:
使用这种方法,您将一些额外的属性传递给工作人员(即数组的宽度和高度),但我认为这位额外数据比复制整个数据数组相关的开销要好。
What I do is pass the entire imagedata object from the context to the worker, and not just the data:
With this approach you're passing a few extra attributes to the worker (namely, the array's width and height), but I think this bit of extra data is better than the overhead associated with copying the entire data array.
将此作为后续发布。我可以重现您的错误,以防其他人可以回答您的问题(不幸的是我不能)。我搜索了 Chromium 问题,看看这是否是一个突出的错误,但没有发现任何东西。鉴于图像处理是 WebWorkers 更流行的用途之一,我希望有人能够快速回答您。
http://jsfiddle.net/gGFSJ/9/
来自 Chrome(星号是我添加的):
来自火狐浏览器:
Posting this as follow up. I can reproduce your error in case anyone else can answer your question (I can't unfortunately). I've searched the Chromium issues to see if it's an outstanding bug, but not found anything. Given image processing is one of the more popular uses of WebWorkers I would hope someone can answer you quickly.
http://jsfiddle.net/gGFSJ/9/
From Chrome (asterisks added by me):
From Firefox:
如果我将 getimagedata.data 数组复制到常规数组,然后将该数组传递给网络工作者,那么 Chrome 就会开始工作。
因此,chrome 无法将 CanvasPixelArray 传递给工作人员,但它可以传递常规数组。但 Firefox 可以直接传递imagedata.data。
If I copy the getimagedata.data array to a regular array and then pass the array to a webworker then Chrome starts working.
So chrome can't pass a CanvasPixelArray to a worker but it can pass a regular Array. But Firefox can pass an imagedata.data directly.