First of all, I have to admit that there are dozens of similar issues here in stackoverflow ( this , this, and myriads of others), but all of them are well resolved using the callback function or just put the code inside this image.onload event:
image.onload = function () {
But this is not my business. This is the signature of my function, which is responsible for loading images:
function patternBuilder (index) { var pattern, image, ... ... image = new Image(); image.id = "_" + index + "_"; image.src = "images/_" + index + "_.png"; image.onload = function () { pattern = ctx.createPattern(image, "repeat"); } return pattern;
So I need to come back! I have to do this, and I have no other chance. The reason I should follow this signature is because this template is used by some external library function that looks like this:
style: function (feature) { var pattern = patternBuilder(feature.get("index")); var style = new ol.style.Style({ fill: new ol.style.Fill({ color: pattern }) }); return style; }
So, even if I can change the logic of my patternBuilder function, I still cannot change the external library function. This external function uses patternBuilder and returns the style variable itself. Thus, there is no possibility for callbacks.
source share