Skip to content

Add Event Listeners for Lifecycle Methods (setup and draw) #6422

Closed
@SableRaf

Description

@SableRaf

Increasing Access

By allowing websites to synchronize with the lifecycle of p5.js sketches, users will experience smoother transitions and less visual jarring. This can be especially beneficial for individuals sensitive to abrupt visual changes.

Most appropriate sub-area of p5.js?

  • Accessibility
  • Color
  • Core/Environment/Rendering
  • Data
  • DOM
  • Events
  • Image
  • IO
  • Math
  • Typography
  • Utilities
  • WebGL
  • Build Process
  • Unit Testing
  • Internalization
  • Friendly Errors
  • Other (specify if possible)

Feature request details

Problem Description

Currently, there isn't a direct way to listen to lifecycle events such as when setup starts or ends, and when draw starts or ends. This can be beneficial for scenarios where users want to synchronize other animations or events on their page with the p5.js sketch lifecycle, like fading in a sketch once it's fully initialized.

Proposed Solution

Provide event listeners/hooks that can be attached to specific lifecycle moments:

  • preloadStarted: Triggered when the preload function begins execution.
  • preloadEnded: Triggered when the preload function completes.
  • setupStarted: Triggered when the setup function begins execution.
  • setupEnded: Triggered when the setup function completes.
  • drawStarted: Triggered when the draw function begins execution.
  • drawEnded: Triggered when the draw function completes.

For instance, in a slideshow containing multiple p5 sketches, we could listen to the setupEnded event on p5.js to smoothly fade in the next sketch once it has been initialized by p5.js and starts rendering on the canvas.

Alternatives considered

While users can currently use custom flags or counters to try and determine when certain functions start or end, having dedicated events would standardize this approach and make it more accessible and reliable.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions