We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi, I use this code in processing to load images of a local path
PImage [] loadImages(String stub, String extension, int numImages)
{
PImage [] animals = new PImage[0];
for(int i =0; i < numImages; i++)
{
PImage img = loadImage(stub+i+extension);
if(img != null)
{
animals = (PImage [])append(animals,img);
}
else
{
break;
}
}
return animals;
}
And then calling this function above with this code:
animals = loadImages("animals/animal", ".jpg", numberLoadAnimals);
How can I traslate it to p5.js? Any help will be apreciate
SALUD...
Answers
I would try to help you out if the code you provided above was correct! [-(
OK you were right. I hope this time it will be more clear
pictures.pde
loadImages.pde
Leave a blank line either side of the code block. Blank lines are needed between paragraphs. I have done this for you in your first post above.
A little better, but still very incomplete Java Mode code! :-&
In JavaScript, loading heavy resources like images, sounds and videos don't happen at once.
What can be done is request the loading and specify a callback function which will be invoked when it's done.
In your case, you expect animals[] to be length = numberLoadAnimals when it's successful.
For each image loaded, it is push()ed into animals[]. Here's my attempt: :(|)
Hi GoToLoop
some question: getImage is the callback function, with quotes?
this my own attempt that.... doesnt works :((
And... how do you declare a new array in p5.js like this ?:
PImage [] animals = new PImage[0];
Thanks in advance SALUD...
I guess you didn't get the part that resources doesn't load immediately in JS:
http://p5js.org/reference/#/p5/loadImage
P5.JS doesn't provide any arrays, but the JS language itself! :-@
If we dunno its length, just an
[]
is enough to create an array.But it's more efficient doing this way:
Array(QUANTITY)
. *-:)My own work v2. What do you think about it?
Well, we can't run it b/c we don't have your image files! Anyhow, you tell us! ;;)
Is it working or not? W/o a callback for loadImage(), you take the risk of getting
null
in animalName! #-oWell... is it working but... not perfectly... :((
I have extend the sketch to include 9 photos and one of the problems is that if i use this code to load images, the first always an the second sametime does not appear, the same case with processing.js, perhaps is the lack of callback function :
And if I use some os your code, the photos appear but if i refresh the browser the photos change the position in particular the third line of photos, in this case i think i have badly use your code. Also i get this error: 2225: Uncaught TypeError: Cannot read property 'canvas' of undefined
Here you can see a live example scual.es Like always any help will be appreciate
The whole code:
My sample code doesn't even use image(). It only loads them w/ callback! [-(
However, I noticed that you don't clear the canvas w/ background()! Might be it... dunno! :-??
You've gotta understand that in JS, images and other similar resources take some time to load!
So while that process is taking place, the sketch advances and doesn't wait for it to get finished! :-SS
That's why we should check whether some p5.Image got added to our array before using image() on it! :-B
For example, before displaying
animals[5]
, we should check ifanimals.length > 5
just to make sure!1 more tip: @ #31
animals = Animal("animals/animal", ".jpg", numberOfAnimals);
Why are you reassigning animals. Wasn't it initialized w/
[]
at the beginning already?! :-?And since your Animal() returns animals @ #55, that ends up being the same as
animals = animals
! ~:>1 last advice: Don't capitalize variables as you did w/ Animal()!
Unless of course it's supposed to be a constructor for a "class". Please rename it to animal(). o->
beautiful code... =)
Hi GoToLoop
First of all I greatly appreciate your advices. This code runs perfectly in pure Processing, and... I thought it will be easy in p5.js and jump to the web but...
Well
_ I noticed that you don't clear the canvas w/ background()_
the boolean visualizePhoto make that the photos load only one time
_ before displaying animals[5], we should check if animals.length > 5 just to make sure_
Sorry, I dont know how to do that
the last two tips have been corrected
Current code:
All photos are already loaded by callbacks at the beginning and transfered to animals[] 1 by 1.
Display already loaded images via image() isn't the same as load images via loadImage()!
Since draw() is invoked @ 60 FPS by default, you end up image() them in a fraction of a second.
Rather relying on visualizePhoto, you coulda issued noLoop() once
currentPhoto == numberOfAnimals
.1st of all, seems like variables
h
¤tPhoto
are the same thing in your sketch. @-)Both represents animals[]'s current index. You should scrap 1 of them to make your sketch more concise! =:)
As mentioned, in order to avoid any unpleasant surprise of failing to image() a photo not loaded yet,
we've gotta check whether the current index we're about to display is less than animals.length.
Otherwise it'd mean that index wasn't push()ed to animals[] yet! In short, needs more time to get loaded! 8-X
Now some extra tips: If all fonts in your sketch is always
textSize(28)
or the ink is alwaysfill(0)
, etc.they should all go into setup(). No need to repeat it @ 60 FPS over & over inside draw(), right? ;;)
Hi GoToLoop
I have corrected the improves of your last comments but the problem remain. I think i am not trying to do any odd, just load severals images in a array and then display it.
what other options do we have?
Current code:
Since I can't run your code due to lack of images, I'm sorry I can't help you out further! 8-|
If page is refreshed, it should display those images at the same place since everything is re-initialized to their initial values! :(|)
Hi GoToLoop
This code with the images (and sounds) are in GITHUB, you can download it. If you have time of course, time is gold... Anyway, thank you very
https://github.com/CharlyPascual/P5/tree/master/gameLucasp5
I've just tested your "P5-master/gameLucasp5/index.html" in Firefox 33, QupZilla 1.8.4 & Opera 12.16.
And I didn't spot any issues when reloading the page w/ those animal photos! :-@
Hi GoToLoop
I feel a little bit stupid (don´t worry about that ) maybe I am missing out, but.. I disagree with you, if you refresh the browser the images change their position, sure, I have tested with FF33 and CHrome 39.
Of course all animal photo are showed but pay attention to the location of the photos, it change¡ not in all refreshing of the browser, but happends.
I have tested in local and in http://www.guarderia-nuevavida.org/scual/index.html
So sorry! Somehow I thought you meant the position images were placed after each page refresh! X_X
Turns out it's the order they're being displayed that's troubling you! (~~)
Since I've tested your sketch w/o a server, simply double-clicked the ".html" file, I had no issues about it!
Well, that's the very nature of async loading: 1st finished loading, 1st push()ed into animals[]!
As you've noticed, the speed each image takes to load varies every time, therefore their order changes! #-o
Easiest way of avoiding all that headache is relying on P5JS's preload() callback.
But I was afraid that feature would have problems in dealing w/ loadings from a loop + string substitutions.
Well, if you wanna take the risk, here's a preload() attempt I just did: >:)
Don´t worry GoToLoop, for me this thread have been a master-class¡¡
I have open another branch https://github.com/CharlyPascual/P5/tree/Preload/gameLucasp5
And tested the preload() also loading sound, now I think I can finish the game.
Thank you very much :-h