Here is one way.
The CanvasContext2D drawImage method is synchronous. Before using this method, the browser must fully display the image.
That way you can use it as a wait method in your waitBIGimage method.
var BIGimage; putBIGimage(); function putBIGimage() { BIGimage = document.createElement("IMG"); BIGimage.height = 200; BIGimage.src = "https://upload.wikimedia.org/wikipedia/commons/c/cf/Black_hole_-_Messier_87.jpg?r=" + Math.random(); BIGimage.onload = waitBIGimage; BIGimage.onerror = console.error; } function waitBIGimage() {
On my Firefox, image processing takes about 1 second, while on my Chrome, the image seems to be already processed when the onload event occurs.
But one big problem of the method is that it is synchronous and thus blocks your scripts during the whole image processing time.
The HTMLImageElement interface has recently been expanded to include the decode decode() method , which should allow us to wait for the image to be ready for drawing, just as you wanted. However, from early tests, I found this method quite deceiving, but this may be due to erroneous early implementations:
var BIGimage; putBIGimage(); function putBIGimage() { BIGimage = document.createElement("IMG"); BIGimage.height = 200; BIGimage.src = "https://upload.wikimedia.org/wikipedia/commons/c/cf/Black_hole_-_Messier_87.jpg?r=" + Math.random(); BIGimage.onload = e => console.log('load event', performance.now()); BIGimage.decode().then(waitBIGimage); BIGimage.onerror = console.error; } function waitBIGimage() { var start = performance.now();
source share