I'm using new Image(), to preload images like so:
function preload() {
var imgs = [];
for (i = 0; i < arguments.length; i++) {
imgs[i] = new Image();
imgs[i].src = arguments[i];
}
return imgs;
}
//function being called like so
var paths = ["../images/image01.jpg","../images/image02.jpg", ...];
var images = preload.apply(null, paths);
This returns an array of img objects... with all of the properties emptied when displayed with the console.log()
My question is - how can I get width and height of every preloaded image?
Or even more - is it possible in some mysterious way to get those dimensions earlier (some sort of pre-reading-pre-loaded-pre-image), so I could just create those preloaded images particular size?
I'm sorry if I don't understand something obvious on "how these things works", it happens to me quite often.
JUST FOR CLARIFICATION
As @Waterscroll pointed out, loading images takes time, so getting image properties has to be done when .onload event occurs. Later on, images can be handled by some callback function.
Feel free to take a look at the other answers as they show more practical approach to the topic.
If I may add something - it may be beneficial in some cases to store image dimensions i.e. in a database. In this particular script I'm getting file paths from a database, so I could do just that with the size of an image too.
argumentsfirst declared ?argumentsdoesn't need to be declared. It is merely "an array-like object corresponding to the arguments passed to the function." - MDN