I receive a URL of an image from a database. What I'm looking for is to get the width and height of the image to form a carousel. To do so, I instantiate an image object inside a for loop, but I can not obtain the dimensions because it is not yet loaded. To solve it I use onload, but I can not get the data inside the onload function to receive it in $ data in the Vue js component.
The dataphoto structure is correct, if it is tested outside of onload it works correctly.
dataphoto": [
{
"id_foto": "107",
"nombre": "Chrysanthemum.jpg",
"ruta": "picsloaded/93/Chrysanthemum.jpg",
"toma": "93",
"width": 0,
"height": 0
}
....
]
getPictureData: function() {
var self = this;
var foto = [];
for (var i = 0; i < this.dataphoto.length; i++) {
foto = new Image();
foto.src = "/static/api/" + this.dataphoto[i].ruta;
foto.onload = (function(nr){
self.dataphoto[nr]["width"] = foto.width;
self.dataphoto[nr]["height"] = foto.height;
})(i);
}
},