I need to send canvas content to my API endpoint using ajax and wait for the response before moving on to the next function. Here is my current sending function:
function sendPicture(){
var video = document.getElementById('video');
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
if (width && height) {
canvas.width = width;
canvas.height = height;
context.drawImage(video, 0, 0, width, height);
var fd = new FormData();
fd.append('video', null);
var reso;
canvas.toBlob(function(blob){
fd.set('video', blob);
}, 'image/jpeg');
reso = $.ajax({
url: "/img",
type : "POST",
processData: false,
contentType: false,
data : fd,
dataType: "text",
});
return reso;
}
}
The ajax statement works within the toBlob
callback, but I lose access to the main scope. I'd like to find a way to block the ajax promise outside of the callback. Perhaps extracting the blob
argument from the callback scope or ensuring that fd.set('video', blob)
sets the formData object outside where it was initially created would help.
Any suggestions on how to convert the canvas to a blob without using the callback method? Or any ideas on how to fill the formData in the outer scope?