I'm try to load image into canvas and text over it, then download it with text.
I successfully load image into canvas and text over it. but when I try to download the image with text it's download black, after many attempts, I tried to ger the canvas's "toDataURL" and discovered it's blank.
I don't know what's the problem, but I think it's get the toDataURL before the image loaded.
here's my piece of code
function addTextToImage(imagePath, text) {
var circle_canvas = document.getElementById("canvas");
var context = circle_canvas.getContext("2d");
// Draw Image function
var img = new Image();
img.src = imagePath;
img.onload = function () {
context.drawImage(img, 0, 0);
context.lineWidth = 1;
context.fillStyle = "#F00";
context.lineStyle = "#F00";
context.font = "18px sans-serif";
context.fillText(text, 50, 50);
};
}
addTextToImage("myImage.jpeg", "My Name is Muhammad");
console.log(circle_canvas.toDataURL());
<canvas id="canvas" width="340" height="340"></canvas>
** Edit The image I wanna write over it, it's from my PC