Skip to content

Support for Top-Level Global Mode #7737

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
1 of 17 tasks
quinton-ashley opened this issue Apr 15, 2025 · 2 comments
Open
1 of 17 tasks

Support for Top-Level Global Mode #7737

quinton-ashley opened this issue Apr 15, 2025 · 2 comments

Comments

@quinton-ashley
Copy link
Contributor

Increasing access

Parity with an existing feature in Processing. Also improves ease of use in ESM sketches especially.

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
  • Internationalization
  • Friendly errors
  • Other (specify if possible)

Feature request details

Ideally p5 could support top-level global mode. createCanvas would be available globally and could be used to start p5 in top level global mode and create a canvas.

createCanvas(200, 200);
rect(20, 20, 50, 50);

There's a precedent for this in Processing. The Processing editor supports running sketches like this without a setup or draw method.

size(200, 200);
rect(20, 20, 50, 50);

There's also a broader precedent in the many programming languages that used to require some kind of main method, that now can run top-level code.

This would be especially useful in the context of improving usability of p5 in an ESM (issue #7670), because it'd make it easy to define functions like draw but also not have to prefix everything with the instance variable.

let p = new p5();

createCanvas(200, 200);

p.draw = () => {
 rect(mouseX, mouseY, 100, 100);
};

It's also just nice in general to be able to declare and define variables on the file level.

https://github.com/q5js/q5.js/wiki/Top%E2%80%90Level-Global-Mode

@ksen0
Copy link
Member

ksen0 commented Apr 25, 2025

Added this to a draft issue related to global mode documentation: p5.js 2.x 🌱🌳 (view) - draft because I am not yet sure what the scope of that issue should be. However, I recognize that this is not primarily a clarification/documentation issue; I think what would be most helpful is for other users (especially educators or people who recently learned p5.js) to weigh in. From other conversations, it seems to be that documenting of what's currently possible is higher priority than considering adding top-level global behavior, but this is also open for discussion.

@ksen0 ksen0 added this to the 2.x Anytime milestone Apr 25, 2025
@LalitNarayanYadav
Copy link

I strongly support adding top-level global mode to p5.js!

It would greatly improve usability for beginners, align p5.js more closely with Processing, and simplify writing ESM-compatible sketches.

Being able to call createCanvas() and draw shapes without needing a setup() function or manually managing instance variables would make learning and prototyping much easier, especially for educators and newcomers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Open for Discussion
Development

No branches or pull requests

4 participants
@ksen0 @quinton-ashley @LalitNarayanYadav and others