I can think of two approaches I've seen before.
- use an ontimeout call to set an arbitrary length delay
- set a function to handle img.onload
The second method is by far the better of the two - it waits as long as is necessary, no less, no more.
You could probably do it with something like this:
img = document.createElement('img');
img.onload = funtion() { ctx.drawImage(img,sx,sy,sw,sh,dx,dy,dw,dh); }
img.src = imgSrcDataGoesHere;
You may run in to trouble if the code that follows img.src = imgSrcDataGoesHere; exits before the img data is decoded. If that happens, you could use a
closure
wrapped around the local vars to make sure they still exist when you need them.