title: JavaScript Image blocked by CORS
body: <p>I have this code below that consists of a HTML Tab and a modal what i have done below is that i used the library <code>HTML2Canvas</code> to capture the selected <code>divs</code>. But the problem is when i click download it doesn't capture the image in my downloaded file. </p>
  
  <p>It keeps having this error <code>Access to Image at '//IMAGE LINK' from origin 'http://localhost' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.</code></p>
  
  <p>I have tried to add crossorigin:anonymous to my img but still no luck. Is there an easy way to solve this? any help would be greatly appreciated.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function sendData() {
  
  
    $('#myModal2').addClass('modal-posit');
  
    var modalButton = $('[data-target="#myModal2"]')[0];
    modalButton.click();
    var modal = $('#myModal2')[0];
    
  
  
    setTimeout(function() {
      html2canvas(document.getElementById('capture'), {
        allowTaint: false,
        useCORS: true
      }).then(function(canvas) {
          downloadCanvas(document.getElementById('test'), canvas, 'test.png');
          modalButton.click();
      });
    }, 1000);
  }
  
  function openCity(evt, cityName) {
    var i, tabcontent, tablinks;
    tabcontent = document.getElementsByClassName("tabcontent");
    for (i = 0; i &lt; tabcontent.length; i++) {
      tabcontent[i].style.display = "none";
    }
    tablinks = document.getElementsByClassName("tablinks");
    for (i = 0; i &lt; tablinks.length; i++) {
      tablinks[i].className = tablinks[i].className.replace(" active", "");
    }
    document.getElementById(cityName).style.display = "block";
    evt.currentTarget.className += " active";
  }
  
  function downloadCanvas(link, canvas, filename) {
      link.href = canvas.toDataURL();
      link.download = filename;
      link.click();
  }
  
  document.getElementById("defaultOpen").click();</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>body {
    font-family: Arial;
  }
  
  .tab {
    overflow: hidden;
    border: 1px solid #ccc;
    background-color: #f1f1f1;
    margin-top: 10px;
    border-top-left-radius: 8px;
    border-top-right-radius: 8px;
  }
  
  
  /* Style the buttons inside the tab */
  
  .tab button {
    background-color: inherit;
    float: left;
    border: none;
    outline: none;
    cursor: pointer;
    padding: 14px 16px;
    transition: 0.3s;
    font-size: 17px;
    border-bottom: 8px;
  }
  
  
  /* Change background color of buttons on hover */
  
  .tab button:hover {
    background-color: #ddd;
  }
  
  
  /* Create an active/current tablink class */
  
  .tab button.active {
    background-color: #ccc;
  }
  
  
  /* Style the tab content */
  
  .tabcontent {
    display: none;
    padding: 6px 25px;
    border: 1px solid #ccc;
    border-top: none;
    -webkit-animation: fadeEffect 1s;
    animation: fadeEffect 1s;
    border-bottom-left-radius: 8px;
    border-bottom-right-radius: 8px;
    background-color: white;
  }
  
  .jobs-panel {
    display: table;
    max-height: 100%;
    width: 85%;
    background-color: #b7bcbe;
    margin-left: auto;
    margin-right: auto;
    margin-top: 25px;
    margin-bottom: 25px;
    padding-bottom: 20px;
    padding-top: 20px;
  }
  
  .tabwidth {
    width: 85%;
    margin: 0 auto;
  }
  .modal-posit{
    position: relative;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;!DOCTYPE html&gt;
  &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt;
  &lt;link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"&gt;
  &lt;script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"&gt;&lt;/script&gt;
  &lt;html&gt;
  
  &lt;head&gt;
      &lt;meta charset="utf-8" /&gt;
      &lt;style&gt;
  
  
      &lt;/style&gt;
      &lt;link rel="shortcut icon" href="//#" /&gt;
      &lt;script type="text/javascript" src="https://html2canvas.hertzen.com/dist/html2canvas.js"&gt;&lt;/script&gt;
      &lt;script type="text/javascript" src="https://html2canvas.hertzen.com/dist/html2canvas.min.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
      &lt;div id="capture"&gt;
      
  
        &lt;div class="jobs-panel"&gt;
    
          &lt;button class="modal-button" data-toggle="modal" data-target="#myModal2"&gt;MODAL BUTTON&lt;/button&gt;
    
          &lt;div class="tabwidth"&gt;
            &lt;div class="tab"&gt;
              &lt;button class="tablinks" onclick="openCity(event, 'Graph')" id="defaultOpen"&gt;Graph&lt;/button&gt;
            &lt;/div&gt;
    
            &lt;div id="Graph" class="tabcontent"&gt;
              &lt;img src="https://s3-ap-southeast-1.amazonaws.com/investingnote-production-webbucket/attachments/41645da792aef1c5054c33de240a52e2c32d205e.png" width="300" height="300" &gt;
            &lt;/div&gt;
    
            &lt;div id="Paris" class="tabcontent"&gt;
              &lt;h3&gt;Paris&lt;/h3&gt;
              &lt;p&gt;Paris is the capital of France.&lt;/p&gt;
            &lt;/div&gt;
    
            &lt;div id="Tokyo" class="tabcontent"&gt;
              &lt;h3&gt;Tokyo&lt;/h3&gt;
              &lt;p&gt;Tokyo is the capital of Japan.&lt;/p&gt;
            &lt;/div&gt;
          &lt;/div&gt;
        &lt;/div&gt;
        &lt;div class="modal fade" id="myModal2" role="dialog"&gt;
          &lt;div class="modal-dialog modal-lg"&gt;
            &lt;div class="modal-content"&gt;
              &lt;div class="modal-header"&gt;
                &lt;button type="button" class="close" data-dismiss="modal"&gt;&amp;times;&lt;/button&gt;
                &lt;h2 class="modal-title center"&gt;FAQ&lt;/h2&gt;
              &lt;/div&gt;
              &lt;div class="modal-body"&gt;
                &lt;div class="central"&gt;
                  &lt;h3 class="bold-text "&gt;QUESTIONS
                  &lt;/h3&gt;
                &lt;/div&gt;
              &lt;/div&gt;
              &lt;div class="modal-footer"&gt;
                &lt;button type="button" class="btn btn-default" data-dismiss="modal"&gt;Close&lt;/button&gt;
              &lt;/div&gt;
            &lt;/div&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
   
      &lt;button id="match-button" onclick="sendData();"&gt;capture&lt;/button&gt;
      &lt;a id="test" href="#"&gt;&lt;/a&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  
  &lt;script&gt;
  
  &lt;/script&gt;</code></pre>
  </div>
  </div>
  </p>