0

What im trying to do:

  1. Draw an image on my canvas
  2. Get the pixel data on that canvas
  3. for each pixel in the pixel data, if the pixel is completely white (RGB value of 255,255,255) then store it in an array so that i will end up with an array of positions on the canvas where each white pixel is


What my problem is:

On step 2 when i try to get the pixel data from the canvas, it throws me the following error:

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

Details:

So i have 2 scripts: main.js and ParticleTitle.js all im doing in main.js is initialize the canvas (setting its size and what not) and then creating a ParticleTitle object and parsing the canvas context as well as how wide and high i want the image to be.

The ParticleTitle.js script looks like this:

var ParticleTitle = 
{
ctx: null,
title_img: null,
width: null,
height: null,
possiblePxs: Array(),


Create: function(ctx_,w,h)
{
    var obj = Object.create(this);
    obj.ctx = ctx_;
    obj.width = w * (obj.ctx.canvas.width);
    obj.height= h * (obj.ctx.canvas.height);
    obj.title_img = new Image();
    obj.title_img.src = "imgres/up_arrow.png";
    obj.init();
    return obj;
},

init: function()
{
    this.ctx.save();
    this.ctx.translate(this.ctx.canvas.width/2,this.ctx.canvas.height/2);
    this.ctx.drawImage(this.title_img,-this.width/2,-this.height/2,this.width,this.height); 
    this.ctx.restore();

    var pixelData =     
  this.ctx.getImageData(0,0,this.ctx.canvas.width,this.ctx.canvas.height); 
 //error thrown here

    },


Update: function()
{

},
};

All the "Create" function does is: initialize the context, calculate the width and height of the image, initializes the image, and then run the init function

The init function then just draws the image on to the canvas and then after that, when i try to use the "getImageData" function i get thrown the error.

anyone know whats wrong and how i might fix it? thanks.

  • 1
    Did you actually read or google the error? – zero298 Oct 26 '17 at 17:48
  • i did, its something about you aren't allowed to get pixel data from another websites images but that doesn't make any sense in this case since im just trying to get it from my own canvas? –  Oct 26 '17 at 17:49
  • Are you loading any external resources from another domain anywhere in your code? Are you drawing any images from another domain onto your canvas? Post enough code to reproduce the issue. – zero298 Oct 26 '17 at 17:52
  • think i found the issue. its a bit wired but apparently it happened because i was using the same image as a background of a div (which i had set in css by using: background:url("..\imgres\up_arrow.png"); ) anyways, it works now :D –  Oct 26 '17 at 18:06
  • How are you loading your page, does your address bar look something like `file://C:/some/local/directory/index.html`? – zero298 Oct 26 '17 at 18:08
  • yeah it does, do you think you know why it was causing it? –  Oct 26 '17 at 18:10
  • Yes, see this answer to [Same origin policy, google chrome, canvas and file:// scheme](https://stackoverflow.com/a/12587751/691711). Stop using the `file://` protocol and use a local server implementation. Most IDE have one built in. You are going to run into far more issues if you keep trying to develop that way. – zero298 Oct 26 '17 at 18:11

0 Answers0