Draw function, Processing, Python

Essentially, you need to identify what specifications need to change each time the sketch is drawn. Instead of representing those specifications within a while loop, use global variables to hold that information, so that it persists between the automatic calls of the draw() function. Then, within the draw() function, update those global variables to prepare for the next call of the draw() function. In a sense, the automatic calls to the draw() function will become a replacement for the iterations of your while loop.

The frameCount variable that already exists is global, and might be useful to you, but it appears that your sketch is complex enough to require additional global variables. For example, you have this:

        angle=0
        while angle < HALF_PI:
            angle += 0.01

Quite likely, one of your global variables will be angle. Make sure it is declared global wherever it is used within a function. Assign it an initial value in the setup() function, then make sure it gets updated each time draw() gets called. The updating does not necessarily need to be performed within the draw() function itself. Instead, it could be updated within a function that is called directly or indirectly by the draw() function.

EDITED on June 11, 2022 to correct a typographical error.

2 Likes