I am trying to pass an image dataURI as an input from a url link to the image. I understand to do this I must use canvas and convert it from there. However, since this is an 'async' method I can't produce a return.
getLogo() {
let image = new Image();
let canvas = document.createElement('canvas');
return new Promise((resolve) => {
image.onload = () => {
canvas.width = image.naturalWidth;
canvas.height = image.naturalHeight;
canvas.getContext('2d').drawImage(image, 0, 0);
let uri = canvas.toDataURL('image/png');
resolve(uri);
};
image.src = this.logo;
});
}
getLogoURI() {
this.getLogo().then((result) => {
console.log(result); // the correct output writes here
return result; // this returns undefined
});
}
I then call this from my class in which I need the URI, this is within a for loop.
let logo = tariff.getLogoURI();
I believe when calling getLogoURI(), this automatically treats it as a synchronous function and therefore doesn't return the right value, but I am unsure.