I want to draw a border around an image after it is loaded using new Image()
This works, I see the border, but modifying image.src inside the onload function calls onload again in an infinite loop. But how can I alter the image otherwise?
let image = new Image();
image.onload = function () {
console.log("onload");
let canvas = document.createElement('canvas');
let ctx = canvas.getContext('2d');
ctx.drawImage(image, 0, 0);
ctx.strokeStyle = 'red';
ctx.lineWidth = 5;
ctx.strokeRect(0, 0, image.width, image.height);
image.src = canvas.toDataURL();
};
image.src = "https://placehold.co/600x400";
document.body.appendChild(image);