javascript-为什么img onload后不能返回值?

时间:2019-09-01 15:31:20

标签: javascript canvas onload

我使用img.onload并在图像加载时打开base64,当我使用console.log()时,它可以显示该值,但是当我返回时,它不会显示文字

var image108Base642 = getImgbace64();
console.log(image108Base642); //no value

    function getImgbace64() {

        dicomimg.src = url;
        dicomimg.onload = function(){
            var canvas = document.createElement("canvas");
            canvas.width = dicomimg.width;
            canvas.height = dicomimg.height;
            var cntx = canvas.getContext("2d");
            cntx.drawImage(dicomimg,0,0,dicomimg.width,dicomimg.height);
            var dataURL = canvas.toDataURL("image/png");
            //image to base64
            trnbase64 = getPixelData(dataURL);
            console.log(trnbase64);
            return trnbase64;
        }
    }

0 个答案:

没有答案