0

I want to load a png-image into a canvas get the base64-string from it.

This is my code:

pic2b64: function () {

    var canvas = document.createElement("canvas");

    var ctx = canvas.getContext('2d');
    var img = new Image();
    img.src = "http://pngimg.com/uploads/dog/dog_PNG50346.png";

    img.addEventListener('load', e => {
        ctx.drawImage(img, 0, 0);
        img.crossOrigin = "Anonymous";
        var dataURL = canvas.toDataURL("image/png");
    })

},

When I want to execute my code, I get the following error in the browser:

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

(programmed in javascript, executed in browser)

barbsan
  • 3,418
  • 11
  • 21
  • 28
Alfons-Alpi
  • 107
  • 11
  • Please take a look at this SO question: [Tainted canvases may not be exported](https://stackoverflow.com/questions/22710627/tainted-canvases-may-not-be-exported) – enxaneta May 20 '19 at 11:32
  • I tried all this, but nothing help me. Maybe its a problem with my code? – Alfons-Alpi May 20 '19 at 11:49
  • 1
    I've tested your code in Codepen and it works for me. Please take a look: https://codepen.io/enxaneta/pen/f941bfd65075fac9eb5194e69e187d5d – enxaneta May 20 '19 at 12:43

0 Answers0