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

preload() not working as expected #132

Closed
jvfe opened this issue Nov 7, 2020 · 4 comments · Fixed by #196
Closed

preload() not working as expected #132

jvfe opened this issue Nov 7, 2020 · 4 comments · Fixed by #196
Labels

Comments

@jvfe
Copy link
Contributor

jvfe commented Nov 7, 2020

So I was following one of the examples on the p5js reference for playing with images. I couldn't get the basic preload() to work:

from pyp5js import *

img = None

def preload():
   global img
   img = loadImage('foto_qualquer.png')

def setup():
   img.loadPixels()
   # do something here

def draw():
   image(img, 0, 0, 100, 100)

I get an 'Uncaught TypeError: img is null' on line 11, which probably means that the sketch is not loading the image in the first place.

However the same doesn't happen if I load the image in setup():

from pyp5js import *

img = None

def setup():
    global img
    img = loadImage('foto_qualquer.png')
    img.loadPixels()
    # do something here

def draw():
    image(img, 0, 0, 100, 100)

Feel free to point out if I'm doing something wrong here, I'm not that familiar with p5js/processing yet.

@berinhard
Copy link
Owner

Thanks for opening this issue @jvfe! Probably pyp5js is configuring the preload function wrongly. I feel that we would have to overwrite the pt.js skecth's preload function the [same way we're doing with the setup and draw inside sketch_setup function.

Another alternative would be to handle the preload function the same way it does with the events functions.

@berinhard
Copy link
Owner

Hey @jvfe, we have a pyp5js group in telegram to talk about the project among other subjects. If you want to join us there, feel free to send me a message over twitter, telegram or over here, and I can send you an invite.

@berinhard
Copy link
Owner

@jvfe if you want to tackle this one, this is probably because we're overwriting the preload function in target sketches from both pyodide and transcrypt modes.

To fix this we should probably:

  1. Remove preload from pyodide's target sketch and transcrypt's pyp5js Javascript files;
  2. Add a def placeholder(): pass at pyodide's placeholder code;
  3. Add a def placeholder(): pass at transcrypt's target sketch;
  4. Update start_p5 in both modes to accept a preload function as parameter and use it to configure the sketch (Pyodide logic / Transcrypt logic)

@jvfe
Copy link
Contributor Author

jvfe commented Nov 2, 2021

@jvfe if you want to tackle this one, this is probably because we're overwriting the preload function in target sketches from both pyodide and transcrypt modes.

To fix this we should probably:

1. Remove `preload` from pyodide's target sketch and transcrypt's pyp5js Javascript files;

2. Add a `def placeholder(): pass` at [pyodide's placeholder code](https://github.com/berinhard/pyp5js/blob/develop/pyp5js/templates/pyodide/target_sketch.js.template#L1609);

3. Add a `def placeholder(): pass` at [transcrypt's target sketch](https://github.com/berinhard/pyp5js/blob/develop/pyp5js/templates/transcrypt/target_sketch.py.template#L7);

4. Update `start_p5` in both modes to accept a `preload` function as parameter and use it to configure the sketch ([Pyodide logic](https://github.com/berinhard/pyp5js/blob/develop/pyp5js/templates/pyodide/target_sketch.js.template#L1568) / [Transcrypt logic](https://github.com/berinhard/pyp5js/blob/develop/pyp5js/templates/transcrypt/pyp5js.py#L1192))

For sure, I'll get to work right away, thank you for the guidance. Will open a PR as soon as I have something.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants