Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

p5.brush can apparently become confused by multiple p5 instances #39

Open
gwhitney opened this issue Nov 19, 2024 · 1 comment
Open
Labels
enhancement New feature or request

Comments

@gwhitney
Copy link

I am creating a gallery of p5 images with many p5 instances on the same web page, several of which use p5.brush. Each instance dutifully calls brush.instance(sketch) in the sketch function it passes to the p5 constructor, and then calls brush.load() in its setup() method. However, I get one or more occurrences of RangeError: invalid array length when I load the page; it's occurring in genField() inside of _ensureReady(). More specifically, this.num_columns ends up as NaN.

What seems to be happening is that one p5 instance A is registering its blend methods (in the _registerMethods() function), and then before those methods get called by A, another instance B has called brush.instance(B) but that instance B has not yet called setup(), and then when blend() is called by the registered methods of A, the global variables _r and _inst are now pointing to B, which has no width property yet, and so the number of columns is being computed to be NaN.

In other words, it seems as though p5.brush is not actually designed to handle multiple p5 instances that are setting up asynchronously, because there is only one _r and one _inst variable in the module. Is that right, or is p5.brush supposed to work in this circumstance (and either I am doing something wrong or there still remains some bug in p5.brush handling multiple instances)?

If p5.brush is currently not designed to handle multiple p5 sketch instances, are you interested in extending p5.brush so that it is safe to use with multiple instances of p5 all running asynchronously? Presumably this would entail keeping multiple p5 instance references around, say in some collection of objects, and making sure that the proper instance is used in any given brush function (rather that just referring to a single global sketch via _r and _inst). Thanks for letting me know your thoughts.

@acamposuribe
Copy link
Owner

Hi @gwhitney!
Apologies for the super late answer.
You are indeed right. p5.brush cannot currently handle multiple instances simultaneously.
Over the last months I've been working on making p5.brush independent from p5, as a side project, and I have left this repository a bit outdated.
Would you be open to make necessary changes yourself through a request?
I can do it myself, but probably it will need to wait for a few more weeks

@acamposuribe acamposuribe added the enhancement New feature or request label Dec 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants