调整画布大小不会更改数据

时间:2013-10-16 08:20:49

标签: javascript canvas

我目前正在使用canvas来调整图像大小,然后将调整后的图像发送到我的服务器。我已经完成了这项工作,并且使用canvas进行罚款。但是对于这个要求,画布将由用户调整大小。我是能够正确调整画布大小并从画布发送调整大小的图像。但发送的图像不是调整大小的图像,而是原始图像。 这是我绘图部分的代码:

function drawImageOnCanvas(me){
    var file = me.files[0];         
    var fileReader = new FileReader();
    fileName = file.name;

    //make the div visible
    $j(".dv2").show();

    fileReader.onloadend = function(e) {
        var tempImg = new Image();
        var dataURL;
        tempImg.src = this.result;
        tempImg.onload = function() {
            var MAX_WIDTH = 1000;
            var MAX_HEIGHT = 800;
            var tempW = tempImg.width;
            var tempH = tempImg.height;
            if (tempW > tempH) {
                if (tempW > MAX_WIDTH) {
                    tempH *= MAX_WIDTH / tempW;
                    tempW = MAX_WIDTH;
                }
            } else {
                if (tempH > MAX_HEIGHT) {
                    tempW *= MAX_HEIGHT / tempH;
                    tempH = MAX_HEIGHT;
                }
            }            
            var canvas = document.getElementById('myCanvas');
            canvas.width = tempW;
            canvas.height = tempH;                  
            var ctx = canvas.getContext("2d");
            ctx.drawImage(this, 0, 0,tempW,tempH);
            $j("#jq_stretch").height(tempH);
            $j("#jq_stretch").width(tempW);
            $j("#jq_hw_size").text(tempW + " x " + tempH);
        }
    }
    fileReader.onerror = function(e) {
        alert("There was an error reading the file.  Please try again.");
    }
    fileReader.onabort = function(e) {
        alert("There was an error reading the file.  Please try again.");
    }
    fileReader.readAsDataURL(file); 
}

然后我让用户调整了部分:

$j(function() {
    $j("#jq_stretch").resizable();
    $j("#jq_stretch").resize(function(){
        $j("#jq_hw_size").text($j(this).width() + " x " + $j(this).height());
    });
});

这是我的发送部分:

function uploadImage(){
    var canvas = document.getElementById('myCanvas');
    var attachment = canvas.toDataURL("image/jpeg");
    attachment = attachment.slice(23);
    var positionIndex = 0;
    var fileSize = attachment.length;   
    var chunkSize = 950000;        //Maximum Javascript Remoting message size is 1,000,000 characters       
    var fileBody = "";
    showMsg('waitmsg',1);

    if(fileSize <= positionIndex + chunkSize) {
        fileBody = attachment.substring(positionIndex);
    } else {
        fileBody = attachment.substring(positionIndex, positionIndex + chunkSize);
    }
    //now try to upload
    VFC99_ImageUploader.doUploadAttachment(
        '{!parentId}', 
        fileBody, 
        fileName, 
        function(result, event) { 
            if(event.status){                 
                if(result == "successful upload") displayMsg(true,result);
                else displayMsg(false,result);
            }
            else 
                displayMsg(false,event.message);

            //finish the waiting msg
            showMsg('waitmsg',0);
        },
        {buffer: true, escape: true, timeout: 120000}
    );
}

1 个答案:

答案 0 :(得分:1)

我得到它的工作。事实上,我正在调整包含画布的div,所以它不会改变画布数据,但画布正在调整大小,但我认为它只是视觉。解决方案是重绘用户大小的画布:

    var canvas = document.getElementById('myCanvas');
    //just added the 6 lines and it works great
var w = $j("#jq_stretch").width();
var h = $j("#jq_stretch").height();
canvas.width = w;
canvas.height = h;
var ctx = canvas.getContext("2d");
    ctx.drawImage(tempImg, 0, 0,w,h);
    var attachment = canvas.toDataURL("image/jpeg");