gpt4 book ai didi

Convert a canvas element into an image without toDataURL or toBlob(将画布元素转换为不带toDataURL或toBlob的图像)

转载 作者:bug小助手 更新时间:2023-10-22 17:34:49 28 4
gpt4 key购买 nike



I've created a little tool that ends up adding a canvas element to the DOM. I'm unable to usetoBlob or toDataURL as the canvas is (apparently) tainted, producing the following error (in chromium)
Uncaught DOMException: Failed to execute 'toBlob' on 'HTMLCanvasElement': Tainted canvases may not be exported. It errors in the same way with the toDataUrl method.

我创建了一个小工具,它最终向DOM添加了一个画布元素。我无法使用toBlob或toDataURL,因为画布(显然)受到污染,导致以下错误(铬)Uncaught DOMException:无法在“HTMLCanvasElement”上执行“toBlob”:可能无法导出受到污染的画布。它以与toDataUrl方法相同的方式出错。


I'm not sure why this happens, but I've checked and it seems to be something unresolved within the library I'm using. So, is there any way to convert the canvas to an image without using toBlob/toDataURL? Please let me know if further information would be beneficial. Thank you all so much.

我不知道为什么会发生这种情况,但我已经检查过了,在我使用的库中似乎有一些未解决的问题。那么,有没有任何方法可以在不使用toBlob/toDataURL的情况下将画布转换为图像?请告诉我进一步的信息是否有益。非常感谢大家。


更多回答

Have you tried anything from: stackoverflow.com/questions/22710627/… ?

你试过stackoverflow.com/questions/22710627/…吗?

A tainted canvas is one that uses an image resource that was loaded from a different site(domain, protocol, etc) and therefore affected by CORS. So if the site it is from doesnt use a cors header that allows use cross site you cant get the image from the canvas through javascript

受污染的画布是指使用从不同站点(域、协议等)加载的图像资源,因此受到CORS影响的画布。因此,如果它来自的网站没有使用允许跨网站使用的cors标头,则无法通过javascript从画布中获取图像

Thanks for your reply. It's not from another site; it's all being generated locally using a plugin (html2canvas). I'm not sure why it's being identified as tainted, but it's something to do with the library and not with my code, so I'm trying to find a workaround.

谢谢你的回复。它不是来自另一个网站;所有这些都是使用插件(html2canvas)在本地生成的。我不知道为什么它会被识别为受污染,但这与库有关,而不是与我的代码有关,所以我正在努力找到一个解决方法。

Are you on Safari? Try to force the canvas renderer of html2canvas (though that should be the default if you didn't set the foreignObjectRendering option). The SVG renderer uses a <foreignObject> element that will taint Safari's canvas for security reasons. But, why do you need either toBlob or toDataURL exactly?

你在Safari上吗?尝试强制使用html2canvas的画布渲染器(尽管如果您没有设置foreignObjectRendering选项,这应该是默认设置)。SVG呈现器使用了一个<foreignObject>元素,出于安全原因,该元素会污染Safari的画布。但是,为什么您需要toBlob或toDataURL呢?

优秀答案推荐

I got around this issue by turning the images into base64 before adding them to the canvas.

我通过在将图像添加到画布之前将其转换为base64来解决这个问题。


After that, it worked just fine for me to export

在那之后,出口对我来说很好


更多回答

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

您的回答可以通过其他支持信息得到改进。请编辑以添加更多详细信息,如引文或文档,以便其他人可以确认您的答案是正确的。你可以在帮助中心找到更多关于如何写出好答案的信息。

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