Im trying to create an image using canvas api. The problem is the image isnt loading no matter what I try, here is my code snippet, Im waiting for the image to load but the generated image doesnt have a background. Can anyone spot what I've done wrong? Thanks in advance:
const backgroundImage = new Image();
backgroundImage.src = '../Assets/pattern.png';
return (backgroundImage.onload = () => {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
canvas.width = width;
canvas.height = height;
if (!ctx) {
return '';
}
ctx.drawImage(backgroundImage, 0, 0, width, height);
ctx.fillRect(0, 0, width, height);
ctx.font = 'bold italic 48px Montserrat';
ctx.fillStyle = 'white';
return canvas.toDataURL('image/png');
});
I've tried different import styles, I've tried using other image sources (unsplash) but no luck