【发布时间】:2020-06-08 17:32:27
【问题描述】:
我将 ImageBitmap 传递给 webworker,在画布上呈现,然后生成一个 URL 以加载到主线程上的 THREE.js。
在主线程中
this.canvas = this.canvasEl.transferControlToOffscreen()
this.workerInstance.postMessage({canvas: this.canvas}, [this.canvas]);
...
createImageBitmap(this.img).then(imageData => {
this.imageBitmap = imageData
this.workerInstance.postMessage({image:imageData}, [imageData])
在工人中
_ctx.drawImage(image, 0, 0)
_canvas.convertToBlob({type: "image/png"}).then(blob => console.log(blob))
DOMException:无法在“OffscreenCanvas”上执行“convertToBlob”: 可能无法导出受污染的“OffscreenCanvas”。
主线程上的图像有crossorigin="anonymous"。它也确实复制了另一个图像,但这是同一个域。
图像是动态创建的:
docString = '<svg width="' + this.width + '" height="' + this.height + '" xmlns="http://www.w3.org/2000/svg"><defs><style type="text/css"><![CDATA[a[href]{color:#0000EE;text-decoration:underline;}' + this.cssembed.join('') + ']]></style></defs><foreignObject x="0" y="0" width="' + this.width + '" height="' + this.height + '">' + parent[0] + docString + parent[1] + '</foreignObject></svg>';
this.img.src = "data:image/svg+xml;utf8," + encodeURIComponent(docString);
代码在这里https://github.com/supereggbert/aframe-htmlembed-component/blob/master/src/htmlcanvas.js
我正在更新它以使用 Offscreen canvas 和 web workers 来加快渲染速度
【问题讨论】:
-
你有没有在没有跨域的情况下加载该图像?可能是缓存问题,请参阅stackoverflow.com/questions/49503171/… 无论如何,我们需要看看您是如何加载该图像的,因为作为跨域加载的图像不应该污染您的画布,并且具有
crossorigin属性的图像但是根本不应该加载服务器未正确提供的服务(因此会使 createImageBitmap 抛出)。另外,如果您只是尝试使用 inscreen 画布的 2D 上下文会怎样? -
啊...一个带有foreignObject的svg...这是一个非常重要的信息,它与跨域数据无关,正如您所说,您确实生成了该图像。 There was this 前段时间我写过,但看来你已经在使用解决方法了,也许他们对 ImageBitmap 做了什么坏事,我会检查一下。
标签: javascript canvas three.js