当我点击提交按钮时,如何在img.onload事件上获取Canvas DataURL?

时间:2013-12-23 07:39:51

标签: javascript html5 canvas

我想将画布保存为服务器上的图像,我有一个提交按钮。当我点击保存按钮时,我调用了一个在img.onload事件上获取画布dataURL的函数。提交表单后问题是img.onload被触发,所以在从服务器返回之前我无法获得dataURL值。
我希望你尽快回答我,这是我的代码:

 <input onclick="savepoint('same');" id="btnsave" type="submit" value="Save Eyes Coordinate" />
function savepoint(btn) {

    SavePreviewImg();
}

function SavePreviewImg() {
    //Other code here to get the image src and other stuff..

    // here the excution of the function doesn't go into onload event it skip it
    // and after the click event finished it comes to onload event
    img.onload = function () {
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        // here i draw the first image
        ctx.drawImage(img, 480, 320);//210,10

        // here i draw the second image and put it on the first
        //image 
        ctx.save();
        ctx.translate(mx, my);
        ctx.rotate(angle * TO_RADIANS);
        ctx.drawImage(imge2, -(width / 2), -(height / 2), width, height);

        ctx.restore();

        //get img data url
        dataURL = canvas.toDataURL("image/png");//.replace(/^data:image\/(png|jpg);base64,/, "");
    }
}

0 个答案:

没有答案