processing / processing/p5.js

Namespaced mode

Open
#7,332 15 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Area:Core Feature Request
Dominant language
JavaScript
Stars
24k
Forks
3.8k
Avg merge
3d 16h
Merged PRs (30d)
25

Description

Increasing access

unsure

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

The current global and instance modes are not ideal for integrating p5 into larger web applications:

  • Global pollutes the namespace
  • Instance requires you to pass the sketch object to every class that wants to use the p5 library, making the code more complex.

It would be great for 2.0 to add a third instantiation mode that's more in line with how THREE, Babylon or other libraries work.

In this mode, p5 functions would be neatly contained in a single global object, getting us closer to the modular approach that's already underway. Lingdong explains it very well in his q5xjs library.

So instead of doing this:

class Rectangle {
  constructor(sketch) {
    this.sketch = sketch;
  }
  draw() {
    this.sketch.fill(255);
    this.sketch.rect(50, 50, 50, 50);
  }
}

let sketch = function(p) {
  let rectangle;
  p.setup = function() {
    p.createCanvas(100,100);
    rectangle = new Rectangle(p);
  };
  p.draw = function(){
    p.background(0);
    rectangle.draw();
  }
};

let myp5 = new p5(sketch);

we would do this:

class Rectangle {
  draw() {
    q5.fill(255);
    q5.rect(50, 50, 50, 50);
  }
}

let q5 = new p5();
let rectangle;

q5.setup = function(){
  q5.createCanvas(100,100);
  rectangle = new Rectangle();
}

q5.draw = function(){
  q5.background(0);
  rectangle.draw();
}

which seems like a very small gain but can lead to much more readable code as the application grows.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing p5.js instantiation through the existing global and instance modes, especially the new p5(sketch) and setup/draw entry points shown in the issue. Compare the proposed namespaced usage with the linked q5xjs library and determine the API and compatibility requirements before implementation. Done means a documented, tested third mode that keeps p5 functions under one object without requiring the sketch object to be passed through application classes.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
computer-graphics, frontend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.