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.
he canvas has been tainted by cross-origin data and tainted canvases may not be exported的更多相关文章
- Ajax本地跨域问题 Cross origin requests are only supported for HTTP
问题:打开本地html文件时,报错如下 Cross origin requests are only supported for protocol schemes: http, data,chrome ...
- Blocking Cross Origin API request for /api/contents Creating Notebook Failed An error occurred while creating a new notebook.
anacoda安装的jupyter,使用nginx进行了转发,远程访问可以进去,但是创建文件和创建目录都会报错 浏览器页面报错: 第一次使用jupyter创建python时错误:Creating No ...
- jquery读取本地文件,Windows上报错。XMLHttpRequest cannot load xxx. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.k.cors.a.c
问题: 测试报告,使用本地的json.txt文件,结果文件读取失败,报错如下: XMLHttpRequest cannot load xxx. Cross origin requests are on ...
- CORS (Cross Origin Resources Share) 跨域
CORS 跨域 1 什么是跨域问题 基于安全考虑,浏览器会限制使用脚本发起任何跨域请求. 所谓的跨域请求,就是与当前页面的 http/ip/port 不一样的请求. 但在实际运用中,跨域获取数据的需求 ...
- nodejs报错 XMLHttpRequest cannot load localhost:3000/test_date/. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.
z在请求本地的时候 如果ajax的URL 前面没有http的话 就会报错 jq.js:2 XMLHttpRequest cannot load localhost:3000/test_date/. ...
- Cross origin requests are only supported for protocol schemes: http, data, chrome,chrome-extension的问题
Cross origin requests are only supported for protocol schemes: http, data, chrome,chrome-extension的问 ...
- 用临时用户数据目录启动Chrome,关闭安全检查等(解决Cross origin requests are only supported for HTTP?)
Cross origin requests are only supported for HTTP? 参考:https://www.zhihu.com/question/20948649 批处理: s ...
- 【chrome错误】Cross origin requests are only supported for protocol schemes: http, data,chrome-extension, https, chrome-extension-reso
使用ajax请求本地文件,chrome会报跨域错误. XMLHttpRequest cannot loadfile:///C:/Users/Li/Desktop/images/alist.json.C ...
- 利用 pyhon 解决 Cross Origin Requests
在学习 ajax 时遇到了一个问题 XMLHttpRequest cannot load file:xxxxxxxx . Cross origin requests are only supporte ...
随机推荐
- DOM事件监听器
DOM事件监听器,允许一个事件触发多个方法.在实际工作中应用比较多. 它的调用形式如下: <body> <div> DOM事件监听器,允许一个事件触发多个方法. </di ...
- 《剑指offer》-连续子数组的最大和
题目描述 HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学.今天测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决.但是,如果向量 ...
- 安卓在代码中设置TextView的drawableLeft、drawableRight、drawableTop、drawableBottom
Drawable rightDrawable = getResources().getDrawable(R.drawable.icon_new); //调用setCompoundDrawables时, ...
- poj 2752 求一个字符串所有的相同前后缀
求一个字符串所有的相同前后缀Sample Input ababcababababcababaaaaaSample Output 2 4 9 181 2 3 4 5 #include <iostr ...
- 乐观锁和悲观锁及CAS实现
乐观锁与悲观锁 悲观锁:总是假设最坏的情况,每次去拿数据的时候都认为别人会修改,所以每次在拿数据的时候都会上锁,这样别人想拿这个数据就会阻塞直到它拿到锁.传统的关系型数据库里边就用到了很多这种锁机制, ...
- 【AtCoder】ARC083
C - Sugar Water 计算一下可以达到水是多少,可以到达的糖是多少 枚举水,然后加最多能加的糖,是\(min(F - i *100,E * 100)\),计算密度,和前一个比较就行 #inc ...
- Linux 文件系统与挂载详解
https://blog.csdn.net/baidu_34051990/article/details/60963947
- P2700 逐个击破 最小生成树
题目描述 现在有N个城市,其中K个被敌方军团占领了,N个城市间有N-1条公路相连,破坏其中某条公路的代价是已知的,现在,告诉你K个敌方军团所在的城市,以及所有公路破坏的代价,请你算出花费最少的代价将这 ...
- 【Java】 剑指offer(5) 从尾到头打印链表
本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集 题目 输入一个链表的头结点,从尾到头反过来打印出每个结点的值.结点定义如下: ...
- poj2186-Popular Cows【Tarjan】+(染色+缩点)(经典)
<题目链接> 题目大意: 有N(N<=10000)头牛,每头牛都想成为most poluler的牛,给出M(M<=50000)个关系,如(1,2)代表1欢迎2,关系可以传递,但 ...