Skip to content

p5.js 2.0 Updates #117

@limzykenneth

Description

@limzykenneth

Hi @ffd8, moving the discussion from the p5.js repo over here. I have tested out P5Live locally and as mentioned in the issue, it seems the main thing not working is overwriting values of global p5.js values such as mouseX, mouseY, or frameCount. Due to the way these values are attached to the global object, the way it works now is that if the user overwrites that value in global mode, we assume the user are doing it intentionally, ie. wanting to use a variable with name frameCount and don't want p5.js to update it or for their update to the value change p5.js behavior. I think this approach leads to less unexpected behavior in case the user did accidentally or intentionally use these global variable names.

For P5Live however, since the intention is to change these value to affect p5.js behavior I can see why it is not working. I have done some tests and have found a potential solution. While modifying the global variable does not work, modifying the instance value still works:

// Instead of 
frameCount = 1;

// This works
p5.instance.frameCount = 1;

However, from skimming through the code where I can, I can't find a good point to obtain p5.instance from the iframe window object. What I would suggest is potentially to do a slight refactor to use window.postMessage instead to communicate from the main process to the iframe process, that way things are more firewalled and separated and the code within the iframe can be written as a p5.js addon, giving it a bit more structure.

In the iframe's own code, it can listen through window.addEventListener("message", () => {}); and depending on message either modify the p5 instance value as needed or do any other operation from there, ie. using message passing for this semantically multithreaded application instead of direct variable access.

Let me know if this make sense and I'm happy to help with implementation if needed. 😄

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions