1

I have code that works fine

function convertImgToBase64URL(url, callback, outputFormat){
    var img = new Image();
    img.crossOrigin = 'Anonymous';
    img.onload = function(){
        var canvas = document.createElement('CANVAS'),
        ctx = canvas.getContext('2d'), dataURL;
        canvas.height = img.height;
        canvas.width = img.width;
        ctx.drawImage(img, 0, 0);
        dataURL = canvas.toDataURL(outputFormat);
        callback(dataURL);
        canvas = null; 
    };
    img.src = url;
}
  
  convertImgToBase64URL('/img/logo.png', function(base64Img){  
    console.log(base64Img);
});

But I need to get the result into a variable

convertImgToBase64URL('/img/logo.png', function(base64Img){  
   var test = base64Img;
});   
alert(test);

Any idea how to pass the result to a variable?

RQEST
  • 107
  • 1
  • 9
  • 2
    Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Sebastian Simon Nov 04 '20 at 07:23

0 Answers0