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,