0

I have this code below that consists of a tab with a image inside of it and i'm using the library HTML2Canvas to capture the entire div and download it but i'm not sure why it is not capturing the image.

I tried adding the configuration option allowTaint:true but it still keeps giving me this error DOMException: Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported. I'm not sure why this keep happening but any help would be greatly appreciated thanks.

function sendData() {
    html2canvas(document.getElementById('capture')).then(function(canvas) {
      $('#test').append(canvas);
      $('#test').attr('href', canvas.toDataURL('image/png'));
      $('#test').attr('download', 'Test.png');
      $('#test')[0].click();
    });
  }


  function openCity(evt, cityName) {
    var i, tabcontent, tablinks;
    tabcontent = document.getElementsByClassName("tabcontent");
    for (i = 0; i < tabcontent.length; i++) {
      tabcontent[i].style.display = "none";
    }
    tablinks = document.getElementsByClassName("tablinks");
    for (i = 0; i < tablinks.length; i++) {
      tablinks[i].className = tablinks[i].className.replace(" active", "");
    }
    document.getElementById(cityName).style.display = "block";
    evt.currentTarget.className += " active";
  }

  document.getElementById("defaultOpen").click();
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;
    }
<!DOCTYPE html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<html>

<head>
  <meta charset="utf-8" />
 
  <link rel="shortcut icon" href="//#" />
  <script type="text/javascript" src="https://html2canvas.hertzen.com/dist/html2canvas.js"></script>
  <script type="text/javascript" src="https://html2canvas.hertzen.com/dist/html2canvas.min.js"></script>
</head>

<body>
  <div id="capture">
    <h1>Hello My man</h1>
    <div class="jobs-panel">

      <p>In this example, we use JavaScript to "click" on the London button, to open the tab on page load.</p>

      <div class="tabwidth">
        <div class="tab">
          <button class="tablinks" onclick="openCity(event, 'London')" id="defaultOpen">London</button>
          <button class="tablinks" onclick="openCity(event, 'Paris')">Paris</button>
          <button class="tablinks" onclick="openCity(event, 'Tokyo')">Tokyo</button>
        </div>

        <div id="London" class="tabcontent">
          <h3>London</h3>
          <img src="https://cdn.bulbagarden.net/upload/thumb/4/49/Ash_Pikachu.png/250px-Ash_Pikachu.png" width="300" height="300">
          <p>London is the capital city of England.</p>
        </div>

        <div id="Paris" class="tabcontent">
          <h3>Paris</h3>
          <p>Paris is the capital of France.</p>
        </div>

        <div id="Tokyo" class="tabcontent">
          <h3>Tokyo</h3>
          <p>Tokyo is the capital of Japan.</p>
        </div>
      </div>
    </div>
  </div>
  <div id="match-button" onclick="sendData();">capture</div>
  <a id="test" href="#"></a>
</body>

</html>
Best Jeanist
  • 1,109
  • 4
  • 14
  • 34

1 Answers1

1

change

<img src="https://cdn.bulbagarden.net/upload/thumb/4/49/Ash_Pikachu.png/250px-Ash_Pikachu.png" width="300" height="300">

to

<img src="https://cdn.bulbagarden.net/upload/thumb/4/49/Ash_Pikachu.png/250px-Ash_Pikachu.png" width="300" height="300" crossorigin>

and in your js make allowTaint: false;

it will work fine,

update jsfiddel -> http://jsfiddle.net/qp4b8frw/14/

JpG
  • 774
  • 8
  • 22
  • In the fiddle it shows that the image is broken and when i download the image its still doesn't capture it – Best Jeanist Jul 06 '18 at 15:39
  • you are using the cdn "https://cdn.bulbagarden.net/upload/thumb/4/49/Ash_Pikachu.png/250px-Ash_Pikachu.png" which is not accessible from "http://fiddle.jshell.net" so store that cdn image in local and use the local image path in it wll work fine as expected or wat u can do is store your pikachu image here, "https://postimages.org/" and use that url... updated fiddle http://jsfiddle.net/qp4b8frw/15/ – JpG Jul 09 '18 at 05:51