来自: 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");
 Copy snippet

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.
 Copy snippet

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>
 Copy snippet

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

Access-Control-Allow-Origin "*"
 Copy snippet

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.
 Copy snippet

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相关推荐

  1. Jetty Cross Origin Filter解决jQuery Ajax跨域访问的方法

    当使用jQuery Ajax post请求时可能会遇到类似这样的错误提示 XMLHttpRequest cannot load http://xxxxxx. Origin http://xxxxxx ...

  2. Access to script at ‘xxx‘ from origin ‘null‘ has been blocked by CORS policy: Cross origin requests

    前言 本地调试js时,遇到本错误 Google Chrome 84.0.4147.135 (正式版本) (64 位) (cohort: Stable Installs Only) 修订版本 c42bd ...

  3. 【chrome错误】Cross origin requests are only supported for protocol schemes: http, data,chrome-extension

    使用ajax请求本地文件,chrome会报跨域错误:XMLHttpRequest cannot loadfile:///C:/Users/Li/Desktop/images/alist.json.Cr ...

  4. [NodeJS] Jest 环境下 Axios 请求报错: Cross origin http://localhost forbidden

    文章目录 问题概述 解决方案 参考 本博客站点已全量迁移至 DevDengChao 的博客 https://blog.dengchao.fun , 后续的新内容将优先在自建博客站进行发布, 欢迎大家访 ...

  5. 【VSCode】from origin ‘null‘ has been blocked by CORS policy: Cross origin requests are only supported

    Access to script at 'file:///C:/Users/dawulei/Desktop/%E9%A1%B9%E7%9B%AE/%E5%9D%A6%E5%85%8B%E5%A4%A7 ...

  6. 解决Cross origin requests are only supported for protocol schemes问题

    解决Cross origin requests are only supported for protocol schemes问题 问题描述 本前端小白在学习的时候尝试按照一个网上的blog的代码来跑 ...

  7. 【跨域异常】Cross origin requests are only supported for protocol schemes: http, data, chrome,chrome-exten

    一.背景描述 下面是我用ajax异步请求时的写法,请注意url是怎么写的,下面的错误就是由于这个url的写法导致的. // 获取省份列表 $(document).ready(function () { ...

  8. Cross origin requests are only supported for protocol schemes: http, data, chrome-extension, edge

    Cross origin requests are only supported for protocol schemes: http, data, chrome-extension, edge, h ...

  9. ajax跨域请求问题:Access to XMLHttpRequest at……has been blocked by CORS policy: Cross origin requests

    背景:本地代码调试时,加载本地资源正常,jquery的AJAX请求服务端资源报错. Access to XMLHttpRequest at '***** ' from origin 'null' ha ...

最新文章

  1. 全球首个开放应用模型 OAM 开源 | 云原生生态周报 Vol. 23
  2. C++ GUI Qt4编程(12)-6.1FindFileDialog
  3. dqn在训练过程中loss越来越大_[动手学强化学习] 2.DQN解决CartPole-v0问题
  4. 【数据结构与算法】二叉树
  5. php ztree异步加载数据格式,zTree异步加载简单demo
  6. Metapath2vec:Scalable Representation Learning for Heterogeneous Networks(结构化深度网络特征表示)
  7. vue引入id3_使用决策树ID3算法,预测收入是否大于50k
  8. 拍摄女生人像,摄影师觉得最大的困难是什么?
  9. 二叉树的创建_【数据结构用python描述】python创建二叉树
  10. Linux 安装 Hive教程
  11. axure rp pro入门
  12. 【收藏】B站科普硬核节目
  13. web 自动化测试(入门篇)
  14. 内联函数什么时候展开_内联函数
  15. EBS开发_收款核销API
  16. 互联网公司的技术博客汇总-阿里腾讯百度等
  17. 中国历史“四大谜案”
  18. c语言冒泡排序与二分法排序
  19. 大数据架构师之路 | 概念术语理解
  20. CSS3基础知识及详情

热门文章

  1. 一些算法分析中的常用级数
  2. PuTTY的下载安装和基本使用方法教程
  3. odoo:onchange与constrain区别
  4. 小学python都学哪些内容_python是什么?连小学生都开始学Python,你还等什么?
  5. shell小技巧(八十二)彩票选号器
  6. 洛谷 Markdown - 从入门到精通
  7. 记一次模拟生成数据发到kafka的bug
  8. mfc和mysql连接 dao_MFC,C与C++程序与MySql数据库连接 ——陈绍钦 csq
  9. KnockoutJs简单入门知识讲解
  10. 分布式框架(一):分布式协议