2

I need to convert my div in image or base64 using JS.

I found the html2canvas library on the Internet that performs this procedure, but always displays the error:

Uncaught SecurityError: Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported.

When I add the element "canvas" it shows no error, but does not generate the base64 and neither image

I did the code with and without the canvas, Find the code below.

With Canvas

<script type="text/javascript">  
  var canvas = document.createElement('canvas');
  var map = document.getElementById("mapa");

  canvas.appendChild(map);

  html2canvas(canvas, { 
    useCORS: true,
    onrendered: function(canvas) {
    var dataUrl= canvas.toDataURL("image/png");
    console.log("Imagem - dataurl",dataUrl);
    var encodedPng = dataUrl.substr(dataUrl.indexOf(',') + 1, dataUrl.length);
    var decodedPng = Base64Binary.decode(encodedPng);
  }});

Without Canvas

<script type="text/javascript">     
  var canvas = document.getElementById("mapa");
  html2canvas(canvas, { 
    useCORS: true,
    onrendered: function(canvas) {
    var dataUrl= canvas.toDataURL("image/png");
    console.log("Imagem - dataurl",dataUrl);
    var encodedPng = dataUrl.substr(dataUrl.indexOf(',') + 1, dataUrl.length);
    var decodedPng = Base64Binary.decode(encodedPng);
  }});

I appreciate any help, as it already does not know what to do to accomplish this task!

Tks,

Sunil Hari
  • 1,716
  • 1
  • 12
  • 20
  • 1
    From what I have read, this seems like a CORS issue. Are you accessing this off of your local file system? If so, try making a test server on your own computer. I think it has to do with accessing local files. See this: http://stackoverflow.com/questions/22710627/tainted-canvases-may-not-be-exported – Jason Aug 07 '14 at 19:44
  • 1
    It contains images outside of your domain, thus the error. – Derek 朕會功夫 Aug 07 '14 at 19:52
  • 1
    I'm actually taking the picture from google, so this causing the error. I'll have to change how I look the picture, put it locally. Thank you very much, – Moisés Pereira Aug 07 '14 at 21:26

0 Answers0