The base64 obtained from the canvas compressed image shows black when the picture is displayed, and the picture is not fully displayed.

sometimes the picture will be a little darker and sometimes it will be completely black. I don"t see what"s wrong with the code. I hope God can help me answer
/ /-ID card upload-

//document.getElementById("img").addEventListener("change", function() {
$("-sharpimg_wrapper").on("change",".input_img",function(){
    var reader = new FileReader();
    var that = this;
    var nodeId = $(this).parent().parent().attr("id");
    reader.onload = function (e) {
        console.log("");
        //console.log(e.target.result);
        compress(e.target.result, fileSize,nodeId);
        compress_show(e.target.result, fileSize,nodeId,that);
    };
    reader.readAsDataURL(this.files[0]);
    var fileSize = Math.round(this.files[0].size / 1024 / 1024); //M
});

/ / Image compression
function compress (res, fileSize,nodeId) {/ / res represents the uploaded picture, and the size of the fileSize-sized picture

    console.log("");
    var img = new Image(),
        maxW = $("-sharpsfzm").width() * 3, //
        maxH = $("-sharpsfzm").height() * 3; //
    console.log(9999);
    img.onload = function() {
        var cvs = document.createElement("canvas"),
            ctx = cvs.getContext("2d");
        if(img.width > maxW) {
            img.height *= maxW / img.width;
            img.width = maxW;
        } else {
            img.width *= maxH / img.height;
            img.height = maxH;
        }
        cvs.width = img.width;
        cvs.height = img.height;

        ctx.clearRect(0, 0, cvs.width, cvs.height);
        ctx.drawImage(img, 0, 0, img.width, img.height);

        var compressRate = getCompressRate(1, fileSize);

        var dataUrl = cvs.toDataURL("image/jpeg", compressRate).replace("data:image/jpeg;base64,","");



        selectPicture(nodeId,dataUrl);

        console.log(cvs);

    }
    img.src = res;

}

/ / Compression ratio

function getCompressRate(allowMaxSize, fileSize) { //sizeMB
    var compressRate = 1;
    if(fileSize / allowMaxSize > 4) {
        compressRate = 0.5;
    } else if(fileSize / allowMaxSize > 3) {
        compressRate = 0.6;
    } else if(fileSize / allowMaxSize > 2) {
        compressRate = 0.7;
    } else if(fileSize > allowMaxSize) {
        compressRate = 0.8;
    } else {
        compressRate = 0.9;
    }
    console.log(compressRate);
    return compressRate;
}
Apr.03,2021

have you solved the problems you encountered? What causes it?

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1ea9c9d-1f81.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1ea9c9d-1f81.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?