0

I have this function that converts a image url to base64 string when i call the function i lost the globalvar data.

Script:

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

And call

        convertImgToBase64(url, function(base64Img){
            //console.log(imageData);
            globalvar = base64Img;
        });

But globalvar lose the data after exit from anonymous function. How can I access the value after the function?

Akhilesh Singh
  • 1,724
  • 2
  • 19
  • 35

0 Answers0