Hello everyone (medium level in p5 / English is not my native language…)
I am currently making my website in p5, it is almost finished.
It’s quite simple, a sort of slideshow.
I would like to have a loading bar for loading my images.
For aesthetic reasons but above all for the practical side :
I currently have a small graphics bug when the following image is not loaded.
(when transitioning from one image to another) (these are big images)
(graphic bug disappears when the image is finally loaded)
I would like to have a “loading bar” to visually validate the end of loading the image
and launch the transition.
There is no function in p5 to get the quantity of bytes loaded or the size of the image.
How can I do this? It’s possible?
I searched a little on my side,
but I prefer to ask here before going on a false trail.
You may want to consider using a callback function.
The callback would tell you when you are done loading.
You may be able to adapt this for your code to proceed after loading.
One approach would be to put up a ‘thumbnail’ (scaled down) image on the bar as each image is loaded. The following reference shows how to create different sized images from the same file using resize(): https://p5js.org/reference/#/p5.Image
Ok, thank you both, I’ll look into that.
I will report back after some tests.
(I’m not sure of your solutions because I need to know the weight of each image)
(the images are loaded while viewing the site, not in preloading)
(I load the first image in preloading)
Hey, maybe you want to share the current status of the project or example code? I feel you are asking about something specific and a live example would tell much more
Write some code to test this and be sure.
You will not know the size of the image until after loading it.
I tested this with code.
That is fine. Load one at a time and don’t keep reloading in draw.
Consider adding some variables for the state of the loading and control flow of code.
That is ok. I did this as well and used the callback (may not be necessary since it is preload and will test more later).
I built on the example I shared to load one image after another only after each was completed loading. I used a variable called loading and set it to true of false to control the flow of the program and loading.
Hello,
thank you very much for the details
I’ll look at this and come back quickly with more information.
(I haven’t coded in a while, I did this last year)
I finally created an object where I store the image,
and a boolean to know if the image is loaded or not.
I just have one problem, Is it possible to pass an argument into the loadImage() callback function?
To be able to assign the boolean to the correct object by passing an id.
(there can be several loadImages happening at the same time…)
Because this doesn’t seem to work, I get an error message. “loadImage() was expecting function(p5.Image) for the second parameter,
received an empty variable instead.”
(the variable is not empty…)
and it launches the function directly without waiting for the loading to finish.
let imagesN = [];
function ImageN() {
this.image_file;
this.loaded = false;
}
function nextImageLoading() {
imagesN[next_id] = new ImageN();
imagesN[next_id].image_file = loadImage("images/" + data.c1[next_id[0]], nextImageLoaded(next_id));
}
function nextImageLoaded(_next_id) {
imagesN[_next_id].loaded = true;
}
or maybe there is another way to do it,
but it seemed good to me.
bulbx