Structure

setup()

p5.setup()

Called to setup initial sketch options.

The setup() function is run once when the program starts and is used to define initial environment options for the sketch.

draw()

p5.draw()

Continuously execute code defined inside.

The draw() function is called directly after setup() and all code inside is continuously executed until the program is stopped (using exit()) or no_loop() is called.

run()

p5.run(sketch_preload=None, sketch_setup=None, sketch_draw=None, frame_rate=60, mode='P2D', renderer='vispy')

Run a sketch.

if no sketch_setup and sketch_draw are specified, p5 automatically “finds” the user-defined setup and draw functions.

Parameters:
  • sketch_setup (function) – The setup function of the sketch (None by default.)
  • sketch_draw (function) – The draw function of the sketch (None by default.)
  • frame_rate (int \(\geq 1\)) – The target frame rate for the sketch.

exit()

p5.exit()

Exit the sketch.

exit() makes sure that necessary cleanup steps are performed before exiting the sketch.

loop()

p5.loop()

Make sure draw() is being called continuously.

loop() reverts the effects of no_loop() and allows draw() to be called continously again.

no_loop()

p5.no_loop()

Stop draw() from being continuously called.

By default, the sketch continuously calls draw() as long as it runs. Calling no_loop() stops draw() from being called the next time. Note that this only prevents execution of the code inside draw() and the user can manipulate the screen contents through event handlers like mouse_pressed(), etc.

redraw()

p5.redraw()

Call draw() once.

If looping has been disabled using no_loop(), redraw() will make sure that draw() is called exactly once.

push_style()

p5.push_style()

Save the current style settings and then restores them on exit.

The ‘style’ information consists of all the parameters controlled by the following functions (the ones indicated by an asterisks ‘*’ aren’t available yet):

  • background
  • fill, no_fill
  • stroke, no_stroke
  • rect_mode
  • ellipse_mode
  • shape_mode
  • color_mode
  • tint
  • (*) stroke_weight
  • (*) stroke_cap
  • (*) stroke_join
  • (*) image_mode
  • (*) text_align
  • (*) text_font
  • (*) text_mode
  • (*) text_size
  • (*) text_leading
  • emissive
  • specular
  • shininess
  • ambient
  • material

pop_style()

p5.pop_style()

Restores previously pushed style settings