最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Uncaught SecurityError: Failed to execute 'getImageData' on 'CanvasRenderingContext2D&#

programmeradmin1浏览0评论

I am getting this error in Chrome and Opera Browsers:

Uncaught SecurityError: Failed to execute 'getImageData' on 'CanvasRenderingContext2D': The canvas has been tainted by cross-origin data.

The code is working fine in Internet Explorer, Mozilla Firefox, and Safari. But I need to fix it in Chrome and Opera. Please help me to find a solution to fix this problem?

I am getting this error at this line

imgData = ctx.getImageData(x1,y1,w,h);

I am getting this error in Chrome and Opera Browsers:

Uncaught SecurityError: Failed to execute 'getImageData' on 'CanvasRenderingContext2D': The canvas has been tainted by cross-origin data.

The code is working fine in Internet Explorer, Mozilla Firefox, and Safari. But I need to fix it in Chrome and Opera. Please help me to find a solution to fix this problem?

I am getting this error at this line

imgData = ctx.getImageData(x1,y1,w,h);
Share Improve this question edited Nov 1, 2014 at 10:33 Bo A 3,1542 gold badges34 silver badges49 bronze badges asked Nov 1, 2014 at 10:10 RakeshRakesh 611 gold badge1 silver badge2 bronze badges 1
  • 1 Possible duplicate of getImageData() error The canvas has been tainted by cross-origin data – Mosh Feu Commented Jan 21, 2016 at 11:34
Add a comment  | 

2 Answers 2

Reset to default 15

May be this will help, as you have mentioned cross origin so try this,

 var UimageObj = new Image();

crossOrigin has to be set to enable the canvas data to be saved.The source image should have access-control-allow-origin set to * or a chosen domain

UimageObj.crossOrigin = 'anonymous';   // crossOrigin attribute has to be set before setting src.If reversed, it wont work.  
UimageObj.src = obj_data.srcUser;

Hope it helps.

When you load your html file from disk using: file://path/to/your/file.html, then Google Chrome and Opera will raise error at line including: imgData = ctx.getImageData(x1,y1,w,h);

Solution is simple: start web server (apache, nginx) put your html file somewhere at web server and load your html file from: http://localhost/somewhere/file.html

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论