he canvas has been tainted by cross-origin data and tainted canvases may not be exported

来自: https://ourcodeworld.com/articles/read/182/the-canvas-has-been-tainted-by-cross-origin-data-and-tainted-canvases-may-not-be-exported

These errors happens when you try to manipulate an image on a canvas that doesn't seems to have the legitim permission to be handled for your code.

They're related (and caused) by the Access-Control-Allow-Origin header of a request (and allowed by the server). The image is from another domain, therefore this behaviour is disallowed in most browsers as this would be a security breach.

What is a tainted canvas?

The HTML specification introduces a crossorigin attribute for images that, in combination with an appropriate CORS header, allows images defined by the img element loaded from foreign origins to be used in canvas as if they were being loaded from the current origin.

See CORS settings attributes for details on how the crossorigin attribute is used.

Although you can use images without CORS approval in your canvas, doing so taints the canvas. Once a canvas has been tainted, you can no longer pull data back out of the canvas. For example, you can no longer use the canvas toBlob()toDataURL(), or getImageData() methods; doing so will throw a security error.

The canvas has been tainted by cross-origin data

Analyze the following code :

var canvas = document.getElementById("canvas"); function drawImageFromWebUrl(sourceurl){ var img = new Image(); img.addEventListener("load", function () { // The image can be drawn from any source canvas.getContext("2d").drawImage(img, 0, 0, img.width, img.height, 0, 0, canvas.width, canvas.height); // However the execution of any of the following methods will make your script fail // if your image doesn't has the right permissions canvas.getContext("2d").getImageData(); canvas.toDataURL(); canvas.toBlob(); }); img.setAttribute("src", sourceurl); } // But, this code is being executed from ourcodeworld and the image fcomes from google. drawImageFromWebUrl("https://www.google.de/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png");

If you have this issue, your code may have some of the methods in common and the image doesn't come from your domain.

Tainted canvases may not be exported

Uncaught SecurityError: Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported.

You'll find this error if you was smart enought to think :

Hey, if i can get the data of a tainted canvas from another domain, let's convert it into a base64 string and then redraw it.

- You, Nobel prize philosophy - 2016

But not smart enough to think that do this action is being blocked too as the image "doesn't belong to you".

Possible solutions

Javascript

You may able to prevent this error by simply setting in your image the crossorigin attribute (with Javascript or HTML) :

<img src="otherdomain.com" crossorigin="Anonymous" /> <!-- Or with Javascript --> <script> var image = new Image(); image.crossOrigin = "Anonymous"; ... </script>

However, this will only work if your server response has the following header on it :

Access-Control-Allow-Origin "*"

Otherwise you'll get instead a new error :

Image from origin 'otherdomain.com' has been blocked from loading by Cross-Origin Resource Sharing policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'yourdomain.com' is therefore not allowed access.

Server side

As the problem is that the image doesn't come from your domain, you can create a proxy with your server language (the same technique used for display http (insecure) content in https (secure) content).

The workflow (in your server language, PHP, C# etc) should be :

 

You can read more about this technique here and may be the force with you.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
"Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported"是一个报错信息,意味着当使用canvas的toDataURL()方法时,画布上存在被污染的图像(即跨域的图像),不能导出。原因是在使用drawImage()方法将跨域的图片绘制到canvas上。解决方法包括以下两个步骤: 1. 在引用图片之前设置跨域资源允许权限。这可以通过在创建image对象之后设置image对象的crossOrigin属性为'anonymous'来实现。示例代码如下: ``` var img = new Image(); img.setAttribute('crossOrigin', 'anonymous'); img.src = '图片地址'; ``` 请确保在设置crossOrigin属性之后再设置图片的src属性。 2. 确保存放图片的服务器开启了跨域允许权限。否则,可能会出现"No 'Access-Control-Allow-Origin' header is present on the requested resource"的错误提示。你可以联系服务器管理员或者在服务器端配置相应的跨域请求头。 通过以上两个步骤,你应该能够解决"Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported"的问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported [已解决]....](https://blog.csdn.net/qq_35516360/article/details/122065183)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [Canvas引入跨域的图片导致toDataURL()报错的问题的解决](https://download.csdn.net/download/weixin_38693419/14830620)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [canvas生成图片toDataURL报错(Uncaught DOMException: Failed to execute ‘toDataURL‘ on ‘HTMLCanvasEl...](https://blog.csdn.net/sumimg/article/details/115375085)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值