py5coding / py5generator

Meta-programming project that creates the py5 library code.
https://py5coding.org/
GNU General Public License v3.0
52 stars 13 forks source link

Suppress default drawing canvas when using sketch_portal() ? #33

Closed sawula closed 3 years ago

sawula commented 3 years ago

Running this in a local Jupyter notebook (in either notebook or lab mode) with py5 kernel generates two drawing canvases. On a positive note, the exit_sketch() button does get rid of the one that's not in the sketch_portal! This is not a problem in a web-served notebook.

image

hx2A commented 3 years ago

As I see it, there is only one drawing canvas, and that is the window that opens up on your screen. The sketch portal is more like a window that views the actual drawing canvas and is not a drawing canvas by itself. But I get what you are thinking about. Why do you have to see the sketch in two places?

Interestingly, there isn't much I can offer to change this. The reason why is intriguing.

Try running this sketch:

def setup():
    size(200, 200)

def draw():
    square(random_int(width), random_int(height), 10)
    if frame_count % 100 == 0:
        println(frame_count)

run_sketch()

Then run this to make the window disappear:

surface = get_surface()

surface.set_visible(False)

You will see the print statements continue to appear, even though the window is no longer visible. Run surface.set_visible(True) to make the window reappear; you will see a lot more squares on the screen. The sketch continued to run, even though it was not visible.

Now repeat the experiment with the P2D renderer. What happens? The print statements stop while the window is not visible. When you make it reappear, the Sketch will reset. The Sketch was unable to run while the window was not visible.

For whatever reason, Processing OpenGL Sketches need an actual window for the Sketch to actually run.

When you are using py5 through mybinder or Jupyter Hub, it is running on a server off in the cloud somewhere, not on your machine. Therefore, the window exists on that server, not your computer. That's why you don't see it open up on your monitor. It happens the server is using something called a virtual frame buffer (Xvfb) to provide an environment for the window. Xvfb is kind of like a fake monitor.

When developing notebooks for content that will eventually go on JupyterHub somewhere, you probably don't need to see or interact with the second window. There are a few things you can do if you don't want to look at it. You can minimize the window or move it off screen (either programmatically or just moving it with your mouse). You can also use the HIDDEN renderer (ie size(200, 200, HIDDEN). The HIDDEN renderer is the same as the default JAVA2D renderer except there is some extra code I added to keep the window invisible. It is because of the HIDDEN renderer that the magics like %%py5draw do not briefly open a window to create the image. However, if you are using an OpenGL renderer (with something like %%py5draw 200 200 -r P2D), a window will briefly appear, and there is nothing that can be done about that.

hx2A commented 3 years ago

@sawula Can I close this?

sawula commented 3 years ago

Yes, thanks

On Thu, Aug 19, 2021 at 11:38 AM hx2A @.***> wrote:

@sawula https://github.com/sawula Can I close this?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/hx2A/py5generator/issues/33#issuecomment-902018393, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA4VGJGXO5EDMAAHVTJTUL3T5UQP7ANCNFSM5BKRE6SQ .