You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Event handler functions such as keyPressed or mouseDragged from p5.js doesn't require any parameter to work. But pyp5js is only working if the event paremeter is present in the function definition.
So, for example, running the following code with the demo editor raises an TypeError as the traceback describes:
Uncaught PythonError: Traceback (most recent call last):
File "<exec>", line 1595, in wrapper
File "<exec>", line 1583, in pre_draw
TypeError: keyPressed() takes 0 positional arguments but 1 was given
But, if we change the keyPressed to receive an argument, it works. So, the pyodide mode must be updated to not require this parameter in order for event functions to work.
The text was updated successfully, but these errors were encountered:
I have managed to find a work around for the issue by making a small modification to the wrapperContent constant in the target_sketch.js file. I modify the global_p5_injection function to:
defglobal_p5_injection(p5_sketch):
""" Injects the p5js's skecth instance as a global variable to setup and draw functions """defdecorator(f, event_function=False):
defwrapper(*args, **kwargs):
global_P5_INSTANCE_P5_INSTANCE=p5_sketchtry:
temp_output=pre_draw(_P5_INSTANCE, f, *args, **kwargs)
returntemp_outputexcept:
traceback_str=traceback.format_exc()
log_error_to_console(traceback_str)
defevent_wrapper(event):
wrapper()
ifevent_function:
returnevent_wrapperreturnwrapperreturndecorator
When the event_function parameter is passed, the decorator wraps the user's code in an outer function which accepts an argument (which p5 seems to expect). The start_p5 function is modified as follows to set event functions:
Event handler functions such as keyPressed or mouseDragged from p5.js doesn't require any parameter to work. But pyp5js is only working if the event paremeter is present in the function definition.
So, for example, running the following code with the demo editor raises an
TypeError
as the traceback describes:Sample code
Traceback error
But, if we change the keyPressed to receive an argument, it works. So, the pyodide mode must be updated to not require this parameter in order for event functions to work.
The text was updated successfully, but these errors were encountered: