I want to get the Blob value of an Canvas without Promise. The reason for this is, that I cannot change the outer code and there is no way to wait for the Promise to complete. Using a Promise will raise the problem, that all outer functions will need to use them, too.
I'm already inside a function where the FormData is submitting and this is my last change to inject an image into the already collected FormData.
I found a synchronous way, using the toDataUrl function (side question: is there ANY reason, why this function is synchronous and toBlob is not?).
This is my approach:
public getBlob(dataType?: string, quality?: number): Blob {
let dataUrl: string = this.canvas.toDataURL(dataType, quality)
let i: number;
let byteString: string;
let mimeString: string;
let ia: Uint8Array;
if(dataUrl.split(',')[0].indexOf('base64') >= 0)
byteString = atob(dataUrl.split(',')[1]);
else
byteString = unescape(dataUrl.split(',')[1]);
mimeString = dataUrl.split(',')[0].split(':')[1].split(';')[0];
ia = new Uint8Array(byteString.length);
for(i = 0; i < byteString.length; i++)
ia[i] = byteString.charCodeAt(i);
return new Blob([ia], {type: mimeString});
}
Is there any better and/or faster way to resolve this task?