title: "Tainted canvases may not be loaded" Cross domain issue with WebGL textures
body: <p>I've learnt a lot in the last 48 hours about cross domain policies, but apparently not enough.</p>
  
  <p>Following on from <a href="https://stackoverflow.com/questions/30926654/cant-get-access-control-allow-origin-header-to-work-as-i-expected">this</a> question. My HTML5 game supports Facebook login. I'm trying to download profile pictures of people's friends. In the HTML5 version of my game I get the following error in Chrome.</p>
  
  <blockquote>
    <p>detailMessage: "com.google.gwt.core.client.JavaScriptException:
    (SecurityError) ↵ stack: Error: Failed to execute 'texImage2D' on
    'WebGLRenderingContext': Tainted canvases may not be loaded.</p>
  </blockquote>
  
  <p>As I understand it, this error occurs because I'm trying to load an image from a different domain, but this can be worked around with an <strong>Access-Control-Allow-Origin</strong> header, as detailed in <a href="https://stackoverflow.com/questions/9972049/cross-origin-data-in-html5-canvas">this</a> question.</p>
  
  <p>The URL I'm trying to download from is</p>
  
  <p><a href="https://graph.facebook.com/1387819034852828/picture?width=150&amp;height=150" rel="noreferrer">https://graph.facebook.com/1387819034852828/picture?width=150&amp;height=150</a></p>
  
  <p>Looking at the network tab in Chrome I can see this has the required <strong>access-control-allow-origin</strong> header and responds with a 302 redirect to a new URL. That URL varies, I guess depending on load balancing, but here's an example URL.</p>
  
  <p><a href="https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xap1/v/t1.0-1/c0.0.160.160/p160x160/11046398_1413754142259317_606640341449680402_n.jpg?oh=6738b578bc134ff207679c832ecd5fe5&amp;oe=562F72A4&amp;__gda__=1445979187_2b0bf0ad3272047d64c7bfc2dbc09a29" rel="noreferrer">https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xap1/v/t1.0-1/c0.0.160.160/p160x160/11046398_1413754142259317_606640341449680402_n.jpg?oh=6738b578bc134ff207679c832ecd5fe5&amp;oe=562F72A4&amp;<strong>gda</strong>=1445979187_2b0bf0ad3272047d64c7bfc2dbc09a29</a></p>
  
  <p>This URL also has the <strong>access-control-allow-origin</strong> header. So I don't understand why this is failing.</p>
  
  <p>Being Facebook, and the fact that thousands of apps, games and websites display users profile pictures, I'm assuming this is possible. I'm aware that I can bounce through my own server, but I'm not sure why I should have to.</p>
  
  <h1>Answer</h1>
  
  <p>I eventually got cross domain image loading working in libgdx with the following code (which is pretty hacky and I'm sure can be improved). I've not managed to get it working with the AssetDownloader yet. I'll hopefully work that out eventually.</p>
  
  <pre><code>public void downloadPixmap(final String url, final DownloadPixmapResponse response) {
      final RootPanel root = RootPanel.get("embed-html");
      final Image img = new Image(url);
      img.getElement().setAttribute("crossOrigin", "anonymous");
      img.addLoadHandler(new LoadHandler() {
  
          @Override
          public void onLoad(LoadEvent event) {
              HtmlLauncher.application.getPreloader().images.put(url, ImageElement.as(img.getElement()));
              response.downloadComplete(new Pixmap(Gdx.files.internal(url)));
              root.remove(img);
          }
      });
      root.add(img);
  }
  
  interface DownloadPixmapResponse {
      void downloadComplete(Pixmap pixmap);
  
      void downloadFailed(Throwable e);
  }
  </code></pre>