Javascript 当我点击submit按钮时,如何在img.onload事件上获取Canvas数据URL?

Javascript 当我点击submit按钮时,如何在img.onload事件上获取Canvas数据URL?,javascript,html,canvas,Javascript,Html,Canvas,我想将画布另存为服务器上的图像,我有一个按钮要提交。当我单击save按钮时,我调用了一个函数,该函数在img.onload事件上获取画布数据URL。问题是img.onload是在提交表单后触发的,所以在它从服务器返回之前,我无法获取dataURL值 这是我的密码: <input onclick="savepoint('same');" id="btnsave" type="submit" value="Save Eyes Coordinate" /> 您可以将type=submi

我想将画布另存为服务器上的图像,我有一个按钮要提交。当我单击save按钮时,我调用了一个函数,该函数在img.onload事件上获取画布数据URL。问题是img.onload是在提交表单后触发的,所以在它从服务器返回之前,我无法获取dataURL值

这是我的密码:

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

您可以将type=submit更改为type=button,然后在img.onload事件处理程序中提交表单。@路人,谢谢,伙计,我找到了另一个解决方案。非常感谢您的帮助。这个老问题在没有给出解决方案的情况下就解决了,所以我想我们现在可以以打字错误/无法生产的方式结束这个问题。
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,/, "");
    }
}