Open nijatmursali opened 3 months ago
Welcome! 👋 Thanks for opening your first issue here! And to ensure the community is able to respond to your issue, please make sure to fill out the inputs in the issue forms. Thank you!
@nijatmursali I may not fully understand what you are trying to do here but I think the problem lies with img.collarSide = p5.loadImage(uri);
. JavaScript relies heavily on asynchronous operations, especially around IO which means that anything that loads things, including loadImage()
will run asynchronously. Which means loadImage()
don't actually finishes loading your image by the time the next line or more of your code is run.
The preload()
function p5.js provides is a way to hide that complexity but it only does so in the function itself. Using the loadImage()
function outside of preload()
will encounter the asynchronous problem mentioned above. It is recommended to use the callbacks of loadImage()
if you need to use it outside of preload()
. You can try to promisify the function if you wish to use async/await too.
Topic
Currently I'm working on a big project where I'm drawing the shoe model in canvas and using some functions to divide them into segments. Those segments are clickable in order to change the images depending on which and how many colors have been chosen. So, let say if we have selected 3 colors then my helper function will generate new image from those colors (or basically will change the colors of current image into new colors).
It works fine, but currently I have 6 segments in total and it does not really work good, so it is not really 100% reliable.
First, I would like to share some variables I have:
and my
sketch
function is like following:and two helper functions to
initializeDrawings
anddrawImages
are used to initialize the masks in the canvas and draw images usingp5.image()
function.Most importantly, I have
changeSegmentImage
where it changes the segment image, but first it getsimageUrl
to convert image to file, and then usingreplaceColors
I convert the image colors into new colors passed and createuri
from it and attach it to mask usingloadImage
.It works all good if I have only two segments visible, but if I have more segments (like 5-6) then it gets slower and actually does not load the image using
loadImage
.